1 /* 2 * Copyright (c) 1983 Regents of the University of California. 3 * All rights reserved. The Berkeley software License Agreement 4 * specifies the terms and conditions for redistribution. 5 */ 6 7 #ifndef lint 8 char copyright[] = 9 "@(#) Copyright (c) 1983 Regents of the University of California.\n\ 10 All rights reserved.\n"; 11 #endif not lint 12 13 #ifndef lint 14 static char sccsid[] = "@(#)talkd.c 5.1 (Berkeley) 6/6/85"; 15 #endif not lint 16 17 /* 18 * The top level of the daemon, the format is heavily borrowed 19 * from rwhod.c. Basically: find out who and where you are; 20 * disconnect all descriptors and ttys, and then endless 21 * loop on waiting for and processing requests 22 */ 23 #include <stdio.h> 24 #include <errno.h> 25 #include <signal.h> 26 27 #include "ctl.h" 28 29 struct sockaddr_in sin = { AF_INET }; 30 31 CTL_MSG request; 32 CTL_RESPONSE response; 33 34 int sockt; 35 int debug = 0; 36 FILE *debugout; 37 void 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 struct sockaddr_in from; 50 int fromlen, cc; 51 52 if (debug) 53 debugout = (FILE *)fopen ("/usr/tmp/talkd.msgs", "w"); 54 55 if (getuid()) { 56 fprintf(stderr, "Talkd : not super user\n"); 57 exit(1); 58 } 59 gethostname(hostname, sizeof (hostname)); 60 (void) chdir("/dev"); 61 signal(SIGALRM, timeout); 62 alarm(TIMEOUT); 63 for (;;) { 64 extern int errno; 65 66 fromlen = sizeof(from); 67 cc = recvfrom(0, (char *) &request, sizeof (request), 0, 68 (struct sockaddr *)&from, &fromlen); 69 if (cc != sizeof(request)) { 70 if (cc < 0 && errno != EINTR) 71 perror("recvfrom"); 72 continue; 73 } 74 lastmsgtime = time(0); 75 swapmsg(&request); 76 if (debug) print_request(&request); 77 process_request(&request, &response); 78 /* can block here, is this what I want? */ 79 cc = sendto(sockt, (char *) &response, 80 sizeof (response), 0, (struct sockaddr *)&request.ctl_addr, 81 sizeof (request.ctl_addr)); 82 if (cc != sizeof(response)) 83 perror("sendto"); 84 } 85 } 86 87 void 88 timeout() 89 { 90 91 if (time(0) - lastmsgtime >= MAXIDLE) 92 exit(0); 93 alarm(TIMEOUT); 94 } 95 96 /* 97 * heuristic to detect if need to swap bytes 98 */ 99 100 swapmsg(req) 101 CTL_MSG *req; 102 { 103 if (req->ctl_addr.sin_family == ntohs(AF_INET)) { 104 req->id_num = ntohl(req->id_num); 105 req->pid = ntohl(req->pid); 106 req->addr.sin_family = ntohs(req->addr.sin_family); 107 req->ctl_addr.sin_family = 108 ntohs(req->ctl_addr.sin_family); 109 } 110 } 111