14535Seric # include "sendmail.h"
24535Seric 
3*5978Seric #ifndef DAEMON
4*5978Seric SCCSID(@(#)daemon.c	3.6		02/26/82	(w/o daemon mode));
5*5978Seric #else
64535Seric 
7*5978Seric # include <sys/socket.h>
8*5978Seric # include <wellknown.h>
9*5978Seric # include <net/in.h>
10*5978Seric 
11*5978Seric SCCSID(@(#)daemon.c	3.6		02/26/82	(with daemon mode));
12*5978Seric 
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;
38*5978Seric 		register int port;
394631Seric 
404636Seric 		/*
414636Seric 		**  Wait for a connection.
424636Seric 		*/
434631Seric 
44*5978Seric 		while ((port = getconnection()) < 0)
45*5978Seric 		{
46*5978Seric 			syserr("getrequests: getconnection failed");
47*5978Seric 			sleep(30);
48*5978Seric 		}
494631Seric 
50*5978Seric 		/*
51*5978Seric 		**  Create a subprocess to process the mail.
52*5978Seric 		*/
53*5978Seric 
54*5978Seric # ifdef DEBUG
55*5978Seric 		if (Debug > 1)
56*5978Seric 			printf("getrequests: forking (port = %d)\n", port);
57*5978Seric # endif DEBUG
58*5978Seric 
594636Seric 		pid = fork();
604636Seric 		if (pid < 0)
614631Seric 		{
624636Seric 			syserr("daemon: cannot fork");
634636Seric 			sleep(10);
64*5978Seric 			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 
75*5978Seric 			InChannel = fdopen(port, "r");
76*5978Seric 			OutChannel = fdopen(port, "w");
774636Seric 			initsys();
78*5978Seric # ifdef DEBUG
79*5978Seric 			if (Debug > 1)
80*5978Seric 				printf("getreq: returning\n");
81*5978Seric # endif DEBUG
824636Seric 			return;
834631Seric 		}
844631Seric 
854636Seric 		/*
864636Seric 		**  PARENT -- wait for child to terminate.
874636Seric 		**	Perhaps we should allow concurrent processing?
884636Seric 		*/
894631Seric 
90*5978Seric # ifdef DEBUG
91*5978Seric 		if (Debug > 1)
92*5978Seric 		{
93*5978Seric 			sleep(2);
94*5978Seric 			printf("getreq: parent waiting\n");
95*5978Seric 		}
96*5978Seric # endif DEBUG
97*5978Seric 
984836Seric 		(void) wait(&st);
99*5978Seric 		close(port);
1004631Seric 	}
1014631Seric }
102*5978Seric /*
103*5978Seric **  GETCONNECTION -- make a connection with the outside world
104*5978Seric **
105*5978Seric **	Parameters:
106*5978Seric **		none.
107*5978Seric **
108*5978Seric **	Returns:
109*5978Seric **		The port for mail traffic.
110*5978Seric **
111*5978Seric **	Side Effects:
112*5978Seric **		Waits for a connection.
113*5978Seric */
114*5978Seric 
115*5978Seric struct sockaddr_in SendmailAddress = { AF_INET, IPPORT_SENDMAIL };
116*5978Seric 
117*5978Seric getconnection()
118*5978Seric {
119*5978Seric 	register int s;
120*5978Seric 	char *host = "localhost";
121*5978Seric 	struct sockaddr otherend;
122*5978Seric 
123*5978Seric 	/*
124*5978Seric 	**  Set up the address for the mailer.
125*5978Seric 	*/
126*5978Seric 
127*5978Seric 	SendmailAddress.sin_addr.s_addr = rhost(&host);
128*5978Seric 
129*5978Seric 	/*
130*5978Seric 	**  Try to actually open the connection.
131*5978Seric 	*/
132*5978Seric 
133*5978Seric # ifdef DEBUG
134*5978Seric 	if (Debug)
135*5978Seric 		printf("getconnection (%s)\n", host);
136*5978Seric # endif DEBUG
137*5978Seric 
138*5978Seric 	s = socket(SOCK_STREAM, 0, &SendmailAddress, SO_ACCEPTCONN);
139*5978Seric 
140*5978Seric # ifdef DEBUG
141*5978Seric 	if (Debug)
142*5978Seric 		printf("getconnection: %d\n", s);
143*5978Seric # endif DEBUG
144*5978Seric 	accept(s, &otherend);
145*5978Seric 
146*5978Seric 	return (s);
147*5978Seric }
148*5978Seric 
149*5978Seric #endif DAEMON
150