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 the above copyright notice and this paragraph are 7 * duplicated in all such forms and that any documentation, 8 * advertising materials, and other materials related to such 9 * distribution and use acknowledge that the software was developed 10 * by the University of California, Berkeley. The name of the 11 * University may not be used to endorse or promote products derived 12 * from this software without specific prior written permission. 13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 15 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 16 */ 17 18 #ifndef lint 19 char copyright[] = 20 "@(#) Copyright (c) 1983 Regents of the University of California.\n\ 21 All rights reserved.\n"; 22 #endif /* not lint */ 23 24 #ifndef lint 25 static char sccsid[] = "@(#)talkd.c 5.6 (Berkeley) 08/16/89"; 26 #endif /* not lint */ 27 28 /* 29 * The top level of the daemon, the format is heavily borrowed 30 * from rwhod.c. Basically: find out who and where you are; 31 * disconnect all descriptors and ttys, and then endless 32 * loop on waiting for and processing requests 33 */ 34 #include <stdio.h> 35 #include <errno.h> 36 #include <signal.h> 37 #include <syslog.h> 38 39 #include <protocols/talkd.h> 40 #include <paths.h> 41 42 CTL_MSG request; 43 CTL_RESPONSE response; 44 45 int sockt; 46 int debug = 0; 47 int timeout(); 48 long lastmsgtime; 49 50 char hostname[32]; 51 52 #define TIMEOUT 30 53 #define MAXIDLE 120 54 55 main(argc, argv) 56 int argc; 57 char *argv[]; 58 { 59 register CTL_MSG *mp = &request; 60 int cc; 61 62 if (getuid()) { 63 fprintf(stderr, "%s: getuid: not super-user", argv[0]); 64 exit(1); 65 } 66 openlog("talkd", LOG_PID, LOG_DAEMON); 67 if (gethostname(hostname, sizeof (hostname) - 1) < 0) { 68 syslog(LOG_ERR, "gethostname: %m"); 69 _exit(1); 70 } 71 if (chdir(_PATH_DEV) < 0) { 72 syslog(LOG_ERR, "chdir: %s: %m", _PATH_DEV); 73 _exit(1); 74 } 75 if (argc > 1 && strcmp(argv[1], "-d") == 0) 76 debug = 1; 77 signal(SIGALRM, timeout); 78 alarm(TIMEOUT); 79 for (;;) { 80 extern int errno; 81 82 cc = recv(0, (char *)mp, sizeof (*mp), 0); 83 if (cc != sizeof (*mp)) { 84 if (cc < 0 && errno != EINTR) 85 syslog(LOG_WARNING, "recv: %m"); 86 continue; 87 } 88 lastmsgtime = time(0); 89 process_request(mp, &response); 90 /* can block here, is this what I want? */ 91 cc = sendto(sockt, (char *)&response, 92 sizeof (response), 0, (struct sockaddr *)&mp->ctl_addr, 93 sizeof (mp->ctl_addr)); 94 if (cc != sizeof (response)) 95 syslog(LOG_WARNING, "sendto: %m"); 96 } 97 } 98 99 timeout() 100 { 101 102 if (time(0) - lastmsgtime >= MAXIDLE) 103 _exit(0); 104 alarm(TIMEOUT); 105 } 106