xref: /csrg-svn/libexec/talkd/talkd.c (revision 34776)
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 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.
16  */
17 
18 #ifndef lint
19 char copyright[] =
20 "@(#) Copyright (c) 1983 Regents of the University of California.\n\
21  All rights reserved.\n";
22 #endif /* not lint */
23 
24 #ifndef lint
25 static char sccsid[] = "@(#)talkd.c	5.4 (Berkeley) 06/18/88";
26 #endif /* not lint */
27 
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
33  */
34 #include <stdio.h>
35 #include <errno.h>
36 #include <signal.h>
37 #include <syslog.h>
38 
39 #include <protocols/talkd.h>
40 
41 CTL_MSG		request;
42 CTL_RESPONSE	response;
43 
44 int	sockt;
45 int	debug = 0;
46 int	timeout();
47 long	lastmsgtime;
48 
49 char	hostname[32];
50 
51 #define TIMEOUT 30
52 #define MAXIDLE 120
53 
54 main(argc, argv)
55 	int argc;
56 	char *argv[];
57 {
58 	register CTL_MSG *mp = &request;
59 	int cc;
60 
61 	if (getuid()) {
62 		fprintf(stderr, "%s: getuid: not super-user", argv[0]);
63 		exit(1);
64 	}
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;
76 	signal(SIGALRM, timeout);
77 	alarm(TIMEOUT);
78 	for (;;) {
79 		extern int errno;
80 
81 		cc = recv(0, (char *)mp, sizeof (*mp), 0);
82 		if (cc != sizeof (*mp)) {
83 			if (cc < 0 && errno != EINTR)
84 				syslog(LOG_WARNING, "recv: %m");
85 			continue;
86 		}
87 		lastmsgtime = time(0);
88 		process_request(mp, &response);
89 		/* can block here, is this what I want? */
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");
94 	}
95 }
96 
97 timeout()
98 {
99 
100 	if (time(0) - lastmsgtime >= MAXIDLE)
101 		_exit(0);
102 	alarm(TIMEOUT);
103 }
104