xref: /onnv-gate/usr/src/cmd/lp/lib/papi/job.c (revision 8966)
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 /*
228569SJonathan.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  * for an older application that may have been linked with a pre-v1.0
382264Sjacobs  * PAPI implementation.
392264Sjacobs  */
402264Sjacobs papi_status_t
412264Sjacobs papiAttributeListAdd(papi_attribute_t ***attrs, int flags, char *name,
422264Sjacobs 		papi_attribute_value_type_t type, papi_attribute_value_t *value)
432264Sjacobs {
442264Sjacobs 	return (papiAttributeListAddValue(attrs, flags, name, type, value));
452264Sjacobs }
462264Sjacobs 
470Sstevel@tonic-gate #ifdef LP_USE_PAPI_ATTR
482264Sjacobs static papi_status_t psm_modifyAttrsFile(papi_attribute_t **attrs, char *file);
492264Sjacobs static papi_status_t psm_modifyAttrsList(char *file, papi_attribute_t **attrs,
502264Sjacobs 					papi_attribute_t ***newAttrs);
510Sstevel@tonic-gate #endif
520Sstevel@tonic-gate 
530Sstevel@tonic-gate 
540Sstevel@tonic-gate void
550Sstevel@tonic-gate papiJobFree(papi_job_t job)
560Sstevel@tonic-gate {
570Sstevel@tonic-gate 	job_t *tmp = (job_t *)job;
580Sstevel@tonic-gate 
590Sstevel@tonic-gate 	if (tmp != NULL) {
600Sstevel@tonic-gate 		papiAttributeListFree(tmp->attributes);
610Sstevel@tonic-gate 		free(tmp);
620Sstevel@tonic-gate 	}
630Sstevel@tonic-gate }
640Sstevel@tonic-gate 
650Sstevel@tonic-gate void
660Sstevel@tonic-gate papiJobListFree(papi_job_t *jobs)
670Sstevel@tonic-gate {
680Sstevel@tonic-gate 	if (jobs != NULL) {
690Sstevel@tonic-gate 		int i;
700Sstevel@tonic-gate 
710Sstevel@tonic-gate 		for (i = 0; jobs[i] != NULL; i++) {
720Sstevel@tonic-gate 			papiJobFree(jobs[i]);
730Sstevel@tonic-gate 		}
740Sstevel@tonic-gate 		free(jobs);
750Sstevel@tonic-gate 	}
760Sstevel@tonic-gate }
770Sstevel@tonic-gate 
780Sstevel@tonic-gate papi_attribute_t **
790Sstevel@tonic-gate papiJobGetAttributeList(papi_job_t job)
800Sstevel@tonic-gate {
810Sstevel@tonic-gate 	job_t *tmp = (job_t *)job;
820Sstevel@tonic-gate 
830Sstevel@tonic-gate 	if (tmp != NULL)
840Sstevel@tonic-gate 		return (tmp->attributes);
850Sstevel@tonic-gate 
860Sstevel@tonic-gate 	return (NULL);
870Sstevel@tonic-gate }
880Sstevel@tonic-gate 
890Sstevel@tonic-gate char *
900Sstevel@tonic-gate papiJobGetPrinterName(papi_job_t job)
910Sstevel@tonic-gate {
920Sstevel@tonic-gate 	job_t *tmp = (job_t *)job;
930Sstevel@tonic-gate 	char *result = NULL;
940Sstevel@tonic-gate 
950Sstevel@tonic-gate 	if (tmp != NULL)
960Sstevel@tonic-gate 		papiAttributeListGetString(tmp->attributes, NULL,
978569SJonathan.Ca@Sun.COM 		    "printer-name", &result);
980Sstevel@tonic-gate 
990Sstevel@tonic-gate 	return (result);
1000Sstevel@tonic-gate }
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate int32_t
1030Sstevel@tonic-gate papiJobGetId(papi_job_t job)
1040Sstevel@tonic-gate {
1050Sstevel@tonic-gate 	job_t *tmp = (job_t *)job;
1060Sstevel@tonic-gate 	int result = -1;
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate 	if (tmp != NULL)
1090Sstevel@tonic-gate 		papiAttributeListGetInteger(tmp->attributes, NULL, "job-id",
1108569SJonathan.Ca@Sun.COM 		    &result);
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate 	return (result);
1130Sstevel@tonic-gate }
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate static REQUEST *
1160Sstevel@tonic-gate create_request(papi_service_t svc, char *printer, papi_attribute_t **attributes)
1170Sstevel@tonic-gate {
1187253Sjacobs 	REQUEST *r;
1190Sstevel@tonic-gate 
1207253Sjacobs 	if ((r = calloc(1, sizeof (*r))) != NULL) {
1217253Sjacobs 		r->priority = -1;
1227253Sjacobs 		r->destination = printer_name_from_uri_id(printer, -1);
1237253Sjacobs 		job_attributes_to_lpsched_request(svc, r, attributes);
1247253Sjacobs 	}
1250Sstevel@tonic-gate 
1267253Sjacobs 	return (r);
1270Sstevel@tonic-gate }
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate static papi_status_t
1300Sstevel@tonic-gate authorized(service_t *svc, int32_t id)
1310Sstevel@tonic-gate {
1320Sstevel@tonic-gate 	papi_status_t result = PAPI_NOT_AUTHORIZED;	/* assume the worst */
1330Sstevel@tonic-gate 	char file[32];
1340Sstevel@tonic-gate 	REQUEST *r;
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate 	snprintf(file, sizeof (file), "%d-0", id);
1370Sstevel@tonic-gate 	if ((r = getrequest(file)) != NULL) {
1380Sstevel@tonic-gate 		uid_t uid = getuid();
1390Sstevel@tonic-gate 		struct passwd *pw = NULL;
1400Sstevel@tonic-gate 		char *user = "intruder";	/* assume an intruder */
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate 		if ((pw = getpwuid(uid)) != NULL)
1430Sstevel@tonic-gate 			user = pw->pw_name;	/* use the process owner */
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate 		if ((uid == 0) || (uid == 71)) { /* root/lp can forge this */
1460Sstevel@tonic-gate 			papi_status_t s;
1470Sstevel@tonic-gate 			s = papiAttributeListGetString(svc->attributes, NULL,
1488569SJonathan.Ca@Sun.COM 			    "user-name", &user);
1490Sstevel@tonic-gate 			if (s != PAPI_OK)	/* true root/lp are almighty */
1500Sstevel@tonic-gate 				result = PAPI_OK;
1510Sstevel@tonic-gate 		}
1520Sstevel@tonic-gate 
153*8966SSonam.Gupta@Sun.COM 		if (result != PAPI_OK) {
154*8966SSonam.Gupta@Sun.COM 			if (strcmp(user, r->user) == 0)
155*8966SSonam.Gupta@Sun.COM 				result = PAPI_OK;
156*8966SSonam.Gupta@Sun.COM 			else {
157*8966SSonam.Gupta@Sun.COM 				/*
158*8966SSonam.Gupta@Sun.COM 				 * user request r->user might contain the
159*8966SSonam.Gupta@Sun.COM 				 * host info also
160*8966SSonam.Gupta@Sun.COM 				 */
161*8966SSonam.Gupta@Sun.COM 				char *token;
162*8966SSonam.Gupta@Sun.COM 				token = strtok(r->user, "@");
163*8966SSonam.Gupta@Sun.COM 
164*8966SSonam.Gupta@Sun.COM 				if (token != NULL) {
165*8966SSonam.Gupta@Sun.COM 					if (strcmp(user, token) == 0)
166*8966SSonam.Gupta@Sun.COM 						result = PAPI_OK;
167*8966SSonam.Gupta@Sun.COM 					free(token);
168*8966SSonam.Gupta@Sun.COM 				}
169*8966SSonam.Gupta@Sun.COM 			}
170*8966SSonam.Gupta@Sun.COM 		}
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate 		freerequest(r);
1730Sstevel@tonic-gate 	} else
1740Sstevel@tonic-gate 		result = PAPI_NOT_FOUND;
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate 	return (result);
1770Sstevel@tonic-gate }
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate static papi_status_t
1802264Sjacobs copy_file(char *from, char *to)
1810Sstevel@tonic-gate {
1820Sstevel@tonic-gate 	int ifd, ofd;
1830Sstevel@tonic-gate 	char buf[BUFSIZ];
1840Sstevel@tonic-gate 	int rc;
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate 	if ((ifd = open(from, O_RDONLY)) < 0)
1870Sstevel@tonic-gate 		return (PAPI_DOCUMENT_ACCESS_ERROR);
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate 	if ((ofd = open(to, O_WRONLY)) < 0) {
1900Sstevel@tonic-gate 		close(ifd);
1910Sstevel@tonic-gate 		return (PAPI_NOT_POSSIBLE);
1920Sstevel@tonic-gate 	}
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate 	while ((rc = read(ifd, buf, sizeof (buf))) > 0)
1950Sstevel@tonic-gate 		write(ofd, buf, rc);
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate 	close(ifd);
1980Sstevel@tonic-gate 	close(ofd);
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate 	return (PAPI_OK);
2010Sstevel@tonic-gate }
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate #ifdef LP_USE_PAPI_ATTR
2050Sstevel@tonic-gate /*
2060Sstevel@tonic-gate  * *****************************************************************************
2070Sstevel@tonic-gate  *
2080Sstevel@tonic-gate  * Description: Create a file containing all the attributes in the attribute
2090Sstevel@tonic-gate  *              list passed to this function.
2100Sstevel@tonic-gate  *              This file is then passed through lpsched and given to either
2110Sstevel@tonic-gate  *              a slow-filter or to the printer's interface script to process
2120Sstevel@tonic-gate  *              the attributes.
2130Sstevel@tonic-gate  *
2140Sstevel@tonic-gate  * Parameters:  attrs - list of attributes and their values
2150Sstevel@tonic-gate  *              file  - file pathname to create and put the attributes into.
2160Sstevel@tonic-gate  *
2170Sstevel@tonic-gate  * *****************************************************************************
2180Sstevel@tonic-gate  */
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate static papi_status_t
2212264Sjacobs psm_copy_attrsToFile(papi_attribute_t **attrs, char *file)
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate {
2240Sstevel@tonic-gate 	papi_status_t result = PAPI_OK;
2252264Sjacobs 
2262264Sjacobs 	if ((attrs != NULL) && (*attrs != NULL)) {
2272264Sjacobs 		FILE *out = NULL;
2280Sstevel@tonic-gate 
2292264Sjacobs 		if ((out = fopen(file, "w")) != NULL) {
2302264Sjacobs 			papiAttributeListPrint(out, attrs, "");
2310Sstevel@tonic-gate 			fclose(out);
2322264Sjacobs 		} else {
2330Sstevel@tonic-gate 			result = PAPI_NOT_POSSIBLE;
2340Sstevel@tonic-gate 		}
2350Sstevel@tonic-gate 	}
2360Sstevel@tonic-gate 
2370Sstevel@tonic-gate 	return (result);
2380Sstevel@tonic-gate } /* psm_copy_attrsToFile */
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate /*
2420Sstevel@tonic-gate  * *****************************************************************************
2430Sstevel@tonic-gate  *
2440Sstevel@tonic-gate  * Description: Modify the given attribute 'file' with the attributes from the
2450Sstevel@tonic-gate  *              'attrs' list. Attributes already in the file will be replaced
2460Sstevel@tonic-gate  *              with the new value. New attributes will be added into the file.
2470Sstevel@tonic-gate  *
2480Sstevel@tonic-gate  * Parameters:  attrs - list of attributes and their values
2490Sstevel@tonic-gate  *              file  - file pathname to create and put the attributes into.
2500Sstevel@tonic-gate  *
2510Sstevel@tonic-gate  * *****************************************************************************
2520Sstevel@tonic-gate  */
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate static papi_status_t
2552264Sjacobs psm_modifyAttrsFile(papi_attribute_t **attrs, char *file)
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate {
2580Sstevel@tonic-gate 	papi_status_t result = PAPI_OK;
2590Sstevel@tonic-gate 	papi_attribute_t **newAttrs = NULL;
2600Sstevel@tonic-gate 	struct stat   tmpBuf;
2610Sstevel@tonic-gate 	FILE *fd = NULL;
2620Sstevel@tonic-gate 
2632264Sjacobs 	if ((attrs != NULL) && (*attrs != NULL) && (file != NULL)) {
2640Sstevel@tonic-gate 
2650Sstevel@tonic-gate 		/*
2660Sstevel@tonic-gate 		 * check file exist before try to modify it, if it doesn't
2670Sstevel@tonic-gate 		 * exist assume there is an error
2680Sstevel@tonic-gate 		 */
2692264Sjacobs 		if (stat(file, &tmpBuf) == 0) {
2700Sstevel@tonic-gate 			/*
2710Sstevel@tonic-gate 			 * if file is currently empty just write the given
2720Sstevel@tonic-gate 			 * attributes to the file otherwise exact the attributes
2730Sstevel@tonic-gate 			 * from the file and modify them accordingly before
2740Sstevel@tonic-gate 			 * writing them back to the file
2750Sstevel@tonic-gate 			 */
2762264Sjacobs 			if (tmpBuf.st_size == 0) {
2770Sstevel@tonic-gate 				newAttrs = (papi_attribute_t **)attrs;
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate 				fd = fopen(file, "w");
2802264Sjacobs 				if (fd != NULL) {
2810Sstevel@tonic-gate 					papiAttributeListPrint(fd,
2822264Sjacobs 							newAttrs, "");
2830Sstevel@tonic-gate 					fclose(fd);
2842264Sjacobs 				} else {
2850Sstevel@tonic-gate 					result = PAPI_NOT_POSSIBLE;
2860Sstevel@tonic-gate 				}
2872264Sjacobs 			} else {
2880Sstevel@tonic-gate 				result =
2890Sstevel@tonic-gate 				    psm_modifyAttrsList(file, attrs, &newAttrs);
2900Sstevel@tonic-gate 
2910Sstevel@tonic-gate 				fd = fopen(file, "w");
2922264Sjacobs 				if (fd != NULL) {
2930Sstevel@tonic-gate 					papiAttributeListPrint(fd,
2942264Sjacobs 								newAttrs, "");
2950Sstevel@tonic-gate 					fclose(fd);
2962264Sjacobs 				} else {
2970Sstevel@tonic-gate 					result = PAPI_NOT_POSSIBLE;
2980Sstevel@tonic-gate 				}
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate 				papiAttributeListFree(newAttrs);
3010Sstevel@tonic-gate 			}
3022264Sjacobs 		} else {
3030Sstevel@tonic-gate 			result = PAPI_NOT_POSSIBLE;
3040Sstevel@tonic-gate 		}
3050Sstevel@tonic-gate 	}
3060Sstevel@tonic-gate 
3070Sstevel@tonic-gate 	return (result);
3080Sstevel@tonic-gate } /* psm_modifyAttrsFile */
3090Sstevel@tonic-gate 
3100Sstevel@tonic-gate 
3110Sstevel@tonic-gate /*
3120Sstevel@tonic-gate  * *****************************************************************************
3130Sstevel@tonic-gate  *
3140Sstevel@tonic-gate  * Description: Extracts the attributes in the given attribute 'file' and
3150Sstevel@tonic-gate  *              creates a new list 'newAttrs' containing the modified list of
3160Sstevel@tonic-gate  *              attributes.
3170Sstevel@tonic-gate  *
3180Sstevel@tonic-gate  * Parameters:  file  - pathname of file containing attributes to be modified
3190Sstevel@tonic-gate  *              attrs - list of attributes and their values to modify
3200Sstevel@tonic-gate  *              newAttrs - returns the modified list of attributes
3210Sstevel@tonic-gate  *
3220Sstevel@tonic-gate  * *****************************************************************************
3230Sstevel@tonic-gate  */
3240Sstevel@tonic-gate 
3250Sstevel@tonic-gate static papi_status_t
3262264Sjacobs psm_modifyAttrsList(char *file, papi_attribute_t **attrs,
3270Sstevel@tonic-gate     papi_attribute_t ***newAttrs)
3280Sstevel@tonic-gate 
3290Sstevel@tonic-gate {
3300Sstevel@tonic-gate 	papi_status_t result = PAPI_OK;
3310Sstevel@tonic-gate 	papi_attribute_t  *nextAttr = NULL;
3320Sstevel@tonic-gate 	papi_attribute_value_t  **values = NULL;
3330Sstevel@tonic-gate 	void *iter = NULL;
3340Sstevel@tonic-gate 	FILE *fd = NULL;
3350Sstevel@tonic-gate 	register int fD = 0;
3360Sstevel@tonic-gate 	char aBuff[200];
3370Sstevel@tonic-gate 	char *a = NULL;
3380Sstevel@tonic-gate 	char *p = NULL;
3390Sstevel@tonic-gate 	int count = 0;
3400Sstevel@tonic-gate 	int n = 0;
3410Sstevel@tonic-gate 
3420Sstevel@tonic-gate 	fd = fopen(file, "r");
3432264Sjacobs 	if (fd != NULL) {
3440Sstevel@tonic-gate 		fD = fileno(fd);
3450Sstevel@tonic-gate 		a = &aBuff[0];
3460Sstevel@tonic-gate 		p = &aBuff[0];
3470Sstevel@tonic-gate 		count = read(fD, &aBuff[0], sizeof (aBuff) - 1);
3482264Sjacobs 		while ((result == PAPI_OK) && (count > 0)) {
3490Sstevel@tonic-gate 			aBuff[count+n] = '\0';
3502264Sjacobs 			if (count == sizeof (aBuff) - n - 1) {
3510Sstevel@tonic-gate 				p = strrchr(aBuff, '\n');
3522264Sjacobs 				if (p != NULL) {
3530Sstevel@tonic-gate 					/* terminate at last complete line */
3540Sstevel@tonic-gate 					*p = '\0';
3550Sstevel@tonic-gate 				}
3560Sstevel@tonic-gate 			}
3570Sstevel@tonic-gate 			result = papiAttributeListFromString(
3580Sstevel@tonic-gate 				newAttrs, PAPI_ATTR_EXCL, aBuff);
3590Sstevel@tonic-gate 
3602264Sjacobs 			if (result == PAPI_OK) {
3610Sstevel@tonic-gate 				/*
3620Sstevel@tonic-gate 				 * handle any part lines and then read the next
3630Sstevel@tonic-gate 				 * buffer from the file
3640Sstevel@tonic-gate 				 */
3650Sstevel@tonic-gate 				n = 0;
3662264Sjacobs 				if (p != a) {
3670Sstevel@tonic-gate 					p++; /* skip NL */
3680Sstevel@tonic-gate 					n = sizeof (aBuff) - 1 - (p - a);
3690Sstevel@tonic-gate 					strncpy(aBuff, p, n);
3700Sstevel@tonic-gate 				}
3710Sstevel@tonic-gate 				count = read(fD, &aBuff[n],
3720Sstevel@tonic-gate 					sizeof (aBuff) - n - 1);
3730Sstevel@tonic-gate 				p = &aBuff[0];
3740Sstevel@tonic-gate 			}
3750Sstevel@tonic-gate 		}
3760Sstevel@tonic-gate 		fclose(fd);
3770Sstevel@tonic-gate 	}
3780Sstevel@tonic-gate 
3790Sstevel@tonic-gate 	/* now modify the attribute list with the new attributes in 'attrs' */
3800Sstevel@tonic-gate 
3810Sstevel@tonic-gate 	nextAttr = papiAttributeListGetNext((papi_attribute_t **)attrs, &iter);
3822264Sjacobs 	while ((result == PAPI_OK) && (nextAttr != NULL)) {
3830Sstevel@tonic-gate 		values = nextAttr->values;
3840Sstevel@tonic-gate 
3852264Sjacobs 		if ((values != NULL) && (*values != NULL)) {
3862264Sjacobs 			result = papiAttributeListAddValue(newAttrs,
3870Sstevel@tonic-gate 						    PAPI_ATTR_REPLACE,
3880Sstevel@tonic-gate 						    nextAttr->name,
3890Sstevel@tonic-gate 						    nextAttr->type, *values);
3900Sstevel@tonic-gate 			values++;
3910Sstevel@tonic-gate 		}
3920Sstevel@tonic-gate 
3930Sstevel@tonic-gate 		while ((result == PAPI_OK) &&
3942264Sjacobs 			(values != NULL) && (*values != NULL)) {
3952264Sjacobs 			result = papiAttributeListAddValue(newAttrs,
3960Sstevel@tonic-gate 						    PAPI_ATTR_APPEND,
3970Sstevel@tonic-gate 						    nextAttr->name,
3980Sstevel@tonic-gate 						    nextAttr->type, *values);
3990Sstevel@tonic-gate 			values++;
4000Sstevel@tonic-gate 		}
4010Sstevel@tonic-gate 		nextAttr =
4020Sstevel@tonic-gate 		    papiAttributeListGetNext((papi_attribute_t **)attrs, &iter);
4030Sstevel@tonic-gate 	}
4040Sstevel@tonic-gate 
4050Sstevel@tonic-gate 	return (result);
4060Sstevel@tonic-gate } /* papi_modifyAttrsList() */
4070Sstevel@tonic-gate #endif
4080Sstevel@tonic-gate 
4090Sstevel@tonic-gate 
4100Sstevel@tonic-gate papi_status_t
4112264Sjacobs papiJobSubmit(papi_service_t handle, char *printer,
4122264Sjacobs 		papi_attribute_t **job_attributes,
4132264Sjacobs 		papi_job_ticket_t *job_ticket,
4142264Sjacobs 		char **files, papi_job_t *job)
4150Sstevel@tonic-gate {
4160Sstevel@tonic-gate 	papi_status_t status;
4170Sstevel@tonic-gate 	service_t *svc = handle;
4188569SJonathan.Ca@Sun.COM 	struct stat statbuf;
4190Sstevel@tonic-gate 	job_t *j;
4200Sstevel@tonic-gate 	int file_no;
4210Sstevel@tonic-gate 	char *request_id = NULL;
4220Sstevel@tonic-gate 	REQUEST *request;
4230Sstevel@tonic-gate 	int i;
4240Sstevel@tonic-gate 	char *c;
4250Sstevel@tonic-gate 	char *tmp = NULL;
4260Sstevel@tonic-gate 	char lpfile[BUFSIZ];
4270Sstevel@tonic-gate 
4280Sstevel@tonic-gate 	if ((svc == NULL) || (printer == NULL) || (files == NULL) ||
4290Sstevel@tonic-gate 	    (job == NULL))
4300Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
4310Sstevel@tonic-gate 
4320Sstevel@tonic-gate 	if (job_ticket != NULL)
4330Sstevel@tonic-gate 		return (PAPI_OPERATION_NOT_SUPPORTED);
4340Sstevel@tonic-gate 
4350Sstevel@tonic-gate 	if (files != NULL)
4368569SJonathan.Ca@Sun.COM 		for (file_no = 0; files[file_no] != NULL; file_no++) {
4370Sstevel@tonic-gate 			if (access(files[file_no], R_OK) < 0) {
4380Sstevel@tonic-gate 				detailed_error(svc,
4398569SJonathan.Ca@Sun.COM 				    gettext("Cannot access file: %s: %s"),
4408569SJonathan.Ca@Sun.COM 				    files[file_no], strerror(errno));
4410Sstevel@tonic-gate 				return (PAPI_BAD_ARGUMENT);
4420Sstevel@tonic-gate 			}
4438569SJonathan.Ca@Sun.COM 			stat(files[file_no], &statbuf);
4448569SJonathan.Ca@Sun.COM 			if (statbuf.st_size == 0) {
4458569SJonathan.Ca@Sun.COM 				detailed_error(svc,
4468569SJonathan.Ca@Sun.COM 				    gettext("Zero byte (empty) file: %s"),
4478569SJonathan.Ca@Sun.COM 				    files[file_no]);
4488569SJonathan.Ca@Sun.COM 				return (PAPI_BAD_ARGUMENT);
4498569SJonathan.Ca@Sun.COM 			}
4508569SJonathan.Ca@Sun.COM 		}
4510Sstevel@tonic-gate 
4520Sstevel@tonic-gate 	if ((*job = j = calloc(1, sizeof (*j))) == NULL)
4530Sstevel@tonic-gate 		return (PAPI_TEMPORARY_ERROR);
4540Sstevel@tonic-gate 
4550Sstevel@tonic-gate 	/* file_no + 1 for the control file (-0) */
4560Sstevel@tonic-gate 	status = lpsched_alloc_files(svc, file_no + 1, &request_id);
4570Sstevel@tonic-gate 	if (status != PAPI_OK)
4580Sstevel@tonic-gate 		return (status);
4590Sstevel@tonic-gate 
4600Sstevel@tonic-gate 	request = create_request(svc, (char *)printer,
4618569SJonathan.Ca@Sun.COM 	    (papi_attribute_t **)job_attributes);
4620Sstevel@tonic-gate 
4630Sstevel@tonic-gate 	for (i = 0; files[i] != NULL; i++) {
4640Sstevel@tonic-gate 		papi_status_t status;
4650Sstevel@tonic-gate 		snprintf(lpfile, sizeof (lpfile), "%s%s-%d",
4668569SJonathan.Ca@Sun.COM 		    "/var/spool/lp/temp/", request_id, i+1);
4670Sstevel@tonic-gate 		status = copy_file(files[i], lpfile);
4680Sstevel@tonic-gate 		if (status != PAPI_OK) {
4690Sstevel@tonic-gate 			detailed_error(svc,
4708569SJonathan.Ca@Sun.COM 			    gettext("unable to copy: %s -> %s: %s"),
4718569SJonathan.Ca@Sun.COM 			    files[i], lpfile, strerror(errno));
4720Sstevel@tonic-gate 				freerequest(request);
4730Sstevel@tonic-gate 			return (PAPI_DEVICE_ERROR);
4740Sstevel@tonic-gate 		}
4750Sstevel@tonic-gate 		addlist(&(request->file_list), lpfile);
4760Sstevel@tonic-gate 	}
4770Sstevel@tonic-gate 
4780Sstevel@tonic-gate #ifdef LP_USE_PAPI_ATTR
4790Sstevel@tonic-gate 	/*
4800Sstevel@tonic-gate 	 * store the job attributes in the PAPI job attribute file that was
4810Sstevel@tonic-gate 	 * created by lpsched_alloc_files(), the attributes will then pass
4820Sstevel@tonic-gate 	 * through lpsched and be given to the slow-filters and the printer's
4830Sstevel@tonic-gate 	 * interface script to process them
4840Sstevel@tonic-gate 	 */
4850Sstevel@tonic-gate 	snprintf(lpfile, sizeof (lpfile), "%s%s-%s",
4868569SJonathan.Ca@Sun.COM 	    "/var/spool/lp/temp/", request_id, LP_PAPIATTRNAME);
4870Sstevel@tonic-gate 	status = psm_copy_attrsToFile(job_attributes, lpfile);
4880Sstevel@tonic-gate 	if (status != PAPI_OK) {
4890Sstevel@tonic-gate 		detailed_error(svc, "unable to copy attributes to file: %s: %s",
4908569SJonathan.Ca@Sun.COM 		    lpfile, strerror(errno));
4910Sstevel@tonic-gate 		return (PAPI_DEVICE_ERROR);
4920Sstevel@tonic-gate 	}
4930Sstevel@tonic-gate #endif
4940Sstevel@tonic-gate 
4950Sstevel@tonic-gate 	/* store the meta-data file */
4960Sstevel@tonic-gate 	snprintf(lpfile, sizeof (lpfile), "%s-0", request_id);
4970Sstevel@tonic-gate 	if (putrequest(lpfile, request) < 0) {
4980Sstevel@tonic-gate 		detailed_error(svc, gettext("unable to save request: %s: %s"),
4998569SJonathan.Ca@Sun.COM 		    lpfile, strerror(errno));
5000Sstevel@tonic-gate 		freerequest(request);
5010Sstevel@tonic-gate 		return (PAPI_DEVICE_ERROR);
5020Sstevel@tonic-gate 	}
5030Sstevel@tonic-gate 
5040Sstevel@tonic-gate 	status = lpsched_commit_job(svc, lpfile, &tmp);
5050Sstevel@tonic-gate 	if (status != PAPI_OK) {
5060Sstevel@tonic-gate 		unlink(lpfile);
5070Sstevel@tonic-gate 		freerequest(request);
5080Sstevel@tonic-gate 		return (status);
5090Sstevel@tonic-gate 	}
5100Sstevel@tonic-gate 
5110Sstevel@tonic-gate 	lpsched_request_to_job_attributes(request, j);
5120Sstevel@tonic-gate 	freerequest(request);
5130Sstevel@tonic-gate 
5140Sstevel@tonic-gate 	if ((c = strrchr(tmp, '-')) != NULL)
5150Sstevel@tonic-gate 		c++;
5160Sstevel@tonic-gate 	papiAttributeListAddInteger(&j->attributes, PAPI_ATTR_REPLACE,
5178569SJonathan.Ca@Sun.COM 	    "job-id", atoi(c));
5180Sstevel@tonic-gate 	papiAttributeListAddString(&j->attributes, PAPI_ATTR_REPLACE,
5198569SJonathan.Ca@Sun.COM 	    "job-uri", tmp);
5200Sstevel@tonic-gate 
5210Sstevel@tonic-gate 	return (PAPI_OK);
5220Sstevel@tonic-gate }
5230Sstevel@tonic-gate 
5240Sstevel@tonic-gate papi_status_t
5252264Sjacobs papiJobSubmitByReference(papi_service_t handle, char *printer,
5262264Sjacobs 		papi_attribute_t **job_attributes,
5272264Sjacobs 		papi_job_ticket_t *job_ticket,
5282264Sjacobs 		char **files, papi_job_t *job)
5290Sstevel@tonic-gate {
5300Sstevel@tonic-gate 	service_t *svc = handle;
5318569SJonathan.Ca@Sun.COM 	struct stat statbuf;
5320Sstevel@tonic-gate 	job_t *j;
5330Sstevel@tonic-gate 	int file_no;
5340Sstevel@tonic-gate 	short status;
5350Sstevel@tonic-gate 	char *request_id = NULL;
5360Sstevel@tonic-gate 	REQUEST *request;
5370Sstevel@tonic-gate 	char *c;
5380Sstevel@tonic-gate 	char *tmp = NULL;
5390Sstevel@tonic-gate 	char lpfile[BUFSIZ];
5400Sstevel@tonic-gate 	char **file_list = NULL;
5410Sstevel@tonic-gate 
5420Sstevel@tonic-gate 	if ((svc == NULL) || (printer == NULL) || (files == NULL) ||
5430Sstevel@tonic-gate 	    (job == NULL))
5440Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
5450Sstevel@tonic-gate 
5460Sstevel@tonic-gate 	if (job_ticket != NULL)
5470Sstevel@tonic-gate 		return (PAPI_OPERATION_NOT_SUPPORTED);
5480Sstevel@tonic-gate 
5490Sstevel@tonic-gate 	if (files != NULL)
5500Sstevel@tonic-gate 		for (file_no = 0; files[file_no] != NULL; file_no++) {
5510Sstevel@tonic-gate 			if (access(files[file_no], R_OK) < 0) {
5520Sstevel@tonic-gate 				detailed_error(svc,
5538569SJonathan.Ca@Sun.COM 				    gettext("Cannot access file: %s: %s"),
5548569SJonathan.Ca@Sun.COM 				    files[file_no], strerror(errno));
5552660Sjacobs 				return (PAPI_DOCUMENT_ACCESS_ERROR);
5560Sstevel@tonic-gate 			}
5578569SJonathan.Ca@Sun.COM 			stat(files[file_no], &statbuf);
5588569SJonathan.Ca@Sun.COM 			if (statbuf.st_size == 0) {
5598569SJonathan.Ca@Sun.COM 				detailed_error(svc,
5608569SJonathan.Ca@Sun.COM 				    gettext("Zero byte (empty) file: %s"),
5618569SJonathan.Ca@Sun.COM 				    files[file_no]);
5628569SJonathan.Ca@Sun.COM 				return (PAPI_BAD_ARGUMENT);
5638569SJonathan.Ca@Sun.COM 			}
5648569SJonathan.Ca@Sun.COM 
5652660Sjacobs 			if (files[file_no][0] != '/') {
5662660Sjacobs 				char path[MAXPATHLEN];
5672660Sjacobs 
5682660Sjacobs 				if (getcwd(path, sizeof (path)) == NULL) {
5692660Sjacobs 					detailed_error(svc, gettext(
5708569SJonathan.Ca@Sun.COM 					    "getcwd for file: %s: %s"),
5718569SJonathan.Ca@Sun.COM 					    files[file_no],
5728569SJonathan.Ca@Sun.COM 					    strerror(errno));
5732660Sjacobs 					return (PAPI_DOCUMENT_ACCESS_ERROR);
5742660Sjacobs 				}
5752660Sjacobs 				strlcat(path, "/", sizeof (path));
5762660Sjacobs 				if (strlcat(path, files[file_no], sizeof (path))
5778569SJonathan.Ca@Sun.COM 				    >= sizeof (path)) {
5782660Sjacobs 					detailed_error(svc, gettext(
5798569SJonathan.Ca@Sun.COM 					    "pathname too long: %s"),
5808569SJonathan.Ca@Sun.COM 					    files[file_no]);
5812660Sjacobs 					return (PAPI_DOCUMENT_ACCESS_ERROR);
5822660Sjacobs 				}
5832660Sjacobs 				addlist(&file_list, path);
5842660Sjacobs 			} else
5852660Sjacobs 				addlist(&file_list, (char *)files[file_no]);
5860Sstevel@tonic-gate 		}
5870Sstevel@tonic-gate 
5880Sstevel@tonic-gate 	if ((*job = j = calloc(1, sizeof (*j))) == NULL)
5890Sstevel@tonic-gate 		return (PAPI_TEMPORARY_ERROR);
5900Sstevel@tonic-gate 
5910Sstevel@tonic-gate 	/* 1 for the control file (-0) */
5920Sstevel@tonic-gate 	status = lpsched_alloc_files(svc, 1, &request_id);
5930Sstevel@tonic-gate 	if (status != PAPI_OK)
5940Sstevel@tonic-gate 		return (status);
5950Sstevel@tonic-gate 
5960Sstevel@tonic-gate 	request = create_request(svc, (char *)printer,
5978569SJonathan.Ca@Sun.COM 	    (papi_attribute_t **)job_attributes);
5980Sstevel@tonic-gate 	request->file_list = file_list;
5990Sstevel@tonic-gate 
6000Sstevel@tonic-gate #ifdef LP_USE_PAPI_ATTR
6010Sstevel@tonic-gate 	/*
6020Sstevel@tonic-gate 	 * store the job attributes in the PAPI job attribute file that was
6030Sstevel@tonic-gate 	 * created by lpsched_alloc_files(), the attributes will then pass
6040Sstevel@tonic-gate 	 * through lpsched and be given to the slow-filters and the printer's
6050Sstevel@tonic-gate 	 * interface script to process them
6060Sstevel@tonic-gate 	 */
6070Sstevel@tonic-gate 	snprintf(lpfile, sizeof (lpfile), "%s%s-%s",
6088569SJonathan.Ca@Sun.COM 	    "/var/spool/lp/temp/", request_id, LP_PAPIATTRNAME);
6090Sstevel@tonic-gate 	status = psm_copy_attrsToFile(job_attributes, lpfile);
6100Sstevel@tonic-gate 	if (status != PAPI_OK) {
6110Sstevel@tonic-gate 		detailed_error(svc, "unable to copy attributes to file: %s: %s",
6128569SJonathan.Ca@Sun.COM 		    lpfile, strerror(errno));
6130Sstevel@tonic-gate 		return (PAPI_DEVICE_ERROR);
6140Sstevel@tonic-gate 	}
6150Sstevel@tonic-gate #endif
6160Sstevel@tonic-gate 
6170Sstevel@tonic-gate 	/* store the meta-data file */
6180Sstevel@tonic-gate 	snprintf(lpfile, sizeof (lpfile), "%s-0", request_id);
6190Sstevel@tonic-gate 	if (putrequest(lpfile, request) < 0) {
6200Sstevel@tonic-gate 		detailed_error(svc, gettext("unable to save request: %s: %s"),
6218569SJonathan.Ca@Sun.COM 		    lpfile, strerror(errno));
6220Sstevel@tonic-gate 		freerequest(request);
6230Sstevel@tonic-gate 		return (PAPI_DEVICE_ERROR);
6240Sstevel@tonic-gate 	}
6250Sstevel@tonic-gate 
6260Sstevel@tonic-gate 	status = lpsched_commit_job(svc, lpfile, &tmp);
6270Sstevel@tonic-gate 	if (status != PAPI_OK) {
6280Sstevel@tonic-gate 		unlink(lpfile);
6290Sstevel@tonic-gate 		freerequest(request);
6300Sstevel@tonic-gate 		return (status);
6310Sstevel@tonic-gate 	}
6320Sstevel@tonic-gate 
6330Sstevel@tonic-gate 	lpsched_request_to_job_attributes(request, j);
6340Sstevel@tonic-gate 
6350Sstevel@tonic-gate 	freerequest(request);
6360Sstevel@tonic-gate 
6370Sstevel@tonic-gate 	if ((c = strrchr(tmp, '-')) != NULL)
6380Sstevel@tonic-gate 		c++;
6390Sstevel@tonic-gate 	papiAttributeListAddInteger(&j->attributes, PAPI_ATTR_REPLACE,
6408569SJonathan.Ca@Sun.COM 	    "job-id", atoi(c));
6410Sstevel@tonic-gate 	papiAttributeListAddString(&j->attributes, PAPI_ATTR_REPLACE,
6428569SJonathan.Ca@Sun.COM 	    "job-uri", tmp);
6430Sstevel@tonic-gate 
6440Sstevel@tonic-gate 	return (PAPI_OK);
6450Sstevel@tonic-gate }
6460Sstevel@tonic-gate 
6470Sstevel@tonic-gate papi_status_t
6482264Sjacobs papiJobValidate(papi_service_t handle, char *printer,
6492264Sjacobs 		papi_attribute_t **job_attributes,
6502264Sjacobs 		papi_job_ticket_t *job_ticket,
6512264Sjacobs 		char **files, papi_job_t *job)
6520Sstevel@tonic-gate {
6530Sstevel@tonic-gate 	papi_status_t status;
6540Sstevel@tonic-gate 	papi_attribute_t **attributes = NULL;
6550Sstevel@tonic-gate 	int i;
6560Sstevel@tonic-gate 
6570Sstevel@tonic-gate 	papiAttributeListAddString(&attributes, PAPI_ATTR_REPLACE,
6588569SJonathan.Ca@Sun.COM 	    "job-hold-until", "indefinite");
6590Sstevel@tonic-gate 	for (i = 0; job_attributes[i]; i++)
6600Sstevel@tonic-gate 		list_append(&attributes, job_attributes[i]);
6610Sstevel@tonic-gate 
6620Sstevel@tonic-gate 	status = papiJobSubmitByReference(handle, printer,
6638569SJonathan.Ca@Sun.COM 	    (papi_attribute_t **)attributes,
6648569SJonathan.Ca@Sun.COM 	    job_ticket, files, job);
6650Sstevel@tonic-gate 	if (status == PAPI_OK) {
6660Sstevel@tonic-gate 		int id = papiJobGetId(*job);
6670Sstevel@tonic-gate 
6680Sstevel@tonic-gate 		if (id != -1)
6690Sstevel@tonic-gate 			papiJobCancel(handle, printer, id);
6700Sstevel@tonic-gate 	}
6710Sstevel@tonic-gate 
6720Sstevel@tonic-gate 	attributes[1] = NULL;	/* after attr[0], they are in another list */
6730Sstevel@tonic-gate 	papiAttributeListFree(attributes);
6740Sstevel@tonic-gate 
6750Sstevel@tonic-gate 	return (status);
6760Sstevel@tonic-gate }
6770Sstevel@tonic-gate 
6780Sstevel@tonic-gate papi_status_t
6792264Sjacobs papiJobStreamOpen(papi_service_t handle, char *printer,
6802264Sjacobs 		papi_attribute_t **job_attributes,
6812264Sjacobs 		papi_job_ticket_t *job_ticket, papi_stream_t *stream)
6820Sstevel@tonic-gate {
6830Sstevel@tonic-gate 	papi_status_t status;
6840Sstevel@tonic-gate 	service_t *svc = handle;
6850Sstevel@tonic-gate 	job_stream_t *s = NULL;
6860Sstevel@tonic-gate 	char *request_id = NULL;
6870Sstevel@tonic-gate 	char lpfile[BUFSIZ];
6880Sstevel@tonic-gate 
6890Sstevel@tonic-gate 	if ((svc == NULL) || (printer == NULL) || (stream == NULL))
6900Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
6910Sstevel@tonic-gate 
6920Sstevel@tonic-gate 	if (job_ticket != NULL)
6930Sstevel@tonic-gate 		return (PAPI_OPERATION_NOT_SUPPORTED);
6940Sstevel@tonic-gate 
6950Sstevel@tonic-gate 	if ((*stream = s = calloc(1, sizeof (*s))) == NULL)
6960Sstevel@tonic-gate 		return (PAPI_TEMPORARY_ERROR);
6970Sstevel@tonic-gate 
6980Sstevel@tonic-gate 	/* 1 for data, 1 for the meta-data (-0) */
6990Sstevel@tonic-gate 	status = lpsched_alloc_files(svc, 2, &request_id);
7000Sstevel@tonic-gate 	if (status != PAPI_OK)
7010Sstevel@tonic-gate 		return (status);
7020Sstevel@tonic-gate 
7030Sstevel@tonic-gate 	s->request = create_request(svc, (char *)printer,
7048569SJonathan.Ca@Sun.COM 	    (papi_attribute_t **)job_attributes);
7050Sstevel@tonic-gate 	snprintf(lpfile, sizeof (lpfile), "/var/spool/lp/temp/%s-1",
7068569SJonathan.Ca@Sun.COM 	    request_id);
7070Sstevel@tonic-gate 	s->fd = open(lpfile, O_WRONLY);
7080Sstevel@tonic-gate 	addlist(&(s->request->file_list), lpfile);
7090Sstevel@tonic-gate 
7100Sstevel@tonic-gate #ifdef LP_USE_PAPI_ATTR
7110Sstevel@tonic-gate 	/*
7120Sstevel@tonic-gate 	 * store the job attributes in the PAPI job attribute file that was
7130Sstevel@tonic-gate 	 * created by lpsched_alloc_files(), the attributes will then pass
7140Sstevel@tonic-gate 	 * through lpsched and be given to the slow-filters and the printer's
7150Sstevel@tonic-gate 	 * interface script to process them
7160Sstevel@tonic-gate 	 */
7170Sstevel@tonic-gate 	snprintf(lpfile, sizeof (lpfile), "%s%s-%s",
7188569SJonathan.Ca@Sun.COM 	    "/var/spool/lp/temp/", request_id, LP_PAPIATTRNAME);
7190Sstevel@tonic-gate 	status = psm_copy_attrsToFile(job_attributes, lpfile);
7200Sstevel@tonic-gate 	if (status != PAPI_OK) {
7210Sstevel@tonic-gate 		detailed_error(svc, "unable to copy attributes to file: %s: %s",
7228569SJonathan.Ca@Sun.COM 		    lpfile, strerror(errno));
7230Sstevel@tonic-gate 		close(s->fd);
7240Sstevel@tonic-gate 		free(s);
7250Sstevel@tonic-gate 		return (PAPI_DEVICE_ERROR);
7260Sstevel@tonic-gate 	}
7270Sstevel@tonic-gate #endif
7280Sstevel@tonic-gate 
7290Sstevel@tonic-gate 	/* store the meta-data file */
7300Sstevel@tonic-gate 	snprintf(lpfile, sizeof (lpfile), "%s-0", request_id);
7310Sstevel@tonic-gate 	s->meta_data_file = strdup(lpfile);
7320Sstevel@tonic-gate 	if (putrequest(lpfile, s->request) < 0) {
7330Sstevel@tonic-gate 		detailed_error(svc, gettext("unable to save request: %s: %s"),
7348569SJonathan.Ca@Sun.COM 		    lpfile, strerror(errno));
7350Sstevel@tonic-gate 		s->request = NULL;
7360Sstevel@tonic-gate 		return (PAPI_DEVICE_ERROR);
7370Sstevel@tonic-gate 	}
7380Sstevel@tonic-gate 
7390Sstevel@tonic-gate 	return (PAPI_OK);
7400Sstevel@tonic-gate }
7410Sstevel@tonic-gate 
7420Sstevel@tonic-gate papi_status_t
7430Sstevel@tonic-gate papiJobStreamWrite(papi_service_t handle,
7442264Sjacobs 		papi_stream_t stream, void *buffer, size_t buflen)
7450Sstevel@tonic-gate {
7460Sstevel@tonic-gate 	service_t *svc = handle;
7470Sstevel@tonic-gate 	job_stream_t *s = stream;
7480Sstevel@tonic-gate 
7490Sstevel@tonic-gate 	if ((svc == NULL) || (stream == NULL) || (buffer == NULL))
7500Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
7510Sstevel@tonic-gate 
7520Sstevel@tonic-gate 	if (write(s->fd, buffer, buflen) != buflen)
7530Sstevel@tonic-gate 		return (PAPI_DEVICE_ERROR);
7540Sstevel@tonic-gate 
7550Sstevel@tonic-gate 	return (PAPI_OK);
7560Sstevel@tonic-gate }
7570Sstevel@tonic-gate papi_status_t
7580Sstevel@tonic-gate papiJobStreamClose(papi_service_t handle,
7590Sstevel@tonic-gate 		papi_stream_t stream, papi_job_t *job)
7600Sstevel@tonic-gate {
7610Sstevel@tonic-gate 	papi_status_t status = PAPI_OK;
7620Sstevel@tonic-gate 	service_t *svc = handle;
7630Sstevel@tonic-gate 	job_stream_t *s = stream;
7640Sstevel@tonic-gate 	job_t *j = NULL;
7650Sstevel@tonic-gate 	char *tmp = NULL, *c;
7660Sstevel@tonic-gate 
7670Sstevel@tonic-gate 	if ((svc == NULL) || (stream == NULL) || (job == NULL))
7680Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
7690Sstevel@tonic-gate 
7700Sstevel@tonic-gate 	if ((*job = j = calloc(1, sizeof (*j))) == NULL)
7710Sstevel@tonic-gate 		return (PAPI_TEMPORARY_ERROR);
7720Sstevel@tonic-gate 
7730Sstevel@tonic-gate 	close(s->fd);
7740Sstevel@tonic-gate 
7750Sstevel@tonic-gate 	lpsched_request_to_job_attributes(s->request, j);
7760Sstevel@tonic-gate 
7770Sstevel@tonic-gate 	if (s->meta_data_file != NULL) {
7780Sstevel@tonic-gate 		status = lpsched_commit_job(svc, s->meta_data_file, &tmp);
7790Sstevel@tonic-gate 		if (status != PAPI_OK) {
7800Sstevel@tonic-gate 			unlink(s->meta_data_file);
7810Sstevel@tonic-gate 			return (status);
7820Sstevel@tonic-gate 		}
7830Sstevel@tonic-gate 		if ((c = strrchr(tmp, '-')) != NULL)
7840Sstevel@tonic-gate 			c++;
7850Sstevel@tonic-gate 		papiAttributeListAddInteger(&j->attributes, PAPI_ATTR_REPLACE,
7868569SJonathan.Ca@Sun.COM 		    "job-id", atoi(c));
7870Sstevel@tonic-gate 		papiAttributeListAddString(&j->attributes, PAPI_ATTR_REPLACE,
7888569SJonathan.Ca@Sun.COM 		    "job-uri", tmp);
7890Sstevel@tonic-gate 		free(s->meta_data_file);
7900Sstevel@tonic-gate 	}
7917253Sjacobs 	freerequest(s->request);
7920Sstevel@tonic-gate 	free(s);
7930Sstevel@tonic-gate 
7940Sstevel@tonic-gate 	return (PAPI_OK);
7950Sstevel@tonic-gate }
7960Sstevel@tonic-gate 
7970Sstevel@tonic-gate papi_status_t
7982264Sjacobs papiJobQuery(papi_service_t handle, char *printer, int32_t job_id,
7992264Sjacobs 		char **requested_attrs,
8000Sstevel@tonic-gate 		papi_job_t *job)
8010Sstevel@tonic-gate {
8020Sstevel@tonic-gate 	service_t *svc = handle;
8030Sstevel@tonic-gate 	job_t *j;
8040Sstevel@tonic-gate 	char *dest;
8050Sstevel@tonic-gate 	char req_id[32];
8060Sstevel@tonic-gate 	short rc;
8070Sstevel@tonic-gate 	char *form = NULL,
8088569SJonathan.Ca@Sun.COM 	    *request_id = NULL,
8098569SJonathan.Ca@Sun.COM 	    *charset = NULL,
8108569SJonathan.Ca@Sun.COM 	    *user = NULL,
8118569SJonathan.Ca@Sun.COM 	    *slabel = NULL,
8128569SJonathan.Ca@Sun.COM 	    *file = NULL;
8130Sstevel@tonic-gate 	time_t date = 0;
8140Sstevel@tonic-gate 	size_t size = 0;
8150Sstevel@tonic-gate 	short  rank = 0,
8168569SJonathan.Ca@Sun.COM 	    state = 0;
8170Sstevel@tonic-gate 
8180Sstevel@tonic-gate 	if ((handle == NULL) || (printer == NULL) || (job_id < 0))
8190Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
8200Sstevel@tonic-gate 
8210Sstevel@tonic-gate 	dest = printer_name_from_uri_id(printer, job_id);
8220Sstevel@tonic-gate 	snprintf(req_id, sizeof (req_id), "%s-%d", dest, job_id);
8230Sstevel@tonic-gate 	free(dest);
8240Sstevel@tonic-gate 
8250Sstevel@tonic-gate 	rc = snd_msg(svc, S_INQUIRE_REQUEST_RANK, 0, "", "", req_id, "", "");
8260Sstevel@tonic-gate 	if (rc < 0)
8270Sstevel@tonic-gate 		return (PAPI_SERVICE_UNAVAILABLE);
8280Sstevel@tonic-gate 
8290Sstevel@tonic-gate 	if (rcv_msg(svc, R_INQUIRE_REQUEST_RANK, &rc, &request_id,
8308569SJonathan.Ca@Sun.COM 	    &user, &slabel, &size, &date, &state, &dest, &form,
8318569SJonathan.Ca@Sun.COM 	    &charset, &rank, &file) < 0) {
8320Sstevel@tonic-gate 		detailed_error(svc,
8338569SJonathan.Ca@Sun.COM 		    gettext("failed to read response from scheduler"));
8340Sstevel@tonic-gate 		return (PAPI_DEVICE_ERROR);
8350Sstevel@tonic-gate 	}
8360Sstevel@tonic-gate 
8370Sstevel@tonic-gate 	if ((request_id == NULL) || (request_id[0] == NULL))
8380Sstevel@tonic-gate 		return (PAPI_NOT_FOUND);
8390Sstevel@tonic-gate 
8400Sstevel@tonic-gate 	if ((*job = j = calloc(1, sizeof (*j))) == NULL)
8410Sstevel@tonic-gate 		return (PAPI_TEMPORARY_ERROR);
8420Sstevel@tonic-gate 
8431676Sjpk 	job_status_to_attributes(j, request_id, user, slabel, size, date, state,
8448569SJonathan.Ca@Sun.COM 	    dest, form, charset, rank, file);
8450Sstevel@tonic-gate 
8460Sstevel@tonic-gate 	snprintf(req_id, sizeof (req_id), "%d-0", job_id);
8470Sstevel@tonic-gate 	lpsched_read_job_configuration(svc, j, req_id);
8480Sstevel@tonic-gate 
8490Sstevel@tonic-gate 	return (PAPI_OK);
8500Sstevel@tonic-gate }
8510Sstevel@tonic-gate 
8520Sstevel@tonic-gate papi_status_t
8532264Sjacobs papiJobMove(papi_service_t handle, char *printer, int32_t job_id,
8542264Sjacobs 		char *destination)
8552264Sjacobs {
8562264Sjacobs 	papi_status_t result = PAPI_OK;
8578266SNagaraj.Yedathore@Sun.COM 	long bits;
8582264Sjacobs 	service_t *svc = handle;
8592264Sjacobs 	char req_id[64];
8602264Sjacobs 	char *queue;
8612264Sjacobs 	char *user = NULL;
8622264Sjacobs 
8632264Sjacobs 	if ((svc == NULL) || (printer == NULL) || (job_id < 0) ||
8642264Sjacobs 	    (destination == NULL))
8652264Sjacobs 		return (PAPI_BAD_ARGUMENT);
8662264Sjacobs 
8672264Sjacobs 	queue = printer_name_from_uri_id(printer, job_id);
8682264Sjacobs 	snprintf(req_id, sizeof (req_id), "%s-%d", queue, job_id);
8692264Sjacobs 	free(queue);
8702264Sjacobs 
8712264Sjacobs 	if (papiAttributeListGetString(svc->attributes, NULL, "user-name",
8728569SJonathan.Ca@Sun.COM 	    &user) == PAPI_OK) {
8732264Sjacobs 		REQUEST *r = getrequest(req_id);
8742264Sjacobs 
8752264Sjacobs 		if ((r != NULL) && (r->user != NULL) &&
8762264Sjacobs 		    (strcmp(r->user, user) != 0))
8772264Sjacobs 			result = PAPI_NOT_AUTHORIZED;
8782264Sjacobs 		freerequest(r);
8792264Sjacobs 	}
8802264Sjacobs 
8812264Sjacobs 	if (result == PAPI_OK) {
8822264Sjacobs 		short status = MOK;
8832264Sjacobs 		char *dest = printer_name_from_uri_id(destination, -1);
8842264Sjacobs 
8852264Sjacobs 		if ((snd_msg(svc, S_MOVE_REQUEST, req_id, dest) < 0) ||
8868266SNagaraj.Yedathore@Sun.COM 		    (rcv_msg(svc, R_MOVE_REQUEST, &status, &bits) < 0))
8872264Sjacobs 			status = MTRANSMITERR;
8882264Sjacobs 
8892264Sjacobs 		free(dest);
8902264Sjacobs 
8912264Sjacobs 		result = lpsched_status_to_papi_status(status);
8922264Sjacobs 	}
8932264Sjacobs 
8942264Sjacobs 	return (result);
8952264Sjacobs }
8962264Sjacobs 
8972264Sjacobs papi_status_t
8982264Sjacobs papiJobCancel(papi_service_t handle, char *printer, int32_t job_id)
8990Sstevel@tonic-gate {
9000Sstevel@tonic-gate 	papi_status_t result = PAPI_OK;
9010Sstevel@tonic-gate 	service_t *svc = handle;
9020Sstevel@tonic-gate 	char req_id[64];
9030Sstevel@tonic-gate 	char *dest;
9040Sstevel@tonic-gate 	char *user = NULL;
9050Sstevel@tonic-gate 
9060Sstevel@tonic-gate 	if ((svc == NULL) || (printer == NULL) || (job_id < 0))
9070Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
9080Sstevel@tonic-gate 
9090Sstevel@tonic-gate 	dest = printer_name_from_uri_id(printer, job_id);
9100Sstevel@tonic-gate 	snprintf(req_id, sizeof (req_id), "%s-%d", dest, job_id);
9110Sstevel@tonic-gate 	free(dest);
9120Sstevel@tonic-gate 
9130Sstevel@tonic-gate 	if (papiAttributeListGetString(svc->attributes, NULL, "user-name",
9148569SJonathan.Ca@Sun.COM 	    &user) == PAPI_OK) {
9150Sstevel@tonic-gate 		REQUEST *r = getrequest(req_id);
9160Sstevel@tonic-gate 
917*8966SSonam.Gupta@Sun.COM 		if ((result = authorized(handle, job_id)) != PAPI_OK)
918*8966SSonam.Gupta@Sun.COM 			result = PAPI_NOT_AUTHORIZED;
919*8966SSonam.Gupta@Sun.COM 
9202264Sjacobs 		if ((r != NULL) && (r->user != NULL) &&
9212264Sjacobs 		    (strcmp(r->user, user) != 0))
9220Sstevel@tonic-gate 			result = PAPI_NOT_AUTHORIZED;
9230Sstevel@tonic-gate 		freerequest(r);
9240Sstevel@tonic-gate 	}
9250Sstevel@tonic-gate 
9260Sstevel@tonic-gate 	if (result == PAPI_OK) {
9270Sstevel@tonic-gate 		short status = MOK;
9280Sstevel@tonic-gate 
9290Sstevel@tonic-gate 		if ((snd_msg(svc, S_CANCEL_REQUEST, req_id) < 0) ||
9300Sstevel@tonic-gate 		    (rcv_msg(svc, R_CANCEL_REQUEST, &status) < 0))
9310Sstevel@tonic-gate 			status = MTRANSMITERR;
9320Sstevel@tonic-gate 
9330Sstevel@tonic-gate 		result = lpsched_status_to_papi_status(status);
9340Sstevel@tonic-gate 	}
9350Sstevel@tonic-gate 
9360Sstevel@tonic-gate 	return (result);
9370Sstevel@tonic-gate }
9380Sstevel@tonic-gate 
9390Sstevel@tonic-gate papi_status_t
9402264Sjacobs hold_release_job(papi_service_t handle, char *printer,
9412264Sjacobs 		int32_t job_id, int flag)
9420Sstevel@tonic-gate {
9430Sstevel@tonic-gate 	papi_status_t status;
9440Sstevel@tonic-gate 	service_t *svc = handle;
9450Sstevel@tonic-gate 	REQUEST *r = NULL;
9460Sstevel@tonic-gate 	char *file;
9470Sstevel@tonic-gate 	char *dest;
9480Sstevel@tonic-gate 
9490Sstevel@tonic-gate 	if ((svc == NULL) || (printer == NULL) || (job_id < 0))
9500Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
9510Sstevel@tonic-gate 
9520Sstevel@tonic-gate 	if ((status = authorized(svc, job_id)) != PAPI_OK)
9530Sstevel@tonic-gate 		return (status);
9540Sstevel@tonic-gate 
9550Sstevel@tonic-gate 	dest = printer_name_from_uri_id(printer, job_id);
9560Sstevel@tonic-gate 	status = lpsched_start_change(svc, dest, job_id, &file);
9570Sstevel@tonic-gate 	if (status != PAPI_OK)
9580Sstevel@tonic-gate 		return (status);
9590Sstevel@tonic-gate 
9600Sstevel@tonic-gate 	if ((r = getrequest(file)) != NULL) {
9610Sstevel@tonic-gate 		r->actions &= ~ACT_RESUME;
9622264Sjacobs 		switch (flag) {
9632264Sjacobs 		case 0:
9640Sstevel@tonic-gate 			r->actions |= ACT_HOLD;
9652264Sjacobs 			break;
9662264Sjacobs 		case 1:
9670Sstevel@tonic-gate 			r->actions |= ACT_RESUME;
9682264Sjacobs 			break;
9692264Sjacobs 		case 2:
9702264Sjacobs 			r->actions |= ACT_IMMEDIATE;
9712264Sjacobs 			break;
9722264Sjacobs 		}
9730Sstevel@tonic-gate 		if (putrequest(file, r) < 0) {
9740Sstevel@tonic-gate 			detailed_error(svc,
9758569SJonathan.Ca@Sun.COM 			    gettext("failed to write job: %s: %s"),
9768569SJonathan.Ca@Sun.COM 			    file, strerror(errno));
9772264Sjacobs 			freerequest(r);
9780Sstevel@tonic-gate 			return (PAPI_DEVICE_ERROR);
9790Sstevel@tonic-gate 		}
9802264Sjacobs 		freerequest(r);
9810Sstevel@tonic-gate 	} else {
9820Sstevel@tonic-gate 		detailed_error(svc, gettext("failed to read job: %s: %s"),
9838569SJonathan.Ca@Sun.COM 		    file, strerror(errno));
9840Sstevel@tonic-gate 		return (PAPI_DEVICE_ERROR);
9850Sstevel@tonic-gate 	}
9860Sstevel@tonic-gate 
9870Sstevel@tonic-gate 	status = lpsched_end_change(svc, dest, job_id);
9880Sstevel@tonic-gate 
9890Sstevel@tonic-gate 	return (status);
9900Sstevel@tonic-gate }
9910Sstevel@tonic-gate 
9920Sstevel@tonic-gate papi_status_t
9932264Sjacobs papiJobHold(papi_service_t handle, char *printer, int32_t job_id)
9940Sstevel@tonic-gate {
9950Sstevel@tonic-gate 	return (hold_release_job(handle, printer, job_id, 0));
9960Sstevel@tonic-gate }
9970Sstevel@tonic-gate 
9980Sstevel@tonic-gate papi_status_t
9992264Sjacobs papiJobRelease(papi_service_t handle, char *printer, int32_t job_id)
10000Sstevel@tonic-gate {
10010Sstevel@tonic-gate 	return (hold_release_job(handle, printer, job_id, 1));
10020Sstevel@tonic-gate }
10030Sstevel@tonic-gate 
10040Sstevel@tonic-gate papi_status_t
10052264Sjacobs papiJobPromote(papi_service_t handle, char *printer, int32_t job_id)
10060Sstevel@tonic-gate {
10072264Sjacobs 	return (hold_release_job(handle, printer, job_id, 2));
10080Sstevel@tonic-gate }
10090Sstevel@tonic-gate 
10100Sstevel@tonic-gate papi_status_t
10112264Sjacobs papiJobModify(papi_service_t handle, char *printer, int32_t job_id,
10122264Sjacobs 		papi_attribute_t **attributes, papi_job_t *job)
10130Sstevel@tonic-gate {
10140Sstevel@tonic-gate 	papi_status_t status;
10150Sstevel@tonic-gate 	job_t *j = NULL;
10160Sstevel@tonic-gate 	service_t *svc = handle;
10170Sstevel@tonic-gate 	char *file = NULL;
10180Sstevel@tonic-gate 	char *dest;
10190Sstevel@tonic-gate 	REQUEST *r = NULL;
10200Sstevel@tonic-gate 	char lpfile[BUFSIZ];
10210Sstevel@tonic-gate 
10220Sstevel@tonic-gate 	if ((svc == NULL) || (printer == NULL) || (job_id < 0) ||
10230Sstevel@tonic-gate 	    (attributes == NULL))
10240Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
10250Sstevel@tonic-gate 
10260Sstevel@tonic-gate 	if ((*job = j = calloc(1, sizeof (*j))) == NULL)
10270Sstevel@tonic-gate 		return (PAPI_TEMPORARY_ERROR);
10280Sstevel@tonic-gate 
10290Sstevel@tonic-gate 	dest = printer_name_from_uri_id(printer, job_id);
10300Sstevel@tonic-gate 	status = lpsched_start_change(svc, dest, job_id, &file);
10310Sstevel@tonic-gate 	if (status != PAPI_OK)
10320Sstevel@tonic-gate 		return (status);
10330Sstevel@tonic-gate 
10340Sstevel@tonic-gate 	if ((r = getrequest(file)) != NULL) {
10350Sstevel@tonic-gate 		job_attributes_to_lpsched_request(handle, r,
10368569SJonathan.Ca@Sun.COM 		    (papi_attribute_t **)attributes);
10370Sstevel@tonic-gate #ifdef LP_USE_PAPI_ATTR
10380Sstevel@tonic-gate 		/*
10390Sstevel@tonic-gate 		 * store the job attributes in the PAPI job attribute file
10400Sstevel@tonic-gate 		 * that was created by the origonal job request. We need to
10410Sstevel@tonic-gate 		 * modify the attributes in the file as per the new attributes
10420Sstevel@tonic-gate 		 */
10430Sstevel@tonic-gate 		snprintf(lpfile, sizeof (lpfile), "%s%d-%s",
10448569SJonathan.Ca@Sun.COM 		    "/var/spool/lp/temp/", job_id, LP_PAPIATTRNAME);
10450Sstevel@tonic-gate 		status = psm_modifyAttrsFile(attributes, lpfile);
10460Sstevel@tonic-gate 		if (status != PAPI_OK) {
10470Sstevel@tonic-gate 			detailed_error(svc,
10488569SJonathan.Ca@Sun.COM 			    "unable to modify the attributes file: %s: %s",
10498569SJonathan.Ca@Sun.COM 			    lpfile, strerror(errno));
10500Sstevel@tonic-gate 			return (PAPI_DEVICE_ERROR);
10510Sstevel@tonic-gate 		}
10520Sstevel@tonic-gate #endif
10530Sstevel@tonic-gate 
10540Sstevel@tonic-gate 		if (putrequest(file, r) < 0) {
10550Sstevel@tonic-gate 			detailed_error(svc,
10568569SJonathan.Ca@Sun.COM 			    gettext("failed to write job: %s: %s"),
10578569SJonathan.Ca@Sun.COM 			    file, strerror(errno));
10580Sstevel@tonic-gate 			freerequest(r);
10590Sstevel@tonic-gate 			return (PAPI_DEVICE_ERROR);
10600Sstevel@tonic-gate 		}
10610Sstevel@tonic-gate 	} else {
10620Sstevel@tonic-gate 		detailed_error(svc, gettext("failed to read job: %s: %s"),
10638569SJonathan.Ca@Sun.COM 		    file, strerror(errno));
10640Sstevel@tonic-gate 		return (PAPI_DEVICE_ERROR);
10650Sstevel@tonic-gate 	}
10660Sstevel@tonic-gate 
10670Sstevel@tonic-gate 	status = lpsched_end_change(svc, dest, job_id);
10680Sstevel@tonic-gate 	lpsched_request_to_job_attributes(r, j);
10696725Sjacobs 
10706725Sjacobs 	papiAttributeListAddInteger(&j->attributes, PAPI_ATTR_REPLACE,
10718569SJonathan.Ca@Sun.COM 	    "job-id", job_id);
10726725Sjacobs 
10730Sstevel@tonic-gate 	freerequest(r);
10740Sstevel@tonic-gate 
10750Sstevel@tonic-gate 	return (status);
10760Sstevel@tonic-gate }
10770Sstevel@tonic-gate 
10780Sstevel@tonic-gate /*
10790Sstevel@tonic-gate  * Extension to PAPI, a variation of this is slated for post-1.0
10800Sstevel@tonic-gate  */
10810Sstevel@tonic-gate #define	DUMMY_FILE	"/var/spool/lp/fifos/FIFO"
10820Sstevel@tonic-gate 
10830Sstevel@tonic-gate papi_status_t
10842264Sjacobs papiJobCreate(papi_service_t handle, char *printer,
10852264Sjacobs 		papi_attribute_t **job_attributes,
10862264Sjacobs 		papi_job_ticket_t *job_ticket, papi_job_t *job)
10870Sstevel@tonic-gate {
10880Sstevel@tonic-gate 	papi_status_t status;
10890Sstevel@tonic-gate 	service_t *svc = handle;
10900Sstevel@tonic-gate 	job_t *j = NULL;
10910Sstevel@tonic-gate 	REQUEST *request;
10920Sstevel@tonic-gate 	char *request_id = NULL;
10930Sstevel@tonic-gate 	char *c;
10940Sstevel@tonic-gate 	char *tmp = NULL;
10950Sstevel@tonic-gate 	char metadata_file[MAXPATHLEN];
10960Sstevel@tonic-gate 
10970Sstevel@tonic-gate 	if ((svc == NULL) || (printer == NULL) || (job == NULL))
10980Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
10990Sstevel@tonic-gate 
11000Sstevel@tonic-gate 	if (job_ticket != NULL)
11010Sstevel@tonic-gate 		return (PAPI_JOB_TICKET_NOT_SUPPORTED);
11020Sstevel@tonic-gate 
11030Sstevel@tonic-gate 	if ((*job = j = calloc(1, sizeof (*j))) == NULL)
11040Sstevel@tonic-gate 		return (PAPI_TEMPORARY_ERROR);
11050Sstevel@tonic-gate 
11060Sstevel@tonic-gate 	/* 1 for the control file (-0) */
11070Sstevel@tonic-gate 	status = lpsched_alloc_files(svc, 1, &request_id);
11080Sstevel@tonic-gate 	if (status != PAPI_OK)
11090Sstevel@tonic-gate 		return (status);
11100Sstevel@tonic-gate 
11110Sstevel@tonic-gate 	/* convert the attributes to an lpsched REQUEST structure */
11120Sstevel@tonic-gate 	request = create_request(svc, (char *)printer,
11138569SJonathan.Ca@Sun.COM 	    (papi_attribute_t **)job_attributes);
11142264Sjacobs 	if (request == NULL)
11152264Sjacobs 		return (PAPI_TEMPORARY_ERROR);
11160Sstevel@tonic-gate 	addlist(&request->file_list, DUMMY_FILE);	/* add a dummy file */
11170Sstevel@tonic-gate 	request->actions |= ACT_HOLD;			/* hold the job */
11180Sstevel@tonic-gate 
11190Sstevel@tonic-gate #ifdef LP_USE_PAPI_ATTR
11200Sstevel@tonic-gate 	/*
11210Sstevel@tonic-gate 	 * store the job attributes in the PAPI job attribute file that was
11220Sstevel@tonic-gate 	 * created by lpsched_alloc_files(), the attributes will then pass
11230Sstevel@tonic-gate 	 * through lpsched and be given to the slow-filters and the printer's
11240Sstevel@tonic-gate 	 * interface script to process them
11250Sstevel@tonic-gate 	 */
11260Sstevel@tonic-gate 	snprintf(metadata_file, sizeof (metadata_file), "%s%s-%s",
11278569SJonathan.Ca@Sun.COM 	    "/var/spool/lp/temp/", request_id, LP_PAPIATTRNAME);
11280Sstevel@tonic-gate 	status = psm_copy_attrsToFile(job_attributes, metadata_file);
11290Sstevel@tonic-gate 	if (status != PAPI_OK) {
11300Sstevel@tonic-gate 		detailed_error(svc, "unable to copy attributes to file: %s: %s",
11318569SJonathan.Ca@Sun.COM 		    metadata_file, strerror(errno));
11322264Sjacobs 		free(request_id);
11330Sstevel@tonic-gate 		return (PAPI_DEVICE_ERROR);
11340Sstevel@tonic-gate 	}
11350Sstevel@tonic-gate #endif
11360Sstevel@tonic-gate 
11370Sstevel@tonic-gate 	/* store the REQUEST on disk */
11380Sstevel@tonic-gate 	snprintf(metadata_file, sizeof (metadata_file), "%s-0", request_id);
11392264Sjacobs 	free(request_id);
11400Sstevel@tonic-gate 	if (putrequest(metadata_file, request) < 0) {
11410Sstevel@tonic-gate 		detailed_error(svc, gettext("unable to save request: %s: %s"),
11428569SJonathan.Ca@Sun.COM 		    metadata_file, strerror(errno));
11430Sstevel@tonic-gate 		return (PAPI_DEVICE_ERROR);
11440Sstevel@tonic-gate 	}
11450Sstevel@tonic-gate 
11460Sstevel@tonic-gate 	status = lpsched_commit_job(svc, metadata_file, &tmp);
11470Sstevel@tonic-gate 	if (status != PAPI_OK) {
11480Sstevel@tonic-gate 		unlink(metadata_file);
11490Sstevel@tonic-gate 		return (status);
11500Sstevel@tonic-gate 	}
11510Sstevel@tonic-gate 
11520Sstevel@tonic-gate 	lpsched_request_to_job_attributes(request, j);
11530Sstevel@tonic-gate 
11540Sstevel@tonic-gate 	if ((c = strrchr(tmp, '-')) != NULL)
11550Sstevel@tonic-gate 		c++;
11560Sstevel@tonic-gate 	papiAttributeListAddInteger(&j->attributes, PAPI_ATTR_REPLACE,
11578569SJonathan.Ca@Sun.COM 	    "job-id", atoi(c));
11580Sstevel@tonic-gate 	papiAttributeListAddString(&j->attributes, PAPI_ATTR_REPLACE,
11598569SJonathan.Ca@Sun.COM 	    "job-uri", tmp);
11600Sstevel@tonic-gate 
11610Sstevel@tonic-gate 	return (PAPI_OK);
11620Sstevel@tonic-gate }
11630Sstevel@tonic-gate 
11640Sstevel@tonic-gate papi_status_t
11650Sstevel@tonic-gate papiJobCommit(papi_service_t handle, char *printer, int32_t id)
11660Sstevel@tonic-gate {
11670Sstevel@tonic-gate 	papi_status_t status = PAPI_OK;
11680Sstevel@tonic-gate 	service_t *svc = handle;
11690Sstevel@tonic-gate 	REQUEST *r = NULL;
11700Sstevel@tonic-gate 	char *metadata_file;
11710Sstevel@tonic-gate 	char *dest;
11720Sstevel@tonic-gate 
11730Sstevel@tonic-gate 	if ((svc == NULL) || (printer == NULL))
11740Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
11750Sstevel@tonic-gate 
11760Sstevel@tonic-gate 	dest = printer_name_from_uri_id(printer, id);
11770Sstevel@tonic-gate 	/* tell the scheduler that we want to change the job */
11780Sstevel@tonic-gate 	status = lpsched_start_change(svc, dest, id, &metadata_file);
11790Sstevel@tonic-gate 	if (status != PAPI_OK)
11800Sstevel@tonic-gate 		return (status);
11810Sstevel@tonic-gate 
11820Sstevel@tonic-gate 	if ((r = getrequest(metadata_file)) != NULL) {
11830Sstevel@tonic-gate 		r->actions &= ~ACT_RESUME;
11840Sstevel@tonic-gate 		r->actions |= ACT_RESUME;
11850Sstevel@tonic-gate 		dellist(&r->file_list, DUMMY_FILE);
11860Sstevel@tonic-gate 
11870Sstevel@tonic-gate 		if (putrequest(metadata_file, r) < 0) {
11880Sstevel@tonic-gate 			detailed_error(svc,
11898569SJonathan.Ca@Sun.COM 			    gettext("failed to write job: %s: %s"),
11908569SJonathan.Ca@Sun.COM 			    metadata_file, strerror(errno));
11912264Sjacobs 			freerequest(r);
11920Sstevel@tonic-gate 			return (PAPI_DEVICE_ERROR);
11930Sstevel@tonic-gate 		}
11940Sstevel@tonic-gate 	} else {
11950Sstevel@tonic-gate 		detailed_error(svc, gettext("failed to read job: %s: %s"),
11968569SJonathan.Ca@Sun.COM 		    metadata_file, strerror(errno));
11970Sstevel@tonic-gate 		return (PAPI_DEVICE_ERROR);
11980Sstevel@tonic-gate 	}
11990Sstevel@tonic-gate 
12000Sstevel@tonic-gate 	status = lpsched_end_change(svc, dest, id);
12010Sstevel@tonic-gate 	freerequest(r);
12020Sstevel@tonic-gate 
12030Sstevel@tonic-gate 	return (status);
12040Sstevel@tonic-gate }
12050Sstevel@tonic-gate 
12060Sstevel@tonic-gate papi_status_t
12070Sstevel@tonic-gate papiJobStreamAdd(papi_service_t handle, char *printer, int32_t id,
12080Sstevel@tonic-gate 		papi_stream_t *stream)
12090Sstevel@tonic-gate {
12100Sstevel@tonic-gate 	papi_status_t status;
12110Sstevel@tonic-gate 	service_t *svc = handle;
12120Sstevel@tonic-gate 	job_stream_t *s = NULL;
12130Sstevel@tonic-gate 	char *metadata_file = NULL;
12140Sstevel@tonic-gate 	char *dest;
12150Sstevel@tonic-gate 	char path[MAXPATHLEN];
12160Sstevel@tonic-gate 
12170Sstevel@tonic-gate 	/* allocate space for the stream */
12180Sstevel@tonic-gate 	if ((*stream = s = calloc(1, sizeof (*s))) == NULL)
12190Sstevel@tonic-gate 		return (PAPI_TEMPORARY_ERROR);
12200Sstevel@tonic-gate 
12210Sstevel@tonic-gate 	dest = printer_name_from_uri_id(printer, id);
12220Sstevel@tonic-gate 	/* create/open data file (only root or lp can really do this */
12230Sstevel@tonic-gate 	snprintf(path, sizeof (path), "/var/spool/lp/temp/%d-XXXXXX", id);
12240Sstevel@tonic-gate 	if ((s->fd = mkstemp(path)) < 0) {
12250Sstevel@tonic-gate 		detailed_error(svc, gettext("unable to create sink (%s): %s"),
12268569SJonathan.Ca@Sun.COM 		    path, strerror(errno));
12270Sstevel@tonic-gate 		free(s);
12280Sstevel@tonic-gate 		return (PAPI_NOT_AUTHORIZED);
12290Sstevel@tonic-gate 	}
12300Sstevel@tonic-gate 
12310Sstevel@tonic-gate 	/* add data file to job */
12320Sstevel@tonic-gate 	status = lpsched_start_change(svc, dest, id, &metadata_file);
12330Sstevel@tonic-gate 	if (status != PAPI_OK) {
12340Sstevel@tonic-gate 		close(s->fd);
12350Sstevel@tonic-gate 		free(s);
12360Sstevel@tonic-gate 		unlink(path);
12370Sstevel@tonic-gate 		return (status);
12380Sstevel@tonic-gate 	}
12390Sstevel@tonic-gate 
12400Sstevel@tonic-gate 	if ((s->request = getrequest(metadata_file)) == NULL) {
12410Sstevel@tonic-gate 		detailed_error(svc, gettext("unable to load request: %s: %s"),
12428569SJonathan.Ca@Sun.COM 		    metadata_file, strerror(errno));
12430Sstevel@tonic-gate 		close(s->fd);
12440Sstevel@tonic-gate 		free(s);
12450Sstevel@tonic-gate 		unlink(path);
12460Sstevel@tonic-gate 		return (PAPI_NOT_POSSIBLE);
12470Sstevel@tonic-gate 	}
12480Sstevel@tonic-gate 
12490Sstevel@tonic-gate 	addlist(&(s->request->file_list), path);
12500Sstevel@tonic-gate 
12510Sstevel@tonic-gate 	if (putrequest(metadata_file, s->request) < 0) {
12520Sstevel@tonic-gate 		detailed_error(svc, gettext("unable to save request: %s: %s"),
12538569SJonathan.Ca@Sun.COM 		    metadata_file, strerror(errno));
12540Sstevel@tonic-gate 		close(s->fd);
12550Sstevel@tonic-gate 		free(s);
12560Sstevel@tonic-gate 		unlink(path);
12570Sstevel@tonic-gate 		return (PAPI_NOT_POSSIBLE);
12580Sstevel@tonic-gate 	}
12590Sstevel@tonic-gate 
12600Sstevel@tonic-gate 	status = lpsched_end_change(svc, dest, id);
12610Sstevel@tonic-gate 
12620Sstevel@tonic-gate 	if (status != PAPI_OK)
12630Sstevel@tonic-gate 		return (status);
12640Sstevel@tonic-gate 
12650Sstevel@tonic-gate 	return (PAPI_OK);
12660Sstevel@tonic-gate }
1267