Annotation of 43BSDTahoe/new/nntp/server/netaux.c, revision 1.1

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

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.