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