1 /* 2 * Copyright (c) 1983 Eric P. Allman 3 * Copyright (c) 1988 Regents of the University of California. 4 * All rights reserved. 5 * 6 * %sccs.include.redist.c% 7 */ 8 9 # include "sendmail.h" 10 11 #ifndef lint 12 #ifdef QUEUE 13 static char sccsid[] = "@(#)queue.c 5.41 (Berkeley) 07/12/92 (with queueing)"; 14 #else 15 static char sccsid[] = "@(#)queue.c 5.41 (Berkeley) 07/12/92 (without queueing)"; 16 #endif 17 #endif /* not lint */ 18 19 # include <sys/stat.h> 20 # include <sys/dir.h> 21 # include <sys/file.h> 22 # include <signal.h> 23 # include <errno.h> 24 # include <pwd.h> 25 # ifdef LOCKF 26 # include <fcntl.h> 27 # endif 28 29 # ifdef QUEUE 30 31 /* 32 ** Work queue. 33 */ 34 35 struct work 36 { 37 char *w_name; /* name of control file */ 38 long w_pri; /* priority of message, see below */ 39 time_t w_ctime; /* creation time of message */ 40 struct work *w_next; /* next in queue */ 41 }; 42 43 typedef struct work WORK; 44 45 WORK *WorkQ; /* queue of things to be done */ 46 /* 47 ** QUEUEUP -- queue a message up for future transmission. 48 ** 49 ** Parameters: 50 ** e -- the envelope to queue up. 51 ** queueall -- if TRUE, queue all addresses, rather than 52 ** just those with the QQUEUEUP flag set. 53 ** announce -- if TRUE, tell when you are queueing up. 54 ** 55 ** Returns: 56 ** none. 57 ** 58 ** Side Effects: 59 ** The current request are saved in a control file. 60 ** The queue file is left locked. 61 */ 62 63 queueup(e, queueall, announce) 64 register ENVELOPE *e; 65 bool queueall; 66 bool announce; 67 { 68 char *qf; 69 register FILE *tfp; 70 register HDR *h; 71 register ADDRESS *q; 72 int fd; 73 int i; 74 bool newid; 75 register char *p; 76 MAILER nullmailer; 77 ADDRESS *lastctladdr; 78 char buf[MAXLINE], tf[MAXLINE]; 79 extern char *macvalue(); 80 extern ADDRESS *getctladdr(); 81 82 /* 83 ** Create control file. 84 */ 85 86 newid = (e->e_id == NULL); 87 strcpy(tf, queuename(e, 't')); 88 tfp = e->e_lockfp; 89 if (tfp == NULL) 90 newid = FALSE; 91 if (newid) 92 { 93 tfp = e->e_lockfp; 94 } 95 else 96 { 97 /* get a locked tf file */ 98 for (i = 100; --i >= 0; ) 99 { 100 # ifdef LOCKF 101 struct flock lfd; 102 # endif 103 104 fd = open(tf, O_CREAT|O_WRONLY|O_EXCL, FileMode); 105 if (fd < 0) 106 { 107 if (errno == EEXIST) 108 continue; 109 syserr("queueup: cannot create temp file %s", tf); 110 return; 111 } 112 # ifdef LOCKF 113 lfd.l_type = F_WRLCK; 114 lfd.l_whence = lfd.l_start = lfd.l_len = 0; 115 if (fcntl(fd, F_SETLK, &lfd) >= 0) 116 break; 117 if (errno != EACCES && errno != EAGAIN) 118 syserr("cannot lockf(%s)", tf); 119 # else 120 if (flock(fd, LOCK_EX|LOCK_NB) >= 0) 121 break; 122 if (errno != EWOULDBLOCK) 123 syserr("cannot flock(%s)", tf); 124 # endif 125 close(fd); 126 } 127 128 tfp = fdopen(fd, "w"); 129 } 130 131 if (tTd(40, 1)) 132 printf("queueing %s\n", e->e_id); 133 134 /* 135 ** If there is no data file yet, create one. 136 */ 137 138 if (e->e_df == NULL) 139 { 140 register FILE *dfp; 141 extern putbody(); 142 143 e->e_df = newstr(queuename(e, 'd')); 144 fd = open(e->e_df, O_WRONLY|O_CREAT, FileMode); 145 if (fd < 0) 146 { 147 syserr("queueup: cannot create %s", e->e_df); 148 if (!newid) 149 (void) fclose(tfp); 150 return; 151 } 152 dfp = fdopen(fd, "w"); 153 (*e->e_putbody)(dfp, ProgMailer, e); 154 (void) fclose(dfp); 155 e->e_putbody = putbody; 156 } 157 158 /* 159 ** Output future work requests. 160 ** Priority and creation time should be first, since 161 ** they are required by orderq. 162 */ 163 164 /* output message priority */ 165 fprintf(tfp, "P%ld\n", e->e_msgpriority); 166 167 /* output creation time */ 168 fprintf(tfp, "T%ld\n", e->e_ctime); 169 170 /* output name of data file */ 171 fprintf(tfp, "D%s\n", e->e_df); 172 173 /* message from envelope, if it exists */ 174 if (e->e_message != NULL) 175 fprintf(tfp, "M%s\n", e->e_message); 176 177 /* $r and $s macro values */ 178 if ((p = macvalue('r', e)) != NULL) 179 fprintf(tfp, "$r%s\n", p); 180 if ((p = macvalue('s', e)) != NULL) 181 fprintf(tfp, "$s%s\n", p); 182 183 /* output name of sender */ 184 fprintf(tfp, "S%s\n", e->e_from.q_paddr); 185 186 /* output list of recipient addresses */ 187 lastctladdr = NULL; 188 for (q = e->e_sendqueue; q != NULL; q = q->q_next) 189 { 190 if (queueall ? !bitset(QDONTSEND|QSENT, q->q_flags) : 191 bitset(QQUEUEUP, q->q_flags)) 192 { 193 ADDRESS *ctladdr; 194 195 if ((ctladdr = getctladdr(q)) != lastctladdr) 196 { 197 printctladdr(ctladdr, tfp); 198 lastctladdr = ctladdr; 199 } 200 fprintf(tfp, "R%s\n", q->q_paddr); 201 if (announce) 202 { 203 e->e_to = q->q_paddr; 204 message(Arpa_Info, "queued"); 205 if (LogLevel > 4) 206 logdelivery("queued", e); 207 e->e_to = NULL; 208 } 209 if (tTd(40, 1)) 210 { 211 printf("queueing "); 212 printaddr(q, FALSE); 213 } 214 } 215 } 216 217 /* output list of error recipients */ 218 for (q = e->e_errorqueue; q != NULL; q = q->q_next) 219 { 220 if (!bitset(QDONTSEND, q->q_flags)) 221 { 222 ADDRESS *ctladdr; 223 224 if ((ctladdr = getctladdr(q)) != lastctladdr) 225 { 226 printctladdr(ctladdr, tfp); 227 lastctladdr = ctladdr; 228 } 229 fprintf(tfp, "E%s\n", q->q_paddr); 230 } 231 } 232 233 /* 234 ** Output headers for this message. 235 ** Expand macros completely here. Queue run will deal with 236 ** everything as absolute headers. 237 ** All headers that must be relative to the recipient 238 ** can be cracked later. 239 ** We set up a "null mailer" -- i.e., a mailer that will have 240 ** no effect on the addresses as they are output. 241 */ 242 243 bzero((char *) &nullmailer, sizeof nullmailer); 244 nullmailer.m_r_rwset = nullmailer.m_s_rwset = -1; 245 nullmailer.m_eol = "\n"; 246 247 define('g', "\001f", e); 248 for (h = e->e_header; h != NULL; h = h->h_link) 249 { 250 extern bool bitzerop(); 251 252 /* don't output null headers */ 253 if (h->h_value == NULL || h->h_value[0] == '\0') 254 continue; 255 256 /* don't output resent headers on non-resent messages */ 257 if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags)) 258 continue; 259 260 /* output this header */ 261 fprintf(tfp, "H"); 262 263 /* if conditional, output the set of conditions */ 264 if (!bitzerop(h->h_mflags) && bitset(H_CHECK|H_ACHECK, h->h_flags)) 265 { 266 int j; 267 268 (void) putc('?', tfp); 269 for (j = '\0'; j <= '\177'; j++) 270 if (bitnset(j, h->h_mflags)) 271 (void) putc(j, tfp); 272 (void) putc('?', tfp); 273 } 274 275 /* output the header: expand macros, convert addresses */ 276 if (bitset(H_DEFAULT, h->h_flags)) 277 { 278 (void) expand(h->h_value, buf, &buf[sizeof buf], e); 279 fprintf(tfp, "%s: %s\n", h->h_field, buf); 280 } 281 else if (bitset(H_FROM|H_RCPT, h->h_flags)) 282 { 283 commaize(h, h->h_value, tfp, bitset(EF_OLDSTYLE, e->e_flags), 284 &nullmailer); 285 } 286 else 287 fprintf(tfp, "%s: %s\n", h->h_field, h->h_value); 288 } 289 290 /* 291 ** Clean up. 292 */ 293 294 if (!newid) 295 { 296 qf = queuename(e, 'q'); 297 if (rename(tf, qf) < 0) 298 syserr("cannot rename(%s, %s), df=%s", tf, qf, e->e_df); 299 if (e->e_lockfp != NULL) 300 (void) fclose(e->e_lockfp); 301 e->e_lockfp = tfp; 302 } 303 else 304 qf = tf; 305 errno = 0; 306 307 # ifdef LOG 308 /* save log info */ 309 if (LogLevel > 15) 310 syslog(LOG_DEBUG, "%s: queueup, qf=%s, df=%s\n", e->e_id, qf, e->e_df); 311 # endif LOG 312 fflush(tfp); 313 return; 314 } 315 316 printctladdr(a, tfp) 317 ADDRESS *a; 318 FILE *tfp; 319 { 320 char *u; 321 struct passwd *pw; 322 extern struct passwd *getpwuid(); 323 324 if (a == NULL) 325 { 326 fprintf(tfp, "C\n"); 327 return; 328 } 329 if (a->q_uid == 0 || (pw = getpwuid(a->q_uid)) == NULL) 330 u = DefUser; 331 else 332 u = pw->pw_name; 333 fprintf(tfp, "C%s\n", u); 334 } 335 336 /* 337 ** RUNQUEUE -- run the jobs in the queue. 338 ** 339 ** Gets the stuff out of the queue in some presumably logical 340 ** order and processes them. 341 ** 342 ** Parameters: 343 ** forkflag -- TRUE if the queue scanning should be done in 344 ** a child process. We double-fork so it is not our 345 ** child and we don't have to clean up after it. 346 ** 347 ** Returns: 348 ** none. 349 ** 350 ** Side Effects: 351 ** runs things in the mail queue. 352 */ 353 354 runqueue(forkflag) 355 bool forkflag; 356 { 357 extern bool shouldqueue(); 358 359 /* 360 ** If no work will ever be selected, don't even bother reading 361 ** the queue. 362 */ 363 364 CurrentLA = getla(); /* get load average */ 365 366 if (shouldqueue(-100000000L)) 367 { 368 if (Verbose) 369 printf("Skipping queue run -- load average too high\n"); 370 371 if (forkflag) 372 return; 373 finis(); 374 } 375 376 /* 377 ** See if we want to go off and do other useful work. 378 */ 379 380 if (forkflag) 381 { 382 int pid; 383 384 pid = dofork(); 385 if (pid != 0) 386 { 387 extern void reapchild(); 388 389 /* parent -- pick up intermediate zombie */ 390 #ifndef SIGCHLD 391 (void) waitfor(pid); 392 #else SIGCHLD 393 (void) signal(SIGCHLD, reapchild); 394 #endif SIGCHLD 395 if (QueueIntvl != 0) 396 (void) setevent(QueueIntvl, runqueue, TRUE); 397 return; 398 } 399 /* child -- double fork */ 400 #ifndef SIGCHLD 401 if (fork() != 0) 402 exit(EX_OK); 403 #else SIGCHLD 404 (void) signal(SIGCHLD, SIG_DFL); 405 #endif SIGCHLD 406 } 407 408 setproctitle("running queue: %s", QueueDir); 409 410 # ifdef LOG 411 if (LogLevel > 11) 412 syslog(LOG_DEBUG, "runqueue %s, pid=%d", QueueDir, getpid()); 413 # endif LOG 414 415 /* 416 ** Release any resources used by the daemon code. 417 */ 418 419 # ifdef DAEMON 420 clrdaemon(); 421 # endif DAEMON 422 423 /* 424 ** Make sure the alias database is open. 425 */ 426 427 initaliases(AliasFile, FALSE); 428 429 /* 430 ** Start making passes through the queue. 431 ** First, read and sort the entire queue. 432 ** Then, process the work in that order. 433 ** But if you take too long, start over. 434 */ 435 436 /* order the existing work requests */ 437 (void) orderq(FALSE); 438 439 /* process them once at a time */ 440 while (WorkQ != NULL) 441 { 442 WORK *w = WorkQ; 443 444 WorkQ = WorkQ->w_next; 445 dowork(w); 446 free(w->w_name); 447 free((char *) w); 448 } 449 450 /* exit without the usual cleanup */ 451 exit(ExitStat); 452 } 453 /* 454 ** ORDERQ -- order the work queue. 455 ** 456 ** Parameters: 457 ** doall -- if set, include everything in the queue (even 458 ** the jobs that cannot be run because the load 459 ** average is too high). Otherwise, exclude those 460 ** jobs. 461 ** 462 ** Returns: 463 ** The number of request in the queue (not necessarily 464 ** the number of requests in WorkQ however). 465 ** 466 ** Side Effects: 467 ** Sets WorkQ to the queue of available work, in order. 468 */ 469 470 # define NEED_P 001 471 # define NEED_T 002 472 473 orderq(doall) 474 bool doall; 475 { 476 register struct direct *d; 477 register WORK *w; 478 DIR *f; 479 register int i; 480 WORK wlist[QUEUESIZE+1]; 481 int wn = -1; 482 extern workcmpf(); 483 484 /* clear out old WorkQ */ 485 for (w = WorkQ; w != NULL; ) 486 { 487 register WORK *nw = w->w_next; 488 489 WorkQ = nw; 490 free(w->w_name); 491 free((char *) w); 492 w = nw; 493 } 494 495 /* open the queue directory */ 496 f = opendir("."); 497 if (f == NULL) 498 { 499 syserr("orderq: cannot open \"%s\" as \".\"", QueueDir); 500 return (0); 501 } 502 503 /* 504 ** Read the work directory. 505 */ 506 507 while ((d = readdir(f)) != NULL) 508 { 509 FILE *cf; 510 char lbuf[MAXNAME]; 511 512 /* is this an interesting entry? */ 513 if (d->d_name[0] != 'q' || d->d_name[1] != 'f') 514 continue; 515 516 /* yes -- open control file (if not too many files) */ 517 if (++wn >= QUEUESIZE) 518 continue; 519 cf = fopen(d->d_name, "r"); 520 if (cf == NULL) 521 { 522 /* this may be some random person sending hir msgs */ 523 /* syserr("orderq: cannot open %s", cbuf); */ 524 if (tTd(41, 2)) 525 printf("orderq: cannot open %s (%d)\n", 526 d->d_name, errno); 527 errno = 0; 528 wn--; 529 continue; 530 } 531 w = &wlist[wn]; 532 w->w_name = newstr(d->d_name); 533 534 /* make sure jobs in creation don't clog queue */ 535 w->w_pri = 0x7fffffff; 536 w->w_ctime = 0; 537 538 /* extract useful information */ 539 i = NEED_P | NEED_T; 540 while (i != 0 && fgets(lbuf, sizeof lbuf, cf) != NULL) 541 { 542 extern long atol(); 543 544 switch (lbuf[0]) 545 { 546 case 'P': 547 w->w_pri = atol(&lbuf[1]); 548 i &= ~NEED_P; 549 break; 550 551 case 'T': 552 w->w_ctime = atol(&lbuf[1]); 553 i &= ~NEED_T; 554 break; 555 } 556 } 557 (void) fclose(cf); 558 559 if (!doall && shouldqueue(w->w_pri)) 560 { 561 /* don't even bother sorting this job in */ 562 wn--; 563 } 564 } 565 (void) closedir(f); 566 wn++; 567 568 /* 569 ** Sort the work directory. 570 */ 571 572 qsort((char *) wlist, min(wn, QUEUESIZE), sizeof *wlist, workcmpf); 573 574 /* 575 ** Convert the work list into canonical form. 576 ** Should be turning it into a list of envelopes here perhaps. 577 */ 578 579 WorkQ = NULL; 580 for (i = min(wn, QUEUESIZE); --i >= 0; ) 581 { 582 w = (WORK *) xalloc(sizeof *w); 583 w->w_name = wlist[i].w_name; 584 w->w_pri = wlist[i].w_pri; 585 w->w_ctime = wlist[i].w_ctime; 586 w->w_next = WorkQ; 587 WorkQ = w; 588 } 589 590 if (tTd(40, 1)) 591 { 592 for (w = WorkQ; w != NULL; w = w->w_next) 593 printf("%32s: pri=%ld\n", w->w_name, w->w_pri); 594 } 595 596 return (wn); 597 } 598 /* 599 ** WORKCMPF -- compare function for ordering work. 600 ** 601 ** Parameters: 602 ** a -- the first argument. 603 ** b -- the second argument. 604 ** 605 ** Returns: 606 ** -1 if a < b 607 ** 0 if a == b 608 ** +1 if a > b 609 ** 610 ** Side Effects: 611 ** none. 612 */ 613 614 workcmpf(a, b) 615 register WORK *a; 616 register WORK *b; 617 { 618 long pa = a->w_pri + a->w_ctime; 619 long pb = b->w_pri + b->w_ctime; 620 621 if (pa == pb) 622 return (0); 623 else if (pa > pb) 624 return (1); 625 else 626 return (-1); 627 } 628 /* 629 ** DOWORK -- do a work request. 630 ** 631 ** Parameters: 632 ** w -- the work request to be satisfied. 633 ** 634 ** Returns: 635 ** none. 636 ** 637 ** Side Effects: 638 ** The work request is satisfied if possible. 639 */ 640 641 dowork(w) 642 register WORK *w; 643 { 644 register int i; 645 extern bool shouldqueue(); 646 extern bool readqf(); 647 648 if (tTd(40, 1)) 649 printf("dowork: %s pri %ld\n", w->w_name, w->w_pri); 650 651 /* 652 ** Ignore jobs that are too expensive for the moment. 653 */ 654 655 if (shouldqueue(w->w_pri)) 656 { 657 if (Verbose) 658 printf("\nSkipping %s\n", w->w_name + 2); 659 return; 660 } 661 662 /* 663 ** Fork for work. 664 */ 665 666 if (ForkQueueRuns) 667 { 668 i = fork(); 669 if (i < 0) 670 { 671 syserr("dowork: cannot fork"); 672 return; 673 } 674 } 675 else 676 { 677 i = 0; 678 } 679 680 if (i == 0) 681 { 682 /* 683 ** CHILD 684 ** Lock the control file to avoid duplicate deliveries. 685 ** Then run the file as though we had just read it. 686 ** We save an idea of the temporary name so we 687 ** can recover on interrupt. 688 */ 689 690 /* set basic modes, etc. */ 691 (void) alarm(0); 692 clearenvelope(CurEnv, FALSE); 693 QueueRun = TRUE; 694 ErrorMode = EM_MAIL; 695 CurEnv->e_id = &w->w_name[2]; 696 # ifdef LOG 697 if (LogLevel > 11) 698 syslog(LOG_DEBUG, "%s: dowork, pid=%d", CurEnv->e_id, 699 getpid()); 700 # endif LOG 701 702 /* don't use the headers from sendmail.cf... */ 703 CurEnv->e_header = NULL; 704 705 /* read the queue control file -- return if locked */ 706 if (!readqf(CurEnv)) 707 { 708 if (ForkQueueRuns) 709 exit(EX_OK); 710 else 711 return; 712 } 713 714 CurEnv->e_flags |= EF_INQUEUE; 715 eatheader(CurEnv); 716 717 /* do the delivery */ 718 if (!bitset(EF_FATALERRS, CurEnv->e_flags)) 719 sendall(CurEnv, SM_DELIVER); 720 721 /* finish up and exit */ 722 if (ForkQueueRuns) 723 finis(); 724 else 725 dropenvelope(CurEnv); 726 } 727 else 728 { 729 /* 730 ** Parent -- pick up results. 731 */ 732 733 errno = 0; 734 (void) waitfor(i); 735 } 736 } 737 /* 738 ** READQF -- read queue file and set up environment. 739 ** 740 ** Parameters: 741 ** e -- the envelope of the job to run. 742 ** 743 ** Returns: 744 ** TRUE if it successfully read the queue file. 745 ** FALSE otherwise. 746 ** 747 ** Side Effects: 748 ** The queue file is returned locked. 749 */ 750 751 bool 752 readqf(e) 753 register ENVELOPE *e; 754 { 755 char *qf; 756 register FILE *qfp; 757 int fd; 758 ADDRESS *ctladdr; 759 char buf[MAXFIELD]; 760 extern char *fgetfolded(); 761 extern long atol(); 762 extern ADDRESS *setctluser(); 763 # ifdef LOCKF 764 struct flock lfd; 765 # endif 766 767 /* 768 ** Read and process the file. 769 */ 770 771 qf = queuename(e, 'q'); 772 qfp = fopen(qf, "r+"); 773 if (qfp == NULL) 774 { 775 if (errno != ENOENT) 776 syserr("readqf: no control file %s", qf); 777 return FALSE; 778 } 779 780 # ifdef LOCKF 781 lfd.l_type = F_WRLCK; 782 lfd.l_whence = lfd.l_start = lfd.l_len = 0; 783 if (fcntl(fileno(qfp), F_SETLK, &lfd) < 0) 784 # else 785 if (flock(fileno(qfp), LOCK_EX|LOCK_NB) < 0) 786 # endif 787 { 788 /* being processed by another queuer */ 789 if (Verbose) 790 printf("%s: locked\n", CurEnv->e_id); 791 # ifdef LOG 792 if (LogLevel > 9) 793 syslog(LOG_DEBUG, "%s: locked", CurEnv->e_id); 794 # endif LOG 795 (void) fclose(qfp); 796 return FALSE; 797 } 798 799 /* save this lock */ 800 e->e_lockfp = qfp; 801 802 /* do basic system initialization */ 803 initsys(); 804 805 FileName = qf; 806 LineNumber = 0; 807 if (Verbose) 808 printf("\nRunning %s\n", e->e_id); 809 ctladdr = NULL; 810 while (fgetfolded(buf, sizeof buf, qfp) != NULL) 811 { 812 if (tTd(40, 4)) 813 printf("+++++ %s\n", buf); 814 switch (buf[0]) 815 { 816 case 'C': /* specify controlling user */ 817 ctladdr = setctluser(&buf[1]); 818 break; 819 820 case 'R': /* specify recipient */ 821 sendtolist(&buf[1], ctladdr, &e->e_sendqueue); 822 break; 823 824 case 'E': /* specify error recipient */ 825 sendtolist(&buf[1], ctladdr, &e->e_errorqueue); 826 break; 827 828 case 'H': /* header */ 829 (void) chompheader(&buf[1], FALSE); 830 break; 831 832 case 'M': /* message */ 833 e->e_message = newstr(&buf[1]); 834 break; 835 836 case 'S': /* sender */ 837 setsender(newstr(&buf[1]), CurEnv); 838 break; 839 840 case 'D': /* data file name */ 841 e->e_df = newstr(&buf[1]); 842 e->e_dfp = fopen(e->e_df, "r"); 843 if (e->e_dfp == NULL) 844 syserr("readqf: cannot open %s", e->e_df); 845 break; 846 847 case 'T': /* init time */ 848 e->e_ctime = atol(&buf[1]); 849 break; 850 851 case 'P': /* message priority */ 852 e->e_msgpriority = atol(&buf[1]) + WkTimeFact; 853 break; 854 855 case '$': /* define macro */ 856 define(buf[1], newstr(&buf[2]), e); 857 break; 858 859 case '\0': /* blank line; ignore */ 860 break; 861 862 default: 863 syserr("readqf(%s:%d): bad line \"%s\"", e->e_id, 864 LineNumber, buf); 865 break; 866 } 867 } 868 869 FileName = NULL; 870 871 /* 872 ** If we haven't read any lines, this queue file is empty. 873 ** Arrange to remove it without referencing any null pointers. 874 */ 875 876 if (LineNumber == 0) 877 { 878 errno = 0; 879 e->e_flags |= EF_CLRQUEUE | EF_FATALERRS | EF_RESPONSE; 880 } 881 return TRUE; 882 } 883 /* 884 ** PRINTQUEUE -- print out a representation of the mail queue 885 ** 886 ** Parameters: 887 ** none. 888 ** 889 ** Returns: 890 ** none. 891 ** 892 ** Side Effects: 893 ** Prints a listing of the mail queue on the standard output. 894 */ 895 896 printqueue() 897 { 898 register WORK *w; 899 FILE *f; 900 int nrequests; 901 char buf[MAXLINE]; 902 903 /* 904 ** Read and order the queue. 905 */ 906 907 nrequests = orderq(TRUE); 908 909 /* 910 ** Print the work list that we have read. 911 */ 912 913 /* first see if there is anything */ 914 if (nrequests <= 0) 915 { 916 printf("Mail queue is empty\n"); 917 return; 918 } 919 920 CurrentLA = getla(); /* get load average */ 921 922 printf("\t\tMail Queue (%d request%s", nrequests, nrequests == 1 ? "" : "s"); 923 if (nrequests > QUEUESIZE) 924 printf(", only %d printed", QUEUESIZE); 925 if (Verbose) 926 printf(")\n--QID-- --Size-- -Priority- ---Q-Time--- -----------Sender/Recipient-----------\n"); 927 else 928 printf(")\n--QID-- --Size-- -----Q-Time----- ------------Sender/Recipient------------\n"); 929 for (w = WorkQ; w != NULL; w = w->w_next) 930 { 931 struct stat st; 932 auto time_t submittime = 0; 933 long dfsize = -1; 934 char message[MAXLINE]; 935 # ifdef LOCKF 936 struct flock lfd; 937 # endif 938 extern bool shouldqueue(); 939 940 f = fopen(w->w_name, "r"); 941 if (f == NULL) 942 { 943 errno = 0; 944 continue; 945 } 946 printf("%7s", w->w_name + 2); 947 # ifdef LOCKF 948 lfd.l_type = F_RDLCK; 949 lfd.l_whence = lfd.l_start = lfd.l_len = 0; 950 if (fcntl(fileno(f), F_GETLK, &lfd) < 0 || lfd.l_type != F_UNLCK) 951 # else 952 if (flock(fileno(f), LOCK_SH|LOCK_NB) < 0) 953 # endif 954 printf("*"); 955 else if (shouldqueue(w->w_pri)) 956 printf("X"); 957 else 958 printf(" "); 959 errno = 0; 960 961 message[0] = '\0'; 962 while (fgets(buf, sizeof buf, f) != NULL) 963 { 964 register int i; 965 966 fixcrlf(buf, TRUE); 967 switch (buf[0]) 968 { 969 case 'M': /* error message */ 970 if ((i = strlen(&buf[1])) >= sizeof message) 971 i = sizeof message; 972 bcopy(&buf[1], message, i); 973 message[i] = '\0'; 974 break; 975 976 case 'S': /* sender name */ 977 if (Verbose) 978 printf("%8ld %10ld %.12s %.38s", dfsize, 979 w->w_pri, ctime(&submittime) + 4, 980 &buf[1]); 981 else 982 printf("%8ld %.16s %.45s", dfsize, 983 ctime(&submittime), &buf[1]); 984 if (message[0] != '\0') 985 printf("\n\t\t (%.60s)", message); 986 break; 987 988 case 'C': /* controlling user */ 989 if (Verbose) 990 printf("\n\t\t\t\t\t (---%.30s---)", &buf[1]); 991 break; 992 993 case 'R': /* recipient name */ 994 if (Verbose) 995 printf("\n\t\t\t\t\t %.38s", &buf[1]); 996 else 997 printf("\n\t\t\t\t %.45s", &buf[1]); 998 break; 999 1000 case 'T': /* creation time */ 1001 submittime = atol(&buf[1]); 1002 break; 1003 1004 case 'D': /* data file name */ 1005 if (stat(&buf[1], &st) >= 0) 1006 dfsize = st.st_size; 1007 break; 1008 } 1009 } 1010 if (submittime == (time_t) 0) 1011 printf(" (no control file)"); 1012 printf("\n"); 1013 (void) fclose(f); 1014 } 1015 } 1016 1017 # endif QUEUE 1018 /* 1019 ** QUEUENAME -- build a file name in the queue directory for this envelope. 1020 ** 1021 ** Assigns an id code if one does not already exist. 1022 ** This code is very careful to avoid trashing existing files 1023 ** under any circumstances. 1024 ** 1025 ** Parameters: 1026 ** e -- envelope to build it in/from. 1027 ** type -- the file type, used as the first character 1028 ** of the file name. 1029 ** 1030 ** Returns: 1031 ** a pointer to the new file name (in a static buffer). 1032 ** 1033 ** Side Effects: 1034 ** If no id code is already assigned, queuename will 1035 ** assign an id code, create a qf file, and leave a 1036 ** locked, open-for-write file pointer in the envelope. 1037 */ 1038 1039 char * 1040 queuename(e, type) 1041 register ENVELOPE *e; 1042 char type; 1043 { 1044 static char buf[MAXNAME]; 1045 static int pid = -1; 1046 char c1 = 'A'; 1047 char c2 = 'A'; 1048 1049 if (e->e_id == NULL) 1050 { 1051 char qf[20]; 1052 1053 /* find a unique id */ 1054 if (pid != getpid()) 1055 { 1056 /* new process -- start back at "AA" */ 1057 pid = getpid(); 1058 c1 = 'A'; 1059 c2 = 'A' - 1; 1060 } 1061 (void) sprintf(qf, "qfAA%05d", pid); 1062 1063 while (c1 < '~' || c2 < 'Z') 1064 { 1065 int i; 1066 # ifdef LOCKF 1067 struct flock lfd; 1068 # endif 1069 1070 if (c2 >= 'Z') 1071 { 1072 c1++; 1073 c2 = 'A' - 1; 1074 } 1075 qf[2] = c1; 1076 qf[3] = ++c2; 1077 if (tTd(7, 20)) 1078 printf("queuename: trying \"%s\"\n", qf); 1079 1080 i = open(qf, O_WRONLY|O_CREAT|O_EXCL, FileMode); 1081 if (i < 0) 1082 { 1083 if (errno == EEXIST) 1084 continue; 1085 syserr("queuename: Cannot create \"%s\" in \"%s\"", 1086 qf, QueueDir); 1087 exit(EX_UNAVAILABLE); 1088 } 1089 # ifdef LOCKF 1090 lfd.l_type = F_WRLCK; 1091 lfd.l_whence = lfd.l_start = lfd.l_len = 0; 1092 if (fcntl(i, F_SETLK, &lfd) >= 0) 1093 # else 1094 if (flock(i, LOCK_EX|LOCK_NB) >= 0) 1095 # endif 1096 { 1097 e->e_lockfp = fdopen(i, "w"); 1098 break; 1099 } 1100 1101 /* a reader got the file; abandon it and try again */ 1102 (void) close(i); 1103 } 1104 if (c1 >= '~' && c2 >= 'Z') 1105 { 1106 syserr("queuename: Cannot create \"%s\" in \"%s\"", 1107 qf, QueueDir); 1108 exit(EX_OSERR); 1109 } 1110 e->e_id = newstr(&qf[2]); 1111 define('i', e->e_id, e); 1112 if (tTd(7, 1)) 1113 printf("queuename: assigned id %s, env=%x\n", e->e_id, e); 1114 # ifdef LOG 1115 if (LogLevel > 16) 1116 syslog(LOG_DEBUG, "%s: assigned id", e->e_id); 1117 # endif LOG 1118 } 1119 1120 if (type == '\0') 1121 return (NULL); 1122 (void) sprintf(buf, "%cf%s", type, e->e_id); 1123 if (tTd(7, 2)) 1124 printf("queuename: %s\n", buf); 1125 return (buf); 1126 } 1127 /* 1128 ** UNLOCKQUEUE -- unlock the queue entry for a specified envelope 1129 ** 1130 ** Parameters: 1131 ** e -- the envelope to unlock. 1132 ** 1133 ** Returns: 1134 ** none 1135 ** 1136 ** Side Effects: 1137 ** unlocks the queue for `e'. 1138 */ 1139 1140 unlockqueue(e) 1141 ENVELOPE *e; 1142 { 1143 /* if there is a lock file in the envelope, close it */ 1144 if (e->e_lockfp != NULL) 1145 fclose(e->e_lockfp); 1146 e->e_lockfp = NULL; 1147 1148 /* remove the transcript */ 1149 # ifdef LOG 1150 if (LogLevel > 19) 1151 syslog(LOG_DEBUG, "%s: unlock", e->e_id); 1152 # endif LOG 1153 if (!tTd(51, 4)) 1154 xunlink(queuename(e, 'x')); 1155 1156 } 1157 /* 1158 ** SETCTLUSER -- create a controlling address 1159 ** 1160 ** Create a fake "address" given only a local login name; this is 1161 ** used as a "controlling user" for future recipient addresses. 1162 ** 1163 ** Parameters: 1164 ** user -- the user name of the controlling user. 1165 ** 1166 ** Returns: 1167 ** An address descriptor for the controlling user. 1168 ** 1169 ** Side Effects: 1170 ** none. 1171 */ 1172 1173 ADDRESS * 1174 setctluser(user) 1175 char *user; 1176 { 1177 register ADDRESS *a; 1178 struct passwd *pw; 1179 1180 /* 1181 ** See if this clears our concept of controlling user. 1182 */ 1183 1184 if (user == NULL || *user == '\0') 1185 return NULL; 1186 1187 /* 1188 ** Set up addr fields for controlling user. 1189 */ 1190 1191 a = (ADDRESS *) xalloc(sizeof *a); 1192 bzero((char *) a, sizeof *a); 1193 if ((pw = getpwnam(user)) != NULL) 1194 { 1195 a->q_home = newstr(pw->pw_dir); 1196 a->q_uid = pw->pw_uid; 1197 a->q_gid = pw->pw_gid; 1198 a->q_ruser = newstr(user); 1199 } 1200 else 1201 { 1202 a->q_uid = DefUid; 1203 a->q_gid = DefGid; 1204 a->q_ruser = newstr(DefUser); 1205 } 1206 1207 a->q_flags |= QGOODUID; /* flag as a "ctladdr" */ 1208 return a; 1209 } 1210