1: #ifndef lint
   2: static char *sccsid = "@(#)netaux.c	1.3	(Berkeley) 3/12/86";
   3: #endif
   4: 
   5: /*
   6:  * Routines to deal with network stuff for
   7:  * stand-alone version of server.
   8:  */
   9: 
  10: #include "common.h"
  11: #include <sys/socket.h>
  12: #include <netinet/in.h>
  13: #include <netdb.h>
  14: #include <sys/ioctl.h>
  15: #include <sys/time.h>
  16: #include <signal.h>
  17: 
  18: #ifdef ALONE
  19: 
  20: 
  21: /*
  22:  * disassoc -- disassociate user's terminal from this
  23:  * process.  Closes all file descriptors, except
  24:  * the control socket.
  25:  *
  26:  *	Parameters:	None.
  27:  *
  28:  *	Returns:	Nothing.
  29:  *
  30:  *	Side effects:	Disassociates this process from
  31:  *			a terminal; closes file descriptors.
  32:  */
  33: 
  34: disassoc()
  35: {
  36:     register int    i;
  37: 
  38:     if (fork())
  39:         exit(0);
  40: 
  41:     for (i = 0; i < 10; i++)
  42:         (void) close(i);
  43: 
  44:     i = open("/dev/tty", O_RDWR);
  45:     if (i >= 0) {
  46:         ioctl(i, TIOCNOTTY, 0);
  47:         (void) close(i);
  48:     }
  49: }
  50: 
  51: 
  52: /*
  53:  * get_socket -- create a socket bound to the appropriate
  54:  *	port number.
  55:  *
  56:  *	Parameters:	None.
  57:  *
  58:  *	Returns:	Socket bound to correct address.
  59:  *
  60:  *	Side effects:	None.
  61:  *
  62:  *	Errors:		Syslogd, cause aboriton.
  63:  */
  64: 
  65: get_socket()
  66: {
  67:     int         s;
  68:     struct sockaddr_in  sin;
  69:     struct servent      *sp;
  70: 
  71:     sp = getservbyname("nntp", "tcp");
  72:     if (sp == NULL) {
  73:         syslog(LOG_ERR, "get_socket: tcp/nntp, unknown service.");
  74:         exit(1);
  75:     }
  76: 
  77:     bzero((char *) &sin, sizeof (sin));
  78:     sin.sin_family = AF_INET;
  79:     sin.sin_addr.s_addr = htonl(INADDR_ANY);
  80:     sin.sin_port = sp->s_port;
  81: 
  82:     s = socket(AF_INET, SOCK_STREAM, 0);
  83:     if (s < 0) {
  84:         syslog(LOG_ERR, "get_socket: socket: %m");
  85:         exit(1);
  86:     }
  87: 
  88:     if (bind(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
  89:         syslog(LOG_ERR, "get_socket: bind: %m");
  90:         exit(1);
  91:     }
  92: 
  93:     return (s);
  94: }
  95: 
  96: /*
  97:  * make_stdio -- make a given socket be our standard input
  98:  *	and output.
  99:  *
 100:  *	Parameters:	"sockt" is the socket we want to
 101:  *			make be file descriptors 0 and 1.
 102:  *
 103:  *	Returns:	Nothing.
 104:  *
 105:  *	Side effects:	None.
 106:  */
 107: 
 108: make_stdio(sockt)
 109: int sockt;
 110: {
 111:     int ttyd, i;
 112: 
 113:     (void) dup2(sockt, 0);
 114:     (void) close(sockt);
 115:     (void) dup2(0, 1);
 116:     for (i = getdtablesize(); --i > 2; )
 117:         (void) close(i);
 118:     ttyd = open("/dev/tty", O_RDWR);
 119:     if (ttyd >= 0) {
 120:         ioctl(ttyd, TIOCNOTTY, 0);
 121:         (void) close(ttyd);
 122:     }
 123: }
 124: 
 125: /*
 126:  * set_timer -- set up the interval timer so that
 127:  *	the active file is read in every so often.
 128:  *
 129:  *	Parameters:	None.
 130:  *
 131:  *	Returns:	Nothing.
 132:  *
 133:  *	Side effects:	Sets interval timer to READINTVL seconds.
 134:  *			Sets SIGALRM to call read_again.
 135:  */
 136: 
 137: set_timer()
 138: {
 139:     struct itimerval    new, old;
 140:     extern int      read_again();
 141: 
 142:     (void) signal(SIGALRM, read_again);
 143: 
 144:     new.it_value.tv_sec = READINTVL;
 145:     new.it_value.tv_usec = 0;
 146:     new.it_interval.tv_sec = READINTVL;
 147:     new.it_interval.tv_usec = 0;
 148:     old.it_value.tv_sec = 0;
 149:     old.it_value.tv_usec = 0;
 150:     old.it_interval.tv_sec = 0;
 151:     old.it_interval.tv_usec = 0;
 152: 
 153:     if (setitimer(ITIMER_REAL, &new, &old) < 0) {
 154:         syslog(LOG_ERR, "set_timer: setitimer: %m\n");
 155:         exit(1);
 156:     }
 157: }
 158: 
 159: 
 160: /*
 161:  * read_again -- (maybe) read in the active file again,
 162:  *	if it's changed since the last time we checked.
 163:  *
 164:  *	Parameters:	None (called by interrupt).
 165:  *
 166:  *	Returns:	Nothing.
 167:  *
 168:  *	Side effects:	May change "num_groups" and "group_array".
 169:  */
 170: 
 171: read_again()
 172: {
 173:     static long last_mtime; /* Last time active file was changed */
 174:     struct stat statbuf;
 175: 
 176:     if (stat(ACTIVE_FILE, &statbuf) < 0)
 177:         return;
 178: 
 179:     if (statbuf.st_mtime != last_mtime) {
 180:         last_mtime = statbuf.st_mtime;
 181:         num_groups = read_groups();
 182:     }
 183: }
 184: 
 185: 
 186: /*
 187:  * reaper -- reap children who are ready to die.
 188:  *	Called by signal.
 189:  *
 190:  *	Parameters:	None.
 191:  *
 192:  *	Returns:	Nothing.
 193:  *
 194:  *	Side effects:	None.
 195:  */
 196: 
 197: reaper()
 198: {
 199:     union wait  status;
 200: 
 201:     while (wait3(&status, WNOHANG, (struct rusage *)0) > 0)
 202:         ;
 203: }
 204: 
 205: #endif

Defined functions

disassoc defined in line 34; used 1 times
get_socket defined in line 65; used 1 times
make_stdio defined in line 108; used 1 times
read_again defined in line 171; used 2 times
reaper defined in line 197; used 2 times
set_timer defined in line 137; used 1 times

Defined variables

sccsid defined in line 2; never used
Last modified: 1986-03-21
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 803
Valid CSS Valid XHTML 1.0 Strict