aboutsummaryrefslogtreecommitdiff
path: root/usr.sbin/lpr/lpc/movejobs.c
blob: 60c9b38d57089555460805a6173f97311867b73f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*
 * ------+---------+---------+---------+---------+---------+---------+---------*
 * Copyright (c) 2002   - Garance Alistair Drosehn <gad@FreeBSD.org>.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *   1. Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *   2. Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * The views and conclusions contained in the software and documentation
 * are those of the authors and should not be interpreted as representing
 * official policies, either expressed or implied, of the FreeBSD Project
 * or FreeBSD, Inc.
 *
 * ------+---------+---------+---------+---------+---------+---------+---------*
 */

#include "lp.cdefs.h"		/* A cross-platform version of <sys/cdefs.h> */
__FBSDID("$FreeBSD$");

/*
 * movejobs.c - The lpc commands which move jobs around.
 */

#include <sys/file.h>
#include <sys/param.h>
#include <sys/queue.h>
#include <sys/time.h>
#include <sys/stat.h>

#include <ctype.h>
#include <dirent.h>	/* just for MAXNAMLEN, for job_cfname in lp.h! */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "lp.h"
#include "lpc.h"
#include "matchjobs.h"
#include "extern.h"

/* Values for origcmd in tqbq_common() */
#define IS_TOPQ		1
#define IS_BOTQ 	2

static int	 process_jobs(int _argc, char *_argv[], process_jqe
		    _process_rtn, void *myinfo);
static process_jqe touch_jqe;
static void 	 tqbq_common(int _argc, char *_argv[], int _origcmd);

/*
 * isdigit is defined to work on an 'int', in the range 0 to 255, plus EOF.
 * Define a wrapper which can take 'char', either signed or unsigned.
 */
#define isdigitch(Anychar)    isdigit(((int) Anychar) & 255)

struct touchjqe_info {			/* for topq/bottomq */
	time_t	 newtime;
};

static int	 nitems;
static struct jobqueue **queue;

/*
 * Process all the jobs, as specified by the user.
 */
static int
process_jobs(int argc, char *argv[], process_jqe process_rtn, void *myinfo)
{
	struct jobspec_hdr jobs_wanted;
	int i, matchcnt, pjres;

	STAILQ_INIT(&jobs_wanted);
	for (i = 0; i < argc; i++) {
		pjres = parse_jobspec(argv[i], &jobs_wanted);
		if (pjres == 0) {
			printf("\tinvalid job specifier: %s\n", argv[i]);
			continue;
		}
	}
	matchcnt = scanq_jobspec(nitems, queue, SCQ_JSORDER, &jobs_wanted,
	    process_rtn, myinfo);

	free_jobspec(&jobs_wanted);
	return (matchcnt);
}

/*
 * Reposition the job by changing the modification time of the
 * control file.
 */
static int
touch_jqe(void *myinfo, struct jobqueue *jq, struct jobspec *jspec)
{
	struct timeval tvp[2];
	struct touchjqe_info *touch_info;
	int ret;

	/*
	 * If the entire queue has been scanned for the current jobspec,
	 * then let the user know if there were no jobs matched by that
	 * specification.
	 */
	if (jq == NULL) {
		if (jspec->matchcnt == 0) {
			format_jobspec(jspec, FMTJS_VERBOSE);
			if (jspec->pluralfmt)
				printf("\tjobs %s are not in the queue\n",
				    jspec->fmtoutput);
			else
				printf("\tjob %s is not in the queue\n",
				    jspec->fmtoutput);
		}
		return (1);
	}

	/*
	 * Do a little juggling with "matched" vs "processed", so a single
	 * job can be matched by multiple specifications, and yet it will
	 * be moved only once.  This is so, eg, 'topq lp 7 7' will not
	 * complain "job 7 is not in queue" for the second specification.
	 */
	jq->job_matched = 0;
	if (jq->job_processed) {
		printf("\tmoved %s earlier\n", jq->job_cfname);
		return (1);
	}
	jq->job_processed = 1;

	touch_info = myinfo;
	tvp[0].tv_sec = tvp[1].tv_sec = ++touch_info->newtime;
	tvp[0].tv_usec = tvp[1].tv_usec = 0;
	seteuid(euid);
	ret = utimes(jq->job_cfname, tvp);
	seteuid(uid);

	if (ret == 0) {
		if (jspec->matcheduser)
			printf("\tmoved %s  (user %s)\n", jq->job_cfname,
			    jspec->matcheduser);
		else
			printf("\tmoved %s\n", jq->job_cfname);
	}
	return (ret);
}

/*
 * Put the specified jobs at the bottom of printer queue.
 */
void
bottomq_cmd(int argc, char *argv[])
{

	if (argc < 3) {
		printf("usage: bottomq printer [jobspec ...]\n");
		return;
	}
	--argc;			/* First argv was the command name */
	++argv;

	tqbq_common(argc, argv, IS_BOTQ);
}

/*
 * Put the specified jobs at the top of printer queue.
 */
void
topq_cmd(int argc, char *argv[])
{

	if (argc < 3) {
		printf("usage: topq printer [jobspec ...]\n");
		return;
	}
	--argc;			/* First argv was the command name */
	++argv;

	tqbq_common(argc, argv, IS_TOPQ);
} 

/*
 * Processing in common between topq and bottomq commands.
 */
void
tqbq_common(int argc, char *argv[], int origcmd)
{
	struct printer myprinter, *pp;
	struct touchjqe_info touch_info;
	int i, movecnt, setres;

	pp = setup_myprinter(*argv, &myprinter, SUMP_CHDIR_SD);
	if (pp == NULL)
		return;
	--argc;			/* Second argv was the printer name */
	++argv;

	nitems = getq(pp, &queue);
	if (nitems == 0) {
		printf("\tthere are no jobs in the queue\n");
		free_printer(pp);
		return;
	}

	/*
	 * The only real difference between topq and bottomq is the
	 * initial value used for newtime.
	 */
	switch (origcmd) {
	case IS_BOTQ:
		/*
		 * When moving jobs to the bottom of the queue, pick a
		 * starting value which is one second after the last job
		 * in the queue.
		*/
		touch_info.newtime = queue[nitems - 1]->job_time + 1;
		break;
	case IS_TOPQ:
		/*
		 * When moving jobs to the top of the queue, the greatest
		 * number of jobs which could be moved is all the jobs
		 * that are in the queue.  Pick a starting value which
		 * leaves plenty of room for all existing jobs.
		 */
		touch_info.newtime = queue[0]->job_time - nitems - 5;
		break;
	default:
		printf("\ninternal error in topq/bottomq processing.\n");
		return;
	}

	movecnt = process_jobs(argc, argv, touch_jqe, &touch_info);

	/*
	 * If any jobs were moved, then chmod the lock file to notify any
	 * active process for this queue that the queue has changed, so
	 * it will rescan the queue to find out the new job order. 
	 */
	if (movecnt == 0)
		printf("\tqueue order unchanged\n");
	else {
		setres = set_qstate(SQS_QCHANGED, pp->lock_file);
		if (setres < 0)
			printf("\t* queue order changed for %s, but the\n"
			    "\t* attempt to set_qstate() failed [%d]!\n",
			    pp->printer, setres);
	}

	for (i = 0; i < nitems; i++)
		free(queue[i]);
	free(queue);
	free_printer(pp);
}