Commit | Line | Data |
---|---|---|
19538291 | 1 | /* |
df2b70fd MK |
2 | * Copyright (c) 1983,1988 Regents of the University of California. |
3 | * All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms are permitted | |
6 | * provided that this notice is preserved and that due credit is given | |
7 | * to the University of California at Berkeley. The name of the University | |
8 | * may not be used to endorse or promote products derived from this | |
9 | * software without specific prior written permission. This software | |
10 | * is provided ``as is'' without express or implied warranty. | |
19538291 DF |
11 | */ |
12 | ||
13 | #ifndef lint | |
14 | char copyright[] = | |
df2b70fd | 15 | "@(#) Copyright (c) 1983,1988 Regents of the University of California.\n\ |
19538291 DF |
16 | All rights reserved.\n"; |
17 | #endif not lint | |
18 | ||
28b5b7e7 | 19 | #ifndef lint |
df2b70fd | 20 | static char sccsid[] = "@(#)implogd.c 5.5 (Berkeley) %G%"; |
19538291 | 21 | #endif not lint |
6046cba1 | 22 | |
6046cba1 | 23 | #include <sgtty.h> |
56013160 | 24 | |
e2feeea9 | 25 | #include <sys/time.h> |
fdaa1466 | 26 | #include <sys/param.h> |
6046cba1 | 27 | #include <sys/socket.h> |
18148cbb | 28 | #include <sys/syslog.h> |
fdaa1466 | 29 | #include <sys/file.h> |
56013160 | 30 | |
ae92ba7a MK |
31 | #include <net/if.h> |
32 | ||
56013160 | 33 | #include <netinet/in.h> |
fdaa1466 | 34 | #include <netimp/if_imp.h> |
6046cba1 SL |
35 | |
36 | #define LOGFILE "/usr/adm/implog" | |
6046cba1 SL |
37 | |
38 | u_char request[1024]; | |
39 | int marktime(); | |
40 | int options; | |
41 | extern int errno; | |
42 | int log; | |
43 | ||
44 | /* | |
45 | * Socket address, internet style, with | |
46 | * unused space taken by timestamp and packet | |
47 | * size. | |
48 | */ | |
49 | struct sockstamp { | |
50 | short sin_family; | |
51 | u_short sin_port; | |
52 | struct in_addr sin_addr; | |
53 | time_t sin_time; | |
56013160 | 54 | int sin_len; |
6046cba1 SL |
55 | }; |
56 | ||
6046cba1 SL |
57 | main(argc, argv) |
58 | char *argv[]; | |
59 | { | |
18148cbb | 60 | int i, s; |
6046cba1 | 61 | time_t t; |
56013160 | 62 | struct sockstamp from; |
6046cba1 SL |
63 | |
64 | argc--, argv++; | |
18148cbb | 65 | openlog("implogd", LOG_PID | LOG_ODELAY, LOG_DAEMON); |
6046cba1 SL |
66 | if (argc > 0 && !strcmp(argv[0], "-d")) |
67 | options |= SO_DEBUG; | |
28026947 MK |
68 | log = open(LOGFILE, O_CREAT|O_WRONLY|O_APPEND, 0644); |
69 | if (log < 0) { | |
18148cbb | 70 | syslog(LOG_ERR, "%s: %m\n", LOGFILE); |
28026947 MK |
71 | perror("implogd: open"); |
72 | exit(1); | |
73 | } | |
74 | from.sin_time = time(0); | |
75 | from.sin_len = sizeof (time_t); | |
76 | write(log, (char *)&from, sizeof (from)); | |
77 | if ((s = socket(AF_IMPLINK, SOCK_RAW, 0)) < 0) { | |
18148cbb | 78 | syslog(LOG_ERR, "socket: %m\n"); |
28026947 MK |
79 | perror("implogd: socket"); |
80 | exit(5); | |
81 | } | |
16f092bf SL |
82 | #ifndef DEBUG |
83 | if (fork()) | |
84 | exit(0); | |
18148cbb MK |
85 | for (i = 0; i < 10; i++) |
86 | if (i != log && i != s) | |
87 | (void) close(i); | |
16f092bf SL |
88 | (void) open("/", 0); |
89 | (void) dup2(0, 1); | |
90 | (void) dup2(0, 2); | |
91 | { int tt = open("/dev/tty", 2); | |
92 | if (tt > 0) { | |
93 | ioctl(tt, TIOCNOTTY, 0); | |
94 | close(tt); | |
95 | } | |
6046cba1 | 96 | } |
16f092bf | 97 | #endif |
6046cba1 | 98 | for (;;) { |
fecd8ea3 | 99 | int fromlen = sizeof (from), len; |
56013160 | 100 | |
fecd8ea3 SL |
101 | len = recvfrom(s, request, sizeof (request), 0, |
102 | &from, &fromlen); | |
103 | if (len < 0) { | |
18148cbb | 104 | syslog(LOG_ERR, "recvfrom: %m\n"); |
fdaa1466 | 105 | perror("implogd: recvfrom"); |
6046cba1 | 106 | continue; |
fdaa1466 | 107 | } |
fecd8ea3 | 108 | if (len == 0 || len > IMPMTU) /* sanity */ |
6046cba1 | 109 | continue; |
56013160 | 110 | from.sin_len = len; |
6046cba1 | 111 | from.sin_time = time(0); |
56013160 SL |
112 | write(log, (char *)&from, sizeof (from)); |
113 | write(log, request, len); | |
6046cba1 SL |
114 | } |
115 | /*NOTREACHED*/ | |
116 | } |