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