xref: /csrg-svn/libexec/talkd/talkd.c (revision 16366)
1 #ifndef lint
2 static	char sccsid[] = "@(#)talkd.c	1.2 (Berkeley) 04/11/84";
3 #endif
4 
5 /*
6  * The top level of the daemon, the format is heavily borrowed
7  * from rwhod.c. Basically: find out who and where you are;
8  * disconnect all descriptors and ttys, and then endless
9  * loop on waiting for and processing requests
10  */
11 #include <stdio.h>
12 #include <errno.h>
13 #include <signal.h>
14 
15 #include "ctl.h"
16 
17 struct	sockaddr_in sin = { AF_INET };
18 
19 CTL_MSG		request;
20 CTL_RESPONSE	response;
21 
22 int	sockt;
23 int	debug = 0;
24 int	timeout();
25 long	lastmsgtime;
26 
27 char	hostname[32];
28 
29 #define TIMEOUT 30
30 #define MAXIDLE 120
31 
32 main(argc, argv)
33 	int argc;
34 	char *argv[];
35 {
36 	struct sockaddr_in from;
37 	int fromlen, cc;
38 
39 	if (getuid()) {
40 		fprintf(stderr, "Talkd : not super user\n");
41 		exit(1);
42 	}
43 	gethostname(hostname, sizeof (hostname));
44 	(void) chdir("/dev");
45 	signal(SIGALRM, timeout);
46 	alarm(TIMEOUT);
47 	for (;;) {
48 		extern int errno;
49 
50 		fromlen = sizeof(from);
51 		cc = recvfrom(0, (char *)&request, sizeof (request), 0,
52 		    &from, &fromlen);
53 		if (cc != sizeof(request)) {
54 			if (cc < 0 && errno != EINTR)
55 			perror("recvfrom");
56 			continue;
57 		}
58 		lastmsgtime = time(0);
59 		process_request(&request, &response);
60 		/* can block here, is this what I want? */
61 		cc = sendto(sockt, (char *) &response,
62 		    sizeof (response), 0, &request.ctl_addr,
63 		    sizeof (request.ctl_addr));
64 		if (cc != sizeof(response))
65 			perror("sendto");
66 	}
67 }
68 
69 timeout()
70 {
71 
72 	if (time(0) - lastmsgtime >= MAXIDLE)
73 		exit(0);
74 	alarm(TIMEOUT);
75 }
76