xref: /csrg-svn/old/implogd/implogd.c (revision 29429)
1 /*
2  * Copyright (c) 1983 Regents of the University of California.
3  * All rights reserved.  The Berkeley software License Agreement
4  * specifies the terms and conditions for redistribution.
5  */
6 
7 #ifndef lint
8 char copyright[] =
9 "@(#) Copyright (c) 1983 Regents of the University of California.\n\
10  All rights reserved.\n";
11 #endif not lint
12 
13 #ifndef lint
14 static char sccsid[] = "@(#)implogd.c	5.3 (Berkeley) 06/25/86";
15 #endif not lint
16 
17 #include <sgtty.h>
18 
19 #include <sys/time.h>
20 #include <sys/param.h>
21 #include <sys/socket.h>
22 #include <sys/syslog.h>
23 #include <sys/file.h>
24 
25 #include <netinet/in.h>
26 #include <netimp/if_imp.h>
27 
28 #define	LOGFILE	"/usr/adm/implog"
29 
30 u_char	request[1024];
31 int	marktime();
32 int	options;
33 extern	int errno;
34 int	log;
35 
36 /*
37  * Socket address, internet style, with
38  * unused space taken by timestamp and packet
39  * size.
40  */
41 struct sockstamp {
42 	short	sin_family;
43 	u_short	sin_port;
44 	struct	in_addr sin_addr;
45 	time_t	sin_time;
46 	int	sin_len;
47 };
48 
49 main(argc, argv)
50 	char *argv[];
51 {
52 	int i, s;
53 	time_t t;
54 	struct sockstamp from;
55 
56 	argc--, argv++;
57 	openlog("implogd", LOG_PID | LOG_ODELAY, LOG_DAEMON);
58 	if (argc > 0 && !strcmp(argv[0], "-d"))
59 		options |= SO_DEBUG;
60 	log = open(LOGFILE, O_CREAT|O_WRONLY|O_APPEND, 0644);
61 	if (log < 0) {
62 		syslog(LOG_ERR, "%s: %m\n", LOGFILE);
63 		perror("implogd: open");
64 		exit(1);
65 	}
66 	from.sin_time = time(0);
67 	from.sin_len = sizeof (time_t);
68 	write(log, (char *)&from, sizeof (from));
69 	if ((s = socket(AF_IMPLINK, SOCK_RAW, 0)) < 0) {
70 		syslog(LOG_ERR, "socket: %m\n");
71 		perror("implogd: socket");
72 		exit(5);
73 	}
74 #ifndef DEBUG
75 	if (fork())
76 		exit(0);
77 	for (i = 0; i < 10; i++)
78 		if (i != log && i != s)
79 			(void) close(i);
80 	(void) open("/", 0);
81 	(void) dup2(0, 1);
82 	(void) dup2(0, 2);
83 	{ int tt = open("/dev/tty", 2);
84 	  if (tt > 0) {
85 		ioctl(tt, TIOCNOTTY, 0);
86 		close(tt);
87 	  }
88 	}
89 #endif
90 	for (;;) {
91 		int fromlen = sizeof (from), len;
92 
93 		len = recvfrom(s, request, sizeof (request), 0,
94 			&from, &fromlen);
95 		if (len < 0) {
96 			syslog(LOG_ERR, "recvfrom: %m\n");
97 			perror("implogd: recvfrom");
98 			continue;
99 		}
100 		if (len == 0 || len > IMPMTU)	/* sanity */
101 			continue;
102 		from.sin_len = len;
103 		from.sin_time = time(0);
104 		write(log, (char *)&from, sizeof (from));
105 		write(log, request, len);
106 	}
107 	/*NOTREACHED*/
108 }
109