1 /* $NetBSD: talkd.c,v 1.21 2009/03/16 01:04:32 lukem 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.21 2009/03/16 01:04:32 lukem 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(int); 84 int main(int, char *[]); 85 86 int 87 main(int argc, char *argv[]) 88 { 89 CTL_MSG *mp = &request; 90 int cc, ch; 91 struct sockaddr ctl_addr; 92 93 openlog("talkd", LOG_PID, LOG_DAEMON); 94 while ((ch = getopt(argc, argv, "dl")) != -1) 95 switch (ch) { 96 case 'd': 97 debug = 1; 98 break; 99 case 'l': 100 logging = 1; 101 break; 102 default: 103 syslog(LOG_ERR, "Usage: %s [-dl]", getprogname()); 104 exit(1); 105 } 106 107 if (gethostname(hostname, sizeof hostname) < 0) { 108 syslog(LOG_ERR, "gethostname: %m"); 109 _exit(1); 110 } 111 hostname[MAXHOSTNAMELEN] = '\0'; /* ensure null termination */ 112 if (chdir(_PATH_DEV) < 0) { 113 syslog(LOG_ERR, "chdir: %s: %m", _PATH_DEV); 114 _exit(1); 115 } 116 signal(SIGALRM, timeout); 117 alarm(TIMEOUT); 118 for (;;) { 119 memset(&response, 0, sizeof(response)); 120 cc = recv(0, (char *)mp, sizeof (*mp), 0); 121 if (cc != sizeof (*mp)) { 122 if (cc < 0 && errno != EINTR) 123 syslog(LOG_WARNING, "recv: %m"); 124 continue; 125 } 126 127 mp->l_name[sizeof(mp->l_name) - 1] = '\0'; 128 mp->r_name[sizeof(mp->r_name) - 1] = '\0'; 129 mp->r_tty[sizeof(mp->r_tty) - 1] = '\0'; 130 131 lastmsgtime = time(0); 132 process_request(mp, &response); 133 134 tsa2sa(&ctl_addr, &mp->ctl_addr); 135 if (ctl_addr.sa_family != AF_INET) 136 continue; 137 138 /* can block here, is this what I want? */ 139 cc = sendto(sockt, (char *)&response, sizeof (response), 0, 140 &ctl_addr, sizeof (ctl_addr)); 141 if (cc != sizeof (response)) 142 syslog(LOG_WARNING, "sendto: %m"); 143 } 144 } 145 146 void 147 timeout(int n) 148 { 149 int save_errno = errno; 150 151 if (time(0) - lastmsgtime >= MAXIDLE) 152 _exit(0); 153 alarm(TIMEOUT); 154 errno = save_errno; 155 } 156 157 void 158 tsa2sa(struct sockaddr *sa, const struct talkd_sockaddr *tsa) 159 { 160 (void)memcpy(sa, tsa, sizeof(*tsa)); 161 sa->sa_len = sizeof(*tsa); 162 sa->sa_family = tsa->sa_family; 163 } 164