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: #if defined(DOSCCS) && !defined(lint)
8: char copyright[] =
9: "@(#) Copyright (c) 1983 Regents of the University of California.\n\
10: All rights reserved.\n";
11:
12: static char sccsid[] = "@(#)talkd.c 5.2.1 (2.11BSD GTE) 1/1/94";
13: #endif
14:
15: /*
16: * The top level of the daemon, the format is heavily borrowed
17: * from rwhod.c. Basically: find out who and where you are;
18: * disconnect all descriptors and ttys, and then endless
19: * loop on waiting for and processing requests
20: */
21: #include <stdio.h>
22: #include <errno.h>
23: #include <signal.h>
24: #include <syslog.h>
25:
26: #include <protocols/talkd.h>
27:
28: CTL_MSG request;
29: CTL_RESPONSE response;
30:
31: int sockt;
32: int debug = 0;
33: int timeout();
34: long time();
35: long lastmsgtime;
36:
37: char hostname[32];
38:
39: #define TIMEOUT 30
40: #define MAXIDLE 120
41:
42: main(argc, argv)
43: int argc;
44: char *argv[];
45: {
46: register CTL_MSG *mp = &request;
47: int cc;
48:
49: if (getuid()) {
50: fprintf(stderr, "%s: getuid: not super-user\n", argv[0]);
51: exit(1);
52: }
53: openlog("talkd", LOG_PID, LOG_DAEMON);
54: if (gethostname(hostname, sizeof (hostname) - 1) < 0) {
55: syslog(LOG_ERR, "gethostname: %m");
56: _exit(1);
57: }
58: if (chdir("/dev") < 0) {
59: syslog(LOG_ERR, "chdir: /dev: %m");
60: _exit(1);
61: }
62: if (argc > 1 && strcmp(argv[1], "-d") == 0)
63: debug = 1;
64: signal(SIGALRM, timeout);
65: alarm(TIMEOUT);
66: for (;;) {
67: extern int errno;
68:
69: cc = recv(0, (char *)mp, sizeof (*mp), 0);
70: if (cc != sizeof (*mp)) {
71: if (cc < 0 && errno != EINTR)
72: syslog(LOG_WARNING, "recv: %m");
73: continue;
74: }
75: lastmsgtime = time(0);
76: process_request(mp, &response);
77: /* can block here, is this what I want? */
78: cc = sendto(sockt, (char *)&response,
79: sizeof (response), 0, &mp->ctl_addr, sizeof (mp->ctl_addr));
80: if (cc != sizeof (response))
81: syslog(LOG_WARNING, "sendto: %m");
82: }
83: }
84:
85: timeout()
86: {
87:
88: if (time(0) - lastmsgtime >= MAXIDLE)
89: _exit(0);
90: alarm(TIMEOUT);
91: }
Defined functions
main
defined in line
42;
never used
Defined variables
debug
defined in line
32; used 10 times
sockt
defined in line
31; used 1 times
Defined macros