summaryrefslogtreecommitdiff
blob: 00e993ddc71b90c4e6e1bbeb51e181cdeaaf1724 (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
/* Syslog listener for systems where /dev/log is a Unix domain socket. */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/time.h>
#include <pwd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/un.h>
#include <errno.h>

#define SIZE 1024
#define SOCKNAME "/dev/log"
#define RUNUSER "daemon"

int main(int argc, char **argv){
	char buf[SIZE];
	ssize_t r, w;
	struct sockaddr_un sa1;
	int s1;
	fd_set fs;
	int fd[2];
	pid_t child;
	struct passwd *userinfo;

	if(argc<2){
		fprintf(stderr, "No logger specified\n");
		exit(1);
	}

	/* Assume that it's ok to grab /dev/log. */
	if ((s1 = open("/proc/kmsg",O_RDONLY)) == -1) {
		perror("/proc/kmsg");
		exit(1);
	}
	/* Dropping privileges here 
	userinfo=getpwnam(RUNUSER);      
	if(userinfo){
		setuid(userinfo->pw_uid);
		seteuid(userinfo->pw_uid);
	} else {
		fprintf(stderr, "No such user: %s\n", RUNUSER);
		exit(1);
	}
*/
	if(pipe(fd)==-1){
		perror("pipe");
		exit(1);
	}
	if((child=fork())==-1){
		perror("fork");
		exit(1);
	}
	if(child==0){ /* We are the child */
		close(fd[1]); /* Child will only be reading from, not writing to parent */
		dup2(fd[0], 0);
		/* Execute logger */
		//execlp("multilog", "multilog", "/tmp/test", NULL);
		argv++;
		execvp(argv[0], argv);
	}

	/* We are Parent */
	close(fd[0]); /* Write to the child */

	for(;;) {
		r = read(s1, buf, SIZE);
		if (r < 0) {
			if (errno!=EINTR)
				perror("read");
			continue;
		}
		while (r) {
			w = write(fd[1], buf, r);
			if (w < 0) {
				if (errno!=EINTR)
					perror("write");
				exit(1);
			}
			r -= w;
		}
	}
}