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