xref: /csrg-svn/old/implogd/implogd.c (revision 21139)
1*21139Sdist /*
2*21139Sdist  * Copyright (c) 1983 Regents of the University of California.
3*21139Sdist  * All rights reserved.  The Berkeley software License Agreement
4*21139Sdist  * specifies the terms and conditions for redistribution.
5*21139Sdist  */
6*21139Sdist 
79198Ssam #ifndef lint
8*21139Sdist char copyright[] =
9*21139Sdist "@(#) Copyright (c) 1983 Regents of the University of California.\n\
10*21139Sdist  All rights reserved.\n";
11*21139Sdist #endif not lint
126480Ssam 
13*21139Sdist #ifndef lint
14*21139Sdist static char sccsid[] = "@(#)implogd.c	5.1 (Berkeley) 05/28/85";
15*21139Sdist #endif not lint
16*21139Sdist 
176480Ssam #include <sgtty.h>
189196Ssam 
1913601Ssam #include <sys/time.h>
209251Ssam #include <sys/param.h>
216480Ssam #include <sys/socket.h>
229251Ssam #include <sys/file.h>
236480Ssam 
249196Ssam #include <netinet/in.h>
259251Ssam #include <netimp/if_imp.h>
269196Ssam 
276480Ssam #define	LOGFILE	"/usr/adm/implog"
286480Ssam 
296480Ssam u_char	request[1024];
306480Ssam int	marktime();
316480Ssam int	options;
326480Ssam extern	int errno;
336480Ssam int	log;
346480Ssam 
356480Ssam /*
366480Ssam  * Socket address, internet style, with
376480Ssam  * unused space taken by timestamp and packet
386480Ssam  * size.
396480Ssam  */
406480Ssam struct sockstamp {
416480Ssam 	short	sin_family;
426480Ssam 	u_short	sin_port;
436480Ssam 	struct	in_addr sin_addr;
446480Ssam 	time_t	sin_time;
459196Ssam 	int	sin_len;
466480Ssam };
476480Ssam 
486480Ssam main(argc, argv)
496480Ssam 	char *argv[];
506480Ssam {
519196Ssam 	int s;
526480Ssam 	time_t t;
539196Ssam 	struct sockstamp from;
546480Ssam 
556480Ssam 	argc--, argv++;
566480Ssam 	if (argc > 0 && !strcmp(argv[0], "-d"))
576480Ssam 		options |= SO_DEBUG;
588457Ssam #ifndef DEBUG
598457Ssam 	if (fork())
608457Ssam 		exit(0);
618457Ssam 	for (s = 0; s < 10; s++)
628457Ssam 		(void) close(t);
638457Ssam 	(void) open("/", 0);
648457Ssam 	(void) dup2(0, 1);
658457Ssam 	(void) dup2(0, 2);
668457Ssam 	{ int tt = open("/dev/tty", 2);
678457Ssam 	  if (tt > 0) {
688457Ssam 		ioctl(tt, TIOCNOTTY, 0);
698457Ssam 		close(tt);
708457Ssam 	  }
716480Ssam 	}
728457Ssam #endif
7313037Ssam 	log = open(LOGFILE, O_CREAT|O_WRONLY|O_APPEND, 0644);
749251Ssam 	if (log < 0) {
759251Ssam 		perror("implogd: open");
766480Ssam 		exit(1);
779251Ssam 	}
786480Ssam 	from.sin_time = time(0);
799196Ssam 	from.sin_len = sizeof (time_t);
809196Ssam 	write(log, (char *)&from, sizeof (from));
819251Ssam 	while ((s = socket(AF_IMPLINK, SOCK_RAW, 0, 0)) < 0) {
829251Ssam 		perror("implogd: socket");
836480Ssam 		sleep(5);
846480Ssam 	}
856480Ssam 	for (;;) {
8612237Ssam 		int fromlen = sizeof (from), len;
879196Ssam 
8812237Ssam 		len = recvfrom(s, request, sizeof (request), 0,
8912237Ssam 			&from, &fromlen);
9012237Ssam 		if (len < 0) {
919251Ssam 			perror("implogd: recvfrom");
926480Ssam 			continue;
939251Ssam 		}
9412237Ssam 		if (len == 0 || len > IMPMTU)	/* sanity */
956480Ssam 			continue;
969196Ssam 		from.sin_len = len;
976480Ssam 		from.sin_time = time(0);
989196Ssam 		write(log, (char *)&from, sizeof (from));
999196Ssam 		write(log, request, len);
1006480Ssam 	}
1016480Ssam 	/*NOTREACHED*/
1026480Ssam }
103