xref: /csrg-svn/old/implogd/implogd.c (revision 33456)
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.4 (Berkeley) 02/08/88";
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 <net/if.h>
26 
27 #include <netinet/in.h>
28 #include <netimp/if_imp.h>
29 
30 #define	LOGFILE	"/usr/adm/implog"
31 
32 u_char	request[1024];
33 int	marktime();
34 int	options;
35 extern	int errno;
36 int	log;
37 
38 /*
39  * Socket address, internet style, with
40  * unused space taken by timestamp and packet
41  * size.
42  */
43 struct sockstamp {
44 	short	sin_family;
45 	u_short	sin_port;
46 	struct	in_addr sin_addr;
47 	time_t	sin_time;
48 	int	sin_len;
49 };
50 
51 main(argc, argv)
52 	char *argv[];
53 {
54 	int i, s;
55 	time_t t;
56 	struct sockstamp from;
57 
58 	argc--, argv++;
59 	openlog("implogd", LOG_PID | LOG_ODELAY, LOG_DAEMON);
60 	if (argc > 0 && !strcmp(argv[0], "-d"))
61 		options |= SO_DEBUG;
62 	log = open(LOGFILE, O_CREAT|O_WRONLY|O_APPEND, 0644);
63 	if (log < 0) {
64 		syslog(LOG_ERR, "%s: %m\n", LOGFILE);
65 		perror("implogd: open");
66 		exit(1);
67 	}
68 	from.sin_time = time(0);
69 	from.sin_len = sizeof (time_t);
70 	write(log, (char *)&from, sizeof (from));
71 	if ((s = socket(AF_IMPLINK, SOCK_RAW, 0)) < 0) {
72 		syslog(LOG_ERR, "socket: %m\n");
73 		perror("implogd: socket");
74 		exit(5);
75 	}
76 #ifndef DEBUG
77 	if (fork())
78 		exit(0);
79 	for (i = 0; i < 10; i++)
80 		if (i != log && i != s)
81 			(void) close(i);
82 	(void) open("/", 0);
83 	(void) dup2(0, 1);
84 	(void) dup2(0, 2);
85 	{ int tt = open("/dev/tty", 2);
86 	  if (tt > 0) {
87 		ioctl(tt, TIOCNOTTY, 0);
88 		close(tt);
89 	  }
90 	}
91 #endif
92 	for (;;) {
93 		int fromlen = sizeof (from), len;
94 
95 		len = recvfrom(s, request, sizeof (request), 0,
96 			&from, &fromlen);
97 		if (len < 0) {
98 			syslog(LOG_ERR, "recvfrom: %m\n");
99 			perror("implogd: recvfrom");
100 			continue;
101 		}
102 		if (len == 0 || len > IMPMTU)	/* sanity */
103 			continue;
104 		from.sin_len = len;
105 		from.sin_time = time(0);
106 		write(log, (char *)&from, sizeof (from));
107 		write(log, request, len);
108 	}
109 	/*NOTREACHED*/
110 }
111