1 /* 2 * Copyright (c) 1983, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 #ifndef lint 35 /* 36 static char sccsid[] = "@(#)displayq.c 8.4 (Berkeley) 4/28/95"; 37 */ 38 static const char rcsid[] = 39 "$Id: displayq.c,v 1.13 1997/12/02 20:45:19 wollman Exp $"; 40 #endif /* not lint */ 41 42 #include <sys/param.h> 43 #include <sys/stat.h> 44 45 #include <ctype.h> 46 #include <dirent.h> 47 #include <errno.h> 48 #include <fcntl.h> 49 #include <signal.h> 50 #include <stdio.h> 51 #include <stdlib.h> 52 #include <string.h> 53 #define psignal foil_gcc_psignal 54 #define sys_siglist foil_gcc_siglist 55 #include <unistd.h> 56 #undef psignal 57 #undef sys_siglist 58 59 #include "lp.h" 60 #include "lp.local.h" 61 #include "pathnames.h" 62 63 /* 64 * Routines to display the state of the queue. 65 */ 66 #define JOBCOL 40 /* column for job # in -l format */ 67 #define OWNCOL 7 /* start of Owner column in normal */ 68 #define SIZCOL 62 /* start of Size column in normal */ 69 70 /* 71 * Stuff for handling job specifications 72 */ 73 extern uid_t uid, euid; 74 75 static int col; /* column on screen */ 76 static char current[40]; /* current file being printed */ 77 static char file[132]; /* print file name */ 78 static int first; /* first file in ``files'' column? */ 79 static int garbage; /* # of garbage cf files */ 80 static int lflag; /* long output option */ 81 static int rank; /* order to be printed (-1=none, 0=active) */ 82 static long totsize; /* total print job size in bytes */ 83 84 static char *head0 = "Rank Owner Job Files"; 85 static char *head1 = "Total Size\n"; 86 87 static void alarmhandler __P((int)); 88 static void warn __P((const struct printer *pp)); 89 90 /* 91 * Display the current state of the queue. Format = 1 if long format. 92 */ 93 void 94 displayq(pp, format) 95 struct printer *pp; 96 int format; 97 { 98 register struct queue *q; 99 register int i, nitems, fd, ret; 100 register char *cp; 101 struct queue **queue; 102 struct stat statb; 103 FILE *fp; 104 void (*savealrm)(int); 105 106 lflag = format; 107 totsize = 0; 108 rank = -1; 109 110 if ((cp = checkremote(pp))) { 111 printf("Warning: %s\n", cp); 112 free(cp); 113 } 114 115 /* 116 * Print out local queue 117 * Find all the control files in the spooling directory 118 */ 119 seteuid(euid); 120 if (chdir(pp->spool_dir) < 0) 121 fatal(pp, "cannot chdir to spooling directory: %s", 122 strerror(errno)); 123 seteuid(uid); 124 if ((nitems = getq(pp, &queue)) < 0) 125 fatal(pp, "cannot examine spooling area\n"); 126 seteuid(euid); 127 ret = stat(pp->lock_file, &statb); 128 seteuid(uid); 129 if (ret >= 0) { 130 if (statb.st_mode & LFM_PRINT_DIS) { 131 if (pp->remote) 132 printf("%s: ", host); 133 printf("Warning: %s is down: ", pp->printer); 134 seteuid(euid); 135 fd = open(pp->status_file, O_RDONLY|O_SHLOCK); 136 seteuid(uid); 137 if (fd >= 0) { 138 while ((i = read(fd, line, sizeof(line))) > 0) 139 (void) fwrite(line, 1, i, stdout); 140 (void) close(fd); /* unlocks as well */ 141 } else 142 putchar('\n'); 143 } 144 if (statb.st_mode & LFM_QUEUE_DIS) { 145 if (pp->remote) 146 printf("%s: ", host); 147 printf("Warning: %s queue is turned off\n", 148 pp->printer); 149 } 150 } 151 152 if (nitems) { 153 seteuid(euid); 154 fp = fopen(pp->lock_file, "r"); 155 seteuid(uid); 156 if (fp == NULL) 157 warn(pp); 158 else { 159 /* get daemon pid */ 160 cp = current; 161 while ((i = getc(fp)) != EOF && i != '\n') 162 *cp++ = i; 163 *cp = '\0'; 164 i = atoi(current); 165 if (i <= 0) { 166 ret = -1; 167 } else { 168 seteuid(euid); 169 ret = kill(i, 0); 170 seteuid(uid); 171 } 172 if (ret < 0) { 173 warn(pp); 174 } else { 175 /* read current file name */ 176 cp = current; 177 while ((i = getc(fp)) != EOF && i != '\n') 178 *cp++ = i; 179 *cp = '\0'; 180 /* 181 * Print the status file. 182 */ 183 if (pp->remote) 184 printf("%s: ", host); 185 seteuid(euid); 186 fd = open(pp->status_file, O_RDONLY|O_SHLOCK); 187 seteuid(uid); 188 if (fd >= 0) { 189 while ((i = read(fd, line, 190 sizeof(line))) > 0) 191 fwrite(line, 1, i, stdout); 192 close(fd); /* unlocks as well */ 193 } else 194 putchar('\n'); 195 } 196 (void) fclose(fp); 197 } 198 /* 199 * Now, examine the control files and print out the jobs to 200 * be done for each user. 201 */ 202 if (!lflag) 203 header(); 204 for (i = 0; i < nitems; i++) { 205 q = queue[i]; 206 inform(pp, q->q_name); 207 free(q); 208 } 209 free(queue); 210 } 211 if (!pp->remote) { 212 if (nitems == 0) 213 puts("no entries"); 214 return; 215 } 216 217 /* 218 * Print foreign queue 219 * Note that a file in transit may show up in either queue. 220 */ 221 if (nitems) 222 putchar('\n'); 223 (void) snprintf(line, sizeof(line), "%c%s", format ? '\4' : '\3', 224 pp->remote_queue); 225 cp = line; 226 for (i = 0; i < requests && cp-line+10 < sizeof(line) - 1; i++) { 227 cp += strlen(cp); 228 (void) sprintf(cp, " %d", requ[i]); 229 } 230 for (i = 0; i < users && cp - line + 1 + strlen(user[i]) < 231 sizeof(line) - 1; i++) { 232 cp += strlen(cp); 233 *cp++ = ' '; 234 (void) strcpy(cp, user[i]); 235 } 236 strcat(line, "\n"); 237 savealrm = signal(SIGALRM, alarmhandler); 238 alarm(pp->conn_timeout); 239 fd = getport(pp, pp->remote_host, 0); 240 alarm(0); 241 (void)signal(SIGALRM, savealrm); 242 if (fd < 0) { 243 if (from != host) 244 printf("%s: ", host); 245 printf("connection to %s is down\n", pp->remote_host); 246 } 247 else { 248 i = strlen(line); 249 if (write(fd, line, i) != i) 250 fatal(pp, "Lost connection"); 251 while ((i = read(fd, line, sizeof(line))) > 0) 252 (void) fwrite(line, 1, i, stdout); 253 (void) close(fd); 254 } 255 } 256 257 /* 258 * Print a warning message if there is no daemon present. 259 */ 260 static void 261 warn(pp) 262 const struct printer *pp; 263 { 264 if (pp->remote) 265 printf("%s: ", host); 266 puts("Warning: no daemon present"); 267 current[0] = '\0'; 268 } 269 270 /* 271 * Print the header for the short listing format 272 */ 273 void 274 header() 275 { 276 printf(head0); 277 col = strlen(head0)+1; 278 blankfill(SIZCOL); 279 printf(head1); 280 } 281 282 void 283 inform(pp, cf) 284 const struct printer *pp; 285 char *cf; 286 { 287 register int j; 288 FILE *cfp; 289 290 /* 291 * There's a chance the control file has gone away 292 * in the meantime; if this is the case just keep going 293 */ 294 seteuid(euid); 295 if ((cfp = fopen(cf, "r")) == NULL) 296 return; 297 seteuid(uid); 298 299 if (rank < 0) 300 rank = 0; 301 if (pp->remote || garbage || strcmp(cf, current)) 302 rank++; 303 j = 0; 304 while (getline(cfp)) { 305 switch (line[0]) { 306 case 'P': /* Was this file specified in the user's list? */ 307 if (!inlist(line+1, cf)) { 308 fclose(cfp); 309 return; 310 } 311 if (lflag) { 312 printf("\n%s: ", line+1); 313 col = strlen(line+1) + 2; 314 prank(rank); 315 blankfill(JOBCOL); 316 printf(" [job %s]\n", cf+3); 317 } else { 318 col = 0; 319 prank(rank); 320 blankfill(OWNCOL); 321 printf("%-10s %-3d ", line+1, atoi(cf+3)); 322 col += 16; 323 first = 1; 324 } 325 continue; 326 default: /* some format specifer and file name? */ 327 if (line[0] < 'a' || line[0] > 'z') 328 continue; 329 if (j == 0 || strcmp(file, line+1) != 0) { 330 (void) strncpy(file, line+1, sizeof(file) - 1); 331 file[sizeof(file) - 1] = '\0'; 332 } 333 j++; 334 continue; 335 case 'N': 336 show(line+1, file, j); 337 file[0] = '\0'; 338 j = 0; 339 } 340 } 341 fclose(cfp); 342 if (!lflag) { 343 blankfill(SIZCOL); 344 printf("%ld bytes\n", totsize); 345 totsize = 0; 346 } 347 } 348 349 int 350 inlist(name, file) 351 char *name, *file; 352 { 353 register int *r, n; 354 register char **u, *cp; 355 356 if (users == 0 && requests == 0) 357 return(1); 358 /* 359 * Check to see if it's in the user list 360 */ 361 for (u = user; u < &user[users]; u++) 362 if (!strcmp(*u, name)) 363 return(1); 364 /* 365 * Check the request list 366 */ 367 for (n = 0, cp = file+3; isdigit(*cp); ) 368 n = n * 10 + (*cp++ - '0'); 369 for (r = requ; r < &requ[requests]; r++) 370 if (*r == n && !strcmp(cp, from)) 371 return(1); 372 return(0); 373 } 374 375 void 376 show(nfile, file, copies) 377 register char *nfile, *file; 378 int copies; 379 { 380 if (strcmp(nfile, " ") == 0) 381 nfile = "(standard input)"; 382 if (lflag) 383 ldump(nfile, file, copies); 384 else 385 dump(nfile, file, copies); 386 } 387 388 /* 389 * Fill the line with blanks to the specified column 390 */ 391 void 392 blankfill(n) 393 register int n; 394 { 395 while (col++ < n) 396 putchar(' '); 397 } 398 399 /* 400 * Give the abbreviated dump of the file names 401 */ 402 void 403 dump(nfile, file, copies) 404 char *nfile, *file; 405 int copies; 406 { 407 register short n, fill; 408 struct stat lbuf; 409 410 /* 411 * Print as many files as will fit 412 * (leaving room for the total size) 413 */ 414 fill = first ? 0 : 2; /* fill space for ``, '' */ 415 if (((n = strlen(nfile)) + col + fill) >= SIZCOL-4) { 416 if (col < SIZCOL) { 417 printf(" ..."), col += 4; 418 blankfill(SIZCOL); 419 } 420 } else { 421 if (first) 422 first = 0; 423 else 424 printf(", "); 425 printf("%s", nfile); 426 col += n+fill; 427 } 428 seteuid(euid); 429 if (*file && !stat(file, &lbuf)) 430 totsize += copies * lbuf.st_size; 431 seteuid(uid); 432 } 433 434 /* 435 * Print the long info about the file 436 */ 437 void 438 ldump(nfile, file, copies) 439 char *nfile, *file; 440 int copies; 441 { 442 struct stat lbuf; 443 444 putchar('\t'); 445 if (copies > 1) 446 printf("%-2d copies of %-19s", copies, nfile); 447 else 448 printf("%-32s", nfile); 449 if (*file && !stat(file, &lbuf)) 450 printf(" %qd bytes", (long long) lbuf.st_size); 451 else 452 printf(" ??? bytes"); 453 putchar('\n'); 454 } 455 456 /* 457 * Print the job's rank in the queue, 458 * update col for screen management 459 */ 460 void 461 prank(n) 462 int n; 463 { 464 char rline[100]; 465 static char *r[] = { 466 "th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th" 467 }; 468 469 if (n == 0) { 470 printf("active"); 471 col += 6; 472 return; 473 } 474 if ((n/10)%10 == 1) 475 (void)snprintf(rline, sizeof(rline), "%dth", n); 476 else 477 (void)snprintf(rline, sizeof(rline), "%d%s", n, r[n%10]); 478 col += strlen(rline); 479 printf("%s", rline); 480 } 481 482 void 483 alarmhandler(signo) 484 int signo; 485 { 486 /* ignored */ 487 } 488