xref: /onnv-gate/usr/src/cmd/lp/lib/papi/job.c (revision 9224)
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) {
1219152SSonam.Gupta@Sun.COM 		char *hostname = NULL;
1229152SSonam.Gupta@Sun.COM 
1237253Sjacobs 		r->priority = -1;
1247253Sjacobs 		r->destination = printer_name_from_uri_id(printer, -1);
1259152SSonam.Gupta@Sun.COM 
1269152SSonam.Gupta@Sun.COM 		papiAttributeListGetString(attributes, NULL,
1279152SSonam.Gupta@Sun.COM 		    "job-originating-host-name", &hostname);
1289152SSonam.Gupta@Sun.COM 
1299152SSonam.Gupta@Sun.COM 		if (hostname == NULL) {
1309152SSonam.Gupta@Sun.COM 			char host[BUFSIZ];
1319152SSonam.Gupta@Sun.COM 
1329152SSonam.Gupta@Sun.COM 			if (gethostname(host, sizeof (host)) == 0)
1339152SSonam.Gupta@Sun.COM 				papiAttributeListAddString(&attributes,
1349152SSonam.Gupta@Sun.COM 				    PAPI_ATTR_REPLACE,
1359152SSonam.Gupta@Sun.COM 				    "job-originating-host-name",
1369152SSonam.Gupta@Sun.COM 				    host);
1379152SSonam.Gupta@Sun.COM 		}
1389152SSonam.Gupta@Sun.COM 
1397253Sjacobs 		job_attributes_to_lpsched_request(svc, r, attributes);
1407253Sjacobs 	}
1410Sstevel@tonic-gate 
1427253Sjacobs 	return (r);
1430Sstevel@tonic-gate }
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate static papi_status_t
1460Sstevel@tonic-gate authorized(service_t *svc, int32_t id)
1470Sstevel@tonic-gate {
1480Sstevel@tonic-gate 	papi_status_t result = PAPI_NOT_AUTHORIZED;	/* assume the worst */
1490Sstevel@tonic-gate 	char file[32];
1500Sstevel@tonic-gate 	REQUEST *r;
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate 	snprintf(file, sizeof (file), "%d-0", id);
1530Sstevel@tonic-gate 	if ((r = getrequest(file)) != NULL) {
1540Sstevel@tonic-gate 		uid_t uid = getuid();
1550Sstevel@tonic-gate 		struct passwd *pw = NULL;
1560Sstevel@tonic-gate 		char *user = "intruder";	/* assume an intruder */
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate 		if ((pw = getpwuid(uid)) != NULL)
1590Sstevel@tonic-gate 			user = pw->pw_name;	/* use the process owner */
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate 		if ((uid == 0) || (uid == 71)) { /* root/lp can forge this */
1620Sstevel@tonic-gate 			papi_status_t s;
1630Sstevel@tonic-gate 			s = papiAttributeListGetString(svc->attributes, NULL,
1648569SJonathan.Ca@Sun.COM 			    "user-name", &user);
1650Sstevel@tonic-gate 			if (s != PAPI_OK)	/* true root/lp are almighty */
1660Sstevel@tonic-gate 				result = PAPI_OK;
1670Sstevel@tonic-gate 		}
1680Sstevel@tonic-gate 
1698966SSonam.Gupta@Sun.COM 		if (result != PAPI_OK) {
1708966SSonam.Gupta@Sun.COM 			if (strcmp(user, r->user) == 0)
1718966SSonam.Gupta@Sun.COM 				result = PAPI_OK;
1728966SSonam.Gupta@Sun.COM 			else {
1738966SSonam.Gupta@Sun.COM 				/*
1748966SSonam.Gupta@Sun.COM 				 * user request r->user might contain the
1758966SSonam.Gupta@Sun.COM 				 * host info also
1768966SSonam.Gupta@Sun.COM 				 */
1778966SSonam.Gupta@Sun.COM 				char *token;
1788966SSonam.Gupta@Sun.COM 				token = strtok(r->user, "@");
1798966SSonam.Gupta@Sun.COM 
1808966SSonam.Gupta@Sun.COM 				if (token != NULL) {
1818966SSonam.Gupta@Sun.COM 					if (strcmp(user, token) == 0)
1828966SSonam.Gupta@Sun.COM 						result = PAPI_OK;
1838966SSonam.Gupta@Sun.COM 					free(token);
1848966SSonam.Gupta@Sun.COM 				}
1858966SSonam.Gupta@Sun.COM 			}
1868966SSonam.Gupta@Sun.COM 		}
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate 		freerequest(r);
1890Sstevel@tonic-gate 	} else
1900Sstevel@tonic-gate 		result = PAPI_NOT_FOUND;
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate 	return (result);
1930Sstevel@tonic-gate }
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate static papi_status_t
1962264Sjacobs copy_file(char *from, char *to)
1970Sstevel@tonic-gate {
1980Sstevel@tonic-gate 	int ifd, ofd;
1990Sstevel@tonic-gate 	char buf[BUFSIZ];
2000Sstevel@tonic-gate 	int rc;
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate 	if ((ifd = open(from, O_RDONLY)) < 0)
2030Sstevel@tonic-gate 		return (PAPI_DOCUMENT_ACCESS_ERROR);
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate 	if ((ofd = open(to, O_WRONLY)) < 0) {
2060Sstevel@tonic-gate 		close(ifd);
2070Sstevel@tonic-gate 		return (PAPI_NOT_POSSIBLE);
2080Sstevel@tonic-gate 	}
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate 	while ((rc = read(ifd, buf, sizeof (buf))) > 0)
2110Sstevel@tonic-gate 		write(ofd, buf, rc);
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate 	close(ifd);
2140Sstevel@tonic-gate 	close(ofd);
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate 	return (PAPI_OK);
2170Sstevel@tonic-gate }
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate #ifdef LP_USE_PAPI_ATTR
2210Sstevel@tonic-gate /*
2220Sstevel@tonic-gate  * *****************************************************************************
2230Sstevel@tonic-gate  *
2240Sstevel@tonic-gate  * Description: Create a file containing all the attributes in the attribute
2250Sstevel@tonic-gate  *              list passed to this function.
2260Sstevel@tonic-gate  *              This file is then passed through lpsched and given to either
2270Sstevel@tonic-gate  *              a slow-filter or to the printer's interface script to process
2280Sstevel@tonic-gate  *              the attributes.
2290Sstevel@tonic-gate  *
2300Sstevel@tonic-gate  * Parameters:  attrs - list of attributes and their values
2310Sstevel@tonic-gate  *              file  - file pathname to create and put the attributes into.
2320Sstevel@tonic-gate  *
2330Sstevel@tonic-gate  * *****************************************************************************
2340Sstevel@tonic-gate  */
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate static papi_status_t
2372264Sjacobs psm_copy_attrsToFile(papi_attribute_t **attrs, char *file)
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate {
2400Sstevel@tonic-gate 	papi_status_t result = PAPI_OK;
2412264Sjacobs 
2422264Sjacobs 	if ((attrs != NULL) && (*attrs != NULL)) {
2432264Sjacobs 		FILE *out = NULL;
2440Sstevel@tonic-gate 
2452264Sjacobs 		if ((out = fopen(file, "w")) != NULL) {
2462264Sjacobs 			papiAttributeListPrint(out, attrs, "");
2470Sstevel@tonic-gate 			fclose(out);
2482264Sjacobs 		} else {
2490Sstevel@tonic-gate 			result = PAPI_NOT_POSSIBLE;
2500Sstevel@tonic-gate 		}
2510Sstevel@tonic-gate 	}
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate 	return (result);
2540Sstevel@tonic-gate } /* psm_copy_attrsToFile */
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate /*
2580Sstevel@tonic-gate  * *****************************************************************************
2590Sstevel@tonic-gate  *
2600Sstevel@tonic-gate  * Description: Modify the given attribute 'file' with the attributes from the
2610Sstevel@tonic-gate  *              'attrs' list. Attributes already in the file will be replaced
2620Sstevel@tonic-gate  *              with the new value. New attributes will be added into the file.
2630Sstevel@tonic-gate  *
2640Sstevel@tonic-gate  * Parameters:  attrs - list of attributes and their values
2650Sstevel@tonic-gate  *              file  - file pathname to create and put the attributes into.
2660Sstevel@tonic-gate  *
2670Sstevel@tonic-gate  * *****************************************************************************
2680Sstevel@tonic-gate  */
2690Sstevel@tonic-gate 
2700Sstevel@tonic-gate static papi_status_t
2712264Sjacobs psm_modifyAttrsFile(papi_attribute_t **attrs, char *file)
2720Sstevel@tonic-gate 
2730Sstevel@tonic-gate {
2740Sstevel@tonic-gate 	papi_status_t result = PAPI_OK;
2750Sstevel@tonic-gate 	papi_attribute_t **newAttrs = NULL;
2760Sstevel@tonic-gate 	struct stat   tmpBuf;
2770Sstevel@tonic-gate 	FILE *fd = NULL;
2780Sstevel@tonic-gate 
2792264Sjacobs 	if ((attrs != NULL) && (*attrs != NULL) && (file != NULL)) {
2800Sstevel@tonic-gate 
2810Sstevel@tonic-gate 		/*
2820Sstevel@tonic-gate 		 * check file exist before try to modify it, if it doesn't
2830Sstevel@tonic-gate 		 * exist assume there is an error
2840Sstevel@tonic-gate 		 */
2852264Sjacobs 		if (stat(file, &tmpBuf) == 0) {
2860Sstevel@tonic-gate 			/*
2870Sstevel@tonic-gate 			 * if file is currently empty just write the given
2880Sstevel@tonic-gate 			 * attributes to the file otherwise exact the attributes
2890Sstevel@tonic-gate 			 * from the file and modify them accordingly before
2900Sstevel@tonic-gate 			 * writing them back to the file
2910Sstevel@tonic-gate 			 */
2922264Sjacobs 			if (tmpBuf.st_size == 0) {
2930Sstevel@tonic-gate 				newAttrs = (papi_attribute_t **)attrs;
2940Sstevel@tonic-gate 
2950Sstevel@tonic-gate 				fd = fopen(file, "w");
2962264Sjacobs 				if (fd != NULL) {
2970Sstevel@tonic-gate 					papiAttributeListPrint(fd,
2982264Sjacobs 							newAttrs, "");
2990Sstevel@tonic-gate 					fclose(fd);
3002264Sjacobs 				} else {
3010Sstevel@tonic-gate 					result = PAPI_NOT_POSSIBLE;
3020Sstevel@tonic-gate 				}
3032264Sjacobs 			} else {
3040Sstevel@tonic-gate 				result =
3050Sstevel@tonic-gate 				    psm_modifyAttrsList(file, attrs, &newAttrs);
3060Sstevel@tonic-gate 
3070Sstevel@tonic-gate 				fd = fopen(file, "w");
3082264Sjacobs 				if (fd != NULL) {
3090Sstevel@tonic-gate 					papiAttributeListPrint(fd,
3102264Sjacobs 								newAttrs, "");
3110Sstevel@tonic-gate 					fclose(fd);
3122264Sjacobs 				} else {
3130Sstevel@tonic-gate 					result = PAPI_NOT_POSSIBLE;
3140Sstevel@tonic-gate 				}
3150Sstevel@tonic-gate 
3160Sstevel@tonic-gate 				papiAttributeListFree(newAttrs);
3170Sstevel@tonic-gate 			}
3182264Sjacobs 		} else {
3190Sstevel@tonic-gate 			result = PAPI_NOT_POSSIBLE;
3200Sstevel@tonic-gate 		}
3210Sstevel@tonic-gate 	}
3220Sstevel@tonic-gate 
3230Sstevel@tonic-gate 	return (result);
3240Sstevel@tonic-gate } /* psm_modifyAttrsFile */
3250Sstevel@tonic-gate 
3260Sstevel@tonic-gate 
3270Sstevel@tonic-gate /*
3280Sstevel@tonic-gate  * *****************************************************************************
3290Sstevel@tonic-gate  *
3300Sstevel@tonic-gate  * Description: Extracts the attributes in the given attribute 'file' and
3310Sstevel@tonic-gate  *              creates a new list 'newAttrs' containing the modified list of
3320Sstevel@tonic-gate  *              attributes.
3330Sstevel@tonic-gate  *
3340Sstevel@tonic-gate  * Parameters:  file  - pathname of file containing attributes to be modified
3350Sstevel@tonic-gate  *              attrs - list of attributes and their values to modify
3360Sstevel@tonic-gate  *              newAttrs - returns the modified list of attributes
3370Sstevel@tonic-gate  *
3380Sstevel@tonic-gate  * *****************************************************************************
3390Sstevel@tonic-gate  */
3400Sstevel@tonic-gate 
3410Sstevel@tonic-gate static papi_status_t
3422264Sjacobs psm_modifyAttrsList(char *file, papi_attribute_t **attrs,
3430Sstevel@tonic-gate     papi_attribute_t ***newAttrs)
3440Sstevel@tonic-gate 
3450Sstevel@tonic-gate {
3460Sstevel@tonic-gate 	papi_status_t result = PAPI_OK;
3470Sstevel@tonic-gate 	papi_attribute_t  *nextAttr = NULL;
3480Sstevel@tonic-gate 	papi_attribute_value_t  **values = NULL;
3490Sstevel@tonic-gate 	void *iter = NULL;
3500Sstevel@tonic-gate 	FILE *fd = NULL;
3510Sstevel@tonic-gate 	register int fD = 0;
3520Sstevel@tonic-gate 	char aBuff[200];
3530Sstevel@tonic-gate 	char *a = NULL;
3540Sstevel@tonic-gate 	char *p = NULL;
3550Sstevel@tonic-gate 	int count = 0;
3560Sstevel@tonic-gate 	int n = 0;
3570Sstevel@tonic-gate 
3580Sstevel@tonic-gate 	fd = fopen(file, "r");
3592264Sjacobs 	if (fd != NULL) {
3600Sstevel@tonic-gate 		fD = fileno(fd);
3610Sstevel@tonic-gate 		a = &aBuff[0];
3620Sstevel@tonic-gate 		p = &aBuff[0];
3630Sstevel@tonic-gate 		count = read(fD, &aBuff[0], sizeof (aBuff) - 1);
3642264Sjacobs 		while ((result == PAPI_OK) && (count > 0)) {
3650Sstevel@tonic-gate 			aBuff[count+n] = '\0';
3662264Sjacobs 			if (count == sizeof (aBuff) - n - 1) {
3670Sstevel@tonic-gate 				p = strrchr(aBuff, '\n');
3682264Sjacobs 				if (p != NULL) {
3690Sstevel@tonic-gate 					/* terminate at last complete line */
3700Sstevel@tonic-gate 					*p = '\0';
3710Sstevel@tonic-gate 				}
3720Sstevel@tonic-gate 			}
3730Sstevel@tonic-gate 			result = papiAttributeListFromString(
3740Sstevel@tonic-gate 				newAttrs, PAPI_ATTR_EXCL, aBuff);
3750Sstevel@tonic-gate 
3762264Sjacobs 			if (result == PAPI_OK) {
3770Sstevel@tonic-gate 				/*
3780Sstevel@tonic-gate 				 * handle any part lines and then read the next
3790Sstevel@tonic-gate 				 * buffer from the file
3800Sstevel@tonic-gate 				 */
3810Sstevel@tonic-gate 				n = 0;
3822264Sjacobs 				if (p != a) {
3830Sstevel@tonic-gate 					p++; /* skip NL */
3840Sstevel@tonic-gate 					n = sizeof (aBuff) - 1 - (p - a);
3850Sstevel@tonic-gate 					strncpy(aBuff, p, n);
3860Sstevel@tonic-gate 				}
3870Sstevel@tonic-gate 				count = read(fD, &aBuff[n],
3880Sstevel@tonic-gate 					sizeof (aBuff) - n - 1);
3890Sstevel@tonic-gate 				p = &aBuff[0];
3900Sstevel@tonic-gate 			}
3910Sstevel@tonic-gate 		}
3920Sstevel@tonic-gate 		fclose(fd);
3930Sstevel@tonic-gate 	}
3940Sstevel@tonic-gate 
3950Sstevel@tonic-gate 	/* now modify the attribute list with the new attributes in 'attrs' */
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate 	nextAttr = papiAttributeListGetNext((papi_attribute_t **)attrs, &iter);
3982264Sjacobs 	while ((result == PAPI_OK) && (nextAttr != NULL)) {
3990Sstevel@tonic-gate 		values = nextAttr->values;
4000Sstevel@tonic-gate 
4012264Sjacobs 		if ((values != NULL) && (*values != NULL)) {
4022264Sjacobs 			result = papiAttributeListAddValue(newAttrs,
4030Sstevel@tonic-gate 						    PAPI_ATTR_REPLACE,
4040Sstevel@tonic-gate 						    nextAttr->name,
4050Sstevel@tonic-gate 						    nextAttr->type, *values);
4060Sstevel@tonic-gate 			values++;
4070Sstevel@tonic-gate 		}
4080Sstevel@tonic-gate 
4090Sstevel@tonic-gate 		while ((result == PAPI_OK) &&
4102264Sjacobs 			(values != NULL) && (*values != NULL)) {
4112264Sjacobs 			result = papiAttributeListAddValue(newAttrs,
4120Sstevel@tonic-gate 						    PAPI_ATTR_APPEND,
4130Sstevel@tonic-gate 						    nextAttr->name,
4140Sstevel@tonic-gate 						    nextAttr->type, *values);
4150Sstevel@tonic-gate 			values++;
4160Sstevel@tonic-gate 		}
4170Sstevel@tonic-gate 		nextAttr =
4180Sstevel@tonic-gate 		    papiAttributeListGetNext((papi_attribute_t **)attrs, &iter);
4190Sstevel@tonic-gate 	}
4200Sstevel@tonic-gate 
4210Sstevel@tonic-gate 	return (result);
4220Sstevel@tonic-gate } /* papi_modifyAttrsList() */
4230Sstevel@tonic-gate #endif
4240Sstevel@tonic-gate 
4250Sstevel@tonic-gate 
4260Sstevel@tonic-gate papi_status_t
4272264Sjacobs papiJobSubmit(papi_service_t handle, char *printer,
4282264Sjacobs 		papi_attribute_t **job_attributes,
4292264Sjacobs 		papi_job_ticket_t *job_ticket,
4302264Sjacobs 		char **files, papi_job_t *job)
4310Sstevel@tonic-gate {
4320Sstevel@tonic-gate 	papi_status_t status;
4330Sstevel@tonic-gate 	service_t *svc = handle;
4348569SJonathan.Ca@Sun.COM 	struct stat statbuf;
4350Sstevel@tonic-gate 	job_t *j;
4360Sstevel@tonic-gate 	int file_no;
4370Sstevel@tonic-gate 	char *request_id = NULL;
4380Sstevel@tonic-gate 	REQUEST *request;
4390Sstevel@tonic-gate 	int i;
4400Sstevel@tonic-gate 	char *c;
4410Sstevel@tonic-gate 	char *tmp = NULL;
4420Sstevel@tonic-gate 	char lpfile[BUFSIZ];
4430Sstevel@tonic-gate 
4440Sstevel@tonic-gate 	if ((svc == NULL) || (printer == NULL) || (files == NULL) ||
4450Sstevel@tonic-gate 	    (job == NULL))
4460Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
4470Sstevel@tonic-gate 
4480Sstevel@tonic-gate 	if (job_ticket != NULL)
4490Sstevel@tonic-gate 		return (PAPI_OPERATION_NOT_SUPPORTED);
4500Sstevel@tonic-gate 
4510Sstevel@tonic-gate 	if (files != NULL)
4528569SJonathan.Ca@Sun.COM 		for (file_no = 0; files[file_no] != NULL; file_no++) {
4530Sstevel@tonic-gate 			if (access(files[file_no], R_OK) < 0) {
4540Sstevel@tonic-gate 				detailed_error(svc,
4558569SJonathan.Ca@Sun.COM 				    gettext("Cannot access file: %s: %s"),
4568569SJonathan.Ca@Sun.COM 				    files[file_no], strerror(errno));
4570Sstevel@tonic-gate 				return (PAPI_BAD_ARGUMENT);
4580Sstevel@tonic-gate 			}
459*9224SJonathan.Ca@Sun.COM 			if (stat(files[file_no], &statbuf) < 0) {
460*9224SJonathan.Ca@Sun.COM 				detailed_error(svc,
461*9224SJonathan.Ca@Sun.COM 				    gettext("Cannot access file: %s: %s"),
462*9224SJonathan.Ca@Sun.COM 				    files[file_no], strerror(errno));
463*9224SJonathan.Ca@Sun.COM 				return (PAPI_DOCUMENT_ACCESS_ERROR);
464*9224SJonathan.Ca@Sun.COM 			}
4658569SJonathan.Ca@Sun.COM 			if (statbuf.st_size == 0) {
4668569SJonathan.Ca@Sun.COM 				detailed_error(svc,
4678569SJonathan.Ca@Sun.COM 				    gettext("Zero byte (empty) file: %s"),
4688569SJonathan.Ca@Sun.COM 				    files[file_no]);
4698569SJonathan.Ca@Sun.COM 				return (PAPI_BAD_ARGUMENT);
4708569SJonathan.Ca@Sun.COM 			}
4718569SJonathan.Ca@Sun.COM 		}
4720Sstevel@tonic-gate 
4730Sstevel@tonic-gate 	if ((*job = j = calloc(1, sizeof (*j))) == NULL)
4740Sstevel@tonic-gate 		return (PAPI_TEMPORARY_ERROR);
4750Sstevel@tonic-gate 
4760Sstevel@tonic-gate 	/* file_no + 1 for the control file (-0) */
4770Sstevel@tonic-gate 	status = lpsched_alloc_files(svc, file_no + 1, &request_id);
4780Sstevel@tonic-gate 	if (status != PAPI_OK)
4790Sstevel@tonic-gate 		return (status);
4800Sstevel@tonic-gate 
4810Sstevel@tonic-gate 	request = create_request(svc, (char *)printer,
4828569SJonathan.Ca@Sun.COM 	    (papi_attribute_t **)job_attributes);
4830Sstevel@tonic-gate 
4840Sstevel@tonic-gate 	for (i = 0; files[i] != NULL; i++) {
4850Sstevel@tonic-gate 		papi_status_t status;
4860Sstevel@tonic-gate 		snprintf(lpfile, sizeof (lpfile), "%s%s-%d",
4878569SJonathan.Ca@Sun.COM 		    "/var/spool/lp/temp/", request_id, i+1);
4880Sstevel@tonic-gate 		status = copy_file(files[i], lpfile);
4890Sstevel@tonic-gate 		if (status != PAPI_OK) {
4900Sstevel@tonic-gate 			detailed_error(svc,
4918569SJonathan.Ca@Sun.COM 			    gettext("unable to copy: %s -> %s: %s"),
4928569SJonathan.Ca@Sun.COM 			    files[i], lpfile, strerror(errno));
4930Sstevel@tonic-gate 				freerequest(request);
4940Sstevel@tonic-gate 			return (PAPI_DEVICE_ERROR);
4950Sstevel@tonic-gate 		}
4960Sstevel@tonic-gate 		addlist(&(request->file_list), lpfile);
4970Sstevel@tonic-gate 	}
4980Sstevel@tonic-gate 
4990Sstevel@tonic-gate #ifdef LP_USE_PAPI_ATTR
5000Sstevel@tonic-gate 	/*
5010Sstevel@tonic-gate 	 * store the job attributes in the PAPI job attribute file that was
5020Sstevel@tonic-gate 	 * created by lpsched_alloc_files(), the attributes will then pass
5030Sstevel@tonic-gate 	 * through lpsched and be given to the slow-filters and the printer's
5040Sstevel@tonic-gate 	 * interface script to process them
5050Sstevel@tonic-gate 	 */
5060Sstevel@tonic-gate 	snprintf(lpfile, sizeof (lpfile), "%s%s-%s",
5078569SJonathan.Ca@Sun.COM 	    "/var/spool/lp/temp/", request_id, LP_PAPIATTRNAME);
5080Sstevel@tonic-gate 	status = psm_copy_attrsToFile(job_attributes, lpfile);
5090Sstevel@tonic-gate 	if (status != PAPI_OK) {
5100Sstevel@tonic-gate 		detailed_error(svc, "unable to copy attributes to file: %s: %s",
5118569SJonathan.Ca@Sun.COM 		    lpfile, strerror(errno));
5120Sstevel@tonic-gate 		return (PAPI_DEVICE_ERROR);
5130Sstevel@tonic-gate 	}
5140Sstevel@tonic-gate #endif
5150Sstevel@tonic-gate 
5160Sstevel@tonic-gate 	/* store the meta-data file */
5170Sstevel@tonic-gate 	snprintf(lpfile, sizeof (lpfile), "%s-0", request_id);
5180Sstevel@tonic-gate 	if (putrequest(lpfile, request) < 0) {
5190Sstevel@tonic-gate 		detailed_error(svc, gettext("unable to save request: %s: %s"),
5208569SJonathan.Ca@Sun.COM 		    lpfile, strerror(errno));
5210Sstevel@tonic-gate 		freerequest(request);
5220Sstevel@tonic-gate 		return (PAPI_DEVICE_ERROR);
5230Sstevel@tonic-gate 	}
5240Sstevel@tonic-gate 
5250Sstevel@tonic-gate 	status = lpsched_commit_job(svc, lpfile, &tmp);
5260Sstevel@tonic-gate 	if (status != PAPI_OK) {
5270Sstevel@tonic-gate 		unlink(lpfile);
5280Sstevel@tonic-gate 		freerequest(request);
5290Sstevel@tonic-gate 		return (status);
5300Sstevel@tonic-gate 	}
5310Sstevel@tonic-gate 
5320Sstevel@tonic-gate 	lpsched_request_to_job_attributes(request, j);
5330Sstevel@tonic-gate 	freerequest(request);
5340Sstevel@tonic-gate 
5350Sstevel@tonic-gate 	if ((c = strrchr(tmp, '-')) != NULL)
5360Sstevel@tonic-gate 		c++;
5370Sstevel@tonic-gate 	papiAttributeListAddInteger(&j->attributes, PAPI_ATTR_REPLACE,
5388569SJonathan.Ca@Sun.COM 	    "job-id", atoi(c));
5390Sstevel@tonic-gate 	papiAttributeListAddString(&j->attributes, PAPI_ATTR_REPLACE,
5408569SJonathan.Ca@Sun.COM 	    "job-uri", tmp);
5410Sstevel@tonic-gate 
5420Sstevel@tonic-gate 	return (PAPI_OK);
5430Sstevel@tonic-gate }
5440Sstevel@tonic-gate 
5450Sstevel@tonic-gate papi_status_t
5462264Sjacobs papiJobSubmitByReference(papi_service_t handle, char *printer,
5472264Sjacobs 		papi_attribute_t **job_attributes,
5482264Sjacobs 		papi_job_ticket_t *job_ticket,
5492264Sjacobs 		char **files, papi_job_t *job)
5500Sstevel@tonic-gate {
5510Sstevel@tonic-gate 	service_t *svc = handle;
5528569SJonathan.Ca@Sun.COM 	struct stat statbuf;
5530Sstevel@tonic-gate 	job_t *j;
5540Sstevel@tonic-gate 	int file_no;
5550Sstevel@tonic-gate 	short status;
5560Sstevel@tonic-gate 	char *request_id = NULL;
5570Sstevel@tonic-gate 	REQUEST *request;
5580Sstevel@tonic-gate 	char *c;
5590Sstevel@tonic-gate 	char *tmp = NULL;
5600Sstevel@tonic-gate 	char lpfile[BUFSIZ];
5610Sstevel@tonic-gate 	char **file_list = NULL;
5620Sstevel@tonic-gate 
5630Sstevel@tonic-gate 	if ((svc == NULL) || (printer == NULL) || (files == NULL) ||
5640Sstevel@tonic-gate 	    (job == NULL))
5650Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
5660Sstevel@tonic-gate 
5670Sstevel@tonic-gate 	if (job_ticket != NULL)
5680Sstevel@tonic-gate 		return (PAPI_OPERATION_NOT_SUPPORTED);
5690Sstevel@tonic-gate 
5700Sstevel@tonic-gate 	if (files != NULL)
5710Sstevel@tonic-gate 		for (file_no = 0; files[file_no] != NULL; file_no++) {
5720Sstevel@tonic-gate 			if (access(files[file_no], R_OK) < 0) {
5730Sstevel@tonic-gate 				detailed_error(svc,
5748569SJonathan.Ca@Sun.COM 				    gettext("Cannot access file: %s: %s"),
5758569SJonathan.Ca@Sun.COM 				    files[file_no], strerror(errno));
5762660Sjacobs 				return (PAPI_DOCUMENT_ACCESS_ERROR);
5770Sstevel@tonic-gate 			}
578*9224SJonathan.Ca@Sun.COM 			if (stat(files[file_no], &statbuf) < 0) {
579*9224SJonathan.Ca@Sun.COM 				detailed_error(svc,
580*9224SJonathan.Ca@Sun.COM 				    gettext("Cannot access file: %s: %s"),
581*9224SJonathan.Ca@Sun.COM 				    files[file_no], strerror(errno));
582*9224SJonathan.Ca@Sun.COM 				return (PAPI_DOCUMENT_ACCESS_ERROR);
583*9224SJonathan.Ca@Sun.COM 			}
5848569SJonathan.Ca@Sun.COM 			if (statbuf.st_size == 0) {
5858569SJonathan.Ca@Sun.COM 				detailed_error(svc,
5868569SJonathan.Ca@Sun.COM 				    gettext("Zero byte (empty) file: %s"),
5878569SJonathan.Ca@Sun.COM 				    files[file_no]);
5888569SJonathan.Ca@Sun.COM 				return (PAPI_BAD_ARGUMENT);
5898569SJonathan.Ca@Sun.COM 			}
5908569SJonathan.Ca@Sun.COM 
5912660Sjacobs 			if (files[file_no][0] != '/') {
5922660Sjacobs 				char path[MAXPATHLEN];
5932660Sjacobs 
5942660Sjacobs 				if (getcwd(path, sizeof (path)) == NULL) {
5952660Sjacobs 					detailed_error(svc, gettext(
5968569SJonathan.Ca@Sun.COM 					    "getcwd for file: %s: %s"),
5978569SJonathan.Ca@Sun.COM 					    files[file_no],
5988569SJonathan.Ca@Sun.COM 					    strerror(errno));
5992660Sjacobs 					return (PAPI_DOCUMENT_ACCESS_ERROR);
6002660Sjacobs 				}
6012660Sjacobs 				strlcat(path, "/", sizeof (path));
6022660Sjacobs 				if (strlcat(path, files[file_no], sizeof (path))
6038569SJonathan.Ca@Sun.COM 				    >= sizeof (path)) {
6042660Sjacobs 					detailed_error(svc, gettext(
6058569SJonathan.Ca@Sun.COM 					    "pathname too long: %s"),
6068569SJonathan.Ca@Sun.COM 					    files[file_no]);
6072660Sjacobs 					return (PAPI_DOCUMENT_ACCESS_ERROR);
6082660Sjacobs 				}
6092660Sjacobs 				addlist(&file_list, path);
6102660Sjacobs 			} else
6112660Sjacobs 				addlist(&file_list, (char *)files[file_no]);
6120Sstevel@tonic-gate 		}
6130Sstevel@tonic-gate 
6140Sstevel@tonic-gate 	if ((*job = j = calloc(1, sizeof (*j))) == NULL)
6150Sstevel@tonic-gate 		return (PAPI_TEMPORARY_ERROR);
6160Sstevel@tonic-gate 
6170Sstevel@tonic-gate 	/* 1 for the control file (-0) */
6180Sstevel@tonic-gate 	status = lpsched_alloc_files(svc, 1, &request_id);
6190Sstevel@tonic-gate 	if (status != PAPI_OK)
6200Sstevel@tonic-gate 		return (status);
6210Sstevel@tonic-gate 
6220Sstevel@tonic-gate 	request = create_request(svc, (char *)printer,
6238569SJonathan.Ca@Sun.COM 	    (papi_attribute_t **)job_attributes);
6240Sstevel@tonic-gate 	request->file_list = file_list;
6250Sstevel@tonic-gate 
6260Sstevel@tonic-gate #ifdef LP_USE_PAPI_ATTR
6270Sstevel@tonic-gate 	/*
6280Sstevel@tonic-gate 	 * store the job attributes in the PAPI job attribute file that was
6290Sstevel@tonic-gate 	 * created by lpsched_alloc_files(), the attributes will then pass
6300Sstevel@tonic-gate 	 * through lpsched and be given to the slow-filters and the printer's
6310Sstevel@tonic-gate 	 * interface script to process them
6320Sstevel@tonic-gate 	 */
6330Sstevel@tonic-gate 	snprintf(lpfile, sizeof (lpfile), "%s%s-%s",
6348569SJonathan.Ca@Sun.COM 	    "/var/spool/lp/temp/", request_id, LP_PAPIATTRNAME);
6350Sstevel@tonic-gate 	status = psm_copy_attrsToFile(job_attributes, lpfile);
6360Sstevel@tonic-gate 	if (status != PAPI_OK) {
6370Sstevel@tonic-gate 		detailed_error(svc, "unable to copy attributes to file: %s: %s",
6388569SJonathan.Ca@Sun.COM 		    lpfile, strerror(errno));
6390Sstevel@tonic-gate 		return (PAPI_DEVICE_ERROR);
6400Sstevel@tonic-gate 	}
6410Sstevel@tonic-gate #endif
6420Sstevel@tonic-gate 
6430Sstevel@tonic-gate 	/* store the meta-data file */
6440Sstevel@tonic-gate 	snprintf(lpfile, sizeof (lpfile), "%s-0", request_id);
6450Sstevel@tonic-gate 	if (putrequest(lpfile, request) < 0) {
6460Sstevel@tonic-gate 		detailed_error(svc, gettext("unable to save request: %s: %s"),
6478569SJonathan.Ca@Sun.COM 		    lpfile, strerror(errno));
6480Sstevel@tonic-gate 		freerequest(request);
6490Sstevel@tonic-gate 		return (PAPI_DEVICE_ERROR);
6500Sstevel@tonic-gate 	}
6510Sstevel@tonic-gate 
6520Sstevel@tonic-gate 	status = lpsched_commit_job(svc, lpfile, &tmp);
6530Sstevel@tonic-gate 	if (status != PAPI_OK) {
6540Sstevel@tonic-gate 		unlink(lpfile);
6550Sstevel@tonic-gate 		freerequest(request);
6560Sstevel@tonic-gate 		return (status);
6570Sstevel@tonic-gate 	}
6580Sstevel@tonic-gate 
6590Sstevel@tonic-gate 	lpsched_request_to_job_attributes(request, j);
6600Sstevel@tonic-gate 
6610Sstevel@tonic-gate 	freerequest(request);
6620Sstevel@tonic-gate 
6630Sstevel@tonic-gate 	if ((c = strrchr(tmp, '-')) != NULL)
6640Sstevel@tonic-gate 		c++;
6650Sstevel@tonic-gate 	papiAttributeListAddInteger(&j->attributes, PAPI_ATTR_REPLACE,
6668569SJonathan.Ca@Sun.COM 	    "job-id", atoi(c));
6670Sstevel@tonic-gate 	papiAttributeListAddString(&j->attributes, PAPI_ATTR_REPLACE,
6688569SJonathan.Ca@Sun.COM 	    "job-uri", tmp);
6690Sstevel@tonic-gate 
6700Sstevel@tonic-gate 	return (PAPI_OK);
6710Sstevel@tonic-gate }
6720Sstevel@tonic-gate 
6730Sstevel@tonic-gate papi_status_t
6742264Sjacobs papiJobValidate(papi_service_t handle, char *printer,
6752264Sjacobs 		papi_attribute_t **job_attributes,
6762264Sjacobs 		papi_job_ticket_t *job_ticket,
6772264Sjacobs 		char **files, papi_job_t *job)
6780Sstevel@tonic-gate {
6790Sstevel@tonic-gate 	papi_status_t status;
6800Sstevel@tonic-gate 	papi_attribute_t **attributes = NULL;
6810Sstevel@tonic-gate 	int i;
6820Sstevel@tonic-gate 
6830Sstevel@tonic-gate 	papiAttributeListAddString(&attributes, PAPI_ATTR_REPLACE,
6848569SJonathan.Ca@Sun.COM 	    "job-hold-until", "indefinite");
6850Sstevel@tonic-gate 	for (i = 0; job_attributes[i]; i++)
6860Sstevel@tonic-gate 		list_append(&attributes, job_attributes[i]);
6870Sstevel@tonic-gate 
6880Sstevel@tonic-gate 	status = papiJobSubmitByReference(handle, printer,
6898569SJonathan.Ca@Sun.COM 	    (papi_attribute_t **)attributes,
6908569SJonathan.Ca@Sun.COM 	    job_ticket, files, job);
6910Sstevel@tonic-gate 	if (status == PAPI_OK) {
6920Sstevel@tonic-gate 		int id = papiJobGetId(*job);
6930Sstevel@tonic-gate 
6940Sstevel@tonic-gate 		if (id != -1)
6950Sstevel@tonic-gate 			papiJobCancel(handle, printer, id);
6960Sstevel@tonic-gate 	}
6970Sstevel@tonic-gate 
6980Sstevel@tonic-gate 	attributes[1] = NULL;	/* after attr[0], they are in another list */
6990Sstevel@tonic-gate 	papiAttributeListFree(attributes);
7000Sstevel@tonic-gate 
7010Sstevel@tonic-gate 	return (status);
7020Sstevel@tonic-gate }
7030Sstevel@tonic-gate 
7040Sstevel@tonic-gate papi_status_t
7052264Sjacobs papiJobStreamOpen(papi_service_t handle, char *printer,
7062264Sjacobs 		papi_attribute_t **job_attributes,
7072264Sjacobs 		papi_job_ticket_t *job_ticket, papi_stream_t *stream)
7080Sstevel@tonic-gate {
7090Sstevel@tonic-gate 	papi_status_t status;
7100Sstevel@tonic-gate 	service_t *svc = handle;
7110Sstevel@tonic-gate 	job_stream_t *s = NULL;
7120Sstevel@tonic-gate 	char *request_id = NULL;
7130Sstevel@tonic-gate 	char lpfile[BUFSIZ];
7140Sstevel@tonic-gate 
7150Sstevel@tonic-gate 	if ((svc == NULL) || (printer == NULL) || (stream == NULL))
7160Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
7170Sstevel@tonic-gate 
7180Sstevel@tonic-gate 	if (job_ticket != NULL)
7190Sstevel@tonic-gate 		return (PAPI_OPERATION_NOT_SUPPORTED);
7200Sstevel@tonic-gate 
7210Sstevel@tonic-gate 	if ((*stream = s = calloc(1, sizeof (*s))) == NULL)
7220Sstevel@tonic-gate 		return (PAPI_TEMPORARY_ERROR);
7230Sstevel@tonic-gate 
7240Sstevel@tonic-gate 	/* 1 for data, 1 for the meta-data (-0) */
7250Sstevel@tonic-gate 	status = lpsched_alloc_files(svc, 2, &request_id);
7260Sstevel@tonic-gate 	if (status != PAPI_OK)
7270Sstevel@tonic-gate 		return (status);
7280Sstevel@tonic-gate 
7290Sstevel@tonic-gate 	s->request = create_request(svc, (char *)printer,
7308569SJonathan.Ca@Sun.COM 	    (papi_attribute_t **)job_attributes);
7310Sstevel@tonic-gate 	snprintf(lpfile, sizeof (lpfile), "/var/spool/lp/temp/%s-1",
7328569SJonathan.Ca@Sun.COM 	    request_id);
7330Sstevel@tonic-gate 	s->fd = open(lpfile, O_WRONLY);
7340Sstevel@tonic-gate 	addlist(&(s->request->file_list), lpfile);
7350Sstevel@tonic-gate 
7360Sstevel@tonic-gate #ifdef LP_USE_PAPI_ATTR
7370Sstevel@tonic-gate 	/*
7380Sstevel@tonic-gate 	 * store the job attributes in the PAPI job attribute file that was
7390Sstevel@tonic-gate 	 * created by lpsched_alloc_files(), the attributes will then pass
7400Sstevel@tonic-gate 	 * through lpsched and be given to the slow-filters and the printer's
7410Sstevel@tonic-gate 	 * interface script to process them
7420Sstevel@tonic-gate 	 */
7430Sstevel@tonic-gate 	snprintf(lpfile, sizeof (lpfile), "%s%s-%s",
7448569SJonathan.Ca@Sun.COM 	    "/var/spool/lp/temp/", request_id, LP_PAPIATTRNAME);
7450Sstevel@tonic-gate 	status = psm_copy_attrsToFile(job_attributes, lpfile);
7460Sstevel@tonic-gate 	if (status != PAPI_OK) {
7470Sstevel@tonic-gate 		detailed_error(svc, "unable to copy attributes to file: %s: %s",
7488569SJonathan.Ca@Sun.COM 		    lpfile, strerror(errno));
7490Sstevel@tonic-gate 		close(s->fd);
7500Sstevel@tonic-gate 		free(s);
7510Sstevel@tonic-gate 		return (PAPI_DEVICE_ERROR);
7520Sstevel@tonic-gate 	}
7530Sstevel@tonic-gate #endif
7540Sstevel@tonic-gate 
7550Sstevel@tonic-gate 	/* store the meta-data file */
7560Sstevel@tonic-gate 	snprintf(lpfile, sizeof (lpfile), "%s-0", request_id);
7570Sstevel@tonic-gate 	s->meta_data_file = strdup(lpfile);
7580Sstevel@tonic-gate 	if (putrequest(lpfile, s->request) < 0) {
7590Sstevel@tonic-gate 		detailed_error(svc, gettext("unable to save request: %s: %s"),
7608569SJonathan.Ca@Sun.COM 		    lpfile, strerror(errno));
7610Sstevel@tonic-gate 		s->request = NULL;
7620Sstevel@tonic-gate 		return (PAPI_DEVICE_ERROR);
7630Sstevel@tonic-gate 	}
7640Sstevel@tonic-gate 
7650Sstevel@tonic-gate 	return (PAPI_OK);
7660Sstevel@tonic-gate }
7670Sstevel@tonic-gate 
7680Sstevel@tonic-gate papi_status_t
7690Sstevel@tonic-gate papiJobStreamWrite(papi_service_t handle,
7702264Sjacobs 		papi_stream_t stream, void *buffer, size_t buflen)
7710Sstevel@tonic-gate {
7720Sstevel@tonic-gate 	service_t *svc = handle;
7730Sstevel@tonic-gate 	job_stream_t *s = stream;
7740Sstevel@tonic-gate 
7750Sstevel@tonic-gate 	if ((svc == NULL) || (stream == NULL) || (buffer == NULL))
7760Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
7770Sstevel@tonic-gate 
7780Sstevel@tonic-gate 	if (write(s->fd, buffer, buflen) != buflen)
7790Sstevel@tonic-gate 		return (PAPI_DEVICE_ERROR);
7800Sstevel@tonic-gate 
7810Sstevel@tonic-gate 	return (PAPI_OK);
7820Sstevel@tonic-gate }
7830Sstevel@tonic-gate papi_status_t
7840Sstevel@tonic-gate papiJobStreamClose(papi_service_t handle,
7850Sstevel@tonic-gate 		papi_stream_t stream, papi_job_t *job)
7860Sstevel@tonic-gate {
7870Sstevel@tonic-gate 	papi_status_t status = PAPI_OK;
7880Sstevel@tonic-gate 	service_t *svc = handle;
7890Sstevel@tonic-gate 	job_stream_t *s = stream;
7900Sstevel@tonic-gate 	job_t *j = NULL;
7910Sstevel@tonic-gate 	char *tmp = NULL, *c;
7920Sstevel@tonic-gate 
7930Sstevel@tonic-gate 	if ((svc == NULL) || (stream == NULL) || (job == NULL))
7940Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
7950Sstevel@tonic-gate 
7960Sstevel@tonic-gate 	if ((*job = j = calloc(1, sizeof (*j))) == NULL)
7970Sstevel@tonic-gate 		return (PAPI_TEMPORARY_ERROR);
7980Sstevel@tonic-gate 
7990Sstevel@tonic-gate 	close(s->fd);
8000Sstevel@tonic-gate 
8010Sstevel@tonic-gate 	lpsched_request_to_job_attributes(s->request, j);
8020Sstevel@tonic-gate 
8030Sstevel@tonic-gate 	if (s->meta_data_file != NULL) {
8040Sstevel@tonic-gate 		status = lpsched_commit_job(svc, s->meta_data_file, &tmp);
8050Sstevel@tonic-gate 		if (status != PAPI_OK) {
8060Sstevel@tonic-gate 			unlink(s->meta_data_file);
8070Sstevel@tonic-gate 			return (status);
8080Sstevel@tonic-gate 		}
8090Sstevel@tonic-gate 		if ((c = strrchr(tmp, '-')) != NULL)
8100Sstevel@tonic-gate 			c++;
8110Sstevel@tonic-gate 		papiAttributeListAddInteger(&j->attributes, PAPI_ATTR_REPLACE,
8128569SJonathan.Ca@Sun.COM 		    "job-id", atoi(c));
8130Sstevel@tonic-gate 		papiAttributeListAddString(&j->attributes, PAPI_ATTR_REPLACE,
8148569SJonathan.Ca@Sun.COM 		    "job-uri", tmp);
8150Sstevel@tonic-gate 		free(s->meta_data_file);
8160Sstevel@tonic-gate 	}
8177253Sjacobs 	freerequest(s->request);
8180Sstevel@tonic-gate 	free(s);
8190Sstevel@tonic-gate 
8200Sstevel@tonic-gate 	return (PAPI_OK);
8210Sstevel@tonic-gate }
8220Sstevel@tonic-gate 
8230Sstevel@tonic-gate papi_status_t
8242264Sjacobs papiJobQuery(papi_service_t handle, char *printer, int32_t job_id,
8252264Sjacobs 		char **requested_attrs,
8260Sstevel@tonic-gate 		papi_job_t *job)
8270Sstevel@tonic-gate {
8280Sstevel@tonic-gate 	service_t *svc = handle;
8290Sstevel@tonic-gate 	job_t *j;
8300Sstevel@tonic-gate 	char *dest;
8310Sstevel@tonic-gate 	char req_id[32];
8320Sstevel@tonic-gate 	short rc;
8330Sstevel@tonic-gate 	char *form = NULL,
8348569SJonathan.Ca@Sun.COM 	    *request_id = NULL,
8358569SJonathan.Ca@Sun.COM 	    *charset = NULL,
8368569SJonathan.Ca@Sun.COM 	    *user = NULL,
8378569SJonathan.Ca@Sun.COM 	    *slabel = NULL,
8388569SJonathan.Ca@Sun.COM 	    *file = NULL;
8390Sstevel@tonic-gate 	time_t date = 0;
8400Sstevel@tonic-gate 	size_t size = 0;
8410Sstevel@tonic-gate 	short  rank = 0,
8428569SJonathan.Ca@Sun.COM 	    state = 0;
8430Sstevel@tonic-gate 
8440Sstevel@tonic-gate 	if ((handle == NULL) || (printer == NULL) || (job_id < 0))
8450Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
8460Sstevel@tonic-gate 
8470Sstevel@tonic-gate 	dest = printer_name_from_uri_id(printer, job_id);
8480Sstevel@tonic-gate 	snprintf(req_id, sizeof (req_id), "%s-%d", dest, job_id);
8490Sstevel@tonic-gate 	free(dest);
8500Sstevel@tonic-gate 
8510Sstevel@tonic-gate 	rc = snd_msg(svc, S_INQUIRE_REQUEST_RANK, 0, "", "", req_id, "", "");
8520Sstevel@tonic-gate 	if (rc < 0)
8530Sstevel@tonic-gate 		return (PAPI_SERVICE_UNAVAILABLE);
8540Sstevel@tonic-gate 
8550Sstevel@tonic-gate 	if (rcv_msg(svc, R_INQUIRE_REQUEST_RANK, &rc, &request_id,
8568569SJonathan.Ca@Sun.COM 	    &user, &slabel, &size, &date, &state, &dest, &form,
8578569SJonathan.Ca@Sun.COM 	    &charset, &rank, &file) < 0) {
8580Sstevel@tonic-gate 		detailed_error(svc,
8598569SJonathan.Ca@Sun.COM 		    gettext("failed to read response from scheduler"));
8600Sstevel@tonic-gate 		return (PAPI_DEVICE_ERROR);
8610Sstevel@tonic-gate 	}
8620Sstevel@tonic-gate 
8630Sstevel@tonic-gate 	if ((request_id == NULL) || (request_id[0] == NULL))
8640Sstevel@tonic-gate 		return (PAPI_NOT_FOUND);
8650Sstevel@tonic-gate 
8660Sstevel@tonic-gate 	if ((*job = j = calloc(1, sizeof (*j))) == NULL)
8670Sstevel@tonic-gate 		return (PAPI_TEMPORARY_ERROR);
8680Sstevel@tonic-gate 
8691676Sjpk 	job_status_to_attributes(j, request_id, user, slabel, size, date, state,
8708569SJonathan.Ca@Sun.COM 	    dest, form, charset, rank, file);
8710Sstevel@tonic-gate 
8720Sstevel@tonic-gate 	snprintf(req_id, sizeof (req_id), "%d-0", job_id);
8730Sstevel@tonic-gate 	lpsched_read_job_configuration(svc, j, req_id);
8740Sstevel@tonic-gate 
8750Sstevel@tonic-gate 	return (PAPI_OK);
8760Sstevel@tonic-gate }
8770Sstevel@tonic-gate 
8780Sstevel@tonic-gate papi_status_t
8792264Sjacobs papiJobMove(papi_service_t handle, char *printer, int32_t job_id,
8802264Sjacobs 		char *destination)
8812264Sjacobs {
8822264Sjacobs 	papi_status_t result = PAPI_OK;
8838266SNagaraj.Yedathore@Sun.COM 	long bits;
8842264Sjacobs 	service_t *svc = handle;
8852264Sjacobs 	char req_id[64];
8862264Sjacobs 	char *queue;
8872264Sjacobs 	char *user = NULL;
8882264Sjacobs 
8892264Sjacobs 	if ((svc == NULL) || (printer == NULL) || (job_id < 0) ||
8902264Sjacobs 	    (destination == NULL))
8912264Sjacobs 		return (PAPI_BAD_ARGUMENT);
8922264Sjacobs 
8932264Sjacobs 	queue = printer_name_from_uri_id(printer, job_id);
8942264Sjacobs 	snprintf(req_id, sizeof (req_id), "%s-%d", queue, job_id);
8952264Sjacobs 	free(queue);
8962264Sjacobs 
8972264Sjacobs 	if (papiAttributeListGetString(svc->attributes, NULL, "user-name",
8988569SJonathan.Ca@Sun.COM 	    &user) == PAPI_OK) {
8992264Sjacobs 		REQUEST *r = getrequest(req_id);
9002264Sjacobs 
9012264Sjacobs 		if ((r != NULL) && (r->user != NULL) &&
9022264Sjacobs 		    (strcmp(r->user, user) != 0))
9032264Sjacobs 			result = PAPI_NOT_AUTHORIZED;
9042264Sjacobs 		freerequest(r);
9052264Sjacobs 	}
9062264Sjacobs 
9072264Sjacobs 	if (result == PAPI_OK) {
9082264Sjacobs 		short status = MOK;
9092264Sjacobs 		char *dest = printer_name_from_uri_id(destination, -1);
9102264Sjacobs 
9112264Sjacobs 		if ((snd_msg(svc, S_MOVE_REQUEST, req_id, dest) < 0) ||
9128266SNagaraj.Yedathore@Sun.COM 		    (rcv_msg(svc, R_MOVE_REQUEST, &status, &bits) < 0))
9132264Sjacobs 			status = MTRANSMITERR;
9142264Sjacobs 
9152264Sjacobs 		free(dest);
9162264Sjacobs 
9172264Sjacobs 		result = lpsched_status_to_papi_status(status);
9182264Sjacobs 	}
9192264Sjacobs 
9202264Sjacobs 	return (result);
9212264Sjacobs }
9222264Sjacobs 
9232264Sjacobs papi_status_t
9242264Sjacobs papiJobCancel(papi_service_t handle, char *printer, int32_t job_id)
9250Sstevel@tonic-gate {
9260Sstevel@tonic-gate 	papi_status_t result = PAPI_OK;
9270Sstevel@tonic-gate 	service_t *svc = handle;
9280Sstevel@tonic-gate 	char req_id[64];
9290Sstevel@tonic-gate 	char *dest;
9300Sstevel@tonic-gate 	char *user = NULL;
9310Sstevel@tonic-gate 
9320Sstevel@tonic-gate 	if ((svc == NULL) || (printer == NULL) || (job_id < 0))
9330Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
9340Sstevel@tonic-gate 
9350Sstevel@tonic-gate 	dest = printer_name_from_uri_id(printer, job_id);
9360Sstevel@tonic-gate 	snprintf(req_id, sizeof (req_id), "%s-%d", dest, job_id);
9370Sstevel@tonic-gate 	free(dest);
9380Sstevel@tonic-gate 
9390Sstevel@tonic-gate 	if (papiAttributeListGetString(svc->attributes, NULL, "user-name",
9408569SJonathan.Ca@Sun.COM 	    &user) == PAPI_OK) {
9410Sstevel@tonic-gate 		REQUEST *r = getrequest(req_id);
9420Sstevel@tonic-gate 
9438966SSonam.Gupta@Sun.COM 		if ((result = authorized(handle, job_id)) != PAPI_OK)
9448966SSonam.Gupta@Sun.COM 			result = PAPI_NOT_AUTHORIZED;
9458966SSonam.Gupta@Sun.COM 
9462264Sjacobs 		if ((r != NULL) && (r->user != NULL) &&
9472264Sjacobs 		    (strcmp(r->user, user) != 0))
9480Sstevel@tonic-gate 			result = PAPI_NOT_AUTHORIZED;
9490Sstevel@tonic-gate 		freerequest(r);
9500Sstevel@tonic-gate 	}
9510Sstevel@tonic-gate 
9520Sstevel@tonic-gate 	if (result == PAPI_OK) {
9530Sstevel@tonic-gate 		short status = MOK;
9540Sstevel@tonic-gate 
9550Sstevel@tonic-gate 		if ((snd_msg(svc, S_CANCEL_REQUEST, req_id) < 0) ||
9560Sstevel@tonic-gate 		    (rcv_msg(svc, R_CANCEL_REQUEST, &status) < 0))
9570Sstevel@tonic-gate 			status = MTRANSMITERR;
9580Sstevel@tonic-gate 
9590Sstevel@tonic-gate 		result = lpsched_status_to_papi_status(status);
9600Sstevel@tonic-gate 	}
9610Sstevel@tonic-gate 
9620Sstevel@tonic-gate 	return (result);
9630Sstevel@tonic-gate }
9640Sstevel@tonic-gate 
9650Sstevel@tonic-gate papi_status_t
9662264Sjacobs hold_release_job(papi_service_t handle, char *printer,
9672264Sjacobs 		int32_t job_id, int flag)
9680Sstevel@tonic-gate {
9690Sstevel@tonic-gate 	papi_status_t status;
9700Sstevel@tonic-gate 	service_t *svc = handle;
9710Sstevel@tonic-gate 	REQUEST *r = NULL;
9720Sstevel@tonic-gate 	char *file;
9730Sstevel@tonic-gate 	char *dest;
9740Sstevel@tonic-gate 
9750Sstevel@tonic-gate 	if ((svc == NULL) || (printer == NULL) || (job_id < 0))
9760Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
9770Sstevel@tonic-gate 
9780Sstevel@tonic-gate 	if ((status = authorized(svc, job_id)) != PAPI_OK)
9790Sstevel@tonic-gate 		return (status);
9800Sstevel@tonic-gate 
9810Sstevel@tonic-gate 	dest = printer_name_from_uri_id(printer, job_id);
9820Sstevel@tonic-gate 	status = lpsched_start_change(svc, dest, job_id, &file);
9830Sstevel@tonic-gate 	if (status != PAPI_OK)
9840Sstevel@tonic-gate 		return (status);
9850Sstevel@tonic-gate 
9860Sstevel@tonic-gate 	if ((r = getrequest(file)) != NULL) {
9870Sstevel@tonic-gate 		r->actions &= ~ACT_RESUME;
9882264Sjacobs 		switch (flag) {
9892264Sjacobs 		case 0:
9900Sstevel@tonic-gate 			r->actions |= ACT_HOLD;
9912264Sjacobs 			break;
9922264Sjacobs 		case 1:
9930Sstevel@tonic-gate 			r->actions |= ACT_RESUME;
9942264Sjacobs 			break;
9952264Sjacobs 		case 2:
9962264Sjacobs 			r->actions |= ACT_IMMEDIATE;
9972264Sjacobs 			break;
9982264Sjacobs 		}
9990Sstevel@tonic-gate 		if (putrequest(file, r) < 0) {
10000Sstevel@tonic-gate 			detailed_error(svc,
10018569SJonathan.Ca@Sun.COM 			    gettext("failed to write job: %s: %s"),
10028569SJonathan.Ca@Sun.COM 			    file, strerror(errno));
10032264Sjacobs 			freerequest(r);
10040Sstevel@tonic-gate 			return (PAPI_DEVICE_ERROR);
10050Sstevel@tonic-gate 		}
10062264Sjacobs 		freerequest(r);
10070Sstevel@tonic-gate 	} else {
10080Sstevel@tonic-gate 		detailed_error(svc, gettext("failed to read job: %s: %s"),
10098569SJonathan.Ca@Sun.COM 		    file, strerror(errno));
10100Sstevel@tonic-gate 		return (PAPI_DEVICE_ERROR);
10110Sstevel@tonic-gate 	}
10120Sstevel@tonic-gate 
10130Sstevel@tonic-gate 	status = lpsched_end_change(svc, dest, job_id);
10140Sstevel@tonic-gate 
10150Sstevel@tonic-gate 	return (status);
10160Sstevel@tonic-gate }
10170Sstevel@tonic-gate 
10180Sstevel@tonic-gate papi_status_t
10192264Sjacobs papiJobHold(papi_service_t handle, char *printer, int32_t job_id)
10200Sstevel@tonic-gate {
10210Sstevel@tonic-gate 	return (hold_release_job(handle, printer, job_id, 0));
10220Sstevel@tonic-gate }
10230Sstevel@tonic-gate 
10240Sstevel@tonic-gate papi_status_t
10252264Sjacobs papiJobRelease(papi_service_t handle, char *printer, int32_t job_id)
10260Sstevel@tonic-gate {
10270Sstevel@tonic-gate 	return (hold_release_job(handle, printer, job_id, 1));
10280Sstevel@tonic-gate }
10290Sstevel@tonic-gate 
10300Sstevel@tonic-gate papi_status_t
10312264Sjacobs papiJobPromote(papi_service_t handle, char *printer, int32_t job_id)
10320Sstevel@tonic-gate {
10332264Sjacobs 	return (hold_release_job(handle, printer, job_id, 2));
10340Sstevel@tonic-gate }
10350Sstevel@tonic-gate 
10360Sstevel@tonic-gate papi_status_t
10372264Sjacobs papiJobModify(papi_service_t handle, char *printer, int32_t job_id,
10382264Sjacobs 		papi_attribute_t **attributes, papi_job_t *job)
10390Sstevel@tonic-gate {
10400Sstevel@tonic-gate 	papi_status_t status;
10410Sstevel@tonic-gate 	job_t *j = NULL;
10420Sstevel@tonic-gate 	service_t *svc = handle;
10430Sstevel@tonic-gate 	char *file = NULL;
10440Sstevel@tonic-gate 	char *dest;
10450Sstevel@tonic-gate 	REQUEST *r = NULL;
10460Sstevel@tonic-gate 	char lpfile[BUFSIZ];
10470Sstevel@tonic-gate 
10480Sstevel@tonic-gate 	if ((svc == NULL) || (printer == NULL) || (job_id < 0) ||
10490Sstevel@tonic-gate 	    (attributes == NULL))
10500Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
10510Sstevel@tonic-gate 
10520Sstevel@tonic-gate 	if ((*job = j = calloc(1, sizeof (*j))) == NULL)
10530Sstevel@tonic-gate 		return (PAPI_TEMPORARY_ERROR);
10540Sstevel@tonic-gate 
10550Sstevel@tonic-gate 	dest = printer_name_from_uri_id(printer, job_id);
10560Sstevel@tonic-gate 	status = lpsched_start_change(svc, dest, job_id, &file);
10570Sstevel@tonic-gate 	if (status != PAPI_OK)
10580Sstevel@tonic-gate 		return (status);
10590Sstevel@tonic-gate 
10600Sstevel@tonic-gate 	if ((r = getrequest(file)) != NULL) {
10610Sstevel@tonic-gate 		job_attributes_to_lpsched_request(handle, r,
10628569SJonathan.Ca@Sun.COM 		    (papi_attribute_t **)attributes);
10630Sstevel@tonic-gate #ifdef LP_USE_PAPI_ATTR
10640Sstevel@tonic-gate 		/*
10650Sstevel@tonic-gate 		 * store the job attributes in the PAPI job attribute file
10660Sstevel@tonic-gate 		 * that was created by the origonal job request. We need to
10670Sstevel@tonic-gate 		 * modify the attributes in the file as per the new attributes
10680Sstevel@tonic-gate 		 */
10690Sstevel@tonic-gate 		snprintf(lpfile, sizeof (lpfile), "%s%d-%s",
10708569SJonathan.Ca@Sun.COM 		    "/var/spool/lp/temp/", job_id, LP_PAPIATTRNAME);
10710Sstevel@tonic-gate 		status = psm_modifyAttrsFile(attributes, lpfile);
10720Sstevel@tonic-gate 		if (status != PAPI_OK) {
10730Sstevel@tonic-gate 			detailed_error(svc,
10748569SJonathan.Ca@Sun.COM 			    "unable to modify the attributes file: %s: %s",
10758569SJonathan.Ca@Sun.COM 			    lpfile, strerror(errno));
10760Sstevel@tonic-gate 			return (PAPI_DEVICE_ERROR);
10770Sstevel@tonic-gate 		}
10780Sstevel@tonic-gate #endif
10790Sstevel@tonic-gate 
10800Sstevel@tonic-gate 		if (putrequest(file, r) < 0) {
10810Sstevel@tonic-gate 			detailed_error(svc,
10828569SJonathan.Ca@Sun.COM 			    gettext("failed to write job: %s: %s"),
10838569SJonathan.Ca@Sun.COM 			    file, strerror(errno));
10840Sstevel@tonic-gate 			freerequest(r);
10850Sstevel@tonic-gate 			return (PAPI_DEVICE_ERROR);
10860Sstevel@tonic-gate 		}
10870Sstevel@tonic-gate 	} else {
10880Sstevel@tonic-gate 		detailed_error(svc, gettext("failed to read job: %s: %s"),
10898569SJonathan.Ca@Sun.COM 		    file, strerror(errno));
10900Sstevel@tonic-gate 		return (PAPI_DEVICE_ERROR);
10910Sstevel@tonic-gate 	}
10920Sstevel@tonic-gate 
10930Sstevel@tonic-gate 	status = lpsched_end_change(svc, dest, job_id);
10940Sstevel@tonic-gate 	lpsched_request_to_job_attributes(r, j);
10956725Sjacobs 
10966725Sjacobs 	papiAttributeListAddInteger(&j->attributes, PAPI_ATTR_REPLACE,
10978569SJonathan.Ca@Sun.COM 	    "job-id", job_id);
10986725Sjacobs 
10990Sstevel@tonic-gate 	freerequest(r);
11000Sstevel@tonic-gate 
11010Sstevel@tonic-gate 	return (status);
11020Sstevel@tonic-gate }
11030Sstevel@tonic-gate 
11040Sstevel@tonic-gate /*
11050Sstevel@tonic-gate  * Extension to PAPI, a variation of this is slated for post-1.0
11060Sstevel@tonic-gate  */
11070Sstevel@tonic-gate #define	DUMMY_FILE	"/var/spool/lp/fifos/FIFO"
11080Sstevel@tonic-gate 
11090Sstevel@tonic-gate papi_status_t
11102264Sjacobs papiJobCreate(papi_service_t handle, char *printer,
11112264Sjacobs 		papi_attribute_t **job_attributes,
11122264Sjacobs 		papi_job_ticket_t *job_ticket, papi_job_t *job)
11130Sstevel@tonic-gate {
11140Sstevel@tonic-gate 	papi_status_t status;
11150Sstevel@tonic-gate 	service_t *svc = handle;
11160Sstevel@tonic-gate 	job_t *j = NULL;
11170Sstevel@tonic-gate 	REQUEST *request;
11180Sstevel@tonic-gate 	char *request_id = NULL;
11190Sstevel@tonic-gate 	char *c;
11200Sstevel@tonic-gate 	char *tmp = NULL;
11210Sstevel@tonic-gate 	char metadata_file[MAXPATHLEN];
11220Sstevel@tonic-gate 
11230Sstevel@tonic-gate 	if ((svc == NULL) || (printer == NULL) || (job == NULL))
11240Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
11250Sstevel@tonic-gate 
11260Sstevel@tonic-gate 	if (job_ticket != NULL)
11270Sstevel@tonic-gate 		return (PAPI_JOB_TICKET_NOT_SUPPORTED);
11280Sstevel@tonic-gate 
11290Sstevel@tonic-gate 	if ((*job = j = calloc(1, sizeof (*j))) == NULL)
11300Sstevel@tonic-gate 		return (PAPI_TEMPORARY_ERROR);
11310Sstevel@tonic-gate 
11320Sstevel@tonic-gate 	/* 1 for the control file (-0) */
11330Sstevel@tonic-gate 	status = lpsched_alloc_files(svc, 1, &request_id);
11340Sstevel@tonic-gate 	if (status != PAPI_OK)
11350Sstevel@tonic-gate 		return (status);
11360Sstevel@tonic-gate 
11370Sstevel@tonic-gate 	/* convert the attributes to an lpsched REQUEST structure */
11380Sstevel@tonic-gate 	request = create_request(svc, (char *)printer,
11398569SJonathan.Ca@Sun.COM 	    (papi_attribute_t **)job_attributes);
11402264Sjacobs 	if (request == NULL)
11412264Sjacobs 		return (PAPI_TEMPORARY_ERROR);
11420Sstevel@tonic-gate 	addlist(&request->file_list, DUMMY_FILE);	/* add a dummy file */
11430Sstevel@tonic-gate 	request->actions |= ACT_HOLD;			/* hold the job */
11440Sstevel@tonic-gate 
11450Sstevel@tonic-gate #ifdef LP_USE_PAPI_ATTR
11460Sstevel@tonic-gate 	/*
11470Sstevel@tonic-gate 	 * store the job attributes in the PAPI job attribute file that was
11480Sstevel@tonic-gate 	 * created by lpsched_alloc_files(), the attributes will then pass
11490Sstevel@tonic-gate 	 * through lpsched and be given to the slow-filters and the printer's
11500Sstevel@tonic-gate 	 * interface script to process them
11510Sstevel@tonic-gate 	 */
11520Sstevel@tonic-gate 	snprintf(metadata_file, sizeof (metadata_file), "%s%s-%s",
11538569SJonathan.Ca@Sun.COM 	    "/var/spool/lp/temp/", request_id, LP_PAPIATTRNAME);
11540Sstevel@tonic-gate 	status = psm_copy_attrsToFile(job_attributes, metadata_file);
11550Sstevel@tonic-gate 	if (status != PAPI_OK) {
11560Sstevel@tonic-gate 		detailed_error(svc, "unable to copy attributes to file: %s: %s",
11578569SJonathan.Ca@Sun.COM 		    metadata_file, strerror(errno));
11582264Sjacobs 		free(request_id);
11590Sstevel@tonic-gate 		return (PAPI_DEVICE_ERROR);
11600Sstevel@tonic-gate 	}
11610Sstevel@tonic-gate #endif
11620Sstevel@tonic-gate 
11630Sstevel@tonic-gate 	/* store the REQUEST on disk */
11640Sstevel@tonic-gate 	snprintf(metadata_file, sizeof (metadata_file), "%s-0", request_id);
11652264Sjacobs 	free(request_id);
11660Sstevel@tonic-gate 	if (putrequest(metadata_file, request) < 0) {
11670Sstevel@tonic-gate 		detailed_error(svc, gettext("unable to save request: %s: %s"),
11688569SJonathan.Ca@Sun.COM 		    metadata_file, strerror(errno));
11690Sstevel@tonic-gate 		return (PAPI_DEVICE_ERROR);
11700Sstevel@tonic-gate 	}
11710Sstevel@tonic-gate 
11720Sstevel@tonic-gate 	status = lpsched_commit_job(svc, metadata_file, &tmp);
11730Sstevel@tonic-gate 	if (status != PAPI_OK) {
11740Sstevel@tonic-gate 		unlink(metadata_file);
11750Sstevel@tonic-gate 		return (status);
11760Sstevel@tonic-gate 	}
11770Sstevel@tonic-gate 
11780Sstevel@tonic-gate 	lpsched_request_to_job_attributes(request, j);
11790Sstevel@tonic-gate 
11800Sstevel@tonic-gate 	if ((c = strrchr(tmp, '-')) != NULL)
11810Sstevel@tonic-gate 		c++;
11820Sstevel@tonic-gate 	papiAttributeListAddInteger(&j->attributes, PAPI_ATTR_REPLACE,
11838569SJonathan.Ca@Sun.COM 	    "job-id", atoi(c));
11840Sstevel@tonic-gate 	papiAttributeListAddString(&j->attributes, PAPI_ATTR_REPLACE,
11858569SJonathan.Ca@Sun.COM 	    "job-uri", tmp);
11860Sstevel@tonic-gate 
11870Sstevel@tonic-gate 	return (PAPI_OK);
11880Sstevel@tonic-gate }
11890Sstevel@tonic-gate 
11900Sstevel@tonic-gate papi_status_t
11910Sstevel@tonic-gate papiJobCommit(papi_service_t handle, char *printer, int32_t id)
11920Sstevel@tonic-gate {
11930Sstevel@tonic-gate 	papi_status_t status = PAPI_OK;
11940Sstevel@tonic-gate 	service_t *svc = handle;
11950Sstevel@tonic-gate 	REQUEST *r = NULL;
11960Sstevel@tonic-gate 	char *metadata_file;
11970Sstevel@tonic-gate 	char *dest;
11980Sstevel@tonic-gate 
11990Sstevel@tonic-gate 	if ((svc == NULL) || (printer == NULL))
12000Sstevel@tonic-gate 		return (PAPI_BAD_ARGUMENT);
12010Sstevel@tonic-gate 
12020Sstevel@tonic-gate 	dest = printer_name_from_uri_id(printer, id);
12030Sstevel@tonic-gate 	/* tell the scheduler that we want to change the job */
12040Sstevel@tonic-gate 	status = lpsched_start_change(svc, dest, id, &metadata_file);
12050Sstevel@tonic-gate 	if (status != PAPI_OK)
12060Sstevel@tonic-gate 		return (status);
12070Sstevel@tonic-gate 
12080Sstevel@tonic-gate 	if ((r = getrequest(metadata_file)) != NULL) {
12090Sstevel@tonic-gate 		r->actions &= ~ACT_RESUME;
12100Sstevel@tonic-gate 		r->actions |= ACT_RESUME;
12110Sstevel@tonic-gate 		dellist(&r->file_list, DUMMY_FILE);
12120Sstevel@tonic-gate 
12130Sstevel@tonic-gate 		if (putrequest(metadata_file, r) < 0) {
12140Sstevel@tonic-gate 			detailed_error(svc,
12158569SJonathan.Ca@Sun.COM 			    gettext("failed to write job: %s: %s"),
12168569SJonathan.Ca@Sun.COM 			    metadata_file, strerror(errno));
12172264Sjacobs 			freerequest(r);
12180Sstevel@tonic-gate 			return (PAPI_DEVICE_ERROR);
12190Sstevel@tonic-gate 		}
12200Sstevel@tonic-gate 	} else {
12210Sstevel@tonic-gate 		detailed_error(svc, gettext("failed to read job: %s: %s"),
12228569SJonathan.Ca@Sun.COM 		    metadata_file, strerror(errno));
12230Sstevel@tonic-gate 		return (PAPI_DEVICE_ERROR);
12240Sstevel@tonic-gate 	}
12250Sstevel@tonic-gate 
12260Sstevel@tonic-gate 	status = lpsched_end_change(svc, dest, id);
12270Sstevel@tonic-gate 	freerequest(r);
12280Sstevel@tonic-gate 
12290Sstevel@tonic-gate 	return (status);
12300Sstevel@tonic-gate }
12310Sstevel@tonic-gate 
12320Sstevel@tonic-gate papi_status_t
12330Sstevel@tonic-gate papiJobStreamAdd(papi_service_t handle, char *printer, int32_t id,
12340Sstevel@tonic-gate 		papi_stream_t *stream)
12350Sstevel@tonic-gate {
12360Sstevel@tonic-gate 	papi_status_t status;
12370Sstevel@tonic-gate 	service_t *svc = handle;
12380Sstevel@tonic-gate 	job_stream_t *s = NULL;
12390Sstevel@tonic-gate 	char *metadata_file = NULL;
12400Sstevel@tonic-gate 	char *dest;
12410Sstevel@tonic-gate 	char path[MAXPATHLEN];
12420Sstevel@tonic-gate 
12430Sstevel@tonic-gate 	/* allocate space for the stream */
12440Sstevel@tonic-gate 	if ((*stream = s = calloc(1, sizeof (*s))) == NULL)
12450Sstevel@tonic-gate 		return (PAPI_TEMPORARY_ERROR);
12460Sstevel@tonic-gate 
12470Sstevel@tonic-gate 	dest = printer_name_from_uri_id(printer, id);
12480Sstevel@tonic-gate 	/* create/open data file (only root or lp can really do this */
12490Sstevel@tonic-gate 	snprintf(path, sizeof (path), "/var/spool/lp/temp/%d-XXXXXX", id);
12500Sstevel@tonic-gate 	if ((s->fd = mkstemp(path)) < 0) {
12510Sstevel@tonic-gate 		detailed_error(svc, gettext("unable to create sink (%s): %s"),
12528569SJonathan.Ca@Sun.COM 		    path, strerror(errno));
12530Sstevel@tonic-gate 		free(s);
12540Sstevel@tonic-gate 		return (PAPI_NOT_AUTHORIZED);
12550Sstevel@tonic-gate 	}
12560Sstevel@tonic-gate 
12570Sstevel@tonic-gate 	/* add data file to job */
12580Sstevel@tonic-gate 	status = lpsched_start_change(svc, dest, id, &metadata_file);
12590Sstevel@tonic-gate 	if (status != PAPI_OK) {
12600Sstevel@tonic-gate 		close(s->fd);
12610Sstevel@tonic-gate 		free(s);
12620Sstevel@tonic-gate 		unlink(path);
12630Sstevel@tonic-gate 		return (status);
12640Sstevel@tonic-gate 	}
12650Sstevel@tonic-gate 
12660Sstevel@tonic-gate 	if ((s->request = getrequest(metadata_file)) == NULL) {
12670Sstevel@tonic-gate 		detailed_error(svc, gettext("unable to load request: %s: %s"),
12688569SJonathan.Ca@Sun.COM 		    metadata_file, strerror(errno));
12690Sstevel@tonic-gate 		close(s->fd);
12700Sstevel@tonic-gate 		free(s);
12710Sstevel@tonic-gate 		unlink(path);
12720Sstevel@tonic-gate 		return (PAPI_NOT_POSSIBLE);
12730Sstevel@tonic-gate 	}
12740Sstevel@tonic-gate 
12750Sstevel@tonic-gate 	addlist(&(s->request->file_list), path);
12760Sstevel@tonic-gate 
12770Sstevel@tonic-gate 	if (putrequest(metadata_file, s->request) < 0) {
12780Sstevel@tonic-gate 		detailed_error(svc, gettext("unable to save request: %s: %s"),
12798569SJonathan.Ca@Sun.COM 		    metadata_file, strerror(errno));
12800Sstevel@tonic-gate 		close(s->fd);
12810Sstevel@tonic-gate 		free(s);
12820Sstevel@tonic-gate 		unlink(path);
12830Sstevel@tonic-gate 		return (PAPI_NOT_POSSIBLE);
12840Sstevel@tonic-gate 	}
12850Sstevel@tonic-gate 
12860Sstevel@tonic-gate 	status = lpsched_end_change(svc, dest, id);
12870Sstevel@tonic-gate 
12880Sstevel@tonic-gate 	if (status != PAPI_OK)
12890Sstevel@tonic-gate 		return (status);
12900Sstevel@tonic-gate 
12910Sstevel@tonic-gate 	return (PAPI_OK);
12920Sstevel@tonic-gate }
1293