xref: /dragonfly/usr.sbin/lpr/lpc/movejobs.c (revision 82730a9c)
1 /*
2  * ------+---------+---------+---------+---------+---------+---------+---------*
3  * Copyright (c) 2002   - Garance Alistair Drosehn <gad@FreeBSD.org>.
4  * All rights reserved.
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:
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  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  * The views and conclusions contained in the software and documentation
28  * are those of the authors and should not be interpreted as representing
29  * official policies, either expressed or implied, of the FreeBSD Project
30  * or FreeBSD, Inc.
31  *
32  * ------+---------+---------+---------+---------+---------+---------+---------*
33  *
34  * $FreeBSD: src/usr.sbin/lpr/lpc/movejobs.c,v 1.1.2.1 2002/07/26 03:12:07 gad Exp $
35  * $DragonFly: src/usr.sbin/lpr/lpc/movejobs.c,v 1.3 2005/08/08 18:58:56 joerg Exp $
36  */
37 
38 /*
39  * movejobs.c - The lpc commands which move jobs around.
40  */
41 
42 #include <sys/file.h>
43 #include <sys/param.h>
44 #include <sys/queue.h>
45 #include <sys/time.h>
46 #include <sys/stat.h>
47 
48 #include <ctype.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <unistd.h>
53 #include "lp.h"
54 #include "lpc.h"
55 #include "matchjobs.h"
56 #include "extern.h"
57 
58 /* Values for origcmd in tqbq_common() */
59 #define IS_TOPQ		1
60 #define IS_BOTQ 	2
61 
62 static int	 process_jobs(int _argc, char *_argv[], process_jqe
63 		    _process_rtn, void *myinfo);
64 static process_jqe touch_jqe;
65 static void 	 tqbq_common(int _argc, char *_argv[], int _origcmd);
66 
67 /*
68  * isdigit is defined to work on an 'int', in the range 0 to 255, plus EOF.
69  * Define a wrapper which can take 'char', either signed or unsigned.
70  */
71 #define isdigitch(Anychar)    isdigit(((int) Anychar) & 255)
72 
73 struct touchjqe_info {			/* for topq/bottomq */
74 	time_t	 newtime;
75 };
76 
77 static int	 nitems;
78 static struct jobqueue **queue;
79 
80 /*
81  * Process all the jobs, as specified by the user.
82  */
83 static int
84 process_jobs(int argc, char *argv[], process_jqe process_rtn, void *myinfo)
85 {
86 	struct jobspec_hdr jobs_wanted;
87 	int i, matchcnt, pjres;
88 
89 	STAILQ_INIT(&jobs_wanted);
90 	for (i = 0; i < argc; i++) {
91 		pjres = parse_jobspec(argv[i], &jobs_wanted);
92 		if (pjres == 0) {
93 			printf("\tinvalid job specifier: %s\n", argv[i]);
94 			continue;
95 		}
96 	}
97 	matchcnt = scanq_jobspec(nitems, queue, SCQ_JSORDER, &jobs_wanted,
98 	    process_rtn, myinfo);
99 
100 	free_jobspec(&jobs_wanted);
101 	return (matchcnt);
102 }
103 
104 /*
105  * Reposition the job by changing the modification time of the
106  * control file.
107  */
108 static int
109 touch_jqe(void *myinfo, struct jobqueue *jq, struct jobspec *jspec)
110 {
111 	struct timeval tvp[2];
112 	struct touchjqe_info *touch_info;
113 	int ret;
114 
115 	/*
116 	 * If the entire queue has been scanned for the current jobspec,
117 	 * then let the user know if there were no jobs matched by that
118 	 * specification.
119 	 */
120 	if (jq == NULL) {
121 		if (jspec->matchcnt == 0) {
122 			format_jobspec(jspec, FMTJS_VERBOSE);
123 			if (jspec->pluralfmt)
124 				printf("\tjobs %s are not in the queue\n",
125 				    jspec->fmtoutput);
126 			else
127 				printf("\tjob %s is not in the queue\n",
128 				    jspec->fmtoutput);
129 		}
130 		return (1);
131 	}
132 
133 	/*
134 	 * Do a little juggling with "matched" vs "processed", so a single
135 	 * job can be matched by multiple specifications, and yet it will
136 	 * be moved only once.  This is so, eg, 'topq lp 7 7' will not
137 	 * complain "job 7 is not in queue" for the second specification.
138 	 */
139 	jq->job_matched = 0;
140 	if (jq->job_processed) {
141 		printf("\tmoved %s earlier\n", jq->job_cfname);
142 		return (1);
143 	}
144 	jq->job_processed = 1;
145 
146 	touch_info = myinfo;
147 	tvp[0].tv_sec = tvp[1].tv_sec = ++touch_info->newtime;
148 	tvp[0].tv_usec = tvp[1].tv_usec = 0;
149 	seteuid(euid);
150 	ret = utimes(jq->job_cfname, tvp);
151 	seteuid(uid);
152 
153 	if (ret == 0) {
154 		if (jspec->matcheduser)
155 			printf("\tmoved %s  (user %s)\n", jq->job_cfname,
156 			    jspec->matcheduser);
157 		else
158 			printf("\tmoved %s\n", jq->job_cfname);
159 	}
160 	return (ret);
161 }
162 
163 /*
164  * Put the specified jobs at the bottom of printer queue.
165  */
166 void
167 bottomq_cmd(int argc, char *argv[])
168 {
169 
170 	if (argc < 3) {
171 		printf("usage: bottomq printer [jobspec ...]\n");
172 		return;
173 	}
174 	--argc;			/* First argv was the command name */
175 	++argv;
176 
177 	tqbq_common(argc, argv, IS_BOTQ);
178 }
179 
180 /*
181  * Put the specified jobs at the top of printer queue.
182  */
183 void
184 topq_cmd(int argc, char *argv[])
185 {
186 
187 	if (argc < 3) {
188 		printf("usage: topq printer [jobspec ...]\n");
189 		return;
190 	}
191 	--argc;			/* First argv was the command name */
192 	++argv;
193 
194 	tqbq_common(argc, argv, IS_TOPQ);
195 }
196 
197 /*
198  * Processing in common between topq and bottomq commands.
199  */
200 void
201 tqbq_common(int argc, char *argv[], int origcmd)
202 {
203 	struct printer myprinter, *pp;
204 	struct touchjqe_info touch_info;
205 	int i, movecnt, setres;
206 
207 	pp = setup_myprinter(*argv, &myprinter, SUMP_CHDIR_SD);
208 	if (pp == NULL)
209 		return;
210 	--argc;			/* Second argv was the printer name */
211 	++argv;
212 
213 	nitems = getq(pp, &queue);
214 	if (nitems == 0) {
215 		printf("\tthere are no jobs in the queue\n");
216 		free_printer(pp);
217 		return;
218 	}
219 
220 	/*
221 	 * The only real difference between topq and bottomq is the
222 	 * initial value used for newtime.
223 	 */
224 	switch (origcmd) {
225 	case IS_BOTQ:
226 		/*
227 		 * When moving jobs to the bottom of the queue, pick a
228 		 * starting value which is one second after the last job
229 		 * in the queue.
230 		*/
231 		touch_info.newtime = queue[nitems - 1]->job_time + 1;
232 		break;
233 	case IS_TOPQ:
234 		/*
235 		 * When moving jobs to the top of the queue, the greatest
236 		 * number of jobs which could be moved is all the jobs
237 		 * that are in the queue.  Pick a starting value which
238 		 * leaves plenty of room for all existing jobs.
239 		 */
240 		touch_info.newtime = queue[0]->job_time - nitems - 5;
241 		break;
242 	default:
243 		printf("\ninternal error in topq/bottomq processing.\n");
244 		return;
245 	}
246 
247 	movecnt = process_jobs(argc, argv, touch_jqe, &touch_info);
248 
249 	/*
250 	 * If any jobs were moved, then chmod the lock file to notify any
251 	 * active process for this queue that the queue has changed, so
252 	 * it will rescan the queue to find out the new job order.
253 	 */
254 	if (movecnt == 0)
255 		printf("\tqueue order unchanged\n");
256 	else {
257 		setres = set_qstate(SQS_QCHANGED, pp->lock_file);
258 		if (setres < 0)
259 			printf("\t* queue order changed for %s, but the\n"
260 			    "\t* attempt to set_qstate() failed [%d]!\n",
261 			    pp->printer, setres);
262 	}
263 
264 	for (i = 0; i < nitems; i++)
265 		free(queue[i]);
266 	free(queue);
267 	free_printer(pp);
268 }
269 
270