Commit | Line | Data |
---|---|---|
d0aeaf5a DF |
1 | /* |
2 | * Copyright (c) 1983 Regents of the University of California. | |
9e678aa5 KB |
3 | * All rights reserved. |
4 | * | |
836fe169 | 5 | * %sccs.include.redist.c% |
d0aeaf5a DF |
6 | */ |
7 | ||
8 | #ifndef lint | |
9 | char copyright[] = | |
10 | "@(#) Copyright (c) 1983 Regents of the University of California.\n\ | |
11 | All rights reserved.\n"; | |
9e678aa5 | 12 | #endif /* not lint */ |
d0aeaf5a | 13 | |
963ce42e | 14 | #ifndef lint |
836fe169 | 15 | static char sccsid[] = "@(#)talkd.c 5.7 (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 | */ |
dabe9d2f MK |
24 | #include <stdio.h> |
25 | #include <errno.h> | |
963ce42e | 26 | #include <signal.h> |
595cdd5e | 27 | #include <syslog.h> |
dabe9d2f | 28 | |
595cdd5e | 29 | #include <protocols/talkd.h> |
7abf8d65 | 30 | #include <paths.h> |
dabe9d2f | 31 | |
963ce42e MK |
32 | CTL_MSG request; |
33 | CTL_RESPONSE response; | |
dabe9d2f | 34 | |
963ce42e MK |
35 | int sockt; |
36 | int debug = 0; | |
37 | int timeout(); | |
38 | long lastmsgtime; | |
dabe9d2f | 39 | |
963ce42e | 40 | char hostname[32]; |
dabe9d2f | 41 | |
963ce42e MK |
42 | #define TIMEOUT 30 |
43 | #define MAXIDLE 120 | |
dabe9d2f | 44 | |
963ce42e MK |
45 | main(argc, argv) |
46 | int argc; | |
47 | char *argv[]; | |
48 | { | |
595cdd5e KM |
49 | register CTL_MSG *mp = &request; |
50 | int cc; | |
dabe9d2f | 51 | |
963ce42e | 52 | if (getuid()) { |
595cdd5e | 53 | fprintf(stderr, "%s: getuid: not super-user", argv[0]); |
963ce42e | 54 | exit(1); |
dabe9d2f | 55 | } |
595cdd5e KM |
56 | openlog("talkd", LOG_PID, LOG_DAEMON); |
57 | if (gethostname(hostname, sizeof (hostname) - 1) < 0) { | |
58 | syslog(LOG_ERR, "gethostname: %m"); | |
59 | _exit(1); | |
60 | } | |
7abf8d65 KB |
61 | if (chdir(_PATH_DEV) < 0) { |
62 | syslog(LOG_ERR, "chdir: %s: %m", _PATH_DEV); | |
595cdd5e KM |
63 | _exit(1); |
64 | } | |
65 | if (argc > 1 && strcmp(argv[1], "-d") == 0) | |
66 | debug = 1; | |
963ce42e MK |
67 | signal(SIGALRM, timeout); |
68 | alarm(TIMEOUT); | |
69 | for (;;) { | |
70 | extern int errno; | |
71 | ||
595cdd5e KM |
72 | cc = recv(0, (char *)mp, sizeof (*mp), 0); |
73 | if (cc != sizeof (*mp)) { | |
963ce42e | 74 | if (cc < 0 && errno != EINTR) |
595cdd5e | 75 | syslog(LOG_WARNING, "recv: %m"); |
963ce42e MK |
76 | continue; |
77 | } | |
78 | lastmsgtime = time(0); | |
595cdd5e | 79 | process_request(mp, &response); |
dabe9d2f | 80 | /* can block here, is this what I want? */ |
595cdd5e | 81 | cc = sendto(sockt, (char *)&response, |
2f497442 MK |
82 | sizeof (response), 0, (struct sockaddr *)&mp->ctl_addr, |
83 | sizeof (mp->ctl_addr)); | |
595cdd5e KM |
84 | if (cc != sizeof (response)) |
85 | syslog(LOG_WARNING, "sendto: %m"); | |
dabe9d2f | 86 | } |
dabe9d2f MK |
87 | } |
88 | ||
963ce42e | 89 | timeout() |
dabe9d2f | 90 | { |
dabe9d2f | 91 | |
963ce42e | 92 | if (time(0) - lastmsgtime >= MAXIDLE) |
595cdd5e | 93 | _exit(0); |
963ce42e | 94 | alarm(TIMEOUT); |
dabe9d2f | 95 | } |