2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 4. Neither the name of the University nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 static const char copyright[] =
33 "@(#) Copyright (c) 1983, 1993\n\
34 The Regents of the University of California. All rights reserved.\n";
39 static char sccsid[] = "@(#)cmds.c 8.2 (Berkeley) 4/28/95";
43 #include "lp.cdefs.h" /* A cross-platform version of <sys/cdefs.h> */
44 __FBSDID("$FreeBSD$");
47 * lpc -- line printer control program -- commands:
50 #include <sys/param.h>
69 #include "pathnames.h"
72 * Return values from kill_qtask().
74 #define KQT_LFERROR -2
75 #define KQT_KILLFAIL -1
76 #define KQT_NODAEMON 0
79 static char *args2line(int argc, char **argv);
80 static int doarg(char *_job);
81 static int doselect(const struct dirent *_d);
82 static int kill_qtask(const char *lf);
83 static int sortq(const struct dirent **a, const struct dirent **b);
84 static int touch(struct jobqueue *_jq);
85 static void unlinkf(char *_name);
86 static void upstat(struct printer *_pp, const char *_msg, int _notify);
87 static void wrapup_clean(int _laststatus);
90 * generic framework for commands which operate on all or a specified
93 enum qsel_val { /* how a given ptr was selected */
94 QSEL_UNKNOWN = -1, /* ... not selected yet */
95 QSEL_BYNAME = 0, /* ... user specifed it by name */
96 QSEL_ALL = 1 /* ... user wants "all" printers */
97 /* (with more to come) */
100 static enum qsel_val generic_qselect; /* indicates how ptr was selected */
101 static int generic_initerr; /* result of initrtn processing */
102 static char *generic_cmdname;
103 static char *generic_msg; /* if a -msg was specified */
104 static char *generic_nullarg;
105 static void (*generic_wrapup)(int _last_status); /* perform rtn wrap-up */
108 generic(void (*specificrtn)(struct printer *_pp), int cmdopts,
109 void (*initrtn)(int _argc, char *_argv[]), int argc, char *argv[])
111 int cmdstatus, more, targc;
112 struct printer myprinter, *pp;
113 char **margv, **targv;
117 * Usage needs a special case for 'down': The user must
118 * either include `-msg', or only the first parameter
119 * that they give will be processed as a printer name.
121 printf("usage: %s {all | printer ...}", argv[0]);
122 if (strcmp(argv[0], "down") == 0) {
123 printf(" -msg [<text> ...]\n");
124 printf(" or: down {all | printer} [<text> ...]");
125 } else if (cmdopts & LPC_MSGOPT)
126 printf(" [-msg <text> ...]");
131 /* The first argument is the command name. */
132 generic_cmdname = *argv++;
136 * The initialization routine for a command might set a generic
137 * "wrapup" routine, which should be called after processing all
138 * the printers in the command. This might print summary info.
140 * Note that the initialization routine may also parse (and
141 * nullify) some of the parameters given on the command, leaving
142 * only the parameters which have to do with printer names.
145 generic_wrapup = NULL;
146 generic_qselect = QSEL_UNKNOWN;
148 /* this just needs to be a distinct value of type 'char *' */
149 if (generic_nullarg == NULL)
150 generic_nullarg = strdup("");
153 * Some commands accept a -msg argument, which indicates that
154 * all remaining arguments should be combined into a string.
157 if (cmdopts & LPC_MSGOPT) {
160 for (; targc > 0; targc--, targv++) {
161 if (strcmp(*targv, "-msg") == 0) {
163 generic_msg = args2line(targc - 1, targv + 1);
168 printf("error: No printer name(s) specified before"
170 printf("usage: %s {all | printer ...}",
172 printf(" [-msg <text> ...]\n");
177 /* call initialization routine, if there is one for this cmd */
178 if (initrtn != NULL) {
180 (*initrtn)(argc, argv);
184 * The initrtn may have null'ed out some of the parameters.
185 * Compact the parameter list to remove those nulls, and
186 * correct the arg-count.
192 for (; targc > 0; targc--, targv++) {
193 if (*targv != generic_nullarg) {
202 if (argc == 1 && strcmp(*argv, "all") == 0) {
203 generic_qselect = QSEL_ALL;
204 more = firstprinter(pp, &cmdstatus);
210 more = nextprinter(pp, &cmdstatus);
214 printf("warning: %s: unresolved "
217 case PCAPERR_SUCCESS:
220 fatal(pp, "%s", pcaperr(cmdstatus));
222 } while (more && cmdstatus);
227 generic_qselect = QSEL_BYNAME; /* specifically-named ptrs */
228 for (; argc > 0; argc--, argv++) {
230 cmdstatus = getprintcap(*argv, pp);
233 fatal(pp, "%s", pcaperr(cmdstatus));
234 case PCAPERR_NOTFOUND:
235 printf("unknown printer %s\n", *argv);
238 printf("warning: %s: unresolved tc= reference(s)\n",
241 case PCAPERR_SUCCESS:
248 if (generic_wrapup) {
249 (*generic_wrapup)(cmdstatus);
257 * Convert an argv-array of character strings into a single string.
260 args2line(int argc, char **argv)
270 cend = buf + sizeof(buf) - 1; /* save room for '\0' */
271 while (--argc >= 0) {
273 while ((cp1 < cend) && (*cp1++ = *cp2++))
283 * Kill the current daemon, to stop printing of the active job.
286 kill_qtask(const char *lf)
290 int errsav, killres, lockres, res;
299 * If there is no lock file, then there is no daemon to
300 * kill. Any other error return means there is some
301 * kind of problem with the lock file.
303 if (errsav != ENOENT)
308 /* If the lock file is empty, then there is no daemon to kill */
309 if (getline(fp) == 0)
313 * If the file can be locked without blocking, then there
314 * no daemon to kill, or we should not try to kill it.
316 * XXX - not sure I understand the reasoning behind this...
318 lockres = flock(fileno(fp), LOCK_SH|LOCK_NB);
326 * If we got a negative pid, then the contents of the
327 * lock file is not valid.
334 killres = kill(pid, SIGTERM);
339 printf("\tdaemon (pid %d) killed\n", pid);
340 } else if (errno == ESRCH) {
344 printf("\tWarning: daemon (pid %d) not killed:\n", pid);
345 printf("\t %s\n", strerror(errsav));
351 printf("\tcannot open lock file: %s\n",
355 printf("\tno daemon to abort\n");
359 /* These two already printed messages to the user. */
362 printf("\t<internal error in kill_qtask>\n");
370 * Write a message into the status file.
373 upstat(struct printer *pp, const char *msg, int notifyuser)
376 char statfile[MAXPATHLEN];
378 status_file_name(pp, statfile, sizeof statfile);
381 fd = open(statfile, O_WRONLY|O_CREAT|O_EXLOCK, STAT_FILE_MODE);
384 printf("\tcannot create status file: %s\n", strerror(errno));
387 (void) ftruncate(fd, 0);
389 (void) write(fd, "\n", 1);
391 (void) write(fd, msg, strlen(msg));
394 if ((msg == (char *)NULL) || (strcmp(msg, "\n") == 0))
395 printf("\tstatus message is now set to nothing.\n");
397 printf("\tstatus message is now: %s", msg);
402 * kill an existing daemon and disable printing.
405 abort_q(struct printer *pp)
410 lock_file_name(pp, lf, sizeof lf);
411 printf("%s:\n", pp->printer);
414 * Turn on the owner execute bit of the lock file to disable printing.
416 setres = set_qstate(SQS_STOPP, lf);
419 * If set_qstate found that there already was a lock file, then
420 * call a routine which will read that lock file and kill the
421 * lpd-process which is listed in that lock file. If the lock
422 * file did not exist, then either there is no daemon running
423 * for this queue, or there is one running but *it* could not
424 * write a lock file (which means we can not determine the
425 * process id of that lpd-process).
430 /* Kill the process */
431 killres = kill_qtask(lf);
435 printf("\tno daemon to abort\n");
438 printf("\tassuming no daemon to abort\n");
441 printf("\t<unexpected result (%d) from set_qstate>\n",
447 upstat(pp, "printing disabled\n", 0);
451 * "global" variables for all the routines related to 'clean' and 'tclean'
453 static time_t cln_now; /* current time */
454 static double cln_minage; /* minimum age before file is removed */
455 static long cln_sizecnt; /* amount of space freed up */
456 static int cln_debug; /* print extra debugging msgs */
457 static int cln_filecnt; /* number of files destroyed */
458 static int cln_foundcore; /* found a core file! */
459 static int cln_queuecnt; /* number of queues checked */
460 static int cln_testonly; /* remove-files vs just-print-info */
463 doselect(const struct dirent *d)
465 int c = d->d_name[0];
467 if ((c == 'c' || c == 'd' || c == 'r' || c == 't') &&
471 if (!strcmp(d->d_name, "core"))
475 if (!strncmp(d->d_name, "errs.", 5))
482 * Comparison routine that clean_q() uses for scandir.
484 * The purpose of this sort is to have all `df' files end up immediately
485 * after the matching `cf' file. For files matching `cf', `df', `rf', or
486 * `tf', it sorts by job number and machine, then by `cf', `df', `rf', or
487 * `tf', and then by the sequence letter (which is A-Z, or a-z). This
488 * routine may also see filenames which do not start with `cf', `df', `rf',
489 * or `tf' (such as `errs.*'), and those are simply sorted by the full
493 * This assumes that all control files start with `cfA*', and it turns
494 * out there are a few implementations of lpr which will create `cfB*'
495 * filenames (they will have datafile names which start with `dfB*').
498 sortq(const struct dirent **a, const struct dirent **b)
500 const int a_lt_b = -1, a_gt_b = 1, cat_other = 10;
501 const char *fname_a, *fname_b, *jnum_a, *jnum_b;
502 int cat_a, cat_b, ch, res, seq_a, seq_b;
504 fname_a = (*a)->d_name;
505 fname_b = (*b)->d_name;
508 * First separate filenames into categories. Categories are
509 * legitimate `cf', `df', `rf' & `tf' filenames, and "other" - in
510 * that order. It is critical that the mapping be exactly the
511 * same for 'a' vs 'b', so define a macro for the job.
513 * [aside: the standard `cf' file has the jobnumber start in
514 * position 4, but some implementations have that as an extra
515 * file-sequence letter, and start the job number in position 5.]
517 #define MAP_TO_CAT(fname_X,cat_X,jnum_X,seq_X) do { \
519 ch = *(fname_X + 2); \
520 jnum_X = fname_X + 3; \
522 if ((*(fname_X + 1) == 'f') && (isalpha(ch))) { \
524 if (*fname_X == 'c') \
526 else if (*fname_X == 'd') \
528 else if (*fname_X == 'r') \
530 else if (*fname_X == 't') \
532 if (cat_X != cat_other) { \
534 if (!isdigit(ch)) { \
538 seq_X = (seq_X << 8) + ch; \
547 MAP_TO_CAT(fname_a, cat_a, jnum_a, seq_a);
548 MAP_TO_CAT(fname_b, cat_b, jnum_b, seq_b);
552 /* First handle all cases which have "other" files */
553 if ((cat_a >= cat_other) || (cat_b >= cat_other)) {
554 /* for two "other" files, just compare the full name */
556 res = strcmp(fname_a, fname_b);
557 else if (cat_a < cat_b)
565 * At this point, we know both files are legitimate `cf', `df', `rf',
566 * or `tf' files. Compare them by job-number and machine name.
568 res = strcmp(jnum_a, jnum_b);
573 * We have two files which belong to the same job. Sort based
574 * on the category of file (`c' before `d', etc).
579 } else if (cat_a > cat_b) {
585 * Two files in the same category for a single job. Sort based
586 * on the sequence letter(s). (usually `A' through `Z', etc).
591 } else if (seq_a > seq_b) {
597 * Given that the filenames in a directory are unique, this SHOULD
598 * never happen (unless there are logic errors in this routine).
599 * But if it does happen, we must return "is equal" or the caller
600 * might see inconsistent results in the sorting order, and that
601 * can trigger other problems.
603 printf("\t*** Error in sortq: %s == %s !\n", fname_a, fname_b);
604 printf("\t*** cat %d == %d ; seq = %d %d\n", cat_a, cat_b,
613 * Remove all spool files and temporaries from the spooling area.
615 * Remove incomplete jobs from spooling area.
619 clean_gi(int argc, char *argv[])
622 /* init some fields before 'clean' is called for each queue */
624 cln_now = time(NULL);
625 cln_minage = 3600.0; /* only delete files >1h old */
630 generic_wrapup = &wrapup_clean;
632 /* see if there are any options specified before the ptr list */
633 for (; argc > 0; argc--, argv++) {
636 if (strcmp(*argv, "-d") == 0) {
637 /* just an example of an option... */
639 *argv = generic_nullarg; /* "erase" it */
641 printf("Invalid option '%s'\n", *argv);
650 tclean_gi(int argc, char *argv[])
653 /* only difference between 'clean' and 'tclean' is one value */
654 /* (...and the fact that 'clean' is priv and 'tclean' is not) */
655 clean_gi(argc, argv);
662 clean_q(struct printer *pp)
665 struct dirent **queue;
667 int didhead, i, n, nitems, rmcp;
672 if (generic_qselect == QSEL_BYNAME) {
673 printf("%s:\n", pp->printer);
679 while (lp < &line[sizeof(line) - 1]) {
680 if ((*lp++ = *cp++) == 0)
684 linerem = sizeof(line) - (lp - line);
688 nitems = scandir(pp->spool_dir, &queue, doselect, sortq);
692 printf("%s:\n", pp->printer);
695 printf("\tcannot examine spool directory\n");
700 printf("%s:\n", pp->printer);
703 printf("\t** found a core file in %s !\n", pp->spool_dir);
708 printf("%s:\n", pp->printer);
710 printf("\t** ----- Sorted list of files being checked:\n");
713 cp = queue[i]->d_name;
714 printf("\t** [%3d] = %s\n", i, cp);
715 } while (++i < nitems);
716 printf("\t** ----- end of sorted list\n");
720 cp = queue[i]->d_name;
724 * A control file. Look for matching data-files.
727 * Note the logic here assumes that the hostname
728 * part of cf-filenames match the hostname part
729 * in df-filenames, and that is not necessarily
730 * true (eg: for multi-homed hosts). This needs
731 * some further thought...
734 while (i + 1 < nitems) {
735 cp1 = queue[i + 1]->d_name;
736 if (*cp1 != 'd' || strcmp(cp + 3, cp1 + 3))
744 } else if (*cp == 'e') {
746 * Must be an errrs or email temp file.
751 * Must be a df with no cf (otherwise, it would have
752 * been skipped above) or an rf or tf file (which can
753 * always be removed if it is old enough).
758 if (strlen(cp) >= linerem) {
759 printf("\t** internal error: 'line' overflow!\n");
760 printf("\t** spooldir = %s\n", pp->spool_dir);
761 printf("\t** cp = %s\n", cp);
764 strlcpy(lp, cp, linerem);
767 } while (++i < nitems);
771 wrapup_clean(int laststatus __unused)
774 printf("Checked %d queues, and ", cln_queuecnt);
775 if (cln_filecnt < 1) {
776 printf("no cruft was found\n");
780 printf("would have ");
782 printf("removed %d files (%ld bytes).\n", cln_filecnt, cln_sizecnt);
789 double agemod, agestat;
791 char linkbuf[BUFSIZ];
794 * We have to use lstat() instead of stat(), in case this is a df*
795 * "file" which is really a symlink due to 'lpr -s' processing. In
796 * that case, we need to check the last-mod time of the symlink, and
797 * not the file that the symlink is pointed at.
800 res = lstat(name, &stbuf);
803 printf("\terror return from stat(%s):\n", name);
804 printf("\t %s\n", strerror(errno));
808 agemod = difftime(cln_now, stbuf.st_mtime);
809 agestat = difftime(cln_now, stbuf.st_ctime);
811 /* this debugging-aid probably is not needed any more... */
812 printf("\t\t modify age=%g secs, stat age=%g secs\n",
815 if ((agemod <= cln_minage) && (agestat <= cln_minage))
819 * if this file is a symlink, then find out the target of the
820 * symlink before unlink-ing the file itself
822 if (S_ISLNK(stbuf.st_mode)) {
824 res = readlink(name, linkbuf, sizeof(linkbuf));
827 printf("\terror return from readlink(%s):\n", name);
828 printf("\t %s\n", strerror(errno));
831 if (res == sizeof(linkbuf))
837 cln_sizecnt += stbuf.st_size;
840 printf("\twould remove %s\n", name);
841 if (S_ISLNK(stbuf.st_mode)) {
842 printf("\t (which is a symlink to %s)\n", linkbuf);
849 printf("\tcannot remove %s (!)\n", name);
851 printf("\tremoved %s\n", name);
853 * Note that for a df* file, this code should also check to see
854 * if it is a symlink to some other file, and if the original
855 * lpr command included '-r' ("remove file"). Of course, this
856 * code would not be removing the df* file unless there was no
857 * matching cf* file, and without the cf* file it is currently
858 * impossible to determine if '-r' had been specified...
860 * As a result of this quandry, we may be leaving behind a
861 * user's file that was supposed to have been removed after
862 * being printed. This may effect services such as CAP or
863 * samba, if they were configured to use 'lpr -r', and if
864 * datafiles are not being properly removed.
866 if (S_ISLNK(stbuf.st_mode)) {
867 printf("\t (which was a symlink to %s)\n", linkbuf);
873 * Enable queuing to the printer (allow lpr to add new jobs to the queue).
876 enable_q(struct printer *pp)
881 lock_file_name(pp, lf, sizeof lf);
882 printf("%s:\n", pp->printer);
884 setres = set_qstate(SQS_ENABLEQ, lf);
891 disable_q(struct printer *pp)
896 lock_file_name(pp, lf, sizeof lf);
897 printf("%s:\n", pp->printer);
899 setres = set_qstate(SQS_DISABLEQ, lf);
903 * Disable queuing and printing and put a message into the status file
904 * (reason for being down). If the user specified `-msg', then use
905 * everything after that as the message for the status file. If the
906 * user did NOT specify `-msg', then the command should take the first
907 * parameter as the printer name, and all remaining parameters as the
908 * message for the status file. (This is to be compatible with the
909 * original definition of 'down', which was implemented long before
910 * `-msg' was around).
913 down_gi(int argc, char *argv[])
916 /* If `-msg' was specified, then this routine has nothing to do. */
917 if (generic_msg != NULL)
921 * If the user only gave one parameter, then use a default msg.
922 * (if argc == 1 at this point, then *argv == name of printer).
925 generic_msg = strdup("printing disabled\n");
930 * The user specified multiple parameters, and did not specify
931 * `-msg'. Build a message from all the parameters after the
932 * first one (and nullify those parameters so generic-processing
933 * will not process them as printer-queue names).
937 generic_msg = args2line(argc, argv);
938 for (; argc > 0; argc--, argv++)
939 *argv = generic_nullarg; /* "erase" it */
943 down_q(struct printer *pp)
948 lock_file_name(pp, lf, sizeof lf);
949 printf("%s:\n", pp->printer);
951 setres = set_qstate(SQS_DISABLEQ+SQS_STOPP, lf);
953 upstat(pp, generic_msg, 1);
960 quit(int argc __unused, char *argv[] __unused)
966 * Kill and restart the daemon.
969 restart_q(struct printer *pp)
971 int killres, setres, startok;
974 lock_file_name(pp, lf, sizeof lf);
975 printf("%s:\n", pp->printer);
977 killres = kill_qtask(lf);
980 * XXX - if the kill worked, we should probably sleep for
981 * a second or so before trying to restart the queue.
984 /* make sure the queue is set to print jobs */
985 setres = set_qstate(SQS_STARTP, lf);
988 startok = startdaemon(pp);
991 printf("\tcouldn't restart daemon\n");
993 printf("\tdaemon restarted\n");
997 * Set the status message of each queue listed. Requires a "-msg"
998 * parameter to indicate the end of the queue list and start of msg text.
1001 setstatus_gi(int argc __unused, char *argv[] __unused)
1004 if (generic_msg == NULL) {
1005 printf("You must specify '-msg' before the text of the new status message.\n");
1006 generic_initerr = 1;
1011 setstatus_q(struct printer *pp)
1015 char lf[MAXPATHLEN];
1017 lock_file_name(pp, lf, sizeof lf);
1018 printf("%s:\n", pp->printer);
1020 upstat(pp, generic_msg, 1);
1023 * Warn the user if 'lpq' will not display this new status-message.
1024 * Note that if lock file does not exist, then the queue is enabled
1025 * for both queuing and printing.
1028 if (stat(lf, &stbuf) >= 0) {
1029 if (stbuf.st_mode & LFM_PRINT_DIS)
1033 printf("\tnote: This queue currently has printing enabled,\n");
1034 printf("\t so this -msg will only be shown by 'lpq' if\n");
1035 printf("\t a job is actively printing on it.\n");
1040 * Enable printing on the specified printer and startup the daemon.
1043 start_q(struct printer *pp)
1045 int setres, startok;
1046 char lf[MAXPATHLEN];
1048 lock_file_name(pp, lf, sizeof lf);
1049 printf("%s:\n", pp->printer);
1051 setres = set_qstate(SQS_STARTP, lf);
1054 startok = startdaemon(pp);
1057 printf("\tcouldn't start daemon\n");
1059 printf("\tdaemon started\n");
1064 * Print the status of the printer queue.
1067 status(struct printer *pp)
1071 register struct dirent *dp;
1073 char file[MAXPATHLEN];
1075 printf("%s:\n", pp->printer);
1076 lock_file_name(pp, file, sizeof file);
1077 if (stat(file, &stbuf) >= 0) {
1078 printf("\tqueuing is %s\n",
1079 ((stbuf.st_mode & LFM_QUEUE_DIS) ? "disabled"
1081 printf("\tprinting is %s\n",
1082 ((stbuf.st_mode & LFM_PRINT_DIS) ? "disabled"
1085 printf("\tqueuing is enabled\n");
1086 printf("\tprinting is enabled\n");
1088 if ((dirp = opendir(pp->spool_dir)) == NULL) {
1089 printf("\tcannot examine spool directory\n");
1093 while ((dp = readdir(dirp)) != NULL) {
1094 if (*dp->d_name == 'c' && dp->d_name[1] == 'f')
1099 printf("\tno entries in spool area\n");
1101 printf("\t1 entry in spool area\n");
1103 printf("\t%d entries in spool area\n", i);
1104 fd = open(file, O_RDONLY);
1105 if (fd < 0 || flock(fd, LOCK_SH|LOCK_NB) == 0) {
1106 (void) close(fd); /* unlocks as well */
1107 printf("\tprinter idle\n");
1111 /* print out the contents of the status file, if it exists */
1112 status_file_name(pp, file, sizeof file);
1113 fd = open(file, O_RDONLY|O_SHLOCK);
1115 (void) fstat(fd, &stbuf);
1116 if (stbuf.st_size > 0) {
1118 while ((i = read(fd, line, sizeof(line))) > 0)
1119 (void) fwrite(line, 1, i, stdout);
1121 (void) close(fd); /* unlocks as well */
1126 * Stop the specified daemon after completing the current job and disable
1130 stop_q(struct printer *pp)
1133 char lf[MAXPATHLEN];
1135 lock_file_name(pp, lf, sizeof lf);
1136 printf("%s:\n", pp->printer);
1138 setres = set_qstate(SQS_STOPP, lf);
1141 upstat(pp, "printing disabled\n", 0);
1144 struct jobqueue **queue;
1149 * Put the specified jobs at the top of printer queue.
1152 topq(int argc, char *argv[])
1156 int cmdstatus, changed;
1157 struct printer myprinter, *pp = &myprinter;
1160 printf("usage: topq printer [jobnum ...] [user ...]\n");
1167 cmdstatus = getprintcap(*argv, pp);
1170 fatal(pp, "%s", pcaperr(cmdstatus));
1171 case PCAPERR_NOTFOUND:
1172 printf("unknown printer %s\n", *argv);
1174 case PCAPERR_TCOPEN:
1175 printf("warning: %s: unresolved tc= reference(s)", *argv);
1177 case PCAPERR_SUCCESS:
1180 printf("%s:\n", pp->printer);
1183 if (chdir(pp->spool_dir) < 0) {
1184 printf("\tcannot chdir to %s\n", pp->spool_dir);
1188 nitems = getq(pp, &queue);
1192 mtime = queue[0]->job_time;
1193 for (i = argc; --i; ) {
1194 if (doarg(argv[i]) == 0) {
1195 printf("\tjob %s is not in the queue\n", argv[i]);
1200 for (i = 0; i < nitems; i++)
1204 printf("\tqueue order unchanged\n");
1208 * Turn on the public execute bit of the lock file to
1209 * get lpd to rebuild the queue after the current job.
1212 if (changed && stat(pp->lock_file, &stbuf) >= 0)
1213 (void) chmod(pp->lock_file, stbuf.st_mode | LFM_RESET_QUE);
1220 * Reposition the job by changing the modification time of
1224 touch(struct jobqueue *jq)
1226 struct timeval tvp[2];
1229 tvp[0].tv_sec = tvp[1].tv_sec = --mtime;
1230 tvp[0].tv_usec = tvp[1].tv_usec = 0;
1232 ret = utimes(jq->job_cfname, tvp);
1238 * Checks if specified job name is in the printer's queue.
1239 * Returns: negative (-1) if argument name is not in the queue.
1244 register struct jobqueue **qq;
1245 register int jobnum, n;
1246 register char *cp, *machine;
1251 * Look for a job item consisting of system name, colon, number
1252 * (example: ucbarpa:114)
1254 if ((cp = strchr(job, ':')) != NULL) {
1262 * Check for job specified by number (example: 112 or 235ucbarpa).
1264 if (isdigit(*job)) {
1267 jobnum = jobnum * 10 + (*job++ - '0');
1268 while (isdigit(*job));
1269 for (qq = queue + nitems; --qq >= queue; ) {
1271 for (cp = (*qq)->job_cfname+3; isdigit(*cp); )
1272 n = n * 10 + (*cp++ - '0');
1275 if (*job && strcmp(job, cp) != 0)
1277 if (machine != NULL && strcmp(machine, cp) != 0)
1279 if (touch(*qq) == 0) {
1280 printf("\tmoved %s\n", (*qq)->job_cfname);
1287 * Process item consisting of owner's name (example: henry).
1289 for (qq = queue + nitems; --qq >= queue; ) {
1291 fp = fopen((*qq)->job_cfname, "r");
1295 while (getline(fp) > 0)
1299 if (line[0] != 'P' || strcmp(job, line+1) != 0)
1301 if (touch(*qq) == 0) {
1302 printf("\tmoved %s\n", (*qq)->job_cfname);
1310 * Enable both queuing & printing, and start printer (undo `down').
1313 up_q(struct printer *pp)
1315 int setres, startok;
1316 char lf[MAXPATHLEN];
1318 lock_file_name(pp, lf, sizeof lf);
1319 printf("%s:\n", pp->printer);
1321 setres = set_qstate(SQS_ENABLEQ+SQS_STARTP, lf);
1324 startok = startdaemon(pp);
1327 printf("\tcouldn't start daemon\n");
1329 printf("\tdaemon started\n");