xref: /csrg-svn/libexec/talkd/talkd.c (revision 26841)
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
8 char copyright[] =
9 "@(#) Copyright (c) 1983 Regents of the University of California.\n\
10  All rights reserved.\n";
11 #endif not lint
12 
13 #ifndef lint
14 static char sccsid[] = "@(#)talkd.c	5.2 (Berkeley) 03/13/86";
15 #endif not lint
16 
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
22  */
23 #include <stdio.h>
24 #include <errno.h>
25 #include <signal.h>
26 #include <syslog.h>
27 
28 #include <protocols/talkd.h>
29 
30 CTL_MSG		request;
31 CTL_RESPONSE	response;
32 
33 int	sockt;
34 int	debug = 0;
35 int	timeout();
36 long	lastmsgtime;
37 
38 char	hostname[32];
39 
40 #define TIMEOUT 30
41 #define MAXIDLE 120
42 
43 main(argc, argv)
44 	int argc;
45 	char *argv[];
46 {
47 	register CTL_MSG *mp = &request;
48 	int cc;
49 
50 	if (getuid()) {
51 		fprintf(stderr, "%s: getuid: not super-user", argv[0]);
52 		exit(1);
53 	}
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;
65 	signal(SIGALRM, timeout);
66 	alarm(TIMEOUT);
67 	for (;;) {
68 		extern int errno;
69 
70 		cc = recv(0, (char *)mp, sizeof (*mp), 0);
71 		if (cc != sizeof (*mp)) {
72 			if (cc < 0 && errno != EINTR)
73 				syslog(LOG_WARNING, "recv: %m");
74 			continue;
75 		}
76 		lastmsgtime = time(0);
77 		process_request(mp, &response);
78 		/* can block here, is this what I want? */
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");
83 	}
84 }
85 
86 timeout()
87 {
88 
89 	if (time(0) - lastmsgtime >= MAXIDLE)
90 		_exit(0);
91 	alarm(TIMEOUT);
92 }
93