xref: /original-bsd/usr.sbin/sendmail/src/queue.c (revision a304ca22)
1 # include "sendmail.h"
2 # include <sys/stat.h>
3 # include <sys/dir.h>
4 # include <signal.h>
5 # include <errno.h>
6 
7 # ifndef QUEUE
8 SCCSID(@(#)queue.c	3.11		03/06/82	(no queueing));
9 # else QUEUE
10 
11 SCCSID(@(#)queue.c	3.11		03/06/82);
12 
13 /*
14 **  QUEUEUP -- queue a message up for future transmission.
15 **
16 **	The queued message should already be in the correct place.
17 **	This routine just outputs the control file as appropriate.
18 **
19 **	Parameters:
20 **		df -- location of the data file.  The name will
21 **			be transformed into a control file name.
22 **
23 **	Returns:
24 **		none.
25 **
26 **	Side Effects:
27 **		The current request (only unsatisfied addresses)
28 **			are saved in a control file.
29 */
30 
31 queueup(df)
32 	char *df;
33 {
34 	char cf[MAXNAME];
35 	register FILE *f;
36 	register HDR *h;
37 	register ADDRESS *q;
38 	extern char *mktemp();
39 	register int i;
40 
41 	/*
42 	**  Create control file.
43 	*/
44 
45 	strcpy(cf, QueueDir);
46 	strcat(cf, "/cfXXXXXX");
47 	(void) mktemp(cf);
48 	f = fopen(cf, "w");
49 	if (f == NULL)
50 	{
51 		syserr("queueup: cannot create control file %s", cf);
52 		return;
53 	}
54 
55 # ifdef DEBUG
56 	if (Debug)
57 		printf("queued in %s\n", cf);
58 # endif DEBUG
59 
60 	/*
61 	**  Output future work requests.
62 	*/
63 
64 	/* output name of data file */
65 	fprintf(f, "D%s\n", df);
66 
67 	/* output name of sender */
68 	fprintf(f, "S%s\n", From.q_paddr);
69 
70 	/* output timeout */
71 	fprintf(f, "T%ld\n", TimeOut);
72 
73 	/* output message priority */
74 	fprintf(f, "P%ld\n", MsgPriority);
75 
76 	/* output macro definitions */
77 	for (i = 0; i < 128; i++)
78 	{
79 		extern char *Macro[128];
80 		register char *p = Macro[i];
81 
82 		if (p != NULL && i != (int) 'b')
83 			fprintf(f, "M%c%s\n", i, p);
84 	}
85 
86 	/* output list of recipient addresses */
87 	for (q = SendQueue; q != NULL; q = q->q_next)
88 	{
89 # ifdef DEBUG
90 		if (Debug > 0)
91 		{
92 			printf("queueing ");
93 			printaddr(q, FALSE);
94 		}
95 # endif DEBUG
96 		if (bitset(QQUEUEUP, q->q_flags))
97 			fprintf(f, "R%s\n", q->q_paddr);
98 	}
99 
100 	/* output headers for this message */
101 	for (h = Header; h != NULL; h = h->h_link)
102 	{
103 		if (h->h_value == NULL || h->h_value[0] == '\0')
104 			continue;
105 		fprintf(f, "H");
106 		if (h->h_mflags != 0 && bitset(H_CHECK|H_ACHECK, h->h_flags))
107 			mfdecode(h->h_mflags, f);
108 		fprintf(f, "%s: %s\n", h->h_field, h->h_value);
109 	}
110 
111 	/*
112 	**  Clean up.
113 	*/
114 
115 	(void) fclose(f);
116 }
117 /*
118 **  RUNQUEUE -- run the jobs in the queue.
119 **
120 **	Gets the stuff out of the queue in some presumably logical
121 **	order and processes them.
122 **
123 **	Parameters:
124 **		none.
125 **
126 **	Returns:
127 **		none.
128 **
129 **	Side Effects:
130 **		runs things in the mail queue.
131 */
132 
133 bool	ReorderQueue;		/* if set, reorder the send queue */
134 int	QueuePid;		/* pid of child running queue */
135 
136 runqueue(forkflag)
137 	bool forkflag;
138 {
139 	extern reordersig();
140 
141 	if (QueueIntvl != 0)
142 	{
143 		(void) signal(SIGALRM, reordersig);
144 		(void) alarm((unsigned) QueueIntvl);
145 	}
146 
147 	if (forkflag)
148 	{
149 		QueuePid = dofork();
150 		if (QueuePid > 0)
151 		{
152 			/* parent */
153 			return;
154 		}
155 		else
156 			(void) alarm((unsigned) 0);
157 	}
158 
159 	for (;;)
160 	{
161 		/*
162 		**  Order the existing work requests.
163 		*/
164 
165 		orderq();
166 
167 		if (WorkQ == NULL)
168 		{
169 			/* no work?  well, maybe later */
170 			if (QueueIntvl == 0)
171 				break;
172 			pause();
173 			continue;
174 		}
175 
176 		ReorderQueue = FALSE;
177 
178 		/*
179 		**  Process them once at a time.
180 		**	The queue could be reordered while we do this to take
181 		**	new requests into account.  If so, the existing job
182 		**	will be finished but the next thing taken off WorkQ
183 		**	may be something else.
184 		*/
185 
186 		while (WorkQ != NULL)
187 		{
188 			WORK *w = WorkQ;
189 
190 			WorkQ = WorkQ->w_next;
191 			dowork(w);
192 			free(w->w_name);
193 			free((char *) w);
194 			if (ReorderQueue)
195 				break;
196 		}
197 
198 		if (QueueIntvl == 0)
199 			break;
200 	}
201 
202 	/* no work to do -- just exit */
203 	finis();
204 }
205 /*
206 **  REORDERSIG -- catch the alarm signal and tell sendmail to reorder queue.
207 **
208 **	Parameters:
209 **		none.
210 **
211 **	Returns:
212 **		none.
213 **
214 **	Side Effects:
215 **		sets the "reorder work queue" flag.
216 */
217 
218 reordersig()
219 {
220 	if (QueuePid == 0)
221 	{
222 		/* we are in a child doing queueing */
223 		ReorderQueue = TRUE;
224 	}
225 	else
226 	{
227 		/* we are in a parent -- poke child or start new one */
228 		if (kill(QueuePid, SIGALRM) < 0)
229 		{
230 			/* no child -- get zombie & start new one */
231 			static int st;
232 
233 			(void) wait(&st);
234 			QueuePid = dofork();
235 			if (QueuePid == 0)
236 			{
237 				/* new child; run queue */
238 				runqueue(FALSE);
239 				finis();
240 			}
241 		}
242 	}
243 
244 	/*
245 	**  Arrange to get this signal again.
246 	*/
247 
248 	(void) signal(SIGALRM, reordersig);
249 	(void) alarm((unsigned) QueueIntvl);
250 }
251 /*
252 **  ORDERQ -- order the work queue.
253 **
254 **	Parameters:
255 **		none.
256 **
257 **	Returns:
258 **		none.
259 **
260 **	Side Effects:
261 **		Sets WorkQ to the queue of available work, in order.
262 */
263 
264 # define WLSIZE		120	/* max size of worklist per sort */
265 
266 orderq()
267 {
268 	struct direct d;
269 	register WORK *w;
270 	register WORK **wp;		/* parent of w */
271 	register FILE *f;
272 	register int i;
273 	WORK wlist[WLSIZE];
274 	int wn = 0;
275 	extern workcmpf();
276 	extern char *QueueDir;
277 
278 	/* clear out old WorkQ */
279 	for (w = WorkQ; w != NULL; )
280 	{
281 		register WORK *nw = w->w_next;
282 
283 		WorkQ = nw;
284 		free(w->w_name);
285 		free((char *) w);
286 		w = nw;
287 	}
288 
289 	/* open the queue directory */
290 	f = fopen(QueueDir, "r");
291 	if (f == NULL)
292 	{
293 		syserr("orderq: cannot open %s", QueueDir);
294 		return;
295 	}
296 
297 	/*
298 	**  Read the work directory.
299 	*/
300 
301 	while (wn < WLSIZE && fread((char *) &d, sizeof d, 1, f) == 1)
302 	{
303 		char cbuf[MAXNAME];
304 		char lbuf[MAXNAME];
305 		FILE *cf;
306 		register char *p;
307 
308 		/* is this an interesting entry? */
309 		if (d.d_ino == 0 || d.d_name[0] != 'c')
310 			continue;
311 
312 		/* yes -- find the control file location */
313 		strcpy(cbuf, QueueDir);
314 		strcat(cbuf, "/");
315 		p = &cbuf[strlen(cbuf)];
316 		strncpy(p, d.d_name, DIRSIZ);
317 		p[DIRSIZ] = '\0';
318 
319 		/* open control file */
320 		cf = fopen(cbuf, "r");
321 		if (cf == NULL)
322 		{
323 			syserr("orderq: cannot open %s", cbuf);
324 			continue;
325 		}
326 		wlist[wn].w_name = newstr(cbuf);
327 
328 		/* extract useful information */
329 		while (fgets(lbuf, sizeof lbuf, cf) != NULL)
330 		{
331 			fixcrlf(lbuf, TRUE);
332 
333 			switch (lbuf[0])
334 			{
335 			  case 'P':		/* message priority */
336 				(void) sscanf(&lbuf[1], "%ld", &wlist[wn].w_pri);
337 				break;
338 			}
339 		}
340 		wn++;
341 		(void) fclose(cf);
342 	}
343 	(void) fclose(f);
344 
345 	/*
346 	**  Sort the work directory.
347 	*/
348 
349 	qsort(wlist, wn, sizeof *wlist, workcmpf);
350 
351 	/*
352 	**  Convert the work list into canonical form.
353 	*/
354 
355 	wp = &WorkQ;
356 	for (i = 0; i < wn; i++)
357 	{
358 		w = (WORK *) xalloc(sizeof *w);
359 		w->w_name = wlist[i].w_name;
360 		w->w_pri = wlist[i].w_pri;
361 		w->w_next = NULL;
362 		*wp = w;
363 		wp = &w->w_next;
364 	}
365 
366 # ifdef DEBUG
367 	if (Debug)
368 	{
369 		for (w = WorkQ; w != NULL; w = w->w_next)
370 			printf("%32s: pri=%ld\n", w->w_name, w->w_pri);
371 	}
372 # endif DEBUG
373 }
374 /*
375 **	WORKCMPF -- compare function for ordering work.
376 **
377 **	Parameters:
378 **		a -- the first argument.
379 **		b -- the second argument.
380 **
381 **	Returns:
382 **		-1 if a < b
383 **		0 if a == b
384 **		1 if a > b
385 **
386 **	Side Effects:
387 **		none.
388 */
389 
390 # define PRIFACT	1800		/* bytes each priority point is worth */
391 
392 workcmpf(a, b)
393 	register WORK *a;
394 	register WORK *b;
395 {
396 	if (a->w_pri == b->w_pri)
397 		return (0);
398 	else if (a->w_pri > b->w_pri)
399 		return (1);
400 	else
401 		return (-1);
402 }
403 /*
404 **  DOWORK -- do a work request.
405 **
406 **	Parameters:
407 **		w -- the work request to be satisfied.
408 **
409 **	Returns:
410 **		none.
411 **
412 **	Side Effects:
413 **		The work request is satisfied if possible.
414 */
415 
416 dowork(w)
417 	register WORK *w;
418 {
419 	register int i;
420 	auto int xstat;
421 
422 # ifdef DEBUG
423 	if (Debug)
424 		printf("dowork: %s pri %ld\n", w->w_name, w->w_pri);
425 # endif DEBUG
426 
427 	/*
428 	**  Fork for work.
429 	*/
430 
431 	i = fork();
432 	if (i < 0)
433 	{
434 		syserr("dowork: cannot fork");
435 		return;
436 	}
437 
438 	if (i == 0)
439 	{
440 		/*
441 		**  CHILD
442 		*/
443 
444 		QueueRun = TRUE;
445 		openxscrpt();
446 		initsys();
447 		readqf(w->w_name);
448 		sendall(FALSE);
449 # ifdef DEBUG
450 		if (Debug > 2)
451 			printf("CurTime=%ld, TimeOut=%ld\n", CurTime, TimeOut);
452 # endif DEBUG
453 		if (QueueUp && CurTime > TimeOut)
454 			timeout(w);
455 		(void) unlink(w->w_name);
456 		finis();
457 	}
458 
459 	/*
460 	**  Parent -- pick up results.
461 	*/
462 
463 	errno = 0;
464 	while ((i = wait(&xstat)) > 0 && errno != EINTR)
465 	{
466 		if (errno == EINTR)
467 		{
468 			errno = 0;
469 		}
470 	}
471 }
472 /*
473 **  READQF -- read queue file and set up environment.
474 **
475 **	Parameters:
476 **		cf -- name of queue control file.
477 **
478 **	Returns:
479 **		none.
480 **
481 **	Side Effects:
482 **		cf is read and created as the current job, as though
483 **		we had been invoked by argument.
484 */
485 
486 readqf(cf)
487 	char *cf;
488 {
489 	register FILE *f;
490 	char buf[MAXLINE];
491 
492 	/*
493 	**  Open the file created by queueup.
494 	*/
495 
496 	f = fopen(cf, "r");
497 	if (f == NULL)
498 	{
499 		syserr("readqf: no cf file %s", cf);
500 		return;
501 	}
502 
503 	/*
504 	**  Read and process the file.
505 	*/
506 
507 	if (Verbose)
508 		message(Arpa_Info, "Running %s (from %s)", cf, From.q_paddr);
509 
510 	while (fgets(buf, sizeof buf, f) != NULL)
511 	{
512 		fixcrlf(buf, TRUE);
513 
514 		switch (buf[0])
515 		{
516 		  case 'R':		/* specify recipient */
517 			sendto(&buf[1], 1, (ADDRESS *) NULL, &SendQueue);
518 			break;
519 
520 		  case 'H':		/* header */
521 			(void) chompheader(&buf[1], FALSE);
522 			break;
523 
524 		  case 'S':		/* sender */
525 			setsender(newstr(&buf[1]));
526 			break;
527 
528 		  case 'D':		/* data file name */
529 			InFileName = newstr(&buf[1]);
530 			TempFile = fopen(InFileName, "r");
531 			if (TempFile == NULL)
532 				syserr("readqf: cannot open %s", InFileName);
533 			break;
534 
535 		  case 'T':		/* timeout */
536 			(void) sscanf(&buf[1], "%ld", &TimeOut);
537 			break;
538 
539 		  case 'P':		/* message priority */
540 			(void) sscanf(&buf[1], "%ld", &MsgPriority);
541 
542 			/* make sure that big things get sent eventually */
543 			MsgPriority -= WKTIMEFACT;
544 			break;
545 
546 		  case 'M':		/* define macro */
547 			define(buf[1], newstr(&buf[2]));
548 			break;
549 
550 		  default:
551 			syserr("readqf(%s): bad line \"%s\"", cf, buf);
552 			break;
553 		}
554 	}
555 }
556 /*
557 **  TIMEOUT -- process timeout on queue file.
558 **
559 **	Parameters:
560 **		w -- pointer to work request that timed out.
561 **
562 **	Returns:
563 **		none.
564 **
565 **	Side Effects:
566 **		Returns a message to the sender saying that this
567 **		message has timed out.
568 */
569 
570 timeout(w)
571 	register WORK *w;
572 {
573 # ifdef DEBUG
574 	if (Debug > 0)
575 		printf("timeout(%s)\n", w->w_name);
576 # endif DEBUG
577 
578 	/* return message to sender */
579 	(void) returntosender("Cannot send mail for three days");
580 
581 	/* arrange to remove files from queue */
582 	QueueUp = FALSE;
583 }
584 
585 # endif QUEUE
586