xref: /original-bsd/usr.sbin/sendmail/src/daemon.c (revision c05f5e42)
1 # include <errno.h>
2 # include "sendmail.h"
3 
4 #ifndef DAEMON
5 SCCSID(@(#)daemon.c	3.8		03/05/82	(w/o daemon mode));
6 #else
7 
8 # include <sys/socket.h>
9 # include <wellknown.h>
10 # include <net/in.h>
11 
12 SCCSID(@(#)daemon.c	3.8		03/05/82	(with daemon mode));
13 
14 /*
15 **  DAEMON.C -- routines to use when running as a daemon.
16 */
17 /*
18 **  GETREQUESTS -- open mail IPC port and get requests.
19 **
20 **	Parameters:
21 **		none.
22 **
23 **	Returns:
24 **		none.
25 **
26 **	Side Effects:
27 **		Waits until some interesting activity occurs.  When
28 **		it does, a child is created to process it, and the
29 **		parent waits for completion.  Return from this
30 **		routine is always in the child.
31 */
32 
33 getrequests()
34 {
35 	for (;;)
36 	{
37 		register int pid;
38 		auto int st;
39 		register int port;
40 
41 		/*
42 		**  Wait for a connection.
43 		*/
44 
45 		while ((port = getconnection()) < 0)
46 		{
47 			syserr("getrequests: getconnection failed");
48 			sleep(30);
49 		}
50 
51 		/*
52 		**  Create a subprocess to process the mail.
53 		*/
54 
55 # ifdef DEBUG
56 		if (Debug > 1)
57 			printf("getrequests: forking (port = %d)\n", port);
58 # endif DEBUG
59 
60 		pid = fork();
61 		if (pid < 0)
62 		{
63 			syserr("daemon: cannot fork");
64 			sleep(10);
65 			close(port);
66 			continue;
67 		}
68 
69 		if (pid == 0)
70 		{
71 			/*
72 			**  CHILD -- return to caller.
73 			**	Verify calling user id if possible here.
74 			*/
75 
76 			InChannel = fdopen(port, "r");
77 			OutChannel = fdopen(port, "w");
78 			initsys();
79 # ifdef DEBUG
80 			if (Debug > 1)
81 				printf("getreq: returning\n");
82 # endif DEBUG
83 			return;
84 		}
85 
86 		/*
87 		**  PARENT -- wait for child to terminate.
88 		**	Perhaps we should allow concurrent processing?
89 		*/
90 
91 # ifdef DEBUG
92 		if (Debug > 1)
93 		{
94 			sleep(2);
95 			printf("getreq: parent waiting\n");
96 		}
97 # endif DEBUG
98 
99 		(void) wait(&st);
100 		close(port);
101 	}
102 }
103 /*
104 **  GETCONNECTION -- make a connection with the outside world
105 **
106 **	Parameters:
107 **		none.
108 **
109 **	Returns:
110 **		The port for mail traffic.
111 **
112 **	Side Effects:
113 **		Waits for a connection.
114 */
115 
116 struct sockaddr_in SendmailAddress = { AF_INET, IPPORT_SMTP };
117 
118 getconnection()
119 {
120 	register int s;
121 	char *host = "localhost";
122 	struct sockaddr otherend;
123 
124 	/*
125 	**  Set up the address for the mailer.
126 	*/
127 
128 	SendmailAddress.sin_addr.s_addr = rhost(&host);
129 
130 	/*
131 	**  Try to actually open the connection.
132 	*/
133 
134 # ifdef DEBUG
135 	if (Debug)
136 		printf("getconnection (%s)\n", host);
137 # endif DEBUG
138 
139 	s = socket(SOCK_STREAM, 0, &SendmailAddress, SO_ACCEPTCONN);
140 
141 # ifdef DEBUG
142 	if (Debug)
143 		printf("getconnection: %d\n", s);
144 # endif DEBUG
145 	accept(s, &otherend);
146 
147 	return (s);
148 }
149 /*
150 **  MAKECONNECTION -- make a connection to an SMTP socket on another machine.
151 **
152 **	Parameters:
153 **		host -- the name of the host.
154 **		outfile -- a pointer to a place to put the outfile
155 **			descriptor.
156 **		infile -- ditto for infile.
157 **
158 **	Returns:
159 **		An exit code telling whether the connection could be
160 **			made and if not why not.
161 **
162 **	Side Effects:
163 **		none.
164 */
165 
166 makeconnection(host, outfile, infile)
167 	char *host;
168 	FILE **outfile;
169 	FILE **infile;
170 {
171 	register int s;
172 
173 	/*
174 	**  Set up the address for the mailer.
175 	*/
176 
177 	if ((SendmailAddress.sin_addr.s_addr = rhost(&host)) == -1)
178 		return (EX_NOHOST);
179 
180 	/*
181 	**  Try to actually open the connection.
182 	*/
183 
184 # ifdef DEBUG
185 	if (Debug)
186 		printf("makeconnection (%s)\n", host);
187 # endif DEBUG
188 
189 	s = socket(SOCK_STREAM, 0, 0, 0);
190 	if (s < 0)
191 	{
192 		syserr("makeconnection: no socket");
193 		goto failure;
194 	}
195 
196 # ifdef DEBUG
197 	if (Debug)
198 		printf("makeconnection: %d\n", s);
199 # endif DEBUG
200 	if (connect(s, &SendmailAddress) < 0)
201 	{
202 		/* failure, decide if temporary or not */
203 	failure:
204 		switch (errno)
205 		{
206 		  case EISCONN:
207 		  case ETIMEDOUT:
208 			/* there are others, I'm sure..... */
209 			return (EX_TEMPFAIL);
210 
211 		  default:
212 			return (EX_UNAVAILABLE);
213 		}
214 	}
215 
216 	/* connection ok, put it into canonical form */
217 	*outfile = fdopen(s, "w");
218 	*infile = fdopen(s, "r");
219 
220 	return (0);
221 }
222 
223 #endif DAEMON
224