xref: /onnv-gate/usr/src/cmd/lp/cmd/lpsched/faults.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23*0Sstevel@tonic-gate /*	  All Rights Reserved  	*/
24*0Sstevel@tonic-gate 
25*0Sstevel@tonic-gate 
26*0Sstevel@tonic-gate /*
27*0Sstevel@tonic-gate  * Copyright 2002 Sun Microsystems, Inc.  All rights reserved.
28*0Sstevel@tonic-gate  * Use is subject to license terms.
29*0Sstevel@tonic-gate  */
30*0Sstevel@tonic-gate 
31*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
32*0Sstevel@tonic-gate /* EMACS_MODES: !fill, lnumb, !overwrite, !nodelete, !picture */
33*0Sstevel@tonic-gate 
34*0Sstevel@tonic-gate #include "lpsched.h"
35*0Sstevel@tonic-gate #include <syslog.h>
36*0Sstevel@tonic-gate 
37*0Sstevel@tonic-gate static char *
38*0Sstevel@tonic-gate shortenReason(char *reason)
39*0Sstevel@tonic-gate {
40*0Sstevel@tonic-gate 	register char	*ptr, *pe;
41*0Sstevel@tonic-gate 	int		peLen;
42*0Sstevel@tonic-gate 
43*0Sstevel@tonic-gate 	if (strncmp(reason,"%%[",3) == 0)
44*0Sstevel@tonic-gate 		reason += 3;
45*0Sstevel@tonic-gate 
46*0Sstevel@tonic-gate 	while (*reason == ' ')
47*0Sstevel@tonic-gate 		reason++;
48*0Sstevel@tonic-gate 
49*0Sstevel@tonic-gate 	pe = "PrinterError:";
50*0Sstevel@tonic-gate 	peLen = strlen(pe);
51*0Sstevel@tonic-gate 	if (strncmp(reason,pe,peLen) == 0)
52*0Sstevel@tonic-gate 		reason += peLen;
53*0Sstevel@tonic-gate 
54*0Sstevel@tonic-gate 	if (((ptr = strchr(reason,']')) != NULL) && (strncmp(ptr,"]%%",3) == 0))
55*0Sstevel@tonic-gate 		*ptr = 0;
56*0Sstevel@tonic-gate 
57*0Sstevel@tonic-gate 	pe = reason + strlen(reason) -1;
58*0Sstevel@tonic-gate 	pe = reason;
59*0Sstevel@tonic-gate 	while (pe = strchr(pe,'\n'))
60*0Sstevel@tonic-gate 		*pe = ' ';
61*0Sstevel@tonic-gate 
62*0Sstevel@tonic-gate 	pe = reason + strlen(reason) -1;
63*0Sstevel@tonic-gate 	while ((pe > reason) && (*pe == ' ')) {
64*0Sstevel@tonic-gate 		*pe = 0;
65*0Sstevel@tonic-gate 		pe--;
66*0Sstevel@tonic-gate 	}
67*0Sstevel@tonic-gate 	return(reason);
68*0Sstevel@tonic-gate }
69*0Sstevel@tonic-gate 
70*0Sstevel@tonic-gate /**
71*0Sstevel@tonic-gate  ** printer_fault() - RECOGNIZE PRINTER FAULT
72*0Sstevel@tonic-gate  **/
73*0Sstevel@tonic-gate 
74*0Sstevel@tonic-gate void
75*0Sstevel@tonic-gate printer_fault(register PSTATUS *pps, register RSTATUS *prs, char *alert_text,
76*0Sstevel@tonic-gate 	 int err)
77*0Sstevel@tonic-gate {
78*0Sstevel@tonic-gate 	register char		*why,*shortWhy;
79*0Sstevel@tonic-gate 
80*0Sstevel@tonic-gate 	pps->status |= PS_FAULTED;
81*0Sstevel@tonic-gate 
82*0Sstevel@tonic-gate 	/*  -F wait  */
83*0Sstevel@tonic-gate 	if (STREQU(pps->printer->fault_rec, NAME_WAIT))
84*0Sstevel@tonic-gate 		disable (pps, CUZ_FAULT, DISABLE_STOP);
85*0Sstevel@tonic-gate 
86*0Sstevel@tonic-gate 	/*  -F beginning  */
87*0Sstevel@tonic-gate 	else if (STREQU(pps->printer->fault_rec, NAME_BEGINNING))
88*0Sstevel@tonic-gate 		terminate (pps->exec);
89*0Sstevel@tonic-gate 
90*0Sstevel@tonic-gate 	/*  -F continue  AND  the interface program died  */
91*0Sstevel@tonic-gate 	else if (!(pps->status & PS_LATER) && !pps->request) {
92*0Sstevel@tonic-gate 		load_str (&pps->dis_reason, CUZ_STOPPED);
93*0Sstevel@tonic-gate 		schedule (EV_LATER, WHEN_PRINTER, EV_ENABLE, pps);
94*0Sstevel@tonic-gate 	}
95*0Sstevel@tonic-gate 
96*0Sstevel@tonic-gate 	if (err) {
97*0Sstevel@tonic-gate 		errno = err;
98*0Sstevel@tonic-gate 		why = makestr(alert_text, "(", PERROR, ")\n", (char *)0);
99*0Sstevel@tonic-gate 	} else if (! alert_text)
100*0Sstevel@tonic-gate 		why = makestr("exec exit fault", (char *) 0);
101*0Sstevel@tonic-gate 	else
102*0Sstevel@tonic-gate 		why = makestr(alert_text, (char *) 0);
103*0Sstevel@tonic-gate 
104*0Sstevel@tonic-gate 	if (!why)
105*0Sstevel@tonic-gate 		why = alert_text;
106*0Sstevel@tonic-gate 
107*0Sstevel@tonic-gate 	shortWhy = (why != alert_text ? shortenReason(why) : why);
108*0Sstevel@tonic-gate 
109*0Sstevel@tonic-gate 	load_str (&pps->fault_reason, shortWhy);
110*0Sstevel@tonic-gate 	dump_fault_status (pps);
111*0Sstevel@tonic-gate 	if (STREQU(pps->printer->fault_alert.shcmd,"show fault"))
112*0Sstevel@tonic-gate 		pps->status |= PS_SHOW_FAULT;
113*0Sstevel@tonic-gate 	else
114*0Sstevel@tonic-gate 		pps->status &= ~PS_SHOW_FAULT;
115*0Sstevel@tonic-gate 
116*0Sstevel@tonic-gate 	note("printer fault. type: %s, status: %x\nmsg: (%s)\n",
117*0Sstevel@tonic-gate 		(pps->printer->fault_alert.shcmd ?
118*0Sstevel@tonic-gate 		    pps->printer->fault_alert.shcmd : "??"),
119*0Sstevel@tonic-gate 		pps->status, shortWhy);
120*0Sstevel@tonic-gate 	syslog(LOG_INFO, "fault (%s), type (%s), status(%x): %s",
121*0Sstevel@tonic-gate 		(pps->printer->name ? pps->printer->name : "NULL"),
122*0Sstevel@tonic-gate 		(pps->printer->fault_alert.shcmd ?
123*0Sstevel@tonic-gate 			pps->printer->fault_alert.shcmd : "NULL"),
124*0Sstevel@tonic-gate 		pps->status, (shortWhy ? shortWhy : "NULL"));
125*0Sstevel@tonic-gate 
126*0Sstevel@tonic-gate 	if (pps->status & PS_SHOW_FAULT)
127*0Sstevel@tonic-gate 		schedule (EV_MESSAGE, pps);
128*0Sstevel@tonic-gate 	else {
129*0Sstevel@tonic-gate 		alert(A_PRINTER, pps, prs, shortWhy);
130*0Sstevel@tonic-gate 	}
131*0Sstevel@tonic-gate 	if (why != alert_text)
132*0Sstevel@tonic-gate 		Free (why);
133*0Sstevel@tonic-gate }
134*0Sstevel@tonic-gate 
135*0Sstevel@tonic-gate /**
136*0Sstevel@tonic-gate  ** clear_printer_fault() - RECOGNIZE PRINTER FAULT
137*0Sstevel@tonic-gate  **/
138*0Sstevel@tonic-gate 
139*0Sstevel@tonic-gate void
140*0Sstevel@tonic-gate clear_printer_fault(register PSTATUS *pps, char *alert_text)
141*0Sstevel@tonic-gate {
142*0Sstevel@tonic-gate 	register char	*why, *shortWhy;
143*0Sstevel@tonic-gate 
144*0Sstevel@tonic-gate 	pps->status &= ~PS_FAULTED;
145*0Sstevel@tonic-gate 
146*0Sstevel@tonic-gate 	why = makestr(alert_text, (char *) 0);
147*0Sstevel@tonic-gate 
148*0Sstevel@tonic-gate 	shortWhy = (why ? shortenReason(why) : alert_text);
149*0Sstevel@tonic-gate 
150*0Sstevel@tonic-gate 	load_str (&pps->fault_reason, shortWhy);
151*0Sstevel@tonic-gate 	dump_fault_status (pps);
152*0Sstevel@tonic-gate 	if (STREQU(pps->printer->fault_alert.shcmd,"show fault"))
153*0Sstevel@tonic-gate 		pps->status |= PS_SHOW_FAULT;
154*0Sstevel@tonic-gate 	else
155*0Sstevel@tonic-gate 		pps->status &= ~PS_SHOW_FAULT;
156*0Sstevel@tonic-gate 
157*0Sstevel@tonic-gate 	if (pps->status & PS_SHOW_FAULT)
158*0Sstevel@tonic-gate 		schedule (EV_MESSAGE, pps);
159*0Sstevel@tonic-gate 	if (why != alert_text)
160*0Sstevel@tonic-gate 		Free(why);
161*0Sstevel@tonic-gate 	schedule(EV_ENABLE, pps);
162*0Sstevel@tonic-gate }
163*0Sstevel@tonic-gate 
164*0Sstevel@tonic-gate /**
165*0Sstevel@tonic-gate  ** dial_problem() - ADDRESS DIAL-OUT PROBLEM
166*0Sstevel@tonic-gate  **/
167*0Sstevel@tonic-gate 
168*0Sstevel@tonic-gate void
169*0Sstevel@tonic-gate dial_problem(register PSTATUS *pps, RSTATUS *prs, int rc)
170*0Sstevel@tonic-gate {
171*0Sstevel@tonic-gate 	static struct problem {
172*0Sstevel@tonic-gate 		char			*reason;
173*0Sstevel@tonic-gate 		int			retry_max,
174*0Sstevel@tonic-gate 					dial_error;
175*0Sstevel@tonic-gate 	}			problems[] = {
176*0Sstevel@tonic-gate 		"DIAL FAILED",			10,	 2, /* D_HUNG  */
177*0Sstevel@tonic-gate 		"CALLER SCRIPT FAILED",		10,	 3, /* NO_ANS  */
178*0Sstevel@tonic-gate 		"CAN'T ACCESS DEVICE",		 0,	 6, /* L_PROB  */
179*0Sstevel@tonic-gate 		"DEVICE LOCKED",		20,	 8, /* DV_NT_A */
180*0Sstevel@tonic-gate 		"NO DEVICES AVAILABLE",		 0,	10, /* NO_BD_A */
181*0Sstevel@tonic-gate 		"SYSTEM NOT IN Systems FILE",	 0,	13, /* BAD_SYS */
182*0Sstevel@tonic-gate 		"UNKNOWN dial() FAILURE",	 0,	0
183*0Sstevel@tonic-gate 	};
184*0Sstevel@tonic-gate 
185*0Sstevel@tonic-gate 	register struct problem	*p;
186*0Sstevel@tonic-gate 
187*0Sstevel@tonic-gate 	register char		*msg;
188*0Sstevel@tonic-gate 
189*0Sstevel@tonic-gate #define PREFIX	"Connect problem: "
190*0Sstevel@tonic-gate #define SUFFIX	"This problem has occurred several times.\nPlease check the dialing instructions for this printer.\n"
191*0Sstevel@tonic-gate 
192*0Sstevel@tonic-gate 
193*0Sstevel@tonic-gate 	for (p = problems; p->dial_error; p++)
194*0Sstevel@tonic-gate 		if (p->dial_error == rc)
195*0Sstevel@tonic-gate 			break;
196*0Sstevel@tonic-gate 
197*0Sstevel@tonic-gate 	if (!p->retry_max) {
198*0Sstevel@tonic-gate 		msg = Malloc(strlen(PREFIX) + strlen(p->reason) + 2);
199*0Sstevel@tonic-gate 		sprintf (msg, "%s%s\n", PREFIX, p->reason);
200*0Sstevel@tonic-gate 		printer_fault (pps, prs, msg, 0);
201*0Sstevel@tonic-gate 		Free (msg);
202*0Sstevel@tonic-gate 
203*0Sstevel@tonic-gate 	} else if (pps->last_dial_rc != rc) {
204*0Sstevel@tonic-gate 		pps->nretry = 1;
205*0Sstevel@tonic-gate 		pps->last_dial_rc = (short)rc;
206*0Sstevel@tonic-gate 
207*0Sstevel@tonic-gate 	} else if (pps->nretry++ > p->retry_max) {
208*0Sstevel@tonic-gate 		pps->nretry = 0;
209*0Sstevel@tonic-gate 		pps->last_dial_rc = (short)rc;
210*0Sstevel@tonic-gate 		msg = Malloc(
211*0Sstevel@tonic-gate 		strlen(PREFIX) + strlen(p->reason) + strlen(SUFFIX) + 2
212*0Sstevel@tonic-gate 		);
213*0Sstevel@tonic-gate 		sprintf (msg, "%s%s%s\n", PREFIX, p->reason, SUFFIX);
214*0Sstevel@tonic-gate 		printer_fault (pps, prs, msg, 0);
215*0Sstevel@tonic-gate 		Free (msg);
216*0Sstevel@tonic-gate 	}
217*0Sstevel@tonic-gate 
218*0Sstevel@tonic-gate 	if (!(pps->status & PS_FAULTED)) {
219*0Sstevel@tonic-gate 		load_str (&pps->dis_reason, p->reason);
220*0Sstevel@tonic-gate 		schedule (EV_LATER, WHEN_PRINTER, EV_ENABLE, pps);
221*0Sstevel@tonic-gate 	}
222*0Sstevel@tonic-gate 
223*0Sstevel@tonic-gate 	return;
224*0Sstevel@tonic-gate }
225