4 * A pale imitation of syslogd. Most notably, doesn't write anything
5 * anywhere except possibly back to syslogd.
18 #include <sys/socket.h>
22 static int we_own_log=0;
23 static char **buffer=NULL;
24 static int buflines=0;
29 struct sockaddr_un addr;
33 bzero(&addr,sizeof(addr));
34 addr.sun_family = AF_LOCAL;
35 strncpy(addr.sun_path,_PATH_LOG,sizeof(addr.sun_path)-1);
36 /* wait for klogd to hit syslog */
38 sock = socket(AF_LOCAL, SOCK_STREAM,0);
39 conn=connect(sock,(struct sockaddr *) &addr,sizeof(addr));
41 if (!conn) write(sock,buffer[x],strlen(buffer[x])+1);
47 void cleanup(int exitcode) {
48 /* If we own the log, unlink it before trying to free our buffer.
49 * Otherwise, sending the buffer to /dev/log doesn't make much sense.... */
54 /* Don't try to free buffer if we were called from a signal handler */
56 if (buffer) freeBuffer();
62 void runDaemon(int sock) {
63 struct sockaddr_un addr;
64 int x,len,addrlen,recvsock,done=0;
70 /* try not to leave stale sockets lying around */
71 /* Hopefully, we won't actually get any of these */
72 signal(SIGHUP,cleanup);
73 signal(SIGINT,cleanup);
74 signal(SIGQUIT,cleanup);
75 signal(SIGILL,cleanup);
76 signal(SIGABRT,cleanup);
77 signal(SIGFPE,cleanup);
78 signal(SIGSEGV,cleanup);
79 signal(SIGPIPE,cleanup);
80 signal(SIGBUS,cleanup);
81 signal(SIGTERM,cleanup);
83 /* Get stat info on /dev/log so we can later check to make sure we
88 pfds.events = POLLIN|POLLPRI;
89 if ( ( (x=poll(&pfds,1,500))==-1) && errno !=EINTR) {
93 if ( (x>0) && pfds.revents & (POLLIN | POLLPRI)) {
94 message = calloc(8192,sizeof(char));
95 recvsock = accept(sock,(struct sockaddr *) &addr, &addrlen);
96 len = read(recvsock,message,8192);
98 buffer = realloc(buffer,(buflines+1)*sizeof(char *));
100 buffer = malloc(sizeof(char *));
102 message[strlen(message)]='\n';
103 buffer[buflines]=message;
112 if ( (x>0) && ( pfds.revents & (POLLHUP | POLLNVAL)) )
114 /* Check to see if syslogd's yanked our socket out from under us */
115 if ( (stat(_PATH_LOG,&s2)!=0) ||
116 (s1.st_ino != s2.st_ino ) || (s1.st_ctime != s2.st_ctime) ||
117 (s1.st_mtime != s2.st_mtime) || (s1.st_atime != s2.st_atime) ) {
125 int main(int argc, char **argv) {
126 struct sockaddr_un addr;
130 /* option processing made simple... */
133 sock = open("/dev/null",O_RDWR);
138 bzero(&addr, sizeof(addr));
139 addr.sun_family = AF_LOCAL;
140 strncpy(addr.sun_path,_PATH_LOG,sizeof(addr.sun_path)-1);
141 sock = socket(AF_LOCAL, SOCK_STREAM,0);
143 /* Bind socket before forking, so we know if the server started */
144 if (!bind(sock,(struct sockaddr *) &addr, sizeof(addr))) {
147 if ((pid=fork())==-1) {
155 /* shouldn't get back here... */