xref: /onnv-gate/usr/src/cmd/lp/lib/papi/job.c (revision 8569)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51676Sjpk  * Common Development and Distribution License (the "License").
61676Sjpk  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*8569SJonathan.Ca@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*LINTLIBRARY*/
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <stdlib.h>
290Sstevel@tonic-gate #include <string.h>
300Sstevel@tonic-gate #include <unistd.h>
310Sstevel@tonic-gate #include <libintl.h>
320Sstevel@tonic-gate #include <pwd.h>
330Sstevel@tonic-gate #include <sys/stat.h>
340Sstevel@tonic-gate #include <papi_impl.h>
350Sstevel@tonic-gate 
362264Sjacobs 
372264Sjacobs /*
382264Sjacobs  * for an older application that may have been linked with a pre-v1.0
392264Sjacobs  * PAPI implementation.
402264Sjacobs  */
412264Sjacobs papi_status_t
422264Sjacobs papiAttributeListAdd(papi_attribute_t ***attrs, int flags, char *name,
432264Sjacobs 		papi_attribute_value_type_t type, papi_attribute_value_t *value)
442264Sjacobs {
452264Sjacobs 	return (papiAttributeListAddValue(attrs, flags, name, type, value));
462264Sjacobs }
472264Sjacobs 
480Sstevel@tonic-gate #ifdef LP_USE_PAPI_ATTR
492264Sjacobs static papi_status_t psm_modifyAttrsFile(papi_attribute_t **attrs, char *file);
502264Sjacobs static papi_status_t psm_modifyAttrsList(char *file, papi_attribute_t **attrs,
512264Sjacobs 					papi_attribute_t ***newAttrs);
520Sstevel@tonic-gate #endif
530Sstevel@tonic-gate 
540Sstevel@tonic-gate 
550Sstevel@tonic-gate void
560Sstevel@tonic-gate papiJobFree(papi_job_t job)
570Sstevel@tonic-gate {
580Sstevel@tonic-gate 	job_t *tmp = (job_t *)job;
590Sstevel@tonic-gate 
600Sstevel@tonic-gate 	if (tmp != NULL) {
610Sstevel@tonic-gate 		papiAttributeListFree(tmp->attributes);
620Sstevel@tonic-gate 		free(tmp);
630Sstevel@tonic-gate 	}
640Sstevel@tonic-gate }
650Sstevel@tonic-gate 
660Sstevel@tonic-gate void
670Sstevel@tonic-gate papiJobListFree(papi_job_t *jobs)
680Sstevel@tonic-gate {
690Sstevel@tonic-gate 	if (jobs != NULL) {
700Sstevel@tonic-gate 		int i;
710Sstevel@tonic-gate 
720Sstevel@tonic-gate 		for (i = 0; jobs[i] != NULL; i++) {
730Sstevel@tonic-gate 			papiJobFree(jobs[i]);
740Sstevel@tonic-gate 		}
750Sstevel@tonic-gate 		free(jobs);
760Sstevel@tonic-gate 	}
770Sstevel@tonic-gate }
780Sstevel@tonic-gate 
790Sstevel@tonic-gate papi_attribute_t **
800Sstevel@tonic-gate papiJobGetAttributeList(papi_job_t job)
810Sstevel@tonic-gate {
820Sstevel@tonic-gate 	job_t *tmp = (job_t *)job;
830Sstevel@tonic-gate 
840Sstevel@tonic-gate 	if (tmp != NULL)
850Sstevel@tonic-gate 		return (tmp->attributes);
860Sstevel@tonic-gate 
870Sstevel@tonic-gate 	return (NULL);
880Sstevel@tonic-gate }
890Sstevel@tonic-gate 
900Sstevel@tonic-gate char *
910Sstevel@tonic-gate papiJobGetPrinterName(papi_job_t job)
920Sstevel@tonic-gate {
930Sstevel@tonic-gate 	job_t *tmp = (job_t *)job;
940Sstevel@tonic-gate 	char *result = NULL;
950Sstevel@tonic-gate 
960Sstevel@tonic-gate 	if (tmp != NULL)
970Sstevel@tonic-gate 		papiAttributeListGetString(tmp->attributes, NULL,
98*8569SJonathan.Ca@Sun.COM 		    "printer-name", &result);
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate 	return (result);
1010Sstevel@tonic-gate }
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate int32_t
1040Sstevel@tonic-gate papiJobGetId(papi_job_t job)
1050Sstevel@tonic-gate {
1060Sstevel@tonic-gate 	job_t *tmp = (job_t *)job;
1070Sstevel@tonic-gate 	int result = -1;
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate 	if (tmp != NULL)
1100Sstevel@tonic-gate 		papiAttributeListGetInteger(tmp->attributes, NULL, "job-id",
111*8569SJonathan.Ca@Sun.COM 		    &result);
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate 	return (result);
1140Sstevel@tonic-gate }
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate static REQUEST *
1170Sstevel@tonic-gate create_request(papi_service_t svc, char *printer, papi_attribute_t **attributes)
1180Sstevel@tonic-gate {
1197253Sjacobs 	REQUEST *r;
1200Sstevel@tonic-gate 
1217253Sjacobs 	if ((r = calloc(1, sizeof (*r))) != NULL) {
1227253Sjacobs 		r->priority = -1;
1237253Sjacobs 		r->destination = printer_name_from_uri_id(printer, -1);
1247253Sjacobs 		job_attributes_to_lpsched_request(svc, r, attributes);
1257253Sjacobs 	}
1260Sstevel@tonic-gate 
1277253Sjacobs 	return (r);
1280Sstevel@tonic-gate }
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate static papi_status_t
1310Sstevel@tonic-gate authorized(service_t *svc, int32_t id)
1320Sstevel@tonic-gate {
1330Sstevel@tonic-gate 	papi_status_t result = PAPI_NOT_AUTHORIZED;	/* assume the worst */
1340Sstevel@tonic-gate 	char file[32];
1350Sstevel@tonic-gate 	REQUEST *r;
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate 	snprintf(file, sizeof (file), "%d-0", id);
1380Sstevel@tonic-gate 	if ((r = getrequest(file)) != NULL) {
1390Sstevel@tonic-gate 		uid_t uid = getuid();
1400Sstevel@tonic-gate 		struct passwd *pw = NULL;
1410Sstevel@tonic-gate 		char *user = "intruder";	/* assume an intruder */
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate 		if ((pw = getpwuid(uid)) != NULL)
1440Sstevel@tonic-gate 			user = pw->pw_name;	/* use the process owner */
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate 		if ((uid == 0) || (uid == 71)) { /* root/lp can forge this */
1470Sstevel@tonic-gate 			papi_status_t s;
1480Sstevel@tonic-gate 			s = papiAttributeListGetString(svc->attributes, NULL,
149*8569SJonathan.Ca@Sun.COM 			    "user-name", &user);
1500Sstevel@tonic-gate 			if (s != PAPI_OK)	/* true root/lp are almighty */
1510Sstevel@tonic-gate 				result = PAPI_OK;
1520Sstevel@tonic-gate 		}
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate 		if ((result != PAPI_OK) && (strcmp(user, r->user) == 0))
1550Sstevel@tonic-gate 			result = PAPI_OK;
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate 		freerequest(r);
1580Sstevel@tonic-gate 	} else
1590Sstevel@tonic-gate 		result = PAPI_NOT_FOUND;
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate 	return (result);
1620Sstevel@tonic-gate }
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate static papi_status_t
1652264Sjacobs copy_file(char *from, char *to)
1660Sstevel@tonic-gate {
1670Sstevel@tonic-gate 	int ifd, ofd;
1680Sstevel@tonic-gate 	char buf[BUFSIZ];
1690Sstevel@tonic-gate 	int rc;
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate 	if ((ifd = open(from, O_RDONLY)) < 0)
1720Sstevel@tonic-gate 		return (PAPI_DOCUMENT_ACCESS_ERROR);
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate 	if ((ofd = open(to, O_WRONLY)) < 0) {
1750Sstevel@tonic-gate 		close(ifd);
1760Sstevel@tonic-gate 		return (PAPI_NOT_POSSIBLE);
1770Sstevel@tonic-gate 	}
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate 	while ((rc = read(ifd, buf, sizeof (buf))) > 0)
1800Sstevel@tonic-gate 		write(ofd, buf, rc);
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate 	close(ifd);
1830Sstevel@tonic-gate 	close(ofd);
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate 	return (PAPI_OK);
1860Sstevel@tonic-gate }
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate #ifdef LP_USE_PAPI_ATTR
1900Sstevel@tonic-gate /*
1910Sstevel@tonic-gate  * *****************************************************************************
1920Sstevel@tonic-gate  *
1930Sstevel@tonic-gate  * Description: Create a file containing all the attributes in the attribute
1940Sstevel@tonic-gate  *              list passed to this function.
1950Sstevel@tonic-gate  *              This file is then passed through lpsched and given to either
1960Sstevel@tonic-gate  *              a slow-filter or to the printer's interface script to process
1970Sstevel@tonic-gate  *              the attributes.
1980Sstevel@tonic-gate  *
1990Sstevel@tonic-gate  * Parameters:  attrs - list of attributes and their values
2000Sstevel@tonic-gate  *              file  - file pathname to create and put the attributes into.
2010Sstevel@tonic-gate  *
2020Sstevel@tonic-gate  * *****************************************************************************
2030Sstevel@tonic-gate  */
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate static papi_status_t
2062264Sjacobs psm_copy_attrsToFile(papi_attribute_t **attrs, char *file)
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate {
2090Sstevel@tonic-gate 	papi_status_t result = PAPI_OK;
2102264Sjacobs 
2112264Sjacobs 	if ((attrs != NULL) && (*attrs != NULL)) {
2122264Sjacobs 		FILE *out = NULL;
2130Sstevel@tonic-gate 
2142264Sjacobs 		if ((out = fopen(file, "w")) != NULL) {
2152264Sjacobs 			papiAttributeListPrint(out, attrs, "");
2160Sstevel@tonic-gate 			fclose(out);
2172264Sjacobs 		} else {
2180Sstevel@tonic-gate 			result = PAPI_NOT_POSSIBLE;
2190Sstevel@tonic-gate 		}
2200Sstevel@tonic-gate 	}
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate 	return (result);
2230Sstevel@tonic-gate } /* psm_copy_attrsToFile */
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate /*
2270Sstevel@tonic-gate  * *****************************************************************************
2280Sstevel@tonic-gate  *
2290Sstevel@tonic-gate  * Description: Modify the given attribute 'file' with the attributes from the
2300Sstevel@tonic-gate  *              'attrs' list. Attributes already in the file will be replaced
2310Sstevel@tonic-gate  *              with the new value. New attributes will be added into the file.
2320Sstevel@tonic-gate  *
2330Sstevel@tonic-gate  * Parameters:  attrs - list of attributes and their values
2340Sstevel@tonic-gate  *              file  - file pathname to create and put the attributes into.
2350Sstevel@tonic-gate  *
2360Sstevel@tonic-gate  * *****************************************************************************
2370Sstevel@tonic-gate  */
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate static papi_status_t
2402264Sjacobs psm_modifyAttrsFile(papi_attribute_t **attrs, char *file)
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate {
2430Sstevel@tonic-gate 	papi_status_t result = PAPI_OK;
2440Sstevel@tonic-gate 	papi_attribute_t **newAttrs = NULL;
2450Sstevel@tonic-gate 	struct stat   tmpBuf;
2460Sstevel@tonic-gate 	FILE *fd = NULL;
2470Sstevel@tonic-gate 
2482264Sjacobs 	if ((attrs != NULL) && (*attrs != NULL) && (file != NULL)) {
2490Sstevel@tonic-gate 
2500Sstevel@tonic-gate 		/*
2510Sstevel@tonic-gate 		 * check file exist before try to modify it, if it doesn't
2520Sstevel@tonic-gate 		 * exist assume there is an error
2530Sstevel@tonic-gate 		 */
2542264Sjacobs 		if (stat(file, &tmpBuf) == 0) {
2550Sstevel@tonic-gate 			/*
2560Sstevel@tonic-gate 			 * if file is currently empty just write the given
2570Sstevel@tonic-gate 			 * attributes to the file otherwise exact the attributes
2580Sstevel@tonic-gate 			 * from the file and modify them accordingly before
2590Sstevel@tonic-gate 			 * writing them back to the file
2600Sstevel@tonic-gate 			 */
2612264Sjacobs 			if (tmpBuf.st_size == 0) {
2620Sstevel@tonic-gate 				newAttrs = (papi_attribute_t **)attrs;
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate 				fd = fopen(file, "w");
2652264Sjacobs 				if (fd != NULL) {
2660Sstevel@tonic-gate 					papiAttributeListPrint(fd,
2672264Sjacobs 							newAttrs, "");
2680Sstevel@tonic-gate 					fclose(fd);
2692264Sjacobs 				} else {
2700Sstevel@tonic-gate 					result = PAPI_NOT_POSSIBLE;
2710Sstevel@tonic-gate 				}
2722264Sjacobs 			} else {
2730Sstevel@tonic-gate 				result =
2740Sstevel@tonic-gate 				    psm_modifyAttrsList(file, attrs, &newAttrs);
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate 				fd = fopen(file, "w");
2772264Sjacobs 				if (fd != NULL) {
2780Sstevel@tonic-gate 					papiAttributeListPrint(fd,
2792264Sjacobs 								newAttrs, "");
2800Sstevel@tonic-gate 					fclose(fd);
2812264Sjacobs 				} else {
2820Sstevel@tonic-gate 					result = PAPI_NOT_POSSIBLE;
2830Sstevel@tonic-gate 				}
2840Sstevel@tonic-gate 
2850Sstevel@tonic-gate 				papiAttributeListFree(newAttrs);
2860Sstevel@tonic-gate 			}
2872264Sjacobs 		} else {
2880Sstevel@tonic-gate 			result = PAPI_NOT_POSSIBLE;
2890Sstevel@tonic-gate 		}
2900Sstevel@tonic-gate 	}
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate 	return (result);
2930Sstevel@tonic-gate } /* psm_modifyAttrsFile */
2940Sstevel@tonic-gate 
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate /*
2970Sstevel@tonic-gate  * *****************************************************************************
2980Sstevel@tonic-gate  *
2990Sstevel@tonic-gate  * Description: Extracts the attributes in the given attribute 'file' and
3000Sstevel@tonic-gate  *              creates a new list 'newAttrs' containing the modified list of
3010Sstevel@tonic-gate  *              attributes.
3020Sstevel@tonic-gate  *
3030Sstevel@tonic-gate  * Parameters:  file  - pathname of file containing attributes to be modified
3040Sstevel@tonic-gate  *              attrs - list of attributes and their values to modify
3050Sstevel@tonic-gate  *              newAttrs - returns the modified list of attributes
3060Sstevel@tonic-gate  *
3070Sstevel@tonic-gate  * *****************************************************************************
3080Sstevel@tonic-gate  */
3090Sstevel@tonic-gate 
3100Sstevel@tonic-gate static papi_status_t
3112264Sjacobs psm_modifyAttrsList(char *file, papi_attribute_t **attrs,
3120Sstevel@tonic-gate     papi_attribute_t ***newAttrs)
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate {
3150Sstevel@tonic-gate 	papi_status_t result = PAPI_OK;
3160Sstevel@tonic-gate 	papi_attribute_t  *nextAttr = NULL;
3170Sstevel@tonic-gate 	papi_attribute_value_t  **values = NULL;
3180Sstevel@tonic-gate 	void *iter = NULL;
3190Sstevel@tonic-gate 	FILE *fd = NULL;
3200Sstevel@tonic-gate 	register int fD = 0;
3210Sstevel@tonic-gate 	char aBuff[200];
3220Sstevel@tonic-gate 	char *a = NULL;
3230Sstevel@tonic-gate 	char *p = NULL;
3240Sstevel@tonic-gate 	int count = 0;
3250Sstevel@tonic-gate 	int n = 0;
3260Sstevel@tonic-gate 
3270Sstevel@tonic-gate 	fd = fopen(file, "r");
3282264Sjacobs 	if (fd != NULL) {
3290Sstevel@tonic-gate 		fD = fileno(fd);
3300Sstevel@tonic-gate 		a = &aBuff[0];
3310Sstevel@tonic-gate 		p = &aBuff[0];
3320Sstevel@tonic-gate 		count = read(fD, &aBuff[0], sizeof (aBuff) - 1);
3332264Sjacobs 		while ((result == PAPI_OK) && (count > 0)) {
3340Sstevel@tonic-gate 			aBuff[count+n] = '\0';
3352264Sjacobs 			if (count == sizeof (aBuff) - n - 1) {
3360Sstevel@tonic-gate 				p = strrchr(aBuff, '\n');
3372264Sjacobs 				if (p != NULL) {
3380Sstevel@tonic-gate 					/* terminate at last complete line */
3390Sstevel@tonic-gate 					*p = '\0';
3400Sstevel@tonic-gate 				}
3410Sstevel@tonic-gate 			}
3420Sstevel@tonic-gate 			result = papiAttributeListFromString(
3430Sstevel@tonic-gate 				newAttrs, PAPI_ATTR_EXCL, aBuff);
3440Sstevel@tonic-gate 
3452264Sjacobs 			if (result == PAPI_OK) {
3460Sstevel@tonic-gate 				/*
3470Sstevel@tonic-gate 				 * handle any part lines and then read the next
3480Sstevel@tonic-gate 				 * buffer from the file
3490Sstevel@tonic-gate 				 */
3500Sstevel@tonic-gate 				n = 0;
3512264Sjacobs 				if (p != a) {
3520Sstevel@tonic-gate 					p++; /* skip NL */
3530Sstevel@tonic-gate 					n = sizeof (aBuff) - 1 - (p - a);
3540Sstevel@tonic-gate 					strncpy(aBuff, p, n);
3550Sstevel@tonic-gate 				}
3560Sstevel@tonic-gate 				count = read(fD, &aBuff[n],
3570Sstevel@tonic-gate 					sizeof (aBuff) - n - 1);
3580Sstevel@tonic-gate 				p = &aBuff[0];
3590Sstevel@tonic-gate 			}
3600Sstevel@tonic-gate 		}
3610Sstevel@tonic-gate 		fclose(fd);
3620Sstevel@tonic-gate 	}
3630Sstevel@tonic-gate 
3640Sstevel@tonic-gate 	/* now modify the attribute list with the new attributes in 'attrs' */
3650Sstevel@tonic-gate 
3660Sstevel@tonic-gate 	nextAttr = papiAttributeListGetNext((papi_attribute_t **)attrs, &iter);
3672264Sjacobs 	while ((result == PAPI_OK) && (nextAttr != NULL)) {
3680Sstevel@tonic-gate 		values = nextAttr->values;
3690Sstevel@tonic-gate 
3702264Sjacobs 		if ((values != NULL) && (*values != NULL)) {
3712264Sjacobs 			result = papiAttributeListAddValue(newAttrs,
3720Sstevel@tonic-gate 						    PAPI_ATTR_REPLACE,
3730Sstevel@tonic-gate 						    nextAttr->name,
3740Sstevel@tonic-gate 						    nextAttr->type, *values);
3750Sstevel@tonic-gate 			values++;
3760Sstevel@tonic-gate 		}
3770Sstevel@tonic-gate 
3780Sstevel@tonic-gate 		while ((result == PAPI_OK) &&
3792264Sjacobs 			(values != NULL) && (*values != NULL)) {
3802264Sjacobs 			result = papiAttributeListAddValue(newAttrs,
3810Sstevel@tonic-gate 						    PAPI_ATTR_APPEND,
3820Sstevel@tonic-gate 						    nextAttr->name,
3830Sstevel@tonic-gate 						    nextAttr->type, *values);
3840Sstevel@tonic-gate 			values++;
3850Sstevel@tonic-gate 		}
3860Sstevel@tonic-gate 		nextAttr =
3870Sstevel@tonic-gate 		    papiAttributeListGetNext((papi_attribute_t **)attrs, &iter);
3880Sstevel@tonic-gate 	}
3890Sstevel@tonic-gate 
3900Sstevel@tonic-gate 	return (result);
3910Sstevel@tonic-gate } /* papi_modifyAttrsList() */
3920Sstevel@tonic-gate #endif
3930Sstevel@tonic-gate 
3940Sstevel@tonic-gate 
3950Sstevel@tonic-gate papi_status_t
3962264Sjacobs papiJobSubmit(papi_service_t handle, char *printer,
3972264Sjacobs 		papi_attribute_t **job_attributes,
3982264Sjacobs 		papi_job_ticket_t *job_ticket,
3992264Sjacobs 		char **files, papi_job_t *job)
4000Sstevel@tonic-gate {
4010Sstevel@tonic-gate 	papi_status_t status;
4020Sstevel@tonic-gate 	service_t *svc = handle;
403*8569SJonathan.Ca@Sun.COM 	struct stat statbuf;
4040Sstevel@tonic-gate 	job_t *j;
4050Sstevel@tonic-gate 	int file_no;
4060Sstevel@tonic-gate 	char *request_id = NULL;
4070Sstevel@tonic-gate 	REQUEST *request;
4080Sstevel@tonic-gate 	int i;
4090Sstevel@tonic-gate 	char *c;
4100Sstevel@tonic-gate 	char *tmp = NULL;
4110Sstevel@tonic-gate 	char lpfile[BUFSIZ];
4120Sstevel@tonic-gate 
4130Sstevel@tonic-gate 	if ((svc == NULL) || (printer == NULL) || (files == NULL) ||
4140Sstevel@tonic-gate 	    (job == NULL))
4150Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
4160Sstevel@tonic-gate 
4170Sstevel@tonic-gate 	if (job_ticket != NULL)
4180Sstevel@tonic-gate 		return (PAPI_OPERATION_NOT_SUPPORTED);
4190Sstevel@tonic-gate 
4200Sstevel@tonic-gate 	if (files != NULL)
421*8569SJonathan.Ca@Sun.COM 		for (file_no = 0; files[file_no] != NULL; file_no++) {
4220Sstevel@tonic-gate 			if (access(files[file_no], R_OK) < 0) {
4230Sstevel@tonic-gate 				detailed_error(svc,
424*8569SJonathan.Ca@Sun.COM 				    gettext("Cannot access file: %s: %s"),
425*8569SJonathan.Ca@Sun.COM 				    files[file_no], strerror(errno));
4260Sstevel@tonic-gate 				return (PAPI_BAD_ARGUMENT);
4270Sstevel@tonic-gate 			}
428*8569SJonathan.Ca@Sun.COM 			stat(files[file_no], &statbuf);
429*8569SJonathan.Ca@Sun.COM 			if (statbuf.st_size == 0) {
430*8569SJonathan.Ca@Sun.COM 				detailed_error(svc,
431*8569SJonathan.Ca@Sun.COM 				    gettext("Zero byte (empty) file: %s"),
432*8569SJonathan.Ca@Sun.COM 				    files[file_no]);
433*8569SJonathan.Ca@Sun.COM 				return (PAPI_BAD_ARGUMENT);
434*8569SJonathan.Ca@Sun.COM 			}
435*8569SJonathan.Ca@Sun.COM 		}
4360Sstevel@tonic-gate 
4370Sstevel@tonic-gate 	if ((*job = j = calloc(1, sizeof (*j))) == NULL)
4380Sstevel@tonic-gate 		return (PAPI_TEMPORARY_ERROR);
4390Sstevel@tonic-gate 
4400Sstevel@tonic-gate 	/* file_no + 1 for the control file (-0) */
4410Sstevel@tonic-gate 	status = lpsched_alloc_files(svc, file_no + 1, &request_id);
4420Sstevel@tonic-gate 	if (status != PAPI_OK)
4430Sstevel@tonic-gate 		return (status);
4440Sstevel@tonic-gate 
4450Sstevel@tonic-gate 	request = create_request(svc, (char *)printer,
446*8569SJonathan.Ca@Sun.COM 	    (papi_attribute_t **)job_attributes);
4470Sstevel@tonic-gate 
4480Sstevel@tonic-gate 	for (i = 0; files[i] != NULL; i++) {
4490Sstevel@tonic-gate 		papi_status_t status;
4500Sstevel@tonic-gate 		snprintf(lpfile, sizeof (lpfile), "%s%s-%d",
451*8569SJonathan.Ca@Sun.COM 		    "/var/spool/lp/temp/", request_id, i+1);
4520Sstevel@tonic-gate 		status = copy_file(files[i], lpfile);
4530Sstevel@tonic-gate 		if (status != PAPI_OK) {
4540Sstevel@tonic-gate 			detailed_error(svc,
455*8569SJonathan.Ca@Sun.COM 			    gettext("unable to copy: %s -> %s: %s"),
456*8569SJonathan.Ca@Sun.COM 			    files[i], lpfile, strerror(errno));
4570Sstevel@tonic-gate 				freerequest(request);
4580Sstevel@tonic-gate 			return (PAPI_DEVICE_ERROR);
4590Sstevel@tonic-gate 		}
4600Sstevel@tonic-gate 		addlist(&(request->file_list), lpfile);
4610Sstevel@tonic-gate 	}
4620Sstevel@tonic-gate 
4630Sstevel@tonic-gate #ifdef LP_USE_PAPI_ATTR
4640Sstevel@tonic-gate 	/*
4650Sstevel@tonic-gate 	 * store the job attributes in the PAPI job attribute file that was
4660Sstevel@tonic-gate 	 * created by lpsched_alloc_files(), the attributes will then pass
4670Sstevel@tonic-gate 	 * through lpsched and be given to the slow-filters and the printer's
4680Sstevel@tonic-gate 	 * interface script to process them
4690Sstevel@tonic-gate 	 */
4700Sstevel@tonic-gate 	snprintf(lpfile, sizeof (lpfile), "%s%s-%s",
471*8569SJonathan.Ca@Sun.COM 	    "/var/spool/lp/temp/", request_id, LP_PAPIATTRNAME);
4720Sstevel@tonic-gate 	status = psm_copy_attrsToFile(job_attributes, lpfile);
4730Sstevel@tonic-gate 	if (status != PAPI_OK) {
4740Sstevel@tonic-gate 		detailed_error(svc, "unable to copy attributes to file: %s: %s",
475*8569SJonathan.Ca@Sun.COM 		    lpfile, strerror(errno));
4760Sstevel@tonic-gate 		return (PAPI_DEVICE_ERROR);
4770Sstevel@tonic-gate 	}
4780Sstevel@tonic-gate #endif
4790Sstevel@tonic-gate 
4800Sstevel@tonic-gate 	/* store the meta-data file */
4810Sstevel@tonic-gate 	snprintf(lpfile, sizeof (lpfile), "%s-0", request_id);
4820Sstevel@tonic-gate 	if (putrequest(lpfile, request) < 0) {
4830Sstevel@tonic-gate 		detailed_error(svc, gettext("unable to save request: %s: %s"),
484*8569SJonathan.Ca@Sun.COM 		    lpfile, strerror(errno));
4850Sstevel@tonic-gate 		freerequest(request);
4860Sstevel@tonic-gate 		return (PAPI_DEVICE_ERROR);
4870Sstevel@tonic-gate 	}
4880Sstevel@tonic-gate 
4890Sstevel@tonic-gate 	status = lpsched_commit_job(svc, lpfile, &tmp);
4900Sstevel@tonic-gate 	if (status != PAPI_OK) {
4910Sstevel@tonic-gate 		unlink(lpfile);
4920Sstevel@tonic-gate 		freerequest(request);
4930Sstevel@tonic-gate 		return (status);
4940Sstevel@tonic-gate 	}
4950Sstevel@tonic-gate 
4960Sstevel@tonic-gate 	lpsched_request_to_job_attributes(request, j);
4970Sstevel@tonic-gate 	freerequest(request);
4980Sstevel@tonic-gate 
4990Sstevel@tonic-gate 	if ((c = strrchr(tmp, '-')) != NULL)
5000Sstevel@tonic-gate 		c++;
5010Sstevel@tonic-gate 	papiAttributeListAddInteger(&j->attributes, PAPI_ATTR_REPLACE,
502*8569SJonathan.Ca@Sun.COM 	    "job-id", atoi(c));
5030Sstevel@tonic-gate 	papiAttributeListAddString(&j->attributes, PAPI_ATTR_REPLACE,
504*8569SJonathan.Ca@Sun.COM 	    "job-uri", tmp);
5050Sstevel@tonic-gate 
5060Sstevel@tonic-gate 	return (PAPI_OK);
5070Sstevel@tonic-gate }
5080Sstevel@tonic-gate 
5090Sstevel@tonic-gate papi_status_t
5102264Sjacobs papiJobSubmitByReference(papi_service_t handle, char *printer,
5112264Sjacobs 		papi_attribute_t **job_attributes,
5122264Sjacobs 		papi_job_ticket_t *job_ticket,
5132264Sjacobs 		char **files, papi_job_t *job)
5140Sstevel@tonic-gate {
5150Sstevel@tonic-gate 	service_t *svc = handle;
516*8569SJonathan.Ca@Sun.COM 	struct stat statbuf;
5170Sstevel@tonic-gate 	job_t *j;
5180Sstevel@tonic-gate 	int file_no;
5190Sstevel@tonic-gate 	short status;
5200Sstevel@tonic-gate 	char *request_id = NULL;
5210Sstevel@tonic-gate 	REQUEST *request;
5220Sstevel@tonic-gate 	char *c;
5230Sstevel@tonic-gate 	char *tmp = NULL;
5240Sstevel@tonic-gate 	char lpfile[BUFSIZ];
5250Sstevel@tonic-gate 	char **file_list = NULL;
5260Sstevel@tonic-gate 
5270Sstevel@tonic-gate 	if ((svc == NULL) || (printer == NULL) || (files == NULL) ||
5280Sstevel@tonic-gate 	    (job == NULL))
5290Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
5300Sstevel@tonic-gate 
5310Sstevel@tonic-gate 	if (job_ticket != NULL)
5320Sstevel@tonic-gate 		return (PAPI_OPERATION_NOT_SUPPORTED);
5330Sstevel@tonic-gate 
5340Sstevel@tonic-gate 	if (files != NULL)
5350Sstevel@tonic-gate 		for (file_no = 0; files[file_no] != NULL; file_no++) {
5360Sstevel@tonic-gate 			if (access(files[file_no], R_OK) < 0) {
5370Sstevel@tonic-gate 				detailed_error(svc,
538*8569SJonathan.Ca@Sun.COM 				    gettext("Cannot access file: %s: %s"),
539*8569SJonathan.Ca@Sun.COM 				    files[file_no], strerror(errno));
5402660Sjacobs 				return (PAPI_DOCUMENT_ACCESS_ERROR);
5410Sstevel@tonic-gate 			}
542*8569SJonathan.Ca@Sun.COM 			stat(files[file_no], &statbuf);
543*8569SJonathan.Ca@Sun.COM 			if (statbuf.st_size == 0) {
544*8569SJonathan.Ca@Sun.COM 				detailed_error(svc,
545*8569SJonathan.Ca@Sun.COM 				    gettext("Zero byte (empty) file: %s"),
546*8569SJonathan.Ca@Sun.COM 				    files[file_no]);
547*8569SJonathan.Ca@Sun.COM 				return (PAPI_BAD_ARGUMENT);
548*8569SJonathan.Ca@Sun.COM 			}
549*8569SJonathan.Ca@Sun.COM 
5502660Sjacobs 			if (files[file_no][0] != '/') {
5512660Sjacobs 				char path[MAXPATHLEN];
5522660Sjacobs 
5532660Sjacobs 				if (getcwd(path, sizeof (path)) == NULL) {
5542660Sjacobs 					detailed_error(svc, gettext(
555*8569SJonathan.Ca@Sun.COM 					    "getcwd for file: %s: %s"),
556*8569SJonathan.Ca@Sun.COM 					    files[file_no],
557*8569SJonathan.Ca@Sun.COM 					    strerror(errno));
5582660Sjacobs 					return (PAPI_DOCUMENT_ACCESS_ERROR);
5592660Sjacobs 				}
5602660Sjacobs 				strlcat(path, "/", sizeof (path));
5612660Sjacobs 				if (strlcat(path, files[file_no], sizeof (path))
562*8569SJonathan.Ca@Sun.COM 				    >= sizeof (path)) {
5632660Sjacobs 					detailed_error(svc, gettext(
564*8569SJonathan.Ca@Sun.COM 					    "pathname too long: %s"),
565*8569SJonathan.Ca@Sun.COM 					    files[file_no]);
5662660Sjacobs 					return (PAPI_DOCUMENT_ACCESS_ERROR);
5672660Sjacobs 				}
5682660Sjacobs 				addlist(&file_list, path);
5692660Sjacobs 			} else
5702660Sjacobs 				addlist(&file_list, (char *)files[file_no]);
5710Sstevel@tonic-gate 		}
5720Sstevel@tonic-gate 
5730Sstevel@tonic-gate 	if ((*job = j = calloc(1, sizeof (*j))) == NULL)
5740Sstevel@tonic-gate 		return (PAPI_TEMPORARY_ERROR);
5750Sstevel@tonic-gate 
5760Sstevel@tonic-gate 	/* 1 for the control file (-0) */
5770Sstevel@tonic-gate 	status = lpsched_alloc_files(svc, 1, &request_id);
5780Sstevel@tonic-gate 	if (status != PAPI_OK)
5790Sstevel@tonic-gate 		return (status);
5800Sstevel@tonic-gate 
5810Sstevel@tonic-gate 	request = create_request(svc, (char *)printer,
582*8569SJonathan.Ca@Sun.COM 	    (papi_attribute_t **)job_attributes);
5830Sstevel@tonic-gate 	request->file_list = file_list;
5840Sstevel@tonic-gate 
5850Sstevel@tonic-gate #ifdef LP_USE_PAPI_ATTR
5860Sstevel@tonic-gate 	/*
5870Sstevel@tonic-gate 	 * store the job attributes in the PAPI job attribute file that was
5880Sstevel@tonic-gate 	 * created by lpsched_alloc_files(), the attributes will then pass
5890Sstevel@tonic-gate 	 * through lpsched and be given to the slow-filters and the printer's
5900Sstevel@tonic-gate 	 * interface script to process them
5910Sstevel@tonic-gate 	 */
5920Sstevel@tonic-gate 	snprintf(lpfile, sizeof (lpfile), "%s%s-%s",
593*8569SJonathan.Ca@Sun.COM 	    "/var/spool/lp/temp/", request_id, LP_PAPIATTRNAME);
5940Sstevel@tonic-gate 	status = psm_copy_attrsToFile(job_attributes, lpfile);
5950Sstevel@tonic-gate 	if (status != PAPI_OK) {
5960Sstevel@tonic-gate 		detailed_error(svc, "unable to copy attributes to file: %s: %s",
597*8569SJonathan.Ca@Sun.COM 		    lpfile, strerror(errno));
5980Sstevel@tonic-gate 		return (PAPI_DEVICE_ERROR);
5990Sstevel@tonic-gate 	}
6000Sstevel@tonic-gate #endif
6010Sstevel@tonic-gate 
6020Sstevel@tonic-gate 	/* store the meta-data file */
6030Sstevel@tonic-gate 	snprintf(lpfile, sizeof (lpfile), "%s-0", request_id);
6040Sstevel@tonic-gate 	if (putrequest(lpfile, request) < 0) {
6050Sstevel@tonic-gate 		detailed_error(svc, gettext("unable to save request: %s: %s"),
606*8569SJonathan.Ca@Sun.COM 		    lpfile, strerror(errno));
6070Sstevel@tonic-gate 		freerequest(request);
6080Sstevel@tonic-gate 		return (PAPI_DEVICE_ERROR);
6090Sstevel@tonic-gate 	}
6100Sstevel@tonic-gate 
6110Sstevel@tonic-gate 	status = lpsched_commit_job(svc, lpfile, &tmp);
6120Sstevel@tonic-gate 	if (status != PAPI_OK) {
6130Sstevel@tonic-gate 		unlink(lpfile);
6140Sstevel@tonic-gate 		freerequest(request);
6150Sstevel@tonic-gate 		return (status);
6160Sstevel@tonic-gate 	}
6170Sstevel@tonic-gate 
6180Sstevel@tonic-gate 	lpsched_request_to_job_attributes(request, j);
6190Sstevel@tonic-gate 
6200Sstevel@tonic-gate 	freerequest(request);
6210Sstevel@tonic-gate 
6220Sstevel@tonic-gate 	if ((c = strrchr(tmp, '-')) != NULL)
6230Sstevel@tonic-gate 		c++;
6240Sstevel@tonic-gate 	papiAttributeListAddInteger(&j->attributes, PAPI_ATTR_REPLACE,
625*8569SJonathan.Ca@Sun.COM 	    "job-id", atoi(c));
6260Sstevel@tonic-gate 	papiAttributeListAddString(&j->attributes, PAPI_ATTR_REPLACE,
627*8569SJonathan.Ca@Sun.COM 	    "job-uri", tmp);
6280Sstevel@tonic-gate 
6290Sstevel@tonic-gate 	return (PAPI_OK);
6300Sstevel@tonic-gate }
6310Sstevel@tonic-gate 
6320Sstevel@tonic-gate papi_status_t
6332264Sjacobs papiJobValidate(papi_service_t handle, char *printer,
6342264Sjacobs 		papi_attribute_t **job_attributes,
6352264Sjacobs 		papi_job_ticket_t *job_ticket,
6362264Sjacobs 		char **files, papi_job_t *job)
6370Sstevel@tonic-gate {
6380Sstevel@tonic-gate 	papi_status_t status;
6390Sstevel@tonic-gate 	papi_attribute_t **attributes = NULL;
6400Sstevel@tonic-gate 	int i;
6410Sstevel@tonic-gate 
6420Sstevel@tonic-gate 	papiAttributeListAddString(&attributes, PAPI_ATTR_REPLACE,
643*8569SJonathan.Ca@Sun.COM 	    "job-hold-until", "indefinite");
6440Sstevel@tonic-gate 	for (i = 0; job_attributes[i]; i++)
6450Sstevel@tonic-gate 		list_append(&attributes, job_attributes[i]);
6460Sstevel@tonic-gate 
6470Sstevel@tonic-gate 	status = papiJobSubmitByReference(handle, printer,
648*8569SJonathan.Ca@Sun.COM 	    (papi_attribute_t **)attributes,
649*8569SJonathan.Ca@Sun.COM 	    job_ticket, files, job);
6500Sstevel@tonic-gate 	if (status == PAPI_OK) {
6510Sstevel@tonic-gate 		int id = papiJobGetId(*job);
6520Sstevel@tonic-gate 
6530Sstevel@tonic-gate 		if (id != -1)
6540Sstevel@tonic-gate 			papiJobCancel(handle, printer, id);
6550Sstevel@tonic-gate 	}
6560Sstevel@tonic-gate 
6570Sstevel@tonic-gate 	attributes[1] = NULL;	/* after attr[0], they are in another list */
6580Sstevel@tonic-gate 	papiAttributeListFree(attributes);
6590Sstevel@tonic-gate 
6600Sstevel@tonic-gate 	return (status);
6610Sstevel@tonic-gate }
6620Sstevel@tonic-gate 
6630Sstevel@tonic-gate papi_status_t
6642264Sjacobs papiJobStreamOpen(papi_service_t handle, char *printer,
6652264Sjacobs 		papi_attribute_t **job_attributes,
6662264Sjacobs 		papi_job_ticket_t *job_ticket, papi_stream_t *stream)
6670Sstevel@tonic-gate {
6680Sstevel@tonic-gate 	papi_status_t status;
6690Sstevel@tonic-gate 	service_t *svc = handle;
6700Sstevel@tonic-gate 	job_stream_t *s = NULL;
6710Sstevel@tonic-gate 	char *request_id = NULL;
6720Sstevel@tonic-gate 	char lpfile[BUFSIZ];
6730Sstevel@tonic-gate 
6740Sstevel@tonic-gate 	if ((svc == NULL) || (printer == NULL) || (stream == NULL))
6750Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
6760Sstevel@tonic-gate 
6770Sstevel@tonic-gate 	if (job_ticket != NULL)
6780Sstevel@tonic-gate 		return (PAPI_OPERATION_NOT_SUPPORTED);
6790Sstevel@tonic-gate 
6800Sstevel@tonic-gate 	if ((*stream = s = calloc(1, sizeof (*s))) == NULL)
6810Sstevel@tonic-gate 		return (PAPI_TEMPORARY_ERROR);
6820Sstevel@tonic-gate 
6830Sstevel@tonic-gate 	/* 1 for data, 1 for the meta-data (-0) */
6840Sstevel@tonic-gate 	status = lpsched_alloc_files(svc, 2, &request_id);
6850Sstevel@tonic-gate 	if (status != PAPI_OK)
6860Sstevel@tonic-gate 		return (status);
6870Sstevel@tonic-gate 
6880Sstevel@tonic-gate 	s->request = create_request(svc, (char *)printer,
689*8569SJonathan.Ca@Sun.COM 	    (papi_attribute_t **)job_attributes);
6900Sstevel@tonic-gate 	snprintf(lpfile, sizeof (lpfile), "/var/spool/lp/temp/%s-1",
691*8569SJonathan.Ca@Sun.COM 	    request_id);
6920Sstevel@tonic-gate 	s->fd = open(lpfile, O_WRONLY);
6930Sstevel@tonic-gate 	addlist(&(s->request->file_list), lpfile);
6940Sstevel@tonic-gate 
6950Sstevel@tonic-gate #ifdef LP_USE_PAPI_ATTR
6960Sstevel@tonic-gate 	/*
6970Sstevel@tonic-gate 	 * store the job attributes in the PAPI job attribute file that was
6980Sstevel@tonic-gate 	 * created by lpsched_alloc_files(), the attributes will then pass
6990Sstevel@tonic-gate 	 * through lpsched and be given to the slow-filters and the printer's
7000Sstevel@tonic-gate 	 * interface script to process them
7010Sstevel@tonic-gate 	 */
7020Sstevel@tonic-gate 	snprintf(lpfile, sizeof (lpfile), "%s%s-%s",
703*8569SJonathan.Ca@Sun.COM 	    "/var/spool/lp/temp/", request_id, LP_PAPIATTRNAME);
7040Sstevel@tonic-gate 	status = psm_copy_attrsToFile(job_attributes, lpfile);
7050Sstevel@tonic-gate 	if (status != PAPI_OK) {
7060Sstevel@tonic-gate 		detailed_error(svc, "unable to copy attributes to file: %s: %s",
707*8569SJonathan.Ca@Sun.COM 		    lpfile, strerror(errno));
7080Sstevel@tonic-gate 		close(s->fd);
7090Sstevel@tonic-gate 		free(s);
7100Sstevel@tonic-gate 		return (PAPI_DEVICE_ERROR);
7110Sstevel@tonic-gate 	}
7120Sstevel@tonic-gate #endif
7130Sstevel@tonic-gate 
7140Sstevel@tonic-gate 	/* store the meta-data file */
7150Sstevel@tonic-gate 	snprintf(lpfile, sizeof (lpfile), "%s-0", request_id);
7160Sstevel@tonic-gate 	s->meta_data_file = strdup(lpfile);
7170Sstevel@tonic-gate 	if (putrequest(lpfile, s->request) < 0) {
7180Sstevel@tonic-gate 		detailed_error(svc, gettext("unable to save request: %s: %s"),
719*8569SJonathan.Ca@Sun.COM 		    lpfile, strerror(errno));
7200Sstevel@tonic-gate 		s->request = NULL;
7210Sstevel@tonic-gate 		return (PAPI_DEVICE_ERROR);
7220Sstevel@tonic-gate 	}
7230Sstevel@tonic-gate 
7240Sstevel@tonic-gate 	return (PAPI_OK);
7250Sstevel@tonic-gate }
7260Sstevel@tonic-gate 
7270Sstevel@tonic-gate papi_status_t
7280Sstevel@tonic-gate papiJobStreamWrite(papi_service_t handle,
7292264Sjacobs 		papi_stream_t stream, void *buffer, size_t buflen)
7300Sstevel@tonic-gate {
7310Sstevel@tonic-gate 	service_t *svc = handle;
7320Sstevel@tonic-gate 	job_stream_t *s = stream;
7330Sstevel@tonic-gate 
7340Sstevel@tonic-gate 	if ((svc == NULL) || (stream == NULL) || (buffer == NULL))
7350Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
7360Sstevel@tonic-gate 
7370Sstevel@tonic-gate 	if (write(s->fd, buffer, buflen) != buflen)
7380Sstevel@tonic-gate 		return (PAPI_DEVICE_ERROR);
7390Sstevel@tonic-gate 
7400Sstevel@tonic-gate 	return (PAPI_OK);
7410Sstevel@tonic-gate }
7420Sstevel@tonic-gate papi_status_t
7430Sstevel@tonic-gate papiJobStreamClose(papi_service_t handle,
7440Sstevel@tonic-gate 		papi_stream_t stream, papi_job_t *job)
7450Sstevel@tonic-gate {
7460Sstevel@tonic-gate 	papi_status_t status = PAPI_OK;
7470Sstevel@tonic-gate 	service_t *svc = handle;
7480Sstevel@tonic-gate 	job_stream_t *s = stream;
7490Sstevel@tonic-gate 	job_t *j = NULL;
7500Sstevel@tonic-gate 	char *tmp = NULL, *c;
7510Sstevel@tonic-gate 
7520Sstevel@tonic-gate 	if ((svc == NULL) || (stream == NULL) || (job == NULL))
7530Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
7540Sstevel@tonic-gate 
7550Sstevel@tonic-gate 	if ((*job = j = calloc(1, sizeof (*j))) == NULL)
7560Sstevel@tonic-gate 		return (PAPI_TEMPORARY_ERROR);
7570Sstevel@tonic-gate 
7580Sstevel@tonic-gate 	close(s->fd);
7590Sstevel@tonic-gate 
7600Sstevel@tonic-gate 	lpsched_request_to_job_attributes(s->request, j);
7610Sstevel@tonic-gate 
7620Sstevel@tonic-gate 	if (s->meta_data_file != NULL) {
7630Sstevel@tonic-gate 		status = lpsched_commit_job(svc, s->meta_data_file, &tmp);
7640Sstevel@tonic-gate 		if (status != PAPI_OK) {
7650Sstevel@tonic-gate 			unlink(s->meta_data_file);
7660Sstevel@tonic-gate 			return (status);
7670Sstevel@tonic-gate 		}
7680Sstevel@tonic-gate 		if ((c = strrchr(tmp, '-')) != NULL)
7690Sstevel@tonic-gate 			c++;
7700Sstevel@tonic-gate 		papiAttributeListAddInteger(&j->attributes, PAPI_ATTR_REPLACE,
771*8569SJonathan.Ca@Sun.COM 		    "job-id", atoi(c));
7720Sstevel@tonic-gate 		papiAttributeListAddString(&j->attributes, PAPI_ATTR_REPLACE,
773*8569SJonathan.Ca@Sun.COM 		    "job-uri", tmp);
7740Sstevel@tonic-gate 		free(s->meta_data_file);
7750Sstevel@tonic-gate 	}
7767253Sjacobs 	freerequest(s->request);
7770Sstevel@tonic-gate 	free(s);
7780Sstevel@tonic-gate 
7790Sstevel@tonic-gate 	return (PAPI_OK);
7800Sstevel@tonic-gate }
7810Sstevel@tonic-gate 
7820Sstevel@tonic-gate papi_status_t
7832264Sjacobs papiJobQuery(papi_service_t handle, char *printer, int32_t job_id,
7842264Sjacobs 		char **requested_attrs,
7850Sstevel@tonic-gate 		papi_job_t *job)
7860Sstevel@tonic-gate {
7870Sstevel@tonic-gate 	service_t *svc = handle;
7880Sstevel@tonic-gate 	job_t *j;
7890Sstevel@tonic-gate 	char *dest;
7900Sstevel@tonic-gate 	char req_id[32];
7910Sstevel@tonic-gate 	short rc;
7920Sstevel@tonic-gate 	char *form = NULL,
793*8569SJonathan.Ca@Sun.COM 	    *request_id = NULL,
794*8569SJonathan.Ca@Sun.COM 	    *charset = NULL,
795*8569SJonathan.Ca@Sun.COM 	    *user = NULL,
796*8569SJonathan.Ca@Sun.COM 	    *slabel = NULL,
797*8569SJonathan.Ca@Sun.COM 	    *file = NULL;
7980Sstevel@tonic-gate 	time_t date = 0;
7990Sstevel@tonic-gate 	size_t size = 0;
8000Sstevel@tonic-gate 	short  rank = 0,
801*8569SJonathan.Ca@Sun.COM 	    state = 0;
8020Sstevel@tonic-gate 
8030Sstevel@tonic-gate 	if ((handle == NULL) || (printer == NULL) || (job_id < 0))
8040Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
8050Sstevel@tonic-gate 
8060Sstevel@tonic-gate 	dest = printer_name_from_uri_id(printer, job_id);
8070Sstevel@tonic-gate 	snprintf(req_id, sizeof (req_id), "%s-%d", dest, job_id);
8080Sstevel@tonic-gate 	free(dest);
8090Sstevel@tonic-gate 
8100Sstevel@tonic-gate 	rc = snd_msg(svc, S_INQUIRE_REQUEST_RANK, 0, "", "", req_id, "", "");
8110Sstevel@tonic-gate 	if (rc < 0)
8120Sstevel@tonic-gate 		return (PAPI_SERVICE_UNAVAILABLE);
8130Sstevel@tonic-gate 
8140Sstevel@tonic-gate 	if (rcv_msg(svc, R_INQUIRE_REQUEST_RANK, &rc, &request_id,
815*8569SJonathan.Ca@Sun.COM 	    &user, &slabel, &size, &date, &state, &dest, &form,
816*8569SJonathan.Ca@Sun.COM 	    &charset, &rank, &file) < 0) {
8170Sstevel@tonic-gate 		detailed_error(svc,
818*8569SJonathan.Ca@Sun.COM 		    gettext("failed to read response from scheduler"));
8190Sstevel@tonic-gate 		return (PAPI_DEVICE_ERROR);
8200Sstevel@tonic-gate 	}
8210Sstevel@tonic-gate 
8220Sstevel@tonic-gate 	if ((request_id == NULL) || (request_id[0] == NULL))
8230Sstevel@tonic-gate 		return (PAPI_NOT_FOUND);
8240Sstevel@tonic-gate 
8250Sstevel@tonic-gate 	if ((*job = j = calloc(1, sizeof (*j))) == NULL)
8260Sstevel@tonic-gate 		return (PAPI_TEMPORARY_ERROR);
8270Sstevel@tonic-gate 
8281676Sjpk 	job_status_to_attributes(j, request_id, user, slabel, size, date, state,
829*8569SJonathan.Ca@Sun.COM 	    dest, form, charset, rank, file);
8300Sstevel@tonic-gate 
8310Sstevel@tonic-gate 	snprintf(req_id, sizeof (req_id), "%d-0", job_id);
8320Sstevel@tonic-gate 	lpsched_read_job_configuration(svc, j, req_id);
8330Sstevel@tonic-gate 
8340Sstevel@tonic-gate 	return (PAPI_OK);
8350Sstevel@tonic-gate }
8360Sstevel@tonic-gate 
8370Sstevel@tonic-gate papi_status_t
8382264Sjacobs papiJobMove(papi_service_t handle, char *printer, int32_t job_id,
8392264Sjacobs 		char *destination)
8402264Sjacobs {
8412264Sjacobs 	papi_status_t result = PAPI_OK;
8428266SNagaraj.Yedathore@Sun.COM 	long bits;
8432264Sjacobs 	service_t *svc = handle;
8442264Sjacobs 	char req_id[64];
8452264Sjacobs 	char *queue;
8462264Sjacobs 	char *user = NULL;
8472264Sjacobs 
8482264Sjacobs 	if ((svc == NULL) || (printer == NULL) || (job_id < 0) ||
8492264Sjacobs 	    (destination == NULL))
8502264Sjacobs 		return (PAPI_BAD_ARGUMENT);
8512264Sjacobs 
8522264Sjacobs 	queue = printer_name_from_uri_id(printer, job_id);
8532264Sjacobs 	snprintf(req_id, sizeof (req_id), "%s-%d", queue, job_id);
8542264Sjacobs 	free(queue);
8552264Sjacobs 
8562264Sjacobs 	if (papiAttributeListGetString(svc->attributes, NULL, "user-name",
857*8569SJonathan.Ca@Sun.COM 	    &user) == PAPI_OK) {
8582264Sjacobs 		REQUEST *r = getrequest(req_id);
8592264Sjacobs 
8602264Sjacobs 		if ((r != NULL) && (r->user != NULL) &&
8612264Sjacobs 		    (strcmp(r->user, user) != 0))
8622264Sjacobs 			result = PAPI_NOT_AUTHORIZED;
8632264Sjacobs 		freerequest(r);
8642264Sjacobs 	}
8652264Sjacobs 
8662264Sjacobs 	if (result == PAPI_OK) {
8672264Sjacobs 		short status = MOK;
8682264Sjacobs 		char *dest = printer_name_from_uri_id(destination, -1);
8692264Sjacobs 
8702264Sjacobs 		if ((snd_msg(svc, S_MOVE_REQUEST, req_id, dest) < 0) ||
8718266SNagaraj.Yedathore@Sun.COM 		    (rcv_msg(svc, R_MOVE_REQUEST, &status, &bits) < 0))
8722264Sjacobs 			status = MTRANSMITERR;
8732264Sjacobs 
8742264Sjacobs 		free(dest);
8752264Sjacobs 
8762264Sjacobs 		result = lpsched_status_to_papi_status(status);
8772264Sjacobs 	}
8782264Sjacobs 
8792264Sjacobs 	return (result);
8802264Sjacobs }
8812264Sjacobs 
8822264Sjacobs papi_status_t
8832264Sjacobs papiJobCancel(papi_service_t handle, char *printer, int32_t job_id)
8840Sstevel@tonic-gate {
8850Sstevel@tonic-gate 	papi_status_t result = PAPI_OK;
8860Sstevel@tonic-gate 	service_t *svc = handle;
8870Sstevel@tonic-gate 	char req_id[64];
8880Sstevel@tonic-gate 	char *dest;
8890Sstevel@tonic-gate 	char *user = NULL;
8900Sstevel@tonic-gate 
8910Sstevel@tonic-gate 	if ((svc == NULL) || (printer == NULL) || (job_id < 0))
8920Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
8930Sstevel@tonic-gate 
8940Sstevel@tonic-gate 	dest = printer_name_from_uri_id(printer, job_id);
8950Sstevel@tonic-gate 	snprintf(req_id, sizeof (req_id), "%s-%d", dest, job_id);
8960Sstevel@tonic-gate 	free(dest);
8970Sstevel@tonic-gate 
8980Sstevel@tonic-gate 	if (papiAttributeListGetString(svc->attributes, NULL, "user-name",
899*8569SJonathan.Ca@Sun.COM 	    &user) == PAPI_OK) {
9000Sstevel@tonic-gate 		REQUEST *r = getrequest(req_id);
9010Sstevel@tonic-gate 
9022264Sjacobs 		if ((r != NULL) && (r->user != NULL) &&
9032264Sjacobs 		    (strcmp(r->user, user) != 0))
9040Sstevel@tonic-gate 			result = PAPI_NOT_AUTHORIZED;
9050Sstevel@tonic-gate 		freerequest(r);
9060Sstevel@tonic-gate 	}
9070Sstevel@tonic-gate 
9080Sstevel@tonic-gate 	if (result == PAPI_OK) {
9090Sstevel@tonic-gate 		short status = MOK;
9100Sstevel@tonic-gate 
9110Sstevel@tonic-gate 		if ((snd_msg(svc, S_CANCEL_REQUEST, req_id) < 0) ||
9120Sstevel@tonic-gate 		    (rcv_msg(svc, R_CANCEL_REQUEST, &status) < 0))
9130Sstevel@tonic-gate 			status = MTRANSMITERR;
9140Sstevel@tonic-gate 
9150Sstevel@tonic-gate 		result = lpsched_status_to_papi_status(status);
9160Sstevel@tonic-gate 	}
9170Sstevel@tonic-gate 
9180Sstevel@tonic-gate 	return (result);
9190Sstevel@tonic-gate }
9200Sstevel@tonic-gate 
9210Sstevel@tonic-gate papi_status_t
9222264Sjacobs hold_release_job(papi_service_t handle, char *printer,
9232264Sjacobs 		int32_t job_id, int flag)
9240Sstevel@tonic-gate {
9250Sstevel@tonic-gate 	papi_status_t status;
9260Sstevel@tonic-gate 	service_t *svc = handle;
9270Sstevel@tonic-gate 	REQUEST *r = NULL;
9280Sstevel@tonic-gate 	char *file;
9290Sstevel@tonic-gate 	char *dest;
9300Sstevel@tonic-gate 
9310Sstevel@tonic-gate 	if ((svc == NULL) || (printer == NULL) || (job_id < 0))
9320Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
9330Sstevel@tonic-gate 
9340Sstevel@tonic-gate 	if ((status = authorized(svc, job_id)) != PAPI_OK)
9350Sstevel@tonic-gate 		return (status);
9360Sstevel@tonic-gate 
9370Sstevel@tonic-gate 	dest = printer_name_from_uri_id(printer, job_id);
9380Sstevel@tonic-gate 	status = lpsched_start_change(svc, dest, job_id, &file);
9390Sstevel@tonic-gate 	if (status != PAPI_OK)
9400Sstevel@tonic-gate 		return (status);
9410Sstevel@tonic-gate 
9420Sstevel@tonic-gate 	if ((r = getrequest(file)) != NULL) {
9430Sstevel@tonic-gate 		r->actions &= ~ACT_RESUME;
9442264Sjacobs 		switch (flag) {
9452264Sjacobs 		case 0:
9460Sstevel@tonic-gate 			r->actions |= ACT_HOLD;
9472264Sjacobs 			break;
9482264Sjacobs 		case 1:
9490Sstevel@tonic-gate 			r->actions |= ACT_RESUME;
9502264Sjacobs 			break;
9512264Sjacobs 		case 2:
9522264Sjacobs 			r->actions |= ACT_IMMEDIATE;
9532264Sjacobs 			break;
9542264Sjacobs 		}
9550Sstevel@tonic-gate 		if (putrequest(file, r) < 0) {
9560Sstevel@tonic-gate 			detailed_error(svc,
957*8569SJonathan.Ca@Sun.COM 			    gettext("failed to write job: %s: %s"),
958*8569SJonathan.Ca@Sun.COM 			    file, strerror(errno));
9592264Sjacobs 			freerequest(r);
9600Sstevel@tonic-gate 			return (PAPI_DEVICE_ERROR);
9610Sstevel@tonic-gate 		}
9622264Sjacobs 		freerequest(r);
9630Sstevel@tonic-gate 	} else {
9640Sstevel@tonic-gate 		detailed_error(svc, gettext("failed to read job: %s: %s"),
965*8569SJonathan.Ca@Sun.COM 		    file, strerror(errno));
9660Sstevel@tonic-gate 		return (PAPI_DEVICE_ERROR);
9670Sstevel@tonic-gate 	}
9680Sstevel@tonic-gate 
9690Sstevel@tonic-gate 	status = lpsched_end_change(svc, dest, job_id);
9700Sstevel@tonic-gate 
9710Sstevel@tonic-gate 	return (status);
9720Sstevel@tonic-gate }
9730Sstevel@tonic-gate 
9740Sstevel@tonic-gate papi_status_t
9752264Sjacobs papiJobHold(papi_service_t handle, char *printer, int32_t job_id)
9760Sstevel@tonic-gate {
9770Sstevel@tonic-gate 	return (hold_release_job(handle, printer, job_id, 0));
9780Sstevel@tonic-gate }
9790Sstevel@tonic-gate 
9800Sstevel@tonic-gate papi_status_t
9812264Sjacobs papiJobRelease(papi_service_t handle, char *printer, int32_t job_id)
9820Sstevel@tonic-gate {
9830Sstevel@tonic-gate 	return (hold_release_job(handle, printer, job_id, 1));
9840Sstevel@tonic-gate }
9850Sstevel@tonic-gate 
9860Sstevel@tonic-gate papi_status_t
9872264Sjacobs papiJobPromote(papi_service_t handle, char *printer, int32_t job_id)
9880Sstevel@tonic-gate {
9892264Sjacobs 	return (hold_release_job(handle, printer, job_id, 2));
9900Sstevel@tonic-gate }
9910Sstevel@tonic-gate 
9920Sstevel@tonic-gate papi_status_t
9932264Sjacobs papiJobModify(papi_service_t handle, char *printer, int32_t job_id,
9942264Sjacobs 		papi_attribute_t **attributes, papi_job_t *job)
9950Sstevel@tonic-gate {
9960Sstevel@tonic-gate 	papi_status_t status;
9970Sstevel@tonic-gate 	job_t *j = NULL;
9980Sstevel@tonic-gate 	service_t *svc = handle;
9990Sstevel@tonic-gate 	char *file = NULL;
10000Sstevel@tonic-gate 	char *dest;
10010Sstevel@tonic-gate 	REQUEST *r = NULL;
10020Sstevel@tonic-gate 	char lpfile[BUFSIZ];
10030Sstevel@tonic-gate 
10040Sstevel@tonic-gate 	if ((svc == NULL) || (printer == NULL) || (job_id < 0) ||
10050Sstevel@tonic-gate 	    (attributes == NULL))
10060Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
10070Sstevel@tonic-gate 
10080Sstevel@tonic-gate 	if ((*job = j = calloc(1, sizeof (*j))) == NULL)
10090Sstevel@tonic-gate 		return (PAPI_TEMPORARY_ERROR);
10100Sstevel@tonic-gate 
10110Sstevel@tonic-gate 	dest = printer_name_from_uri_id(printer, job_id);
10120Sstevel@tonic-gate 	status = lpsched_start_change(svc, dest, job_id, &file);
10130Sstevel@tonic-gate 	if (status != PAPI_OK)
10140Sstevel@tonic-gate 		return (status);
10150Sstevel@tonic-gate 
10160Sstevel@tonic-gate 	if ((r = getrequest(file)) != NULL) {
10170Sstevel@tonic-gate 		job_attributes_to_lpsched_request(handle, r,
1018*8569SJonathan.Ca@Sun.COM 		    (papi_attribute_t **)attributes);
10190Sstevel@tonic-gate #ifdef LP_USE_PAPI_ATTR
10200Sstevel@tonic-gate 		/*
10210Sstevel@tonic-gate 		 * store the job attributes in the PAPI job attribute file
10220Sstevel@tonic-gate 		 * that was created by the origonal job request. We need to
10230Sstevel@tonic-gate 		 * modify the attributes in the file as per the new attributes
10240Sstevel@tonic-gate 		 */
10250Sstevel@tonic-gate 		snprintf(lpfile, sizeof (lpfile), "%s%d-%s",
1026*8569SJonathan.Ca@Sun.COM 		    "/var/spool/lp/temp/", job_id, LP_PAPIATTRNAME);
10270Sstevel@tonic-gate 		status = psm_modifyAttrsFile(attributes, lpfile);
10280Sstevel@tonic-gate 		if (status != PAPI_OK) {
10290Sstevel@tonic-gate 			detailed_error(svc,
1030*8569SJonathan.Ca@Sun.COM 			    "unable to modify the attributes file: %s: %s",
1031*8569SJonathan.Ca@Sun.COM 			    lpfile, strerror(errno));
10320Sstevel@tonic-gate 			return (PAPI_DEVICE_ERROR);
10330Sstevel@tonic-gate 		}
10340Sstevel@tonic-gate #endif
10350Sstevel@tonic-gate 
10360Sstevel@tonic-gate 		if (putrequest(file, r) < 0) {
10370Sstevel@tonic-gate 			detailed_error(svc,
1038*8569SJonathan.Ca@Sun.COM 			    gettext("failed to write job: %s: %s"),
1039*8569SJonathan.Ca@Sun.COM 			    file, strerror(errno));
10400Sstevel@tonic-gate 			freerequest(r);
10410Sstevel@tonic-gate 			return (PAPI_DEVICE_ERROR);
10420Sstevel@tonic-gate 		}
10430Sstevel@tonic-gate 	} else {
10440Sstevel@tonic-gate 		detailed_error(svc, gettext("failed to read job: %s: %s"),
1045*8569SJonathan.Ca@Sun.COM 		    file, strerror(errno));
10460Sstevel@tonic-gate 		return (PAPI_DEVICE_ERROR);
10470Sstevel@tonic-gate 	}
10480Sstevel@tonic-gate 
10490Sstevel@tonic-gate 	status = lpsched_end_change(svc, dest, job_id);
10500Sstevel@tonic-gate 	lpsched_request_to_job_attributes(r, j);
10516725Sjacobs 
10526725Sjacobs 	papiAttributeListAddInteger(&j->attributes, PAPI_ATTR_REPLACE,
1053*8569SJonathan.Ca@Sun.COM 	    "job-id", job_id);
10546725Sjacobs 
10550Sstevel@tonic-gate 	freerequest(r);
10560Sstevel@tonic-gate 
10570Sstevel@tonic-gate 	return (status);
10580Sstevel@tonic-gate }
10590Sstevel@tonic-gate 
10600Sstevel@tonic-gate /*
10610Sstevel@tonic-gate  * Extension to PAPI, a variation of this is slated for post-1.0
10620Sstevel@tonic-gate  */
10630Sstevel@tonic-gate #define	DUMMY_FILE	"/var/spool/lp/fifos/FIFO"
10640Sstevel@tonic-gate 
10650Sstevel@tonic-gate papi_status_t
10662264Sjacobs papiJobCreate(papi_service_t handle, char *printer,
10672264Sjacobs 		papi_attribute_t **job_attributes,
10682264Sjacobs 		papi_job_ticket_t *job_ticket, papi_job_t *job)
10690Sstevel@tonic-gate {
10700Sstevel@tonic-gate 	papi_status_t status;
10710Sstevel@tonic-gate 	service_t *svc = handle;
10720Sstevel@tonic-gate 	job_t *j = NULL;
10730Sstevel@tonic-gate 	REQUEST *request;
10740Sstevel@tonic-gate 	char *request_id = NULL;
10750Sstevel@tonic-gate 	char *c;
10760Sstevel@tonic-gate 	char *tmp = NULL;
10770Sstevel@tonic-gate 	char metadata_file[MAXPATHLEN];
10780Sstevel@tonic-gate 
10790Sstevel@tonic-gate 	if ((svc == NULL) || (printer == NULL) || (job == NULL))
10800Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
10810Sstevel@tonic-gate 
10820Sstevel@tonic-gate 	if (job_ticket != NULL)
10830Sstevel@tonic-gate 		return (PAPI_JOB_TICKET_NOT_SUPPORTED);
10840Sstevel@tonic-gate 
10850Sstevel@tonic-gate 	if ((*job = j = calloc(1, sizeof (*j))) == NULL)
10860Sstevel@tonic-gate 		return (PAPI_TEMPORARY_ERROR);
10870Sstevel@tonic-gate 
10880Sstevel@tonic-gate 	/* 1 for the control file (-0) */
10890Sstevel@tonic-gate 	status = lpsched_alloc_files(svc, 1, &request_id);
10900Sstevel@tonic-gate 	if (status != PAPI_OK)
10910Sstevel@tonic-gate 		return (status);
10920Sstevel@tonic-gate 
10930Sstevel@tonic-gate 	/* convert the attributes to an lpsched REQUEST structure */
10940Sstevel@tonic-gate 	request = create_request(svc, (char *)printer,
1095*8569SJonathan.Ca@Sun.COM 	    (papi_attribute_t **)job_attributes);
10962264Sjacobs 	if (request == NULL)
10972264Sjacobs 		return (PAPI_TEMPORARY_ERROR);
10980Sstevel@tonic-gate 	addlist(&request->file_list, DUMMY_FILE);	/* add a dummy file */
10990Sstevel@tonic-gate 	request->actions |= ACT_HOLD;			/* hold the job */
11000Sstevel@tonic-gate 
11010Sstevel@tonic-gate #ifdef LP_USE_PAPI_ATTR
11020Sstevel@tonic-gate 	/*
11030Sstevel@tonic-gate 	 * store the job attributes in the PAPI job attribute file that was
11040Sstevel@tonic-gate 	 * created by lpsched_alloc_files(), the attributes will then pass
11050Sstevel@tonic-gate 	 * through lpsched and be given to the slow-filters and the printer's
11060Sstevel@tonic-gate 	 * interface script to process them
11070Sstevel@tonic-gate 	 */
11080Sstevel@tonic-gate 	snprintf(metadata_file, sizeof (metadata_file), "%s%s-%s",
1109*8569SJonathan.Ca@Sun.COM 	    "/var/spool/lp/temp/", request_id, LP_PAPIATTRNAME);
11100Sstevel@tonic-gate 	status = psm_copy_attrsToFile(job_attributes, metadata_file);
11110Sstevel@tonic-gate 	if (status != PAPI_OK) {
11120Sstevel@tonic-gate 		detailed_error(svc, "unable to copy attributes to file: %s: %s",
1113*8569SJonathan.Ca@Sun.COM 		    metadata_file, strerror(errno));
11142264Sjacobs 		free(request_id);
11150Sstevel@tonic-gate 		return (PAPI_DEVICE_ERROR);
11160Sstevel@tonic-gate 	}
11170Sstevel@tonic-gate #endif
11180Sstevel@tonic-gate 
11190Sstevel@tonic-gate 	/* store the REQUEST on disk */
11200Sstevel@tonic-gate 	snprintf(metadata_file, sizeof (metadata_file), "%s-0", request_id);
11212264Sjacobs 	free(request_id);
11220Sstevel@tonic-gate 	if (putrequest(metadata_file, request) < 0) {
11230Sstevel@tonic-gate 		detailed_error(svc, gettext("unable to save request: %s: %s"),
1124*8569SJonathan.Ca@Sun.COM 		    metadata_file, strerror(errno));
11250Sstevel@tonic-gate 		return (PAPI_DEVICE_ERROR);
11260Sstevel@tonic-gate 	}
11270Sstevel@tonic-gate 
11280Sstevel@tonic-gate 	status = lpsched_commit_job(svc, metadata_file, &tmp);
11290Sstevel@tonic-gate 	if (status != PAPI_OK) {
11300Sstevel@tonic-gate 		unlink(metadata_file);
11310Sstevel@tonic-gate 		return (status);
11320Sstevel@tonic-gate 	}
11330Sstevel@tonic-gate 
11340Sstevel@tonic-gate 	lpsched_request_to_job_attributes(request, j);
11350Sstevel@tonic-gate 
11360Sstevel@tonic-gate 	if ((c = strrchr(tmp, '-')) != NULL)
11370Sstevel@tonic-gate 		c++;
11380Sstevel@tonic-gate 	papiAttributeListAddInteger(&j->attributes, PAPI_ATTR_REPLACE,
1139*8569SJonathan.Ca@Sun.COM 	    "job-id", atoi(c));
11400Sstevel@tonic-gate 	papiAttributeListAddString(&j->attributes, PAPI_ATTR_REPLACE,
1141*8569SJonathan.Ca@Sun.COM 	    "job-uri", tmp);
11420Sstevel@tonic-gate 
11430Sstevel@tonic-gate 	return (PAPI_OK);
11440Sstevel@tonic-gate }
11450Sstevel@tonic-gate 
11460Sstevel@tonic-gate papi_status_t
11470Sstevel@tonic-gate papiJobCommit(papi_service_t handle, char *printer, int32_t id)
11480Sstevel@tonic-gate {
11490Sstevel@tonic-gate 	papi_status_t status = PAPI_OK;
11500Sstevel@tonic-gate 	service_t *svc = handle;
11510Sstevel@tonic-gate 	REQUEST *r = NULL;
11520Sstevel@tonic-gate 	char *metadata_file;
11530Sstevel@tonic-gate 	char *dest;
11540Sstevel@tonic-gate 
11550Sstevel@tonic-gate 	if ((svc == NULL) || (printer == NULL))
11560Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
11570Sstevel@tonic-gate 
11580Sstevel@tonic-gate 	dest = printer_name_from_uri_id(printer, id);
11590Sstevel@tonic-gate 	/* tell the scheduler that we want to change the job */
11600Sstevel@tonic-gate 	status = lpsched_start_change(svc, dest, id, &metadata_file);
11610Sstevel@tonic-gate 	if (status != PAPI_OK)
11620Sstevel@tonic-gate 		return (status);
11630Sstevel@tonic-gate 
11640Sstevel@tonic-gate 	if ((r = getrequest(metadata_file)) != NULL) {
11650Sstevel@tonic-gate 		r->actions &= ~ACT_RESUME;
11660Sstevel@tonic-gate 		r->actions |= ACT_RESUME;
11670Sstevel@tonic-gate 		dellist(&r->file_list, DUMMY_FILE);
11680Sstevel@tonic-gate 
11690Sstevel@tonic-gate 		if (putrequest(metadata_file, r) < 0) {
11700Sstevel@tonic-gate 			detailed_error(svc,
1171*8569SJonathan.Ca@Sun.COM 			    gettext("failed to write job: %s: %s"),
1172*8569SJonathan.Ca@Sun.COM 			    metadata_file, strerror(errno));
11732264Sjacobs 			freerequest(r);
11740Sstevel@tonic-gate 			return (PAPI_DEVICE_ERROR);
11750Sstevel@tonic-gate 		}
11760Sstevel@tonic-gate 	} else {
11770Sstevel@tonic-gate 		detailed_error(svc, gettext("failed to read job: %s: %s"),
1178*8569SJonathan.Ca@Sun.COM 		    metadata_file, strerror(errno));
11790Sstevel@tonic-gate 		return (PAPI_DEVICE_ERROR);
11800Sstevel@tonic-gate 	}
11810Sstevel@tonic-gate 
11820Sstevel@tonic-gate 	status = lpsched_end_change(svc, dest, id);
11830Sstevel@tonic-gate 	freerequest(r);
11840Sstevel@tonic-gate 
11850Sstevel@tonic-gate 	return (status);
11860Sstevel@tonic-gate }
11870Sstevel@tonic-gate 
11880Sstevel@tonic-gate papi_status_t
11890Sstevel@tonic-gate papiJobStreamAdd(papi_service_t handle, char *printer, int32_t id,
11900Sstevel@tonic-gate 		papi_stream_t *stream)
11910Sstevel@tonic-gate {
11920Sstevel@tonic-gate 	papi_status_t status;
11930Sstevel@tonic-gate 	service_t *svc = handle;
11940Sstevel@tonic-gate 	job_stream_t *s = NULL;
11950Sstevel@tonic-gate 	char *metadata_file = NULL;
11960Sstevel@tonic-gate 	char *dest;
11970Sstevel@tonic-gate 	char path[MAXPATHLEN];
11980Sstevel@tonic-gate 
11990Sstevel@tonic-gate 	/* allocate space for the stream */
12000Sstevel@tonic-gate 	if ((*stream = s = calloc(1, sizeof (*s))) == NULL)
12010Sstevel@tonic-gate 		return (PAPI_TEMPORARY_ERROR);
12020Sstevel@tonic-gate 
12030Sstevel@tonic-gate 	dest = printer_name_from_uri_id(printer, id);
12040Sstevel@tonic-gate 	/* create/open data file (only root or lp can really do this */
12050Sstevel@tonic-gate 	snprintf(path, sizeof (path), "/var/spool/lp/temp/%d-XXXXXX", id);
12060Sstevel@tonic-gate 	if ((s->fd = mkstemp(path)) < 0) {
12070Sstevel@tonic-gate 		detailed_error(svc, gettext("unable to create sink (%s): %s"),
1208*8569SJonathan.Ca@Sun.COM 		    path, strerror(errno));
12090Sstevel@tonic-gate 		free(s);
12100Sstevel@tonic-gate 		return (PAPI_NOT_AUTHORIZED);
12110Sstevel@tonic-gate 	}
12120Sstevel@tonic-gate 
12130Sstevel@tonic-gate 	/* add data file to job */
12140Sstevel@tonic-gate 	status = lpsched_start_change(svc, dest, id, &metadata_file);
12150Sstevel@tonic-gate 	if (status != PAPI_OK) {
12160Sstevel@tonic-gate 		close(s->fd);
12170Sstevel@tonic-gate 		free(s);
12180Sstevel@tonic-gate 		unlink(path);
12190Sstevel@tonic-gate 		return (status);
12200Sstevel@tonic-gate 	}
12210Sstevel@tonic-gate 
12220Sstevel@tonic-gate 	if ((s->request = getrequest(metadata_file)) == NULL) {
12230Sstevel@tonic-gate 		detailed_error(svc, gettext("unable to load request: %s: %s"),
1224*8569SJonathan.Ca@Sun.COM 		    metadata_file, strerror(errno));
12250Sstevel@tonic-gate 		close(s->fd);
12260Sstevel@tonic-gate 		free(s);
12270Sstevel@tonic-gate 		unlink(path);
12280Sstevel@tonic-gate 		return (PAPI_NOT_POSSIBLE);
12290Sstevel@tonic-gate 	}
12300Sstevel@tonic-gate 
12310Sstevel@tonic-gate 	addlist(&(s->request->file_list), path);
12320Sstevel@tonic-gate 
12330Sstevel@tonic-gate 	if (putrequest(metadata_file, s->request) < 0) {
12340Sstevel@tonic-gate 		detailed_error(svc, gettext("unable to save request: %s: %s"),
1235*8569SJonathan.Ca@Sun.COM 		    metadata_file, strerror(errno));
12360Sstevel@tonic-gate 		close(s->fd);
12370Sstevel@tonic-gate 		free(s);
12380Sstevel@tonic-gate 		unlink(path);
12390Sstevel@tonic-gate 		return (PAPI_NOT_POSSIBLE);
12400Sstevel@tonic-gate 	}
12410Sstevel@tonic-gate 
12420Sstevel@tonic-gate 	status = lpsched_end_change(svc, dest, id);
12430Sstevel@tonic-gate 
12440Sstevel@tonic-gate 	if (status != PAPI_OK)
12450Sstevel@tonic-gate 		return (status);
12460Sstevel@tonic-gate 
12470Sstevel@tonic-gate 	return (PAPI_OK);
12480Sstevel@tonic-gate }
1249