aboutsummaryrefslogtreecommitdiffstats
path: root/tools/misc/xen_log.c
blob: 29f7d4c050076530ad3fe895eb9bd17de7684bcb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <stdlib.h>
#include <syslog.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include <string.h>

#include "dom0_defs.h"
#include "mem_defs.h"

#define SILENT_ERRORS_FROM_XEN
#define SYSLOG 1
#define SYSLOGTO LOG_LOCAL5

int logoutput;

void stripit(char *str)
{
    int i;
    for ( i = 0; str[i]; i++ )
        if ( (str[i] == '\n') || (str[i] == '\r') ) 
            str[i] = '\0';
}

void errexit(char *str)
{
    if ( logoutput == SYSLOG )
    {
        stripit(str);
        syslog(LOG_ERR, "%s failed: %d (%m)", str, errno);
    } 
    else 
    {
        printf("%s", str);
    }
    exit(1);
}

void log(char *str)
{
    if ( logoutput == SYSLOG )
    {
        stripit(str);
        syslog(LOG_INFO, "%s", str);
    } 
    else 
    {
        printf("%s", str);
    }
}

void process(void)
{
    dom0_op_t op;
    unsigned char buf[208], obuf[224];
    struct sockaddr_in addr, from;
    int fromlen = sizeof(from);
    int len, fd = socket(PF_INET, SOCK_DGRAM, 0);
    unsigned short int lastport = 0, curport = 0;
    
    if ( fd < 0 )
        errexit("could not open datagram socket");

    memset(&addr, 0, sizeof(addr));
    addr.sin_addr.s_addr = htonl(0xa9fe0100); /* 169.254.1.0 */
    addr.sin_port = htons(666);
    addr.sin_family = AF_INET;

    if ( bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0 )
        errexit("could not bind to local address and port");

    op.cmd = DOM0_GETDOMAININFO;

    while ( (len = recvfrom(fd, buf, sizeof(buf), 0,
                            (struct sockaddr *)&from, &fromlen)) >= 0 )
    {
        curport = ntohs(from.sin_port);
        if ( lastport != curport )
        {
            op.u.getdomaininfo.domain = (int)curport;
            if ( do_dom0_op(&op) < 0 )
                log("Error resolving domain name\n");
            else
                lastport = curport;
        }

        sprintf(obuf, "[%s] %s", op.u.getdomaininfo.name, buf);
        log(obuf);

        fromlen = sizeof(from);
    }
}

void closeall(int fd)
{
    int fdlimit = sysconf(_SC_OPEN_MAX);

    while (fd < fdlimit)
        close(fd++);
}

int daemon(int nochdir, int noclose)
{
    switch (fork())
    {
    case 0:  break;
    case -1: return -1;
    default: _exit(0);
    }

    if (setsid() < 0)
        return -1;

    switch (fork())
    {
    case 0:  break;
    case -1: return -1;
    default: _exit(0);
    }

    if (!nochdir)
        chdir("/");

    if (!noclose)
    {
        closeall(0);
        open("/dev/null",O_RDWR);
        dup(0); dup(0);
    }

    return 0;
}

int main(int argc, char **argv)
{
    int c;

    logoutput = 0;
    opterr = 0;

    while ( (c = getopt (argc, argv, "dh")) != -1 )
    {
        switch(c)
        {
        case 'd':
            logoutput = SYSLOG;
            if (daemon(0,0) < 0)
            {
                errno = 2;
                errexit("daemon");
            } else {
                openlog("xen_log", LOG_PID, SYSLOGTO);
            }
            break;
        case 'h':
            printf("Usage: xen_log [options]\n");
            printf("Capture and display output of xen domains.\n\n");
            printf("  -d       Daemonize and send output to syslog.\n");
            exit(0);
            break;
        }
    }

    process();

    return 0;
}