|
|
1.1 root 1: /*
2: * Copyright (c) 1983 Regents of the University of California.
3: * All rights reserved.
4: *
5: * Redistribution and use in source and binary forms are permitted
6: * provided that: (1) source distributions retain this entire copyright
7: * notice and comment, and (2) distributions including binaries display
8: * the following acknowledgement: ``This product includes software
9: * developed by the University of California, Berkeley and its contributors''
10: * in the documentation or other materials provided with the distribution
11: * and in all advertising materials mentioning features or use of this
12: * software. Neither the name of the University nor the names of its
13: * contributors may be used to endorse or promote products derived
14: * from this software without specific prior written permission.
15: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
16: * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
17: * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
18: */
19:
20: #ifndef lint
21: char copyright[] =
22: "@(#) Copyright (c) 1983 Regents of the University of California.\n\
23: All rights reserved.\n";
24: #endif /* not lint */
25:
26: #ifndef lint
27: static char sccsid[] = "@(#)talkd.c 5.7 (Berkeley) 6/1/90";
28: #endif /* not lint */
29:
30: /*
31: * The top level of the daemon, the format is heavily borrowed
32: * from rwhod.c. Basically: find out who and where you are;
33: * disconnect all descriptors and ttys, and then endless
34: * loop on waiting for and processing requests
35: */
36: #include <stdio.h>
37: #include <errno.h>
38: #include <signal.h>
39: #include <syslog.h>
40:
41: #include <protocols/talkd.h>
42: #include <paths.h>
43:
44: CTL_MSG request;
45: CTL_RESPONSE response;
46:
47: int sockt;
48: int debug = 0;
49: int timeout();
50: long lastmsgtime;
51:
52: char hostname[32];
53:
54: #define TIMEOUT 30
55: #define MAXIDLE 120
56:
57: main(argc, argv)
58: int argc;
59: char *argv[];
60: {
61: register CTL_MSG *mp = &request;
62: int cc;
63:
64: if (getuid()) {
65: fprintf(stderr, "%s: getuid: not super-user", argv[0]);
66: exit(1);
67: }
68: openlog("talkd", LOG_PID, LOG_DAEMON);
69: if (gethostname(hostname, sizeof (hostname) - 1) < 0) {
70: syslog(LOG_ERR, "gethostname: %m");
71: _exit(1);
72: }
73: if (chdir(_PATH_DEV) < 0) {
74: syslog(LOG_ERR, "chdir: %s: %m", _PATH_DEV);
75: _exit(1);
76: }
77: if (argc > 1 && strcmp(argv[1], "-d") == 0)
78: debug = 1;
79: signal(SIGALRM, timeout);
80: alarm(TIMEOUT);
81: for (;;) {
82: extern int errno;
83:
84: cc = recv(0, (char *)mp, sizeof (*mp), 0);
85: if (cc != sizeof (*mp)) {
86: if (cc < 0 && errno != EINTR)
87: syslog(LOG_WARNING, "recv: %m");
88: continue;
89: }
90: lastmsgtime = time(0);
91: process_request(mp, &response);
92: /* can block here, is this what I want? */
93: cc = sendto(sockt, (char *)&response,
94: sizeof (response), 0, (struct sockaddr *)&mp->ctl_addr,
95: sizeof (mp->ctl_addr));
96: if (cc != sizeof (response))
97: syslog(LOG_WARNING, "sendto: %m");
98: }
99: }
100:
101: timeout()
102: {
103:
104: if (time(0) - lastmsgtime >= MAXIDLE)
105: _exit(0);
106: alarm(TIMEOUT);
107: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.