1
/* $OpenBSD: cmds.c,v 1.9 1997/07/25 18:57:24 grr Exp $ */
4
* Copyright (c) 1983, 1993
5
* The Regents of the University of California. All rights reserved.
8
* Redistribution and use in source and binary forms, with or without
9
* modification, are permitted provided that the following conditions
11
* 1. Redistributions of source code must retain the above copyright
12
* notice, this list of conditions and the following disclaimer.
13
* 2. Redistributions in binary form must reproduce the above copyright
14
* notice, this list of conditions and the following disclaimer in the
15
* documentation and/or other materials provided with the distribution.
16
* 3. All advertising materials mentioning features or use of this software
17
* must display the following acknowledgement:
18
* This product includes software developed by the University of
19
* California, Berkeley and its contributors.
20
* 4. Neither the name of the University nor the names of its contributors
21
* may be used to endorse or promote products derived from this software
22
* without specific prior written permission.
24
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38
static char copyright[] =
39
"@(#) Copyright (c) 1983, 1993\n\
40
The Regents of the University of California. All rights reserved.\n";
45
static char sccsid[] = "@(#)cmds.c 8.2 (Berkeley) 4/28/95";
47
static char rcsid[] = "$OpenBSD: cmds.c,v 1.9 1997/07/25 18:57:24 grr Exp $";
52
* lpc -- line printer control program -- commands:
55
#include <sys/param.h>
73
#include "pathnames.h"
75
extern uid_t uid, euid;
77
static void abortpr __P((int));
78
static void cleanpr __P((void));
79
static void disablepr __P((void));
80
static int doarg __P((char *));
81
static int doselect __P((struct dirent *));
82
static void enablepr __P((void));
83
static void prstat __P((void));
84
static void putmsg __P((int, char **));
85
static int sortq __P((const void *, const void *));
86
static void startpr __P((int));
87
static void stoppr __P((void));
88
static int touch __P((struct queue *));
89
static void unlinkf __P((char *));
90
static void upstat __P((char *));
93
* kill an existing daemon and disable printing.
100
register int c, status;
101
register char *cp1, *cp2;
105
printf("Usage: abort {all | printer ...}\n");
108
if (argc == 2 && !strcmp(argv[1], "all")) {
110
while (cgetnext(&bp, printcapdb) > 0) {
113
while ((c = *cp2++) && c != '|' && c != ':' &&
114
(cp1 - prbuf) < sizeof(prbuf))
123
if ((status = cgetent(&bp, printcapdb, printer)) == -2) {
124
printf("cannot open printer description file\n");
126
} else if (status == -1) {
127
printf("unknown printer %s\n", printer);
129
} else if (status == -3)
130
fatal("potential reference loop detected in printcap file");
143
if (cgetstr(bp, "sd", &SD) == -1)
145
if (cgetstr(bp, "lo", &LO) == -1)
147
(void) snprintf(line, sizeof(line), "%s/%s", SD, LO);
148
printf("%s:\n", printer);
151
* Turn on the owner execute bit of the lock file to disable printing.
155
if (stat(line, &stbuf) >= 0) {
156
if (chmod(line, (stbuf.st_mode & 0777) | 0100) < 0)
157
printf("\tcannot disable printing\n");
159
upstat("printing disabled\n");
160
printf("\tprinting disabled\n");
162
} else if (errno == ENOENT) {
163
if ((fd = open(line, O_WRONLY|O_CREAT, 0760)) < 0)
164
printf("\tcannot create lock file\n");
167
upstat("printing disabled\n");
168
printf("\tprinting disabled\n");
169
printf("\tno daemon to abort\n");
173
printf("\tcannot stat lock file\n");
178
* Kill the current daemon to stop printing now.
180
if ((fp = fopen(line, "r")) == NULL) {
181
printf("\tcannot open lock file\n");
184
if (!getline(fp) || flock(fileno(fp), LOCK_SH|LOCK_NB) == 0) {
185
(void) fclose(fp); /* unlocks as well */
186
printf("\tno daemon to abort\n");
190
if (kill(pid = atoi(line), SIGTERM) < 0) {
192
printf("\tno daemon to abort\n");
194
printf("\tWarning: daemon (pid %d) not killed\n", pid);
196
printf("\tdaemon (pid %d) killed\n", pid);
202
* Write a message into the status file.
209
char statfile[MAXPATHLEN];
211
if (cgetstr(bp, "st", &ST) == -1)
213
(void) snprintf(statfile, sizeof(statfile), "%s/%s", SD, ST);
215
fd = open(statfile, O_WRONLY|O_CREAT, 0664);
216
if (fd < 0 || flock(fd, LOCK_EX) < 0) {
217
printf("\tcannot create status file\n");
220
(void) ftruncate(fd, 0);
221
if (msg == (char *)NULL)
222
(void) write(fd, "\n", 1);
224
(void) write(fd, msg, strlen(msg));
229
* Remove all spool files and temporaries from the spooling area.
236
register int c, status;
237
register char *cp1, *cp2;
241
printf("Usage: clean {all | printer ...}\n");
244
if (argc == 2 && !strcmp(argv[1], "all")) {
246
while (cgetnext(&bp, printcapdb) > 0) {
249
while ((c = *cp2++) && c != '|' && c != ':' &&
250
(cp1 - prbuf) < sizeof(prbuf))
259
if ((status = cgetent(&bp, printcapdb, printer)) == -2) {
260
printf("cannot open printer description file\n");
262
} else if (status == -1) {
263
printf("unknown printer %s\n", printer);
265
} else if (status == -3)
266
fatal("potential reference loop detected in printcap file");
276
int c = d->d_name[0];
278
if ((c == 't' || c == 'c' || c == 'd') && d->d_name[1] == 'f')
284
* Comparison routine for scandir. Sort by job number and machine, then
285
* by `cf', `tf', or `df', then by the sequence letter A-Z, a-z.
291
struct dirent **d1, **d2;
294
d1 = (struct dirent **)a;
295
d2 = (struct dirent **)b;
296
if ((c1 = strcmp((*d1)->d_name + 3, (*d2)->d_name + 3)))
298
c1 = (*d1)->d_name[0];
299
c2 = (*d2)->d_name[0];
301
return((*d1)->d_name[2] - (*d2)->d_name[2]);
304
if (c1 == 'd' || c2 == 'c')
310
* Remove incomplete jobs from spooling area.
316
register char *cp, *cp1, *lp;
317
struct dirent **queue;
320
if (cgetstr(bp, "sd", &SD) == -1)
322
printf("%s:\n", printer);
324
for (lp = line, cp = SD; (lp - line) < sizeof(line) && (*lp++ = *cp++);)
329
nitems = scandir(SD, &queue, doselect, sortq);
332
printf("\tcannot examine spool directory\n");
339
cp = queue[i]->d_name;
342
while (i + 1 < nitems) {
343
cp1 = queue[i + 1]->d_name;
344
if (*cp1 != 'd' || strcmp(cp + 3, cp1 + 3))
350
strncpy(lp, cp, sizeof(line) - strlen(line) - 1);
351
line[sizeof(line) - 1] = '\0';
356
* Must be a df with no cf (otherwise, it would have
357
* been skipped above) or a tf file (which can always
360
strncpy(lp, cp, sizeof(line) - strlen(line) - 1);
361
line[sizeof(line) - 1] = '\0';
364
} while (++i < nitems);
372
if (unlink(name) < 0)
373
printf("\tcannot remove %s\n", name);
375
printf("\tremoved %s\n", name);
380
* Enable queuing to the printer (allow lpr's).
387
register int c, status;
388
register char *cp1, *cp2;
392
printf("Usage: enable {all | printer ...}\n");
395
if (argc == 2 && !strcmp(argv[1], "all")) {
397
while (cgetnext(&bp, printcapdb) > 0) {
400
while ((c = *cp2++) && c != '|' && c != ':' &&
401
(cp1 - prbuf) < sizeof(prbuf))
410
if ((status = cgetent(&bp, printcapdb, printer)) == -2) {
411
printf("cannot open printer description file\n");
413
} else if (status == -1) {
414
printf("unknown printer %s\n", printer);
416
} else if (status == -3)
417
fatal("potential reference loop detected in printcap file");
428
if (cgetstr(bp, "sd", &SD) == -1)
430
if (cgetstr(bp, "lo", &LO) == -1)
432
(void) snprintf(line, sizeof(line), "%s/%s", SD, LO);
433
printf("%s:\n", printer);
436
* Turn off the group execute bit of the lock file to enable queuing.
439
if (stat(line, &stbuf) >= 0) {
440
if (chmod(line, stbuf.st_mode & 0767) < 0)
441
printf("\tcannot enable queuing\n");
443
printf("\tqueuing enabled\n");
456
register int c, status;
457
register char *cp1, *cp2;
461
printf("Usage: disable {all | printer ...}\n");
464
if (argc == 2 && !strcmp(argv[1], "all")) {
466
while (cgetnext(&bp, printcapdb) > 0) {
469
while ((c = *cp2++) && c != '|' && c != ':' &&
470
(cp1 - prbuf) < sizeof(prbuf))
479
if ((status = cgetent(&bp, printcapdb, printer)) == -2) {
480
printf("cannot open printer description file\n");
482
} else if (status == -1) {
483
printf("unknown printer %s\n", printer);
485
} else if (status == -3)
486
fatal("potential reference loop detected in printcap file");
498
if (cgetstr(bp, "sd", &SD) == -1)
500
if (cgetstr(bp, "lo", &LO) == -1)
502
(void) snprintf(line, sizeof(line), "%s/%s", SD, LO);
503
printf("%s:\n", printer);
505
* Turn on the group execute bit of the lock file to disable queuing.
508
if (stat(line, &stbuf) >= 0) {
509
if (chmod(line, (stbuf.st_mode & 0777) | 010) < 0)
510
printf("\tcannot disable queuing\n");
512
printf("\tqueuing disabled\n");
513
} else if (errno == ENOENT) {
514
if ((fd = open(line, O_WRONLY|O_CREAT, 0670)) < 0)
515
printf("\tcannot create lock file\n");
518
printf("\tqueuing disabled\n");
521
printf("\tcannot stat lock file\n");
526
* Disable queuing and printing and put a message into the status file
527
* (reason for being down).
534
register int c, status;
535
register char *cp1, *cp2;
539
printf("Usage: down {all | printer} [message ...]\n");
542
if (!strcmp(argv[1], "all")) {
544
while (cgetnext(&bp, printcapdb) > 0) {
547
while ((c = *cp2++) && c != '|' && c != ':' &&
548
(cp1 - prbuf) < sizeof(prbuf))
551
putmsg(argc - 2, argv + 2);
556
if ((status = cgetent(&bp, printcapdb, printer)) == -2) {
557
printf("cannot open printer description file\n");
559
} else if (status == -1) {
560
printf("unknown printer %s\n", printer);
562
} else if (status == -3)
563
fatal("potential reference loop detected in printcap file");
565
putmsg(argc - 2, argv + 2);
574
register char *cp1, *cp2;
578
if (cgetstr(bp, "sd", &SD) == -1)
580
if (cgetstr(bp, "lo", &LO) == -1)
582
if (cgetstr(bp, "st", &ST) == -1)
584
printf("%s:\n", printer);
586
* Turn on the group execute bit of the lock file to disable queuing and
587
* turn on the owner execute bit of the lock file to disable printing.
589
(void) snprintf(line, sizeof(line), "%s/%s", SD, LO);
591
if (stat(line, &stbuf) >= 0) {
592
if (chmod(line, (stbuf.st_mode & 0777) | 0110) < 0)
593
printf("\tcannot disable queuing\n");
595
printf("\tprinter and queuing disabled\n");
596
} else if (errno == ENOENT) {
597
if ((fd = open(line, O_WRONLY|O_CREAT, 0770)) < 0)
598
printf("\tcannot create lock file\n");
601
printf("\tprinter and queuing disabled\n");
606
printf("\tcannot stat lock file\n");
608
* Write the message into the status file.
610
(void) snprintf(line, sizeof(line), "%s/%s", SD, ST);
611
fd = open(line, O_WRONLY|O_CREAT, 0664);
612
if (fd < 0 || flock(fd, LOCK_EX) < 0) {
613
printf("\tcannot create status file\n");
618
(void) ftruncate(fd, 0);
620
(void) write(fd, "\n", 1);
625
while (--argc >= 0) {
627
while ((cp1 - buf) < sizeof(buf) && (*cp1++ = *cp2++))
633
(void) write(fd, buf, strlen(buf));
649
* Kill and restart the daemon.
656
register int c, status;
657
register char *cp1, *cp2;
661
printf("Usage: restart {all | printer ...}\n");
664
if (argc == 2 && !strcmp(argv[1], "all")) {
666
while (cgetnext(&bp, printcapdb) > 0) {
669
while ((c = *cp2++) && c != '|' && c != ':' &&
670
(cp1 - prbuf) < sizeof(prbuf))
680
if ((status = cgetent(&bp, printcapdb, printer)) == -2) {
681
printf("cannot open printer description file\n");
683
} else if (status == -1) {
684
printf("unknown printer %s\n", printer);
686
} else if (status == -3)
687
fatal("potential reference loop detected in printcap file");
695
* Enable printing on the specified printer and startup the daemon.
702
register int c, status;
703
register char *cp1, *cp2;
707
printf("Usage: start {all | printer ...}\n");
710
if (argc == 2 && !strcmp(argv[1], "all")) {
712
while (cgetnext(&bp, printcapdb) > 0) {
715
while ((c = *cp2++) && c != '|' && c != ':' &&
716
(cp1 - prbuf) < sizeof(prbuf))
725
if ((status = cgetent(&bp, printcapdb, printer)) == -2) {
726
printf("cannot open printer description file\n");
728
} else if (status == -1) {
729
printf("unknown printer %s\n", printer);
731
} else if (status == -3)
732
fatal("potential reference loop detected in printcap file");
744
if (cgetstr(bp, "sd", &SD) == -1)
746
if (cgetstr(bp, "lo", &LO) == -1)
748
(void) snprintf(line, sizeof(line), "%s/%s", SD, LO);
749
printf("%s:\n", printer);
752
* Turn off the owner execute bit of the lock file to enable printing.
755
if (enable && stat(line, &stbuf) >= 0) {
756
if (chmod(line, stbuf.st_mode & (enable==2 ? 0666 : 0677)) < 0)
757
printf("\tcannot enable printing\n");
759
printf("\tprinting enabled\n");
761
if (!startdaemon(printer))
762
printf("\tcouldn't start daemon\n");
764
printf("\tdaemon started\n");
769
* Print the status of each queue listed or all the queues.
776
register int c, status;
777
register char *cp1, *cp2;
780
if (argc == 1 || argc == 2 && !strcmp(argv[1], "all")) {
782
while (cgetnext(&bp, printcapdb) > 0) {
785
while ((c = *cp2++) && c != '|' && c != ':' &&
786
(cp1 - prbuf) < sizeof(prbuf))
795
if ((status = cgetent(&bp, printcapdb, printer)) == -2) {
796
printf("cannot open printer description file\n");
798
} else if (status == -1) {
799
printf("unknown printer %s\n", printer);
801
} else if (status == -3)
802
fatal("potential reference loop detected in printcap file");
809
* Print the status of the printer queue.
816
register struct dirent *dp;
819
if (cgetstr(bp, "sd", &SD) == -1)
821
if (cgetstr(bp, "lo", &LO) == -1)
823
if (cgetstr(bp, "st", &ST) == -1)
825
printf("%s:\n", printer);
826
(void) snprintf(line, sizeof(line), "%s/%s", SD, LO);
827
if (stat(line, &stbuf) >= 0) {
828
printf("\tqueuing is %s\n",
829
(stbuf.st_mode & 010) ? "disabled" : "enabled");
830
printf("\tprinting is %s\n",
831
(stbuf.st_mode & 0100) ? "disabled" : "enabled");
833
printf("\tqueuing is enabled\n");
834
printf("\tprinting is enabled\n");
836
if ((dirp = opendir(SD)) == NULL) {
837
printf("\tcannot examine spool directory\n");
841
while ((dp = readdir(dirp)) != NULL) {
842
if (*dp->d_name == 'c' && dp->d_name[1] == 'f')
847
printf("\tno entries\n");
849
printf("\t1 entry in spool area\n");
851
printf("\t%d entries in spool area\n", i);
852
fd = open(line, O_RDONLY);
853
if (fd < 0 || flock(fd, LOCK_SH|LOCK_NB) == 0) {
854
(void) close(fd); /* unlocks as well */
855
printf("\tprinter idle\n");
860
(void) snprintf(line, sizeof(line), "%s/%s", SD, ST);
861
fd = open(line, O_RDONLY);
863
(void) flock(fd, LOCK_SH);
864
while ((i = read(fd, line, sizeof(line))) > 0)
865
(void) fwrite(line, 1, i, stdout);
866
(void) close(fd); /* unlocks as well */
871
* Stop the specified daemon after completing the current job and disable
879
register int c, status;
880
register char *cp1, *cp2;
884
printf("Usage: stop {all | printer ...}\n");
887
if (argc == 2 && !strcmp(argv[1], "all")) {
889
while (cgetnext(&bp, printcapdb) > 0) {
892
while ((c = *cp2++) && c != '|' && c != ':' &&
893
(cp1 - prbuf) < sizeof(prbuf))
902
if ((status = cgetent(&bp, printcapdb, printer)) == -2) {
903
printf("cannot open printer description file\n");
905
} else if (status == -1) {
906
printf("unknown printer %s\n", printer);
908
} else if (status == -3)
909
fatal("potential reference loop detected in printcap file");
921
if (cgetstr(bp, "sd", &SD) == -1)
923
if (cgetstr(bp, "lo", &LO) == -1)
925
(void) snprintf(line, sizeof(line), "%s/%s", SD, LO);
926
printf("%s:\n", printer);
929
* Turn on the owner execute bit of the lock file to disable printing.
932
if (stat(line, &stbuf) >= 0) {
933
if (chmod(line, (stbuf.st_mode & 0777) | 0100) < 0)
934
printf("\tcannot disable printing\n");
936
upstat("printing disabled\n");
937
printf("\tprinting disabled\n");
939
} else if (errno == ENOENT) {
940
if ((fd = open(line, O_WRONLY|O_CREAT, 0760)) < 0)
941
printf("\tcannot create lock file\n");
944
upstat("printing disabled\n");
945
printf("\tprinting disabled\n");
948
printf("\tcannot stat lock file\n");
952
struct queue **queue;
957
* Put the specified jobs at the top of printer queue.
969
printf("Usage: topq printer [jobnum ...] [user ...]\n");
975
status = cgetent(&bp, printcapdb, printer);
977
printf("cannot open printer description file\n");
979
} else if (status == -1) {
980
printf("%s: unknown printer\n", printer);
982
} else if (status == -3)
983
fatal("potential reference loop detected in printcap file");
985
if (cgetstr(bp, "sd", &SD) == -1)
987
if (cgetstr(bp, "lo", &LO) == -1)
989
printf("%s:\n", printer);
993
printf("\tcannot chdir to %s\n", SD);
997
nitems = getq(&queue);
1001
mtime = queue[0]->q_time;
1002
for (i = argc; --i; ) {
1003
if (doarg(argv[i]) == 0) {
1004
printf("\tjob %s is not in the queue\n", argv[i]);
1009
for (i = 0; i < nitems; i++)
1013
printf("\tqueue order unchanged\n");
1017
* Turn on the public execute bit of the lock file to
1018
* get lpd to rebuild the queue after the current job.
1021
if (changed && stat(LO, &stbuf) >= 0)
1022
(void) chmod(LO, (stbuf.st_mode & 0777) | 01);
1029
* Reposition the job by changing the modification time of
1036
struct timeval tvp[2];
1039
tvp[0].tv_sec = tvp[1].tv_sec = --mtime;
1040
tvp[0].tv_usec = tvp[1].tv_usec = 0;
1042
ret = utimes(q->q_name, tvp);
1048
* Checks if specified job name is in the printer's queue.
1049
* Returns: negative (-1) if argument name is not in the queue.
1055
register struct queue **qq;
1056
register int jobnum, n;
1057
register char *cp, *machine;
1062
* Look for a job item consisting of system name, colon, number
1063
* (example: ucbarpa:114)
1065
if ((cp = strchr(job, ':')) != NULL) {
1073
* Check for job specified by number (example: 112 or 235ucbarpa).
1075
if (isdigit(*job)) {
1078
jobnum = jobnum * 10 + (*job++ - '0');
1079
while (isdigit(*job));
1080
for (qq = queue + nitems; --qq >= queue; ) {
1082
for (cp = (*qq)->q_name+3; isdigit(*cp); )
1083
n = n * 10 + (*cp++ - '0');
1086
if (*job && strcmp(job, cp) != 0)
1088
if (machine != NULL && strcmp(machine, cp) != 0)
1090
if (touch(*qq) == 0) {
1091
printf("\tmoved %s\n", (*qq)->q_name);
1098
* Process item consisting of owner's name (example: henry).
1100
for (qq = queue + nitems; --qq >= queue; ) {
1102
fp = fopen((*qq)->q_name, "r");
1106
while (getline(fp) > 0)
1110
if (line[0] != 'P' || strcmp(job, line+1) != 0)
1112
if (touch(*qq) == 0) {
1113
printf("\tmoved %s\n", (*qq)->q_name);
1121
* Enable everything and start printer (undo `down').
1128
register int c, status;
1129
register char *cp1, *cp2;
1133
printf("Usage: up {all | printer ...}\n");
1136
if (argc == 2 && !strcmp(argv[1], "all")) {
1138
while (cgetnext(&bp, printcapdb) > 0) {
1141
while ((c = *cp2++) && c != '|' && c != ':' &&
1142
(cp1 - prbuf) < sizeof(prbuf))
1151
if ((status = cgetent(&bp, printcapdb, printer)) == -2) {
1152
printf("cannot open printer description file\n");
1154
} else if (status == -1) {
1155
printf("unknown printer %s\n", printer);
1157
} else if (status == -3)
1158
fatal("potential reference loop detected in printcap file");