1 /* 2 * Copyright (c) 1983 Regents of the University of California. 3 * All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 */ 7 8 #ifndef lint 9 char copyright[] = 10 "@(#) Copyright (c) 1983 Regents of the University of California.\n\ 11 All rights reserved.\n"; 12 #endif /* not lint */ 13 14 #ifndef lint 15 static char sccsid[] = "@(#)talkd.c 5.7 (Berkeley) 06/01/90"; 16 #endif /* not lint */ 17 18 /* 19 * The top level of the daemon, the format is heavily borrowed 20 * from rwhod.c. Basically: find out who and where you are; 21 * disconnect all descriptors and ttys, and then endless 22 * loop on waiting for and processing requests 23 */ 24 #include <stdio.h> 25 #include <errno.h> 26 #include <signal.h> 27 #include <syslog.h> 28 29 #include <protocols/talkd.h> 30 #include <paths.h> 31 32 CTL_MSG request; 33 CTL_RESPONSE response; 34 35 int sockt; 36 int debug = 0; 37 int timeout(); 38 long lastmsgtime; 39 40 char hostname[32]; 41 42 #define TIMEOUT 30 43 #define MAXIDLE 120 44 45 main(argc, argv) 46 int argc; 47 char *argv[]; 48 { 49 register CTL_MSG *mp = &request; 50 int cc; 51 52 if (getuid()) { 53 fprintf(stderr, "%s: getuid: not super-user", argv[0]); 54 exit(1); 55 } 56 openlog("talkd", LOG_PID, LOG_DAEMON); 57 if (gethostname(hostname, sizeof (hostname) - 1) < 0) { 58 syslog(LOG_ERR, "gethostname: %m"); 59 _exit(1); 60 } 61 if (chdir(_PATH_DEV) < 0) { 62 syslog(LOG_ERR, "chdir: %s: %m", _PATH_DEV); 63 _exit(1); 64 } 65 if (argc > 1 && strcmp(argv[1], "-d") == 0) 66 debug = 1; 67 signal(SIGALRM, timeout); 68 alarm(TIMEOUT); 69 for (;;) { 70 extern int errno; 71 72 cc = recv(0, (char *)mp, sizeof (*mp), 0); 73 if (cc != sizeof (*mp)) { 74 if (cc < 0 && errno != EINTR) 75 syslog(LOG_WARNING, "recv: %m"); 76 continue; 77 } 78 lastmsgtime = time(0); 79 process_request(mp, &response); 80 /* can block here, is this what I want? */ 81 cc = sendto(sockt, (char *)&response, 82 sizeof (response), 0, (struct sockaddr *)&mp->ctl_addr, 83 sizeof (mp->ctl_addr)); 84 if (cc != sizeof (response)) 85 syslog(LOG_WARNING, "sendto: %m"); 86 } 87 } 88 89 timeout() 90 { 91 92 if (time(0) - lastmsgtime >= MAXIDLE) 93 _exit(0); 94 alarm(TIMEOUT); 95 } 96