1 /* 2 * Copyright (c) 1983, 1993 3 * The Regents of the University of California. All rights reserved. 4 * (c) UNIX System Laboratories, Inc. 5 * All or some portions of this file are derived from material licensed 6 * to the University of California by American Telephone and Telegraph 7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 8 * the permission of UNIX System Laboratories, Inc. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the University of 21 * California, Berkeley and its contributors. 22 * 4. Neither the name of the University nor the names of its contributors 23 * may be used to endorse or promote products derived from this software 24 * without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 */ 38 39 #if 0 40 #ifndef lint 41 static char sccsid[] = "@(#)common.c 8.5 (Berkeley) 4/28/95"; 42 #endif /* not lint */ 43 #endif 44 45 #include "lp.cdefs.h" /* A cross-platform version of <sys/cdefs.h> */ 46 __FBSDID("$FreeBSD$"); 47 48 #include <sys/param.h> 49 #include <sys/stat.h> 50 #include <sys/time.h> 51 #include <sys/types.h> 52 53 #include <ctype.h> 54 #include <dirent.h> 55 #include <errno.h> 56 #include <fcntl.h> 57 #include <stdio.h> 58 #include <stdlib.h> 59 #include <string.h> 60 #include <unistd.h> 61 62 #include "lp.h" 63 #include "lp.local.h" 64 #include "pathnames.h" 65 66 /* 67 * Routines and data common to all the line printer functions. 68 */ 69 char line[BUFSIZ]; 70 const char *progname; /* program name */ 71 72 extern uid_t uid, euid; 73 74 static int compar(const void *_p1, const void *_p2); 75 76 /* 77 * isdigit() takes a parameter of 'int', but expect values in the range 78 * of unsigned char. Define a wrapper which takes a value of type 'char', 79 * whether signed or unsigned, and ensure it ends up in the right range. 80 */ 81 #define isdigitch(Anychar) isdigit((u_char)(Anychar)) 82 83 /* 84 * Getline reads a line from the control file cfp, removes tabs, converts 85 * new-line to null and leaves it in line. 86 * Returns 0 at EOF or the number of characters read. 87 */ 88 int 89 getline(FILE *cfp) 90 { 91 register int linel = 0; 92 register char *lp = line; 93 register int c; 94 95 while ((c = getc(cfp)) != '\n' && (size_t)(linel+1) < sizeof(line)) { 96 if (c == EOF) 97 return(0); 98 if (c == '\t') { 99 do { 100 *lp++ = ' '; 101 linel++; 102 } while ((linel & 07) != 0 && (size_t)(linel+1) < 103 sizeof(line)); 104 continue; 105 } 106 *lp++ = c; 107 linel++; 108 } 109 *lp++ = '\0'; 110 return(linel); 111 } 112 113 /* 114 * Scan the current directory and make a list of daemon files sorted by 115 * creation time. 116 * Return the number of entries and a pointer to the list. 117 */ 118 int 119 getq(const struct printer *pp, struct jobqueue *(*namelist[])) 120 { 121 register struct dirent *d; 122 register struct jobqueue *q, **queue; 123 size_t arraysz, entrysz, nitems; 124 struct stat stbuf; 125 DIR *dirp; 126 int statres; 127 128 seteuid(euid); 129 if ((dirp = opendir(pp->spool_dir)) == NULL) { 130 seteuid(uid); 131 return (-1); 132 } 133 if (fstat(dirfd(dirp), &stbuf) < 0) 134 goto errdone; 135 seteuid(uid); 136 137 /* 138 * Estimate the array size by taking the size of the directory file 139 * and dividing it by a multiple of the minimum size entry. 140 */ 141 arraysz = (stbuf.st_size / 24); 142 if (arraysz < 16) 143 arraysz = 16; 144 queue = (struct jobqueue **)malloc(arraysz * sizeof(struct jobqueue *)); 145 if (queue == NULL) 146 goto errdone; 147 148 nitems = 0; 149 while ((d = readdir(dirp)) != NULL) { 150 if (d->d_name[0] != 'c' || d->d_name[1] != 'f') 151 continue; /* daemon control files only */ 152 seteuid(euid); 153 statres = stat(d->d_name, &stbuf); 154 seteuid(uid); 155 if (statres < 0) 156 continue; /* Doesn't exist */ 157 entrysz = sizeof(struct jobqueue) - sizeof(q->job_cfname) + 158 strlen(d->d_name) + 1; 159 q = (struct jobqueue *)malloc(entrysz); 160 if (q == NULL) 161 goto errdone; 162 q->job_matched = 0; 163 q->job_processed = 0; 164 q->job_time = stbuf.st_mtime; 165 strcpy(q->job_cfname, d->d_name); 166 /* 167 * Check to make sure the array has space left and 168 * realloc the maximum size. 169 */ 170 if (++nitems > arraysz) { 171 arraysz *= 2; 172 queue = (struct jobqueue **)realloc((char *)queue, 173 arraysz * sizeof(struct jobqueue *)); 174 if (queue == NULL) 175 goto errdone; 176 } 177 queue[nitems-1] = q; 178 } 179 closedir(dirp); 180 if (nitems) 181 qsort(queue, nitems, sizeof(struct jobqueue *), compar); 182 *namelist = queue; 183 return(nitems); 184 185 errdone: 186 closedir(dirp); 187 seteuid(uid); 188 return (-1); 189 } 190 191 /* 192 * Compare modification times. 193 */ 194 static int 195 compar(const void *p1, const void *p2) 196 { 197 const struct jobqueue *qe1, *qe2; 198 199 qe1 = *(const struct jobqueue * const *)p1; 200 qe2 = *(const struct jobqueue * const *)p2; 201 202 if (qe1->job_time < qe2->job_time) 203 return (-1); 204 if (qe1->job_time > qe2->job_time) 205 return (1); 206 /* 207 * At this point, the two files have the same last-modification time. 208 * return a result based on filenames, so that 'cfA001some.host' will 209 * come before 'cfA002some.host'. Since the jobid ('001') will wrap 210 * around when it gets to '999', we also assume that '9xx' jobs are 211 * older than '0xx' jobs. 212 */ 213 if ((qe1->job_cfname[3] == '9') && (qe2->job_cfname[3] == '0')) 214 return (-1); 215 if ((qe1->job_cfname[3] == '0') && (qe2->job_cfname[3] == '9')) 216 return (1); 217 return (strcmp(qe1->job_cfname, qe2->job_cfname)); 218 } 219 220 /* 221 * A simple routine to determine the job number for a print job based on 222 * the name of its control file. The algorithm used here may look odd, but 223 * the main issue is that all parts of `lpd', `lpc', `lpq' & `lprm' must be 224 * using the same algorithm, whatever that algorithm may be. If the caller 225 * provides a non-null value for ''hostpp', then this returns a pointer to 226 * the start of the hostname (or IP address?) as found in the filename. 227 * 228 * Algorithm: The standard `cf' file has the job number start in position 4, 229 * but some implementations have that as an extra file-sequence letter, and 230 * start the job number in position 5. The job number is usually three bytes, 231 * but may be as many as five. Confusing matters still more, some Windows 232 * print servers will append an IP address to the job number, instead of 233 * the expected hostname. So, if the job number ends with a '.', then 234 * assume the correct jobnum value is the first three digits. 235 */ 236 int 237 calc_jobnum(const char *cfname, const char **hostpp) 238 { 239 int jnum; 240 const char *cp, *numstr, *hoststr; 241 242 numstr = cfname + 3; 243 if (!isdigitch(*numstr)) 244 numstr++; 245 jnum = 0; 246 for (cp = numstr; (cp < numstr + 5) && isdigitch(*cp); cp++) 247 jnum = jnum * 10 + (*cp - '0'); 248 hoststr = cp; 249 250 /* 251 * If the filename was built with an IP number instead of a hostname, 252 * then recalculate using only the first three digits found. 253 */ 254 while(isdigitch(*cp)) 255 cp++; 256 if (*cp == '.') { 257 jnum = 0; 258 for (cp = numstr; (cp < numstr + 3) && isdigitch(*cp); cp++) 259 jnum = jnum * 10 + (*cp - '0'); 260 hoststr = cp; 261 } 262 if (hostpp != NULL) 263 *hostpp = hoststr; 264 return (jnum); 265 } 266 267 /* sleep n milliseconds */ 268 void 269 delay(int millisec) 270 { 271 struct timeval tdelay; 272 273 if (millisec <= 0 || millisec > 10000) 274 fatal((struct printer *)0, /* fatal() knows how to deal */ 275 "unreasonable delay period (%d)", millisec); 276 tdelay.tv_sec = millisec / 1000; 277 tdelay.tv_usec = millisec * 1000 % 1000000; 278 (void) select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &tdelay); 279 } 280 281 char * 282 lock_file_name(const struct printer *pp, char *buf, size_t len) 283 { 284 static char staticbuf[MAXPATHLEN]; 285 286 if (buf == 0) 287 buf = staticbuf; 288 if (len == 0) 289 len = MAXPATHLEN; 290 291 if (pp->lock_file[0] == '/') 292 strlcpy(buf, pp->lock_file, len); 293 else 294 snprintf(buf, len, "%s/%s", pp->spool_dir, pp->lock_file); 295 296 return buf; 297 } 298 299 char * 300 status_file_name(const struct printer *pp, char *buf, size_t len) 301 { 302 static char staticbuf[MAXPATHLEN]; 303 304 if (buf == 0) 305 buf = staticbuf; 306 if (len == 0) 307 len = MAXPATHLEN; 308 309 if (pp->status_file[0] == '/') 310 strlcpy(buf, pp->status_file, len); 311 else 312 snprintf(buf, len, "%s/%s", pp->spool_dir, pp->status_file); 313 314 return buf; 315 } 316 317 /* 318 * Routine to change operational state of a print queue. The operational 319 * state is indicated by the access bits on the lock file for the queue. 320 * At present, this is only called from various routines in lpc/cmds.c. 321 * 322 * XXX - Note that this works by changing access-bits on the 323 * file, and you can only do that if you are the owner of 324 * the file, or root. Thus, this won't really work for 325 * userids in the "LPR_OPER" group, unless lpc is running 326 * setuid to root (or maybe setuid to daemon). 327 * Generally lpc is installed setgid to daemon, but does 328 * not run setuid. 329 */ 330 int 331 set_qstate(int action, const char *lfname) 332 { 333 struct stat stbuf; 334 mode_t chgbits, newbits, oldmask; 335 const char *failmsg, *okmsg; 336 static const char *nomsg = "no state msg"; 337 int chres, errsav, fd, res, statres; 338 339 /* 340 * Find what the current access-bits are. 341 */ 342 memset(&stbuf, 0, sizeof(stbuf)); 343 seteuid(euid); 344 statres = stat(lfname, &stbuf); 345 errsav = errno; 346 seteuid(uid); 347 if ((statres < 0) && (errsav != ENOENT)) { 348 printf("\tcannot stat() lock file\n"); 349 return (SQS_STATFAIL); 350 /* NOTREACHED */ 351 } 352 353 /* 354 * Determine which bit(s) should change for the requested action. 355 */ 356 chgbits = stbuf.st_mode; 357 newbits = LOCK_FILE_MODE; 358 okmsg = NULL; 359 failmsg = NULL; 360 if (action & SQS_QCHANGED) { 361 chgbits |= LFM_RESET_QUE; 362 newbits |= LFM_RESET_QUE; 363 /* The okmsg is not actually printed for this case. */ 364 okmsg = nomsg; 365 failmsg = "set queue-changed"; 366 } 367 if (action & SQS_DISABLEQ) { 368 chgbits |= LFM_QUEUE_DIS; 369 newbits |= LFM_QUEUE_DIS; 370 okmsg = "queuing disabled"; 371 failmsg = "disable queuing"; 372 } 373 if (action & SQS_STOPP) { 374 chgbits |= LFM_PRINT_DIS; 375 newbits |= LFM_PRINT_DIS; 376 okmsg = "printing disabled"; 377 failmsg = "disable printing"; 378 if (action & SQS_DISABLEQ) { 379 okmsg = "printer and queuing disabled"; 380 failmsg = "disable queuing and printing"; 381 } 382 } 383 if (action & SQS_ENABLEQ) { 384 chgbits &= ~LFM_QUEUE_DIS; 385 newbits &= ~LFM_QUEUE_DIS; 386 okmsg = "queuing enabled"; 387 failmsg = "enable queuing"; 388 } 389 if (action & SQS_STARTP) { 390 chgbits &= ~LFM_PRINT_DIS; 391 newbits &= ~LFM_PRINT_DIS; 392 okmsg = "printing enabled"; 393 failmsg = "enable printing"; 394 } 395 if (okmsg == NULL) { 396 /* This routine was called with an invalid action. */ 397 printf("\t<error in set_qstate!>\n"); 398 return (SQS_PARMERR); 399 /* NOTREACHED */ 400 } 401 402 res = 0; 403 if (statres >= 0) { 404 /* The file already exists, so change the access. */ 405 seteuid(euid); 406 chres = chmod(lfname, chgbits); 407 errsav = errno; 408 seteuid(uid); 409 res = SQS_CHGOK; 410 if (chres < 0) 411 res = SQS_CHGFAIL; 412 } else if (newbits == LOCK_FILE_MODE) { 413 /* 414 * The file does not exist, but the state requested is 415 * the same as the default state when no file exists. 416 * Thus, there is no need to create the file. 417 */ 418 res = SQS_SKIPCREOK; 419 } else { 420 /* 421 * The file did not exist, so create it with the 422 * appropriate access bits for the requested action. 423 * Push a new umask around that create, to make sure 424 * all the read/write bits are set as desired. 425 */ 426 oldmask = umask(S_IWOTH); 427 seteuid(euid); 428 fd = open(lfname, O_WRONLY|O_CREAT, newbits); 429 errsav = errno; 430 seteuid(uid); 431 umask(oldmask); 432 res = SQS_CREFAIL; 433 if (fd >= 0) { 434 res = SQS_CREOK; 435 close(fd); 436 } 437 } 438 439 switch (res) { 440 case SQS_CHGOK: 441 case SQS_CREOK: 442 case SQS_SKIPCREOK: 443 if (okmsg != nomsg) 444 printf("\t%s\n", okmsg); 445 break; 446 case SQS_CREFAIL: 447 printf("\tcannot create lock file: %s\n", 448 strerror(errsav)); 449 break; 450 default: 451 printf("\tcannot %s: %s\n", failmsg, strerror(errsav)); 452 break; 453 } 454 455 return (res); 456 } 457 458 /* routine to get a current timestamp, optionally in a standard-fmt string */ 459 void 460 lpd_gettime(struct timespec *tsp, char *strp, size_t strsize) 461 { 462 struct timespec local_ts; 463 struct timeval btime; 464 char tempstr[TIMESTR_SIZE]; 465 #ifdef STRFTIME_WRONG_z 466 char *destp; 467 #endif 468 469 if (tsp == NULL) 470 tsp = &local_ts; 471 472 /* some platforms have a routine called clock_gettime, but the 473 * routine does nothing but return "not implemented". */ 474 memset(tsp, 0, sizeof(struct timespec)); 475 if (clock_gettime(CLOCK_REALTIME, tsp)) { 476 /* nanosec-aware rtn failed, fall back to microsec-aware rtn */ 477 memset(tsp, 0, sizeof(struct timespec)); 478 gettimeofday(&btime, NULL); 479 tsp->tv_sec = btime.tv_sec; 480 tsp->tv_nsec = btime.tv_usec * 1000; 481 } 482 483 /* caller may not need a character-ized version */ 484 if ((strp == NULL) || (strsize < 1)) 485 return; 486 487 strftime(tempstr, TIMESTR_SIZE, LPD_TIMESTAMP_PATTERN, 488 localtime(&tsp->tv_sec)); 489 490 /* 491 * This check is for implementations of strftime which treat %z 492 * (timezone as [+-]hhmm ) like %Z (timezone as characters), or 493 * completely ignore %z. This section is not needed on freebsd. 494 * I'm not sure this is completely right, but it should work OK 495 * for EST and EDT... 496 */ 497 #ifdef STRFTIME_WRONG_z 498 destp = strrchr(tempstr, ':'); 499 if (destp != NULL) { 500 destp += 3; 501 if ((*destp != '+') && (*destp != '-')) { 502 char savday[6]; 503 int tzmin = timezone / 60; 504 int tzhr = tzmin / 60; 505 if (daylight) 506 tzhr--; 507 strcpy(savday, destp + strlen(destp) - 4); 508 snprintf(destp, (destp - tempstr), "%+03d%02d", 509 (-1*tzhr), tzmin % 60); 510 strcat(destp, savday); 511 } 512 } 513 #endif 514 515 if (strsize > TIMESTR_SIZE) { 516 strsize = TIMESTR_SIZE; 517 strp[TIMESTR_SIZE+1] = '\0'; 518 } 519 strlcpy(strp, tempstr, strsize); 520 } 521 522 /* routines for writing transfer-statistic records */ 523 void 524 trstat_init(struct printer *pp, const char *fname, int filenum) 525 { 526 register const char *srcp; 527 register char *destp, *endp; 528 529 /* 530 * Figure out the job id of this file. The filename should be 531 * 'cf', 'df', or maybe 'tf', followed by a letter (or sometimes 532 * two), followed by the jobnum, followed by a hostname. 533 * The jobnum is usually 3 digits, but might be as many as 5. 534 * Note that some care has to be taken parsing this, as the 535 * filename could be coming from a remote-host, and thus might 536 * not look anything like what is expected... 537 */ 538 memset(pp->jobnum, 0, sizeof(pp->jobnum)); 539 pp->jobnum[0] = '0'; 540 srcp = strchr(fname, '/'); 541 if (srcp == NULL) 542 srcp = fname; 543 destp = &(pp->jobnum[0]); 544 endp = destp + 5; 545 while (*srcp != '\0' && (*srcp < '0' || *srcp > '9')) 546 srcp++; 547 while (*srcp >= '0' && *srcp <= '9' && destp < endp) 548 *(destp++) = *(srcp++); 549 550 /* get the starting time in both numeric and string formats, and 551 * save those away along with the file-number */ 552 pp->jobdfnum = filenum; 553 lpd_gettime(&pp->tr_start, pp->tr_timestr, (size_t)TIMESTR_SIZE); 554 555 return; 556 } 557 558 void 559 trstat_write(struct printer *pp, tr_sendrecv sendrecv, size_t bytecnt, 560 const char *userid, const char *otherhost, const char *orighost) 561 { 562 #define STATLINE_SIZE 1024 563 double trtime; 564 size_t remspace; 565 int statfile; 566 char thishost[MAXHOSTNAMELEN], statline[STATLINE_SIZE]; 567 char *eostat; 568 const char *lprhost, *recvdev, *recvhost, *rectype; 569 const char *sendhost, *statfname; 570 #define UPD_EOSTAT(xStr) do { \ 571 eostat = strchr(xStr, '\0'); \ 572 remspace = eostat - xStr; \ 573 } while(0) 574 575 lpd_gettime(&pp->tr_done, NULL, (size_t)0); 576 trtime = DIFFTIME_TS(pp->tr_done, pp->tr_start); 577 578 gethostname(thishost, sizeof(thishost)); 579 lprhost = sendhost = recvhost = recvdev = NULL; 580 switch (sendrecv) { 581 case TR_SENDING: 582 rectype = "send"; 583 statfname = pp->stat_send; 584 sendhost = thishost; 585 recvhost = otherhost; 586 break; 587 case TR_RECVING: 588 rectype = "recv"; 589 statfname = pp->stat_recv; 590 sendhost = otherhost; 591 recvhost = thishost; 592 break; 593 case TR_PRINTING: 594 /* 595 * This case is for copying to a device (presumably local, 596 * though filters using things like 'net/CAP' can confuse 597 * this assumption...). 598 */ 599 rectype = "prnt"; 600 statfname = pp->stat_send; 601 sendhost = thishost; 602 recvdev = _PATH_DEFDEVLP; 603 if (pp->lp) recvdev = pp->lp; 604 break; 605 default: 606 /* internal error... should we syslog/printf an error? */ 607 return; 608 } 609 if (statfname == NULL) 610 return; 611 612 /* 613 * the original-host and userid are found out by reading thru the 614 * cf (control-file) for the job. Unfortunately, on incoming jobs 615 * the df's (data-files) are sent before the matching cf, so the 616 * orighost & userid are generally not-available for incoming jobs. 617 * 618 * (it would be nice to create a work-around for that..) 619 */ 620 if (orighost && (*orighost != '\0')) 621 lprhost = orighost; 622 else 623 lprhost = ".na."; 624 if (*userid == '\0') 625 userid = NULL; 626 627 /* 628 * Format of statline. 629 * Some of the keywords listed here are not implemented here, but 630 * they are listed to reserve the meaning for a given keyword. 631 * Fields are separated by a blank. The fields in statline are: 632 * <tstamp> - time the transfer started 633 * <ptrqueue> - name of the printer queue (the short-name...) 634 * <hname> - hostname the file originally came from (the 635 * 'lpr host'), if known, or "_na_" if not known. 636 * <xxx> - id of job from that host (generally three digits) 637 * <n> - file count (# of file within job) 638 * <rectype> - 4-byte field indicating the type of transfer 639 * statistics record. "send" means it's from the 640 * host sending a datafile, "recv" means it's from 641 * a host as it receives a datafile. 642 * user=<userid> - user who sent the job (if known) 643 * secs=<n> - seconds it took to transfer the file 644 * bytes=<n> - number of bytes transfered (ie, "bytecount") 645 * bps=<n.n>e<n> - Bytes/sec (if the transfer was "big enough" 646 * for this to be useful) 647 * ! top=<str> - type of printer (if the type is defined in 648 * printcap, and if this statline is for sending 649 * a file to that ptr) 650 * ! qls=<n> - queue-length at start of send/print-ing a job 651 * ! qle=<n> - queue-length at end of send/print-ing a job 652 * sip=<addr> - IP address of sending host, only included when 653 * receiving a job. 654 * shost=<hname> - sending host (if that does != the original host) 655 * rhost=<hname> - hostname receiving the file (ie, "destination") 656 * rdev=<dev> - device receiving the file, when the file is being 657 * send to a device instead of a remote host. 658 * 659 * Note: A single print job may be transferred multiple times. The 660 * original 'lpr' occurs on one host, and that original host might 661 * send to some interim host (or print server). That interim host 662 * might turn around and send the job to yet another host (most likely 663 * the real printer). The 'shost=' parameter is only included if the 664 * sending host for this particular transfer is NOT the same as the 665 * host which did the original 'lpr'. 666 * 667 * Many values have 'something=' tags before them, because they are 668 * in some sense "optional", or their order may vary. "Optional" may 669 * mean in the sense that different SITES might choose to have other 670 * fields in the record, or that some fields are only included under 671 * some circumstances. Programs processing these records should not 672 * assume the order or existence of any of these keyword fields. 673 */ 674 snprintf(statline, STATLINE_SIZE, "%s %s %s %s %03ld %s", 675 pp->tr_timestr, pp->printer, lprhost, pp->jobnum, 676 pp->jobdfnum, rectype); 677 UPD_EOSTAT(statline); 678 679 if (userid != NULL) { 680 snprintf(eostat, remspace, " user=%s", userid); 681 UPD_EOSTAT(statline); 682 } 683 snprintf(eostat, remspace, " secs=%#.2f bytes=%lu", trtime, 684 (unsigned long)bytecnt); 685 UPD_EOSTAT(statline); 686 687 /* 688 * The bps field duplicates info from bytes and secs, so do 689 * not bother to include it for very small files. 690 */ 691 if ((bytecnt > 25000) && (trtime > 1.1)) { 692 snprintf(eostat, remspace, " bps=%#.2e", 693 ((double)bytecnt/trtime)); 694 UPD_EOSTAT(statline); 695 } 696 697 if (sendrecv == TR_RECVING) { 698 if (remspace > 5+strlen(from_ip) ) { 699 snprintf(eostat, remspace, " sip=%s", from_ip); 700 UPD_EOSTAT(statline); 701 } 702 } 703 if (0 != strcmp(lprhost, sendhost)) { 704 if (remspace > 7+strlen(sendhost) ) { 705 snprintf(eostat, remspace, " shost=%s", sendhost); 706 UPD_EOSTAT(statline); 707 } 708 } 709 if (recvhost) { 710 if (remspace > 7+strlen(recvhost) ) { 711 snprintf(eostat, remspace, " rhost=%s", recvhost); 712 UPD_EOSTAT(statline); 713 } 714 } 715 if (recvdev) { 716 if (remspace > 6+strlen(recvdev) ) { 717 snprintf(eostat, remspace, " rdev=%s", recvdev); 718 UPD_EOSTAT(statline); 719 } 720 } 721 if (remspace > 1) { 722 strcpy(eostat, "\n"); 723 } else { 724 /* probably should back up to just before the final " x=".. */ 725 strcpy(statline+STATLINE_SIZE-2, "\n"); 726 } 727 statfile = open(statfname, O_WRONLY|O_APPEND, 0664); 728 if (statfile < 0) { 729 /* statfile was given, but we can't open it. should we 730 * syslog/printf this as an error? */ 731 return; 732 } 733 write(statfile, statline, strlen(statline)); 734 close(statfile); 735 736 return; 737 #undef UPD_EOSTAT 738 } 739 740 #include <stdarg.h> 741 742 void 743 fatal(const struct printer *pp, const char *msg, ...) 744 { 745 va_list ap; 746 va_start(ap, msg); 747 /* this error message is being sent to the 'from_host' */ 748 if (from_host != local_host) 749 (void)printf("%s: ", local_host); 750 (void)printf("%s: ", progname); 751 if (pp && pp->printer) 752 (void)printf("%s: ", pp->printer); 753 (void)vprintf(msg, ap); 754 va_end(ap); 755 (void)putchar('\n'); 756 exit(1); 757 } 758 759 /* 760 * Close all file descriptors from START on up. 761 * This is a horrific kluge, since getdtablesize() might return 762 * ``infinity'', in which case we will be spending a long time 763 * closing ``files'' which were never open. Perhaps it would 764 * be better to close the first N fds, for some small value of N. 765 */ 766 void 767 closeallfds(int start) 768 { 769 int stop = getdtablesize(); 770 for (; start < stop; start++) 771 close(start); 772 } 773 774