Home | History | Annotate | Line # | Download | only in talkd
talkd.c revision 1.19.4.1
      1 /*	$NetBSD: talkd.c,v 1.19.4.1 2008/09/18 04:39:18 wrstuden 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\
     35  The Regents of the University of California.  All rights reserved.");
     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.19.4.1 2008/09/18 04:39:18 wrstuden 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 time_t	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 		tsa2sa(&ctl_addr, &mp->ctl_addr);
    137 		if (ctl_addr.sa_family != AF_INET)
    138 			continue;
    139 
    140 		/* can block here, is this what I want? */
    141 		cc = sendto(sockt, (char *)&response, sizeof (response), 0,
    142 		    &ctl_addr, sizeof (ctl_addr));
    143 		if (cc != sizeof (response))
    144 			syslog(LOG_WARNING, "sendto: %m");
    145 	}
    146 }
    147 
    148 void
    149 timeout(n)
    150 	int n;
    151 {
    152 	int save_errno = errno;
    153 
    154 	if (time(0) - lastmsgtime >= MAXIDLE)
    155 		_exit(0);
    156 	alarm(TIMEOUT);
    157 	errno = save_errno;
    158 }
    159 
    160 void
    161 tsa2sa(struct sockaddr *sa, const struct talkd_sockaddr *tsa)
    162 {
    163 	(void)memcpy(sa, tsa, sizeof(*tsa));
    164 	sa->sa_len = sizeof(*tsa);
    165 	sa->sa_family = tsa->sa_family;
    166 }
    167