14535Seric # include "sendmail.h"
24535Seric 
35978Seric #ifndef DAEMON
4*5979Seric SCCSID(@(#)daemon.c	3.7		02/26/82	(w/o daemon mode));
55978Seric #else
64535Seric 
75978Seric # include <sys/socket.h>
85978Seric # include <wellknown.h>
95978Seric # include <net/in.h>
105978Seric 
11*5979Seric SCCSID(@(#)daemon.c	3.7		02/26/82	(with daemon mode));
125978Seric 
134535Seric /*
144535Seric **  DAEMON.C -- routines to use when running as a daemon.
154535Seric */
164535Seric /*
174535Seric **  GETREQUESTS -- open mail IPC port and get requests.
184535Seric **
194535Seric **	Parameters:
204535Seric **		none.
214535Seric **
224535Seric **	Returns:
234535Seric **		none.
244535Seric **
254535Seric **	Side Effects:
264535Seric **		Waits until some interesting activity occurs.  When
274535Seric **		it does, a child is created to process it, and the
284535Seric **		parent waits for completion.  Return from this
294535Seric **		routine is always in the child.
304535Seric */
314535Seric 
324535Seric getrequests()
334535Seric {
344631Seric 	for (;;)
354631Seric 	{
364636Seric 		register int pid;
374636Seric 		auto int st;
385978Seric 		register int port;
394631Seric 
404636Seric 		/*
414636Seric 		**  Wait for a connection.
424636Seric 		*/
434631Seric 
445978Seric 		while ((port = getconnection()) < 0)
455978Seric 		{
465978Seric 			syserr("getrequests: getconnection failed");
475978Seric 			sleep(30);
485978Seric 		}
494631Seric 
505978Seric 		/*
515978Seric 		**  Create a subprocess to process the mail.
525978Seric 		*/
535978Seric 
545978Seric # ifdef DEBUG
555978Seric 		if (Debug > 1)
565978Seric 			printf("getrequests: forking (port = %d)\n", port);
575978Seric # endif DEBUG
585978Seric 
594636Seric 		pid = fork();
604636Seric 		if (pid < 0)
614631Seric 		{
624636Seric 			syserr("daemon: cannot fork");
634636Seric 			sleep(10);
645978Seric 			close(port);
654636Seric 			continue;
664631Seric 		}
674631Seric 
684636Seric 		if (pid == 0)
694631Seric 		{
704636Seric 			/*
714636Seric 			**  CHILD -- return to caller.
724636Seric 			**	Verify calling user id if possible here.
734636Seric 			*/
744631Seric 
755978Seric 			InChannel = fdopen(port, "r");
765978Seric 			OutChannel = fdopen(port, "w");
774636Seric 			initsys();
785978Seric # ifdef DEBUG
795978Seric 			if (Debug > 1)
805978Seric 				printf("getreq: returning\n");
815978Seric # endif DEBUG
824636Seric 			return;
834631Seric 		}
844631Seric 
854636Seric 		/*
864636Seric 		**  PARENT -- wait for child to terminate.
874636Seric 		**	Perhaps we should allow concurrent processing?
884636Seric 		*/
894631Seric 
905978Seric # ifdef DEBUG
915978Seric 		if (Debug > 1)
925978Seric 		{
935978Seric 			sleep(2);
945978Seric 			printf("getreq: parent waiting\n");
955978Seric 		}
965978Seric # endif DEBUG
975978Seric 
984836Seric 		(void) wait(&st);
995978Seric 		close(port);
1004631Seric 	}
1014631Seric }
1025978Seric /*
1035978Seric **  GETCONNECTION -- make a connection with the outside world
1045978Seric **
1055978Seric **	Parameters:
1065978Seric **		none.
1075978Seric **
1085978Seric **	Returns:
1095978Seric **		The port for mail traffic.
1105978Seric **
1115978Seric **	Side Effects:
1125978Seric **		Waits for a connection.
1135978Seric */
1145978Seric 
115*5979Seric struct sockaddr_in SendmailAddress = { AF_INET, IPPORT_SMTP };
1165978Seric 
1175978Seric getconnection()
1185978Seric {
1195978Seric 	register int s;
1205978Seric 	char *host = "localhost";
1215978Seric 	struct sockaddr otherend;
1225978Seric 
1235978Seric 	/*
1245978Seric 	**  Set up the address for the mailer.
1255978Seric 	*/
1265978Seric 
1275978Seric 	SendmailAddress.sin_addr.s_addr = rhost(&host);
1285978Seric 
1295978Seric 	/*
1305978Seric 	**  Try to actually open the connection.
1315978Seric 	*/
1325978Seric 
1335978Seric # ifdef DEBUG
1345978Seric 	if (Debug)
1355978Seric 		printf("getconnection (%s)\n", host);
1365978Seric # endif DEBUG
1375978Seric 
1385978Seric 	s = socket(SOCK_STREAM, 0, &SendmailAddress, SO_ACCEPTCONN);
1395978Seric 
1405978Seric # ifdef DEBUG
1415978Seric 	if (Debug)
1425978Seric 		printf("getconnection: %d\n", s);
1435978Seric # endif DEBUG
1445978Seric 	accept(s, &otherend);
1455978Seric 
1465978Seric 	return (s);
1475978Seric }
1485978Seric 
1495978Seric #endif DAEMON
150