xref: /netbsd-src/libexec/talkd/talkd.c (revision 23c8222edbfb0f0932d88a8351d3a0cf817dfb9e)
1 /*	$NetBSD: talkd.c,v 1.17 2003/08/07 09:46:51 agc Exp $	*/
2 
3 /*
4  * Copyright (c) 1983, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 #ifndef lint
34 __COPYRIGHT("@(#) Copyright (c) 1983, 1993\n\
35 	The Regents of the University of California.  All rights reserved.\n");
36 #endif /* not lint */
37 
38 #ifndef lint
39 #if 0
40 static char sccsid[] = "@(#)talkd.c	8.1 (Berkeley) 6/4/93";
41 #else
42 __RCSID("$NetBSD: talkd.c,v 1.17 2003/08/07 09:46:51 agc Exp $");
43 #endif
44 #endif /* not lint */
45 
46 /*
47  * The top level of the daemon, the format is heavily borrowed
48  * from rwhod.c. Basically: find out who and where you are;
49  * disconnect all descriptors and ttys, and then endless
50  * loop on waiting for and processing requests
51  */
52 #include <sys/types.h>
53 #include <sys/socket.h>
54 #include <sys/param.h>
55 
56 #include <protocols/talkd.h>
57 
58 #include <errno.h>
59 #include <paths.h>
60 #include <signal.h>
61 #include <stdio.h>
62 #include <stdlib.h>
63 #include <string.h>
64 #include <syslog.h>
65 #include <time.h>
66 #include <unistd.h>
67 
68 #include "extern.h"
69 
70 CTL_MSG		request;
71 CTL_RESPONSE	response;
72 
73 int	sockt = STDIN_FILENO;
74 int	debug = 0;
75 int	logging = 0;
76 long	lastmsgtime;
77 
78 char	hostname[MAXHOSTNAMELEN + 1];
79 
80 #define TIMEOUT 30
81 #define MAXIDLE 120
82 
83 static void timeout __P((int));
84 int	main __P((int, char *[]));
85 
86 int
87 main(argc, argv)
88 	int argc;
89 	char *argv[];
90 {
91 	CTL_MSG *mp = &request;
92 	int cc, ch;
93 	struct sockaddr ctl_addr;
94 
95 	openlog("talkd", LOG_PID, LOG_DAEMON);
96 	while ((ch = getopt(argc, argv, "dl")) != -1)
97 		switch (ch) {
98 		case 'd':
99 			debug = 1;
100 			break;
101 		case 'l':
102 			logging = 1;
103 			break;
104 		default:
105 			syslog(LOG_ERR, "Usage: %s [-dl]", getprogname());
106 			exit(1);
107 		}
108 
109 	if (gethostname(hostname, sizeof hostname) < 0) {
110 		syslog(LOG_ERR, "gethostname: %m");
111 		_exit(1);
112 	}
113 	hostname[MAXHOSTNAMELEN] = '\0';  /* ensure null termination */
114 	if (chdir(_PATH_DEV) < 0) {
115 		syslog(LOG_ERR, "chdir: %s: %m", _PATH_DEV);
116 		_exit(1);
117 	}
118 	signal(SIGALRM, timeout);
119 	alarm(TIMEOUT);
120 	for (;;) {
121 		memset(&response, 0, sizeof(response));
122 		cc = recv(0, (char *)mp, sizeof (*mp), 0);
123 		if (cc != sizeof (*mp)) {
124 			if (cc < 0 && errno != EINTR)
125 				syslog(LOG_WARNING, "recv: %m");
126 			continue;
127 		}
128 
129 		mp->l_name[sizeof(mp->l_name) - 1] = '\0';
130 		mp->r_name[sizeof(mp->r_name) - 1] = '\0';
131 		mp->r_tty[sizeof(mp->r_tty) - 1] = '\0';
132 
133 		lastmsgtime = time(0);
134 		process_request(mp, &response);
135 
136 		(void)memcpy(&ctl_addr, &mp->ctl_addr, sizeof(ctl_addr));
137 		ctl_addr.sa_family = mp->ctl_addr.sa_family;
138 		ctl_addr.sa_len = sizeof(ctl_addr);
139 		if (ctl_addr.sa_family != AF_INET)
140 			continue;
141 
142 		/* can block here, is this what I want? */
143 		cc = sendto(sockt, (char *)&response, sizeof (response), 0,
144 		    &ctl_addr, sizeof (ctl_addr));
145 		if (cc != sizeof (response))
146 			syslog(LOG_WARNING, "sendto: %m");
147 	}
148 }
149 
150 void
151 timeout(n)
152 	int n;
153 {
154 	int save_errno = errno;
155 
156 	if (time(0) - lastmsgtime >= MAXIDLE)
157 		_exit(0);
158 	alarm(TIMEOUT);
159 	errno = save_errno;
160 }
161