xref: /titanic_51/usr/src/cmd/lp/lib/papi/job.c (revision bb698c4dc004b5a8bca0f4a464a12121081d887e)
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
545916cd2Sjpk  * Common Development and Distribution License (the "License").
645916cd2Sjpk  * 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  */
217c478bd9Sstevel@tonic-gate /*
22324104abSsonam gupta - Sun Microsystems - Bangalore India  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
237c478bd9Sstevel@tonic-gate  */
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate /*LINTLIBRARY*/
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include <stdlib.h>
287c478bd9Sstevel@tonic-gate #include <string.h>
297c478bd9Sstevel@tonic-gate #include <unistd.h>
307c478bd9Sstevel@tonic-gate #include <libintl.h>
317c478bd9Sstevel@tonic-gate #include <pwd.h>
327c478bd9Sstevel@tonic-gate #include <sys/stat.h>
337c478bd9Sstevel@tonic-gate #include <papi_impl.h>
347c478bd9Sstevel@tonic-gate 
35355b4669Sjacobs /*
36355b4669Sjacobs  * for an older application that may have been linked with a pre-v1.0
37355b4669Sjacobs  * PAPI implementation.
38355b4669Sjacobs  */
39355b4669Sjacobs papi_status_t
40355b4669Sjacobs papiAttributeListAdd(papi_attribute_t ***attrs, int flags, char *name,
41355b4669Sjacobs 		papi_attribute_value_type_t type, papi_attribute_value_t *value)
42355b4669Sjacobs {
43355b4669Sjacobs 	return (papiAttributeListAddValue(attrs, flags, name, type, value));
44355b4669Sjacobs }
45355b4669Sjacobs 
467c478bd9Sstevel@tonic-gate #ifdef LP_USE_PAPI_ATTR
47355b4669Sjacobs static papi_status_t psm_modifyAttrsFile(papi_attribute_t **attrs, char *file);
48355b4669Sjacobs static papi_status_t psm_modifyAttrsList(char *file, papi_attribute_t **attrs,
49355b4669Sjacobs 					papi_attribute_t ***newAttrs);
507c478bd9Sstevel@tonic-gate #endif
517c478bd9Sstevel@tonic-gate 
52324104abSsonam gupta - Sun Microsystems - Bangalore India int32_t
53324104abSsonam gupta - Sun Microsystems - Bangalore India check_job_id(papi_service_t svc, char *printer, int32_t id)
54324104abSsonam gupta - Sun Microsystems - Bangalore India {
55324104abSsonam gupta - Sun Microsystems - Bangalore India 	papi_job_t *jobs = NULL;
56324104abSsonam gupta - Sun Microsystems - Bangalore India 	papi_status_t status;
57324104abSsonam gupta - Sun Microsystems - Bangalore India 	int ret = -1;
58324104abSsonam gupta - Sun Microsystems - Bangalore India 	char *jattrs[] = { "job-id",
59324104abSsonam gupta - Sun Microsystems - Bangalore India 	    "job-id-requested", NULL };
60324104abSsonam gupta - Sun Microsystems - Bangalore India 
61324104abSsonam gupta - Sun Microsystems - Bangalore India 	status = papiPrinterListJobs(svc, printer, jattrs, PAPI_LIST_JOBS_ALL,
62324104abSsonam gupta - Sun Microsystems - Bangalore India 	    0, &jobs);
63324104abSsonam gupta - Sun Microsystems - Bangalore India 
64324104abSsonam gupta - Sun Microsystems - Bangalore India 	if (status != PAPI_OK) {
65324104abSsonam gupta - Sun Microsystems - Bangalore India 		detailed_error(svc,
66324104abSsonam gupta - Sun Microsystems - Bangalore India 		    gettext("Failed to query service for %s: %s\n"),
67324104abSsonam gupta - Sun Microsystems - Bangalore India 		    printer, lpsched_status_string(status));
68324104abSsonam gupta - Sun Microsystems - Bangalore India 		return (-1);
69324104abSsonam gupta - Sun Microsystems - Bangalore India 	}
70324104abSsonam gupta - Sun Microsystems - Bangalore India 
71324104abSsonam gupta - Sun Microsystems - Bangalore India 	if (jobs != NULL) {
72324104abSsonam gupta - Sun Microsystems - Bangalore India 		int i = 0;
73324104abSsonam gupta - Sun Microsystems - Bangalore India 
74324104abSsonam gupta - Sun Microsystems - Bangalore India 		for (i = 0; jobs[i] != NULL; i++) {
75324104abSsonam gupta - Sun Microsystems - Bangalore India 			int32_t rid = -1;
76324104abSsonam gupta - Sun Microsystems - Bangalore India 			int32_t jid = -1;
77324104abSsonam gupta - Sun Microsystems - Bangalore India 			papi_attribute_t **list =
78324104abSsonam gupta - Sun Microsystems - Bangalore India 			    papiJobGetAttributeList(jobs[i]);
79324104abSsonam gupta - Sun Microsystems - Bangalore India 
80324104abSsonam gupta - Sun Microsystems - Bangalore India 			papiAttributeListGetInteger(list, NULL,
81324104abSsonam gupta - Sun Microsystems - Bangalore India 			    "job-id-requested", &rid);
82324104abSsonam gupta - Sun Microsystems - Bangalore India 			papiAttributeListGetInteger(list, NULL,
83324104abSsonam gupta - Sun Microsystems - Bangalore India 			    "job-id", &jid);
84324104abSsonam gupta - Sun Microsystems - Bangalore India 
85324104abSsonam gupta - Sun Microsystems - Bangalore India 			/*
86324104abSsonam gupta - Sun Microsystems - Bangalore India 			 * check if id matches with either rid or jid
87324104abSsonam gupta - Sun Microsystems - Bangalore India 			 */
88324104abSsonam gupta - Sun Microsystems - Bangalore India 			if (rid == id) {
89324104abSsonam gupta - Sun Microsystems - Bangalore India 				/* get the actual id and return it */
90324104abSsonam gupta - Sun Microsystems - Bangalore India 				papiAttributeListGetInteger(list, NULL,
91324104abSsonam gupta - Sun Microsystems - Bangalore India 				    "job-id", &id);
92324104abSsonam gupta - Sun Microsystems - Bangalore India 				return (id);
93324104abSsonam gupta - Sun Microsystems - Bangalore India 			} else if (jid == id) {
94324104abSsonam gupta - Sun Microsystems - Bangalore India 				if (rid != -1) {
95324104abSsonam gupta - Sun Microsystems - Bangalore India 					/*
96324104abSsonam gupta - Sun Microsystems - Bangalore India 					 * It is a remote lpd job.
97324104abSsonam gupta - Sun Microsystems - Bangalore India 					 * It cannot be modified based on job-id
98324104abSsonam gupta - Sun Microsystems - Bangalore India 					 * or spool number
99324104abSsonam gupta - Sun Microsystems - Bangalore India 					 */
100324104abSsonam gupta - Sun Microsystems - Bangalore India 					return (-1);
101324104abSsonam gupta - Sun Microsystems - Bangalore India 				} else {
102324104abSsonam gupta - Sun Microsystems - Bangalore India 					/*
103324104abSsonam gupta - Sun Microsystems - Bangalore India 					 * It is either local job or
104324104abSsonam gupta - Sun Microsystems - Bangalore India 					 * remote ipp job
105324104abSsonam gupta - Sun Microsystems - Bangalore India 					 */
106324104abSsonam gupta - Sun Microsystems - Bangalore India 					return (id);
107324104abSsonam gupta - Sun Microsystems - Bangalore India 				}
108324104abSsonam gupta - Sun Microsystems - Bangalore India 			}
109324104abSsonam gupta - Sun Microsystems - Bangalore India 		}
110324104abSsonam gupta - Sun Microsystems - Bangalore India 	}
111324104abSsonam gupta - Sun Microsystems - Bangalore India 	return (id);
112324104abSsonam gupta - Sun Microsystems - Bangalore India }
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate void
1157c478bd9Sstevel@tonic-gate papiJobFree(papi_job_t job)
1167c478bd9Sstevel@tonic-gate {
1177c478bd9Sstevel@tonic-gate 	job_t *tmp = (job_t *)job;
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 	if (tmp != NULL) {
1207c478bd9Sstevel@tonic-gate 		papiAttributeListFree(tmp->attributes);
1217c478bd9Sstevel@tonic-gate 		free(tmp);
1227c478bd9Sstevel@tonic-gate 	}
1237c478bd9Sstevel@tonic-gate }
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate void
1267c478bd9Sstevel@tonic-gate papiJobListFree(papi_job_t *jobs)
1277c478bd9Sstevel@tonic-gate {
1287c478bd9Sstevel@tonic-gate 	if (jobs != NULL) {
1297c478bd9Sstevel@tonic-gate 		int i;
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 		for (i = 0; jobs[i] != NULL; i++) {
1327c478bd9Sstevel@tonic-gate 			papiJobFree(jobs[i]);
1337c478bd9Sstevel@tonic-gate 		}
1347c478bd9Sstevel@tonic-gate 		free(jobs);
1357c478bd9Sstevel@tonic-gate 	}
1367c478bd9Sstevel@tonic-gate }
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate papi_attribute_t **
1397c478bd9Sstevel@tonic-gate papiJobGetAttributeList(papi_job_t job)
1407c478bd9Sstevel@tonic-gate {
1417c478bd9Sstevel@tonic-gate 	job_t *tmp = (job_t *)job;
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 	if (tmp != NULL)
1447c478bd9Sstevel@tonic-gate 		return (tmp->attributes);
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 	return (NULL);
1477c478bd9Sstevel@tonic-gate }
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate char *
1507c478bd9Sstevel@tonic-gate papiJobGetPrinterName(papi_job_t job)
1517c478bd9Sstevel@tonic-gate {
1527c478bd9Sstevel@tonic-gate 	job_t *tmp = (job_t *)job;
1537c478bd9Sstevel@tonic-gate 	char *result = NULL;
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 	if (tmp != NULL)
1567c478bd9Sstevel@tonic-gate 		papiAttributeListGetString(tmp->attributes, NULL,
1577c478bd9Sstevel@tonic-gate 		    "printer-name", &result);
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 	return (result);
1607c478bd9Sstevel@tonic-gate }
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate int32_t
1637c478bd9Sstevel@tonic-gate papiJobGetId(papi_job_t job)
1647c478bd9Sstevel@tonic-gate {
1657c478bd9Sstevel@tonic-gate 	job_t *tmp = (job_t *)job;
1667c478bd9Sstevel@tonic-gate 	int result = -1;
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 	if (tmp != NULL)
1697c478bd9Sstevel@tonic-gate 		papiAttributeListGetInteger(tmp->attributes, NULL, "job-id",
1707c478bd9Sstevel@tonic-gate 		    &result);
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 	return (result);
1737c478bd9Sstevel@tonic-gate }
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate static REQUEST *
1767c478bd9Sstevel@tonic-gate create_request(papi_service_t svc, char *printer, papi_attribute_t **attributes)
1777c478bd9Sstevel@tonic-gate {
178c1ecd8b9Sjacobs 	REQUEST *r;
1797c478bd9Sstevel@tonic-gate 
180c1ecd8b9Sjacobs 	if ((r = calloc(1, sizeof (*r))) != NULL) {
1813d09a4feSsonam gupta - Sun Microsystems - Bangalore India 		char *hostname = NULL;
1823d09a4feSsonam gupta - Sun Microsystems - Bangalore India 
183c1ecd8b9Sjacobs 		r->priority = -1;
184c1ecd8b9Sjacobs 		r->destination = printer_name_from_uri_id(printer, -1);
1853d09a4feSsonam gupta - Sun Microsystems - Bangalore India 
1863d09a4feSsonam gupta - Sun Microsystems - Bangalore India 		papiAttributeListGetString(attributes, NULL,
1873d09a4feSsonam gupta - Sun Microsystems - Bangalore India 		    "job-originating-host-name", &hostname);
1883d09a4feSsonam gupta - Sun Microsystems - Bangalore India 
1893d09a4feSsonam gupta - Sun Microsystems - Bangalore India 		if (hostname == NULL) {
1903d09a4feSsonam gupta - Sun Microsystems - Bangalore India 			char host[BUFSIZ];
1913d09a4feSsonam gupta - Sun Microsystems - Bangalore India 
1923d09a4feSsonam gupta - Sun Microsystems - Bangalore India 			if (gethostname(host, sizeof (host)) == 0)
1933d09a4feSsonam gupta - Sun Microsystems - Bangalore India 				papiAttributeListAddString(&attributes,
1943d09a4feSsonam gupta - Sun Microsystems - Bangalore India 				    PAPI_ATTR_REPLACE,
1953d09a4feSsonam gupta - Sun Microsystems - Bangalore India 				    "job-originating-host-name",
1963d09a4feSsonam gupta - Sun Microsystems - Bangalore India 				    host);
1973d09a4feSsonam gupta - Sun Microsystems - Bangalore India 		}
1983d09a4feSsonam gupta - Sun Microsystems - Bangalore India 
199c1ecd8b9Sjacobs 		job_attributes_to_lpsched_request(svc, r, attributes);
200c1ecd8b9Sjacobs 	}
2017c478bd9Sstevel@tonic-gate 
202c1ecd8b9Sjacobs 	return (r);
2037c478bd9Sstevel@tonic-gate }
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate static papi_status_t
2067c478bd9Sstevel@tonic-gate authorized(service_t *svc, int32_t id)
2077c478bd9Sstevel@tonic-gate {
2087c478bd9Sstevel@tonic-gate 	papi_status_t result = PAPI_NOT_AUTHORIZED;	/* assume the worst */
2097c478bd9Sstevel@tonic-gate 	char file[32];
2107c478bd9Sstevel@tonic-gate 	REQUEST *r;
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 	snprintf(file, sizeof (file), "%d-0", id);
2137c478bd9Sstevel@tonic-gate 	if ((r = getrequest(file)) != NULL) {
2147c478bd9Sstevel@tonic-gate 		uid_t uid = getuid();
2157c478bd9Sstevel@tonic-gate 		struct passwd *pw = NULL;
2167c478bd9Sstevel@tonic-gate 		char *user = "intruder";	/* assume an intruder */
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 		if ((pw = getpwuid(uid)) != NULL)
2197c478bd9Sstevel@tonic-gate 			user = pw->pw_name;	/* use the process owner */
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate 		if ((uid == 0) || (uid == 71)) { /* root/lp can forge this */
2227c478bd9Sstevel@tonic-gate 			papi_status_t s;
2237c478bd9Sstevel@tonic-gate 			s = papiAttributeListGetString(svc->attributes, NULL,
2247c478bd9Sstevel@tonic-gate 			    "user-name", &user);
2257c478bd9Sstevel@tonic-gate 			if (s != PAPI_OK)	/* true root/lp are almighty */
2267c478bd9Sstevel@tonic-gate 				result = PAPI_OK;
2277c478bd9Sstevel@tonic-gate 		}
2287c478bd9Sstevel@tonic-gate 
2296b5764c3Ssonam gupta - Sun Microsystems - Bangalore India 		if (result != PAPI_OK) {
2306b5764c3Ssonam gupta - Sun Microsystems - Bangalore India 			if (strcmp(user, r->user) == 0)
2317c478bd9Sstevel@tonic-gate 				result = PAPI_OK;
2326b5764c3Ssonam gupta - Sun Microsystems - Bangalore India 			else {
2336b5764c3Ssonam gupta - Sun Microsystems - Bangalore India 				/*
234f4a94adaSsonam gupta - Sun Microsystems - Bangalore India 				 * user and r->user might contain the
2356b5764c3Ssonam gupta - Sun Microsystems - Bangalore India 				 * host info also
2366b5764c3Ssonam gupta - Sun Microsystems - Bangalore India 				 */
237f4a94adaSsonam gupta - Sun Microsystems - Bangalore India 				char *token1 = strtok(r->user, "@");
238f4a94adaSsonam gupta - Sun Microsystems - Bangalore India 				char *token2 = strtok(NULL, "@");
239f4a94adaSsonam gupta - Sun Microsystems - Bangalore India 				char *token3 = strtok(user, "@");
240f4a94adaSsonam gupta - Sun Microsystems - Bangalore India 				char *token4 = strtok(NULL, "@");
2416b5764c3Ssonam gupta - Sun Microsystems - Bangalore India 
242f4a94adaSsonam gupta - Sun Microsystems - Bangalore India 				/*
243f4a94adaSsonam gupta - Sun Microsystems - Bangalore India 				 * token1 and token3 contain usernames
244f4a94adaSsonam gupta - Sun Microsystems - Bangalore India 				 * token2 and token4 contain hostnames
245f4a94adaSsonam gupta - Sun Microsystems - Bangalore India 				 */
246f4a94adaSsonam gupta - Sun Microsystems - Bangalore India 				if ((token1 == NULL) || (token3 == NULL))
247f4a94adaSsonam gupta - Sun Microsystems - Bangalore India 					result = PAPI_NOT_AUTHORIZED;
248f4a94adaSsonam gupta - Sun Microsystems - Bangalore India 				else if ((token4 != NULL) &&
249f4a94adaSsonam gupta - Sun Microsystems - Bangalore India 				    (strcmp(token4, "localhost") == 0) &&
250f4a94adaSsonam gupta - Sun Microsystems - Bangalore India 				    (strcmp(token3, "root") == 0) ||
251f4a94adaSsonam gupta - Sun Microsystems - Bangalore India 				    (strcmp(token3, "lp") == 0)) {
252f4a94adaSsonam gupta - Sun Microsystems - Bangalore India 					/*
253f4a94adaSsonam gupta - Sun Microsystems - Bangalore India 					 * root/lp user on server can
254f4a94adaSsonam gupta - Sun Microsystems - Bangalore India 					 * cancel any requset
255f4a94adaSsonam gupta - Sun Microsystems - Bangalore India 					 */
2566b5764c3Ssonam gupta - Sun Microsystems - Bangalore India 					result = PAPI_OK;
257f4a94adaSsonam gupta - Sun Microsystems - Bangalore India 				} else if (strcmp(token1, token3) == 0) {
258f4a94adaSsonam gupta - Sun Microsystems - Bangalore India 					/*
259f4a94adaSsonam gupta - Sun Microsystems - Bangalore India 					 * usernames are same
260f4a94adaSsonam gupta - Sun Microsystems - Bangalore India 					 * compare the hostnames
261f4a94adaSsonam gupta - Sun Microsystems - Bangalore India 					 */
262f4a94adaSsonam gupta - Sun Microsystems - Bangalore India 					if ((token4 != NULL) &&
263f4a94adaSsonam gupta - Sun Microsystems - Bangalore India 					    (token2 != NULL) &&
264f4a94adaSsonam gupta - Sun Microsystems - Bangalore India 					    (strcmp(token4, "localhost") ==
265f4a94adaSsonam gupta - Sun Microsystems - Bangalore India 					    0)) {
266f4a94adaSsonam gupta - Sun Microsystems - Bangalore India 						/*
267f4a94adaSsonam gupta - Sun Microsystems - Bangalore India 						 * Its server machine
268f4a94adaSsonam gupta - Sun Microsystems - Bangalore India 						 */
269f4a94adaSsonam gupta - Sun Microsystems - Bangalore India 						static char host[256];
270f4a94adaSsonam gupta - Sun Microsystems - Bangalore India 						if (gethostname(host,
271f4a94adaSsonam gupta - Sun Microsystems - Bangalore India 						    sizeof (host)) == 0) {
272f4a94adaSsonam gupta - Sun Microsystems - Bangalore India 							if ((host != NULL) &&
273f4a94adaSsonam gupta - Sun Microsystems - Bangalore India 							    (strcmp(host,
274f4a94adaSsonam gupta - Sun Microsystems - Bangalore India 							    token2) == 0))
275f4a94adaSsonam gupta - Sun Microsystems - Bangalore India 								result =
276f4a94adaSsonam gupta - Sun Microsystems - Bangalore India 								    PAPI_OK;
277f4a94adaSsonam gupta - Sun Microsystems - Bangalore India 						}
278f4a94adaSsonam gupta - Sun Microsystems - Bangalore India 
279f4a94adaSsonam gupta - Sun Microsystems - Bangalore India 					} else if ((token4 != NULL) &&
280f4a94adaSsonam gupta - Sun Microsystems - Bangalore India 					    (token2 != NULL) &&
281f4a94adaSsonam gupta - Sun Microsystems - Bangalore India 					    (strcmp(token4, token2) == 0)) {
282f4a94adaSsonam gupta - Sun Microsystems - Bangalore India 						result = PAPI_OK;
283f4a94adaSsonam gupta - Sun Microsystems - Bangalore India 					} else if ((token4 == NULL) &&
284f4a94adaSsonam gupta - Sun Microsystems - Bangalore India 					    (token2 != NULL)) {
285f4a94adaSsonam gupta - Sun Microsystems - Bangalore India 						/*
286f4a94adaSsonam gupta - Sun Microsystems - Bangalore India 						 * When the request is sent from
287f4a94adaSsonam gupta - Sun Microsystems - Bangalore India 						 * client to server using ipp
288f4a94adaSsonam gupta - Sun Microsystems - Bangalore India 						 * token4 is NULL
289f4a94adaSsonam gupta - Sun Microsystems - Bangalore India 						 */
290f4a94adaSsonam gupta - Sun Microsystems - Bangalore India 						result = PAPI_OK;
291f4a94adaSsonam gupta - Sun Microsystems - Bangalore India 					}
2926b5764c3Ssonam gupta - Sun Microsystems - Bangalore India 				}
2936b5764c3Ssonam gupta - Sun Microsystems - Bangalore India 			}
2946b5764c3Ssonam gupta - Sun Microsystems - Bangalore India 		}
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate 		freerequest(r);
2977c478bd9Sstevel@tonic-gate 	} else
2987c478bd9Sstevel@tonic-gate 		result = PAPI_NOT_FOUND;
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate 	return (result);
3017c478bd9Sstevel@tonic-gate }
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate static papi_status_t
304355b4669Sjacobs copy_file(char *from, char *to)
3057c478bd9Sstevel@tonic-gate {
3067c478bd9Sstevel@tonic-gate 	int ifd, ofd;
3077c478bd9Sstevel@tonic-gate 	char buf[BUFSIZ];
3087c478bd9Sstevel@tonic-gate 	int rc;
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate 	if ((ifd = open(from, O_RDONLY)) < 0)
3117c478bd9Sstevel@tonic-gate 		return (PAPI_DOCUMENT_ACCESS_ERROR);
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate 	if ((ofd = open(to, O_WRONLY)) < 0) {
3147c478bd9Sstevel@tonic-gate 		close(ifd);
3157c478bd9Sstevel@tonic-gate 		return (PAPI_NOT_POSSIBLE);
3167c478bd9Sstevel@tonic-gate 	}
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 	while ((rc = read(ifd, buf, sizeof (buf))) > 0)
3197c478bd9Sstevel@tonic-gate 		write(ofd, buf, rc);
3207c478bd9Sstevel@tonic-gate 
3217c478bd9Sstevel@tonic-gate 	close(ifd);
3227c478bd9Sstevel@tonic-gate 	close(ofd);
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate 	return (PAPI_OK);
3257c478bd9Sstevel@tonic-gate }
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate #ifdef LP_USE_PAPI_ATTR
3297c478bd9Sstevel@tonic-gate /*
3307c478bd9Sstevel@tonic-gate  * *****************************************************************************
3317c478bd9Sstevel@tonic-gate  *
3327c478bd9Sstevel@tonic-gate  * Description: Create a file containing all the attributes in the attribute
3337c478bd9Sstevel@tonic-gate  *              list passed to this function.
3347c478bd9Sstevel@tonic-gate  *              This file is then passed through lpsched and given to either
3357c478bd9Sstevel@tonic-gate  *              a slow-filter or to the printer's interface script to process
3367c478bd9Sstevel@tonic-gate  *              the attributes.
3377c478bd9Sstevel@tonic-gate  *
3387c478bd9Sstevel@tonic-gate  * Parameters:  attrs - list of attributes and their values
3397c478bd9Sstevel@tonic-gate  *              file  - file pathname to create and put the attributes into.
3407c478bd9Sstevel@tonic-gate  *
3417c478bd9Sstevel@tonic-gate  * *****************************************************************************
3427c478bd9Sstevel@tonic-gate  */
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate static papi_status_t
345355b4669Sjacobs psm_copy_attrsToFile(papi_attribute_t **attrs, char *file)
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate {
3487c478bd9Sstevel@tonic-gate 	papi_status_t result = PAPI_OK;
349355b4669Sjacobs 
350355b4669Sjacobs 	if ((attrs != NULL) && (*attrs != NULL)) {
3517c478bd9Sstevel@tonic-gate 		FILE *out = NULL;
3527c478bd9Sstevel@tonic-gate 
353355b4669Sjacobs 		if ((out = fopen(file, "w")) != NULL) {
354355b4669Sjacobs 			papiAttributeListPrint(out, attrs, "");
3557c478bd9Sstevel@tonic-gate 			fclose(out);
356355b4669Sjacobs 		} else {
3577c478bd9Sstevel@tonic-gate 			result = PAPI_NOT_POSSIBLE;
3587c478bd9Sstevel@tonic-gate 		}
3597c478bd9Sstevel@tonic-gate 	}
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate 	return (result);
3627c478bd9Sstevel@tonic-gate } /* psm_copy_attrsToFile */
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate /*
3667c478bd9Sstevel@tonic-gate  * *****************************************************************************
3677c478bd9Sstevel@tonic-gate  *
3687c478bd9Sstevel@tonic-gate  * Description: Modify the given attribute 'file' with the attributes from the
3697c478bd9Sstevel@tonic-gate  *              'attrs' list. Attributes already in the file will be replaced
3707c478bd9Sstevel@tonic-gate  *              with the new value. New attributes will be added into the file.
3717c478bd9Sstevel@tonic-gate  *
3727c478bd9Sstevel@tonic-gate  * Parameters:  attrs - list of attributes and their values
3737c478bd9Sstevel@tonic-gate  *              file  - file pathname to create and put the attributes into.
3747c478bd9Sstevel@tonic-gate  *
3757c478bd9Sstevel@tonic-gate  * *****************************************************************************
3767c478bd9Sstevel@tonic-gate  */
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate static papi_status_t
379355b4669Sjacobs psm_modifyAttrsFile(papi_attribute_t **attrs, char *file)
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate {
3827c478bd9Sstevel@tonic-gate 	papi_status_t result = PAPI_OK;
3837c478bd9Sstevel@tonic-gate 	papi_attribute_t **newAttrs = NULL;
3847c478bd9Sstevel@tonic-gate 	struct stat   tmpBuf;
3857c478bd9Sstevel@tonic-gate 	FILE *fd = NULL;
3867c478bd9Sstevel@tonic-gate 
387355b4669Sjacobs 	if ((attrs != NULL) && (*attrs != NULL) && (file != NULL)) {
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate 		/*
3907c478bd9Sstevel@tonic-gate 		 * check file exist before try to modify it, if it doesn't
3917c478bd9Sstevel@tonic-gate 		 * exist assume there is an error
3927c478bd9Sstevel@tonic-gate 		 */
393355b4669Sjacobs 		if (stat(file, &tmpBuf) == 0) {
3947c478bd9Sstevel@tonic-gate 			/*
3957c478bd9Sstevel@tonic-gate 			 * if file is currently empty just write the given
3967c478bd9Sstevel@tonic-gate 			 * attributes to the file otherwise exact the attributes
3977c478bd9Sstevel@tonic-gate 			 * from the file and modify them accordingly before
3987c478bd9Sstevel@tonic-gate 			 * writing them back to the file
3997c478bd9Sstevel@tonic-gate 			 */
400355b4669Sjacobs 			if (tmpBuf.st_size == 0) {
4017c478bd9Sstevel@tonic-gate 				newAttrs = (papi_attribute_t **)attrs;
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate 				fd = fopen(file, "w");
404355b4669Sjacobs 				if (fd != NULL) {
4057c478bd9Sstevel@tonic-gate 					papiAttributeListPrint(fd,
406355b4669Sjacobs 							newAttrs, "");
4077c478bd9Sstevel@tonic-gate 					fclose(fd);
408355b4669Sjacobs 				} else {
4097c478bd9Sstevel@tonic-gate 					result = PAPI_NOT_POSSIBLE;
4107c478bd9Sstevel@tonic-gate 				}
411355b4669Sjacobs 			} else {
4127c478bd9Sstevel@tonic-gate 				result =
4137c478bd9Sstevel@tonic-gate 				    psm_modifyAttrsList(file, attrs, &newAttrs);
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate 				fd = fopen(file, "w");
416355b4669Sjacobs 				if (fd != NULL) {
4177c478bd9Sstevel@tonic-gate 					papiAttributeListPrint(fd,
418355b4669Sjacobs 								newAttrs, "");
4197c478bd9Sstevel@tonic-gate 					fclose(fd);
420355b4669Sjacobs 				} else {
4217c478bd9Sstevel@tonic-gate 					result = PAPI_NOT_POSSIBLE;
4227c478bd9Sstevel@tonic-gate 				}
4237c478bd9Sstevel@tonic-gate 
4247c478bd9Sstevel@tonic-gate 				papiAttributeListFree(newAttrs);
4257c478bd9Sstevel@tonic-gate 			}
426355b4669Sjacobs 		} else {
4277c478bd9Sstevel@tonic-gate 			result = PAPI_NOT_POSSIBLE;
4287c478bd9Sstevel@tonic-gate 		}
4297c478bd9Sstevel@tonic-gate 	}
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate 	return (result);
4327c478bd9Sstevel@tonic-gate } /* psm_modifyAttrsFile */
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate 
4357c478bd9Sstevel@tonic-gate /*
4367c478bd9Sstevel@tonic-gate  * *****************************************************************************
4377c478bd9Sstevel@tonic-gate  *
4387c478bd9Sstevel@tonic-gate  * Description: Extracts the attributes in the given attribute 'file' and
4397c478bd9Sstevel@tonic-gate  *              creates a new list 'newAttrs' containing the modified list of
4407c478bd9Sstevel@tonic-gate  *              attributes.
4417c478bd9Sstevel@tonic-gate  *
4427c478bd9Sstevel@tonic-gate  * Parameters:  file  - pathname of file containing attributes to be modified
4437c478bd9Sstevel@tonic-gate  *              attrs - list of attributes and their values to modify
4447c478bd9Sstevel@tonic-gate  *              newAttrs - returns the modified list of attributes
4457c478bd9Sstevel@tonic-gate  *
4467c478bd9Sstevel@tonic-gate  * *****************************************************************************
4477c478bd9Sstevel@tonic-gate  */
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate static papi_status_t
450355b4669Sjacobs psm_modifyAttrsList(char *file, papi_attribute_t **attrs,
4517c478bd9Sstevel@tonic-gate     papi_attribute_t ***newAttrs)
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate {
4547c478bd9Sstevel@tonic-gate 	papi_status_t result = PAPI_OK;
4557c478bd9Sstevel@tonic-gate 	papi_attribute_t  *nextAttr = NULL;
4567c478bd9Sstevel@tonic-gate 	papi_attribute_value_t  **values = NULL;
4577c478bd9Sstevel@tonic-gate 	void *iter = NULL;
4587c478bd9Sstevel@tonic-gate 	FILE *fd = NULL;
4597c478bd9Sstevel@tonic-gate 	register int fD = 0;
4607c478bd9Sstevel@tonic-gate 	char aBuff[200];
4617c478bd9Sstevel@tonic-gate 	char *a = NULL;
4627c478bd9Sstevel@tonic-gate 	char *p = NULL;
4637c478bd9Sstevel@tonic-gate 	int count = 0;
4647c478bd9Sstevel@tonic-gate 	int n = 0;
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate 	fd = fopen(file, "r");
467355b4669Sjacobs 	if (fd != NULL) {
4687c478bd9Sstevel@tonic-gate 		fD = fileno(fd);
4697c478bd9Sstevel@tonic-gate 		a = &aBuff[0];
4707c478bd9Sstevel@tonic-gate 		p = &aBuff[0];
4717c478bd9Sstevel@tonic-gate 		count = read(fD, &aBuff[0], sizeof (aBuff) - 1);
472355b4669Sjacobs 		while ((result == PAPI_OK) && (count > 0)) {
4737c478bd9Sstevel@tonic-gate 			aBuff[count+n] = '\0';
474355b4669Sjacobs 			if (count == sizeof (aBuff) - n - 1) {
4757c478bd9Sstevel@tonic-gate 				p = strrchr(aBuff, '\n');
476355b4669Sjacobs 				if (p != NULL) {
4777c478bd9Sstevel@tonic-gate 					/* terminate at last complete line */
4787c478bd9Sstevel@tonic-gate 					*p = '\0';
4797c478bd9Sstevel@tonic-gate 				}
4807c478bd9Sstevel@tonic-gate 			}
4817c478bd9Sstevel@tonic-gate 			result = papiAttributeListFromString(
4827c478bd9Sstevel@tonic-gate 				newAttrs, PAPI_ATTR_EXCL, aBuff);
4837c478bd9Sstevel@tonic-gate 
484355b4669Sjacobs 			if (result == PAPI_OK) {
4857c478bd9Sstevel@tonic-gate 				/*
4867c478bd9Sstevel@tonic-gate 				 * handle any part lines and then read the next
4877c478bd9Sstevel@tonic-gate 				 * buffer from the file
4887c478bd9Sstevel@tonic-gate 				 */
4897c478bd9Sstevel@tonic-gate 				n = 0;
490355b4669Sjacobs 				if (p != a) {
4917c478bd9Sstevel@tonic-gate 					p++; /* skip NL */
4927c478bd9Sstevel@tonic-gate 					n = sizeof (aBuff) - 1 - (p - a);
4937c478bd9Sstevel@tonic-gate 					strncpy(aBuff, p, n);
4947c478bd9Sstevel@tonic-gate 				}
4957c478bd9Sstevel@tonic-gate 				count = read(fD, &aBuff[n],
4967c478bd9Sstevel@tonic-gate 					sizeof (aBuff) - n - 1);
4977c478bd9Sstevel@tonic-gate 				p = &aBuff[0];
4987c478bd9Sstevel@tonic-gate 			}
4997c478bd9Sstevel@tonic-gate 		}
5007c478bd9Sstevel@tonic-gate 		fclose(fd);
5017c478bd9Sstevel@tonic-gate 	}
5027c478bd9Sstevel@tonic-gate 
5037c478bd9Sstevel@tonic-gate 	/* now modify the attribute list with the new attributes in 'attrs' */
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate 	nextAttr = papiAttributeListGetNext((papi_attribute_t **)attrs, &iter);
506355b4669Sjacobs 	while ((result == PAPI_OK) && (nextAttr != NULL)) {
5077c478bd9Sstevel@tonic-gate 		values = nextAttr->values;
5087c478bd9Sstevel@tonic-gate 
509355b4669Sjacobs 		if ((values != NULL) && (*values != NULL)) {
510355b4669Sjacobs 			result = papiAttributeListAddValue(newAttrs,
5117c478bd9Sstevel@tonic-gate 						    PAPI_ATTR_REPLACE,
5127c478bd9Sstevel@tonic-gate 						    nextAttr->name,
5137c478bd9Sstevel@tonic-gate 						    nextAttr->type, *values);
5147c478bd9Sstevel@tonic-gate 			values++;
5157c478bd9Sstevel@tonic-gate 		}
5167c478bd9Sstevel@tonic-gate 
5177c478bd9Sstevel@tonic-gate 		while ((result == PAPI_OK) &&
518355b4669Sjacobs 			(values != NULL) && (*values != NULL)) {
519355b4669Sjacobs 			result = papiAttributeListAddValue(newAttrs,
5207c478bd9Sstevel@tonic-gate 						    PAPI_ATTR_APPEND,
5217c478bd9Sstevel@tonic-gate 						    nextAttr->name,
5227c478bd9Sstevel@tonic-gate 						    nextAttr->type, *values);
5237c478bd9Sstevel@tonic-gate 			values++;
5247c478bd9Sstevel@tonic-gate 		}
5257c478bd9Sstevel@tonic-gate 		nextAttr =
5267c478bd9Sstevel@tonic-gate 		    papiAttributeListGetNext((papi_attribute_t **)attrs, &iter);
5277c478bd9Sstevel@tonic-gate 	}
5287c478bd9Sstevel@tonic-gate 
5297c478bd9Sstevel@tonic-gate 	return (result);
5307c478bd9Sstevel@tonic-gate } /* papi_modifyAttrsList() */
5317c478bd9Sstevel@tonic-gate #endif
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate 
5347c478bd9Sstevel@tonic-gate papi_status_t
535355b4669Sjacobs papiJobSubmit(papi_service_t handle, char *printer,
536355b4669Sjacobs 		papi_attribute_t **job_attributes,
537355b4669Sjacobs 		papi_job_ticket_t *job_ticket,
538355b4669Sjacobs 		char **files, papi_job_t *job)
5397c478bd9Sstevel@tonic-gate {
5407c478bd9Sstevel@tonic-gate 	papi_status_t status;
5417c478bd9Sstevel@tonic-gate 	service_t *svc = handle;
54295c2d302SJonathan Cowper-Andrewes 	struct stat statbuf;
5437c478bd9Sstevel@tonic-gate 	job_t *j;
5447c478bd9Sstevel@tonic-gate 	int file_no;
5457c478bd9Sstevel@tonic-gate 	char *request_id = NULL;
5467c478bd9Sstevel@tonic-gate 	REQUEST *request;
5477c478bd9Sstevel@tonic-gate 	int i;
5487c478bd9Sstevel@tonic-gate 	char *c;
5497c478bd9Sstevel@tonic-gate 	char *tmp = NULL;
5507c478bd9Sstevel@tonic-gate 	char lpfile[BUFSIZ];
5517c478bd9Sstevel@tonic-gate 
5527c478bd9Sstevel@tonic-gate 	if ((svc == NULL) || (printer == NULL) || (files == NULL) ||
5537c478bd9Sstevel@tonic-gate 	    (job == NULL))
5547c478bd9Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
5557c478bd9Sstevel@tonic-gate 
5567c478bd9Sstevel@tonic-gate 	if (job_ticket != NULL)
5577c478bd9Sstevel@tonic-gate 		return (PAPI_OPERATION_NOT_SUPPORTED);
5587c478bd9Sstevel@tonic-gate 
5597c478bd9Sstevel@tonic-gate 	if (files != NULL)
56095c2d302SJonathan Cowper-Andrewes 		for (file_no = 0; files[file_no] != NULL; file_no++) {
5617c478bd9Sstevel@tonic-gate 			if (access(files[file_no], R_OK) < 0) {
5627c478bd9Sstevel@tonic-gate 				detailed_error(svc,
5637c478bd9Sstevel@tonic-gate 				    gettext("Cannot access file: %s: %s"),
5647c478bd9Sstevel@tonic-gate 				    files[file_no], strerror(errno));
5657c478bd9Sstevel@tonic-gate 				return (PAPI_BAD_ARGUMENT);
5667c478bd9Sstevel@tonic-gate 			}
567a5669307SJonathan Cowper-Andrewes 			if (stat(files[file_no], &statbuf) < 0) {
568a5669307SJonathan Cowper-Andrewes 				detailed_error(svc,
569a5669307SJonathan Cowper-Andrewes 				    gettext("Cannot access file: %s: %s"),
570a5669307SJonathan Cowper-Andrewes 				    files[file_no], strerror(errno));
571a5669307SJonathan Cowper-Andrewes 				return (PAPI_DOCUMENT_ACCESS_ERROR);
572a5669307SJonathan Cowper-Andrewes 			}
57395c2d302SJonathan Cowper-Andrewes 			if (statbuf.st_size == 0) {
57495c2d302SJonathan Cowper-Andrewes 				detailed_error(svc,
57595c2d302SJonathan Cowper-Andrewes 				    gettext("Zero byte (empty) file: %s"),
57695c2d302SJonathan Cowper-Andrewes 				    files[file_no]);
57795c2d302SJonathan Cowper-Andrewes 				return (PAPI_BAD_ARGUMENT);
57895c2d302SJonathan Cowper-Andrewes 			}
57995c2d302SJonathan Cowper-Andrewes 		}
5807c478bd9Sstevel@tonic-gate 
5817c478bd9Sstevel@tonic-gate 	if ((*job = j = calloc(1, sizeof (*j))) == NULL)
5827c478bd9Sstevel@tonic-gate 		return (PAPI_TEMPORARY_ERROR);
5837c478bd9Sstevel@tonic-gate 
5847c478bd9Sstevel@tonic-gate 	/* file_no + 1 for the control file (-0) */
5857c478bd9Sstevel@tonic-gate 	status = lpsched_alloc_files(svc, file_no + 1, &request_id);
5867c478bd9Sstevel@tonic-gate 	if (status != PAPI_OK)
5877c478bd9Sstevel@tonic-gate 		return (status);
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate 	request = create_request(svc, (char *)printer,
5907c478bd9Sstevel@tonic-gate 	    (papi_attribute_t **)job_attributes);
5917c478bd9Sstevel@tonic-gate 
5927c478bd9Sstevel@tonic-gate 	for (i = 0; files[i] != NULL; i++) {
5937c478bd9Sstevel@tonic-gate 		papi_status_t status;
5947c478bd9Sstevel@tonic-gate 		snprintf(lpfile, sizeof (lpfile), "%s%s-%d",
5957c478bd9Sstevel@tonic-gate 		    "/var/spool/lp/temp/", request_id, i+1);
5967c478bd9Sstevel@tonic-gate 		status = copy_file(files[i], lpfile);
5977c478bd9Sstevel@tonic-gate 		if (status != PAPI_OK) {
5987c478bd9Sstevel@tonic-gate 			detailed_error(svc,
5997c478bd9Sstevel@tonic-gate 			    gettext("unable to copy: %s -> %s: %s"),
6007c478bd9Sstevel@tonic-gate 			    files[i], lpfile, strerror(errno));
6017c478bd9Sstevel@tonic-gate 				freerequest(request);
6027c478bd9Sstevel@tonic-gate 			return (PAPI_DEVICE_ERROR);
6037c478bd9Sstevel@tonic-gate 		}
6047c478bd9Sstevel@tonic-gate 		addlist(&(request->file_list), lpfile);
6057c478bd9Sstevel@tonic-gate 	}
6067c478bd9Sstevel@tonic-gate 
6077c478bd9Sstevel@tonic-gate #ifdef LP_USE_PAPI_ATTR
6087c478bd9Sstevel@tonic-gate 	/*
6097c478bd9Sstevel@tonic-gate 	 * store the job attributes in the PAPI job attribute file that was
6107c478bd9Sstevel@tonic-gate 	 * created by lpsched_alloc_files(), the attributes will then pass
6117c478bd9Sstevel@tonic-gate 	 * through lpsched and be given to the slow-filters and the printer's
6127c478bd9Sstevel@tonic-gate 	 * interface script to process them
6137c478bd9Sstevel@tonic-gate 	 */
6147c478bd9Sstevel@tonic-gate 	snprintf(lpfile, sizeof (lpfile), "%s%s-%s",
6157c478bd9Sstevel@tonic-gate 	    "/var/spool/lp/temp/", request_id, LP_PAPIATTRNAME);
6167c478bd9Sstevel@tonic-gate 	status = psm_copy_attrsToFile(job_attributes, lpfile);
6177c478bd9Sstevel@tonic-gate 	if (status != PAPI_OK) {
6187c478bd9Sstevel@tonic-gate 		detailed_error(svc, "unable to copy attributes to file: %s: %s",
6197c478bd9Sstevel@tonic-gate 		    lpfile, strerror(errno));
6207c478bd9Sstevel@tonic-gate 		return (PAPI_DEVICE_ERROR);
6217c478bd9Sstevel@tonic-gate 	}
6227c478bd9Sstevel@tonic-gate #endif
6237c478bd9Sstevel@tonic-gate 
6247c478bd9Sstevel@tonic-gate 	/* store the meta-data file */
6257c478bd9Sstevel@tonic-gate 	snprintf(lpfile, sizeof (lpfile), "%s-0", request_id);
6267c478bd9Sstevel@tonic-gate 	if (putrequest(lpfile, request) < 0) {
6277c478bd9Sstevel@tonic-gate 		detailed_error(svc, gettext("unable to save request: %s: %s"),
6287c478bd9Sstevel@tonic-gate 		    lpfile, strerror(errno));
6297c478bd9Sstevel@tonic-gate 		freerequest(request);
6307c478bd9Sstevel@tonic-gate 		return (PAPI_DEVICE_ERROR);
6317c478bd9Sstevel@tonic-gate 	}
6327c478bd9Sstevel@tonic-gate 
6337c478bd9Sstevel@tonic-gate 	status = lpsched_commit_job(svc, lpfile, &tmp);
6347c478bd9Sstevel@tonic-gate 	if (status != PAPI_OK) {
6357c478bd9Sstevel@tonic-gate 		unlink(lpfile);
6367c478bd9Sstevel@tonic-gate 		freerequest(request);
6377c478bd9Sstevel@tonic-gate 		return (status);
6387c478bd9Sstevel@tonic-gate 	}
6397c478bd9Sstevel@tonic-gate 
6407c478bd9Sstevel@tonic-gate 	lpsched_request_to_job_attributes(request, j);
6417c478bd9Sstevel@tonic-gate 	freerequest(request);
6427c478bd9Sstevel@tonic-gate 
6437c478bd9Sstevel@tonic-gate 	if ((c = strrchr(tmp, '-')) != NULL)
6447c478bd9Sstevel@tonic-gate 		c++;
6457c478bd9Sstevel@tonic-gate 	papiAttributeListAddInteger(&j->attributes, PAPI_ATTR_REPLACE,
6467c478bd9Sstevel@tonic-gate 	    "job-id", atoi(c));
6477c478bd9Sstevel@tonic-gate 	papiAttributeListAddString(&j->attributes, PAPI_ATTR_REPLACE,
6487c478bd9Sstevel@tonic-gate 	    "job-uri", tmp);
6497c478bd9Sstevel@tonic-gate 
6507c478bd9Sstevel@tonic-gate 	return (PAPI_OK);
6517c478bd9Sstevel@tonic-gate }
6527c478bd9Sstevel@tonic-gate 
6537c478bd9Sstevel@tonic-gate papi_status_t
654355b4669Sjacobs papiJobSubmitByReference(papi_service_t handle, char *printer,
655355b4669Sjacobs 		papi_attribute_t **job_attributes,
656355b4669Sjacobs 		papi_job_ticket_t *job_ticket,
657355b4669Sjacobs 		char **files, papi_job_t *job)
6587c478bd9Sstevel@tonic-gate {
6597c478bd9Sstevel@tonic-gate 	service_t *svc = handle;
66095c2d302SJonathan Cowper-Andrewes 	struct stat statbuf;
6617c478bd9Sstevel@tonic-gate 	job_t *j;
6627c478bd9Sstevel@tonic-gate 	int file_no;
6637c478bd9Sstevel@tonic-gate 	short status;
6647c478bd9Sstevel@tonic-gate 	char *request_id = NULL;
6657c478bd9Sstevel@tonic-gate 	REQUEST *request;
6667c478bd9Sstevel@tonic-gate 	char *c;
6677c478bd9Sstevel@tonic-gate 	char *tmp = NULL;
6687c478bd9Sstevel@tonic-gate 	char lpfile[BUFSIZ];
6697c478bd9Sstevel@tonic-gate 	char **file_list = NULL;
6707c478bd9Sstevel@tonic-gate 
6717c478bd9Sstevel@tonic-gate 	if ((svc == NULL) || (printer == NULL) || (files == NULL) ||
6727c478bd9Sstevel@tonic-gate 	    (job == NULL))
6737c478bd9Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
6747c478bd9Sstevel@tonic-gate 
6757c478bd9Sstevel@tonic-gate 	if (job_ticket != NULL)
6767c478bd9Sstevel@tonic-gate 		return (PAPI_OPERATION_NOT_SUPPORTED);
6777c478bd9Sstevel@tonic-gate 
6787c478bd9Sstevel@tonic-gate 	if (files != NULL)
6797c478bd9Sstevel@tonic-gate 		for (file_no = 0; files[file_no] != NULL; file_no++) {
6807c478bd9Sstevel@tonic-gate 			if (access(files[file_no], R_OK) < 0) {
6817c478bd9Sstevel@tonic-gate 				detailed_error(svc,
6827c478bd9Sstevel@tonic-gate 				    gettext("Cannot access file: %s: %s"),
6837c478bd9Sstevel@tonic-gate 				    files[file_no], strerror(errno));
684e2738c5eSjacobs 				return (PAPI_DOCUMENT_ACCESS_ERROR);
6857c478bd9Sstevel@tonic-gate 			}
686a5669307SJonathan Cowper-Andrewes 			if (stat(files[file_no], &statbuf) < 0) {
687a5669307SJonathan Cowper-Andrewes 				detailed_error(svc,
688a5669307SJonathan Cowper-Andrewes 				    gettext("Cannot access file: %s: %s"),
689a5669307SJonathan Cowper-Andrewes 				    files[file_no], strerror(errno));
690a5669307SJonathan Cowper-Andrewes 				return (PAPI_DOCUMENT_ACCESS_ERROR);
691a5669307SJonathan Cowper-Andrewes 			}
69295c2d302SJonathan Cowper-Andrewes 			if (statbuf.st_size == 0) {
69395c2d302SJonathan Cowper-Andrewes 				detailed_error(svc,
69495c2d302SJonathan Cowper-Andrewes 				    gettext("Zero byte (empty) file: %s"),
69595c2d302SJonathan Cowper-Andrewes 				    files[file_no]);
69695c2d302SJonathan Cowper-Andrewes 				return (PAPI_BAD_ARGUMENT);
69795c2d302SJonathan Cowper-Andrewes 			}
69895c2d302SJonathan Cowper-Andrewes 
699e2738c5eSjacobs 			if (files[file_no][0] != '/') {
700e2738c5eSjacobs 				char path[MAXPATHLEN];
701e2738c5eSjacobs 
702e2738c5eSjacobs 				if (getcwd(path, sizeof (path)) == NULL) {
703e2738c5eSjacobs 					detailed_error(svc, gettext(
704e2738c5eSjacobs 					    "getcwd for file: %s: %s"),
705e2738c5eSjacobs 					    files[file_no],
706e2738c5eSjacobs 					    strerror(errno));
707e2738c5eSjacobs 					return (PAPI_DOCUMENT_ACCESS_ERROR);
708e2738c5eSjacobs 				}
709e2738c5eSjacobs 				strlcat(path, "/", sizeof (path));
710e2738c5eSjacobs 				if (strlcat(path, files[file_no], sizeof (path))
711e2738c5eSjacobs 				    >= sizeof (path)) {
712e2738c5eSjacobs 					detailed_error(svc, gettext(
713e2738c5eSjacobs 					    "pathname too long: %s"),
714e2738c5eSjacobs 					    files[file_no]);
715e2738c5eSjacobs 					return (PAPI_DOCUMENT_ACCESS_ERROR);
716e2738c5eSjacobs 				}
717e2738c5eSjacobs 				addlist(&file_list, path);
718e2738c5eSjacobs 			} else
7197c478bd9Sstevel@tonic-gate 				addlist(&file_list, (char *)files[file_no]);
7207c478bd9Sstevel@tonic-gate 		}
7217c478bd9Sstevel@tonic-gate 
7227c478bd9Sstevel@tonic-gate 	if ((*job = j = calloc(1, sizeof (*j))) == NULL)
7237c478bd9Sstevel@tonic-gate 		return (PAPI_TEMPORARY_ERROR);
7247c478bd9Sstevel@tonic-gate 
7257c478bd9Sstevel@tonic-gate 	/* 1 for the control file (-0) */
7267c478bd9Sstevel@tonic-gate 	status = lpsched_alloc_files(svc, 1, &request_id);
7277c478bd9Sstevel@tonic-gate 	if (status != PAPI_OK)
7287c478bd9Sstevel@tonic-gate 		return (status);
7297c478bd9Sstevel@tonic-gate 
7307c478bd9Sstevel@tonic-gate 	request = create_request(svc, (char *)printer,
7317c478bd9Sstevel@tonic-gate 	    (papi_attribute_t **)job_attributes);
7327c478bd9Sstevel@tonic-gate 	request->file_list = file_list;
7337c478bd9Sstevel@tonic-gate 
7347c478bd9Sstevel@tonic-gate #ifdef LP_USE_PAPI_ATTR
7357c478bd9Sstevel@tonic-gate 	/*
7367c478bd9Sstevel@tonic-gate 	 * store the job attributes in the PAPI job attribute file that was
7377c478bd9Sstevel@tonic-gate 	 * created by lpsched_alloc_files(), the attributes will then pass
7387c478bd9Sstevel@tonic-gate 	 * through lpsched and be given to the slow-filters and the printer's
7397c478bd9Sstevel@tonic-gate 	 * interface script to process them
7407c478bd9Sstevel@tonic-gate 	 */
7417c478bd9Sstevel@tonic-gate 	snprintf(lpfile, sizeof (lpfile), "%s%s-%s",
7427c478bd9Sstevel@tonic-gate 	    "/var/spool/lp/temp/", request_id, LP_PAPIATTRNAME);
7437c478bd9Sstevel@tonic-gate 	status = psm_copy_attrsToFile(job_attributes, lpfile);
7447c478bd9Sstevel@tonic-gate 	if (status != PAPI_OK) {
7457c478bd9Sstevel@tonic-gate 		detailed_error(svc, "unable to copy attributes to file: %s: %s",
7467c478bd9Sstevel@tonic-gate 		    lpfile, strerror(errno));
7477c478bd9Sstevel@tonic-gate 		return (PAPI_DEVICE_ERROR);
7487c478bd9Sstevel@tonic-gate 	}
7497c478bd9Sstevel@tonic-gate #endif
7507c478bd9Sstevel@tonic-gate 
7517c478bd9Sstevel@tonic-gate 	/* store the meta-data file */
7527c478bd9Sstevel@tonic-gate 	snprintf(lpfile, sizeof (lpfile), "%s-0", request_id);
7537c478bd9Sstevel@tonic-gate 	if (putrequest(lpfile, request) < 0) {
7547c478bd9Sstevel@tonic-gate 		detailed_error(svc, gettext("unable to save request: %s: %s"),
7557c478bd9Sstevel@tonic-gate 		    lpfile, strerror(errno));
7567c478bd9Sstevel@tonic-gate 		freerequest(request);
7577c478bd9Sstevel@tonic-gate 		return (PAPI_DEVICE_ERROR);
7587c478bd9Sstevel@tonic-gate 	}
7597c478bd9Sstevel@tonic-gate 
7607c478bd9Sstevel@tonic-gate 	status = lpsched_commit_job(svc, lpfile, &tmp);
7617c478bd9Sstevel@tonic-gate 	if (status != PAPI_OK) {
7627c478bd9Sstevel@tonic-gate 		unlink(lpfile);
7637c478bd9Sstevel@tonic-gate 		freerequest(request);
7647c478bd9Sstevel@tonic-gate 		return (status);
7657c478bd9Sstevel@tonic-gate 	}
7667c478bd9Sstevel@tonic-gate 
7677c478bd9Sstevel@tonic-gate 	lpsched_request_to_job_attributes(request, j);
7687c478bd9Sstevel@tonic-gate 
7697c478bd9Sstevel@tonic-gate 	freerequest(request);
7707c478bd9Sstevel@tonic-gate 
7717c478bd9Sstevel@tonic-gate 	if ((c = strrchr(tmp, '-')) != NULL)
7727c478bd9Sstevel@tonic-gate 		c++;
7737c478bd9Sstevel@tonic-gate 	papiAttributeListAddInteger(&j->attributes, PAPI_ATTR_REPLACE,
7747c478bd9Sstevel@tonic-gate 	    "job-id", atoi(c));
7757c478bd9Sstevel@tonic-gate 	papiAttributeListAddString(&j->attributes, PAPI_ATTR_REPLACE,
7767c478bd9Sstevel@tonic-gate 	    "job-uri", tmp);
7777c478bd9Sstevel@tonic-gate 
7787c478bd9Sstevel@tonic-gate 	return (PAPI_OK);
7797c478bd9Sstevel@tonic-gate }
7807c478bd9Sstevel@tonic-gate 
7817c478bd9Sstevel@tonic-gate papi_status_t
782355b4669Sjacobs papiJobValidate(papi_service_t handle, char *printer,
783355b4669Sjacobs 		papi_attribute_t **job_attributes,
784355b4669Sjacobs 		papi_job_ticket_t *job_ticket,
785355b4669Sjacobs 		char **files, papi_job_t *job)
7867c478bd9Sstevel@tonic-gate {
7877c478bd9Sstevel@tonic-gate 	papi_status_t status;
7887c478bd9Sstevel@tonic-gate 	papi_attribute_t **attributes = NULL;
7897c478bd9Sstevel@tonic-gate 	int i;
7907c478bd9Sstevel@tonic-gate 
7917c478bd9Sstevel@tonic-gate 	papiAttributeListAddString(&attributes, PAPI_ATTR_REPLACE,
79243b9c050Sjacobs 	    "job-hold-until", "indefinite");
7937c478bd9Sstevel@tonic-gate 	for (i = 0; job_attributes[i]; i++)
7947c478bd9Sstevel@tonic-gate 		list_append(&attributes, job_attributes[i]);
7957c478bd9Sstevel@tonic-gate 
7967c478bd9Sstevel@tonic-gate 	status = papiJobSubmitByReference(handle, printer,
797355b4669Sjacobs 	    (papi_attribute_t **)attributes,
7987c478bd9Sstevel@tonic-gate 	    job_ticket, files, job);
7997c478bd9Sstevel@tonic-gate 	if (status == PAPI_OK) {
8007c478bd9Sstevel@tonic-gate 		int id = papiJobGetId(*job);
8017c478bd9Sstevel@tonic-gate 
8027c478bd9Sstevel@tonic-gate 		if (id != -1)
8037c478bd9Sstevel@tonic-gate 			papiJobCancel(handle, printer, id);
8047c478bd9Sstevel@tonic-gate 	}
8057c478bd9Sstevel@tonic-gate 
8067c478bd9Sstevel@tonic-gate 	attributes[1] = NULL;	/* after attr[0], they are in another list */
8077c478bd9Sstevel@tonic-gate 	papiAttributeListFree(attributes);
8087c478bd9Sstevel@tonic-gate 
8097c478bd9Sstevel@tonic-gate 	return (status);
8107c478bd9Sstevel@tonic-gate }
8117c478bd9Sstevel@tonic-gate 
8127c478bd9Sstevel@tonic-gate papi_status_t
813355b4669Sjacobs papiJobStreamOpen(papi_service_t handle, char *printer,
814355b4669Sjacobs 		papi_attribute_t **job_attributes,
815355b4669Sjacobs 		papi_job_ticket_t *job_ticket, papi_stream_t *stream)
8167c478bd9Sstevel@tonic-gate {
8177c478bd9Sstevel@tonic-gate 	papi_status_t status;
8187c478bd9Sstevel@tonic-gate 	service_t *svc = handle;
8197c478bd9Sstevel@tonic-gate 	job_stream_t *s = NULL;
8207c478bd9Sstevel@tonic-gate 	char *request_id = NULL;
8217c478bd9Sstevel@tonic-gate 	char lpfile[BUFSIZ];
8227c478bd9Sstevel@tonic-gate 
8237c478bd9Sstevel@tonic-gate 	if ((svc == NULL) || (printer == NULL) || (stream == NULL))
8247c478bd9Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
8257c478bd9Sstevel@tonic-gate 
8267c478bd9Sstevel@tonic-gate 	if (job_ticket != NULL)
8277c478bd9Sstevel@tonic-gate 		return (PAPI_OPERATION_NOT_SUPPORTED);
8287c478bd9Sstevel@tonic-gate 
8297c478bd9Sstevel@tonic-gate 	if ((*stream = s = calloc(1, sizeof (*s))) == NULL)
8307c478bd9Sstevel@tonic-gate 		return (PAPI_TEMPORARY_ERROR);
8317c478bd9Sstevel@tonic-gate 
8327c478bd9Sstevel@tonic-gate 	/* 1 for data, 1 for the meta-data (-0) */
8337c478bd9Sstevel@tonic-gate 	status = lpsched_alloc_files(svc, 2, &request_id);
8347c478bd9Sstevel@tonic-gate 	if (status != PAPI_OK)
8357c478bd9Sstevel@tonic-gate 		return (status);
8367c478bd9Sstevel@tonic-gate 
837*bb698c4dSsonam gupta - Sun Microsystems - Bangalore India 	papiAttributeListAddString(&job_attributes, PAPI_ATTR_EXCL,
838*bb698c4dSsonam gupta - Sun Microsystems - Bangalore India 	    "job-name", "standard input");
839*bb698c4dSsonam gupta - Sun Microsystems - Bangalore India 
8407c478bd9Sstevel@tonic-gate 	s->request = create_request(svc, (char *)printer,
8417c478bd9Sstevel@tonic-gate 	    (papi_attribute_t **)job_attributes);
8427c478bd9Sstevel@tonic-gate 	snprintf(lpfile, sizeof (lpfile), "/var/spool/lp/temp/%s-1",
8437c478bd9Sstevel@tonic-gate 	    request_id);
8447c478bd9Sstevel@tonic-gate 	s->fd = open(lpfile, O_WRONLY);
8457c478bd9Sstevel@tonic-gate 	addlist(&(s->request->file_list), lpfile);
8467c478bd9Sstevel@tonic-gate 
8477c478bd9Sstevel@tonic-gate #ifdef LP_USE_PAPI_ATTR
8487c478bd9Sstevel@tonic-gate 	/*
8497c478bd9Sstevel@tonic-gate 	 * store the job attributes in the PAPI job attribute file that was
8507c478bd9Sstevel@tonic-gate 	 * created by lpsched_alloc_files(), the attributes will then pass
8517c478bd9Sstevel@tonic-gate 	 * through lpsched and be given to the slow-filters and the printer's
8527c478bd9Sstevel@tonic-gate 	 * interface script to process them
8537c478bd9Sstevel@tonic-gate 	 */
8547c478bd9Sstevel@tonic-gate 	snprintf(lpfile, sizeof (lpfile), "%s%s-%s",
8557c478bd9Sstevel@tonic-gate 	    "/var/spool/lp/temp/", request_id, LP_PAPIATTRNAME);
8567c478bd9Sstevel@tonic-gate 	status = psm_copy_attrsToFile(job_attributes, lpfile);
8577c478bd9Sstevel@tonic-gate 	if (status != PAPI_OK) {
8587c478bd9Sstevel@tonic-gate 		detailed_error(svc, "unable to copy attributes to file: %s: %s",
8597c478bd9Sstevel@tonic-gate 		    lpfile, strerror(errno));
8607c478bd9Sstevel@tonic-gate 		close(s->fd);
8617c478bd9Sstevel@tonic-gate 		free(s);
8627c478bd9Sstevel@tonic-gate 		return (PAPI_DEVICE_ERROR);
8637c478bd9Sstevel@tonic-gate 	}
8647c478bd9Sstevel@tonic-gate #endif
8657c478bd9Sstevel@tonic-gate 
8667c478bd9Sstevel@tonic-gate 	/* store the meta-data file */
8677c478bd9Sstevel@tonic-gate 	snprintf(lpfile, sizeof (lpfile), "%s-0", request_id);
8687c478bd9Sstevel@tonic-gate 	s->meta_data_file = strdup(lpfile);
8697c478bd9Sstevel@tonic-gate 	if (putrequest(lpfile, s->request) < 0) {
8707c478bd9Sstevel@tonic-gate 		detailed_error(svc, gettext("unable to save request: %s: %s"),
8717c478bd9Sstevel@tonic-gate 		    lpfile, strerror(errno));
8727c478bd9Sstevel@tonic-gate 		s->request = NULL;
8737c478bd9Sstevel@tonic-gate 		return (PAPI_DEVICE_ERROR);
8747c478bd9Sstevel@tonic-gate 	}
8757c478bd9Sstevel@tonic-gate 
8767c478bd9Sstevel@tonic-gate 	return (PAPI_OK);
8777c478bd9Sstevel@tonic-gate }
8787c478bd9Sstevel@tonic-gate 
8797c478bd9Sstevel@tonic-gate papi_status_t
8807c478bd9Sstevel@tonic-gate papiJobStreamWrite(papi_service_t handle,
881355b4669Sjacobs 		papi_stream_t stream, void *buffer, size_t buflen)
8827c478bd9Sstevel@tonic-gate {
8837c478bd9Sstevel@tonic-gate 	service_t *svc = handle;
8847c478bd9Sstevel@tonic-gate 	job_stream_t *s = stream;
8857c478bd9Sstevel@tonic-gate 
8867c478bd9Sstevel@tonic-gate 	if ((svc == NULL) || (stream == NULL) || (buffer == NULL))
8877c478bd9Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
8887c478bd9Sstevel@tonic-gate 
8897c478bd9Sstevel@tonic-gate 	if (write(s->fd, buffer, buflen) != buflen)
8907c478bd9Sstevel@tonic-gate 		return (PAPI_DEVICE_ERROR);
8917c478bd9Sstevel@tonic-gate 
8927c478bd9Sstevel@tonic-gate 	return (PAPI_OK);
8937c478bd9Sstevel@tonic-gate }
8947c478bd9Sstevel@tonic-gate papi_status_t
8957c478bd9Sstevel@tonic-gate papiJobStreamClose(papi_service_t handle,
8967c478bd9Sstevel@tonic-gate 		papi_stream_t stream, papi_job_t *job)
8977c478bd9Sstevel@tonic-gate {
8987c478bd9Sstevel@tonic-gate 	papi_status_t status = PAPI_OK;
8997c478bd9Sstevel@tonic-gate 	service_t *svc = handle;
9007c478bd9Sstevel@tonic-gate 	job_stream_t *s = stream;
9017c478bd9Sstevel@tonic-gate 	job_t *j = NULL;
9027c478bd9Sstevel@tonic-gate 	char *tmp = NULL, *c;
9037c478bd9Sstevel@tonic-gate 
9047c478bd9Sstevel@tonic-gate 	if ((svc == NULL) || (stream == NULL) || (job == NULL))
9057c478bd9Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
9067c478bd9Sstevel@tonic-gate 
9077c478bd9Sstevel@tonic-gate 	if ((*job = j = calloc(1, sizeof (*j))) == NULL)
9087c478bd9Sstevel@tonic-gate 		return (PAPI_TEMPORARY_ERROR);
9097c478bd9Sstevel@tonic-gate 
9107c478bd9Sstevel@tonic-gate 	close(s->fd);
9117c478bd9Sstevel@tonic-gate 
9127c478bd9Sstevel@tonic-gate 	lpsched_request_to_job_attributes(s->request, j);
9137c478bd9Sstevel@tonic-gate 
9147c478bd9Sstevel@tonic-gate 	if (s->meta_data_file != NULL) {
9157c478bd9Sstevel@tonic-gate 		status = lpsched_commit_job(svc, s->meta_data_file, &tmp);
9167c478bd9Sstevel@tonic-gate 		if (status != PAPI_OK) {
9177c478bd9Sstevel@tonic-gate 			unlink(s->meta_data_file);
9187c478bd9Sstevel@tonic-gate 			return (status);
9197c478bd9Sstevel@tonic-gate 		}
9207c478bd9Sstevel@tonic-gate 		if ((c = strrchr(tmp, '-')) != NULL)
9217c478bd9Sstevel@tonic-gate 			c++;
9227c478bd9Sstevel@tonic-gate 		papiAttributeListAddInteger(&j->attributes, PAPI_ATTR_REPLACE,
9237c478bd9Sstevel@tonic-gate 		    "job-id", atoi(c));
9247c478bd9Sstevel@tonic-gate 		papiAttributeListAddString(&j->attributes, PAPI_ATTR_REPLACE,
9257c478bd9Sstevel@tonic-gate 		    "job-uri", tmp);
9267c478bd9Sstevel@tonic-gate 		free(s->meta_data_file);
9277c478bd9Sstevel@tonic-gate 	}
928c1ecd8b9Sjacobs 	freerequest(s->request);
9297c478bd9Sstevel@tonic-gate 	free(s);
9307c478bd9Sstevel@tonic-gate 
9317c478bd9Sstevel@tonic-gate 	return (PAPI_OK);
9327c478bd9Sstevel@tonic-gate }
9337c478bd9Sstevel@tonic-gate 
9347c478bd9Sstevel@tonic-gate papi_status_t
935355b4669Sjacobs papiJobQuery(papi_service_t handle, char *printer, int32_t job_id,
936355b4669Sjacobs 		char **requested_attrs,
9377c478bd9Sstevel@tonic-gate 		papi_job_t *job)
9387c478bd9Sstevel@tonic-gate {
9397c478bd9Sstevel@tonic-gate 	service_t *svc = handle;
9407c478bd9Sstevel@tonic-gate 	job_t *j;
9417c478bd9Sstevel@tonic-gate 	char *dest;
9427c478bd9Sstevel@tonic-gate 	char req_id[32];
9437c478bd9Sstevel@tonic-gate 	short rc;
9447c478bd9Sstevel@tonic-gate 	char *form = NULL,
9457c478bd9Sstevel@tonic-gate 	    *request_id = NULL,
9467c478bd9Sstevel@tonic-gate 	    *charset = NULL,
9477c478bd9Sstevel@tonic-gate 	    *user = NULL,
94845916cd2Sjpk 	    *slabel = NULL,
9497c478bd9Sstevel@tonic-gate 	    *file = NULL;
9507c478bd9Sstevel@tonic-gate 	time_t date = 0;
9517c478bd9Sstevel@tonic-gate 	size_t size = 0;
9527c478bd9Sstevel@tonic-gate 	short  rank = 0,
9537c478bd9Sstevel@tonic-gate 	    state = 0;
9547c478bd9Sstevel@tonic-gate 
9557c478bd9Sstevel@tonic-gate 	if ((handle == NULL) || (printer == NULL) || (job_id < 0))
9567c478bd9Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
9577c478bd9Sstevel@tonic-gate 
9587c478bd9Sstevel@tonic-gate 	dest = printer_name_from_uri_id(printer, job_id);
9597c478bd9Sstevel@tonic-gate 	snprintf(req_id, sizeof (req_id), "%s-%d", dest, job_id);
9607c478bd9Sstevel@tonic-gate 	free(dest);
9617c478bd9Sstevel@tonic-gate 
9627c478bd9Sstevel@tonic-gate 	rc = snd_msg(svc, S_INQUIRE_REQUEST_RANK, 0, "", "", req_id, "", "");
9637c478bd9Sstevel@tonic-gate 	if (rc < 0)
9647c478bd9Sstevel@tonic-gate 		return (PAPI_SERVICE_UNAVAILABLE);
9657c478bd9Sstevel@tonic-gate 
9667c478bd9Sstevel@tonic-gate 	if (rcv_msg(svc, R_INQUIRE_REQUEST_RANK, &rc, &request_id,
96745916cd2Sjpk 	    &user, &slabel, &size, &date, &state, &dest, &form,
9687c478bd9Sstevel@tonic-gate 	    &charset, &rank, &file) < 0) {
9697c478bd9Sstevel@tonic-gate 		detailed_error(svc,
9707c478bd9Sstevel@tonic-gate 		    gettext("failed to read response from scheduler"));
9717c478bd9Sstevel@tonic-gate 		return (PAPI_DEVICE_ERROR);
9727c478bd9Sstevel@tonic-gate 	}
9737c478bd9Sstevel@tonic-gate 
9747c478bd9Sstevel@tonic-gate 	if ((request_id == NULL) || (request_id[0] == NULL))
9757c478bd9Sstevel@tonic-gate 		return (PAPI_NOT_FOUND);
9767c478bd9Sstevel@tonic-gate 
9777c478bd9Sstevel@tonic-gate 	if ((*job = j = calloc(1, sizeof (*j))) == NULL)
9787c478bd9Sstevel@tonic-gate 		return (PAPI_TEMPORARY_ERROR);
9797c478bd9Sstevel@tonic-gate 
9807c478bd9Sstevel@tonic-gate 	snprintf(req_id, sizeof (req_id), "%d-0", job_id);
9817c478bd9Sstevel@tonic-gate 	lpsched_read_job_configuration(svc, j, req_id);
9827c478bd9Sstevel@tonic-gate 
983e061a5d7SGowtham Thommandra 	job_status_to_attributes(j, request_id, user, slabel, size, date, state,
984e061a5d7SGowtham Thommandra 	    dest, form, charset, rank, file);
985e061a5d7SGowtham Thommandra 
9867c478bd9Sstevel@tonic-gate 	return (PAPI_OK);
9877c478bd9Sstevel@tonic-gate }
9887c478bd9Sstevel@tonic-gate 
9897c478bd9Sstevel@tonic-gate papi_status_t
990355b4669Sjacobs papiJobMove(papi_service_t handle, char *printer, int32_t job_id,
991355b4669Sjacobs 		char *destination)
992355b4669Sjacobs {
993355b4669Sjacobs 	papi_status_t result = PAPI_OK;
994e8e36937S"Nagaraj Yedathore - Sun Microsystems - Bangalore India" 	long bits;
995355b4669Sjacobs 	service_t *svc = handle;
996355b4669Sjacobs 	char req_id[64];
997355b4669Sjacobs 	char *queue;
998355b4669Sjacobs 	char *user = NULL;
999355b4669Sjacobs 
1000355b4669Sjacobs 	if ((svc == NULL) || (printer == NULL) || (job_id < 0) ||
1001355b4669Sjacobs 	    (destination == NULL))
1002355b4669Sjacobs 		return (PAPI_BAD_ARGUMENT);
1003355b4669Sjacobs 
1004355b4669Sjacobs 	queue = printer_name_from_uri_id(printer, job_id);
1005355b4669Sjacobs 	snprintf(req_id, sizeof (req_id), "%s-%d", queue, job_id);
1006355b4669Sjacobs 	free(queue);
1007355b4669Sjacobs 
1008355b4669Sjacobs 	if (papiAttributeListGetString(svc->attributes, NULL, "user-name",
1009355b4669Sjacobs 	    &user) == PAPI_OK) {
1010355b4669Sjacobs 		REQUEST *r = getrequest(req_id);
1011355b4669Sjacobs 
1012355b4669Sjacobs 		if ((r != NULL) && (r->user != NULL) &&
1013355b4669Sjacobs 		    (strcmp(r->user, user) != 0))
1014355b4669Sjacobs 			result = PAPI_NOT_AUTHORIZED;
1015355b4669Sjacobs 		freerequest(r);
1016355b4669Sjacobs 	}
1017355b4669Sjacobs 
1018355b4669Sjacobs 	if (result == PAPI_OK) {
1019355b4669Sjacobs 		short status = MOK;
1020355b4669Sjacobs 		char *dest = printer_name_from_uri_id(destination, -1);
1021355b4669Sjacobs 
1022355b4669Sjacobs 		if ((snd_msg(svc, S_MOVE_REQUEST, req_id, dest) < 0) ||
1023e8e36937S"Nagaraj Yedathore - Sun Microsystems - Bangalore India" 		    (rcv_msg(svc, R_MOVE_REQUEST, &status, &bits) < 0))
1024355b4669Sjacobs 			status = MTRANSMITERR;
1025355b4669Sjacobs 
1026355b4669Sjacobs 		free(dest);
1027355b4669Sjacobs 
1028355b4669Sjacobs 		result = lpsched_status_to_papi_status(status);
1029355b4669Sjacobs 	}
1030355b4669Sjacobs 
1031355b4669Sjacobs 	return (result);
1032355b4669Sjacobs }
1033355b4669Sjacobs 
1034355b4669Sjacobs papi_status_t
1035355b4669Sjacobs papiJobCancel(papi_service_t handle, char *printer, int32_t job_id)
10367c478bd9Sstevel@tonic-gate {
10377c478bd9Sstevel@tonic-gate 	papi_status_t result = PAPI_OK;
10387c478bd9Sstevel@tonic-gate 	service_t *svc = handle;
10397c478bd9Sstevel@tonic-gate 	char req_id[64];
10407c478bd9Sstevel@tonic-gate 	char *dest;
10417c478bd9Sstevel@tonic-gate 	char *user = NULL;
10427c478bd9Sstevel@tonic-gate 
10437c478bd9Sstevel@tonic-gate 	if ((svc == NULL) || (printer == NULL) || (job_id < 0))
10447c478bd9Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
10457c478bd9Sstevel@tonic-gate 
10467c478bd9Sstevel@tonic-gate 	dest = printer_name_from_uri_id(printer, job_id);
10477c478bd9Sstevel@tonic-gate 	snprintf(req_id, sizeof (req_id), "%s-%d", dest, job_id);
10487c478bd9Sstevel@tonic-gate 	free(dest);
10497c478bd9Sstevel@tonic-gate 
10507c478bd9Sstevel@tonic-gate 	if (papiAttributeListGetString(svc->attributes, NULL, "user-name",
10517c478bd9Sstevel@tonic-gate 	    &user) == PAPI_OK) {
10527c478bd9Sstevel@tonic-gate 		REQUEST *r = getrequest(req_id);
10537c478bd9Sstevel@tonic-gate 
10546b5764c3Ssonam gupta - Sun Microsystems - Bangalore India 		if ((result = authorized(handle, job_id)) != PAPI_OK)
10556b5764c3Ssonam gupta - Sun Microsystems - Bangalore India 			result = PAPI_NOT_AUTHORIZED;
10566b5764c3Ssonam gupta - Sun Microsystems - Bangalore India 
1057355b4669Sjacobs 		if ((r != NULL) && (r->user != NULL) &&
1058355b4669Sjacobs 		    (strcmp(r->user, user) != 0))
10597c478bd9Sstevel@tonic-gate 			result = PAPI_NOT_AUTHORIZED;
10607c478bd9Sstevel@tonic-gate 		freerequest(r);
10617c478bd9Sstevel@tonic-gate 	}
10627c478bd9Sstevel@tonic-gate 
10637c478bd9Sstevel@tonic-gate 	if (result == PAPI_OK) {
10647c478bd9Sstevel@tonic-gate 		short status = MOK;
10657c478bd9Sstevel@tonic-gate 
10667c478bd9Sstevel@tonic-gate 		if ((snd_msg(svc, S_CANCEL_REQUEST, req_id) < 0) ||
10677c478bd9Sstevel@tonic-gate 		    (rcv_msg(svc, R_CANCEL_REQUEST, &status) < 0))
10687c478bd9Sstevel@tonic-gate 			status = MTRANSMITERR;
10697c478bd9Sstevel@tonic-gate 
10707c478bd9Sstevel@tonic-gate 		result = lpsched_status_to_papi_status(status);
10717c478bd9Sstevel@tonic-gate 	}
10727c478bd9Sstevel@tonic-gate 
10737c478bd9Sstevel@tonic-gate 	return (result);
10747c478bd9Sstevel@tonic-gate }
10757c478bd9Sstevel@tonic-gate 
10767c478bd9Sstevel@tonic-gate papi_status_t
1077355b4669Sjacobs hold_release_job(papi_service_t handle, char *printer,
1078355b4669Sjacobs 		int32_t job_id, int flag)
10797c478bd9Sstevel@tonic-gate {
10807c478bd9Sstevel@tonic-gate 	papi_status_t status;
10817c478bd9Sstevel@tonic-gate 	service_t *svc = handle;
10827c478bd9Sstevel@tonic-gate 	REQUEST *r = NULL;
10837c478bd9Sstevel@tonic-gate 	char *file;
10847c478bd9Sstevel@tonic-gate 	char *dest;
10857c478bd9Sstevel@tonic-gate 
10867c478bd9Sstevel@tonic-gate 	if ((svc == NULL) || (printer == NULL) || (job_id < 0))
10877c478bd9Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
10887c478bd9Sstevel@tonic-gate 
10897c478bd9Sstevel@tonic-gate 	if ((status = authorized(svc, job_id)) != PAPI_OK)
10907c478bd9Sstevel@tonic-gate 		return (status);
10917c478bd9Sstevel@tonic-gate 
10927c478bd9Sstevel@tonic-gate 	dest = printer_name_from_uri_id(printer, job_id);
10937c478bd9Sstevel@tonic-gate 	status = lpsched_start_change(svc, dest, job_id, &file);
10947c478bd9Sstevel@tonic-gate 	if (status != PAPI_OK)
10957c478bd9Sstevel@tonic-gate 		return (status);
10967c478bd9Sstevel@tonic-gate 
10977c478bd9Sstevel@tonic-gate 	if ((r = getrequest(file)) != NULL) {
10987c478bd9Sstevel@tonic-gate 		r->actions &= ~ACT_RESUME;
1099355b4669Sjacobs 		switch (flag) {
1100355b4669Sjacobs 		case 0:
11017c478bd9Sstevel@tonic-gate 			r->actions |= ACT_HOLD;
1102355b4669Sjacobs 			break;
1103355b4669Sjacobs 		case 1:
11047c478bd9Sstevel@tonic-gate 			r->actions |= ACT_RESUME;
1105355b4669Sjacobs 			break;
1106355b4669Sjacobs 		case 2:
1107355b4669Sjacobs 			r->actions |= ACT_IMMEDIATE;
1108355b4669Sjacobs 			break;
1109355b4669Sjacobs 		}
11107c478bd9Sstevel@tonic-gate 		if (putrequest(file, r) < 0) {
11117c478bd9Sstevel@tonic-gate 			detailed_error(svc,
11127c478bd9Sstevel@tonic-gate 			    gettext("failed to write job: %s: %s"),
11137c478bd9Sstevel@tonic-gate 			    file, strerror(errno));
1114355b4669Sjacobs 			freerequest(r);
11157c478bd9Sstevel@tonic-gate 			return (PAPI_DEVICE_ERROR);
11167c478bd9Sstevel@tonic-gate 		}
1117355b4669Sjacobs 		freerequest(r);
11187c478bd9Sstevel@tonic-gate 	} else {
11197c478bd9Sstevel@tonic-gate 		detailed_error(svc, gettext("failed to read job: %s: %s"),
11207c478bd9Sstevel@tonic-gate 		    file, strerror(errno));
11217c478bd9Sstevel@tonic-gate 		return (PAPI_DEVICE_ERROR);
11227c478bd9Sstevel@tonic-gate 	}
11237c478bd9Sstevel@tonic-gate 
11247c478bd9Sstevel@tonic-gate 	status = lpsched_end_change(svc, dest, job_id);
11257c478bd9Sstevel@tonic-gate 
11267c478bd9Sstevel@tonic-gate 	return (status);
11277c478bd9Sstevel@tonic-gate }
11287c478bd9Sstevel@tonic-gate 
11297c478bd9Sstevel@tonic-gate papi_status_t
1130355b4669Sjacobs papiJobHold(papi_service_t handle, char *printer, int32_t job_id)
11317c478bd9Sstevel@tonic-gate {
11327c478bd9Sstevel@tonic-gate 	return (hold_release_job(handle, printer, job_id, 0));
11337c478bd9Sstevel@tonic-gate }
11347c478bd9Sstevel@tonic-gate 
11357c478bd9Sstevel@tonic-gate papi_status_t
1136355b4669Sjacobs papiJobRelease(papi_service_t handle, char *printer, int32_t job_id)
11377c478bd9Sstevel@tonic-gate {
11387c478bd9Sstevel@tonic-gate 	return (hold_release_job(handle, printer, job_id, 1));
11397c478bd9Sstevel@tonic-gate }
11407c478bd9Sstevel@tonic-gate 
11417c478bd9Sstevel@tonic-gate papi_status_t
1142355b4669Sjacobs papiJobPromote(papi_service_t handle, char *printer, int32_t job_id)
11437c478bd9Sstevel@tonic-gate {
1144355b4669Sjacobs 	return (hold_release_job(handle, printer, job_id, 2));
11457c478bd9Sstevel@tonic-gate }
11467c478bd9Sstevel@tonic-gate 
11477c478bd9Sstevel@tonic-gate papi_status_t
1148355b4669Sjacobs papiJobModify(papi_service_t handle, char *printer, int32_t job_id,
1149355b4669Sjacobs 		papi_attribute_t **attributes, papi_job_t *job)
11507c478bd9Sstevel@tonic-gate {
11517c478bd9Sstevel@tonic-gate 	papi_status_t status;
11527c478bd9Sstevel@tonic-gate 	job_t *j = NULL;
11537c478bd9Sstevel@tonic-gate 	service_t *svc = handle;
11547c478bd9Sstevel@tonic-gate 	char *file = NULL;
11557c478bd9Sstevel@tonic-gate 	char *dest;
11567c478bd9Sstevel@tonic-gate 	REQUEST *r = NULL;
11577c478bd9Sstevel@tonic-gate 	char lpfile[BUFSIZ];
1158324104abSsonam gupta - Sun Microsystems - Bangalore India 	int32_t job_id_actual;
11597c478bd9Sstevel@tonic-gate 
11607c478bd9Sstevel@tonic-gate 	if ((svc == NULL) || (printer == NULL) || (job_id < 0) ||
11617c478bd9Sstevel@tonic-gate 	    (attributes == NULL))
11627c478bd9Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
11637c478bd9Sstevel@tonic-gate 
11647c478bd9Sstevel@tonic-gate 	if ((*job = j = calloc(1, sizeof (*j))) == NULL)
11657c478bd9Sstevel@tonic-gate 		return (PAPI_TEMPORARY_ERROR);
11667c478bd9Sstevel@tonic-gate 
11677c478bd9Sstevel@tonic-gate 	dest = printer_name_from_uri_id(printer, job_id);
1168324104abSsonam gupta - Sun Microsystems - Bangalore India 
1169324104abSsonam gupta - Sun Microsystems - Bangalore India 	/*
1170324104abSsonam gupta - Sun Microsystems - Bangalore India 	 * job-id might be job-id-requested
1171324104abSsonam gupta - Sun Microsystems - Bangalore India 	 * If it is job-id-requested then we need to
1172324104abSsonam gupta - Sun Microsystems - Bangalore India 	 * look for corresponding job-id
1173324104abSsonam gupta - Sun Microsystems - Bangalore India 	 */
1174324104abSsonam gupta - Sun Microsystems - Bangalore India 	job_id_actual = check_job_id(svc, printer, job_id);
1175324104abSsonam gupta - Sun Microsystems - Bangalore India 
1176324104abSsonam gupta - Sun Microsystems - Bangalore India 	if (job_id_actual < 0) {
1177324104abSsonam gupta - Sun Microsystems - Bangalore India 		status = PAPI_NOT_FOUND;
1178324104abSsonam gupta - Sun Microsystems - Bangalore India 		detailed_error(svc,
1179324104abSsonam gupta - Sun Microsystems - Bangalore India 		    "failed to initiate change for job (%s-%d): %s",
1180324104abSsonam gupta - Sun Microsystems - Bangalore India 		    dest, job_id, "no such resource");
1181324104abSsonam gupta - Sun Microsystems - Bangalore India 		return (status);
1182324104abSsonam gupta - Sun Microsystems - Bangalore India 	}
1183324104abSsonam gupta - Sun Microsystems - Bangalore India 
1184324104abSsonam gupta - Sun Microsystems - Bangalore India 	status = lpsched_start_change(svc, dest, job_id_actual, &file);
11857c478bd9Sstevel@tonic-gate 	if (status != PAPI_OK)
11867c478bd9Sstevel@tonic-gate 		return (status);
11877c478bd9Sstevel@tonic-gate 
11887c478bd9Sstevel@tonic-gate 	if ((r = getrequest(file)) != NULL) {
11897c478bd9Sstevel@tonic-gate 		job_attributes_to_lpsched_request(handle, r,
11907c478bd9Sstevel@tonic-gate 		    (papi_attribute_t **)attributes);
11917c478bd9Sstevel@tonic-gate #ifdef LP_USE_PAPI_ATTR
11927c478bd9Sstevel@tonic-gate 		/*
11937c478bd9Sstevel@tonic-gate 		 * store the job attributes in the PAPI job attribute file
1194324104abSsonam gupta - Sun Microsystems - Bangalore India 		 * that was created by the original job request. We need to
11957c478bd9Sstevel@tonic-gate 		 * modify the attributes in the file as per the new attributes
11967c478bd9Sstevel@tonic-gate 		 */
11977c478bd9Sstevel@tonic-gate 		snprintf(lpfile, sizeof (lpfile), "%s%d-%s",
1198324104abSsonam gupta - Sun Microsystems - Bangalore India 		    "/var/spool/lp/temp/", job_id_actual, LP_PAPIATTRNAME);
11997c478bd9Sstevel@tonic-gate 		status = psm_modifyAttrsFile(attributes, lpfile);
12007c478bd9Sstevel@tonic-gate 		if (status != PAPI_OK) {
12017c478bd9Sstevel@tonic-gate 			detailed_error(svc,
12027c478bd9Sstevel@tonic-gate 			    "unable to modify the attributes file: %s: %s",
12037c478bd9Sstevel@tonic-gate 			    lpfile, strerror(errno));
12047c478bd9Sstevel@tonic-gate 			return (PAPI_DEVICE_ERROR);
12057c478bd9Sstevel@tonic-gate 		}
12067c478bd9Sstevel@tonic-gate #endif
12077c478bd9Sstevel@tonic-gate 
12087c478bd9Sstevel@tonic-gate 		if (putrequest(file, r) < 0) {
12097c478bd9Sstevel@tonic-gate 			detailed_error(svc,
12107c478bd9Sstevel@tonic-gate 			    gettext("failed to write job: %s: %s"),
12117c478bd9Sstevel@tonic-gate 			    file, strerror(errno));
12127c478bd9Sstevel@tonic-gate 			freerequest(r);
12137c478bd9Sstevel@tonic-gate 			return (PAPI_DEVICE_ERROR);
12147c478bd9Sstevel@tonic-gate 		}
12157c478bd9Sstevel@tonic-gate 	} else {
12167c478bd9Sstevel@tonic-gate 		detailed_error(svc, gettext("failed to read job: %s: %s"),
12177c478bd9Sstevel@tonic-gate 		    file, strerror(errno));
12187c478bd9Sstevel@tonic-gate 		return (PAPI_DEVICE_ERROR);
12197c478bd9Sstevel@tonic-gate 	}
12207c478bd9Sstevel@tonic-gate 
1221324104abSsonam gupta - Sun Microsystems - Bangalore India 	status = lpsched_end_change(svc, dest, job_id_actual);
12227c478bd9Sstevel@tonic-gate 	lpsched_request_to_job_attributes(r, j);
122343b9c050Sjacobs 
122443b9c050Sjacobs 	papiAttributeListAddInteger(&j->attributes, PAPI_ATTR_REPLACE,
1225324104abSsonam gupta - Sun Microsystems - Bangalore India 	    "job-id", job_id_actual);
122643b9c050Sjacobs 
12277c478bd9Sstevel@tonic-gate 	freerequest(r);
12287c478bd9Sstevel@tonic-gate 
12297c478bd9Sstevel@tonic-gate 	return (status);
12307c478bd9Sstevel@tonic-gate }
12317c478bd9Sstevel@tonic-gate 
12327c478bd9Sstevel@tonic-gate /*
12337c478bd9Sstevel@tonic-gate  * Extension to PAPI, a variation of this is slated for post-1.0
12347c478bd9Sstevel@tonic-gate  */
12357c478bd9Sstevel@tonic-gate #define	DUMMY_FILE	"/var/spool/lp/fifos/FIFO"
12367c478bd9Sstevel@tonic-gate 
12377c478bd9Sstevel@tonic-gate papi_status_t
1238355b4669Sjacobs papiJobCreate(papi_service_t handle, char *printer,
1239355b4669Sjacobs 		papi_attribute_t **job_attributes,
1240355b4669Sjacobs 		papi_job_ticket_t *job_ticket, papi_job_t *job)
12417c478bd9Sstevel@tonic-gate {
12427c478bd9Sstevel@tonic-gate 	papi_status_t status;
12437c478bd9Sstevel@tonic-gate 	service_t *svc = handle;
12447c478bd9Sstevel@tonic-gate 	job_t *j = NULL;
12457c478bd9Sstevel@tonic-gate 	REQUEST *request;
12467c478bd9Sstevel@tonic-gate 	char *request_id = NULL;
12477c478bd9Sstevel@tonic-gate 	char *c;
12487c478bd9Sstevel@tonic-gate 	char *tmp = NULL;
12497c478bd9Sstevel@tonic-gate 	char metadata_file[MAXPATHLEN];
12507c478bd9Sstevel@tonic-gate 
12517c478bd9Sstevel@tonic-gate 	if ((svc == NULL) || (printer == NULL) || (job == NULL))
12527c478bd9Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
12537c478bd9Sstevel@tonic-gate 
12547c478bd9Sstevel@tonic-gate 	if (job_ticket != NULL)
12557c478bd9Sstevel@tonic-gate 		return (PAPI_JOB_TICKET_NOT_SUPPORTED);
12567c478bd9Sstevel@tonic-gate 
12577c478bd9Sstevel@tonic-gate 	if ((*job = j = calloc(1, sizeof (*j))) == NULL)
12587c478bd9Sstevel@tonic-gate 		return (PAPI_TEMPORARY_ERROR);
12597c478bd9Sstevel@tonic-gate 
12607c478bd9Sstevel@tonic-gate 	/* 1 for the control file (-0) */
12617c478bd9Sstevel@tonic-gate 	status = lpsched_alloc_files(svc, 1, &request_id);
12627c478bd9Sstevel@tonic-gate 	if (status != PAPI_OK)
12637c478bd9Sstevel@tonic-gate 		return (status);
12647c478bd9Sstevel@tonic-gate 
12657c478bd9Sstevel@tonic-gate 	/* convert the attributes to an lpsched REQUEST structure */
12667c478bd9Sstevel@tonic-gate 	request = create_request(svc, (char *)printer,
12677c478bd9Sstevel@tonic-gate 	    (papi_attribute_t **)job_attributes);
1268355b4669Sjacobs 	if (request == NULL)
1269355b4669Sjacobs 		return (PAPI_TEMPORARY_ERROR);
12707c478bd9Sstevel@tonic-gate 	addlist(&request->file_list, DUMMY_FILE);	/* add a dummy file */
12717c478bd9Sstevel@tonic-gate 	request->actions |= ACT_HOLD;			/* hold the job */
12727c478bd9Sstevel@tonic-gate 
12737c478bd9Sstevel@tonic-gate #ifdef LP_USE_PAPI_ATTR
12747c478bd9Sstevel@tonic-gate 	/*
12757c478bd9Sstevel@tonic-gate 	 * store the job attributes in the PAPI job attribute file that was
12767c478bd9Sstevel@tonic-gate 	 * created by lpsched_alloc_files(), the attributes will then pass
12777c478bd9Sstevel@tonic-gate 	 * through lpsched and be given to the slow-filters and the printer's
12787c478bd9Sstevel@tonic-gate 	 * interface script to process them
12797c478bd9Sstevel@tonic-gate 	 */
12807c478bd9Sstevel@tonic-gate 	snprintf(metadata_file, sizeof (metadata_file), "%s%s-%s",
12817c478bd9Sstevel@tonic-gate 	    "/var/spool/lp/temp/", request_id, LP_PAPIATTRNAME);
12827c478bd9Sstevel@tonic-gate 	status = psm_copy_attrsToFile(job_attributes, metadata_file);
12837c478bd9Sstevel@tonic-gate 	if (status != PAPI_OK) {
12847c478bd9Sstevel@tonic-gate 		detailed_error(svc, "unable to copy attributes to file: %s: %s",
12857c478bd9Sstevel@tonic-gate 		    metadata_file, strerror(errno));
1286355b4669Sjacobs 		free(request_id);
12877c478bd9Sstevel@tonic-gate 		return (PAPI_DEVICE_ERROR);
12887c478bd9Sstevel@tonic-gate 	}
12897c478bd9Sstevel@tonic-gate #endif
12907c478bd9Sstevel@tonic-gate 
12917c478bd9Sstevel@tonic-gate 	/* store the REQUEST on disk */
12927c478bd9Sstevel@tonic-gate 	snprintf(metadata_file, sizeof (metadata_file), "%s-0", request_id);
1293355b4669Sjacobs 	free(request_id);
12947c478bd9Sstevel@tonic-gate 	if (putrequest(metadata_file, request) < 0) {
12957c478bd9Sstevel@tonic-gate 		detailed_error(svc, gettext("unable to save request: %s: %s"),
12967c478bd9Sstevel@tonic-gate 		    metadata_file, strerror(errno));
12977c478bd9Sstevel@tonic-gate 		return (PAPI_DEVICE_ERROR);
12987c478bd9Sstevel@tonic-gate 	}
12997c478bd9Sstevel@tonic-gate 
13007c478bd9Sstevel@tonic-gate 	status = lpsched_commit_job(svc, metadata_file, &tmp);
13017c478bd9Sstevel@tonic-gate 	if (status != PAPI_OK) {
13027c478bd9Sstevel@tonic-gate 		unlink(metadata_file);
13037c478bd9Sstevel@tonic-gate 		return (status);
13047c478bd9Sstevel@tonic-gate 	}
13057c478bd9Sstevel@tonic-gate 
13067c478bd9Sstevel@tonic-gate 	lpsched_request_to_job_attributes(request, j);
13077c478bd9Sstevel@tonic-gate 
13087c478bd9Sstevel@tonic-gate 	if ((c = strrchr(tmp, '-')) != NULL)
13097c478bd9Sstevel@tonic-gate 		c++;
13107c478bd9Sstevel@tonic-gate 	papiAttributeListAddInteger(&j->attributes, PAPI_ATTR_REPLACE,
13117c478bd9Sstevel@tonic-gate 	    "job-id", atoi(c));
13127c478bd9Sstevel@tonic-gate 	papiAttributeListAddString(&j->attributes, PAPI_ATTR_REPLACE,
13137c478bd9Sstevel@tonic-gate 	    "job-uri", tmp);
13147c478bd9Sstevel@tonic-gate 
13157c478bd9Sstevel@tonic-gate 	return (PAPI_OK);
13167c478bd9Sstevel@tonic-gate }
13177c478bd9Sstevel@tonic-gate 
13187c478bd9Sstevel@tonic-gate papi_status_t
13197c478bd9Sstevel@tonic-gate papiJobCommit(papi_service_t handle, char *printer, int32_t id)
13207c478bd9Sstevel@tonic-gate {
13217c478bd9Sstevel@tonic-gate 	papi_status_t status = PAPI_OK;
13227c478bd9Sstevel@tonic-gate 	service_t *svc = handle;
13237c478bd9Sstevel@tonic-gate 	REQUEST *r = NULL;
13247c478bd9Sstevel@tonic-gate 	char *metadata_file;
13257c478bd9Sstevel@tonic-gate 	char *dest;
13267c478bd9Sstevel@tonic-gate 
13277c478bd9Sstevel@tonic-gate 	if ((svc == NULL) || (printer == NULL))
13287c478bd9Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
13297c478bd9Sstevel@tonic-gate 
13307c478bd9Sstevel@tonic-gate 	dest = printer_name_from_uri_id(printer, id);
13317c478bd9Sstevel@tonic-gate 	/* tell the scheduler that we want to change the job */
13327c478bd9Sstevel@tonic-gate 	status = lpsched_start_change(svc, dest, id, &metadata_file);
13337c478bd9Sstevel@tonic-gate 	if (status != PAPI_OK)
13347c478bd9Sstevel@tonic-gate 		return (status);
13357c478bd9Sstevel@tonic-gate 
13367c478bd9Sstevel@tonic-gate 	if ((r = getrequest(metadata_file)) != NULL) {
13377c478bd9Sstevel@tonic-gate 		r->actions &= ~ACT_RESUME;
13387c478bd9Sstevel@tonic-gate 		r->actions |= ACT_RESUME;
13397c478bd9Sstevel@tonic-gate 		dellist(&r->file_list, DUMMY_FILE);
13407c478bd9Sstevel@tonic-gate 
13417c478bd9Sstevel@tonic-gate 		if (putrequest(metadata_file, r) < 0) {
13427c478bd9Sstevel@tonic-gate 			detailed_error(svc,
13437c478bd9Sstevel@tonic-gate 			    gettext("failed to write job: %s: %s"),
13447c478bd9Sstevel@tonic-gate 			    metadata_file, strerror(errno));
1345355b4669Sjacobs 			freerequest(r);
13467c478bd9Sstevel@tonic-gate 			return (PAPI_DEVICE_ERROR);
13477c478bd9Sstevel@tonic-gate 		}
13487c478bd9Sstevel@tonic-gate 	} else {
13497c478bd9Sstevel@tonic-gate 		detailed_error(svc, gettext("failed to read job: %s: %s"),
13507c478bd9Sstevel@tonic-gate 		    metadata_file, strerror(errno));
13517c478bd9Sstevel@tonic-gate 		return (PAPI_DEVICE_ERROR);
13527c478bd9Sstevel@tonic-gate 	}
13537c478bd9Sstevel@tonic-gate 
13547c478bd9Sstevel@tonic-gate 	status = lpsched_end_change(svc, dest, id);
13557c478bd9Sstevel@tonic-gate 	freerequest(r);
13567c478bd9Sstevel@tonic-gate 
13577c478bd9Sstevel@tonic-gate 	return (status);
13587c478bd9Sstevel@tonic-gate }
13597c478bd9Sstevel@tonic-gate 
13607c478bd9Sstevel@tonic-gate papi_status_t
13617c478bd9Sstevel@tonic-gate papiJobStreamAdd(papi_service_t handle, char *printer, int32_t id,
13627c478bd9Sstevel@tonic-gate 		papi_stream_t *stream)
13637c478bd9Sstevel@tonic-gate {
13647c478bd9Sstevel@tonic-gate 	papi_status_t status;
13657c478bd9Sstevel@tonic-gate 	service_t *svc = handle;
13667c478bd9Sstevel@tonic-gate 	job_stream_t *s = NULL;
13677c478bd9Sstevel@tonic-gate 	char *metadata_file = NULL;
13687c478bd9Sstevel@tonic-gate 	char *dest;
13697c478bd9Sstevel@tonic-gate 	char path[MAXPATHLEN];
13707c478bd9Sstevel@tonic-gate 
13717c478bd9Sstevel@tonic-gate 	/* allocate space for the stream */
13727c478bd9Sstevel@tonic-gate 	if ((*stream = s = calloc(1, sizeof (*s))) == NULL)
13737c478bd9Sstevel@tonic-gate 		return (PAPI_TEMPORARY_ERROR);
13747c478bd9Sstevel@tonic-gate 
13757c478bd9Sstevel@tonic-gate 	dest = printer_name_from_uri_id(printer, id);
13767c478bd9Sstevel@tonic-gate 	/* create/open data file (only root or lp can really do this */
13777c478bd9Sstevel@tonic-gate 	snprintf(path, sizeof (path), "/var/spool/lp/temp/%d-XXXXXX", id);
13787c478bd9Sstevel@tonic-gate 	if ((s->fd = mkstemp(path)) < 0) {
13797c478bd9Sstevel@tonic-gate 		detailed_error(svc, gettext("unable to create sink (%s): %s"),
13807c478bd9Sstevel@tonic-gate 		    path, strerror(errno));
13817c478bd9Sstevel@tonic-gate 		free(s);
13827c478bd9Sstevel@tonic-gate 		return (PAPI_NOT_AUTHORIZED);
13837c478bd9Sstevel@tonic-gate 	}
13847c478bd9Sstevel@tonic-gate 
13857c478bd9Sstevel@tonic-gate 	/* add data file to job */
13867c478bd9Sstevel@tonic-gate 	status = lpsched_start_change(svc, dest, id, &metadata_file);
13877c478bd9Sstevel@tonic-gate 	if (status != PAPI_OK) {
13887c478bd9Sstevel@tonic-gate 		close(s->fd);
13897c478bd9Sstevel@tonic-gate 		free(s);
13907c478bd9Sstevel@tonic-gate 		unlink(path);
13917c478bd9Sstevel@tonic-gate 		return (status);
13927c478bd9Sstevel@tonic-gate 	}
13937c478bd9Sstevel@tonic-gate 
13947c478bd9Sstevel@tonic-gate 	if ((s->request = getrequest(metadata_file)) == NULL) {
13957c478bd9Sstevel@tonic-gate 		detailed_error(svc, gettext("unable to load request: %s: %s"),
13967c478bd9Sstevel@tonic-gate 		    metadata_file, strerror(errno));
13977c478bd9Sstevel@tonic-gate 		close(s->fd);
13987c478bd9Sstevel@tonic-gate 		free(s);
13997c478bd9Sstevel@tonic-gate 		unlink(path);
14007c478bd9Sstevel@tonic-gate 		return (PAPI_NOT_POSSIBLE);
14017c478bd9Sstevel@tonic-gate 	}
14027c478bd9Sstevel@tonic-gate 
14037c478bd9Sstevel@tonic-gate 	addlist(&(s->request->file_list), path);
14047c478bd9Sstevel@tonic-gate 
14057c478bd9Sstevel@tonic-gate 	if (putrequest(metadata_file, s->request) < 0) {
14067c478bd9Sstevel@tonic-gate 		detailed_error(svc, gettext("unable to save request: %s: %s"),
14077c478bd9Sstevel@tonic-gate 		    metadata_file, strerror(errno));
14087c478bd9Sstevel@tonic-gate 		close(s->fd);
14097c478bd9Sstevel@tonic-gate 		free(s);
14107c478bd9Sstevel@tonic-gate 		unlink(path);
14117c478bd9Sstevel@tonic-gate 		return (PAPI_NOT_POSSIBLE);
14127c478bd9Sstevel@tonic-gate 	}
14137c478bd9Sstevel@tonic-gate 
14147c478bd9Sstevel@tonic-gate 	status = lpsched_end_change(svc, dest, id);
14157c478bd9Sstevel@tonic-gate 
14167c478bd9Sstevel@tonic-gate 	if (status != PAPI_OK)
14177c478bd9Sstevel@tonic-gate 		return (status);
14187c478bd9Sstevel@tonic-gate 
14197c478bd9Sstevel@tonic-gate 	return (PAPI_OK);
14207c478bd9Sstevel@tonic-gate }
1421