This is msendd.c in view mode; [Download] [Up]
/* msendd.c: * * daemon required to allow msend to operate. works with inetd also. * * (c) Copyright 1988, 1989, 1990 Jim Frost. All Rights Reserved. Please see * the accompanying file "Copyright" for more information. * * special thanks to Barry Shein (bzs@bu-it.bu.edu) for the skeleton * that this was built from. */ #include "Copyright" #include "config.h" #include "msend.h" #ifdef ROUTE short doroute = 1; #endif #ifdef DBROADCAST short dobroadcast = 1; #endif #ifdef SECURE_PORT short notsecure = 1; #endif static void fireman() /* catches falling babies */ { WAIT_STATUS wstatus; while(wait3(&wstatus,WNOHANG,NULL) > 0); } static void sigint() { error("SIGINT interrupt received"); die(0); } static void sigterm() { error("SIGTERM interrupt received"); die(0); } main(argc,argv) int argc; char *argv[]; { char c; int s,t; int i; struct sockaddr_in isa; extern int errno; while ((c = getopt(argc, argv, "rb")) != (char) -1) { switch(c) { case 'r': #ifdef ROUTE doroute = 0; #endif break; case 'b': #ifdef DBROADCAST dobroadcast = 0; #endif break; } } #ifndef ALONE /* inetd(8), don't leave home without it */ #ifdef SECURE_PORT i = sizeof(isa); s = 0; /* stdin */ if (getpeername(s,(struct sockaddr *)&isa,&i) >= 0){ if ((ntohs(isa.sin_port) < IPPORT_RESERVED) && (ntohs(isa.sin_port) > IPPORT_RESERVED/2)) notsecure = 0; } #endif domessage(0); exit(0); #else /* disassociate ourselves from any terminal */ for (i= 0; i < getdtablesize(); i++) close(i); if ((i= open("/dev/tty", O_RDWR)) >= 0) { if (ioctl(i, TIOCNOTTY, NULL) < 0) error("Can't disassociate from tty\n"); close(i); } /* normal daemon. set up for listening and then go into an infinite * loop for connections */ error("msendd started"); /* remember when it all started */ if ((s= establish(portnum())) < 0) { /* bind to a socket */ if (errno == EADDRINUSE) { printf("msendd: cannot allocate port (daemon already active?)\n"); } else perror("msendd: establish"); die(1); } /* an ioctl should go here after openning /dev/tty but i can't remember * which ioctl */ switch (fork()) { case -1: /* oh well */ error("can't fork, sorry\n"); case 0: break; default: exit(0); } /* after establishing ourselves, become DAEMONUID */ if (getuid() == ROOTUID) seteuid(DAEMONUID); i = sizeof(isa); /* find it's "name" */ getsockname(s,&isa,&i); signal(SIGHUP,SIG_IGN); signal(SIGSTOP,SIG_IGN); /* bad things happened w/o this */ signal(SIGTSTP,SIG_IGN); /* seems to get this sometimes */ signal(SIGINT,sigint); signal(SIGTERM,sigterm); signal(SIGCHLD,fireman); /* set up to catch dead children */ listen(s,BACKLOG); /* set maximum backlog */ for(;;) { i = sizeof isa ; if((t = accept(s,&isa,&i)) < 0) { if(errno != EINTR) /* we'll get an EINTR when child dies */ error("accept error"); continue; /* try again */ } if (fork() == 0) { close(s); /* i have no need for you */ setpgrp(0, getpid()); /* parent doesn't want signals! */ #ifdef SECURE_PORT if (getpeername(t,(struct sockaddr *)&isa,&i) >= 0){ if ((ntohs(isa.sin_port) < IPPORT_RESERVED) && (ntohs(isa.sin_port) > IPPORT_RESERVED/2)) notsecure = 0; } #endif domessage(t); /* process the message(s) */ exit(0); /* domessage does this too */ } /* fork errors disconnect immediately, tough luck on the user */ else close(t); } #endif }
These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.