ANSIfication
[unix-history] / usr / src / libexec / talkd / talkd.c
CommitLineData
d0aeaf5a
DF
1/*
2 * Copyright (c) 1983 Regents of the University of California.
9e678aa5
KB
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms are permitted
b8c620d6
KB
6 * provided that the above copyright notice and this paragraph are
7 * duplicated in all such forms and that any documentation,
8 * advertising materials, and other materials related to such
9 * distribution and use acknowledge that the software was developed
10 * by the University of California, Berkeley. The name of the
11 * University may not be used to endorse or promote products derived
12 * from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
d0aeaf5a
DF
16 */
17
18#ifndef lint
19char copyright[] =
20"@(#) Copyright (c) 1983 Regents of the University of California.\n\
21 All rights reserved.\n";
9e678aa5 22#endif /* not lint */
d0aeaf5a 23
963ce42e 24#ifndef lint
b8c620d6 25static char sccsid[] = "@(#)talkd.c 5.4 (Berkeley) %G%";
9e678aa5 26#endif /* not lint */
dabe9d2f 27
963ce42e
MK
28/*
29 * The top level of the daemon, the format is heavily borrowed
30 * from rwhod.c. Basically: find out who and where you are;
31 * disconnect all descriptors and ttys, and then endless
32 * loop on waiting for and processing requests
dabe9d2f 33 */
dabe9d2f
MK
34#include <stdio.h>
35#include <errno.h>
963ce42e 36#include <signal.h>
595cdd5e 37#include <syslog.h>
dabe9d2f 38
595cdd5e 39#include <protocols/talkd.h>
dabe9d2f 40
963ce42e
MK
41CTL_MSG request;
42CTL_RESPONSE response;
dabe9d2f 43
963ce42e
MK
44int sockt;
45int debug = 0;
46int timeout();
47long lastmsgtime;
dabe9d2f 48
963ce42e 49char hostname[32];
dabe9d2f 50
963ce42e
MK
51#define TIMEOUT 30
52#define MAXIDLE 120
dabe9d2f 53
963ce42e
MK
54main(argc, argv)
55 int argc;
56 char *argv[];
57{
595cdd5e
KM
58 register CTL_MSG *mp = &request;
59 int cc;
dabe9d2f 60
963ce42e 61 if (getuid()) {
595cdd5e 62 fprintf(stderr, "%s: getuid: not super-user", argv[0]);
963ce42e 63 exit(1);
dabe9d2f 64 }
595cdd5e
KM
65 openlog("talkd", LOG_PID, LOG_DAEMON);
66 if (gethostname(hostname, sizeof (hostname) - 1) < 0) {
67 syslog(LOG_ERR, "gethostname: %m");
68 _exit(1);
69 }
70 if (chdir("/dev") < 0) {
71 syslog(LOG_ERR, "chdir: /dev: %m");
72 _exit(1);
73 }
74 if (argc > 1 && strcmp(argv[1], "-d") == 0)
75 debug = 1;
963ce42e
MK
76 signal(SIGALRM, timeout);
77 alarm(TIMEOUT);
78 for (;;) {
79 extern int errno;
80
595cdd5e
KM
81 cc = recv(0, (char *)mp, sizeof (*mp), 0);
82 if (cc != sizeof (*mp)) {
963ce42e 83 if (cc < 0 && errno != EINTR)
595cdd5e 84 syslog(LOG_WARNING, "recv: %m");
963ce42e
MK
85 continue;
86 }
87 lastmsgtime = time(0);
595cdd5e 88 process_request(mp, &response);
dabe9d2f 89 /* can block here, is this what I want? */
595cdd5e
KM
90 cc = sendto(sockt, (char *)&response,
91 sizeof (response), 0, &mp->ctl_addr, sizeof (mp->ctl_addr));
92 if (cc != sizeof (response))
93 syslog(LOG_WARNING, "sendto: %m");
dabe9d2f 94 }
dabe9d2f
MK
95}
96
963ce42e 97timeout()
dabe9d2f 98{
dabe9d2f 99
963ce42e 100 if (time(0) - lastmsgtime >= MAXIDLE)
595cdd5e 101 _exit(0);
963ce42e 102 alarm(TIMEOUT);
dabe9d2f 103}