xref: /illumos-gate/usr/src/cmd/lp/cmd/lpsched/requeue.c (revision 3db86aab)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23 /*	  All Rights Reserved  	*/
24 
25 
26 #ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.6.1.4	*/
27 /* EMACS_MODES: !fill, lnumb, !overwrite, !nodelete, !picture */
28 
29 #include "lpsched.h"
30 #include "validate.h"
31 
32 /*
33  * The routines in this file are used to examine queued requests
34  * to see if something must be done about them. We don't bother
35  * checking requests that are:
36  *
37  *	- printing (we could, to allow the administrator to stop
38  *	  a request by making a configuration change, but that
39  *	  can lead to trouble (yet another way to terminate a child)
40  *	  and the administrator can always disable the request to
41  *	  force it to stop printing and be reevaluated);
42  *
43  *	- changing, since once the change is complete the request
44  *	  will be reevaluated again anyway;
45  *
46  *	- notifying, since the request is essentially finished
47  *
48  *	- being sent or already sent to a remote machine;
49  *
50  *	- done.
51  *
52  * Requests that are being held or are filtering ARE to be considered,
53  * because things may have changed to make them impossible to print.
54  */
55 #define RS_SKIP	((RS_ACTIVE & ~RS_FILTERING) | RS_DONE)
56 #define	SKIP_IT(PRS) ((PRS)->request->outcome & RS_SKIP)
57 
58 /**
59  ** queue_attract() - REASSIGN REQUEST(S) TO PRINTER, IF POSSIBLE
60  **/
61 
62 void
63 queue_attract(PSTATUS *pps, int (*qchk_p)(RSTATUS *), int attract_just_one)
64 {
65 	register RSTATUS	*prs;
66 	register CSTATUS	*pcs;
67 	int			called_schedule	= 0;
68 
69 
70 	/*
71 	 * Evaluate requests that:
72 	 *	- meet a criteria set by a function passed.
73 	 *	- are already queued for the printer
74 	 *	- are destined for a class containing this printer
75 	 *	- or are destined for any printer
76 	 * We stop on the first one that will work on the printer,
77 	 * and schedule an interface for the printer (which will
78 	 * find the first request ready, namely the one we stopped on).
79 	 */
80 
81 #define	SAMECLASS(PRS,PPS) \
82 	( \
83 		((pcs = search_ctable(PRS->request->destination)) != NULL) \
84 	     && searchlist(PPS->printer->name, pcs->class->members) \
85 	)
86 
87 #define ISANY(PRS)	STREQU(PRS->request->destination, NAME_ANY)
88 
89 	for (prs = Request_List; prs; prs = prs->next) {
90 		if (
91 			!SKIP_IT(prs)
92 		     && (!qchk_p || (*qchk_p)(prs))
93 		     && (
94 				prs->printer == pps
95 			     || ISANY(prs)
96 			     || SAMECLASS(prs, pps)
97 			)
98 		)
99 			/*
100 			 * Don't need to evaluate the request if it
101 			 * is already queued!
102 			 */
103 			if (
104 				prs->printer == pps
105 			     || evaluate_request(prs, pps, 0) == MOK
106 			) {
107 				/*
108 				 * This request was attracted to the
109 				 * printer but maybe it now needs to be
110 				 * filtered. If so, filter it but see if
111 				 * there's another request all set to go.
112 				 */
113 				if (NEEDS_FILTERING(prs))
114 					schedule (EV_SLOWF, prs);
115 				else {
116 					if (!called_schedule) {
117 						schedule (EV_INTERF, pps);
118 						called_schedule = 1;
119 					}
120 					if (attract_just_one)
121 						break;
122 				}
123 			}
124 	}
125 
126 	return;
127 }
128 
129 /**
130  ** queue_repel() - REASSIGN REQUESTS TO ANOTHER PRINTER, IF POSSIBLE
131  **/
132 
133 int
134 queue_repel(PSTATUS *pps, int move_off, int (*qchk_p)(RSTATUS *))
135 {
136 	register RSTATUS	*prs;
137 	register int		all_can		= 1;
138 	register PSTATUS	*stop_pps	= (move_off? pps : 0);
139 
140 
141 	/*
142 	 * Reevaluate all requests that are assigned to this
143 	 * printer, to see if there's another printer that
144 	 * can handle them.
145 	 *
146 	 * If the "move_off" flag is set, don't consider the current
147 	 * printer when reevaluating, but also don't cancel the request
148 	 * if it can't be moved off the printer.
149 	 * (Currently this is only used when deciding if a printer
150 	 * can be deleted.)
151 	 */
152 	BEGIN_WALK_BY_PRINTER_LOOP (prs, pps)
153 
154 		/*
155 		 * "all_can" keeps track of whether all of the requests
156 		 * of interest to the caller (governed by "qchk_p") can
157 		 * be moved to another printer. Now we don't move certain
158 		 * requests (active, done, gone remote), and some of those
159 		 * matter in the ``all can'' consideration.
160 		 */
161 		if (qchk_p && !(*qchk_p)(prs))
162 			continue;
163 		else if (SKIP_IT(prs)) {
164 			if ( !(prs->request->outcome & RS_DONE) )
165 				all_can = 0;
166 			continue;
167 
168 		} else
169 
170 			if (reevaluate_request(prs, stop_pps) == MOK) {
171 
172 				/*
173 				 * If this request needs to be filtered,
174 				 * try to schedule it for filtering,
175 				 * otherwise schedule it for printing.
176 				 * We are inefficient here, because we may
177 				 * try to schedule many requests but the
178 				 * filtering slot(s) and printers are
179 				 * busy; but the requests may languish
180 				 * if we don't check here.
181 				 */
182 				if (NEEDS_FILTERING(prs))
183 					schedule (EV_SLOWF, prs);
184 				else
185 					schedule (EV_INTERF, prs->printer);
186 
187 			} else {
188 				all_can = 0;
189 				if (!move_off)
190 					cancel (prs, 1);
191 				else
192 					prs->reason = MOK;
193 			}
194 	END_WALK_LOOP
195 
196 	return (all_can);
197 }
198 
199 /**
200  ** queue_check() - CHECK ALL REQUESTS AGAIN
201  **/
202 
203 void
204 queue_check(int (*qchk_p)( RSTATUS * ))
205 {
206 	register RSTATUS	*prs;
207 
208 
209 	for (prs = Request_List; prs; prs = prs->next)
210 		if (!SKIP_IT(prs) && (!qchk_p || (*qchk_p)(prs)))
211 			if (reevaluate_request(prs, (PSTATUS *)0) == MOK)
212 				if (NEEDS_FILTERING(prs))
213 					schedule (EV_SLOWF, prs);
214 				else
215 					schedule (EV_INTERF, prs->printer);
216 			else
217 				cancel (prs, 1);
218 
219 	return;
220 }
221 
222 /**
223  ** qchk_waiting() - CHECK IF REQUEST IS READY TO PRINT
224  ** qchk_filter() - CHECK IF REQUEST NEEDS A FILTER
225  ** qchk_form() - CHECK IF REQUEST NEEDS A FORM
226  ** qchk_pwheel() - CHECK IF REQUEST NEEDS PRINT A WHEEL
227  **/
228 
229 int
230 qchk_waiting(RSTATUS *prs)
231 {
232 	return (
233 		!(prs->request->outcome & (RS_HELD|RS_DONE|RS_ACTIVE))
234 	     && !NEEDS_FILTERING(prs)
235 	);
236 }
237 
238 int
239 qchk_filter(RSTATUS *prs)
240 {
241 	/*
242 	 * No need to reevaluate this request if it isn't using a filter
243 	 * or if it is done or is being changed.
244 	 */
245 	return (
246 		!(prs->request->outcome & (RS_DONE|RS_CHANGING|RS_NOTIFY))
247 	     && (prs->slow || prs->fast)
248 	);
249 }
250 
251 FSTATUS *		form_in_question;
252 
253 int
254 qchk_form(RSTATUS *prs)
255 {
256 	return (prs->form == form_in_question);
257 }
258 
259 char *			pwheel_in_question;
260 
261 int
262 qchk_pwheel(RSTATUS *prs)
263 {
264 	return (SAME(prs->pwheel_name, pwheel_in_question));
265 }
266