xref: /csrg-svn/libexec/talkd/talkd.c (revision 34360)
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 this notice is preserved and that due credit is given
7  * to the University of California at Berkeley. The name of the University
8  * may not be used to endorse or promote products derived from this
9  * software without specific prior written permission. This software
10  * is provided ``as is'' without express or implied warranty.
11  */
12 
13 #ifndef lint
14 char copyright[] =
15 "@(#) Copyright (c) 1983 Regents of the University of California.\n\
16  All rights reserved.\n";
17 #endif /* not lint */
18 
19 #ifndef lint
20 static char sccsid[] = "@(#)talkd.c	5.3 (Berkeley) 05/20/88";
21 #endif /* not lint */
22 
23 /*
24  * The top level of the daemon, the format is heavily borrowed
25  * from rwhod.c. Basically: find out who and where you are;
26  * disconnect all descriptors and ttys, and then endless
27  * loop on waiting for and processing requests
28  */
29 #include <stdio.h>
30 #include <errno.h>
31 #include <signal.h>
32 #include <syslog.h>
33 
34 #include <protocols/talkd.h>
35 
36 CTL_MSG		request;
37 CTL_RESPONSE	response;
38 
39 int	sockt;
40 int	debug = 0;
41 int	timeout();
42 long	lastmsgtime;
43 
44 char	hostname[32];
45 
46 #define TIMEOUT 30
47 #define MAXIDLE 120
48 
49 main(argc, argv)
50 	int argc;
51 	char *argv[];
52 {
53 	register CTL_MSG *mp = &request;
54 	int cc;
55 
56 	if (getuid()) {
57 		fprintf(stderr, "%s: getuid: not super-user", argv[0]);
58 		exit(1);
59 	}
60 	openlog("talkd", LOG_PID, LOG_DAEMON);
61 	if (gethostname(hostname, sizeof (hostname) - 1) < 0) {
62 		syslog(LOG_ERR, "gethostname: %m");
63 		_exit(1);
64 	}
65 	if (chdir("/dev") < 0) {
66 		syslog(LOG_ERR, "chdir: /dev: %m");
67 		_exit(1);
68 	}
69 	if (argc > 1 && strcmp(argv[1], "-d") == 0)
70 		debug = 1;
71 	signal(SIGALRM, timeout);
72 	alarm(TIMEOUT);
73 	for (;;) {
74 		extern int errno;
75 
76 		cc = recv(0, (char *)mp, sizeof (*mp), 0);
77 		if (cc != sizeof (*mp)) {
78 			if (cc < 0 && errno != EINTR)
79 				syslog(LOG_WARNING, "recv: %m");
80 			continue;
81 		}
82 		lastmsgtime = time(0);
83 		process_request(mp, &response);
84 		/* can block here, is this what I want? */
85 		cc = sendto(sockt, (char *)&response,
86 		    sizeof (response), 0, &mp->ctl_addr, sizeof (mp->ctl_addr));
87 		if (cc != sizeof (response))
88 			syslog(LOG_WARNING, "sendto: %m");
89 	}
90 }
91 
92 timeout()
93 {
94 
95 	if (time(0) - lastmsgtime >= MAXIDLE)
96 		_exit(0);
97 	alarm(TIMEOUT);
98 }
99