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 /*
23*0Sstevel@tonic-gate  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate /*LINTLIBRARY*/
30*0Sstevel@tonic-gate 
31*0Sstevel@tonic-gate #include <stdio.h>
32*0Sstevel@tonic-gate #include <stdarg.h>
33*0Sstevel@tonic-gate #include <libintl.h>
34*0Sstevel@tonic-gate #include <string.h>
35*0Sstevel@tonic-gate #include <stdlib.h>
36*0Sstevel@tonic-gate 
37*0Sstevel@tonic-gate 
38*0Sstevel@tonic-gate /* lpsched include files */
39*0Sstevel@tonic-gate #include "lp.h"
40*0Sstevel@tonic-gate #include "msgs.h"
41*0Sstevel@tonic-gate #include "printers.h"
42*0Sstevel@tonic-gate 
43*0Sstevel@tonic-gate #include <papi_impl.h>
44*0Sstevel@tonic-gate 
45*0Sstevel@tonic-gate 
46*0Sstevel@tonic-gate /*
47*0Sstevel@tonic-gate  * Format and send message to lpsched (die if any errors occur)
48*0Sstevel@tonic-gate  */
49*0Sstevel@tonic-gate /*VARARGS1*/
50*0Sstevel@tonic-gate int
51*0Sstevel@tonic-gate snd_msg(service_t *svc, int type, ...)
52*0Sstevel@tonic-gate {
53*0Sstevel@tonic-gate 	int rc = -1;
54*0Sstevel@tonic-gate 	va_list	ap;
55*0Sstevel@tonic-gate 
56*0Sstevel@tonic-gate 	if (svc == NULL)
57*0Sstevel@tonic-gate 		return (-1);
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate 	/* fill the message buffer */
60*0Sstevel@tonic-gate 	va_start(ap, type);
61*0Sstevel@tonic-gate 	rc = _putmessage(svc->msgbuf, type, ap);
62*0Sstevel@tonic-gate 	va_end(ap);
63*0Sstevel@tonic-gate 	if (rc < 0) {
64*0Sstevel@tonic-gate 		detailed_error(svc,
65*0Sstevel@tonic-gate 			gettext("unable to build message for scheduler: %s"),
66*0Sstevel@tonic-gate 				strerror(errno));
67*0Sstevel@tonic-gate 		return (rc);
68*0Sstevel@tonic-gate 	}
69*0Sstevel@tonic-gate 
70*0Sstevel@tonic-gate 	/* write the message */
71*0Sstevel@tonic-gate 	while (((rc = mwrite(svc->md, svc->msgbuf)) < 0) && (errno == EINTR));
72*0Sstevel@tonic-gate 
73*0Sstevel@tonic-gate 	if (rc < 0)
74*0Sstevel@tonic-gate 		detailed_error(svc,
75*0Sstevel@tonic-gate 			gettext("unable to send message to scheduler: %s"),
76*0Sstevel@tonic-gate 				strerror(errno));
77*0Sstevel@tonic-gate 	return (rc);
78*0Sstevel@tonic-gate }
79*0Sstevel@tonic-gate 
80*0Sstevel@tonic-gate /*
81*0Sstevel@tonic-gate  * Receive message from lpsched (die if any errors occur)
82*0Sstevel@tonic-gate  */
83*0Sstevel@tonic-gate int
84*0Sstevel@tonic-gate rcv_msg(service_t *svc, int type, ...)
85*0Sstevel@tonic-gate {
86*0Sstevel@tonic-gate 	int rc = -1;
87*0Sstevel@tonic-gate 
88*0Sstevel@tonic-gate 	if (svc == NULL)
89*0Sstevel@tonic-gate 		return (-1);
90*0Sstevel@tonic-gate 
91*0Sstevel@tonic-gate 	/* read the message */
92*0Sstevel@tonic-gate 	while (((rc = mread(svc->md, svc->msgbuf, svc->msgbuf_size)) < 0) &&
93*0Sstevel@tonic-gate 		(errno == EINTR));
94*0Sstevel@tonic-gate 
95*0Sstevel@tonic-gate 	if (rc < 0)
96*0Sstevel@tonic-gate 		detailed_error(svc,
97*0Sstevel@tonic-gate 			gettext("unable to read message from scheduler: %s"),
98*0Sstevel@tonic-gate 				strerror(errno));
99*0Sstevel@tonic-gate 	else {
100*0Sstevel@tonic-gate 		va_list ap;
101*0Sstevel@tonic-gate 
102*0Sstevel@tonic-gate 		va_start(ap, type);
103*0Sstevel@tonic-gate 		rc = _getmessage(svc->msgbuf, type, ap);
104*0Sstevel@tonic-gate 		va_end(ap);
105*0Sstevel@tonic-gate 
106*0Sstevel@tonic-gate 		if (rc < 0)
107*0Sstevel@tonic-gate 			detailed_error(svc,
108*0Sstevel@tonic-gate 			gettext("unable to parse message from scheduler: %s"),
109*0Sstevel@tonic-gate 				strerror(errno));
110*0Sstevel@tonic-gate 	}
111*0Sstevel@tonic-gate 
112*0Sstevel@tonic-gate 	return (rc);
113*0Sstevel@tonic-gate }
114*0Sstevel@tonic-gate 
115*0Sstevel@tonic-gate papi_status_t
116*0Sstevel@tonic-gate lpsched_status_to_papi_status(int status)
117*0Sstevel@tonic-gate {
118*0Sstevel@tonic-gate 	switch (status) {
119*0Sstevel@tonic-gate 	case MNOMEM:
120*0Sstevel@tonic-gate 		return (PAPI_TEMPORARY_ERROR);
121*0Sstevel@tonic-gate 	case MNOFILTER:
122*0Sstevel@tonic-gate 		return (PAPI_DOCUMENT_FORMAT_ERROR);
123*0Sstevel@tonic-gate 	case MNOOPEN:
124*0Sstevel@tonic-gate 		return (PAPI_DOCUMENT_ACCESS_ERROR);
125*0Sstevel@tonic-gate 	case MERRDEST:
126*0Sstevel@tonic-gate 		return (PAPI_DEVICE_ERROR);
127*0Sstevel@tonic-gate 	case MDENYDEST:
128*0Sstevel@tonic-gate 		return (PAPI_NOT_ACCEPTING);
129*0Sstevel@tonic-gate 	case MNOMEDIA:
130*0Sstevel@tonic-gate 		return (PAPI_PRINT_SUPPORT_FILE_NOT_FOUND);
131*0Sstevel@tonic-gate 	case MDENYMEDIA:
132*0Sstevel@tonic-gate 	case MNOPERM:
133*0Sstevel@tonic-gate 		return (PAPI_NOT_AUTHORIZED);
134*0Sstevel@tonic-gate 	case MUNKNOWN:
135*0Sstevel@tonic-gate 	case MNODEST:
136*0Sstevel@tonic-gate 	case MNOINFO:
137*0Sstevel@tonic-gate 		return (PAPI_NOT_FOUND);
138*0Sstevel@tonic-gate 	case MTRANSMITERR:
139*0Sstevel@tonic-gate 		return (PAPI_SERVICE_UNAVAILABLE);
140*0Sstevel@tonic-gate 	case M2LATE:
141*0Sstevel@tonic-gate 		return (PAPI_GONE);
142*0Sstevel@tonic-gate 	case MOK:
143*0Sstevel@tonic-gate 	case MOKMORE:
144*0Sstevel@tonic-gate 		return (PAPI_OK);
145*0Sstevel@tonic-gate 	}
146*0Sstevel@tonic-gate 
147*0Sstevel@tonic-gate 	return (PAPI_INTERNAL_ERROR);
148*0Sstevel@tonic-gate }
149*0Sstevel@tonic-gate 
150*0Sstevel@tonic-gate char *
151*0Sstevel@tonic-gate lpsched_status_string(short status)
152*0Sstevel@tonic-gate {
153*0Sstevel@tonic-gate 		switch (status) {
154*0Sstevel@tonic-gate 	case MNOMEM:
155*0Sstevel@tonic-gate 		return (gettext("lpsched: out of memory"));
156*0Sstevel@tonic-gate 	case MNOFILTER:
157*0Sstevel@tonic-gate 		return (gettext("No filter available to convert job"));
158*0Sstevel@tonic-gate 	case MNOOPEN:
159*0Sstevel@tonic-gate 		return (gettext("lpsched: could not open request"));
160*0Sstevel@tonic-gate 	case MERRDEST:
161*0Sstevel@tonic-gate 		return (gettext("An error occured in submission"));
162*0Sstevel@tonic-gate 	case MDENYDEST:
163*0Sstevel@tonic-gate 		return (gettext("destination denied request"));
164*0Sstevel@tonic-gate 	case MNOMEDIA:
165*0Sstevel@tonic-gate 		return (gettext("unknown form specified in job"));
166*0Sstevel@tonic-gate 	case MDENYMEDIA:
167*0Sstevel@tonic-gate 		return (gettext("access denied to form specified in job"));
168*0Sstevel@tonic-gate 	case MUNKNOWN:
169*0Sstevel@tonic-gate 		return (gettext("no such resource"));
170*0Sstevel@tonic-gate 	case MNODEST:
171*0Sstevel@tonic-gate 		return (gettext("unknown destination"));
172*0Sstevel@tonic-gate 	case MNOPERM:
173*0Sstevel@tonic-gate 		return (gettext("permission denied"));
174*0Sstevel@tonic-gate 	case MNOINFO:
175*0Sstevel@tonic-gate 		return (gettext("no information available"));
176*0Sstevel@tonic-gate 	case MTRANSMITERR:
177*0Sstevel@tonic-gate 		return (gettext("failure to communicate with lpsched"));
178*0Sstevel@tonic-gate 	default: {
179*0Sstevel@tonic-gate 		static char result[16];
180*0Sstevel@tonic-gate 
181*0Sstevel@tonic-gate 		snprintf(result, sizeof (result), gettext("status: %d"),
182*0Sstevel@tonic-gate 								status);
183*0Sstevel@tonic-gate 		return (result);
184*0Sstevel@tonic-gate 		}
185*0Sstevel@tonic-gate 	}
186*0Sstevel@tonic-gate }
187*0Sstevel@tonic-gate 
188*0Sstevel@tonic-gate papi_status_t
189*0Sstevel@tonic-gate lpsched_alloc_files(papi_service_t svc, int number, char **prefix)
190*0Sstevel@tonic-gate {
191*0Sstevel@tonic-gate 	papi_status_t result = PAPI_OK;
192*0Sstevel@tonic-gate 	short status = MOK;
193*0Sstevel@tonic-gate 
194*0Sstevel@tonic-gate 	if ((svc == NULL) || (prefix == NULL))
195*0Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
196*0Sstevel@tonic-gate 
197*0Sstevel@tonic-gate 	if ((snd_msg(svc, S_ALLOC_FILES, number) < 0) ||
198*0Sstevel@tonic-gate 	    (rcv_msg(svc, R_ALLOC_FILES, &status, prefix) < 0))
199*0Sstevel@tonic-gate 		status = MTRANSMITERR;
200*0Sstevel@tonic-gate 
201*0Sstevel@tonic-gate 	if (status != MOK) {
202*0Sstevel@tonic-gate 		detailed_error(svc,
203*0Sstevel@tonic-gate 		gettext("failed to allocate %d file(s) for request: %s"),
204*0Sstevel@tonic-gate 			number, lpsched_status_string(status));
205*0Sstevel@tonic-gate 		result = lpsched_status_to_papi_status(status);
206*0Sstevel@tonic-gate 	}
207*0Sstevel@tonic-gate 
208*0Sstevel@tonic-gate 	return (result);
209*0Sstevel@tonic-gate }
210*0Sstevel@tonic-gate 
211*0Sstevel@tonic-gate papi_status_t
212*0Sstevel@tonic-gate lpsched_commit_job(papi_service_t svc, char *job, char **tmp)
213*0Sstevel@tonic-gate /* job is host/req-id */
214*0Sstevel@tonic-gate {
215*0Sstevel@tonic-gate 	papi_status_t result = PAPI_OK;
216*0Sstevel@tonic-gate 	short status = MOK;
217*0Sstevel@tonic-gate 	long bits;
218*0Sstevel@tonic-gate 
219*0Sstevel@tonic-gate 	if ((svc == NULL) || (job == NULL) || (tmp == NULL))
220*0Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
221*0Sstevel@tonic-gate 
222*0Sstevel@tonic-gate 	if ((snd_msg(svc, S_PRINT_REQUEST, job) < 0) ||
223*0Sstevel@tonic-gate 	    (rcv_msg(svc, R_PRINT_REQUEST, &status, tmp, &bits) < 0))
224*0Sstevel@tonic-gate 		status = MTRANSMITERR;
225*0Sstevel@tonic-gate 
226*0Sstevel@tonic-gate 	if (status != MOK) {
227*0Sstevel@tonic-gate 		detailed_error(svc, gettext("failed to commit job (%s): %s"),
228*0Sstevel@tonic-gate 			job, lpsched_status_string(status));
229*0Sstevel@tonic-gate 		result = lpsched_status_to_papi_status(status);
230*0Sstevel@tonic-gate 	}
231*0Sstevel@tonic-gate 
232*0Sstevel@tonic-gate 	return (result);
233*0Sstevel@tonic-gate }
234*0Sstevel@tonic-gate 
235*0Sstevel@tonic-gate papi_status_t
236*0Sstevel@tonic-gate lpsched_start_change(papi_service_t svc, const char *printer, int32_t job_id,
237*0Sstevel@tonic-gate 		char **tmp)
238*0Sstevel@tonic-gate {
239*0Sstevel@tonic-gate 	papi_status_t result = PAPI_OK;
240*0Sstevel@tonic-gate 	short status = MOK;
241*0Sstevel@tonic-gate 	char req[BUFSIZ];
242*0Sstevel@tonic-gate 	char *dest;
243*0Sstevel@tonic-gate 
244*0Sstevel@tonic-gate 	if ((svc == NULL) || (printer == NULL) || (job_id < 0))
245*0Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
246*0Sstevel@tonic-gate 
247*0Sstevel@tonic-gate 	dest = printer_name_from_uri_id(printer, job_id);
248*0Sstevel@tonic-gate 	snprintf(req, sizeof (req), "%s-%d", dest, job_id);
249*0Sstevel@tonic-gate 	free(dest);
250*0Sstevel@tonic-gate 
251*0Sstevel@tonic-gate 	if ((snd_msg(svc, S_START_CHANGE_REQUEST, req) < 0) ||
252*0Sstevel@tonic-gate 	    (rcv_msg(svc, R_START_CHANGE_REQUEST, &status, tmp) < 0))
253*0Sstevel@tonic-gate 		status = MTRANSMITERR;
254*0Sstevel@tonic-gate 
255*0Sstevel@tonic-gate 	if (status != MOK) {
256*0Sstevel@tonic-gate 		detailed_error(svc,
257*0Sstevel@tonic-gate 		gettext("failed to initiate change for job (%s-%d): %s"),
258*0Sstevel@tonic-gate 			printer,
259*0Sstevel@tonic-gate 			job_id, lpsched_status_string(status));
260*0Sstevel@tonic-gate 		result = lpsched_status_to_papi_status(status);
261*0Sstevel@tonic-gate 	}
262*0Sstevel@tonic-gate 
263*0Sstevel@tonic-gate 	return (result);
264*0Sstevel@tonic-gate }
265*0Sstevel@tonic-gate 
266*0Sstevel@tonic-gate papi_status_t
267*0Sstevel@tonic-gate lpsched_end_change(papi_service_t svc, const char *printer, int32_t job_id)
268*0Sstevel@tonic-gate {
269*0Sstevel@tonic-gate 	papi_status_t result = PAPI_OK;
270*0Sstevel@tonic-gate 	short status = MOK;
271*0Sstevel@tonic-gate 	long bits;
272*0Sstevel@tonic-gate 	char req[BUFSIZ];
273*0Sstevel@tonic-gate 	char *dest;
274*0Sstevel@tonic-gate 
275*0Sstevel@tonic-gate 	if ((svc == NULL) || (printer == NULL) || (job_id < 0))
276*0Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
277*0Sstevel@tonic-gate 
278*0Sstevel@tonic-gate 	dest = printer_name_from_uri_id(printer, job_id);
279*0Sstevel@tonic-gate 	snprintf(req, sizeof (req), "%s-%d", dest, job_id);
280*0Sstevel@tonic-gate 	free(dest);
281*0Sstevel@tonic-gate 
282*0Sstevel@tonic-gate 	if ((snd_msg(svc, S_END_CHANGE_REQUEST, req) < 0) ||
283*0Sstevel@tonic-gate 	    (rcv_msg(svc, R_END_CHANGE_REQUEST, &status, &bits) < 0))
284*0Sstevel@tonic-gate 		status = MTRANSMITERR;
285*0Sstevel@tonic-gate 
286*0Sstevel@tonic-gate 	if (status != MOK) {
287*0Sstevel@tonic-gate 		detailed_error(svc,
288*0Sstevel@tonic-gate 		gettext("failed to commit change for job (%s-%d): %s"), printer,
289*0Sstevel@tonic-gate 			job_id, lpsched_status_string(status));
290*0Sstevel@tonic-gate 		result = lpsched_status_to_papi_status(status);
291*0Sstevel@tonic-gate 	}
292*0Sstevel@tonic-gate 
293*0Sstevel@tonic-gate 	return (result);
294*0Sstevel@tonic-gate }
295*0Sstevel@tonic-gate 
296*0Sstevel@tonic-gate papi_status_t
297*0Sstevel@tonic-gate lpsched_enable_printer(papi_service_t svc, const char *printer)
298*0Sstevel@tonic-gate {
299*0Sstevel@tonic-gate 	papi_status_t result = PAPI_OK;
300*0Sstevel@tonic-gate 	short	 status;
301*0Sstevel@tonic-gate 	char	*req_id;
302*0Sstevel@tonic-gate 	char *dest;
303*0Sstevel@tonic-gate 
304*0Sstevel@tonic-gate 	if ((svc == NULL) || (printer == NULL))
305*0Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
306*0Sstevel@tonic-gate 
307*0Sstevel@tonic-gate 	dest = printer_name_from_uri_id(printer, -1);
308*0Sstevel@tonic-gate 	if ((snd_msg(svc, S_ENABLE_DEST, dest) < 0) ||
309*0Sstevel@tonic-gate 	    (rcv_msg(svc, R_ENABLE_DEST, &status, &req_id) < 0))
310*0Sstevel@tonic-gate 		status = MTRANSMITERR;
311*0Sstevel@tonic-gate 	free(dest);
312*0Sstevel@tonic-gate 
313*0Sstevel@tonic-gate 	if ((status != MOK) && (status != MERRDEST)) {
314*0Sstevel@tonic-gate 		detailed_error(svc, "%s: %s", printer,
315*0Sstevel@tonic-gate 			lpsched_status_string(status));
316*0Sstevel@tonic-gate 		result = lpsched_status_to_papi_status(status);
317*0Sstevel@tonic-gate 	}
318*0Sstevel@tonic-gate 
319*0Sstevel@tonic-gate 	return (result);
320*0Sstevel@tonic-gate }
321*0Sstevel@tonic-gate 
322*0Sstevel@tonic-gate papi_status_t
323*0Sstevel@tonic-gate lpsched_disable_printer(papi_service_t svc, const char *printer,
324*0Sstevel@tonic-gate 		const char *message)
325*0Sstevel@tonic-gate {
326*0Sstevel@tonic-gate 	papi_status_t result = PAPI_OK;
327*0Sstevel@tonic-gate 	short	 status;
328*0Sstevel@tonic-gate 	char	*req_id;
329*0Sstevel@tonic-gate 	char *dest;
330*0Sstevel@tonic-gate 
331*0Sstevel@tonic-gate 	if ((svc == NULL) || (printer == NULL))
332*0Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
333*0Sstevel@tonic-gate 
334*0Sstevel@tonic-gate 	if (message == NULL)
335*0Sstevel@tonic-gate 		message = "stopped by user";
336*0Sstevel@tonic-gate 
337*0Sstevel@tonic-gate 	dest = printer_name_from_uri_id(printer, -1);
338*0Sstevel@tonic-gate 	if ((snd_msg(svc, S_DISABLE_DEST, dest, message, 0) < 0) ||
339*0Sstevel@tonic-gate 	    (rcv_msg(svc, R_DISABLE_DEST, &status, &req_id) < 0))
340*0Sstevel@tonic-gate 		status = MTRANSMITERR;
341*0Sstevel@tonic-gate 	free(dest);
342*0Sstevel@tonic-gate 
343*0Sstevel@tonic-gate 	if ((status != MOK) && (status != MERRDEST)) {
344*0Sstevel@tonic-gate 		detailed_error(svc, "%s: %s", printer,
345*0Sstevel@tonic-gate 			lpsched_status_string(status));
346*0Sstevel@tonic-gate 		result = lpsched_status_to_papi_status(status);
347*0Sstevel@tonic-gate 	}
348*0Sstevel@tonic-gate 
349*0Sstevel@tonic-gate 	return (result);
350*0Sstevel@tonic-gate }
351