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