16039Seric # include <errno.h> 24535Seric # include "sendmail.h" 34535Seric 45978Seric #ifndef DAEMON 5*8518Seric SCCSID(@(#)daemon.c 3.31 10/13/82 (w/o daemon mode)); 65978Seric #else 74535Seric 85978Seric # include <sys/socket.h> 95978Seric # include <net/in.h> 107117Seric # include <wait.h> 115978Seric 12*8518Seric SCCSID(@(#)daemon.c 3.31 10/13/82 (with daemon mode)); 135978Seric 144535Seric /* 154535Seric ** DAEMON.C -- routines to use when running as a daemon. 167556Seric ** 177556Seric ** This entire file is highly dependent on the 4.2 BSD 187556Seric ** interprocess communication primitives. No attempt has 197556Seric ** been made to make this file portable to Version 7, 207556Seric ** Version 6, MPX files, etc. If you should try such a 217556Seric ** thing yourself, I recommend chucking the entire file 227556Seric ** and starting from scratch. Basic semantics are: 237556Seric ** 247556Seric ** getrequests() 257556Seric ** Opens a port and initiates a connection. 267556Seric ** Returns in a child. Must set InChannel and 277556Seric ** OutChannel appropriately. 287556Seric ** makeconnection(host, port, outfile, infile) 297556Seric ** Make a connection to the named host on the given 307556Seric ** port. Set *outfile and *infile to the files 317556Seric ** appropriate for communication. Returns zero on 327556Seric ** success, else an exit status describing the 337556Seric ** error. 347556Seric ** 357556Seric ** The semantics of both of these should be clean. 364535Seric */ 374535Seric /* 384535Seric ** GETREQUESTS -- open mail IPC port and get requests. 394535Seric ** 404535Seric ** Parameters: 414535Seric ** none. 424535Seric ** 434535Seric ** Returns: 444535Seric ** none. 454535Seric ** 464535Seric ** Side Effects: 474535Seric ** Waits until some interesting activity occurs. When 484535Seric ** it does, a child is created to process it, and the 494535Seric ** parent waits for completion. Return from this 504535Seric ** routine is always in the child. 514535Seric */ 524535Seric 537117Seric # define MAXCONNS 4 /* maximum simultaneous sendmails */ 547117Seric 554535Seric getrequests() 564535Seric { 577117Seric union wait status; 587117Seric int numconnections = 0; 597117Seric 604631Seric for (;;) 614631Seric { 624636Seric register int pid; 635978Seric register int port; 644631Seric 654636Seric /* 664636Seric ** Wait for a connection. 674636Seric */ 684631Seric 695978Seric while ((port = getconnection()) < 0) 705978Seric { 71*8518Seric # ifdef LOG 72*8518Seric if (LogLevel > 0) 73*8518Seric syslog(LOG_SALERT, "cannot get connection"); 74*8518Seric # endif LOG 757556Seric finis(); 765978Seric } 774631Seric 785978Seric /* 795978Seric ** Create a subprocess to process the mail. 805978Seric */ 815978Seric 825978Seric # ifdef DEBUG 837677Seric if (tTd(15, 2)) 845978Seric printf("getrequests: forking (port = %d)\n", port); 855978Seric # endif DEBUG 865978Seric 874636Seric pid = fork(); 884636Seric if (pid < 0) 894631Seric { 904636Seric syserr("daemon: cannot fork"); 914636Seric sleep(10); 927009Seric (void) close(port); 934636Seric continue; 944631Seric } 954631Seric 964636Seric if (pid == 0) 974631Seric { 984636Seric /* 994636Seric ** CHILD -- return to caller. 1004636Seric ** Verify calling user id if possible here. 1014636Seric */ 1024631Seric 1035978Seric InChannel = fdopen(port, "r"); 1045978Seric OutChannel = fdopen(port, "w"); 1055978Seric # ifdef DEBUG 1067677Seric if (tTd(15, 2)) 1075978Seric printf("getreq: returning\n"); 1085978Seric # endif DEBUG 1097876Seric # ifdef LOG 1107876Seric if (LogLevel > 11) 1117876Seric syslog(LOG_DEBUG, "connected, pid=%d", getpid()); 1127876Seric # endif LOG 1134636Seric return; 1144631Seric } 1154631Seric 1164636Seric /* 1174636Seric ** PARENT -- wait for child to terminate. 1184636Seric ** Perhaps we should allow concurrent processing? 1194636Seric */ 1204631Seric 1215978Seric # ifdef DEBUG 1227677Seric if (tTd(15, 2)) 1235978Seric { 1245978Seric sleep(2); 1255978Seric printf("getreq: parent waiting\n"); 1265978Seric } 1275978Seric # endif DEBUG 1285978Seric 1297117Seric /* close the port so that others will hang (for a while) */ 1307009Seric (void) close(port); 1317117Seric 1327117Seric /* pick up old zombies; implement load limiting */ 1337117Seric numconnections++; 1347117Seric while (wait3(&status, numconnections < MAXCONNS ? WNOHANG : 0, 0) > 0) 1357117Seric numconnections--; 1364631Seric } 1374631Seric } 1385978Seric /* 1395978Seric ** GETCONNECTION -- make a connection with the outside world 1405978Seric ** 1415978Seric ** Parameters: 1425978Seric ** none. 1435978Seric ** 1445978Seric ** Returns: 1455978Seric ** The port for mail traffic. 1465978Seric ** 1475978Seric ** Side Effects: 1485978Seric ** Waits for a connection. 1495978Seric */ 1505978Seric 1517117Seric #define IPPORT_PLAYPORT 3055 /* random number */ 1527117Seric 1535979Seric struct sockaddr_in SendmailAddress = { AF_INET, IPPORT_SMTP }; 1545978Seric 1555978Seric getconnection() 1565978Seric { 1575978Seric register int s; 1585978Seric struct sockaddr otherend; 1595978Seric 1605978Seric /* 1615978Seric ** Set up the address for the mailer. 1625978Seric */ 1635978Seric 1646058Seric SendmailAddress.sin_addr.s_addr = 0; 1656633Seric SendmailAddress.sin_port = IPPORT_SMTP; 1667117Seric # ifdef DEBUG 1677677Seric if (tTd(15, 15)) 1687117Seric SendmailAddress.sin_port = IPPORT_PLAYPORT; 1697117Seric # endif DEBUG 1707117Seric SendmailAddress.sin_port = htons(SendmailAddress.sin_port); 1715978Seric 1725978Seric /* 1735978Seric ** Try to actually open the connection. 1745978Seric */ 1755978Seric 1765978Seric # ifdef DEBUG 1777677Seric if (tTd(15, 1)) 1786058Seric printf("getconnection\n"); 1795978Seric # endif DEBUG 1805978Seric 1818276Seric for (;;) 1827117Seric { 1838352Seric int i; 1848352Seric 1857556Seric /* get a socket for the SMTP connection */ 1868352Seric /* do loop is to avoid 4.1b kernel bug (?) */ 1878352Seric i = 50; 1888352Seric do 1898352Seric { 1908352Seric s = socket(SOCK_STREAM, 0, &SendmailAddress, SO_ACCEPTCONN); 1918352Seric if (s < 0) 1928352Seric sleep(5); 1938352Seric } while (--i > 0 && s < 0); 1947556Seric if (s < 0) 1957556Seric { 1967556Seric /* probably another daemon already */ 1977556Seric syserr("getconnection: can't create socket"); 1987892Seric return (-1); 1997556Seric } 2005978Seric 2015978Seric # ifdef DEBUG 2027677Seric if (tTd(15, 1)) 2037556Seric printf("getconnection: %d\n", s); 2045978Seric # endif DEBUG 2057556Seric 2067556Seric /* wait for a connection */ 2078276Seric /* contorted code is due to a 4.1a kernel bug */ 2088276Seric errno = 0; 2098276Seric if (accept(s, &otherend) >= 0) 2108276Seric return (s); 2118276Seric (void) close(s); 2128276Seric if (errno != EINTR) 2138276Seric syserr("getconnection: accept"); 2148351Seric sleep(5); 2157117Seric } 2165978Seric } 2176039Seric /* 2186039Seric ** MAKECONNECTION -- make a connection to an SMTP socket on another machine. 2196039Seric ** 2206039Seric ** Parameters: 2216039Seric ** host -- the name of the host. 2226633Seric ** port -- the port number to connect to. 2236039Seric ** outfile -- a pointer to a place to put the outfile 2246039Seric ** descriptor. 2256039Seric ** infile -- ditto for infile. 2266039Seric ** 2276039Seric ** Returns: 2286039Seric ** An exit code telling whether the connection could be 2296039Seric ** made and if not why not. 2306039Seric ** 2316039Seric ** Side Effects: 2326039Seric ** none. 2336039Seric */ 2345978Seric 2356633Seric makeconnection(host, port, outfile, infile) 2366039Seric char *host; 2377286Seric u_short port; 2386039Seric FILE **outfile; 2396039Seric FILE **infile; 2406039Seric { 2416039Seric register int s; 2426039Seric 2436039Seric /* 2446039Seric ** Set up the address for the mailer. 2456039Seric */ 2466039Seric 2476039Seric if ((SendmailAddress.sin_addr.s_addr = rhost(&host)) == -1) 2486039Seric return (EX_NOHOST); 2496633Seric if (port == 0) 2506633Seric port = IPPORT_SMTP; 2517117Seric SendmailAddress.sin_port = htons(port); 2526039Seric 2536039Seric /* 2546039Seric ** Try to actually open the connection. 2556039Seric */ 2566039Seric 2576039Seric # ifdef DEBUG 2587677Seric if (tTd(16, 1)) 2596039Seric printf("makeconnection (%s)\n", host); 2606039Seric # endif DEBUG 2616039Seric 2627009Seric s = socket(SOCK_STREAM, 0, (struct sockaddr_in *) 0, 0); 2636039Seric if (s < 0) 2646039Seric { 2656039Seric syserr("makeconnection: no socket"); 2666039Seric goto failure; 2676039Seric } 2686039Seric 2696039Seric # ifdef DEBUG 2707677Seric if (tTd(16, 1)) 2716039Seric printf("makeconnection: %d\n", s); 2726039Seric # endif DEBUG 2737677Seric (void) fflush(Xscript); /* for debugging */ 2746039Seric if (connect(s, &SendmailAddress) < 0) 2756039Seric { 2766039Seric /* failure, decide if temporary or not */ 2776039Seric failure: 2786039Seric switch (errno) 2796039Seric { 2806039Seric case EISCONN: 2816039Seric case ETIMEDOUT: 2826897Seric case EINPROGRESS: 2836897Seric case EALREADY: 2846897Seric case EADDRINUSE: 2856897Seric case ENETDOWN: 2866897Seric case ENETRESET: 2876897Seric case ENOBUFS: 2887204Seric case ECONNREFUSED: 2896039Seric /* there are others, I'm sure..... */ 2906039Seric return (EX_TEMPFAIL); 2916039Seric 2926039Seric default: 2936039Seric return (EX_UNAVAILABLE); 2946039Seric } 2956039Seric } 2966039Seric 2976039Seric /* connection ok, put it into canonical form */ 2986039Seric *outfile = fdopen(s, "w"); 2996039Seric *infile = fdopen(s, "r"); 3006039Seric 3016039Seric return (0); 3026039Seric } 3036039Seric 3045978Seric #endif DAEMON 305