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: 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:     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:             &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, &request.ctl_addr,
  81:             sizeof (request.ctl_addr));
  82:         if (cc != sizeof(response))
  83:             perror("sendto");
  84:     }
  85: }
  86: 
  87: timeout()
  88: {
  89: 
  90:     if (time(0) - lastmsgtime >= MAXIDLE)
  91:         exit(0);
  92:     alarm(TIMEOUT);
  93: }
  94: 
  95: /*
  96:  * heuristic to detect if need to swap bytes
  97:  */
  98: 
  99: swapmsg(req)
 100:     CTL_MSG *req;
 101: {
 102:     if (req->ctl_addr.sin_family == ntohs(AF_INET)) {
 103:         req->id_num = ntohl(req->id_num);
 104:         req->pid = ntohl(req->pid);
 105:         req->addr.sin_family = ntohs(req->addr.sin_family);
 106:         req->ctl_addr.sin_family =
 107:             ntohs(req->ctl_addr.sin_family);
 108:     }
 109: }

Defined functions

main defined in line 45; never used
swapmsg defined in line 99; used 1 times
  • in line 75
timeout defined in line 87; used 2 times

Defined variables

copyright defined in line 8; never used
debug defined in line 35; used 11 times
hostname defined in line 40; used 3 times
lastmsgtime defined in line 38; used 2 times
request defined in line 31; used 8 times
response defined in line 32; used 4 times
sccsid defined in line 14; never used
sin defined in line 29; never used
sockt defined in line 34; used 1 times
  • in line 79

Defined macros

MAXIDLE defined in line 43; used 1 times
  • in line 90
TIMEOUT defined in line 42; used 2 times
Last modified: 1986-03-16
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 1174
Valid CSS Valid XHTML 1.0 Strict