xref: /illumos-gate/usr/src/cmd/lp/cmd/lpsched/init.c (revision 55fea89d)
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 
227c478bd9Sstevel@tonic-gate /*
230a44ef6dSjacobs  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
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 #include "lpsched.h"
327c478bd9Sstevel@tonic-gate #include <syslog.h>
337c478bd9Sstevel@tonic-gate 
3419d32b9aSRobert Mustacchi CLSTATUS	**CStatus = NULL;		/* Status of same          */
350a44ef6dSjacobs PSTATUS		**PStatus = NULL;		/* Status of same          */
360a44ef6dSjacobs FSTATUS		**FStatus = NULL;		/* status of same	   */
37*55fea89dSDan Cross PWSTATUS	**PWStatus = NULL;		/* Status of same          */
380a44ef6dSjacobs EXEC		**Exec_Table = NULL;		/* Running processes       */
390a44ef6dSjacobs EXEC		**Exec_Slow = NULL;		/*   Slow filters	   */
400a44ef6dSjacobs EXEC		**Exec_Notify = NULL;		/*   Notifications	   */
410a44ef6dSjacobs RSTATUS		*Request_List = NULL;		/* Queue of print requests */
427c478bd9Sstevel@tonic-gate 
430a44ef6dSjacobs int		ET_SlowSize	= 1,
440a44ef6dSjacobs 		ET_NotifySize	= 1;
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate static void	init_printers(),
477c478bd9Sstevel@tonic-gate 		init_classes(),
487c478bd9Sstevel@tonic-gate 		init_forms(),
497c478bd9Sstevel@tonic-gate 		init_pwheels(),
507c478bd9Sstevel@tonic-gate 		init_exec();
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate 
530a44ef6dSjacobs static void	init_requests();
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate void
init_memory(void)567c478bd9Sstevel@tonic-gate init_memory(void)
577c478bd9Sstevel@tonic-gate {
580a44ef6dSjacobs     init_exec();
597c478bd9Sstevel@tonic-gate     init_printers();
607c478bd9Sstevel@tonic-gate     init_classes();
617c478bd9Sstevel@tonic-gate     init_forms();
627c478bd9Sstevel@tonic-gate     init_pwheels();
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate     /*
657c478bd9Sstevel@tonic-gate      * Load the status after the basic structures have been loaded,
667c478bd9Sstevel@tonic-gate      * but before loading requests still in the queue. This is so
677c478bd9Sstevel@tonic-gate      * the requests can be compared against accurate status information
687c478bd9Sstevel@tonic-gate      * (except rejection status--accept the jobs anyway).
697c478bd9Sstevel@tonic-gate      */
707c478bd9Sstevel@tonic-gate     load_status();
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate     Loadfilters(Lp_A_Filters);
737c478bd9Sstevel@tonic-gate 
740a44ef6dSjacobs     init_requests();
757c478bd9Sstevel@tonic-gate }
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate static void
init_printers()787c478bd9Sstevel@tonic-gate init_printers()
797c478bd9Sstevel@tonic-gate {
807c478bd9Sstevel@tonic-gate     PRINTER	*p;
810a44ef6dSjacobs     int i = 0;
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate     while((p = Getprinter(NAME_ALL)) != NULL || errno != ENOENT) {
840a44ef6dSjacobs 	if ((!p) || (p->remote))	/* NULL or this is remote, ignore it */
857c478bd9Sstevel@tonic-gate 		continue;
867c478bd9Sstevel@tonic-gate 
870a44ef6dSjacobs 	(void) new_pstatus(p);
880a44ef6dSjacobs 	syslog(LOG_DEBUG, "Loaded printer: %s", p->name);
897c478bd9Sstevel@tonic-gate     }
907c478bd9Sstevel@tonic-gate }
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate static void
init_classes()937c478bd9Sstevel@tonic-gate init_classes()
947c478bd9Sstevel@tonic-gate {
950a44ef6dSjacobs     CLASS       *c;
967c478bd9Sstevel@tonic-gate 
970a44ef6dSjacobs     while((c = Getclass(NAME_ALL)) != NULL) {
980a44ef6dSjacobs 	(void) new_cstatus(c);
990a44ef6dSjacobs         syslog(LOG_DEBUG, "Loaded class: %s", c->name);
1007c478bd9Sstevel@tonic-gate     }
1017c478bd9Sstevel@tonic-gate }
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate static void
init_forms()1047c478bd9Sstevel@tonic-gate init_forms()
1057c478bd9Sstevel@tonic-gate {
1060a44ef6dSjacobs     _FORM	*f;
1070a44ef6dSjacobs     int		i = 0;
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate     while ((f = Getform(NAME_ALL)) != NULL) {
1100a44ef6dSjacobs 	(void) new_fstatus(f);
1110a44ef6dSjacobs 	syslog(LOG_DEBUG, "Loaded form: %s", f->name);
1127c478bd9Sstevel@tonic-gate     }
1137c478bd9Sstevel@tonic-gate }
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate static void
init_pwheels()1167c478bd9Sstevel@tonic-gate init_pwheels()
1177c478bd9Sstevel@tonic-gate {
1187c478bd9Sstevel@tonic-gate     PWHEEL	*p;
1190a44ef6dSjacobs     int i = 0;
120*55fea89dSDan Cross 
1217c478bd9Sstevel@tonic-gate     while((p = Getpwheel(NAME_ALL)) != NULL || errno != ENOENT)
1227c478bd9Sstevel@tonic-gate     {
1230a44ef6dSjacobs 	if (!p)			/* NULL, ignore it. */
1247c478bd9Sstevel@tonic-gate 	    continue;
1257c478bd9Sstevel@tonic-gate 
1260a44ef6dSjacobs 	(void) new_pwstatus(p);
1270a44ef6dSjacobs     	syslog(LOG_DEBUG, "Loaded print-wheel: %s", p->name);
1287c478bd9Sstevel@tonic-gate     }
1297c478bd9Sstevel@tonic-gate }
1307c478bd9Sstevel@tonic-gate 
1310a44ef6dSjacobs static void
init_requests(void)1327c478bd9Sstevel@tonic-gate init_requests(void)
1337c478bd9Sstevel@tonic-gate {
1340a44ef6dSjacobs     RSTATUS		**table = NULL;
1357c478bd9Sstevel@tonic-gate     REQUEST		*r;
1367c478bd9Sstevel@tonic-gate     SECURE		*s;
1377c478bd9Sstevel@tonic-gate     char		*name;
1387c478bd9Sstevel@tonic-gate     char		*sysdir;
1397c478bd9Sstevel@tonic-gate     char		*sysname;
1407c478bd9Sstevel@tonic-gate     char		*reqfile = NULL;
1417c478bd9Sstevel@tonic-gate     long		addr = -1;
1427c478bd9Sstevel@tonic-gate     long		sysaddr = -1;
1437c478bd9Sstevel@tonic-gate     short		vr_ret;
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate     while((sysname = next_dir(Lp_Requests, &addr)) != NULL) {
1460a44ef6dSjacobs     	RSTATUS		*rsp;
1470a44ef6dSjacobs 
1487c478bd9Sstevel@tonic-gate 	sysdir = makepath(Lp_Requests, sysname, NULL);
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 	while((name = next_file(sysdir, &sysaddr)) != NULL) {
151*55fea89dSDan Cross 	    reqfile = makepath(sysname, name, NULL);
1527c478bd9Sstevel@tonic-gate 	    Free(name);
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 	    if ((s = Getsecure(reqfile)) == NULL) {
1550a44ef6dSjacobs 		RSTATUS tmp;
1560a44ef6dSjacobs 
1570a44ef6dSjacobs 		memset(&tmp, 0, sizeof (tmp));
1580a44ef6dSjacobs 		tmp.req_file = reqfile;	/* fix for 1103890 */
1590a44ef6dSjacobs 		rmfiles(&tmp, 0);
1600a44ef6dSjacobs 		free(tmp.req_file);
1617c478bd9Sstevel@tonic-gate 		continue;
1627c478bd9Sstevel@tonic-gate 	    }
1630a44ef6dSjacobs 	    syslog(LOG_DEBUG, "Loaded request: %s", reqfile);
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 	    if((r = Getrequest(reqfile)) == NULL) {
1660a44ef6dSjacobs 		RSTATUS tmp;
1670a44ef6dSjacobs 
1680a44ef6dSjacobs 		memset(&tmp, 0, sizeof (tmp));
1690a44ef6dSjacobs 		tmp.req_file = reqfile;	/* fix for 1103890 */
1700a44ef6dSjacobs 		rmfiles(&tmp, 0);
1710a44ef6dSjacobs 		freesecure(s);
1720a44ef6dSjacobs 		free(tmp.req_file);
1737c478bd9Sstevel@tonic-gate 		continue;
1747c478bd9Sstevel@tonic-gate 	    }
1750a44ef6dSjacobs 	    syslog(LOG_DEBUG, "Loaded secure: %s", s->req_id);
1767c478bd9Sstevel@tonic-gate 
1770a44ef6dSjacobs 	    rsp = new_rstatus(r, s);
1780a44ef6dSjacobs 
1790a44ef6dSjacobs 	    r->outcome &= ~RS_ACTIVE;	/* it can't be! */
1800a44ef6dSjacobs 	    rsp->req_file = reqfile;
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	    if ((r->outcome & (RS_CANCELLED|RS_FAILED)) &&
1830a44ef6dSjacobs 		!(r->outcome & RS_NOTIFY)) {
1840a44ef6dSjacobs 			rmfiles(rsp, 0);
1850a44ef6dSjacobs 			free_rstatus(rsp);
1860a44ef6dSjacobs 			continue;
1877c478bd9Sstevel@tonic-gate 	    }
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 	    /*
1907c478bd9Sstevel@tonic-gate 	     * So far, the only way RS_NOTIFY can be set without there
1917c478bd9Sstevel@tonic-gate 	     * being a notification file containing the message to the
1927c478bd9Sstevel@tonic-gate 	     * user, is if the request was cancelled. This is because
1937c478bd9Sstevel@tonic-gate 	     * cancelling a request does not cause the creation of the
1947c478bd9Sstevel@tonic-gate 	     * message if the request is currently printing or filtering.
1957c478bd9Sstevel@tonic-gate 	     * (The message is created after the child process dies.)
1967c478bd9Sstevel@tonic-gate 	     * Thus, we know what to say.
1977c478bd9Sstevel@tonic-gate 	     *
1987c478bd9Sstevel@tonic-gate 	     * If this behaviour changes, we may have to find another way
1997c478bd9Sstevel@tonic-gate 	     * of determining what to say in the message.
2007c478bd9Sstevel@tonic-gate 	     */
2017c478bd9Sstevel@tonic-gate 	    if (r->outcome & RS_NOTIFY) {
2020a44ef6dSjacobs 		char	*file = makereqerr(rsp);
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 		if (Access(file, F_OK) == -1) {
2057c478bd9Sstevel@tonic-gate 		    if (!(r->outcome & RS_CANCELLED)) {
2067c478bd9Sstevel@tonic-gate 			Free(file);
2070a44ef6dSjacobs 			rmfiles(rsp, 0);
2080a44ef6dSjacobs 			free_rstatus(rsp);
2097c478bd9Sstevel@tonic-gate 			continue;
2107c478bd9Sstevel@tonic-gate 		    }
2110a44ef6dSjacobs 		    notify(rsp, NULL, 0, 0, 0);
2127c478bd9Sstevel@tonic-gate 		}
2137c478bd9Sstevel@tonic-gate 		Free(file);
2147c478bd9Sstevel@tonic-gate 	    }
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 	    /* fix for bugid 1103709. if validate_request returns
2177c478bd9Sstevel@tonic-gate 	     * MNODEST, then the printer for the request doesn't exist
2187c478bd9Sstevel@tonic-gate 	     * anymore! E.g. lpadmin -x was issued, and the request
2197c478bd9Sstevel@tonic-gate 	     * hasn't been cleaned up. In this case, the "printer"
2207c478bd9Sstevel@tonic-gate 	     * element of table[] will be NULL, and cancel will
2217c478bd9Sstevel@tonic-gate 	     * core dump! So we clean this up here.
2227c478bd9Sstevel@tonic-gate 	     */
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 	    /*
2257c478bd9Sstevel@tonic-gate 	     * Well actually this happens with MDENYDEST too. The real problem
2267c478bd9Sstevel@tonic-gate 	     * is if the printer is NULL, so test for it
2277c478bd9Sstevel@tonic-gate 	     */
2287c478bd9Sstevel@tonic-gate 
2290a44ef6dSjacobs 	    if ((vr_ret=validate_request(rsp, NULL, 1)) != MOK) {
2300a44ef6dSjacobs 		if (vr_ret == MNODEST || (rsp->printer == NULL)) {
2310a44ef6dSjacobs 			rmfiles(rsp, 0);
2320a44ef6dSjacobs 			free_rstatus(rsp);
2337c478bd9Sstevel@tonic-gate 			continue;
2347c478bd9Sstevel@tonic-gate 		}
2350a44ef6dSjacobs 		cancel(rsp, 1);
2367c478bd9Sstevel@tonic-gate 	    }
2377c478bd9Sstevel@tonic-gate 
2380a44ef6dSjacobs 	    list_append((void ***)&table, (void *)rsp);
2397c478bd9Sstevel@tonic-gate 	}
2407c478bd9Sstevel@tonic-gate 	Free(sysdir);
2417c478bd9Sstevel@tonic-gate 	Free(sysname);
2427c478bd9Sstevel@tonic-gate 	sysaddr = -1;
2437c478bd9Sstevel@tonic-gate     }
244*55fea89dSDan Cross 
2450a44ef6dSjacobs     if (table != NULL) {
2460a44ef6dSjacobs 	unsigned long i;
2477c478bd9Sstevel@tonic-gate 
2480a44ef6dSjacobs 	for (i = 0; table[i] != NULL; i++);
2497c478bd9Sstevel@tonic-gate 
2500a44ef6dSjacobs 	qsort((void *)table, i, sizeof(RSTATUS *),
2510a44ef6dSjacobs 			(int (*)(const void * , const void *))rsort);
2527c478bd9Sstevel@tonic-gate 
2530a44ef6dSjacobs 	for (i = 0; table[i] != NULL; i++) {
2540a44ef6dSjacobs 		table[i]->next = table[i + 1];
2550a44ef6dSjacobs 		if (table[i + 1] != NULL)
2560a44ef6dSjacobs 			table[i + 1]->prev = table[i];
2577c478bd9Sstevel@tonic-gate 	}
2587c478bd9Sstevel@tonic-gate 
2590a44ef6dSjacobs 	Request_List = *table;
2600a44ef6dSjacobs 	Free(table);
2610a44ef6dSjacobs     }
2627c478bd9Sstevel@tonic-gate }
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate static void
init_exec()2657c478bd9Sstevel@tonic-gate init_exec()
2667c478bd9Sstevel@tonic-gate {
2670a44ef6dSjacobs     EXEC	*ep;
2687c478bd9Sstevel@tonic-gate     int		i;
2697c478bd9Sstevel@tonic-gate 
2700a44ef6dSjacobs     for (i = 0; i < ET_SlowSize; i++) {
2710a44ef6dSjacobs 	ep = new_exec(EX_SLOWF, NULL);
2720a44ef6dSjacobs 	list_append((void ***)&Exec_Slow, (void *)ep);
2737c478bd9Sstevel@tonic-gate     }
2747c478bd9Sstevel@tonic-gate 
2750a44ef6dSjacobs     for (i = 0; i < ET_NotifySize; i++) {
2760a44ef6dSjacobs 	ep = new_exec(EX_NOTIFY, NULL);
2770a44ef6dSjacobs 	list_append((void ***)&Exec_Notify, (void *)ep);
2787c478bd9Sstevel@tonic-gate     }
2797c478bd9Sstevel@tonic-gate }
280