cmds.c (95291) | cmds.c (98152) |
---|---|
1/* 2 * Copyright (c) 1983, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: --- 29 unchanged lines hidden (view full) --- 38 The Regents of the University of California. All rights reserved.\n"; 39#endif /* not lint */ 40 41#ifndef lint 42/* 43static char sccsid[] = "@(#)cmds.c 8.2 (Berkeley) 4/28/95"; 44*/ 45static const char rcsid[] = | 1/* 2 * Copyright (c) 1983, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: --- 29 unchanged lines hidden (view full) --- 38 The Regents of the University of California. All rights reserved.\n"; 39#endif /* not lint */ 40 41#ifndef lint 42/* 43static char sccsid[] = "@(#)cmds.c 8.2 (Berkeley) 4/28/95"; 44*/ 45static const char rcsid[] = |
46 "$FreeBSD: head/usr.sbin/lpr/lpc/cmds.c 95291 2002-04-22 23:28:42Z gad $"; | 46 "$FreeBSD: head/usr.sbin/lpr/lpc/cmds.c 98152 2002-06-13 01:55:48Z gad $"; |
47#endif /* not lint */ 48 49/* 50 * lpc -- line printer control program -- commands: 51 */ 52 53#include <sys/param.h> 54#include <sys/time.h> --- 10 unchanged lines hidden (view full) --- 65#include <ctype.h> 66#include <string.h> 67#include "lp.h" 68#include "lp.local.h" 69#include "lpc.h" 70#include "extern.h" 71#include "pathnames.h" 72 | 47#endif /* not lint */ 48 49/* 50 * lpc -- line printer control program -- commands: 51 */ 52 53#include <sys/param.h> 54#include <sys/time.h> --- 10 unchanged lines hidden (view full) --- 65#include <ctype.h> 66#include <string.h> 67#include "lp.h" 68#include "lp.local.h" 69#include "lpc.h" 70#include "extern.h" 71#include "pathnames.h" 72 |
73/* 74 * Return values from kill_qtask(). 75 */ 76#define KQT_LFERROR -2 77#define KQT_KILLFAIL -1 78#define KQT_NODAEMON 0 79#define KQT_KILLOK 1 80 |
|
73static void abortpr(struct printer *_pp, int _dis); 74static int doarg(char *_job); 75static int doselect(struct dirent *_d); | 81static void abortpr(struct printer *_pp, int _dis); 82static int doarg(char *_job); 83static int doselect(struct dirent *_d); |
84static int kill_qtask(const char *lf); |
|
76static void putmsg(struct printer *_pp, int _argc, char **_argv); 77static int sortq(const void *_a, const void *_b); 78static void startpr(struct printer *_pp, int _chgenable); 79static int touch(struct jobqueue *_jq); 80static void unlinkf(char *_name); 81static void upstat(struct printer *_pp, const char *_msg); 82static void wrapup_clean(int _laststatus); 83 --- 189 unchanged lines hidden (view full) --- 273 printf("\tWarning: daemon (pid %d) not killed\n", pid); 274 } else 275 printf("\tdaemon (pid %d) killed\n", pid); 276out: 277 seteuid(uid); 278} 279 280/* | 85static void putmsg(struct printer *_pp, int _argc, char **_argv); 86static int sortq(const void *_a, const void *_b); 87static void startpr(struct printer *_pp, int _chgenable); 88static int touch(struct jobqueue *_jq); 89static void unlinkf(char *_name); 90static void upstat(struct printer *_pp, const char *_msg); 91static void wrapup_clean(int _laststatus); 92 --- 189 unchanged lines hidden (view full) --- 282 printf("\tWarning: daemon (pid %d) not killed\n", pid); 283 } else 284 printf("\tdaemon (pid %d) killed\n", pid); 285out: 286 seteuid(uid); 287} 288 289/* |
290 * Kill the current daemon, to stop printing of the active job. 291 */ 292static int 293kill_qtask(const char *lf) 294{ 295 FILE *fp; 296 pid_t pid; 297 int errsav, killres, lockres, res; 298 299 seteuid(euid); 300 fp = fopen(lf, "r"); 301 errsav = errno; 302 seteuid(uid); 303 res = KQT_NODAEMON; 304 if (fp == NULL) { 305 /* 306 * If there is no lock file, then there is no daemon to 307 * kill. Any other error return means there is some 308 * kind of problem with the lock file. 309 */ 310 if (errsav != ENOENT) 311 res = KQT_LFERROR; 312 goto killdone; 313 } 314 315 /* If the lock file is empty, then there is no daemon to kill */ 316 if (getline(fp) == 0) 317 goto killdone; 318 319 /* 320 * If the file can be locked without blocking, then there 321 * no daemon to kill, or we should not try to kill it. 322 * 323 * XXX - not sure I understand the reasoning behind this... 324 */ 325 lockres = flock(fileno(fp), LOCK_SH|LOCK_NB); 326 (void) fclose(fp); 327 if (lockres == 0) 328 goto killdone; 329 330 pid = atoi(line); 331 if (pid < 0) { 332 /* 333 * If we got a negative pid, then the contents of the 334 * lock file is not valid. 335 */ 336 res = KQT_LFERROR; 337 goto killdone; 338 } 339 340 seteuid(uid); 341 killres = kill(pid, SIGTERM); 342 errsav = errno; 343 seteuid(uid); 344 if (killres == 0) { 345 res = KQT_KILLOK; 346 printf("\tdaemon (pid %d) killed\n", pid); 347 } else if (errno == ESRCH) { 348 res = KQT_NODAEMON; 349 } else { 350 res = KQT_KILLFAIL; 351 printf("\tWarning: daemon (pid %d) not killed:\n", pid); 352 printf("\t %s\n", strerror(errsav)); 353 } 354 355killdone: 356 switch (res) { 357 case KQT_LFERROR: 358 printf("\tcannot open lock file: %s\n", 359 strerror(errsav)); 360 break; 361 case KQT_NODAEMON: 362 printf("\tno daemon to abort\n"); 363 break; 364 case KQT_KILLFAIL: 365 case KQT_KILLOK: 366 /* These two already printed messages to the user. */ 367 break; 368 default: 369 printf("\t<internal error in kill_qtask>\n"); 370 break; 371 } 372 373 return (res); 374} 375 376/* |
|
281 * Write a message into the status file. 282 */ 283static void 284upstat(struct printer *pp, const char *msg) 285{ 286 register int fd; 287 char statfile[MAXPATHLEN]; 288 --- 8 unchanged lines hidden (view full) --- 297 if (msg == (char *)NULL) 298 (void) write(fd, "\n", 1); 299 else 300 (void) write(fd, msg, strlen(msg)); 301 (void) close(fd); 302} 303 304/* | 377 * Write a message into the status file. 378 */ 379static void 380upstat(struct printer *pp, const char *msg) 381{ 382 register int fd; 383 char statfile[MAXPATHLEN]; 384 --- 8 unchanged lines hidden (view full) --- 393 if (msg == (char *)NULL) 394 (void) write(fd, "\n", 1); 395 else 396 (void) write(fd, msg, strlen(msg)); 397 (void) close(fd); 398} 399 400/* |
401 * kill an existing daemon and disable printing. 402 */ 403void 404abort_q(struct printer *pp) 405{ 406 int killres, setres; 407 char lf[MAXPATHLEN]; 408 409 lock_file_name(pp, lf, sizeof lf); 410 printf("%s:\n", pp->printer); 411 412 /* 413 * Turn on the owner execute bit of the lock file to disable printing. 414 */ 415 setres = set_qstate(SQS_STOPP, lf); 416 417 /* 418 * If set_qstate found that there already was a lock file, then 419 * call a routine which will read that lock file and kill the 420 * lpd-process which is listed in that lock file. If the lock 421 * file did not exist, then either there is no daemon running 422 * for this queue, or there is one running but *it* could not 423 * write a lock file (which means we can not determine the 424 * process id of that lpd-process). 425 */ 426 switch (setres) { 427 case SQS_CHGOK: 428 case SQS_CHGFAIL: 429 /* Kill the process */ 430 killres = kill_qtask(lf); 431 break; 432 case SQS_CREOK: 433 case SQS_CREFAIL: 434 printf("\tno daemon to abort\n"); 435 break; 436 case SQS_STATFAIL: 437 printf("\tassuming no daemon to abort\n"); 438 break; 439 default: 440 printf("\t<unexpected result (%d) from set_qstate>\n", 441 setres); 442 break; 443 } 444 445 if (setres >= 0) { 446 seteuid(euid); 447 upstat(pp, "printing disabled\n"); 448 seteuid(uid); 449 } 450} 451 452/* |
|
305 * "global" variables for all the routines related to 'clean' and 'tclean' 306 */ 307static time_t cln_now; /* current time */ 308static double cln_minage; /* minimum age before file is removed */ 309static long cln_sizecnt; /* amount of space freed up */ 310static int cln_debug; /* print extra debugging msgs */ 311static int cln_filecnt; /* number of files destroyed */ 312static int cln_foundcore; /* found a core file! */ --- 432 unchanged lines hidden (view full) --- 745 printf("\tcannot enable queuing\n"); 746 else 747 printf("\tqueuing enabled\n"); 748 } 749 seteuid(uid); 750} 751 752/* | 453 * "global" variables for all the routines related to 'clean' and 'tclean' 454 */ 455static time_t cln_now; /* current time */ 456static double cln_minage; /* minimum age before file is removed */ 457static long cln_sizecnt; /* amount of space freed up */ 458static int cln_debug; /* print extra debugging msgs */ 459static int cln_filecnt; /* number of files destroyed */ 460static int cln_foundcore; /* found a core file! */ --- 432 unchanged lines hidden (view full) --- 893 printf("\tcannot enable queuing\n"); 894 else 895 printf("\tqueuing enabled\n"); 896 } 897 seteuid(uid); 898} 899 900/* |
901 * Enable queuing to the printer (allow lpr to add new jobs to the queue). 902 */ 903void 904enable_q(struct printer *pp) 905{ 906 int setres; 907 char lf[MAXPATHLEN]; 908 909 lock_file_name(pp, lf, sizeof lf); 910 printf("%s:\n", pp->printer); 911 912 setres = set_qstate(SQS_ENABLEQ, lf); 913} 914 915/* |
|
753 * Disable queuing. 754 */ 755void 756disable(struct printer *pp) 757{ 758 register int fd; 759 struct stat stbuf; 760 char lf[MAXPATHLEN]; --- 20 unchanged lines hidden (view full) --- 781 printf("\tqueuing disabled\n"); 782 } 783 } else 784 printf("\tcannot stat lock file\n"); 785 seteuid(uid); 786} 787 788/* | 916 * Disable queuing. 917 */ 918void 919disable(struct printer *pp) 920{ 921 register int fd; 922 struct stat stbuf; 923 char lf[MAXPATHLEN]; --- 20 unchanged lines hidden (view full) --- 944 printf("\tqueuing disabled\n"); 945 } 946 } else 947 printf("\tcannot stat lock file\n"); 948 seteuid(uid); 949} 950 951/* |
952 * Disable queuing. 953 */ 954void 955disable_q(struct printer *pp) 956{ 957 int setres; 958 char lf[MAXPATHLEN]; 959 960 lock_file_name(pp, lf, sizeof lf); 961 printf("%s:\n", pp->printer); 962 963 setres = set_qstate(SQS_DISABLEQ, lf); 964} 965 966/* |
|
789 * Disable queuing and printing and put a message into the status file 790 * (reason for being down). 791 */ 792void 793down(int argc, char *argv[]) 794{ 795 int cmdstatus, more; 796 struct printer myprinter, *pp = &myprinter; --- 122 unchanged lines hidden (view full) --- 919void 920restart(struct printer *pp) 921{ 922 abortpr(pp, 0); 923 startpr(pp, 0); 924} 925 926/* | 967 * Disable queuing and printing and put a message into the status file 968 * (reason for being down). 969 */ 970void 971down(int argc, char *argv[]) 972{ 973 int cmdstatus, more; 974 struct printer myprinter, *pp = &myprinter; --- 122 unchanged lines hidden (view full) --- 1097void 1098restart(struct printer *pp) 1099{ 1100 abortpr(pp, 0); 1101 startpr(pp, 0); 1102} 1103 1104/* |
1105 * Kill and restart the daemon. 1106 */ 1107void 1108restart_q(struct printer *pp) 1109{ 1110 int killres, setres, startok; 1111 char lf[MAXPATHLEN]; 1112 1113 lock_file_name(pp, lf, sizeof lf); 1114 printf("%s:\n", pp->printer); 1115 1116 killres = kill_qtask(lf); 1117 1118 /* 1119 * XXX - if the kill worked, we should probably sleep for 1120 * a second or so before trying to restart the queue. 1121 */ 1122 1123 /* make sure the queue is set to print jobs */ 1124 setres = set_qstate(SQS_STARTP, lf); 1125 1126 seteuid(euid); 1127 startok = startdaemon(pp); 1128 seteuid(uid); 1129 if (!startok) 1130 printf("\tcouldn't restart daemon\n"); 1131 else 1132 printf("\tdaemon restarted\n"); 1133} 1134 1135/* |
|
927 * Enable printing on the specified printer and startup the daemon. 928 */ 929void 930startcmd(struct printer *pp) 931{ 932 startpr(pp, 1); 933} 934 --- 22 unchanged lines hidden (view full) --- 957 if (!startdaemon(pp)) 958 printf("\tcouldn't start daemon\n"); 959 else 960 printf("\tdaemon started\n"); 961 seteuid(uid); 962} 963 964/* | 1136 * Enable printing on the specified printer and startup the daemon. 1137 */ 1138void 1139startcmd(struct printer *pp) 1140{ 1141 startpr(pp, 1); 1142} 1143 --- 22 unchanged lines hidden (view full) --- 1166 if (!startdaemon(pp)) 1167 printf("\tcouldn't start daemon\n"); 1168 else 1169 printf("\tdaemon started\n"); 1170 seteuid(uid); 1171} 1172 1173/* |
1174 * Enable printing on the specified printer and startup the daemon. 1175 */ 1176void 1177start_q(struct printer *pp) 1178{ 1179 int setres, startok; 1180 char lf[MAXPATHLEN]; 1181 1182 lock_file_name(pp, lf, sizeof lf); 1183 printf("%s:\n", pp->printer); 1184 1185 setres = set_qstate(SQS_STARTP, lf); 1186 1187 seteuid(euid); 1188 startok = startdaemon(pp); 1189 seteuid(uid); 1190 if (!startok) 1191 printf("\tcouldn't start daemon\n"); 1192 else 1193 printf("\tdaemon started\n"); 1194 seteuid(uid); 1195} 1196 1197/* |
|
965 * Print the status of the printer queue. 966 */ 967void 968status(struct printer *pp) 969{ 970 struct stat stbuf; 971 register int fd, i; 972 register struct dirent *dp; --- 86 unchanged lines hidden (view full) --- 1059 upstat(pp, "printing disabled\n"); 1060 printf("\tprinting disabled\n"); 1061 } 1062 } else 1063 printf("\tcannot stat lock file\n"); 1064 seteuid(uid); 1065} 1066 | 1198 * Print the status of the printer queue. 1199 */ 1200void 1201status(struct printer *pp) 1202{ 1203 struct stat stbuf; 1204 register int fd, i; 1205 register struct dirent *dp; --- 86 unchanged lines hidden (view full) --- 1292 upstat(pp, "printing disabled\n"); 1293 printf("\tprinting disabled\n"); 1294 } 1295 } else 1296 printf("\tcannot stat lock file\n"); 1297 seteuid(uid); 1298} 1299 |
1300/* 1301 * Stop the specified daemon after completing the current job and disable 1302 * printing. 1303 */ 1304void 1305stop_q(struct printer *pp) 1306{ 1307 int setres; 1308 char lf[MAXPATHLEN]; 1309 1310 lock_file_name(pp, lf, sizeof lf); 1311 printf("%s:\n", pp->printer); 1312 1313 setres = set_qstate(SQS_STOPP, lf); 1314 1315 if (setres >= 0) { 1316 seteuid(euid); 1317 upstat(pp, "printing disabled\n"); 1318 seteuid(uid); 1319 } 1320} 1321 |
|
1067struct jobqueue **queue; 1068int nitems; 1069time_t mtime; 1070 1071/* 1072 * Put the specified jobs at the top of printer queue. 1073 */ 1074void --- 157 unchanged lines hidden (view full) --- 1232/* 1233 * Enable everything and start printer (undo `down'). 1234 */ 1235void 1236up(struct printer *pp) 1237{ 1238 startpr(pp, 2); 1239} | 1322struct jobqueue **queue; 1323int nitems; 1324time_t mtime; 1325 1326/* 1327 * Put the specified jobs at the top of printer queue. 1328 */ 1329void --- 157 unchanged lines hidden (view full) --- 1487/* 1488 * Enable everything and start printer (undo `down'). 1489 */ 1490void 1491up(struct printer *pp) 1492{ 1493 startpr(pp, 2); 1494} |
1495 1496/* 1497 * Enable both queuing & printing, and start printer (undo `down'). 1498 */ 1499void 1500up_q(struct printer *pp) 1501{ 1502 int setres, startok; 1503 char lf[MAXPATHLEN]; 1504 1505 lock_file_name(pp, lf, sizeof lf); 1506 printf("%s:\n", pp->printer); 1507 1508 setres = set_qstate(SQS_ENABLEQ+SQS_STARTP, lf); 1509 1510 seteuid(euid); 1511 startok = startdaemon(pp); 1512 seteuid(uid); 1513 if (!startok) 1514 printf("\tcouldn't start daemon\n"); 1515 else 1516 printf("\tdaemon started\n"); 1517} |
|