xref: /onnv-gate/usr/src/cmd/lp/lib/papi/job.c (revision 7253)
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 /*
226725Sjacobs  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate /*LINTLIBRARY*/
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #include <stdlib.h>
310Sstevel@tonic-gate #include <string.h>
320Sstevel@tonic-gate #include <unistd.h>
330Sstevel@tonic-gate #include <libintl.h>
340Sstevel@tonic-gate #include <pwd.h>
350Sstevel@tonic-gate #include <sys/stat.h>
360Sstevel@tonic-gate #include <papi_impl.h>
370Sstevel@tonic-gate 
382264Sjacobs 
392264Sjacobs /*
402264Sjacobs  * for an older application that may have been linked with a pre-v1.0
412264Sjacobs  * PAPI implementation.
422264Sjacobs  */
432264Sjacobs papi_status_t
442264Sjacobs papiAttributeListAdd(papi_attribute_t ***attrs, int flags, char *name,
452264Sjacobs 		papi_attribute_value_type_t type, papi_attribute_value_t *value)
462264Sjacobs {
472264Sjacobs 	return (papiAttributeListAddValue(attrs, flags, name, type, value));
482264Sjacobs }
492264Sjacobs 
500Sstevel@tonic-gate #ifdef LP_USE_PAPI_ATTR
512264Sjacobs static papi_status_t psm_modifyAttrsFile(papi_attribute_t **attrs, char *file);
522264Sjacobs static papi_status_t psm_modifyAttrsList(char *file, papi_attribute_t **attrs,
532264Sjacobs 					papi_attribute_t ***newAttrs);
540Sstevel@tonic-gate #endif
550Sstevel@tonic-gate 
560Sstevel@tonic-gate 
570Sstevel@tonic-gate void
580Sstevel@tonic-gate papiJobFree(papi_job_t job)
590Sstevel@tonic-gate {
600Sstevel@tonic-gate 	job_t *tmp = (job_t *)job;
610Sstevel@tonic-gate 
620Sstevel@tonic-gate 	if (tmp != NULL) {
630Sstevel@tonic-gate 		papiAttributeListFree(tmp->attributes);
640Sstevel@tonic-gate 		free(tmp);
650Sstevel@tonic-gate 	}
660Sstevel@tonic-gate }
670Sstevel@tonic-gate 
680Sstevel@tonic-gate void
690Sstevel@tonic-gate papiJobListFree(papi_job_t *jobs)
700Sstevel@tonic-gate {
710Sstevel@tonic-gate 	if (jobs != NULL) {
720Sstevel@tonic-gate 		int i;
730Sstevel@tonic-gate 
740Sstevel@tonic-gate 		for (i = 0; jobs[i] != NULL; i++) {
750Sstevel@tonic-gate 			papiJobFree(jobs[i]);
760Sstevel@tonic-gate 		}
770Sstevel@tonic-gate 		free(jobs);
780Sstevel@tonic-gate 	}
790Sstevel@tonic-gate }
800Sstevel@tonic-gate 
810Sstevel@tonic-gate papi_attribute_t **
820Sstevel@tonic-gate papiJobGetAttributeList(papi_job_t job)
830Sstevel@tonic-gate {
840Sstevel@tonic-gate 	job_t *tmp = (job_t *)job;
850Sstevel@tonic-gate 
860Sstevel@tonic-gate 	if (tmp != NULL)
870Sstevel@tonic-gate 		return (tmp->attributes);
880Sstevel@tonic-gate 
890Sstevel@tonic-gate 	return (NULL);
900Sstevel@tonic-gate }
910Sstevel@tonic-gate 
920Sstevel@tonic-gate char *
930Sstevel@tonic-gate papiJobGetPrinterName(papi_job_t job)
940Sstevel@tonic-gate {
950Sstevel@tonic-gate 	job_t *tmp = (job_t *)job;
960Sstevel@tonic-gate 	char *result = NULL;
970Sstevel@tonic-gate 
980Sstevel@tonic-gate 	if (tmp != NULL)
990Sstevel@tonic-gate 		papiAttributeListGetString(tmp->attributes, NULL,
1000Sstevel@tonic-gate 					"printer-name", &result);
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate 	return (result);
1030Sstevel@tonic-gate }
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate int32_t
1060Sstevel@tonic-gate papiJobGetId(papi_job_t job)
1070Sstevel@tonic-gate {
1080Sstevel@tonic-gate 	job_t *tmp = (job_t *)job;
1090Sstevel@tonic-gate 	int result = -1;
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate 	if (tmp != NULL)
1120Sstevel@tonic-gate 		papiAttributeListGetInteger(tmp->attributes, NULL, "job-id",
1130Sstevel@tonic-gate 					&result);
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate 	return (result);
1160Sstevel@tonic-gate }
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate static REQUEST *
1190Sstevel@tonic-gate create_request(papi_service_t svc, char *printer, papi_attribute_t **attributes)
1200Sstevel@tonic-gate {
121*7253Sjacobs 	REQUEST *r;
1220Sstevel@tonic-gate 
123*7253Sjacobs 	if ((r = calloc(1, sizeof (*r))) != NULL) {
124*7253Sjacobs 		r->priority = -1;
125*7253Sjacobs 		r->destination = printer_name_from_uri_id(printer, -1);
126*7253Sjacobs 		job_attributes_to_lpsched_request(svc, r, attributes);
127*7253Sjacobs 	}
1280Sstevel@tonic-gate 
129*7253Sjacobs 	return (r);
1300Sstevel@tonic-gate }
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate static papi_status_t
1330Sstevel@tonic-gate authorized(service_t *svc, int32_t id)
1340Sstevel@tonic-gate {
1350Sstevel@tonic-gate 	papi_status_t result = PAPI_NOT_AUTHORIZED;	/* assume the worst */
1360Sstevel@tonic-gate 	char file[32];
1370Sstevel@tonic-gate 	REQUEST *r;
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate 	snprintf(file, sizeof (file), "%d-0", id);
1400Sstevel@tonic-gate 	if ((r = getrequest(file)) != NULL) {
1410Sstevel@tonic-gate 		uid_t uid = getuid();
1420Sstevel@tonic-gate 		struct passwd *pw = NULL;
1430Sstevel@tonic-gate 		char *user = "intruder";	/* assume an intruder */
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate 		if ((pw = getpwuid(uid)) != NULL)
1460Sstevel@tonic-gate 			user = pw->pw_name;	/* use the process owner */
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate 		if ((uid == 0) || (uid == 71)) { /* root/lp can forge this */
1490Sstevel@tonic-gate 			papi_status_t s;
1500Sstevel@tonic-gate 			s = papiAttributeListGetString(svc->attributes, NULL,
1510Sstevel@tonic-gate 					"user-name", &user);
1520Sstevel@tonic-gate 			if (s != PAPI_OK)	/* true root/lp are almighty */
1530Sstevel@tonic-gate 				result = PAPI_OK;
1540Sstevel@tonic-gate 		}
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate 		if ((result != PAPI_OK) && (strcmp(user, r->user) == 0))
1570Sstevel@tonic-gate 			result = PAPI_OK;
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate 		freerequest(r);
1600Sstevel@tonic-gate 	} else
1610Sstevel@tonic-gate 		result = PAPI_NOT_FOUND;
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate 	return (result);
1640Sstevel@tonic-gate }
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate static papi_status_t
1672264Sjacobs copy_file(char *from, char *to)
1680Sstevel@tonic-gate {
1690Sstevel@tonic-gate 	int ifd, ofd;
1700Sstevel@tonic-gate 	char buf[BUFSIZ];
1710Sstevel@tonic-gate 	int rc;
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate 	if ((ifd = open(from, O_RDONLY)) < 0)
1740Sstevel@tonic-gate 		return (PAPI_DOCUMENT_ACCESS_ERROR);
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate 	if ((ofd = open(to, O_WRONLY)) < 0) {
1770Sstevel@tonic-gate 		close(ifd);
1780Sstevel@tonic-gate 		return (PAPI_NOT_POSSIBLE);
1790Sstevel@tonic-gate 	}
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate 	while ((rc = read(ifd, buf, sizeof (buf))) > 0)
1820Sstevel@tonic-gate 		write(ofd, buf, rc);
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate 	close(ifd);
1850Sstevel@tonic-gate 	close(ofd);
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate 	return (PAPI_OK);
1880Sstevel@tonic-gate }
1890Sstevel@tonic-gate 
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate #ifdef LP_USE_PAPI_ATTR
1920Sstevel@tonic-gate /*
1930Sstevel@tonic-gate  * *****************************************************************************
1940Sstevel@tonic-gate  *
1950Sstevel@tonic-gate  * Description: Create a file containing all the attributes in the attribute
1960Sstevel@tonic-gate  *              list passed to this function.
1970Sstevel@tonic-gate  *              This file is then passed through lpsched and given to either
1980Sstevel@tonic-gate  *              a slow-filter or to the printer's interface script to process
1990Sstevel@tonic-gate  *              the attributes.
2000Sstevel@tonic-gate  *
2010Sstevel@tonic-gate  * Parameters:  attrs - list of attributes and their values
2020Sstevel@tonic-gate  *              file  - file pathname to create and put the attributes into.
2030Sstevel@tonic-gate  *
2040Sstevel@tonic-gate  * *****************************************************************************
2050Sstevel@tonic-gate  */
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate static papi_status_t
2082264Sjacobs psm_copy_attrsToFile(papi_attribute_t **attrs, char *file)
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate {
2110Sstevel@tonic-gate 	papi_status_t result = PAPI_OK;
2122264Sjacobs 
2132264Sjacobs 	if ((attrs != NULL) && (*attrs != NULL)) {
2142264Sjacobs 		FILE *out = NULL;
2150Sstevel@tonic-gate 
2162264Sjacobs 		if ((out = fopen(file, "w")) != NULL) {
2172264Sjacobs 			papiAttributeListPrint(out, attrs, "");
2180Sstevel@tonic-gate 			fclose(out);
2192264Sjacobs 		} else {
2200Sstevel@tonic-gate 			result = PAPI_NOT_POSSIBLE;
2210Sstevel@tonic-gate 		}
2220Sstevel@tonic-gate 	}
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate 	return (result);
2250Sstevel@tonic-gate } /* psm_copy_attrsToFile */
2260Sstevel@tonic-gate 
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate /*
2290Sstevel@tonic-gate  * *****************************************************************************
2300Sstevel@tonic-gate  *
2310Sstevel@tonic-gate  * Description: Modify the given attribute 'file' with the attributes from the
2320Sstevel@tonic-gate  *              'attrs' list. Attributes already in the file will be replaced
2330Sstevel@tonic-gate  *              with the new value. New attributes will be added into the file.
2340Sstevel@tonic-gate  *
2350Sstevel@tonic-gate  * Parameters:  attrs - list of attributes and their values
2360Sstevel@tonic-gate  *              file  - file pathname to create and put the attributes into.
2370Sstevel@tonic-gate  *
2380Sstevel@tonic-gate  * *****************************************************************************
2390Sstevel@tonic-gate  */
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate static papi_status_t
2422264Sjacobs psm_modifyAttrsFile(papi_attribute_t **attrs, char *file)
2430Sstevel@tonic-gate 
2440Sstevel@tonic-gate {
2450Sstevel@tonic-gate 	papi_status_t result = PAPI_OK;
2460Sstevel@tonic-gate 	papi_attribute_t **newAttrs = NULL;
2470Sstevel@tonic-gate 	struct stat   tmpBuf;
2480Sstevel@tonic-gate 	FILE *fd = NULL;
2490Sstevel@tonic-gate 
2502264Sjacobs 	if ((attrs != NULL) && (*attrs != NULL) && (file != NULL)) {
2510Sstevel@tonic-gate 
2520Sstevel@tonic-gate 		/*
2530Sstevel@tonic-gate 		 * check file exist before try to modify it, if it doesn't
2540Sstevel@tonic-gate 		 * exist assume there is an error
2550Sstevel@tonic-gate 		 */
2562264Sjacobs 		if (stat(file, &tmpBuf) == 0) {
2570Sstevel@tonic-gate 			/*
2580Sstevel@tonic-gate 			 * if file is currently empty just write the given
2590Sstevel@tonic-gate 			 * attributes to the file otherwise exact the attributes
2600Sstevel@tonic-gate 			 * from the file and modify them accordingly before
2610Sstevel@tonic-gate 			 * writing them back to the file
2620Sstevel@tonic-gate 			 */
2632264Sjacobs 			if (tmpBuf.st_size == 0) {
2640Sstevel@tonic-gate 				newAttrs = (papi_attribute_t **)attrs;
2650Sstevel@tonic-gate 
2660Sstevel@tonic-gate 				fd = fopen(file, "w");
2672264Sjacobs 				if (fd != NULL) {
2680Sstevel@tonic-gate 					papiAttributeListPrint(fd,
2692264Sjacobs 							newAttrs, "");
2700Sstevel@tonic-gate 					fclose(fd);
2712264Sjacobs 				} else {
2720Sstevel@tonic-gate 					result = PAPI_NOT_POSSIBLE;
2730Sstevel@tonic-gate 				}
2742264Sjacobs 			} else {
2750Sstevel@tonic-gate 				result =
2760Sstevel@tonic-gate 				    psm_modifyAttrsList(file, attrs, &newAttrs);
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate 				fd = fopen(file, "w");
2792264Sjacobs 				if (fd != NULL) {
2800Sstevel@tonic-gate 					papiAttributeListPrint(fd,
2812264Sjacobs 								newAttrs, "");
2820Sstevel@tonic-gate 					fclose(fd);
2832264Sjacobs 				} else {
2840Sstevel@tonic-gate 					result = PAPI_NOT_POSSIBLE;
2850Sstevel@tonic-gate 				}
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate 				papiAttributeListFree(newAttrs);
2880Sstevel@tonic-gate 			}
2892264Sjacobs 		} else {
2900Sstevel@tonic-gate 			result = PAPI_NOT_POSSIBLE;
2910Sstevel@tonic-gate 		}
2920Sstevel@tonic-gate 	}
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate 	return (result);
2950Sstevel@tonic-gate } /* psm_modifyAttrsFile */
2960Sstevel@tonic-gate 
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate /*
2990Sstevel@tonic-gate  * *****************************************************************************
3000Sstevel@tonic-gate  *
3010Sstevel@tonic-gate  * Description: Extracts the attributes in the given attribute 'file' and
3020Sstevel@tonic-gate  *              creates a new list 'newAttrs' containing the modified list of
3030Sstevel@tonic-gate  *              attributes.
3040Sstevel@tonic-gate  *
3050Sstevel@tonic-gate  * Parameters:  file  - pathname of file containing attributes to be modified
3060Sstevel@tonic-gate  *              attrs - list of attributes and their values to modify
3070Sstevel@tonic-gate  *              newAttrs - returns the modified list of attributes
3080Sstevel@tonic-gate  *
3090Sstevel@tonic-gate  * *****************************************************************************
3100Sstevel@tonic-gate  */
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate static papi_status_t
3132264Sjacobs psm_modifyAttrsList(char *file, papi_attribute_t **attrs,
3140Sstevel@tonic-gate     papi_attribute_t ***newAttrs)
3150Sstevel@tonic-gate 
3160Sstevel@tonic-gate {
3170Sstevel@tonic-gate 	papi_status_t result = PAPI_OK;
3180Sstevel@tonic-gate 	papi_attribute_t  *nextAttr = NULL;
3190Sstevel@tonic-gate 	papi_attribute_value_t  **values = NULL;
3200Sstevel@tonic-gate 	void *iter = NULL;
3210Sstevel@tonic-gate 	FILE *fd = NULL;
3220Sstevel@tonic-gate 	register int fD = 0;
3230Sstevel@tonic-gate 	char aBuff[200];
3240Sstevel@tonic-gate 	char *a = NULL;
3250Sstevel@tonic-gate 	char *p = NULL;
3260Sstevel@tonic-gate 	int count = 0;
3270Sstevel@tonic-gate 	int n = 0;
3280Sstevel@tonic-gate 
3290Sstevel@tonic-gate 	fd = fopen(file, "r");
3302264Sjacobs 	if (fd != NULL) {
3310Sstevel@tonic-gate 		fD = fileno(fd);
3320Sstevel@tonic-gate 		a = &aBuff[0];
3330Sstevel@tonic-gate 		p = &aBuff[0];
3340Sstevel@tonic-gate 		count = read(fD, &aBuff[0], sizeof (aBuff) - 1);
3352264Sjacobs 		while ((result == PAPI_OK) && (count > 0)) {
3360Sstevel@tonic-gate 			aBuff[count+n] = '\0';
3372264Sjacobs 			if (count == sizeof (aBuff) - n - 1) {
3380Sstevel@tonic-gate 				p = strrchr(aBuff, '\n');
3392264Sjacobs 				if (p != NULL) {
3400Sstevel@tonic-gate 					/* terminate at last complete line */
3410Sstevel@tonic-gate 					*p = '\0';
3420Sstevel@tonic-gate 				}
3430Sstevel@tonic-gate 			}
3440Sstevel@tonic-gate 			result = papiAttributeListFromString(
3450Sstevel@tonic-gate 				newAttrs, PAPI_ATTR_EXCL, aBuff);
3460Sstevel@tonic-gate 
3472264Sjacobs 			if (result == PAPI_OK) {
3480Sstevel@tonic-gate 				/*
3490Sstevel@tonic-gate 				 * handle any part lines and then read the next
3500Sstevel@tonic-gate 				 * buffer from the file
3510Sstevel@tonic-gate 				 */
3520Sstevel@tonic-gate 				n = 0;
3532264Sjacobs 				if (p != a) {
3540Sstevel@tonic-gate 					p++; /* skip NL */
3550Sstevel@tonic-gate 					n = sizeof (aBuff) - 1 - (p - a);
3560Sstevel@tonic-gate 					strncpy(aBuff, p, n);
3570Sstevel@tonic-gate 				}
3580Sstevel@tonic-gate 				count = read(fD, &aBuff[n],
3590Sstevel@tonic-gate 					sizeof (aBuff) - n - 1);
3600Sstevel@tonic-gate 				p = &aBuff[0];
3610Sstevel@tonic-gate 			}
3620Sstevel@tonic-gate 		}
3630Sstevel@tonic-gate 		fclose(fd);
3640Sstevel@tonic-gate 	}
3650Sstevel@tonic-gate 
3660Sstevel@tonic-gate 	/* now modify the attribute list with the new attributes in 'attrs' */
3670Sstevel@tonic-gate 
3680Sstevel@tonic-gate 	nextAttr = papiAttributeListGetNext((papi_attribute_t **)attrs, &iter);
3692264Sjacobs 	while ((result == PAPI_OK) && (nextAttr != NULL)) {
3700Sstevel@tonic-gate 		values = nextAttr->values;
3710Sstevel@tonic-gate 
3722264Sjacobs 		if ((values != NULL) && (*values != NULL)) {
3732264Sjacobs 			result = papiAttributeListAddValue(newAttrs,
3740Sstevel@tonic-gate 						    PAPI_ATTR_REPLACE,
3750Sstevel@tonic-gate 						    nextAttr->name,
3760Sstevel@tonic-gate 						    nextAttr->type, *values);
3770Sstevel@tonic-gate 			values++;
3780Sstevel@tonic-gate 		}
3790Sstevel@tonic-gate 
3800Sstevel@tonic-gate 		while ((result == PAPI_OK) &&
3812264Sjacobs 			(values != NULL) && (*values != NULL)) {
3822264Sjacobs 			result = papiAttributeListAddValue(newAttrs,
3830Sstevel@tonic-gate 						    PAPI_ATTR_APPEND,
3840Sstevel@tonic-gate 						    nextAttr->name,
3850Sstevel@tonic-gate 						    nextAttr->type, *values);
3860Sstevel@tonic-gate 			values++;
3870Sstevel@tonic-gate 		}
3880Sstevel@tonic-gate 		nextAttr =
3890Sstevel@tonic-gate 		    papiAttributeListGetNext((papi_attribute_t **)attrs, &iter);
3900Sstevel@tonic-gate 	}
3910Sstevel@tonic-gate 
3920Sstevel@tonic-gate 	return (result);
3930Sstevel@tonic-gate } /* papi_modifyAttrsList() */
3940Sstevel@tonic-gate #endif
3950Sstevel@tonic-gate 
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate papi_status_t
3982264Sjacobs papiJobSubmit(papi_service_t handle, char *printer,
3992264Sjacobs 		papi_attribute_t **job_attributes,
4002264Sjacobs 		papi_job_ticket_t *job_ticket,
4012264Sjacobs 		char **files, papi_job_t *job)
4020Sstevel@tonic-gate {
4030Sstevel@tonic-gate 	papi_status_t status;
4040Sstevel@tonic-gate 	service_t *svc = handle;
4050Sstevel@tonic-gate 	job_t *j;
4060Sstevel@tonic-gate 	int file_no;
4070Sstevel@tonic-gate 	char *request_id = NULL;
4080Sstevel@tonic-gate 	REQUEST *request;
4090Sstevel@tonic-gate 	int i;
4100Sstevel@tonic-gate 	char *c;
4110Sstevel@tonic-gate 	char *tmp = NULL;
4120Sstevel@tonic-gate 	char lpfile[BUFSIZ];
4130Sstevel@tonic-gate 
4140Sstevel@tonic-gate 	if ((svc == NULL) || (printer == NULL) || (files == NULL) ||
4150Sstevel@tonic-gate 	    (job == NULL))
4160Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
4170Sstevel@tonic-gate 
4180Sstevel@tonic-gate 	if (job_ticket != NULL)
4190Sstevel@tonic-gate 		return (PAPI_OPERATION_NOT_SUPPORTED);
4200Sstevel@tonic-gate 
4210Sstevel@tonic-gate 	if (files != NULL)
4220Sstevel@tonic-gate 		for (file_no = 0; files[file_no] != NULL; file_no++)
4230Sstevel@tonic-gate 			if (access(files[file_no], R_OK) < 0) {
4240Sstevel@tonic-gate 				detailed_error(svc,
4250Sstevel@tonic-gate 					gettext("Cannot access file: %s: %s"),
4260Sstevel@tonic-gate 					files[file_no], strerror(errno));
4270Sstevel@tonic-gate 				return (PAPI_BAD_ARGUMENT);
4280Sstevel@tonic-gate 			}
4290Sstevel@tonic-gate 
4300Sstevel@tonic-gate 	if ((*job = j = calloc(1, sizeof (*j))) == NULL)
4310Sstevel@tonic-gate 		return (PAPI_TEMPORARY_ERROR);
4320Sstevel@tonic-gate 
4330Sstevel@tonic-gate 	/* file_no + 1 for the control file (-0) */
4340Sstevel@tonic-gate 	status = lpsched_alloc_files(svc, file_no + 1, &request_id);
4350Sstevel@tonic-gate 	if (status != PAPI_OK)
4360Sstevel@tonic-gate 		return (status);
4370Sstevel@tonic-gate 
4380Sstevel@tonic-gate 	request = create_request(svc, (char *)printer,
4390Sstevel@tonic-gate 				(papi_attribute_t **)job_attributes);
4400Sstevel@tonic-gate 
4410Sstevel@tonic-gate 	for (i = 0; files[i] != NULL; i++) {
4420Sstevel@tonic-gate 		papi_status_t status;
4430Sstevel@tonic-gate 		snprintf(lpfile, sizeof (lpfile), "%s%s-%d",
4440Sstevel@tonic-gate 			"/var/spool/lp/temp/", request_id, i+1);
4450Sstevel@tonic-gate 		status = copy_file(files[i], lpfile);
4460Sstevel@tonic-gate 		if (status != PAPI_OK) {
4470Sstevel@tonic-gate 			detailed_error(svc,
4480Sstevel@tonic-gate 				gettext("unable to copy: %s -> %s: %s"),
4490Sstevel@tonic-gate 				files[i], lpfile, strerror(errno));
4500Sstevel@tonic-gate 				freerequest(request);
4510Sstevel@tonic-gate 			return (PAPI_DEVICE_ERROR);
4520Sstevel@tonic-gate 		}
4530Sstevel@tonic-gate 		addlist(&(request->file_list), lpfile);
4540Sstevel@tonic-gate 	}
4550Sstevel@tonic-gate 
4560Sstevel@tonic-gate #ifdef LP_USE_PAPI_ATTR
4570Sstevel@tonic-gate 	/*
4580Sstevel@tonic-gate 	 * store the job attributes in the PAPI job attribute file that was
4590Sstevel@tonic-gate 	 * created by lpsched_alloc_files(), the attributes will then pass
4600Sstevel@tonic-gate 	 * through lpsched and be given to the slow-filters and the printer's
4610Sstevel@tonic-gate 	 * interface script to process them
4620Sstevel@tonic-gate 	 */
4630Sstevel@tonic-gate 	snprintf(lpfile, sizeof (lpfile), "%s%s-%s",
4640Sstevel@tonic-gate 		"/var/spool/lp/temp/", request_id, LP_PAPIATTRNAME);
4650Sstevel@tonic-gate 	status = psm_copy_attrsToFile(job_attributes, lpfile);
4660Sstevel@tonic-gate 	if (status != PAPI_OK) {
4670Sstevel@tonic-gate 		detailed_error(svc, "unable to copy attributes to file: %s: %s",
4680Sstevel@tonic-gate 				lpfile, strerror(errno));
4690Sstevel@tonic-gate 		return (PAPI_DEVICE_ERROR);
4700Sstevel@tonic-gate 	}
4710Sstevel@tonic-gate #endif
4720Sstevel@tonic-gate 
4730Sstevel@tonic-gate 	/* store the meta-data file */
4740Sstevel@tonic-gate 	snprintf(lpfile, sizeof (lpfile), "%s-0", request_id);
4750Sstevel@tonic-gate 	if (putrequest(lpfile, request) < 0) {
4760Sstevel@tonic-gate 		detailed_error(svc, gettext("unable to save request: %s: %s"),
4770Sstevel@tonic-gate 			lpfile, strerror(errno));
4780Sstevel@tonic-gate 		freerequest(request);
4790Sstevel@tonic-gate 		return (PAPI_DEVICE_ERROR);
4800Sstevel@tonic-gate 	}
4810Sstevel@tonic-gate 
4820Sstevel@tonic-gate 	status = lpsched_commit_job(svc, lpfile, &tmp);
4830Sstevel@tonic-gate 	if (status != PAPI_OK) {
4840Sstevel@tonic-gate 		unlink(lpfile);
4850Sstevel@tonic-gate 		freerequest(request);
4860Sstevel@tonic-gate 		return (status);
4870Sstevel@tonic-gate 	}
4880Sstevel@tonic-gate 
4890Sstevel@tonic-gate 	lpsched_request_to_job_attributes(request, j);
4900Sstevel@tonic-gate 	freerequest(request);
4910Sstevel@tonic-gate 
4920Sstevel@tonic-gate 	if ((c = strrchr(tmp, '-')) != NULL)
4930Sstevel@tonic-gate 		c++;
4940Sstevel@tonic-gate 	papiAttributeListAddInteger(&j->attributes, PAPI_ATTR_REPLACE,
4950Sstevel@tonic-gate 			"job-id", atoi(c));
4960Sstevel@tonic-gate 	papiAttributeListAddString(&j->attributes, PAPI_ATTR_REPLACE,
4970Sstevel@tonic-gate 			"job-uri", tmp);
4980Sstevel@tonic-gate 
4990Sstevel@tonic-gate 	return (PAPI_OK);
5000Sstevel@tonic-gate }
5010Sstevel@tonic-gate 
5020Sstevel@tonic-gate papi_status_t
5032264Sjacobs papiJobSubmitByReference(papi_service_t handle, char *printer,
5042264Sjacobs 		papi_attribute_t **job_attributes,
5052264Sjacobs 		papi_job_ticket_t *job_ticket,
5062264Sjacobs 		char **files, papi_job_t *job)
5070Sstevel@tonic-gate {
5080Sstevel@tonic-gate 	service_t *svc = handle;
5090Sstevel@tonic-gate 	job_t *j;
5100Sstevel@tonic-gate 	int file_no;
5110Sstevel@tonic-gate 	short status;
5120Sstevel@tonic-gate 	char *request_id = NULL;
5130Sstevel@tonic-gate 	REQUEST *request;
5140Sstevel@tonic-gate 	char *c;
5150Sstevel@tonic-gate 	char *tmp = NULL;
5160Sstevel@tonic-gate 	char lpfile[BUFSIZ];
5170Sstevel@tonic-gate 	char **file_list = NULL;
5180Sstevel@tonic-gate 
5190Sstevel@tonic-gate 	if ((svc == NULL) || (printer == NULL) || (files == NULL) ||
5200Sstevel@tonic-gate 	    (job == NULL))
5210Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
5220Sstevel@tonic-gate 
5230Sstevel@tonic-gate 	if (job_ticket != NULL)
5240Sstevel@tonic-gate 		return (PAPI_OPERATION_NOT_SUPPORTED);
5250Sstevel@tonic-gate 
5260Sstevel@tonic-gate 	if (files != NULL)
5270Sstevel@tonic-gate 		for (file_no = 0; files[file_no] != NULL; file_no++) {
5280Sstevel@tonic-gate 			if (access(files[file_no], R_OK) < 0) {
5290Sstevel@tonic-gate 				detailed_error(svc,
5300Sstevel@tonic-gate 					gettext("Cannot access file: %s: %s"),
5310Sstevel@tonic-gate 					files[file_no], strerror(errno));
5322660Sjacobs 				return (PAPI_DOCUMENT_ACCESS_ERROR);
5330Sstevel@tonic-gate 			}
5342660Sjacobs 			if (files[file_no][0] != '/') {
5352660Sjacobs 				char path[MAXPATHLEN];
5362660Sjacobs 
5372660Sjacobs 				if (getcwd(path, sizeof (path)) == NULL) {
5382660Sjacobs 					detailed_error(svc, gettext(
5392660Sjacobs 						"getcwd for file: %s: %s"),
5402660Sjacobs 						files[file_no],
5412660Sjacobs 						strerror(errno));
5422660Sjacobs 					return (PAPI_DOCUMENT_ACCESS_ERROR);
5432660Sjacobs 				}
5442660Sjacobs 				strlcat(path, "/", sizeof (path));
5452660Sjacobs 				if (strlcat(path, files[file_no], sizeof (path))
5462660Sjacobs 						>= sizeof (path)) {
5472660Sjacobs 					detailed_error(svc, gettext(
5482660Sjacobs 						"pathname too long: %s"),
5492660Sjacobs 						files[file_no]);
5502660Sjacobs 					return (PAPI_DOCUMENT_ACCESS_ERROR);
5512660Sjacobs 				}
5522660Sjacobs 				addlist(&file_list, path);
5532660Sjacobs 			} else
5542660Sjacobs 				addlist(&file_list, (char *)files[file_no]);
5550Sstevel@tonic-gate 		}
5560Sstevel@tonic-gate 
5570Sstevel@tonic-gate 	if ((*job = j = calloc(1, sizeof (*j))) == NULL)
5580Sstevel@tonic-gate 		return (PAPI_TEMPORARY_ERROR);
5590Sstevel@tonic-gate 
5600Sstevel@tonic-gate 	/* 1 for the control file (-0) */
5610Sstevel@tonic-gate 	status = lpsched_alloc_files(svc, 1, &request_id);
5620Sstevel@tonic-gate 	if (status != PAPI_OK)
5630Sstevel@tonic-gate 		return (status);
5640Sstevel@tonic-gate 
5650Sstevel@tonic-gate 	request = create_request(svc, (char *)printer,
5660Sstevel@tonic-gate 				(papi_attribute_t **)job_attributes);
5670Sstevel@tonic-gate 	request->file_list = file_list;
5680Sstevel@tonic-gate 
5690Sstevel@tonic-gate #ifdef LP_USE_PAPI_ATTR
5700Sstevel@tonic-gate 	/*
5710Sstevel@tonic-gate 	 * store the job attributes in the PAPI job attribute file that was
5720Sstevel@tonic-gate 	 * created by lpsched_alloc_files(), the attributes will then pass
5730Sstevel@tonic-gate 	 * through lpsched and be given to the slow-filters and the printer's
5740Sstevel@tonic-gate 	 * interface script to process them
5750Sstevel@tonic-gate 	 */
5760Sstevel@tonic-gate 	snprintf(lpfile, sizeof (lpfile), "%s%s-%s",
5770Sstevel@tonic-gate 		"/var/spool/lp/temp/", request_id, LP_PAPIATTRNAME);
5780Sstevel@tonic-gate 	status = psm_copy_attrsToFile(job_attributes, lpfile);
5790Sstevel@tonic-gate 	if (status != PAPI_OK) {
5800Sstevel@tonic-gate 		detailed_error(svc, "unable to copy attributes to file: %s: %s",
5810Sstevel@tonic-gate 				lpfile, strerror(errno));
5820Sstevel@tonic-gate 		return (PAPI_DEVICE_ERROR);
5830Sstevel@tonic-gate 	}
5840Sstevel@tonic-gate #endif
5850Sstevel@tonic-gate 
5860Sstevel@tonic-gate 	/* store the meta-data file */
5870Sstevel@tonic-gate 	snprintf(lpfile, sizeof (lpfile), "%s-0", request_id);
5880Sstevel@tonic-gate 	if (putrequest(lpfile, request) < 0) {
5890Sstevel@tonic-gate 		detailed_error(svc, gettext("unable to save request: %s: %s"),
5900Sstevel@tonic-gate 			lpfile, strerror(errno));
5910Sstevel@tonic-gate 		freerequest(request);
5920Sstevel@tonic-gate 		return (PAPI_DEVICE_ERROR);
5930Sstevel@tonic-gate 	}
5940Sstevel@tonic-gate 
5950Sstevel@tonic-gate 	status = lpsched_commit_job(svc, lpfile, &tmp);
5960Sstevel@tonic-gate 	if (status != PAPI_OK) {
5970Sstevel@tonic-gate 		unlink(lpfile);
5980Sstevel@tonic-gate 		freerequest(request);
5990Sstevel@tonic-gate 		return (status);
6000Sstevel@tonic-gate 	}
6010Sstevel@tonic-gate 
6020Sstevel@tonic-gate 	lpsched_request_to_job_attributes(request, j);
6030Sstevel@tonic-gate 
6040Sstevel@tonic-gate 	freerequest(request);
6050Sstevel@tonic-gate 
6060Sstevel@tonic-gate 	if ((c = strrchr(tmp, '-')) != NULL)
6070Sstevel@tonic-gate 		c++;
6080Sstevel@tonic-gate 	papiAttributeListAddInteger(&j->attributes, PAPI_ATTR_REPLACE,
6090Sstevel@tonic-gate 			"job-id", atoi(c));
6100Sstevel@tonic-gate 	papiAttributeListAddString(&j->attributes, PAPI_ATTR_REPLACE,
6110Sstevel@tonic-gate 			"job-uri", tmp);
6120Sstevel@tonic-gate 
6130Sstevel@tonic-gate 	return (PAPI_OK);
6140Sstevel@tonic-gate }
6150Sstevel@tonic-gate 
6160Sstevel@tonic-gate papi_status_t
6172264Sjacobs papiJobValidate(papi_service_t handle, char *printer,
6182264Sjacobs 		papi_attribute_t **job_attributes,
6192264Sjacobs 		papi_job_ticket_t *job_ticket,
6202264Sjacobs 		char **files, papi_job_t *job)
6210Sstevel@tonic-gate {
6220Sstevel@tonic-gate 	papi_status_t status;
6230Sstevel@tonic-gate 	papi_attribute_t **attributes = NULL;
6240Sstevel@tonic-gate 	int i;
6250Sstevel@tonic-gate 
6260Sstevel@tonic-gate 	papiAttributeListAddString(&attributes, PAPI_ATTR_REPLACE,
6276725Sjacobs 			"job-hold-until", "indefinite");
6280Sstevel@tonic-gate 	for (i = 0; job_attributes[i]; i++)
6290Sstevel@tonic-gate 		list_append(&attributes, job_attributes[i]);
6300Sstevel@tonic-gate 
6310Sstevel@tonic-gate 	status = papiJobSubmitByReference(handle, printer,
6322264Sjacobs 				(papi_attribute_t **)attributes,
6330Sstevel@tonic-gate 				job_ticket, files, job);
6340Sstevel@tonic-gate 	if (status == PAPI_OK) {
6350Sstevel@tonic-gate 		int id = papiJobGetId(*job);
6360Sstevel@tonic-gate 
6370Sstevel@tonic-gate 		if (id != -1)
6380Sstevel@tonic-gate 			papiJobCancel(handle, printer, id);
6390Sstevel@tonic-gate 	}
6400Sstevel@tonic-gate 
6410Sstevel@tonic-gate 	attributes[1] = NULL;	/* after attr[0], they are in another list */
6420Sstevel@tonic-gate 	papiAttributeListFree(attributes);
6430Sstevel@tonic-gate 
6440Sstevel@tonic-gate 	return (status);
6450Sstevel@tonic-gate }
6460Sstevel@tonic-gate 
6470Sstevel@tonic-gate papi_status_t
6482264Sjacobs papiJobStreamOpen(papi_service_t handle, char *printer,
6492264Sjacobs 		papi_attribute_t **job_attributes,
6502264Sjacobs 		papi_job_ticket_t *job_ticket, papi_stream_t *stream)
6510Sstevel@tonic-gate {
6520Sstevel@tonic-gate 	papi_status_t status;
6530Sstevel@tonic-gate 	service_t *svc = handle;
6540Sstevel@tonic-gate 	job_stream_t *s = NULL;
6550Sstevel@tonic-gate 	char *request_id = NULL;
6560Sstevel@tonic-gate 	char lpfile[BUFSIZ];
6570Sstevel@tonic-gate 
6580Sstevel@tonic-gate 	if ((svc == NULL) || (printer == NULL) || (stream == NULL))
6590Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
6600Sstevel@tonic-gate 
6610Sstevel@tonic-gate 	if (job_ticket != NULL)
6620Sstevel@tonic-gate 		return (PAPI_OPERATION_NOT_SUPPORTED);
6630Sstevel@tonic-gate 
6640Sstevel@tonic-gate 	if ((*stream = s = calloc(1, sizeof (*s))) == NULL)
6650Sstevel@tonic-gate 		return (PAPI_TEMPORARY_ERROR);
6660Sstevel@tonic-gate 
6670Sstevel@tonic-gate 	/* 1 for data, 1 for the meta-data (-0) */
6680Sstevel@tonic-gate 	status = lpsched_alloc_files(svc, 2, &request_id);
6690Sstevel@tonic-gate 	if (status != PAPI_OK)
6700Sstevel@tonic-gate 		return (status);
6710Sstevel@tonic-gate 
6720Sstevel@tonic-gate 	s->request = create_request(svc, (char *)printer,
6730Sstevel@tonic-gate 				(papi_attribute_t **)job_attributes);
6740Sstevel@tonic-gate 	snprintf(lpfile, sizeof (lpfile), "/var/spool/lp/temp/%s-1",
6750Sstevel@tonic-gate 							request_id);
6760Sstevel@tonic-gate 	s->fd = open(lpfile, O_WRONLY);
6770Sstevel@tonic-gate 	addlist(&(s->request->file_list), lpfile);
6780Sstevel@tonic-gate 
6790Sstevel@tonic-gate #ifdef LP_USE_PAPI_ATTR
6800Sstevel@tonic-gate 	/*
6810Sstevel@tonic-gate 	 * store the job attributes in the PAPI job attribute file that was
6820Sstevel@tonic-gate 	 * created by lpsched_alloc_files(), the attributes will then pass
6830Sstevel@tonic-gate 	 * through lpsched and be given to the slow-filters and the printer's
6840Sstevel@tonic-gate 	 * interface script to process them
6850Sstevel@tonic-gate 	 */
6860Sstevel@tonic-gate 	snprintf(lpfile, sizeof (lpfile), "%s%s-%s",
6870Sstevel@tonic-gate 		"/var/spool/lp/temp/", request_id, LP_PAPIATTRNAME);
6880Sstevel@tonic-gate 	status = psm_copy_attrsToFile(job_attributes, lpfile);
6890Sstevel@tonic-gate 	if (status != PAPI_OK) {
6900Sstevel@tonic-gate 		detailed_error(svc, "unable to copy attributes to file: %s: %s",
6910Sstevel@tonic-gate 				lpfile, strerror(errno));
6920Sstevel@tonic-gate 		close(s->fd);
6930Sstevel@tonic-gate 		free(s);
6940Sstevel@tonic-gate 		return (PAPI_DEVICE_ERROR);
6950Sstevel@tonic-gate 	}
6960Sstevel@tonic-gate #endif
6970Sstevel@tonic-gate 
6980Sstevel@tonic-gate 	/* store the meta-data file */
6990Sstevel@tonic-gate 	snprintf(lpfile, sizeof (lpfile), "%s-0", request_id);
7000Sstevel@tonic-gate 	s->meta_data_file = strdup(lpfile);
7010Sstevel@tonic-gate 	if (putrequest(lpfile, s->request) < 0) {
7020Sstevel@tonic-gate 		detailed_error(svc, gettext("unable to save request: %s: %s"),
7030Sstevel@tonic-gate 			lpfile, strerror(errno));
7040Sstevel@tonic-gate 		s->request = NULL;
7050Sstevel@tonic-gate 		return (PAPI_DEVICE_ERROR);
7060Sstevel@tonic-gate 	}
7070Sstevel@tonic-gate 
7080Sstevel@tonic-gate 	return (PAPI_OK);
7090Sstevel@tonic-gate }
7100Sstevel@tonic-gate 
7110Sstevel@tonic-gate papi_status_t
7120Sstevel@tonic-gate papiJobStreamWrite(papi_service_t handle,
7132264Sjacobs 		papi_stream_t stream, void *buffer, size_t buflen)
7140Sstevel@tonic-gate {
7150Sstevel@tonic-gate 	service_t *svc = handle;
7160Sstevel@tonic-gate 	job_stream_t *s = stream;
7170Sstevel@tonic-gate 
7180Sstevel@tonic-gate 	if ((svc == NULL) || (stream == NULL) || (buffer == NULL))
7190Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
7200Sstevel@tonic-gate 
7210Sstevel@tonic-gate 	if (write(s->fd, buffer, buflen) != buflen)
7220Sstevel@tonic-gate 		return (PAPI_DEVICE_ERROR);
7230Sstevel@tonic-gate 
7240Sstevel@tonic-gate 	return (PAPI_OK);
7250Sstevel@tonic-gate }
7260Sstevel@tonic-gate papi_status_t
7270Sstevel@tonic-gate papiJobStreamClose(papi_service_t handle,
7280Sstevel@tonic-gate 		papi_stream_t stream, papi_job_t *job)
7290Sstevel@tonic-gate {
7300Sstevel@tonic-gate 	papi_status_t status = PAPI_OK;
7310Sstevel@tonic-gate 	service_t *svc = handle;
7320Sstevel@tonic-gate 	job_stream_t *s = stream;
7330Sstevel@tonic-gate 	job_t *j = NULL;
7340Sstevel@tonic-gate 	char *tmp = NULL, *c;
7350Sstevel@tonic-gate 
7360Sstevel@tonic-gate 	if ((svc == NULL) || (stream == NULL) || (job == NULL))
7370Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
7380Sstevel@tonic-gate 
7390Sstevel@tonic-gate 	if ((*job = j = calloc(1, sizeof (*j))) == NULL)
7400Sstevel@tonic-gate 		return (PAPI_TEMPORARY_ERROR);
7410Sstevel@tonic-gate 
7420Sstevel@tonic-gate 	close(s->fd);
7430Sstevel@tonic-gate 
7440Sstevel@tonic-gate 	lpsched_request_to_job_attributes(s->request, j);
7450Sstevel@tonic-gate 
7460Sstevel@tonic-gate 	if (s->meta_data_file != NULL) {
7470Sstevel@tonic-gate 		status = lpsched_commit_job(svc, s->meta_data_file, &tmp);
7480Sstevel@tonic-gate 		if (status != PAPI_OK) {
7490Sstevel@tonic-gate 			unlink(s->meta_data_file);
7500Sstevel@tonic-gate 			return (status);
7510Sstevel@tonic-gate 		}
7520Sstevel@tonic-gate 		if ((c = strrchr(tmp, '-')) != NULL)
7530Sstevel@tonic-gate 			c++;
7540Sstevel@tonic-gate 		papiAttributeListAddInteger(&j->attributes, PAPI_ATTR_REPLACE,
7550Sstevel@tonic-gate 			"job-id", atoi(c));
7560Sstevel@tonic-gate 		papiAttributeListAddString(&j->attributes, PAPI_ATTR_REPLACE,
7570Sstevel@tonic-gate 			"job-uri", tmp);
7580Sstevel@tonic-gate 		free(s->meta_data_file);
7590Sstevel@tonic-gate 	}
760*7253Sjacobs 	freerequest(s->request);
7610Sstevel@tonic-gate 	free(s);
7620Sstevel@tonic-gate 
7630Sstevel@tonic-gate 	return (PAPI_OK);
7640Sstevel@tonic-gate }
7650Sstevel@tonic-gate 
7660Sstevel@tonic-gate papi_status_t
7672264Sjacobs papiJobQuery(papi_service_t handle, char *printer, int32_t job_id,
7682264Sjacobs 		char **requested_attrs,
7690Sstevel@tonic-gate 		papi_job_t *job)
7700Sstevel@tonic-gate {
7710Sstevel@tonic-gate 	service_t *svc = handle;
7720Sstevel@tonic-gate 	job_t *j;
7730Sstevel@tonic-gate 	char *dest;
7740Sstevel@tonic-gate 	char req_id[32];
7750Sstevel@tonic-gate 	short rc;
7760Sstevel@tonic-gate 	char *form = NULL,
7770Sstevel@tonic-gate 		*request_id = NULL,
7780Sstevel@tonic-gate 		*charset = NULL,
7790Sstevel@tonic-gate 		*user = NULL,
7801676Sjpk 		*slabel = NULL,
7810Sstevel@tonic-gate 		*file = NULL;
7820Sstevel@tonic-gate 	time_t date = 0;
7830Sstevel@tonic-gate 	size_t size = 0;
7840Sstevel@tonic-gate 	short  rank = 0,
7850Sstevel@tonic-gate 		state = 0;
7860Sstevel@tonic-gate 
7870Sstevel@tonic-gate 	if ((handle == NULL) || (printer == NULL) || (job_id < 0))
7880Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
7890Sstevel@tonic-gate 
7900Sstevel@tonic-gate 	dest = printer_name_from_uri_id(printer, job_id);
7910Sstevel@tonic-gate 	snprintf(req_id, sizeof (req_id), "%s-%d", dest, job_id);
7920Sstevel@tonic-gate 	free(dest);
7930Sstevel@tonic-gate 
7940Sstevel@tonic-gate 	rc = snd_msg(svc, S_INQUIRE_REQUEST_RANK, 0, "", "", req_id, "", "");
7950Sstevel@tonic-gate 	if (rc < 0)
7960Sstevel@tonic-gate 		return (PAPI_SERVICE_UNAVAILABLE);
7970Sstevel@tonic-gate 
7980Sstevel@tonic-gate 	if (rcv_msg(svc, R_INQUIRE_REQUEST_RANK, &rc, &request_id,
7991676Sjpk 			&user, &slabel, &size, &date, &state, &dest, &form,
8000Sstevel@tonic-gate 			&charset, &rank, &file) < 0) {
8010Sstevel@tonic-gate 		detailed_error(svc,
8020Sstevel@tonic-gate 			gettext("failed to read response from scheduler"));
8030Sstevel@tonic-gate 		return (PAPI_DEVICE_ERROR);
8040Sstevel@tonic-gate 	}
8050Sstevel@tonic-gate 
8060Sstevel@tonic-gate 	if ((request_id == NULL) || (request_id[0] == NULL))
8070Sstevel@tonic-gate 		return (PAPI_NOT_FOUND);
8080Sstevel@tonic-gate 
8090Sstevel@tonic-gate 	if ((*job = j = calloc(1, sizeof (*j))) == NULL)
8100Sstevel@tonic-gate 		return (PAPI_TEMPORARY_ERROR);
8110Sstevel@tonic-gate 
8121676Sjpk 	job_status_to_attributes(j, request_id, user, slabel, size, date, state,
8130Sstevel@tonic-gate 				dest, form, charset, rank, file);
8140Sstevel@tonic-gate 
8150Sstevel@tonic-gate 	snprintf(req_id, sizeof (req_id), "%d-0", job_id);
8160Sstevel@tonic-gate 	lpsched_read_job_configuration(svc, j, req_id);
8170Sstevel@tonic-gate 
8180Sstevel@tonic-gate 	return (PAPI_OK);
8190Sstevel@tonic-gate }
8200Sstevel@tonic-gate 
8210Sstevel@tonic-gate papi_status_t
8222264Sjacobs papiJobMove(papi_service_t handle, char *printer, int32_t job_id,
8232264Sjacobs 		char *destination)
8242264Sjacobs {
8252264Sjacobs 	papi_status_t result = PAPI_OK;
8262264Sjacobs 	service_t *svc = handle;
8272264Sjacobs 	char req_id[64];
8282264Sjacobs 	char *queue;
8292264Sjacobs 	char *user = NULL;
8302264Sjacobs 
8312264Sjacobs 	if ((svc == NULL) || (printer == NULL) || (job_id < 0) ||
8322264Sjacobs 	    (destination == NULL))
8332264Sjacobs 		return (PAPI_BAD_ARGUMENT);
8342264Sjacobs 
8352264Sjacobs 	queue = printer_name_from_uri_id(printer, job_id);
8362264Sjacobs 	snprintf(req_id, sizeof (req_id), "%s-%d", queue, job_id);
8372264Sjacobs 	free(queue);
8382264Sjacobs 
8392264Sjacobs 	if (papiAttributeListGetString(svc->attributes, NULL, "user-name",
8402264Sjacobs 			&user) == PAPI_OK) {
8412264Sjacobs 		REQUEST *r = getrequest(req_id);
8422264Sjacobs 
8432264Sjacobs 		if ((r != NULL) && (r->user != NULL) &&
8442264Sjacobs 		    (strcmp(r->user, user) != 0))
8452264Sjacobs 			result = PAPI_NOT_AUTHORIZED;
8462264Sjacobs 		freerequest(r);
8472264Sjacobs 	}
8482264Sjacobs 
8492264Sjacobs 	if (result == PAPI_OK) {
8502264Sjacobs 		short status = MOK;
8512264Sjacobs 		char *dest = printer_name_from_uri_id(destination, -1);
8522264Sjacobs 
8532264Sjacobs 		if ((snd_msg(svc, S_MOVE_REQUEST, req_id, dest) < 0) ||
8542264Sjacobs 		    (rcv_msg(svc, R_MOVE_REQUEST, &status) < 0))
8552264Sjacobs 			status = MTRANSMITERR;
8562264Sjacobs 
8572264Sjacobs 		free(dest);
8582264Sjacobs 
8592264Sjacobs 		result = lpsched_status_to_papi_status(status);
8602264Sjacobs 	}
8612264Sjacobs 
8622264Sjacobs 	return (result);
8632264Sjacobs }
8642264Sjacobs 
8652264Sjacobs papi_status_t
8662264Sjacobs papiJobCancel(papi_service_t handle, char *printer, int32_t job_id)
8670Sstevel@tonic-gate {
8680Sstevel@tonic-gate 	papi_status_t result = PAPI_OK;
8690Sstevel@tonic-gate 	service_t *svc = handle;
8700Sstevel@tonic-gate 	char req_id[64];
8710Sstevel@tonic-gate 	char *dest;
8720Sstevel@tonic-gate 	char *user = NULL;
8730Sstevel@tonic-gate 
8740Sstevel@tonic-gate 	if ((svc == NULL) || (printer == NULL) || (job_id < 0))
8750Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
8760Sstevel@tonic-gate 
8770Sstevel@tonic-gate 	dest = printer_name_from_uri_id(printer, job_id);
8780Sstevel@tonic-gate 	snprintf(req_id, sizeof (req_id), "%s-%d", dest, job_id);
8790Sstevel@tonic-gate 	free(dest);
8800Sstevel@tonic-gate 
8810Sstevel@tonic-gate 	if (papiAttributeListGetString(svc->attributes, NULL, "user-name",
8820Sstevel@tonic-gate 			&user) == PAPI_OK) {
8830Sstevel@tonic-gate 		REQUEST *r = getrequest(req_id);
8840Sstevel@tonic-gate 
8852264Sjacobs 		if ((r != NULL) && (r->user != NULL) &&
8862264Sjacobs 		    (strcmp(r->user, user) != 0))
8870Sstevel@tonic-gate 			result = PAPI_NOT_AUTHORIZED;
8880Sstevel@tonic-gate 		freerequest(r);
8890Sstevel@tonic-gate 	}
8900Sstevel@tonic-gate 
8910Sstevel@tonic-gate 	if (result == PAPI_OK) {
8920Sstevel@tonic-gate 		short status = MOK;
8930Sstevel@tonic-gate 
8940Sstevel@tonic-gate 		if ((snd_msg(svc, S_CANCEL_REQUEST, req_id) < 0) ||
8950Sstevel@tonic-gate 		    (rcv_msg(svc, R_CANCEL_REQUEST, &status) < 0))
8960Sstevel@tonic-gate 			status = MTRANSMITERR;
8970Sstevel@tonic-gate 
8980Sstevel@tonic-gate 		result = lpsched_status_to_papi_status(status);
8990Sstevel@tonic-gate 	}
9000Sstevel@tonic-gate 
9010Sstevel@tonic-gate 	return (result);
9020Sstevel@tonic-gate }
9030Sstevel@tonic-gate 
9040Sstevel@tonic-gate papi_status_t
9052264Sjacobs hold_release_job(papi_service_t handle, char *printer,
9062264Sjacobs 		int32_t job_id, int flag)
9070Sstevel@tonic-gate {
9080Sstevel@tonic-gate 	papi_status_t status;
9090Sstevel@tonic-gate 	service_t *svc = handle;
9100Sstevel@tonic-gate 	REQUEST *r = NULL;
9110Sstevel@tonic-gate 	char *file;
9120Sstevel@tonic-gate 	char *dest;
9130Sstevel@tonic-gate 
9140Sstevel@tonic-gate 	if ((svc == NULL) || (printer == NULL) || (job_id < 0))
9150Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
9160Sstevel@tonic-gate 
9170Sstevel@tonic-gate 	if ((status = authorized(svc, job_id)) != PAPI_OK)
9180Sstevel@tonic-gate 		return (status);
9190Sstevel@tonic-gate 
9200Sstevel@tonic-gate 	dest = printer_name_from_uri_id(printer, job_id);
9210Sstevel@tonic-gate 	status = lpsched_start_change(svc, dest, job_id, &file);
9220Sstevel@tonic-gate 	if (status != PAPI_OK)
9230Sstevel@tonic-gate 		return (status);
9240Sstevel@tonic-gate 
9250Sstevel@tonic-gate 	if ((r = getrequest(file)) != NULL) {
9260Sstevel@tonic-gate 		r->actions &= ~ACT_RESUME;
9272264Sjacobs 		switch (flag) {
9282264Sjacobs 		case 0:
9290Sstevel@tonic-gate 			r->actions |= ACT_HOLD;
9302264Sjacobs 			break;
9312264Sjacobs 		case 1:
9320Sstevel@tonic-gate 			r->actions |= ACT_RESUME;
9332264Sjacobs 			break;
9342264Sjacobs 		case 2:
9352264Sjacobs 			r->actions |= ACT_IMMEDIATE;
9362264Sjacobs 			break;
9372264Sjacobs 		}
9380Sstevel@tonic-gate 		if (putrequest(file, r) < 0) {
9390Sstevel@tonic-gate 			detailed_error(svc,
9400Sstevel@tonic-gate 				gettext("failed to write job: %s: %s"),
9410Sstevel@tonic-gate 				file, strerror(errno));
9422264Sjacobs 			freerequest(r);
9430Sstevel@tonic-gate 			return (PAPI_DEVICE_ERROR);
9440Sstevel@tonic-gate 		}
9452264Sjacobs 		freerequest(r);
9460Sstevel@tonic-gate 	} else {
9470Sstevel@tonic-gate 		detailed_error(svc, gettext("failed to read job: %s: %s"),
9480Sstevel@tonic-gate 				file, strerror(errno));
9490Sstevel@tonic-gate 		return (PAPI_DEVICE_ERROR);
9500Sstevel@tonic-gate 	}
9510Sstevel@tonic-gate 
9520Sstevel@tonic-gate 	status = lpsched_end_change(svc, dest, job_id);
9530Sstevel@tonic-gate 
9540Sstevel@tonic-gate 	return (status);
9550Sstevel@tonic-gate }
9560Sstevel@tonic-gate 
9570Sstevel@tonic-gate papi_status_t
9582264Sjacobs papiJobHold(papi_service_t handle, char *printer, int32_t job_id)
9590Sstevel@tonic-gate {
9600Sstevel@tonic-gate 	return (hold_release_job(handle, printer, job_id, 0));
9610Sstevel@tonic-gate }
9620Sstevel@tonic-gate 
9630Sstevel@tonic-gate papi_status_t
9642264Sjacobs papiJobRelease(papi_service_t handle, char *printer, int32_t job_id)
9650Sstevel@tonic-gate {
9660Sstevel@tonic-gate 	return (hold_release_job(handle, printer, job_id, 1));
9670Sstevel@tonic-gate }
9680Sstevel@tonic-gate 
9690Sstevel@tonic-gate papi_status_t
9702264Sjacobs papiJobPromote(papi_service_t handle, char *printer, int32_t job_id)
9710Sstevel@tonic-gate {
9722264Sjacobs 	return (hold_release_job(handle, printer, job_id, 2));
9730Sstevel@tonic-gate }
9740Sstevel@tonic-gate 
9750Sstevel@tonic-gate papi_status_t
9762264Sjacobs papiJobModify(papi_service_t handle, char *printer, int32_t job_id,
9772264Sjacobs 		papi_attribute_t **attributes, papi_job_t *job)
9780Sstevel@tonic-gate {
9790Sstevel@tonic-gate 	papi_status_t status;
9800Sstevel@tonic-gate 	job_t *j = NULL;
9810Sstevel@tonic-gate 	service_t *svc = handle;
9820Sstevel@tonic-gate 	char *file = NULL;
9830Sstevel@tonic-gate 	char *dest;
9840Sstevel@tonic-gate 	REQUEST *r = NULL;
9850Sstevel@tonic-gate 	char lpfile[BUFSIZ];
9860Sstevel@tonic-gate 
9870Sstevel@tonic-gate 	if ((svc == NULL) || (printer == NULL) || (job_id < 0) ||
9880Sstevel@tonic-gate 	    (attributes == NULL))
9890Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
9900Sstevel@tonic-gate 
9910Sstevel@tonic-gate 	if ((status = authorized(svc, job_id)) != PAPI_OK)
9920Sstevel@tonic-gate 		return (status);
9930Sstevel@tonic-gate 
9940Sstevel@tonic-gate 	if ((*job = j = calloc(1, sizeof (*j))) == NULL)
9950Sstevel@tonic-gate 		return (PAPI_TEMPORARY_ERROR);
9960Sstevel@tonic-gate 
9970Sstevel@tonic-gate 	dest = printer_name_from_uri_id(printer, job_id);
9980Sstevel@tonic-gate 	status = lpsched_start_change(svc, dest, job_id, &file);
9990Sstevel@tonic-gate 	if (status != PAPI_OK)
10000Sstevel@tonic-gate 		return (status);
10010Sstevel@tonic-gate 
10020Sstevel@tonic-gate 	if ((r = getrequest(file)) != NULL) {
10030Sstevel@tonic-gate 		job_attributes_to_lpsched_request(handle, r,
10040Sstevel@tonic-gate 				(papi_attribute_t **)attributes);
10050Sstevel@tonic-gate #ifdef LP_USE_PAPI_ATTR
10060Sstevel@tonic-gate 		/*
10070Sstevel@tonic-gate 		 * store the job attributes in the PAPI job attribute file
10080Sstevel@tonic-gate 		 * that was created by the origonal job request. We need to
10090Sstevel@tonic-gate 		 * modify the attributes in the file as per the new attributes
10100Sstevel@tonic-gate 		 */
10110Sstevel@tonic-gate 		snprintf(lpfile, sizeof (lpfile), "%s%d-%s",
10120Sstevel@tonic-gate 			"/var/spool/lp/temp/", job_id, LP_PAPIATTRNAME);
10130Sstevel@tonic-gate 		status = psm_modifyAttrsFile(attributes, lpfile);
10140Sstevel@tonic-gate 		if (status != PAPI_OK) {
10150Sstevel@tonic-gate 			detailed_error(svc,
10160Sstevel@tonic-gate 				"unable to modify the attributes file: %s: %s",
10170Sstevel@tonic-gate 				lpfile, strerror(errno));
10180Sstevel@tonic-gate 			return (PAPI_DEVICE_ERROR);
10190Sstevel@tonic-gate 		}
10200Sstevel@tonic-gate #endif
10210Sstevel@tonic-gate 
10220Sstevel@tonic-gate 		if (putrequest(file, r) < 0) {
10230Sstevel@tonic-gate 			detailed_error(svc,
10240Sstevel@tonic-gate 				gettext("failed to write job: %s: %s"),
10250Sstevel@tonic-gate 				file, strerror(errno));
10260Sstevel@tonic-gate 			freerequest(r);
10270Sstevel@tonic-gate 			return (PAPI_DEVICE_ERROR);
10280Sstevel@tonic-gate 		}
10290Sstevel@tonic-gate 	} else {
10300Sstevel@tonic-gate 		detailed_error(svc, gettext("failed to read job: %s: %s"),
10310Sstevel@tonic-gate 				file, strerror(errno));
10320Sstevel@tonic-gate 		return (PAPI_DEVICE_ERROR);
10330Sstevel@tonic-gate 	}
10340Sstevel@tonic-gate 
10350Sstevel@tonic-gate 	status = lpsched_end_change(svc, dest, job_id);
10360Sstevel@tonic-gate 	lpsched_request_to_job_attributes(r, j);
10376725Sjacobs 
10386725Sjacobs 	papiAttributeListAddInteger(&j->attributes, PAPI_ATTR_REPLACE,
10396725Sjacobs 			"job-id", job_id);
10406725Sjacobs 
10410Sstevel@tonic-gate 	freerequest(r);
10420Sstevel@tonic-gate 
10430Sstevel@tonic-gate 	return (status);
10440Sstevel@tonic-gate }
10450Sstevel@tonic-gate 
10460Sstevel@tonic-gate /*
10470Sstevel@tonic-gate  * Extension to PAPI, a variation of this is slated for post-1.0
10480Sstevel@tonic-gate  */
10490Sstevel@tonic-gate #define	DUMMY_FILE	"/var/spool/lp/fifos/FIFO"
10500Sstevel@tonic-gate 
10510Sstevel@tonic-gate papi_status_t
10522264Sjacobs papiJobCreate(papi_service_t handle, char *printer,
10532264Sjacobs 		papi_attribute_t **job_attributes,
10542264Sjacobs 		papi_job_ticket_t *job_ticket, papi_job_t *job)
10550Sstevel@tonic-gate {
10560Sstevel@tonic-gate 	papi_status_t status;
10570Sstevel@tonic-gate 	service_t *svc = handle;
10580Sstevel@tonic-gate 	job_t *j = NULL;
10590Sstevel@tonic-gate 	REQUEST *request;
10600Sstevel@tonic-gate 	char *request_id = NULL;
10610Sstevel@tonic-gate 	char *c;
10620Sstevel@tonic-gate 	char *tmp = NULL;
10630Sstevel@tonic-gate 	char metadata_file[MAXPATHLEN];
10640Sstevel@tonic-gate 
10650Sstevel@tonic-gate 	if ((svc == NULL) || (printer == NULL) || (job == NULL))
10660Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
10670Sstevel@tonic-gate 
10680Sstevel@tonic-gate 	if (job_ticket != NULL)
10690Sstevel@tonic-gate 		return (PAPI_JOB_TICKET_NOT_SUPPORTED);
10700Sstevel@tonic-gate 
10710Sstevel@tonic-gate 	if ((*job = j = calloc(1, sizeof (*j))) == NULL)
10720Sstevel@tonic-gate 		return (PAPI_TEMPORARY_ERROR);
10730Sstevel@tonic-gate 
10740Sstevel@tonic-gate 	/* 1 for the control file (-0) */
10750Sstevel@tonic-gate 	status = lpsched_alloc_files(svc, 1, &request_id);
10760Sstevel@tonic-gate 	if (status != PAPI_OK)
10770Sstevel@tonic-gate 		return (status);
10780Sstevel@tonic-gate 
10790Sstevel@tonic-gate 	/* convert the attributes to an lpsched REQUEST structure */
10800Sstevel@tonic-gate 	request = create_request(svc, (char *)printer,
10810Sstevel@tonic-gate 				(papi_attribute_t **)job_attributes);
10822264Sjacobs 	if (request == NULL)
10832264Sjacobs 		return (PAPI_TEMPORARY_ERROR);
10840Sstevel@tonic-gate 	addlist(&request->file_list, DUMMY_FILE);	/* add a dummy file */
10850Sstevel@tonic-gate 	request->actions |= ACT_HOLD;			/* hold the job */
10860Sstevel@tonic-gate 
10870Sstevel@tonic-gate #ifdef LP_USE_PAPI_ATTR
10880Sstevel@tonic-gate 	/*
10890Sstevel@tonic-gate 	 * store the job attributes in the PAPI job attribute file that was
10900Sstevel@tonic-gate 	 * created by lpsched_alloc_files(), the attributes will then pass
10910Sstevel@tonic-gate 	 * through lpsched and be given to the slow-filters and the printer's
10920Sstevel@tonic-gate 	 * interface script to process them
10930Sstevel@tonic-gate 	 */
10940Sstevel@tonic-gate 	snprintf(metadata_file, sizeof (metadata_file), "%s%s-%s",
10950Sstevel@tonic-gate 		"/var/spool/lp/temp/", request_id, LP_PAPIATTRNAME);
10960Sstevel@tonic-gate 	status = psm_copy_attrsToFile(job_attributes, metadata_file);
10970Sstevel@tonic-gate 	if (status != PAPI_OK) {
10980Sstevel@tonic-gate 		detailed_error(svc, "unable to copy attributes to file: %s: %s",
10990Sstevel@tonic-gate 				metadata_file, strerror(errno));
11002264Sjacobs 		free(request_id);
11010Sstevel@tonic-gate 		return (PAPI_DEVICE_ERROR);
11020Sstevel@tonic-gate 	}
11030Sstevel@tonic-gate #endif
11040Sstevel@tonic-gate 
11050Sstevel@tonic-gate 	/* store the REQUEST on disk */
11060Sstevel@tonic-gate 	snprintf(metadata_file, sizeof (metadata_file), "%s-0", request_id);
11072264Sjacobs 	free(request_id);
11080Sstevel@tonic-gate 	if (putrequest(metadata_file, request) < 0) {
11090Sstevel@tonic-gate 		detailed_error(svc, gettext("unable to save request: %s: %s"),
11100Sstevel@tonic-gate 			metadata_file, strerror(errno));
11110Sstevel@tonic-gate 		return (PAPI_DEVICE_ERROR);
11120Sstevel@tonic-gate 	}
11130Sstevel@tonic-gate 
11140Sstevel@tonic-gate 	status = lpsched_commit_job(svc, metadata_file, &tmp);
11150Sstevel@tonic-gate 	if (status != PAPI_OK) {
11160Sstevel@tonic-gate 		unlink(metadata_file);
11170Sstevel@tonic-gate 		return (status);
11180Sstevel@tonic-gate 	}
11190Sstevel@tonic-gate 
11200Sstevel@tonic-gate 	lpsched_request_to_job_attributes(request, j);
11210Sstevel@tonic-gate 
11220Sstevel@tonic-gate 	if ((c = strrchr(tmp, '-')) != NULL)
11230Sstevel@tonic-gate 		c++;
11240Sstevel@tonic-gate 	papiAttributeListAddInteger(&j->attributes, PAPI_ATTR_REPLACE,
11250Sstevel@tonic-gate 			"job-id", atoi(c));
11260Sstevel@tonic-gate 	papiAttributeListAddString(&j->attributes, PAPI_ATTR_REPLACE,
11270Sstevel@tonic-gate 			"job-uri", tmp);
11280Sstevel@tonic-gate 
11290Sstevel@tonic-gate 	return (PAPI_OK);
11300Sstevel@tonic-gate }
11310Sstevel@tonic-gate 
11320Sstevel@tonic-gate papi_status_t
11330Sstevel@tonic-gate papiJobCommit(papi_service_t handle, char *printer, int32_t id)
11340Sstevel@tonic-gate {
11350Sstevel@tonic-gate 	papi_status_t status = PAPI_OK;
11360Sstevel@tonic-gate 	service_t *svc = handle;
11370Sstevel@tonic-gate 	REQUEST *r = NULL;
11380Sstevel@tonic-gate 	char *metadata_file;
11390Sstevel@tonic-gate 	char *dest;
11400Sstevel@tonic-gate 
11410Sstevel@tonic-gate 	if ((svc == NULL) || (printer == NULL))
11420Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
11430Sstevel@tonic-gate 
11440Sstevel@tonic-gate 	dest = printer_name_from_uri_id(printer, id);
11450Sstevel@tonic-gate 	/* tell the scheduler that we want to change the job */
11460Sstevel@tonic-gate 	status = lpsched_start_change(svc, dest, id, &metadata_file);
11470Sstevel@tonic-gate 	if (status != PAPI_OK)
11480Sstevel@tonic-gate 		return (status);
11490Sstevel@tonic-gate 
11500Sstevel@tonic-gate 	if ((r = getrequest(metadata_file)) != NULL) {
11510Sstevel@tonic-gate 		r->actions &= ~ACT_RESUME;
11520Sstevel@tonic-gate 		r->actions |= ACT_RESUME;
11530Sstevel@tonic-gate 		dellist(&r->file_list, DUMMY_FILE);
11540Sstevel@tonic-gate 
11550Sstevel@tonic-gate 		if (putrequest(metadata_file, r) < 0) {
11560Sstevel@tonic-gate 			detailed_error(svc,
11570Sstevel@tonic-gate 				gettext("failed to write job: %s: %s"),
11580Sstevel@tonic-gate 				metadata_file, strerror(errno));
11592264Sjacobs 			freerequest(r);
11600Sstevel@tonic-gate 			return (PAPI_DEVICE_ERROR);
11610Sstevel@tonic-gate 		}
11620Sstevel@tonic-gate 	} else {
11630Sstevel@tonic-gate 		detailed_error(svc, gettext("failed to read job: %s: %s"),
11640Sstevel@tonic-gate 				metadata_file, strerror(errno));
11650Sstevel@tonic-gate 		return (PAPI_DEVICE_ERROR);
11660Sstevel@tonic-gate 	}
11670Sstevel@tonic-gate 
11680Sstevel@tonic-gate 	status = lpsched_end_change(svc, dest, id);
11690Sstevel@tonic-gate 	freerequest(r);
11700Sstevel@tonic-gate 
11710Sstevel@tonic-gate 	return (status);
11720Sstevel@tonic-gate }
11730Sstevel@tonic-gate 
11740Sstevel@tonic-gate papi_status_t
11750Sstevel@tonic-gate papiJobStreamAdd(papi_service_t handle, char *printer, int32_t id,
11760Sstevel@tonic-gate 		papi_stream_t *stream)
11770Sstevel@tonic-gate {
11780Sstevel@tonic-gate 	papi_status_t status;
11790Sstevel@tonic-gate 	service_t *svc = handle;
11800Sstevel@tonic-gate 	job_stream_t *s = NULL;
11810Sstevel@tonic-gate 	char *metadata_file = NULL;
11820Sstevel@tonic-gate 	char *dest;
11830Sstevel@tonic-gate 	char path[MAXPATHLEN];
11840Sstevel@tonic-gate 
11850Sstevel@tonic-gate 	/* allocate space for the stream */
11860Sstevel@tonic-gate 	if ((*stream = s = calloc(1, sizeof (*s))) == NULL)
11870Sstevel@tonic-gate 		return (PAPI_TEMPORARY_ERROR);
11880Sstevel@tonic-gate 
11890Sstevel@tonic-gate 	dest = printer_name_from_uri_id(printer, id);
11900Sstevel@tonic-gate 	/* create/open data file (only root or lp can really do this */
11910Sstevel@tonic-gate 	snprintf(path, sizeof (path), "/var/spool/lp/temp/%d-XXXXXX", id);
11920Sstevel@tonic-gate 	if ((s->fd = mkstemp(path)) < 0) {
11930Sstevel@tonic-gate 		detailed_error(svc, gettext("unable to create sink (%s): %s"),
11940Sstevel@tonic-gate 			path, strerror(errno));
11950Sstevel@tonic-gate 		free(s);
11960Sstevel@tonic-gate 		return (PAPI_NOT_AUTHORIZED);
11970Sstevel@tonic-gate 	}
11980Sstevel@tonic-gate 
11990Sstevel@tonic-gate 	/* add data file to job */
12000Sstevel@tonic-gate 	status = lpsched_start_change(svc, dest, id, &metadata_file);
12010Sstevel@tonic-gate 	if (status != PAPI_OK) {
12020Sstevel@tonic-gate 		close(s->fd);
12030Sstevel@tonic-gate 		free(s);
12040Sstevel@tonic-gate 		unlink(path);
12050Sstevel@tonic-gate 		return (status);
12060Sstevel@tonic-gate 	}
12070Sstevel@tonic-gate 
12080Sstevel@tonic-gate 	if ((s->request = getrequest(metadata_file)) == NULL) {
12090Sstevel@tonic-gate 		detailed_error(svc, gettext("unable to load request: %s: %s"),
12100Sstevel@tonic-gate 			metadata_file, strerror(errno));
12110Sstevel@tonic-gate 		close(s->fd);
12120Sstevel@tonic-gate 		free(s);
12130Sstevel@tonic-gate 		unlink(path);
12140Sstevel@tonic-gate 		return (PAPI_NOT_POSSIBLE);
12150Sstevel@tonic-gate 	}
12160Sstevel@tonic-gate 
12170Sstevel@tonic-gate 	addlist(&(s->request->file_list), path);
12180Sstevel@tonic-gate 
12190Sstevel@tonic-gate 	if (putrequest(metadata_file, s->request) < 0) {
12200Sstevel@tonic-gate 		detailed_error(svc, gettext("unable to save request: %s: %s"),
12210Sstevel@tonic-gate 			metadata_file, strerror(errno));
12220Sstevel@tonic-gate 		close(s->fd);
12230Sstevel@tonic-gate 		free(s);
12240Sstevel@tonic-gate 		unlink(path);
12250Sstevel@tonic-gate 		return (PAPI_NOT_POSSIBLE);
12260Sstevel@tonic-gate 	}
12270Sstevel@tonic-gate 
12280Sstevel@tonic-gate 	status = lpsched_end_change(svc, dest, id);
12290Sstevel@tonic-gate 
12300Sstevel@tonic-gate 	if (status != PAPI_OK)
12310Sstevel@tonic-gate 		return (status);
12320Sstevel@tonic-gate 
12330Sstevel@tonic-gate 	return (PAPI_OK);
12340Sstevel@tonic-gate }
1235