16039Seric # include <errno.h> 24535Seric # include "sendmail.h" 34535Seric 45978Seric #ifndef DAEMON 5*8276Seric SCCSID(@(#)daemon.c 3.28 09/30/82 (w/o daemon mode)); 65978Seric #else 74535Seric 85978Seric # include <sys/socket.h> 95978Seric # include <net/in.h> 107117Seric # include <wait.h> 115978Seric 12*8276Seric SCCSID(@(#)daemon.c 3.28 09/30/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 { 715978Seric syserr("getrequests: getconnection failed"); 727556Seric finis(); 735978Seric } 744631Seric 755978Seric /* 765978Seric ** Create a subprocess to process the mail. 775978Seric */ 785978Seric 795978Seric # ifdef DEBUG 807677Seric if (tTd(15, 2)) 815978Seric printf("getrequests: forking (port = %d)\n", port); 825978Seric # endif DEBUG 835978Seric 844636Seric pid = fork(); 854636Seric if (pid < 0) 864631Seric { 874636Seric syserr("daemon: cannot fork"); 884636Seric sleep(10); 897009Seric (void) close(port); 904636Seric continue; 914631Seric } 924631Seric 934636Seric if (pid == 0) 944631Seric { 954636Seric /* 964636Seric ** CHILD -- return to caller. 974636Seric ** Verify calling user id if possible here. 984636Seric */ 994631Seric 1005978Seric InChannel = fdopen(port, "r"); 1015978Seric OutChannel = fdopen(port, "w"); 1025978Seric # ifdef DEBUG 1037677Seric if (tTd(15, 2)) 1045978Seric printf("getreq: returning\n"); 1055978Seric # endif DEBUG 1067876Seric # ifdef LOG 1077876Seric if (LogLevel > 11) 1087876Seric syslog(LOG_DEBUG, "connected, pid=%d", getpid()); 1097876Seric # endif LOG 1104636Seric return; 1114631Seric } 1124631Seric 1134636Seric /* 1144636Seric ** PARENT -- wait for child to terminate. 1154636Seric ** Perhaps we should allow concurrent processing? 1164636Seric */ 1174631Seric 1185978Seric # ifdef DEBUG 1197677Seric if (tTd(15, 2)) 1205978Seric { 1215978Seric sleep(2); 1225978Seric printf("getreq: parent waiting\n"); 1235978Seric } 1245978Seric # endif DEBUG 1255978Seric 1267117Seric /* close the port so that others will hang (for a while) */ 1277009Seric (void) close(port); 1287117Seric 1297117Seric /* pick up old zombies; implement load limiting */ 1307117Seric numconnections++; 1317117Seric while (wait3(&status, numconnections < MAXCONNS ? WNOHANG : 0, 0) > 0) 1327117Seric numconnections--; 1334631Seric } 1344631Seric } 1355978Seric /* 1365978Seric ** GETCONNECTION -- make a connection with the outside world 1375978Seric ** 1385978Seric ** Parameters: 1395978Seric ** none. 1405978Seric ** 1415978Seric ** Returns: 1425978Seric ** The port for mail traffic. 1435978Seric ** 1445978Seric ** Side Effects: 1455978Seric ** Waits for a connection. 1465978Seric */ 1475978Seric 1487117Seric #define IPPORT_PLAYPORT 3055 /* random number */ 1497117Seric 1505979Seric struct sockaddr_in SendmailAddress = { AF_INET, IPPORT_SMTP }; 1515978Seric 1525978Seric getconnection() 1535978Seric { 1545978Seric register int s; 1555978Seric struct sockaddr otherend; 1565978Seric 1575978Seric /* 1585978Seric ** Set up the address for the mailer. 1595978Seric */ 1605978Seric 1616058Seric SendmailAddress.sin_addr.s_addr = 0; 1626633Seric SendmailAddress.sin_port = IPPORT_SMTP; 1637117Seric # ifdef DEBUG 1647677Seric if (tTd(15, 15)) 1657117Seric SendmailAddress.sin_port = IPPORT_PLAYPORT; 1667117Seric # endif DEBUG 1677117Seric SendmailAddress.sin_port = htons(SendmailAddress.sin_port); 1685978Seric 1695978Seric /* 1705978Seric ** Try to actually open the connection. 1715978Seric */ 1725978Seric 1735978Seric # ifdef DEBUG 1747677Seric if (tTd(15, 1)) 1756058Seric printf("getconnection\n"); 1765978Seric # endif DEBUG 1775978Seric 178*8276Seric for (;;) 1797117Seric { 1807556Seric /* get a socket for the SMTP connection */ 1817556Seric s = socket(SOCK_STREAM, 0, &SendmailAddress, SO_ACCEPTCONN); 1827556Seric if (s < 0) 1837556Seric { 1847556Seric /* probably another daemon already */ 1857556Seric syserr("getconnection: can't create socket"); 1867892Seric return (-1); 1877556Seric } 1885978Seric 1895978Seric # ifdef DEBUG 1907677Seric if (tTd(15, 1)) 1917556Seric printf("getconnection: %d\n", s); 1925978Seric # endif DEBUG 1937556Seric 1947556Seric /* wait for a connection */ 195*8276Seric /* contorted code is due to a 4.1a kernel bug */ 196*8276Seric errno = 0; 197*8276Seric if (accept(s, &otherend) >= 0) 198*8276Seric return (s); 199*8276Seric (void) close(s); 200*8276Seric if (errno != EINTR) 2017892Seric { 202*8276Seric syserr("getconnection: accept"); 203*8276Seric sleep(5); 204*8276Seric } 2057117Seric } 2065978Seric } 2076039Seric /* 2086039Seric ** MAKECONNECTION -- make a connection to an SMTP socket on another machine. 2096039Seric ** 2106039Seric ** Parameters: 2116039Seric ** host -- the name of the host. 2126633Seric ** port -- the port number to connect to. 2136039Seric ** outfile -- a pointer to a place to put the outfile 2146039Seric ** descriptor. 2156039Seric ** infile -- ditto for infile. 2166039Seric ** 2176039Seric ** Returns: 2186039Seric ** An exit code telling whether the connection could be 2196039Seric ** made and if not why not. 2206039Seric ** 2216039Seric ** Side Effects: 2226039Seric ** none. 2236039Seric */ 2245978Seric 2256633Seric makeconnection(host, port, outfile, infile) 2266039Seric char *host; 2277286Seric u_short port; 2286039Seric FILE **outfile; 2296039Seric FILE **infile; 2306039Seric { 2316039Seric register int s; 2326039Seric 2336039Seric /* 2346039Seric ** Set up the address for the mailer. 2356039Seric */ 2366039Seric 2376039Seric if ((SendmailAddress.sin_addr.s_addr = rhost(&host)) == -1) 2386039Seric return (EX_NOHOST); 2396633Seric if (port == 0) 2406633Seric port = IPPORT_SMTP; 2417117Seric SendmailAddress.sin_port = htons(port); 2426039Seric 2436039Seric /* 2446039Seric ** Try to actually open the connection. 2456039Seric */ 2466039Seric 2476039Seric # ifdef DEBUG 2487677Seric if (tTd(16, 1)) 2496039Seric printf("makeconnection (%s)\n", host); 2506039Seric # endif DEBUG 2516039Seric 2527009Seric s = socket(SOCK_STREAM, 0, (struct sockaddr_in *) 0, 0); 2536039Seric if (s < 0) 2546039Seric { 2556039Seric syserr("makeconnection: no socket"); 2566039Seric goto failure; 2576039Seric } 2586039Seric 2596039Seric # ifdef DEBUG 2607677Seric if (tTd(16, 1)) 2616039Seric printf("makeconnection: %d\n", s); 2626039Seric # endif DEBUG 2637677Seric (void) fflush(Xscript); /* for debugging */ 2646039Seric if (connect(s, &SendmailAddress) < 0) 2656039Seric { 2666039Seric /* failure, decide if temporary or not */ 2676039Seric failure: 2686039Seric switch (errno) 2696039Seric { 2706039Seric case EISCONN: 2716039Seric case ETIMEDOUT: 2726897Seric case EINPROGRESS: 2736897Seric case EALREADY: 2746897Seric case EADDRINUSE: 2756897Seric case ENETDOWN: 2766897Seric case ENETRESET: 2776897Seric case ENOBUFS: 2787204Seric case ECONNREFUSED: 2796039Seric /* there are others, I'm sure..... */ 2806039Seric return (EX_TEMPFAIL); 2816039Seric 2826039Seric default: 2836039Seric return (EX_UNAVAILABLE); 2846039Seric } 2856039Seric } 2866039Seric 2876039Seric /* connection ok, put it into canonical form */ 2886039Seric *outfile = fdopen(s, "w"); 2896039Seric *infile = fdopen(s, "r"); 2906039Seric 2916039Seric return (0); 2926039Seric } 2936039Seric 2945978Seric #endif DAEMON 295