Commit | Line | Data |
---|---|---|
d0aeaf5a | 1 | /* |
517d1f5c KB |
2 | * Copyright (c) 1983, 1993 |
3 | * The Regents of the University of California. All rights reserved. | |
9e678aa5 | 4 | * |
836fe169 | 5 | * %sccs.include.redist.c% |
d0aeaf5a DF |
6 | */ |
7 | ||
8 | #ifndef lint | |
517d1f5c KB |
9 | static char copyright[] = |
10 | "@(#) Copyright (c) 1983, 1993\n\ | |
11 | The Regents of the University of California. All rights reserved.\n"; | |
9e678aa5 | 12 | #endif /* not lint */ |
d0aeaf5a | 13 | |
963ce42e | 14 | #ifndef lint |
517d1f5c | 15 | static char sccsid[] = "@(#)talkd.c 8.1 (Berkeley) %G%"; |
9e678aa5 | 16 | #endif /* not lint */ |
dabe9d2f | 17 | |
963ce42e MK |
18 | /* |
19 | * The top level of the daemon, the format is heavily borrowed | |
20 | * from rwhod.c. Basically: find out who and where you are; | |
21 | * disconnect all descriptors and ttys, and then endless | |
22 | * loop on waiting for and processing requests | |
dabe9d2f | 23 | */ |
b26f7bc4 KB |
24 | #include <sys/types.h> |
25 | #include <sys/socket.h> | |
26 | #include <protocols/talkd.h> | |
963ce42e | 27 | #include <signal.h> |
595cdd5e | 28 | #include <syslog.h> |
b26f7bc4 KB |
29 | #include <time.h> |
30 | #include <errno.h> | |
31 | #include <unistd.h> | |
32 | #include <stdio.h> | |
33 | #include <stdlib.h> | |
34 | #include <string.h> | |
7abf8d65 | 35 | #include <paths.h> |
dabe9d2f | 36 | |
963ce42e MK |
37 | CTL_MSG request; |
38 | CTL_RESPONSE response; | |
dabe9d2f | 39 | |
963ce42e MK |
40 | int sockt; |
41 | int debug = 0; | |
b26f7bc4 | 42 | void timeout(); |
963ce42e | 43 | long lastmsgtime; |
dabe9d2f | 44 | |
963ce42e | 45 | char hostname[32]; |
dabe9d2f | 46 | |
963ce42e MK |
47 | #define TIMEOUT 30 |
48 | #define MAXIDLE 120 | |
dabe9d2f | 49 | |
963ce42e MK |
50 | main(argc, argv) |
51 | int argc; | |
52 | char *argv[]; | |
53 | { | |
595cdd5e KM |
54 | register CTL_MSG *mp = &request; |
55 | int cc; | |
dabe9d2f | 56 | |
963ce42e | 57 | if (getuid()) { |
be14d31b | 58 | fprintf(stderr, "%s: getuid: not super-user\n", argv[0]); |
963ce42e | 59 | exit(1); |
dabe9d2f | 60 | } |
595cdd5e KM |
61 | openlog("talkd", LOG_PID, LOG_DAEMON); |
62 | if (gethostname(hostname, sizeof (hostname) - 1) < 0) { | |
63 | syslog(LOG_ERR, "gethostname: %m"); | |
64 | _exit(1); | |
65 | } | |
7abf8d65 KB |
66 | if (chdir(_PATH_DEV) < 0) { |
67 | syslog(LOG_ERR, "chdir: %s: %m", _PATH_DEV); | |
595cdd5e KM |
68 | _exit(1); |
69 | } | |
70 | if (argc > 1 && strcmp(argv[1], "-d") == 0) | |
71 | debug = 1; | |
963ce42e MK |
72 | signal(SIGALRM, timeout); |
73 | alarm(TIMEOUT); | |
74 | for (;;) { | |
75 | extern int errno; | |
76 | ||
595cdd5e KM |
77 | cc = recv(0, (char *)mp, sizeof (*mp), 0); |
78 | if (cc != sizeof (*mp)) { | |
963ce42e | 79 | if (cc < 0 && errno != EINTR) |
595cdd5e | 80 | syslog(LOG_WARNING, "recv: %m"); |
963ce42e MK |
81 | continue; |
82 | } | |
83 | lastmsgtime = time(0); | |
595cdd5e | 84 | process_request(mp, &response); |
dabe9d2f | 85 | /* can block here, is this what I want? */ |
595cdd5e | 86 | cc = sendto(sockt, (char *)&response, |
2f497442 MK |
87 | sizeof (response), 0, (struct sockaddr *)&mp->ctl_addr, |
88 | sizeof (mp->ctl_addr)); | |
595cdd5e KM |
89 | if (cc != sizeof (response)) |
90 | syslog(LOG_WARNING, "sendto: %m"); | |
dabe9d2f | 91 | } |
dabe9d2f MK |
92 | } |
93 | ||
b26f7bc4 | 94 | void |
963ce42e | 95 | timeout() |
dabe9d2f | 96 | { |
dabe9d2f | 97 | |
963ce42e | 98 | if (time(0) - lastmsgtime >= MAXIDLE) |
595cdd5e | 99 | _exit(0); |
963ce42e | 100 | alarm(TIMEOUT); |
dabe9d2f | 101 | } |