xref: /illumos-gate/usr/src/cmd/lp/cmd/lpsched/requeue.c (revision 19d32b9a)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50a44ef6dSjacobs  * Common Development and Distribution License (the "License").
60a44ef6dSjacobs  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
210a44ef6dSjacobs 
220a44ef6dSjacobs /*
230a44ef6dSjacobs  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
240a44ef6dSjacobs  * Use is subject to license terms.
250a44ef6dSjacobs  */
260a44ef6dSjacobs 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate /* EMACS_MODES: !fill, lnumb, !overwrite, !nodelete, !picture */
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #include "lpsched.h"
347c478bd9Sstevel@tonic-gate #include "validate.h"
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate /*
377c478bd9Sstevel@tonic-gate  * The routines in this file are used to examine queued requests
387c478bd9Sstevel@tonic-gate  * to see if something must be done about them. We don't bother
397c478bd9Sstevel@tonic-gate  * checking requests that are:
407c478bd9Sstevel@tonic-gate  *
417c478bd9Sstevel@tonic-gate  *	- printing (we could, to allow the administrator to stop
427c478bd9Sstevel@tonic-gate  *	  a request by making a configuration change, but that
437c478bd9Sstevel@tonic-gate  *	  can lead to trouble (yet another way to terminate a child)
447c478bd9Sstevel@tonic-gate  *	  and the administrator can always disable the request to
457c478bd9Sstevel@tonic-gate  *	  force it to stop printing and be reevaluated);
467c478bd9Sstevel@tonic-gate  *
477c478bd9Sstevel@tonic-gate  *	- changing, since once the change is complete the request
487c478bd9Sstevel@tonic-gate  *	  will be reevaluated again anyway;
497c478bd9Sstevel@tonic-gate  *
507c478bd9Sstevel@tonic-gate  *	- notifying, since the request is essentially finished
517c478bd9Sstevel@tonic-gate  *
527c478bd9Sstevel@tonic-gate  *	- being sent or already sent to a remote machine;
537c478bd9Sstevel@tonic-gate  *
547c478bd9Sstevel@tonic-gate  *	- done.
557c478bd9Sstevel@tonic-gate  *
567c478bd9Sstevel@tonic-gate  * Requests that are being held or are filtering ARE to be considered,
577c478bd9Sstevel@tonic-gate  * because things may have changed to make them impossible to print.
587c478bd9Sstevel@tonic-gate  */
597c478bd9Sstevel@tonic-gate #define RS_SKIP	((RS_ACTIVE & ~RS_FILTERING) | RS_DONE)
607c478bd9Sstevel@tonic-gate #define	SKIP_IT(PRS) ((PRS)->request->outcome & RS_SKIP)
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate /**
637c478bd9Sstevel@tonic-gate  ** queue_attract() - REASSIGN REQUEST(S) TO PRINTER, IF POSSIBLE
647c478bd9Sstevel@tonic-gate  **/
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate void
queue_attract(PSTATUS * pps,int (* qchk_p)(RSTATUS *),int attract_just_one)677c478bd9Sstevel@tonic-gate queue_attract(PSTATUS *pps, int (*qchk_p)(RSTATUS *), int attract_just_one)
687c478bd9Sstevel@tonic-gate {
697c478bd9Sstevel@tonic-gate 	register RSTATUS	*prs;
70*19d32b9aSRobert Mustacchi 	register CLSTATUS	*pcs;
717c478bd9Sstevel@tonic-gate 	int			called_schedule	= 0;
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate 	/*
757c478bd9Sstevel@tonic-gate 	 * Evaluate requests that:
767c478bd9Sstevel@tonic-gate 	 *	- meet a criteria set by a function passed.
777c478bd9Sstevel@tonic-gate 	 *	- are already queued for the printer
787c478bd9Sstevel@tonic-gate 	 *	- are destined for a class containing this printer
797c478bd9Sstevel@tonic-gate 	 *	- or are destined for any printer
807c478bd9Sstevel@tonic-gate 	 * We stop on the first one that will work on the printer,
817c478bd9Sstevel@tonic-gate 	 * and schedule an interface for the printer (which will
827c478bd9Sstevel@tonic-gate 	 * find the first request ready, namely the one we stopped on).
837c478bd9Sstevel@tonic-gate 	 */
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate #define	SAMECLASS(PRS,PPS) \
867c478bd9Sstevel@tonic-gate 	( \
870a44ef6dSjacobs 		((pcs = search_cstatus(PRS->request->destination)) != NULL) \
887c478bd9Sstevel@tonic-gate 	     && searchlist(PPS->printer->name, pcs->class->members) \
897c478bd9Sstevel@tonic-gate 	)
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate #define ISANY(PRS)	STREQU(PRS->request->destination, NAME_ANY)
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate 	for (prs = Request_List; prs; prs = prs->next) {
947c478bd9Sstevel@tonic-gate 		if (
957c478bd9Sstevel@tonic-gate 			!SKIP_IT(prs)
967c478bd9Sstevel@tonic-gate 		     && (!qchk_p || (*qchk_p)(prs))
977c478bd9Sstevel@tonic-gate 		     && (
987c478bd9Sstevel@tonic-gate 				prs->printer == pps
997c478bd9Sstevel@tonic-gate 			     || ISANY(prs)
1007c478bd9Sstevel@tonic-gate 			     || SAMECLASS(prs, pps)
1017c478bd9Sstevel@tonic-gate 			)
1027c478bd9Sstevel@tonic-gate 		)
1037c478bd9Sstevel@tonic-gate 			/*
1047c478bd9Sstevel@tonic-gate 			 * Don't need to evaluate the request if it
1057c478bd9Sstevel@tonic-gate 			 * is already queued!
1067c478bd9Sstevel@tonic-gate 			 */
1077c478bd9Sstevel@tonic-gate 			if (
1087c478bd9Sstevel@tonic-gate 				prs->printer == pps
1097c478bd9Sstevel@tonic-gate 			     || evaluate_request(prs, pps, 0) == MOK
1107c478bd9Sstevel@tonic-gate 			) {
1117c478bd9Sstevel@tonic-gate 				/*
1127c478bd9Sstevel@tonic-gate 				 * This request was attracted to the
1137c478bd9Sstevel@tonic-gate 				 * printer but maybe it now needs to be
1147c478bd9Sstevel@tonic-gate 				 * filtered. If so, filter it but see if
1157c478bd9Sstevel@tonic-gate 				 * there's another request all set to go.
1167c478bd9Sstevel@tonic-gate 				 */
1177c478bd9Sstevel@tonic-gate 				if (NEEDS_FILTERING(prs))
1187c478bd9Sstevel@tonic-gate 					schedule (EV_SLOWF, prs);
1197c478bd9Sstevel@tonic-gate 				else {
1207c478bd9Sstevel@tonic-gate 					if (!called_schedule) {
1217c478bd9Sstevel@tonic-gate 						schedule (EV_INTERF, pps);
1227c478bd9Sstevel@tonic-gate 						called_schedule = 1;
1237c478bd9Sstevel@tonic-gate 					}
1247c478bd9Sstevel@tonic-gate 					if (attract_just_one)
1257c478bd9Sstevel@tonic-gate 						break;
1267c478bd9Sstevel@tonic-gate 				}
1277c478bd9Sstevel@tonic-gate 			}
1287c478bd9Sstevel@tonic-gate 	}
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 	return;
1317c478bd9Sstevel@tonic-gate }
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate /**
1347c478bd9Sstevel@tonic-gate  ** queue_repel() - REASSIGN REQUESTS TO ANOTHER PRINTER, IF POSSIBLE
1357c478bd9Sstevel@tonic-gate  **/
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate int
queue_repel(PSTATUS * pps,int move_off,int (* qchk_p)(RSTATUS *))1387c478bd9Sstevel@tonic-gate queue_repel(PSTATUS *pps, int move_off, int (*qchk_p)(RSTATUS *))
1397c478bd9Sstevel@tonic-gate {
1407c478bd9Sstevel@tonic-gate 	register RSTATUS	*prs;
1417c478bd9Sstevel@tonic-gate 	register int		all_can		= 1;
1427c478bd9Sstevel@tonic-gate 	register PSTATUS	*stop_pps	= (move_off? pps : 0);
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 	/*
1457c478bd9Sstevel@tonic-gate 	 * Reevaluate all requests that are assigned to this
1467c478bd9Sstevel@tonic-gate 	 * printer, to see if there's another printer that
1477c478bd9Sstevel@tonic-gate 	 * can handle them.
1487c478bd9Sstevel@tonic-gate 	 *
1497c478bd9Sstevel@tonic-gate 	 * If the "move_off" flag is set, don't consider the current
1507c478bd9Sstevel@tonic-gate 	 * printer when reevaluating, but also don't cancel the request
1517c478bd9Sstevel@tonic-gate 	 * if it can't be moved off the printer.
1527c478bd9Sstevel@tonic-gate 	 * (Currently this is only used when deciding if a printer
1537c478bd9Sstevel@tonic-gate 	 * can be deleted.)
1547c478bd9Sstevel@tonic-gate 	 */
1550a44ef6dSjacobs 	for (prs = Request_List; prs != NULL; prs = prs->next) {
1560a44ef6dSjacobs 		if (prs->printer != pps)
1570a44ef6dSjacobs 			continue;
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 		/*
1607c478bd9Sstevel@tonic-gate 		 * "all_can" keeps track of whether all of the requests
1617c478bd9Sstevel@tonic-gate 		 * of interest to the caller (governed by "qchk_p") can
1627c478bd9Sstevel@tonic-gate 		 * be moved to another printer. Now we don't move certain
1637c478bd9Sstevel@tonic-gate 		 * requests (active, done, gone remote), and some of those
1647c478bd9Sstevel@tonic-gate 		 * matter in the ``all can'' consideration.
1657c478bd9Sstevel@tonic-gate 		 */
1667c478bd9Sstevel@tonic-gate 		if (qchk_p && !(*qchk_p)(prs))
1677c478bd9Sstevel@tonic-gate 			continue;
1687c478bd9Sstevel@tonic-gate 		else if (SKIP_IT(prs)) {
1697c478bd9Sstevel@tonic-gate 			if ( !(prs->request->outcome & RS_DONE) )
1707c478bd9Sstevel@tonic-gate 				all_can = 0;
1717c478bd9Sstevel@tonic-gate 			continue;
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 		} else
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 			if (reevaluate_request(prs, stop_pps) == MOK) {
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 				/*
1787c478bd9Sstevel@tonic-gate 				 * If this request needs to be filtered,
1797c478bd9Sstevel@tonic-gate 				 * try to schedule it for filtering,
1807c478bd9Sstevel@tonic-gate 				 * otherwise schedule it for printing.
1817c478bd9Sstevel@tonic-gate 				 * We are inefficient here, because we may
1827c478bd9Sstevel@tonic-gate 				 * try to schedule many requests but the
1837c478bd9Sstevel@tonic-gate 				 * filtering slot(s) and printers are
1847c478bd9Sstevel@tonic-gate 				 * busy; but the requests may languish
1857c478bd9Sstevel@tonic-gate 				 * if we don't check here.
1867c478bd9Sstevel@tonic-gate 				 */
1877c478bd9Sstevel@tonic-gate 				if (NEEDS_FILTERING(prs))
1887c478bd9Sstevel@tonic-gate 					schedule (EV_SLOWF, prs);
1897c478bd9Sstevel@tonic-gate 				else
1907c478bd9Sstevel@tonic-gate 					schedule (EV_INTERF, prs->printer);
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 			} else {
1937c478bd9Sstevel@tonic-gate 				all_can = 0;
1947c478bd9Sstevel@tonic-gate 				if (!move_off)
1957c478bd9Sstevel@tonic-gate 					cancel (prs, 1);
1967c478bd9Sstevel@tonic-gate 				else
1977c478bd9Sstevel@tonic-gate 					prs->reason = MOK;
1987c478bd9Sstevel@tonic-gate 			}
1990a44ef6dSjacobs 	}
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 	return (all_can);
2027c478bd9Sstevel@tonic-gate }
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate /**
2057c478bd9Sstevel@tonic-gate  ** queue_check() - CHECK ALL REQUESTS AGAIN
2067c478bd9Sstevel@tonic-gate  **/
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate void
queue_check(int (* qchk_p)(RSTATUS *))2097c478bd9Sstevel@tonic-gate queue_check(int (*qchk_p)( RSTATUS * ))
2107c478bd9Sstevel@tonic-gate {
2117c478bd9Sstevel@tonic-gate 	register RSTATUS	*prs;
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 	for (prs = Request_List; prs; prs = prs->next)
2157c478bd9Sstevel@tonic-gate 		if (!SKIP_IT(prs) && (!qchk_p || (*qchk_p)(prs)))
2167c478bd9Sstevel@tonic-gate 			if (reevaluate_request(prs, (PSTATUS *)0) == MOK)
2177c478bd9Sstevel@tonic-gate 				if (NEEDS_FILTERING(prs))
2187c478bd9Sstevel@tonic-gate 					schedule (EV_SLOWF, prs);
2197c478bd9Sstevel@tonic-gate 				else
2207c478bd9Sstevel@tonic-gate 					schedule (EV_INTERF, prs->printer);
2217c478bd9Sstevel@tonic-gate 			else
2227c478bd9Sstevel@tonic-gate 				cancel (prs, 1);
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 	return;
2257c478bd9Sstevel@tonic-gate }
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate /**
2287c478bd9Sstevel@tonic-gate  ** qchk_waiting() - CHECK IF REQUEST IS READY TO PRINT
2297c478bd9Sstevel@tonic-gate  ** qchk_filter() - CHECK IF REQUEST NEEDS A FILTER
2307c478bd9Sstevel@tonic-gate  ** qchk_form() - CHECK IF REQUEST NEEDS A FORM
2317c478bd9Sstevel@tonic-gate  ** qchk_pwheel() - CHECK IF REQUEST NEEDS PRINT A WHEEL
2327c478bd9Sstevel@tonic-gate  **/
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate int
qchk_waiting(RSTATUS * prs)2357c478bd9Sstevel@tonic-gate qchk_waiting(RSTATUS *prs)
2367c478bd9Sstevel@tonic-gate {
2377c478bd9Sstevel@tonic-gate 	return (
2387c478bd9Sstevel@tonic-gate 		!(prs->request->outcome & (RS_HELD|RS_DONE|RS_ACTIVE))
2397c478bd9Sstevel@tonic-gate 	     && !NEEDS_FILTERING(prs)
2407c478bd9Sstevel@tonic-gate 	);
2417c478bd9Sstevel@tonic-gate }
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate int
qchk_filter(RSTATUS * prs)2447c478bd9Sstevel@tonic-gate qchk_filter(RSTATUS *prs)
2457c478bd9Sstevel@tonic-gate {
2467c478bd9Sstevel@tonic-gate 	/*
2477c478bd9Sstevel@tonic-gate 	 * No need to reevaluate this request if it isn't using a filter
2487c478bd9Sstevel@tonic-gate 	 * or if it is done or is being changed.
2497c478bd9Sstevel@tonic-gate 	 */
2507c478bd9Sstevel@tonic-gate 	return (
2517c478bd9Sstevel@tonic-gate 		!(prs->request->outcome & (RS_DONE|RS_CHANGING|RS_NOTIFY))
2527c478bd9Sstevel@tonic-gate 	     && (prs->slow || prs->fast)
2537c478bd9Sstevel@tonic-gate 	);
2547c478bd9Sstevel@tonic-gate }
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate FSTATUS *		form_in_question;
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate int
qchk_form(RSTATUS * prs)2597c478bd9Sstevel@tonic-gate qchk_form(RSTATUS *prs)
2607c478bd9Sstevel@tonic-gate {
2617c478bd9Sstevel@tonic-gate 	return (prs->form == form_in_question);
2627c478bd9Sstevel@tonic-gate }
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate char *			pwheel_in_question;
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate int
qchk_pwheel(RSTATUS * prs)2677c478bd9Sstevel@tonic-gate qchk_pwheel(RSTATUS *prs)
2687c478bd9Sstevel@tonic-gate {
2697c478bd9Sstevel@tonic-gate 	return (SAME(prs->pwheel_name, pwheel_in_question));
2707c478bd9Sstevel@tonic-gate }
271