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 /*
2212091SSonam.Gupta@Sun.COM * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
230Sstevel@tonic-gate */
240Sstevel@tonic-gate
250Sstevel@tonic-gate /*LINTLIBRARY*/
260Sstevel@tonic-gate
270Sstevel@tonic-gate #include <stdlib.h>
280Sstevel@tonic-gate #include <string.h>
290Sstevel@tonic-gate #include <unistd.h>
300Sstevel@tonic-gate #include <libintl.h>
310Sstevel@tonic-gate #include <pwd.h>
320Sstevel@tonic-gate #include <sys/stat.h>
330Sstevel@tonic-gate #include <papi_impl.h>
340Sstevel@tonic-gate
352264Sjacobs /*
362264Sjacobs * for an older application that may have been linked with a pre-v1.0
372264Sjacobs * PAPI implementation.
382264Sjacobs */
392264Sjacobs papi_status_t
papiAttributeListAdd(papi_attribute_t *** attrs,int flags,char * name,papi_attribute_value_type_t type,papi_attribute_value_t * value)402264Sjacobs papiAttributeListAdd(papi_attribute_t ***attrs, int flags, char *name,
412264Sjacobs papi_attribute_value_type_t type, papi_attribute_value_t *value)
422264Sjacobs {
432264Sjacobs return (papiAttributeListAddValue(attrs, flags, name, type, value));
442264Sjacobs }
452264Sjacobs
460Sstevel@tonic-gate #ifdef LP_USE_PAPI_ATTR
472264Sjacobs static papi_status_t psm_modifyAttrsFile(papi_attribute_t **attrs, char *file);
482264Sjacobs static papi_status_t psm_modifyAttrsList(char *file, papi_attribute_t **attrs,
492264Sjacobs papi_attribute_t ***newAttrs);
500Sstevel@tonic-gate #endif
510Sstevel@tonic-gate
5212091SSonam.Gupta@Sun.COM int32_t
check_job_id(papi_service_t svc,char * printer,int32_t id)5312091SSonam.Gupta@Sun.COM check_job_id(papi_service_t svc, char *printer, int32_t id)
5412091SSonam.Gupta@Sun.COM {
5512091SSonam.Gupta@Sun.COM papi_job_t *jobs = NULL;
5612091SSonam.Gupta@Sun.COM papi_status_t status;
5712091SSonam.Gupta@Sun.COM int ret = -1;
5812091SSonam.Gupta@Sun.COM char *jattrs[] = { "job-id",
5912091SSonam.Gupta@Sun.COM "job-id-requested", NULL };
6012091SSonam.Gupta@Sun.COM
6112091SSonam.Gupta@Sun.COM status = papiPrinterListJobs(svc, printer, jattrs, PAPI_LIST_JOBS_ALL,
6212091SSonam.Gupta@Sun.COM 0, &jobs);
6312091SSonam.Gupta@Sun.COM
6412091SSonam.Gupta@Sun.COM if (status != PAPI_OK) {
6512091SSonam.Gupta@Sun.COM detailed_error(svc,
6612091SSonam.Gupta@Sun.COM gettext("Failed to query service for %s: %s\n"),
6712091SSonam.Gupta@Sun.COM printer, lpsched_status_string(status));
6812091SSonam.Gupta@Sun.COM return (-1);
6912091SSonam.Gupta@Sun.COM }
7012091SSonam.Gupta@Sun.COM
7112091SSonam.Gupta@Sun.COM if (jobs != NULL) {
7212091SSonam.Gupta@Sun.COM int i = 0;
7312091SSonam.Gupta@Sun.COM
7412091SSonam.Gupta@Sun.COM for (i = 0; jobs[i] != NULL; i++) {
7512091SSonam.Gupta@Sun.COM int32_t rid = -1;
7612091SSonam.Gupta@Sun.COM int32_t jid = -1;
7712091SSonam.Gupta@Sun.COM papi_attribute_t **list =
7812091SSonam.Gupta@Sun.COM papiJobGetAttributeList(jobs[i]);
7912091SSonam.Gupta@Sun.COM
8012091SSonam.Gupta@Sun.COM papiAttributeListGetInteger(list, NULL,
8112091SSonam.Gupta@Sun.COM "job-id-requested", &rid);
8212091SSonam.Gupta@Sun.COM papiAttributeListGetInteger(list, NULL,
8312091SSonam.Gupta@Sun.COM "job-id", &jid);
8412091SSonam.Gupta@Sun.COM
8512091SSonam.Gupta@Sun.COM /*
8612091SSonam.Gupta@Sun.COM * check if id matches with either rid or jid
8712091SSonam.Gupta@Sun.COM */
8812091SSonam.Gupta@Sun.COM if (rid == id) {
8912091SSonam.Gupta@Sun.COM /* get the actual id and return it */
9012091SSonam.Gupta@Sun.COM papiAttributeListGetInteger(list, NULL,
9112091SSonam.Gupta@Sun.COM "job-id", &id);
9212091SSonam.Gupta@Sun.COM return (id);
9312091SSonam.Gupta@Sun.COM } else if (jid == id) {
9412091SSonam.Gupta@Sun.COM if (rid != -1) {
9512091SSonam.Gupta@Sun.COM /*
9612091SSonam.Gupta@Sun.COM * It is a remote lpd job.
9712091SSonam.Gupta@Sun.COM * It cannot be modified based on job-id
9812091SSonam.Gupta@Sun.COM * or spool number
9912091SSonam.Gupta@Sun.COM */
10012091SSonam.Gupta@Sun.COM return (-1);
10112091SSonam.Gupta@Sun.COM } else {
10212091SSonam.Gupta@Sun.COM /*
10312091SSonam.Gupta@Sun.COM * It is either local job or
10412091SSonam.Gupta@Sun.COM * remote ipp job
10512091SSonam.Gupta@Sun.COM */
10612091SSonam.Gupta@Sun.COM return (id);
10712091SSonam.Gupta@Sun.COM }
10812091SSonam.Gupta@Sun.COM }
10912091SSonam.Gupta@Sun.COM }
11012091SSonam.Gupta@Sun.COM }
11112091SSonam.Gupta@Sun.COM return (id);
11212091SSonam.Gupta@Sun.COM }
1130Sstevel@tonic-gate
1140Sstevel@tonic-gate void
papiJobFree(papi_job_t job)1150Sstevel@tonic-gate papiJobFree(papi_job_t job)
1160Sstevel@tonic-gate {
1170Sstevel@tonic-gate job_t *tmp = (job_t *)job;
1180Sstevel@tonic-gate
1190Sstevel@tonic-gate if (tmp != NULL) {
1200Sstevel@tonic-gate papiAttributeListFree(tmp->attributes);
1210Sstevel@tonic-gate free(tmp);
1220Sstevel@tonic-gate }
1230Sstevel@tonic-gate }
1240Sstevel@tonic-gate
1250Sstevel@tonic-gate void
papiJobListFree(papi_job_t * jobs)1260Sstevel@tonic-gate papiJobListFree(papi_job_t *jobs)
1270Sstevel@tonic-gate {
1280Sstevel@tonic-gate if (jobs != NULL) {
1290Sstevel@tonic-gate int i;
1300Sstevel@tonic-gate
1310Sstevel@tonic-gate for (i = 0; jobs[i] != NULL; i++) {
1320Sstevel@tonic-gate papiJobFree(jobs[i]);
1330Sstevel@tonic-gate }
1340Sstevel@tonic-gate free(jobs);
1350Sstevel@tonic-gate }
1360Sstevel@tonic-gate }
1370Sstevel@tonic-gate
1380Sstevel@tonic-gate papi_attribute_t **
papiJobGetAttributeList(papi_job_t job)1390Sstevel@tonic-gate papiJobGetAttributeList(papi_job_t job)
1400Sstevel@tonic-gate {
1410Sstevel@tonic-gate job_t *tmp = (job_t *)job;
1420Sstevel@tonic-gate
1430Sstevel@tonic-gate if (tmp != NULL)
1440Sstevel@tonic-gate return (tmp->attributes);
1450Sstevel@tonic-gate
1460Sstevel@tonic-gate return (NULL);
1470Sstevel@tonic-gate }
1480Sstevel@tonic-gate
1490Sstevel@tonic-gate char *
papiJobGetPrinterName(papi_job_t job)1500Sstevel@tonic-gate papiJobGetPrinterName(papi_job_t job)
1510Sstevel@tonic-gate {
1520Sstevel@tonic-gate job_t *tmp = (job_t *)job;
1530Sstevel@tonic-gate char *result = NULL;
1540Sstevel@tonic-gate
1550Sstevel@tonic-gate if (tmp != NULL)
1560Sstevel@tonic-gate papiAttributeListGetString(tmp->attributes, NULL,
1578569SJonathan.Ca@Sun.COM "printer-name", &result);
1580Sstevel@tonic-gate
1590Sstevel@tonic-gate return (result);
1600Sstevel@tonic-gate }
1610Sstevel@tonic-gate
1620Sstevel@tonic-gate int32_t
papiJobGetId(papi_job_t job)1630Sstevel@tonic-gate papiJobGetId(papi_job_t job)
1640Sstevel@tonic-gate {
1650Sstevel@tonic-gate job_t *tmp = (job_t *)job;
1660Sstevel@tonic-gate int result = -1;
1670Sstevel@tonic-gate
1680Sstevel@tonic-gate if (tmp != NULL)
1690Sstevel@tonic-gate papiAttributeListGetInteger(tmp->attributes, NULL, "job-id",
1708569SJonathan.Ca@Sun.COM &result);
1710Sstevel@tonic-gate
1720Sstevel@tonic-gate return (result);
1730Sstevel@tonic-gate }
1740Sstevel@tonic-gate
1750Sstevel@tonic-gate static REQUEST *
create_request(papi_service_t svc,char * printer,papi_attribute_t ** attributes)1760Sstevel@tonic-gate create_request(papi_service_t svc, char *printer, papi_attribute_t **attributes)
1770Sstevel@tonic-gate {
1787253Sjacobs REQUEST *r;
1790Sstevel@tonic-gate
1807253Sjacobs if ((r = calloc(1, sizeof (*r))) != NULL) {
1819152SSonam.Gupta@Sun.COM char *hostname = NULL;
1829152SSonam.Gupta@Sun.COM
1837253Sjacobs r->priority = -1;
1847253Sjacobs r->destination = printer_name_from_uri_id(printer, -1);
1859152SSonam.Gupta@Sun.COM
1869152SSonam.Gupta@Sun.COM papiAttributeListGetString(attributes, NULL,
1879152SSonam.Gupta@Sun.COM "job-originating-host-name", &hostname);
1889152SSonam.Gupta@Sun.COM
1899152SSonam.Gupta@Sun.COM if (hostname == NULL) {
1909152SSonam.Gupta@Sun.COM char host[BUFSIZ];
1919152SSonam.Gupta@Sun.COM
1929152SSonam.Gupta@Sun.COM if (gethostname(host, sizeof (host)) == 0)
1939152SSonam.Gupta@Sun.COM papiAttributeListAddString(&attributes,
1949152SSonam.Gupta@Sun.COM PAPI_ATTR_REPLACE,
1959152SSonam.Gupta@Sun.COM "job-originating-host-name",
1969152SSonam.Gupta@Sun.COM host);
1979152SSonam.Gupta@Sun.COM }
1989152SSonam.Gupta@Sun.COM
1997253Sjacobs job_attributes_to_lpsched_request(svc, r, attributes);
2007253Sjacobs }
2010Sstevel@tonic-gate
2027253Sjacobs return (r);
2030Sstevel@tonic-gate }
2040Sstevel@tonic-gate
2050Sstevel@tonic-gate static papi_status_t
authorized(service_t * svc,int32_t id)2060Sstevel@tonic-gate authorized(service_t *svc, int32_t id)
2070Sstevel@tonic-gate {
2080Sstevel@tonic-gate papi_status_t result = PAPI_NOT_AUTHORIZED; /* assume the worst */
2090Sstevel@tonic-gate char file[32];
2100Sstevel@tonic-gate REQUEST *r;
2110Sstevel@tonic-gate
2120Sstevel@tonic-gate snprintf(file, sizeof (file), "%d-0", id);
2130Sstevel@tonic-gate if ((r = getrequest(file)) != NULL) {
2140Sstevel@tonic-gate uid_t uid = getuid();
2150Sstevel@tonic-gate struct passwd *pw = NULL;
2160Sstevel@tonic-gate char *user = "intruder"; /* assume an intruder */
2170Sstevel@tonic-gate
2180Sstevel@tonic-gate if ((pw = getpwuid(uid)) != NULL)
2190Sstevel@tonic-gate user = pw->pw_name; /* use the process owner */
2200Sstevel@tonic-gate
2210Sstevel@tonic-gate if ((uid == 0) || (uid == 71)) { /* root/lp can forge this */
2220Sstevel@tonic-gate papi_status_t s;
2230Sstevel@tonic-gate s = papiAttributeListGetString(svc->attributes, NULL,
2248569SJonathan.Ca@Sun.COM "user-name", &user);
2250Sstevel@tonic-gate if (s != PAPI_OK) /* true root/lp are almighty */
2260Sstevel@tonic-gate result = PAPI_OK;
2270Sstevel@tonic-gate }
2280Sstevel@tonic-gate
2298966SSonam.Gupta@Sun.COM if (result != PAPI_OK) {
2308966SSonam.Gupta@Sun.COM if (strcmp(user, r->user) == 0)
2318966SSonam.Gupta@Sun.COM result = PAPI_OK;
2328966SSonam.Gupta@Sun.COM else {
2338966SSonam.Gupta@Sun.COM /*
2349742SSonam.Gupta@Sun.COM * user and r->user might contain the
2358966SSonam.Gupta@Sun.COM * host info also
2368966SSonam.Gupta@Sun.COM */
2379742SSonam.Gupta@Sun.COM char *token1 = strtok(r->user, "@");
2389742SSonam.Gupta@Sun.COM char *token2 = strtok(NULL, "@");
2399742SSonam.Gupta@Sun.COM char *token3 = strtok(user, "@");
2409742SSonam.Gupta@Sun.COM char *token4 = strtok(NULL, "@");
2418966SSonam.Gupta@Sun.COM
2429742SSonam.Gupta@Sun.COM /*
2439742SSonam.Gupta@Sun.COM * token1 and token3 contain usernames
2449742SSonam.Gupta@Sun.COM * token2 and token4 contain hostnames
2459742SSonam.Gupta@Sun.COM */
2469742SSonam.Gupta@Sun.COM if ((token1 == NULL) || (token3 == NULL))
2479742SSonam.Gupta@Sun.COM result = PAPI_NOT_AUTHORIZED;
2489742SSonam.Gupta@Sun.COM else if ((token4 != NULL) &&
2499742SSonam.Gupta@Sun.COM (strcmp(token4, "localhost") == 0) &&
2509742SSonam.Gupta@Sun.COM (strcmp(token3, "root") == 0) ||
2519742SSonam.Gupta@Sun.COM (strcmp(token3, "lp") == 0)) {
2529742SSonam.Gupta@Sun.COM /*
2539742SSonam.Gupta@Sun.COM * root/lp user on server can
2549742SSonam.Gupta@Sun.COM * cancel any requset
2559742SSonam.Gupta@Sun.COM */
2569742SSonam.Gupta@Sun.COM result = PAPI_OK;
2579742SSonam.Gupta@Sun.COM } else if (strcmp(token1, token3) == 0) {
2589742SSonam.Gupta@Sun.COM /*
2599742SSonam.Gupta@Sun.COM * usernames are same
2609742SSonam.Gupta@Sun.COM * compare the hostnames
2619742SSonam.Gupta@Sun.COM */
2629742SSonam.Gupta@Sun.COM if ((token4 != NULL) &&
2639742SSonam.Gupta@Sun.COM (token2 != NULL) &&
2649742SSonam.Gupta@Sun.COM (strcmp(token4, "localhost") ==
2659742SSonam.Gupta@Sun.COM 0)) {
2669742SSonam.Gupta@Sun.COM /*
2679742SSonam.Gupta@Sun.COM * Its server machine
2689742SSonam.Gupta@Sun.COM */
2699742SSonam.Gupta@Sun.COM static char host[256];
2709742SSonam.Gupta@Sun.COM if (gethostname(host,
2719742SSonam.Gupta@Sun.COM sizeof (host)) == 0) {
2729742SSonam.Gupta@Sun.COM if ((host != NULL) &&
2739742SSonam.Gupta@Sun.COM (strcmp(host,
2749742SSonam.Gupta@Sun.COM token2) == 0))
2759742SSonam.Gupta@Sun.COM result =
2769742SSonam.Gupta@Sun.COM PAPI_OK;
2779742SSonam.Gupta@Sun.COM }
2789742SSonam.Gupta@Sun.COM
2799742SSonam.Gupta@Sun.COM } else if ((token4 != NULL) &&
2809742SSonam.Gupta@Sun.COM (token2 != NULL) &&
2819742SSonam.Gupta@Sun.COM (strcmp(token4, token2) == 0)) {
2828966SSonam.Gupta@Sun.COM result = PAPI_OK;
2839742SSonam.Gupta@Sun.COM } else if ((token4 == NULL) &&
2849742SSonam.Gupta@Sun.COM (token2 != NULL)) {
2859742SSonam.Gupta@Sun.COM /*
2869742SSonam.Gupta@Sun.COM * When the request is sent from
2879742SSonam.Gupta@Sun.COM * client to server using ipp
2889742SSonam.Gupta@Sun.COM * token4 is NULL
2899742SSonam.Gupta@Sun.COM */
2909742SSonam.Gupta@Sun.COM result = PAPI_OK;
2919742SSonam.Gupta@Sun.COM }
2928966SSonam.Gupta@Sun.COM }
2938966SSonam.Gupta@Sun.COM }
2948966SSonam.Gupta@Sun.COM }
2950Sstevel@tonic-gate
2960Sstevel@tonic-gate freerequest(r);
2970Sstevel@tonic-gate } else
2980Sstevel@tonic-gate result = PAPI_NOT_FOUND;
2990Sstevel@tonic-gate
3000Sstevel@tonic-gate return (result);
3010Sstevel@tonic-gate }
3020Sstevel@tonic-gate
3030Sstevel@tonic-gate static papi_status_t
copy_file(char * from,char * to)3042264Sjacobs copy_file(char *from, char *to)
3050Sstevel@tonic-gate {
3060Sstevel@tonic-gate int ifd, ofd;
3070Sstevel@tonic-gate char buf[BUFSIZ];
3080Sstevel@tonic-gate int rc;
3090Sstevel@tonic-gate
3100Sstevel@tonic-gate if ((ifd = open(from, O_RDONLY)) < 0)
3110Sstevel@tonic-gate return (PAPI_DOCUMENT_ACCESS_ERROR);
3120Sstevel@tonic-gate
3130Sstevel@tonic-gate if ((ofd = open(to, O_WRONLY)) < 0) {
3140Sstevel@tonic-gate close(ifd);
3150Sstevel@tonic-gate return (PAPI_NOT_POSSIBLE);
3160Sstevel@tonic-gate }
3170Sstevel@tonic-gate
3180Sstevel@tonic-gate while ((rc = read(ifd, buf, sizeof (buf))) > 0)
3190Sstevel@tonic-gate write(ofd, buf, rc);
3200Sstevel@tonic-gate
3210Sstevel@tonic-gate close(ifd);
3220Sstevel@tonic-gate close(ofd);
3230Sstevel@tonic-gate
3240Sstevel@tonic-gate return (PAPI_OK);
3250Sstevel@tonic-gate }
3260Sstevel@tonic-gate
3270Sstevel@tonic-gate
3280Sstevel@tonic-gate #ifdef LP_USE_PAPI_ATTR
3290Sstevel@tonic-gate /*
3300Sstevel@tonic-gate * *****************************************************************************
3310Sstevel@tonic-gate *
3320Sstevel@tonic-gate * Description: Create a file containing all the attributes in the attribute
3330Sstevel@tonic-gate * list passed to this function.
3340Sstevel@tonic-gate * This file is then passed through lpsched and given to either
3350Sstevel@tonic-gate * a slow-filter or to the printer's interface script to process
3360Sstevel@tonic-gate * the attributes.
3370Sstevel@tonic-gate *
3380Sstevel@tonic-gate * Parameters: attrs - list of attributes and their values
3390Sstevel@tonic-gate * file - file pathname to create and put the attributes into.
3400Sstevel@tonic-gate *
3410Sstevel@tonic-gate * *****************************************************************************
3420Sstevel@tonic-gate */
3430Sstevel@tonic-gate
3440Sstevel@tonic-gate static papi_status_t
psm_copy_attrsToFile(papi_attribute_t ** attrs,char * file)3452264Sjacobs psm_copy_attrsToFile(papi_attribute_t **attrs, char *file)
3460Sstevel@tonic-gate
3470Sstevel@tonic-gate {
3480Sstevel@tonic-gate papi_status_t result = PAPI_OK;
3492264Sjacobs
3502264Sjacobs if ((attrs != NULL) && (*attrs != NULL)) {
3512264Sjacobs FILE *out = NULL;
3520Sstevel@tonic-gate
3532264Sjacobs if ((out = fopen(file, "w")) != NULL) {
3542264Sjacobs papiAttributeListPrint(out, attrs, "");
3550Sstevel@tonic-gate fclose(out);
3562264Sjacobs } else {
3570Sstevel@tonic-gate result = PAPI_NOT_POSSIBLE;
3580Sstevel@tonic-gate }
3590Sstevel@tonic-gate }
3600Sstevel@tonic-gate
3610Sstevel@tonic-gate return (result);
3620Sstevel@tonic-gate } /* psm_copy_attrsToFile */
3630Sstevel@tonic-gate
3640Sstevel@tonic-gate
3650Sstevel@tonic-gate /*
3660Sstevel@tonic-gate * *****************************************************************************
3670Sstevel@tonic-gate *
3680Sstevel@tonic-gate * Description: Modify the given attribute 'file' with the attributes from the
3690Sstevel@tonic-gate * 'attrs' list. Attributes already in the file will be replaced
3700Sstevel@tonic-gate * with the new value. New attributes will be added into the file.
3710Sstevel@tonic-gate *
3720Sstevel@tonic-gate * Parameters: attrs - list of attributes and their values
3730Sstevel@tonic-gate * file - file pathname to create and put the attributes into.
3740Sstevel@tonic-gate *
3750Sstevel@tonic-gate * *****************************************************************************
3760Sstevel@tonic-gate */
3770Sstevel@tonic-gate
3780Sstevel@tonic-gate static papi_status_t
psm_modifyAttrsFile(papi_attribute_t ** attrs,char * file)3792264Sjacobs psm_modifyAttrsFile(papi_attribute_t **attrs, char *file)
3800Sstevel@tonic-gate
3810Sstevel@tonic-gate {
3820Sstevel@tonic-gate papi_status_t result = PAPI_OK;
3830Sstevel@tonic-gate papi_attribute_t **newAttrs = NULL;
3840Sstevel@tonic-gate struct stat tmpBuf;
3850Sstevel@tonic-gate FILE *fd = NULL;
3860Sstevel@tonic-gate
3872264Sjacobs if ((attrs != NULL) && (*attrs != NULL) && (file != NULL)) {
3880Sstevel@tonic-gate
3890Sstevel@tonic-gate /*
3900Sstevel@tonic-gate * check file exist before try to modify it, if it doesn't
3910Sstevel@tonic-gate * exist assume there is an error
3920Sstevel@tonic-gate */
3932264Sjacobs if (stat(file, &tmpBuf) == 0) {
3940Sstevel@tonic-gate /*
3950Sstevel@tonic-gate * if file is currently empty just write the given
3960Sstevel@tonic-gate * attributes to the file otherwise exact the attributes
3970Sstevel@tonic-gate * from the file and modify them accordingly before
3980Sstevel@tonic-gate * writing them back to the file
3990Sstevel@tonic-gate */
4002264Sjacobs if (tmpBuf.st_size == 0) {
4010Sstevel@tonic-gate newAttrs = (papi_attribute_t **)attrs;
4020Sstevel@tonic-gate
4030Sstevel@tonic-gate fd = fopen(file, "w");
4042264Sjacobs if (fd != NULL) {
4050Sstevel@tonic-gate papiAttributeListPrint(fd,
4062264Sjacobs newAttrs, "");
4070Sstevel@tonic-gate fclose(fd);
4082264Sjacobs } else {
4090Sstevel@tonic-gate result = PAPI_NOT_POSSIBLE;
4100Sstevel@tonic-gate }
4112264Sjacobs } else {
4120Sstevel@tonic-gate result =
4130Sstevel@tonic-gate psm_modifyAttrsList(file, attrs, &newAttrs);
4140Sstevel@tonic-gate
4150Sstevel@tonic-gate fd = fopen(file, "w");
4162264Sjacobs if (fd != NULL) {
4170Sstevel@tonic-gate papiAttributeListPrint(fd,
4182264Sjacobs newAttrs, "");
4190Sstevel@tonic-gate fclose(fd);
4202264Sjacobs } else {
4210Sstevel@tonic-gate result = PAPI_NOT_POSSIBLE;
4220Sstevel@tonic-gate }
4230Sstevel@tonic-gate
4240Sstevel@tonic-gate papiAttributeListFree(newAttrs);
4250Sstevel@tonic-gate }
4262264Sjacobs } else {
4270Sstevel@tonic-gate result = PAPI_NOT_POSSIBLE;
4280Sstevel@tonic-gate }
4290Sstevel@tonic-gate }
4300Sstevel@tonic-gate
4310Sstevel@tonic-gate return (result);
4320Sstevel@tonic-gate } /* psm_modifyAttrsFile */
4330Sstevel@tonic-gate
4340Sstevel@tonic-gate
4350Sstevel@tonic-gate /*
4360Sstevel@tonic-gate * *****************************************************************************
4370Sstevel@tonic-gate *
4380Sstevel@tonic-gate * Description: Extracts the attributes in the given attribute 'file' and
4390Sstevel@tonic-gate * creates a new list 'newAttrs' containing the modified list of
4400Sstevel@tonic-gate * attributes.
4410Sstevel@tonic-gate *
4420Sstevel@tonic-gate * Parameters: file - pathname of file containing attributes to be modified
4430Sstevel@tonic-gate * attrs - list of attributes and their values to modify
4440Sstevel@tonic-gate * newAttrs - returns the modified list of attributes
4450Sstevel@tonic-gate *
4460Sstevel@tonic-gate * *****************************************************************************
4470Sstevel@tonic-gate */
4480Sstevel@tonic-gate
4490Sstevel@tonic-gate static papi_status_t
psm_modifyAttrsList(char * file,papi_attribute_t ** attrs,papi_attribute_t *** newAttrs)4502264Sjacobs psm_modifyAttrsList(char *file, papi_attribute_t **attrs,
4510Sstevel@tonic-gate papi_attribute_t ***newAttrs)
4520Sstevel@tonic-gate
4530Sstevel@tonic-gate {
4540Sstevel@tonic-gate papi_status_t result = PAPI_OK;
4550Sstevel@tonic-gate papi_attribute_t *nextAttr = NULL;
4560Sstevel@tonic-gate papi_attribute_value_t **values = NULL;
4570Sstevel@tonic-gate void *iter = NULL;
4580Sstevel@tonic-gate FILE *fd = NULL;
4590Sstevel@tonic-gate register int fD = 0;
4600Sstevel@tonic-gate char aBuff[200];
4610Sstevel@tonic-gate char *a = NULL;
4620Sstevel@tonic-gate char *p = NULL;
4630Sstevel@tonic-gate int count = 0;
4640Sstevel@tonic-gate int n = 0;
4650Sstevel@tonic-gate
4660Sstevel@tonic-gate fd = fopen(file, "r");
4672264Sjacobs if (fd != NULL) {
4680Sstevel@tonic-gate fD = fileno(fd);
4690Sstevel@tonic-gate a = &aBuff[0];
4700Sstevel@tonic-gate p = &aBuff[0];
4710Sstevel@tonic-gate count = read(fD, &aBuff[0], sizeof (aBuff) - 1);
4722264Sjacobs while ((result == PAPI_OK) && (count > 0)) {
4730Sstevel@tonic-gate aBuff[count+n] = '\0';
4742264Sjacobs if (count == sizeof (aBuff) - n - 1) {
4750Sstevel@tonic-gate p = strrchr(aBuff, '\n');
4762264Sjacobs if (p != NULL) {
4770Sstevel@tonic-gate /* terminate at last complete line */
4780Sstevel@tonic-gate *p = '\0';
4790Sstevel@tonic-gate }
4800Sstevel@tonic-gate }
4810Sstevel@tonic-gate result = papiAttributeListFromString(
4820Sstevel@tonic-gate newAttrs, PAPI_ATTR_EXCL, aBuff);
4830Sstevel@tonic-gate
4842264Sjacobs if (result == PAPI_OK) {
4850Sstevel@tonic-gate /*
4860Sstevel@tonic-gate * handle any part lines and then read the next
4870Sstevel@tonic-gate * buffer from the file
4880Sstevel@tonic-gate */
4890Sstevel@tonic-gate n = 0;
4902264Sjacobs if (p != a) {
4910Sstevel@tonic-gate p++; /* skip NL */
4920Sstevel@tonic-gate n = sizeof (aBuff) - 1 - (p - a);
4930Sstevel@tonic-gate strncpy(aBuff, p, n);
4940Sstevel@tonic-gate }
4950Sstevel@tonic-gate count = read(fD, &aBuff[n],
4960Sstevel@tonic-gate sizeof (aBuff) - n - 1);
4970Sstevel@tonic-gate p = &aBuff[0];
4980Sstevel@tonic-gate }
4990Sstevel@tonic-gate }
5000Sstevel@tonic-gate fclose(fd);
5010Sstevel@tonic-gate }
5020Sstevel@tonic-gate
5030Sstevel@tonic-gate /* now modify the attribute list with the new attributes in 'attrs' */
5040Sstevel@tonic-gate
5050Sstevel@tonic-gate nextAttr = papiAttributeListGetNext((papi_attribute_t **)attrs, &iter);
5062264Sjacobs while ((result == PAPI_OK) && (nextAttr != NULL)) {
5070Sstevel@tonic-gate values = nextAttr->values;
5080Sstevel@tonic-gate
5092264Sjacobs if ((values != NULL) && (*values != NULL)) {
5102264Sjacobs result = papiAttributeListAddValue(newAttrs,
5110Sstevel@tonic-gate PAPI_ATTR_REPLACE,
5120Sstevel@tonic-gate nextAttr->name,
5130Sstevel@tonic-gate nextAttr->type, *values);
5140Sstevel@tonic-gate values++;
5150Sstevel@tonic-gate }
5160Sstevel@tonic-gate
5170Sstevel@tonic-gate while ((result == PAPI_OK) &&
5182264Sjacobs (values != NULL) && (*values != NULL)) {
5192264Sjacobs result = papiAttributeListAddValue(newAttrs,
5200Sstevel@tonic-gate PAPI_ATTR_APPEND,
5210Sstevel@tonic-gate nextAttr->name,
5220Sstevel@tonic-gate nextAttr->type, *values);
5230Sstevel@tonic-gate values++;
5240Sstevel@tonic-gate }
5250Sstevel@tonic-gate nextAttr =
5260Sstevel@tonic-gate papiAttributeListGetNext((papi_attribute_t **)attrs, &iter);
5270Sstevel@tonic-gate }
5280Sstevel@tonic-gate
5290Sstevel@tonic-gate return (result);
5300Sstevel@tonic-gate } /* papi_modifyAttrsList() */
5310Sstevel@tonic-gate #endif
5320Sstevel@tonic-gate
5330Sstevel@tonic-gate
5340Sstevel@tonic-gate papi_status_t
papiJobSubmit(papi_service_t handle,char * printer,papi_attribute_t ** job_attributes,papi_job_ticket_t * job_ticket,char ** files,papi_job_t * job)5352264Sjacobs papiJobSubmit(papi_service_t handle, char *printer,
5362264Sjacobs papi_attribute_t **job_attributes,
5372264Sjacobs papi_job_ticket_t *job_ticket,
5382264Sjacobs char **files, papi_job_t *job)
5390Sstevel@tonic-gate {
5400Sstevel@tonic-gate papi_status_t status;
5410Sstevel@tonic-gate service_t *svc = handle;
5428569SJonathan.Ca@Sun.COM struct stat statbuf;
5430Sstevel@tonic-gate job_t *j;
5440Sstevel@tonic-gate int file_no;
5450Sstevel@tonic-gate char *request_id = NULL;
5460Sstevel@tonic-gate REQUEST *request;
5470Sstevel@tonic-gate int i;
5480Sstevel@tonic-gate char *c;
5490Sstevel@tonic-gate char *tmp = NULL;
5500Sstevel@tonic-gate char lpfile[BUFSIZ];
5510Sstevel@tonic-gate
5520Sstevel@tonic-gate if ((svc == NULL) || (printer == NULL) || (files == NULL) ||
5530Sstevel@tonic-gate (job == NULL))
5540Sstevel@tonic-gate return (PAPI_BAD_ARGUMENT);
5550Sstevel@tonic-gate
5560Sstevel@tonic-gate if (job_ticket != NULL)
5570Sstevel@tonic-gate return (PAPI_OPERATION_NOT_SUPPORTED);
5580Sstevel@tonic-gate
5590Sstevel@tonic-gate if (files != NULL)
5608569SJonathan.Ca@Sun.COM for (file_no = 0; files[file_no] != NULL; file_no++) {
5610Sstevel@tonic-gate if (access(files[file_no], R_OK) < 0) {
5620Sstevel@tonic-gate detailed_error(svc,
5638569SJonathan.Ca@Sun.COM gettext("Cannot access file: %s: %s"),
5648569SJonathan.Ca@Sun.COM files[file_no], strerror(errno));
5650Sstevel@tonic-gate return (PAPI_BAD_ARGUMENT);
5660Sstevel@tonic-gate }
5679224SJonathan.Ca@Sun.COM if (stat(files[file_no], &statbuf) < 0) {
5689224SJonathan.Ca@Sun.COM detailed_error(svc,
5699224SJonathan.Ca@Sun.COM gettext("Cannot access file: %s: %s"),
5709224SJonathan.Ca@Sun.COM files[file_no], strerror(errno));
5719224SJonathan.Ca@Sun.COM return (PAPI_DOCUMENT_ACCESS_ERROR);
5729224SJonathan.Ca@Sun.COM }
5738569SJonathan.Ca@Sun.COM if (statbuf.st_size == 0) {
5748569SJonathan.Ca@Sun.COM detailed_error(svc,
5758569SJonathan.Ca@Sun.COM gettext("Zero byte (empty) file: %s"),
5768569SJonathan.Ca@Sun.COM files[file_no]);
5778569SJonathan.Ca@Sun.COM return (PAPI_BAD_ARGUMENT);
5788569SJonathan.Ca@Sun.COM }
5798569SJonathan.Ca@Sun.COM }
5800Sstevel@tonic-gate
5810Sstevel@tonic-gate if ((*job = j = calloc(1, sizeof (*j))) == NULL)
5820Sstevel@tonic-gate return (PAPI_TEMPORARY_ERROR);
5830Sstevel@tonic-gate
5840Sstevel@tonic-gate /* file_no + 1 for the control file (-0) */
5850Sstevel@tonic-gate status = lpsched_alloc_files(svc, file_no + 1, &request_id);
5860Sstevel@tonic-gate if (status != PAPI_OK)
5870Sstevel@tonic-gate return (status);
5880Sstevel@tonic-gate
5890Sstevel@tonic-gate request = create_request(svc, (char *)printer,
5908569SJonathan.Ca@Sun.COM (papi_attribute_t **)job_attributes);
5910Sstevel@tonic-gate
5920Sstevel@tonic-gate for (i = 0; files[i] != NULL; i++) {
5930Sstevel@tonic-gate papi_status_t status;
5940Sstevel@tonic-gate snprintf(lpfile, sizeof (lpfile), "%s%s-%d",
5958569SJonathan.Ca@Sun.COM "/var/spool/lp/temp/", request_id, i+1);
5960Sstevel@tonic-gate status = copy_file(files[i], lpfile);
5970Sstevel@tonic-gate if (status != PAPI_OK) {
5980Sstevel@tonic-gate detailed_error(svc,
5998569SJonathan.Ca@Sun.COM gettext("unable to copy: %s -> %s: %s"),
6008569SJonathan.Ca@Sun.COM files[i], lpfile, strerror(errno));
6010Sstevel@tonic-gate freerequest(request);
6020Sstevel@tonic-gate return (PAPI_DEVICE_ERROR);
6030Sstevel@tonic-gate }
6040Sstevel@tonic-gate addlist(&(request->file_list), lpfile);
6050Sstevel@tonic-gate }
6060Sstevel@tonic-gate
6070Sstevel@tonic-gate #ifdef LP_USE_PAPI_ATTR
6080Sstevel@tonic-gate /*
6090Sstevel@tonic-gate * store the job attributes in the PAPI job attribute file that was
6100Sstevel@tonic-gate * created by lpsched_alloc_files(), the attributes will then pass
6110Sstevel@tonic-gate * through lpsched and be given to the slow-filters and the printer's
6120Sstevel@tonic-gate * interface script to process them
6130Sstevel@tonic-gate */
6140Sstevel@tonic-gate snprintf(lpfile, sizeof (lpfile), "%s%s-%s",
6158569SJonathan.Ca@Sun.COM "/var/spool/lp/temp/", request_id, LP_PAPIATTRNAME);
6160Sstevel@tonic-gate status = psm_copy_attrsToFile(job_attributes, lpfile);
6170Sstevel@tonic-gate if (status != PAPI_OK) {
6180Sstevel@tonic-gate detailed_error(svc, "unable to copy attributes to file: %s: %s",
6198569SJonathan.Ca@Sun.COM lpfile, strerror(errno));
6200Sstevel@tonic-gate return (PAPI_DEVICE_ERROR);
6210Sstevel@tonic-gate }
6220Sstevel@tonic-gate #endif
6230Sstevel@tonic-gate
6240Sstevel@tonic-gate /* store the meta-data file */
6250Sstevel@tonic-gate snprintf(lpfile, sizeof (lpfile), "%s-0", request_id);
6260Sstevel@tonic-gate if (putrequest(lpfile, request) < 0) {
6270Sstevel@tonic-gate detailed_error(svc, gettext("unable to save request: %s: %s"),
6288569SJonathan.Ca@Sun.COM lpfile, strerror(errno));
6290Sstevel@tonic-gate freerequest(request);
6300Sstevel@tonic-gate return (PAPI_DEVICE_ERROR);
6310Sstevel@tonic-gate }
6320Sstevel@tonic-gate
6330Sstevel@tonic-gate status = lpsched_commit_job(svc, lpfile, &tmp);
6340Sstevel@tonic-gate if (status != PAPI_OK) {
6350Sstevel@tonic-gate unlink(lpfile);
6360Sstevel@tonic-gate freerequest(request);
6370Sstevel@tonic-gate return (status);
6380Sstevel@tonic-gate }
6390Sstevel@tonic-gate
6400Sstevel@tonic-gate lpsched_request_to_job_attributes(request, j);
6410Sstevel@tonic-gate freerequest(request);
6420Sstevel@tonic-gate
6430Sstevel@tonic-gate if ((c = strrchr(tmp, '-')) != NULL)
6440Sstevel@tonic-gate c++;
6450Sstevel@tonic-gate papiAttributeListAddInteger(&j->attributes, PAPI_ATTR_REPLACE,
6468569SJonathan.Ca@Sun.COM "job-id", atoi(c));
6470Sstevel@tonic-gate papiAttributeListAddString(&j->attributes, PAPI_ATTR_REPLACE,
6488569SJonathan.Ca@Sun.COM "job-uri", tmp);
6490Sstevel@tonic-gate
6500Sstevel@tonic-gate return (PAPI_OK);
6510Sstevel@tonic-gate }
6520Sstevel@tonic-gate
6530Sstevel@tonic-gate papi_status_t
papiJobSubmitByReference(papi_service_t handle,char * printer,papi_attribute_t ** job_attributes,papi_job_ticket_t * job_ticket,char ** files,papi_job_t * job)6542264Sjacobs papiJobSubmitByReference(papi_service_t handle, char *printer,
6552264Sjacobs papi_attribute_t **job_attributes,
6562264Sjacobs papi_job_ticket_t *job_ticket,
6572264Sjacobs char **files, papi_job_t *job)
6580Sstevel@tonic-gate {
6590Sstevel@tonic-gate service_t *svc = handle;
6608569SJonathan.Ca@Sun.COM struct stat statbuf;
6610Sstevel@tonic-gate job_t *j;
6620Sstevel@tonic-gate int file_no;
6630Sstevel@tonic-gate short status;
6640Sstevel@tonic-gate char *request_id = NULL;
6650Sstevel@tonic-gate REQUEST *request;
6660Sstevel@tonic-gate char *c;
6670Sstevel@tonic-gate char *tmp = NULL;
6680Sstevel@tonic-gate char lpfile[BUFSIZ];
6690Sstevel@tonic-gate char **file_list = NULL;
6700Sstevel@tonic-gate
6710Sstevel@tonic-gate if ((svc == NULL) || (printer == NULL) || (files == NULL) ||
6720Sstevel@tonic-gate (job == NULL))
6730Sstevel@tonic-gate return (PAPI_BAD_ARGUMENT);
6740Sstevel@tonic-gate
6750Sstevel@tonic-gate if (job_ticket != NULL)
6760Sstevel@tonic-gate return (PAPI_OPERATION_NOT_SUPPORTED);
6770Sstevel@tonic-gate
6780Sstevel@tonic-gate if (files != NULL)
6790Sstevel@tonic-gate for (file_no = 0; files[file_no] != NULL; file_no++) {
6800Sstevel@tonic-gate if (access(files[file_no], R_OK) < 0) {
6810Sstevel@tonic-gate detailed_error(svc,
6828569SJonathan.Ca@Sun.COM gettext("Cannot access file: %s: %s"),
6838569SJonathan.Ca@Sun.COM files[file_no], strerror(errno));
6842660Sjacobs return (PAPI_DOCUMENT_ACCESS_ERROR);
6850Sstevel@tonic-gate }
6869224SJonathan.Ca@Sun.COM if (stat(files[file_no], &statbuf) < 0) {
6879224SJonathan.Ca@Sun.COM detailed_error(svc,
6889224SJonathan.Ca@Sun.COM gettext("Cannot access file: %s: %s"),
6899224SJonathan.Ca@Sun.COM files[file_no], strerror(errno));
6909224SJonathan.Ca@Sun.COM return (PAPI_DOCUMENT_ACCESS_ERROR);
6919224SJonathan.Ca@Sun.COM }
6928569SJonathan.Ca@Sun.COM if (statbuf.st_size == 0) {
6938569SJonathan.Ca@Sun.COM detailed_error(svc,
6948569SJonathan.Ca@Sun.COM gettext("Zero byte (empty) file: %s"),
6958569SJonathan.Ca@Sun.COM files[file_no]);
6968569SJonathan.Ca@Sun.COM return (PAPI_BAD_ARGUMENT);
6978569SJonathan.Ca@Sun.COM }
6988569SJonathan.Ca@Sun.COM
6992660Sjacobs if (files[file_no][0] != '/') {
7002660Sjacobs char path[MAXPATHLEN];
7012660Sjacobs
7022660Sjacobs if (getcwd(path, sizeof (path)) == NULL) {
7032660Sjacobs detailed_error(svc, gettext(
7048569SJonathan.Ca@Sun.COM "getcwd for file: %s: %s"),
7058569SJonathan.Ca@Sun.COM files[file_no],
7068569SJonathan.Ca@Sun.COM strerror(errno));
7072660Sjacobs return (PAPI_DOCUMENT_ACCESS_ERROR);
7082660Sjacobs }
7092660Sjacobs strlcat(path, "/", sizeof (path));
7102660Sjacobs if (strlcat(path, files[file_no], sizeof (path))
7118569SJonathan.Ca@Sun.COM >= sizeof (path)) {
7122660Sjacobs detailed_error(svc, gettext(
7138569SJonathan.Ca@Sun.COM "pathname too long: %s"),
7148569SJonathan.Ca@Sun.COM files[file_no]);
7152660Sjacobs return (PAPI_DOCUMENT_ACCESS_ERROR);
7162660Sjacobs }
7172660Sjacobs addlist(&file_list, path);
7182660Sjacobs } else
7192660Sjacobs addlist(&file_list, (char *)files[file_no]);
7200Sstevel@tonic-gate }
7210Sstevel@tonic-gate
7220Sstevel@tonic-gate if ((*job = j = calloc(1, sizeof (*j))) == NULL)
7230Sstevel@tonic-gate return (PAPI_TEMPORARY_ERROR);
7240Sstevel@tonic-gate
7250Sstevel@tonic-gate /* 1 for the control file (-0) */
7260Sstevel@tonic-gate status = lpsched_alloc_files(svc, 1, &request_id);
7270Sstevel@tonic-gate if (status != PAPI_OK)
7280Sstevel@tonic-gate return (status);
7290Sstevel@tonic-gate
7300Sstevel@tonic-gate request = create_request(svc, (char *)printer,
7318569SJonathan.Ca@Sun.COM (papi_attribute_t **)job_attributes);
7320Sstevel@tonic-gate request->file_list = file_list;
7330Sstevel@tonic-gate
7340Sstevel@tonic-gate #ifdef LP_USE_PAPI_ATTR
7350Sstevel@tonic-gate /*
7360Sstevel@tonic-gate * store the job attributes in the PAPI job attribute file that was
7370Sstevel@tonic-gate * created by lpsched_alloc_files(), the attributes will then pass
7380Sstevel@tonic-gate * through lpsched and be given to the slow-filters and the printer's
7390Sstevel@tonic-gate * interface script to process them
7400Sstevel@tonic-gate */
7410Sstevel@tonic-gate snprintf(lpfile, sizeof (lpfile), "%s%s-%s",
7428569SJonathan.Ca@Sun.COM "/var/spool/lp/temp/", request_id, LP_PAPIATTRNAME);
7430Sstevel@tonic-gate status = psm_copy_attrsToFile(job_attributes, lpfile);
7440Sstevel@tonic-gate if (status != PAPI_OK) {
7450Sstevel@tonic-gate detailed_error(svc, "unable to copy attributes to file: %s: %s",
7468569SJonathan.Ca@Sun.COM lpfile, strerror(errno));
7470Sstevel@tonic-gate return (PAPI_DEVICE_ERROR);
7480Sstevel@tonic-gate }
7490Sstevel@tonic-gate #endif
7500Sstevel@tonic-gate
7510Sstevel@tonic-gate /* store the meta-data file */
7520Sstevel@tonic-gate snprintf(lpfile, sizeof (lpfile), "%s-0", request_id);
7530Sstevel@tonic-gate if (putrequest(lpfile, request) < 0) {
7540Sstevel@tonic-gate detailed_error(svc, gettext("unable to save request: %s: %s"),
7558569SJonathan.Ca@Sun.COM lpfile, strerror(errno));
7560Sstevel@tonic-gate freerequest(request);
7570Sstevel@tonic-gate return (PAPI_DEVICE_ERROR);
7580Sstevel@tonic-gate }
7590Sstevel@tonic-gate
7600Sstevel@tonic-gate status = lpsched_commit_job(svc, lpfile, &tmp);
7610Sstevel@tonic-gate if (status != PAPI_OK) {
7620Sstevel@tonic-gate unlink(lpfile);
7630Sstevel@tonic-gate freerequest(request);
7640Sstevel@tonic-gate return (status);
7650Sstevel@tonic-gate }
7660Sstevel@tonic-gate
7670Sstevel@tonic-gate lpsched_request_to_job_attributes(request, j);
7680Sstevel@tonic-gate
7690Sstevel@tonic-gate freerequest(request);
7700Sstevel@tonic-gate
7710Sstevel@tonic-gate if ((c = strrchr(tmp, '-')) != NULL)
7720Sstevel@tonic-gate c++;
7730Sstevel@tonic-gate papiAttributeListAddInteger(&j->attributes, PAPI_ATTR_REPLACE,
7748569SJonathan.Ca@Sun.COM "job-id", atoi(c));
7750Sstevel@tonic-gate papiAttributeListAddString(&j->attributes, PAPI_ATTR_REPLACE,
7768569SJonathan.Ca@Sun.COM "job-uri", tmp);
7770Sstevel@tonic-gate
7780Sstevel@tonic-gate return (PAPI_OK);
7790Sstevel@tonic-gate }
7800Sstevel@tonic-gate
7810Sstevel@tonic-gate papi_status_t
papiJobValidate(papi_service_t handle,char * printer,papi_attribute_t ** job_attributes,papi_job_ticket_t * job_ticket,char ** files,papi_job_t * job)7822264Sjacobs papiJobValidate(papi_service_t handle, char *printer,
7832264Sjacobs papi_attribute_t **job_attributes,
7842264Sjacobs papi_job_ticket_t *job_ticket,
7852264Sjacobs char **files, papi_job_t *job)
7860Sstevel@tonic-gate {
7870Sstevel@tonic-gate papi_status_t status;
7880Sstevel@tonic-gate papi_attribute_t **attributes = NULL;
7890Sstevel@tonic-gate int i;
7900Sstevel@tonic-gate
7910Sstevel@tonic-gate papiAttributeListAddString(&attributes, PAPI_ATTR_REPLACE,
7928569SJonathan.Ca@Sun.COM "job-hold-until", "indefinite");
7930Sstevel@tonic-gate for (i = 0; job_attributes[i]; i++)
7940Sstevel@tonic-gate list_append(&attributes, job_attributes[i]);
7950Sstevel@tonic-gate
7960Sstevel@tonic-gate status = papiJobSubmitByReference(handle, printer,
7978569SJonathan.Ca@Sun.COM (papi_attribute_t **)attributes,
7988569SJonathan.Ca@Sun.COM job_ticket, files, job);
7990Sstevel@tonic-gate if (status == PAPI_OK) {
8000Sstevel@tonic-gate int id = papiJobGetId(*job);
8010Sstevel@tonic-gate
8020Sstevel@tonic-gate if (id != -1)
8030Sstevel@tonic-gate papiJobCancel(handle, printer, id);
8040Sstevel@tonic-gate }
8050Sstevel@tonic-gate
8060Sstevel@tonic-gate attributes[1] = NULL; /* after attr[0], they are in another list */
8070Sstevel@tonic-gate papiAttributeListFree(attributes);
8080Sstevel@tonic-gate
8090Sstevel@tonic-gate return (status);
8100Sstevel@tonic-gate }
8110Sstevel@tonic-gate
8120Sstevel@tonic-gate papi_status_t
papiJobStreamOpen(papi_service_t handle,char * printer,papi_attribute_t ** job_attributes,papi_job_ticket_t * job_ticket,papi_stream_t * stream)8132264Sjacobs papiJobStreamOpen(papi_service_t handle, char *printer,
8142264Sjacobs papi_attribute_t **job_attributes,
8152264Sjacobs papi_job_ticket_t *job_ticket, papi_stream_t *stream)
8160Sstevel@tonic-gate {
8170Sstevel@tonic-gate papi_status_t status;
8180Sstevel@tonic-gate service_t *svc = handle;
8190Sstevel@tonic-gate job_stream_t *s = NULL;
8200Sstevel@tonic-gate char *request_id = NULL;
8210Sstevel@tonic-gate char lpfile[BUFSIZ];
8220Sstevel@tonic-gate
8230Sstevel@tonic-gate if ((svc == NULL) || (printer == NULL) || (stream == NULL))
8240Sstevel@tonic-gate return (PAPI_BAD_ARGUMENT);
8250Sstevel@tonic-gate
8260Sstevel@tonic-gate if (job_ticket != NULL)
8270Sstevel@tonic-gate return (PAPI_OPERATION_NOT_SUPPORTED);
8280Sstevel@tonic-gate
8290Sstevel@tonic-gate if ((*stream = s = calloc(1, sizeof (*s))) == NULL)
8300Sstevel@tonic-gate return (PAPI_TEMPORARY_ERROR);
8310Sstevel@tonic-gate
8320Sstevel@tonic-gate /* 1 for data, 1 for the meta-data (-0) */
8330Sstevel@tonic-gate status = lpsched_alloc_files(svc, 2, &request_id);
8340Sstevel@tonic-gate if (status != PAPI_OK)
8350Sstevel@tonic-gate return (status);
8360Sstevel@tonic-gate
837*12249SSonam.Gupta@Sun.COM papiAttributeListAddString(&job_attributes, PAPI_ATTR_EXCL,
838*12249SSonam.Gupta@Sun.COM "job-name", "standard input");
839*12249SSonam.Gupta@Sun.COM
8400Sstevel@tonic-gate s->request = create_request(svc, (char *)printer,
8418569SJonathan.Ca@Sun.COM (papi_attribute_t **)job_attributes);
8420Sstevel@tonic-gate snprintf(lpfile, sizeof (lpfile), "/var/spool/lp/temp/%s-1",
8438569SJonathan.Ca@Sun.COM request_id);
8440Sstevel@tonic-gate s->fd = open(lpfile, O_WRONLY);
8450Sstevel@tonic-gate addlist(&(s->request->file_list), lpfile);
8460Sstevel@tonic-gate
8470Sstevel@tonic-gate #ifdef LP_USE_PAPI_ATTR
8480Sstevel@tonic-gate /*
8490Sstevel@tonic-gate * store the job attributes in the PAPI job attribute file that was
8500Sstevel@tonic-gate * created by lpsched_alloc_files(), the attributes will then pass
8510Sstevel@tonic-gate * through lpsched and be given to the slow-filters and the printer's
8520Sstevel@tonic-gate * interface script to process them
8530Sstevel@tonic-gate */
8540Sstevel@tonic-gate snprintf(lpfile, sizeof (lpfile), "%s%s-%s",
8558569SJonathan.Ca@Sun.COM "/var/spool/lp/temp/", request_id, LP_PAPIATTRNAME);
8560Sstevel@tonic-gate status = psm_copy_attrsToFile(job_attributes, lpfile);
8570Sstevel@tonic-gate if (status != PAPI_OK) {
8580Sstevel@tonic-gate detailed_error(svc, "unable to copy attributes to file: %s: %s",
8598569SJonathan.Ca@Sun.COM lpfile, strerror(errno));
8600Sstevel@tonic-gate close(s->fd);
8610Sstevel@tonic-gate free(s);
8620Sstevel@tonic-gate return (PAPI_DEVICE_ERROR);
8630Sstevel@tonic-gate }
8640Sstevel@tonic-gate #endif
8650Sstevel@tonic-gate
8660Sstevel@tonic-gate /* store the meta-data file */
8670Sstevel@tonic-gate snprintf(lpfile, sizeof (lpfile), "%s-0", request_id);
8680Sstevel@tonic-gate s->meta_data_file = strdup(lpfile);
8690Sstevel@tonic-gate if (putrequest(lpfile, s->request) < 0) {
8700Sstevel@tonic-gate detailed_error(svc, gettext("unable to save request: %s: %s"),
8718569SJonathan.Ca@Sun.COM lpfile, strerror(errno));
8720Sstevel@tonic-gate s->request = NULL;
8730Sstevel@tonic-gate return (PAPI_DEVICE_ERROR);
8740Sstevel@tonic-gate }
8750Sstevel@tonic-gate
8760Sstevel@tonic-gate return (PAPI_OK);
8770Sstevel@tonic-gate }
8780Sstevel@tonic-gate
8790Sstevel@tonic-gate papi_status_t
papiJobStreamWrite(papi_service_t handle,papi_stream_t stream,void * buffer,size_t buflen)8800Sstevel@tonic-gate papiJobStreamWrite(papi_service_t handle,
8812264Sjacobs papi_stream_t stream, void *buffer, size_t buflen)
8820Sstevel@tonic-gate {
8830Sstevel@tonic-gate service_t *svc = handle;
8840Sstevel@tonic-gate job_stream_t *s = stream;
8850Sstevel@tonic-gate
8860Sstevel@tonic-gate if ((svc == NULL) || (stream == NULL) || (buffer == NULL))
8870Sstevel@tonic-gate return (PAPI_BAD_ARGUMENT);
8880Sstevel@tonic-gate
8890Sstevel@tonic-gate if (write(s->fd, buffer, buflen) != buflen)
8900Sstevel@tonic-gate return (PAPI_DEVICE_ERROR);
8910Sstevel@tonic-gate
8920Sstevel@tonic-gate return (PAPI_OK);
8930Sstevel@tonic-gate }
8940Sstevel@tonic-gate papi_status_t
papiJobStreamClose(papi_service_t handle,papi_stream_t stream,papi_job_t * job)8950Sstevel@tonic-gate papiJobStreamClose(papi_service_t handle,
8960Sstevel@tonic-gate papi_stream_t stream, papi_job_t *job)
8970Sstevel@tonic-gate {
8980Sstevel@tonic-gate papi_status_t status = PAPI_OK;
8990Sstevel@tonic-gate service_t *svc = handle;
9000Sstevel@tonic-gate job_stream_t *s = stream;
9010Sstevel@tonic-gate job_t *j = NULL;
9020Sstevel@tonic-gate char *tmp = NULL, *c;
9030Sstevel@tonic-gate
9040Sstevel@tonic-gate if ((svc == NULL) || (stream == NULL) || (job == NULL))
9050Sstevel@tonic-gate return (PAPI_BAD_ARGUMENT);
9060Sstevel@tonic-gate
9070Sstevel@tonic-gate if ((*job = j = calloc(1, sizeof (*j))) == NULL)
9080Sstevel@tonic-gate return (PAPI_TEMPORARY_ERROR);
9090Sstevel@tonic-gate
9100Sstevel@tonic-gate close(s->fd);
9110Sstevel@tonic-gate
9120Sstevel@tonic-gate lpsched_request_to_job_attributes(s->request, j);
9130Sstevel@tonic-gate
9140Sstevel@tonic-gate if (s->meta_data_file != NULL) {
9150Sstevel@tonic-gate status = lpsched_commit_job(svc, s->meta_data_file, &tmp);
9160Sstevel@tonic-gate if (status != PAPI_OK) {
9170Sstevel@tonic-gate unlink(s->meta_data_file);
9180Sstevel@tonic-gate return (status);
9190Sstevel@tonic-gate }
9200Sstevel@tonic-gate if ((c = strrchr(tmp, '-')) != NULL)
9210Sstevel@tonic-gate c++;
9220Sstevel@tonic-gate papiAttributeListAddInteger(&j->attributes, PAPI_ATTR_REPLACE,
9238569SJonathan.Ca@Sun.COM "job-id", atoi(c));
9240Sstevel@tonic-gate papiAttributeListAddString(&j->attributes, PAPI_ATTR_REPLACE,
9258569SJonathan.Ca@Sun.COM "job-uri", tmp);
9260Sstevel@tonic-gate free(s->meta_data_file);
9270Sstevel@tonic-gate }
9287253Sjacobs freerequest(s->request);
9290Sstevel@tonic-gate free(s);
9300Sstevel@tonic-gate
9310Sstevel@tonic-gate return (PAPI_OK);
9320Sstevel@tonic-gate }
9330Sstevel@tonic-gate
9340Sstevel@tonic-gate papi_status_t
papiJobQuery(papi_service_t handle,char * printer,int32_t job_id,char ** requested_attrs,papi_job_t * job)9352264Sjacobs papiJobQuery(papi_service_t handle, char *printer, int32_t job_id,
9362264Sjacobs char **requested_attrs,
9370Sstevel@tonic-gate papi_job_t *job)
9380Sstevel@tonic-gate {
9390Sstevel@tonic-gate service_t *svc = handle;
9400Sstevel@tonic-gate job_t *j;
9410Sstevel@tonic-gate char *dest;
9420Sstevel@tonic-gate char req_id[32];
9430Sstevel@tonic-gate short rc;
9440Sstevel@tonic-gate char *form = NULL,
9458569SJonathan.Ca@Sun.COM *request_id = NULL,
9468569SJonathan.Ca@Sun.COM *charset = NULL,
9478569SJonathan.Ca@Sun.COM *user = NULL,
9488569SJonathan.Ca@Sun.COM *slabel = NULL,
9498569SJonathan.Ca@Sun.COM *file = NULL;
9500Sstevel@tonic-gate time_t date = 0;
9510Sstevel@tonic-gate size_t size = 0;
9520Sstevel@tonic-gate short rank = 0,
9538569SJonathan.Ca@Sun.COM state = 0;
9540Sstevel@tonic-gate
9550Sstevel@tonic-gate if ((handle == NULL) || (printer == NULL) || (job_id < 0))
9560Sstevel@tonic-gate return (PAPI_BAD_ARGUMENT);
9570Sstevel@tonic-gate
9580Sstevel@tonic-gate dest = printer_name_from_uri_id(printer, job_id);
9590Sstevel@tonic-gate snprintf(req_id, sizeof (req_id), "%s-%d", dest, job_id);
9600Sstevel@tonic-gate free(dest);
9610Sstevel@tonic-gate
9620Sstevel@tonic-gate rc = snd_msg(svc, S_INQUIRE_REQUEST_RANK, 0, "", "", req_id, "", "");
9630Sstevel@tonic-gate if (rc < 0)
9640Sstevel@tonic-gate return (PAPI_SERVICE_UNAVAILABLE);
9650Sstevel@tonic-gate
9660Sstevel@tonic-gate if (rcv_msg(svc, R_INQUIRE_REQUEST_RANK, &rc, &request_id,
9678569SJonathan.Ca@Sun.COM &user, &slabel, &size, &date, &state, &dest, &form,
9688569SJonathan.Ca@Sun.COM &charset, &rank, &file) < 0) {
9690Sstevel@tonic-gate detailed_error(svc,
9708569SJonathan.Ca@Sun.COM gettext("failed to read response from scheduler"));
9710Sstevel@tonic-gate return (PAPI_DEVICE_ERROR);
9720Sstevel@tonic-gate }
9730Sstevel@tonic-gate
9740Sstevel@tonic-gate if ((request_id == NULL) || (request_id[0] == NULL))
9750Sstevel@tonic-gate return (PAPI_NOT_FOUND);
9760Sstevel@tonic-gate
9770Sstevel@tonic-gate if ((*job = j = calloc(1, sizeof (*j))) == NULL)
9780Sstevel@tonic-gate return (PAPI_TEMPORARY_ERROR);
9790Sstevel@tonic-gate
98010781SGowtham.Thommandra@Sun.COM snprintf(req_id, sizeof (req_id), "%d-0", job_id);
98110781SGowtham.Thommandra@Sun.COM lpsched_read_job_configuration(svc, j, req_id);
98210781SGowtham.Thommandra@Sun.COM
9831676Sjpk job_status_to_attributes(j, request_id, user, slabel, size, date, state,
9848569SJonathan.Ca@Sun.COM dest, form, charset, rank, file);
9850Sstevel@tonic-gate
9860Sstevel@tonic-gate return (PAPI_OK);
9870Sstevel@tonic-gate }
9880Sstevel@tonic-gate
9890Sstevel@tonic-gate papi_status_t
papiJobMove(papi_service_t handle,char * printer,int32_t job_id,char * destination)9902264Sjacobs papiJobMove(papi_service_t handle, char *printer, int32_t job_id,
9912264Sjacobs char *destination)
9922264Sjacobs {
9932264Sjacobs papi_status_t result = PAPI_OK;
9948266SNagaraj.Yedathore@Sun.COM long bits;
9952264Sjacobs service_t *svc = handle;
9962264Sjacobs char req_id[64];
9972264Sjacobs char *queue;
9982264Sjacobs char *user = NULL;
9992264Sjacobs
10002264Sjacobs if ((svc == NULL) || (printer == NULL) || (job_id < 0) ||
10012264Sjacobs (destination == NULL))
10022264Sjacobs return (PAPI_BAD_ARGUMENT);
10032264Sjacobs
10042264Sjacobs queue = printer_name_from_uri_id(printer, job_id);
10052264Sjacobs snprintf(req_id, sizeof (req_id), "%s-%d", queue, job_id);
10062264Sjacobs free(queue);
10072264Sjacobs
10082264Sjacobs if (papiAttributeListGetString(svc->attributes, NULL, "user-name",
10098569SJonathan.Ca@Sun.COM &user) == PAPI_OK) {
10102264Sjacobs REQUEST *r = getrequest(req_id);
10112264Sjacobs
10122264Sjacobs if ((r != NULL) && (r->user != NULL) &&
10132264Sjacobs (strcmp(r->user, user) != 0))
10142264Sjacobs result = PAPI_NOT_AUTHORIZED;
10152264Sjacobs freerequest(r);
10162264Sjacobs }
10172264Sjacobs
10182264Sjacobs if (result == PAPI_OK) {
10192264Sjacobs short status = MOK;
10202264Sjacobs char *dest = printer_name_from_uri_id(destination, -1);
10212264Sjacobs
10222264Sjacobs if ((snd_msg(svc, S_MOVE_REQUEST, req_id, dest) < 0) ||
10238266SNagaraj.Yedathore@Sun.COM (rcv_msg(svc, R_MOVE_REQUEST, &status, &bits) < 0))
10242264Sjacobs status = MTRANSMITERR;
10252264Sjacobs
10262264Sjacobs free(dest);
10272264Sjacobs
10282264Sjacobs result = lpsched_status_to_papi_status(status);
10292264Sjacobs }
10302264Sjacobs
10312264Sjacobs return (result);
10322264Sjacobs }
10332264Sjacobs
10342264Sjacobs papi_status_t
papiJobCancel(papi_service_t handle,char * printer,int32_t job_id)10352264Sjacobs papiJobCancel(papi_service_t handle, char *printer, int32_t job_id)
10360Sstevel@tonic-gate {
10370Sstevel@tonic-gate papi_status_t result = PAPI_OK;
10380Sstevel@tonic-gate service_t *svc = handle;
10390Sstevel@tonic-gate char req_id[64];
10400Sstevel@tonic-gate char *dest;
10410Sstevel@tonic-gate char *user = NULL;
10420Sstevel@tonic-gate
10430Sstevel@tonic-gate if ((svc == NULL) || (printer == NULL) || (job_id < 0))
10440Sstevel@tonic-gate return (PAPI_BAD_ARGUMENT);
10450Sstevel@tonic-gate
10460Sstevel@tonic-gate dest = printer_name_from_uri_id(printer, job_id);
10470Sstevel@tonic-gate snprintf(req_id, sizeof (req_id), "%s-%d", dest, job_id);
10480Sstevel@tonic-gate free(dest);
10490Sstevel@tonic-gate
10500Sstevel@tonic-gate if (papiAttributeListGetString(svc->attributes, NULL, "user-name",
10518569SJonathan.Ca@Sun.COM &user) == PAPI_OK) {
10520Sstevel@tonic-gate REQUEST *r = getrequest(req_id);
10530Sstevel@tonic-gate
10548966SSonam.Gupta@Sun.COM if ((result = authorized(handle, job_id)) != PAPI_OK)
10558966SSonam.Gupta@Sun.COM result = PAPI_NOT_AUTHORIZED;
10568966SSonam.Gupta@Sun.COM
10572264Sjacobs if ((r != NULL) && (r->user != NULL) &&
10582264Sjacobs (strcmp(r->user, user) != 0))
10590Sstevel@tonic-gate result = PAPI_NOT_AUTHORIZED;
10600Sstevel@tonic-gate freerequest(r);
10610Sstevel@tonic-gate }
10620Sstevel@tonic-gate
10630Sstevel@tonic-gate if (result == PAPI_OK) {
10640Sstevel@tonic-gate short status = MOK;
10650Sstevel@tonic-gate
10660Sstevel@tonic-gate if ((snd_msg(svc, S_CANCEL_REQUEST, req_id) < 0) ||
10670Sstevel@tonic-gate (rcv_msg(svc, R_CANCEL_REQUEST, &status) < 0))
10680Sstevel@tonic-gate status = MTRANSMITERR;
10690Sstevel@tonic-gate
10700Sstevel@tonic-gate result = lpsched_status_to_papi_status(status);
10710Sstevel@tonic-gate }
10720Sstevel@tonic-gate
10730Sstevel@tonic-gate return (result);
10740Sstevel@tonic-gate }
10750Sstevel@tonic-gate
10760Sstevel@tonic-gate papi_status_t
hold_release_job(papi_service_t handle,char * printer,int32_t job_id,int flag)10772264Sjacobs hold_release_job(papi_service_t handle, char *printer,
10782264Sjacobs int32_t job_id, int flag)
10790Sstevel@tonic-gate {
10800Sstevel@tonic-gate papi_status_t status;
10810Sstevel@tonic-gate service_t *svc = handle;
10820Sstevel@tonic-gate REQUEST *r = NULL;
10830Sstevel@tonic-gate char *file;
10840Sstevel@tonic-gate char *dest;
10850Sstevel@tonic-gate
10860Sstevel@tonic-gate if ((svc == NULL) || (printer == NULL) || (job_id < 0))
10870Sstevel@tonic-gate return (PAPI_BAD_ARGUMENT);
10880Sstevel@tonic-gate
10890Sstevel@tonic-gate if ((status = authorized(svc, job_id)) != PAPI_OK)
10900Sstevel@tonic-gate return (status);
10910Sstevel@tonic-gate
10920Sstevel@tonic-gate dest = printer_name_from_uri_id(printer, job_id);
10930Sstevel@tonic-gate status = lpsched_start_change(svc, dest, job_id, &file);
10940Sstevel@tonic-gate if (status != PAPI_OK)
10950Sstevel@tonic-gate return (status);
10960Sstevel@tonic-gate
10970Sstevel@tonic-gate if ((r = getrequest(file)) != NULL) {
10980Sstevel@tonic-gate r->actions &= ~ACT_RESUME;
10992264Sjacobs switch (flag) {
11002264Sjacobs case 0:
11010Sstevel@tonic-gate r->actions |= ACT_HOLD;
11022264Sjacobs break;
11032264Sjacobs case 1:
11040Sstevel@tonic-gate r->actions |= ACT_RESUME;
11052264Sjacobs break;
11062264Sjacobs case 2:
11072264Sjacobs r->actions |= ACT_IMMEDIATE;
11082264Sjacobs break;
11092264Sjacobs }
11100Sstevel@tonic-gate if (putrequest(file, r) < 0) {
11110Sstevel@tonic-gate detailed_error(svc,
11128569SJonathan.Ca@Sun.COM gettext("failed to write job: %s: %s"),
11138569SJonathan.Ca@Sun.COM file, strerror(errno));
11142264Sjacobs freerequest(r);
11150Sstevel@tonic-gate return (PAPI_DEVICE_ERROR);
11160Sstevel@tonic-gate }
11172264Sjacobs freerequest(r);
11180Sstevel@tonic-gate } else {
11190Sstevel@tonic-gate detailed_error(svc, gettext("failed to read job: %s: %s"),
11208569SJonathan.Ca@Sun.COM file, strerror(errno));
11210Sstevel@tonic-gate return (PAPI_DEVICE_ERROR);
11220Sstevel@tonic-gate }
11230Sstevel@tonic-gate
11240Sstevel@tonic-gate status = lpsched_end_change(svc, dest, job_id);
11250Sstevel@tonic-gate
11260Sstevel@tonic-gate return (status);
11270Sstevel@tonic-gate }
11280Sstevel@tonic-gate
11290Sstevel@tonic-gate papi_status_t
papiJobHold(papi_service_t handle,char * printer,int32_t job_id)11302264Sjacobs papiJobHold(papi_service_t handle, char *printer, int32_t job_id)
11310Sstevel@tonic-gate {
11320Sstevel@tonic-gate return (hold_release_job(handle, printer, job_id, 0));
11330Sstevel@tonic-gate }
11340Sstevel@tonic-gate
11350Sstevel@tonic-gate papi_status_t
papiJobRelease(papi_service_t handle,char * printer,int32_t job_id)11362264Sjacobs papiJobRelease(papi_service_t handle, char *printer, int32_t job_id)
11370Sstevel@tonic-gate {
11380Sstevel@tonic-gate return (hold_release_job(handle, printer, job_id, 1));
11390Sstevel@tonic-gate }
11400Sstevel@tonic-gate
11410Sstevel@tonic-gate papi_status_t
papiJobPromote(papi_service_t handle,char * printer,int32_t job_id)11422264Sjacobs papiJobPromote(papi_service_t handle, char *printer, int32_t job_id)
11430Sstevel@tonic-gate {
11442264Sjacobs return (hold_release_job(handle, printer, job_id, 2));
11450Sstevel@tonic-gate }
11460Sstevel@tonic-gate
11470Sstevel@tonic-gate papi_status_t
papiJobModify(papi_service_t handle,char * printer,int32_t job_id,papi_attribute_t ** attributes,papi_job_t * job)11482264Sjacobs papiJobModify(papi_service_t handle, char *printer, int32_t job_id,
11492264Sjacobs papi_attribute_t **attributes, papi_job_t *job)
11500Sstevel@tonic-gate {
11510Sstevel@tonic-gate papi_status_t status;
11520Sstevel@tonic-gate job_t *j = NULL;
11530Sstevel@tonic-gate service_t *svc = handle;
11540Sstevel@tonic-gate char *file = NULL;
11550Sstevel@tonic-gate char *dest;
11560Sstevel@tonic-gate REQUEST *r = NULL;
11570Sstevel@tonic-gate char lpfile[BUFSIZ];
115812091SSonam.Gupta@Sun.COM int32_t job_id_actual;
11590Sstevel@tonic-gate
11600Sstevel@tonic-gate if ((svc == NULL) || (printer == NULL) || (job_id < 0) ||
11610Sstevel@tonic-gate (attributes == NULL))
11620Sstevel@tonic-gate return (PAPI_BAD_ARGUMENT);
11630Sstevel@tonic-gate
11640Sstevel@tonic-gate if ((*job = j = calloc(1, sizeof (*j))) == NULL)
11650Sstevel@tonic-gate return (PAPI_TEMPORARY_ERROR);
11660Sstevel@tonic-gate
11670Sstevel@tonic-gate dest = printer_name_from_uri_id(printer, job_id);
116812091SSonam.Gupta@Sun.COM
116912091SSonam.Gupta@Sun.COM /*
117012091SSonam.Gupta@Sun.COM * job-id might be job-id-requested
117112091SSonam.Gupta@Sun.COM * If it is job-id-requested then we need to
117212091SSonam.Gupta@Sun.COM * look for corresponding job-id
117312091SSonam.Gupta@Sun.COM */
117412091SSonam.Gupta@Sun.COM job_id_actual = check_job_id(svc, printer, job_id);
117512091SSonam.Gupta@Sun.COM
117612091SSonam.Gupta@Sun.COM if (job_id_actual < 0) {
117712091SSonam.Gupta@Sun.COM status = PAPI_NOT_FOUND;
117812091SSonam.Gupta@Sun.COM detailed_error(svc,
117912091SSonam.Gupta@Sun.COM "failed to initiate change for job (%s-%d): %s",
118012091SSonam.Gupta@Sun.COM dest, job_id, "no such resource");
118112091SSonam.Gupta@Sun.COM return (status);
118212091SSonam.Gupta@Sun.COM }
118312091SSonam.Gupta@Sun.COM
118412091SSonam.Gupta@Sun.COM status = lpsched_start_change(svc, dest, job_id_actual, &file);
11850Sstevel@tonic-gate if (status != PAPI_OK)
11860Sstevel@tonic-gate return (status);
11870Sstevel@tonic-gate
11880Sstevel@tonic-gate if ((r = getrequest(file)) != NULL) {
11890Sstevel@tonic-gate job_attributes_to_lpsched_request(handle, r,
11908569SJonathan.Ca@Sun.COM (papi_attribute_t **)attributes);
11910Sstevel@tonic-gate #ifdef LP_USE_PAPI_ATTR
11920Sstevel@tonic-gate /*
11930Sstevel@tonic-gate * store the job attributes in the PAPI job attribute file
119412091SSonam.Gupta@Sun.COM * that was created by the original job request. We need to
11950Sstevel@tonic-gate * modify the attributes in the file as per the new attributes
11960Sstevel@tonic-gate */
11970Sstevel@tonic-gate snprintf(lpfile, sizeof (lpfile), "%s%d-%s",
119812091SSonam.Gupta@Sun.COM "/var/spool/lp/temp/", job_id_actual, LP_PAPIATTRNAME);
11990Sstevel@tonic-gate status = psm_modifyAttrsFile(attributes, lpfile);
12000Sstevel@tonic-gate if (status != PAPI_OK) {
12010Sstevel@tonic-gate detailed_error(svc,
12028569SJonathan.Ca@Sun.COM "unable to modify the attributes file: %s: %s",
12038569SJonathan.Ca@Sun.COM lpfile, strerror(errno));
12040Sstevel@tonic-gate return (PAPI_DEVICE_ERROR);
12050Sstevel@tonic-gate }
12060Sstevel@tonic-gate #endif
12070Sstevel@tonic-gate
12080Sstevel@tonic-gate if (putrequest(file, r) < 0) {
12090Sstevel@tonic-gate detailed_error(svc,
12108569SJonathan.Ca@Sun.COM gettext("failed to write job: %s: %s"),
12118569SJonathan.Ca@Sun.COM file, strerror(errno));
12120Sstevel@tonic-gate freerequest(r);
12130Sstevel@tonic-gate return (PAPI_DEVICE_ERROR);
12140Sstevel@tonic-gate }
12150Sstevel@tonic-gate } else {
12160Sstevel@tonic-gate detailed_error(svc, gettext("failed to read job: %s: %s"),
12178569SJonathan.Ca@Sun.COM file, strerror(errno));
12180Sstevel@tonic-gate return (PAPI_DEVICE_ERROR);
12190Sstevel@tonic-gate }
12200Sstevel@tonic-gate
122112091SSonam.Gupta@Sun.COM status = lpsched_end_change(svc, dest, job_id_actual);
12220Sstevel@tonic-gate lpsched_request_to_job_attributes(r, j);
12236725Sjacobs
12246725Sjacobs papiAttributeListAddInteger(&j->attributes, PAPI_ATTR_REPLACE,
122512091SSonam.Gupta@Sun.COM "job-id", job_id_actual);
12266725Sjacobs
12270Sstevel@tonic-gate freerequest(r);
12280Sstevel@tonic-gate
12290Sstevel@tonic-gate return (status);
12300Sstevel@tonic-gate }
12310Sstevel@tonic-gate
12320Sstevel@tonic-gate /*
12330Sstevel@tonic-gate * Extension to PAPI, a variation of this is slated for post-1.0
12340Sstevel@tonic-gate */
12350Sstevel@tonic-gate #define DUMMY_FILE "/var/spool/lp/fifos/FIFO"
12360Sstevel@tonic-gate
12370Sstevel@tonic-gate papi_status_t
papiJobCreate(papi_service_t handle,char * printer,papi_attribute_t ** job_attributes,papi_job_ticket_t * job_ticket,papi_job_t * job)12382264Sjacobs papiJobCreate(papi_service_t handle, char *printer,
12392264Sjacobs papi_attribute_t **job_attributes,
12402264Sjacobs papi_job_ticket_t *job_ticket, papi_job_t *job)
12410Sstevel@tonic-gate {
12420Sstevel@tonic-gate papi_status_t status;
12430Sstevel@tonic-gate service_t *svc = handle;
12440Sstevel@tonic-gate job_t *j = NULL;
12450Sstevel@tonic-gate REQUEST *request;
12460Sstevel@tonic-gate char *request_id = NULL;
12470Sstevel@tonic-gate char *c;
12480Sstevel@tonic-gate char *tmp = NULL;
12490Sstevel@tonic-gate char metadata_file[MAXPATHLEN];
12500Sstevel@tonic-gate
12510Sstevel@tonic-gate if ((svc == NULL) || (printer == NULL) || (job == NULL))
12520Sstevel@tonic-gate return (PAPI_BAD_ARGUMENT);
12530Sstevel@tonic-gate
12540Sstevel@tonic-gate if (job_ticket != NULL)
12550Sstevel@tonic-gate return (PAPI_JOB_TICKET_NOT_SUPPORTED);
12560Sstevel@tonic-gate
12570Sstevel@tonic-gate if ((*job = j = calloc(1, sizeof (*j))) == NULL)
12580Sstevel@tonic-gate return (PAPI_TEMPORARY_ERROR);
12590Sstevel@tonic-gate
12600Sstevel@tonic-gate /* 1 for the control file (-0) */
12610Sstevel@tonic-gate status = lpsched_alloc_files(svc, 1, &request_id);
12620Sstevel@tonic-gate if (status != PAPI_OK)
12630Sstevel@tonic-gate return (status);
12640Sstevel@tonic-gate
12650Sstevel@tonic-gate /* convert the attributes to an lpsched REQUEST structure */
12660Sstevel@tonic-gate request = create_request(svc, (char *)printer,
12678569SJonathan.Ca@Sun.COM (papi_attribute_t **)job_attributes);
12682264Sjacobs if (request == NULL)
12692264Sjacobs return (PAPI_TEMPORARY_ERROR);
12700Sstevel@tonic-gate addlist(&request->file_list, DUMMY_FILE); /* add a dummy file */
12710Sstevel@tonic-gate request->actions |= ACT_HOLD; /* hold the job */
12720Sstevel@tonic-gate
12730Sstevel@tonic-gate #ifdef LP_USE_PAPI_ATTR
12740Sstevel@tonic-gate /*
12750Sstevel@tonic-gate * store the job attributes in the PAPI job attribute file that was
12760Sstevel@tonic-gate * created by lpsched_alloc_files(), the attributes will then pass
12770Sstevel@tonic-gate * through lpsched and be given to the slow-filters and the printer's
12780Sstevel@tonic-gate * interface script to process them
12790Sstevel@tonic-gate */
12800Sstevel@tonic-gate snprintf(metadata_file, sizeof (metadata_file), "%s%s-%s",
12818569SJonathan.Ca@Sun.COM "/var/spool/lp/temp/", request_id, LP_PAPIATTRNAME);
12820Sstevel@tonic-gate status = psm_copy_attrsToFile(job_attributes, metadata_file);
12830Sstevel@tonic-gate if (status != PAPI_OK) {
12840Sstevel@tonic-gate detailed_error(svc, "unable to copy attributes to file: %s: %s",
12858569SJonathan.Ca@Sun.COM metadata_file, strerror(errno));
12862264Sjacobs free(request_id);
12870Sstevel@tonic-gate return (PAPI_DEVICE_ERROR);
12880Sstevel@tonic-gate }
12890Sstevel@tonic-gate #endif
12900Sstevel@tonic-gate
12910Sstevel@tonic-gate /* store the REQUEST on disk */
12920Sstevel@tonic-gate snprintf(metadata_file, sizeof (metadata_file), "%s-0", request_id);
12932264Sjacobs free(request_id);
12940Sstevel@tonic-gate if (putrequest(metadata_file, request) < 0) {
12950Sstevel@tonic-gate detailed_error(svc, gettext("unable to save request: %s: %s"),
12968569SJonathan.Ca@Sun.COM metadata_file, strerror(errno));
12970Sstevel@tonic-gate return (PAPI_DEVICE_ERROR);
12980Sstevel@tonic-gate }
12990Sstevel@tonic-gate
13000Sstevel@tonic-gate status = lpsched_commit_job(svc, metadata_file, &tmp);
13010Sstevel@tonic-gate if (status != PAPI_OK) {
13020Sstevel@tonic-gate unlink(metadata_file);
13030Sstevel@tonic-gate return (status);
13040Sstevel@tonic-gate }
13050Sstevel@tonic-gate
13060Sstevel@tonic-gate lpsched_request_to_job_attributes(request, j);
13070Sstevel@tonic-gate
13080Sstevel@tonic-gate if ((c = strrchr(tmp, '-')) != NULL)
13090Sstevel@tonic-gate c++;
13100Sstevel@tonic-gate papiAttributeListAddInteger(&j->attributes, PAPI_ATTR_REPLACE,
13118569SJonathan.Ca@Sun.COM "job-id", atoi(c));
13120Sstevel@tonic-gate papiAttributeListAddString(&j->attributes, PAPI_ATTR_REPLACE,
13138569SJonathan.Ca@Sun.COM "job-uri", tmp);
13140Sstevel@tonic-gate
13150Sstevel@tonic-gate return (PAPI_OK);
13160Sstevel@tonic-gate }
13170Sstevel@tonic-gate
13180Sstevel@tonic-gate papi_status_t
papiJobCommit(papi_service_t handle,char * printer,int32_t id)13190Sstevel@tonic-gate papiJobCommit(papi_service_t handle, char *printer, int32_t id)
13200Sstevel@tonic-gate {
13210Sstevel@tonic-gate papi_status_t status = PAPI_OK;
13220Sstevel@tonic-gate service_t *svc = handle;
13230Sstevel@tonic-gate REQUEST *r = NULL;
13240Sstevel@tonic-gate char *metadata_file;
13250Sstevel@tonic-gate char *dest;
13260Sstevel@tonic-gate
13270Sstevel@tonic-gate if ((svc == NULL) || (printer == NULL))
13280Sstevel@tonic-gate return (PAPI_BAD_ARGUMENT);
13290Sstevel@tonic-gate
13300Sstevel@tonic-gate dest = printer_name_from_uri_id(printer, id);
13310Sstevel@tonic-gate /* tell the scheduler that we want to change the job */
13320Sstevel@tonic-gate status = lpsched_start_change(svc, dest, id, &metadata_file);
13330Sstevel@tonic-gate if (status != PAPI_OK)
13340Sstevel@tonic-gate return (status);
13350Sstevel@tonic-gate
13360Sstevel@tonic-gate if ((r = getrequest(metadata_file)) != NULL) {
13370Sstevel@tonic-gate r->actions &= ~ACT_RESUME;
13380Sstevel@tonic-gate r->actions |= ACT_RESUME;
13390Sstevel@tonic-gate dellist(&r->file_list, DUMMY_FILE);
13400Sstevel@tonic-gate
13410Sstevel@tonic-gate if (putrequest(metadata_file, r) < 0) {
13420Sstevel@tonic-gate detailed_error(svc,
13438569SJonathan.Ca@Sun.COM gettext("failed to write job: %s: %s"),
13448569SJonathan.Ca@Sun.COM metadata_file, strerror(errno));
13452264Sjacobs freerequest(r);
13460Sstevel@tonic-gate return (PAPI_DEVICE_ERROR);
13470Sstevel@tonic-gate }
13480Sstevel@tonic-gate } else {
13490Sstevel@tonic-gate detailed_error(svc, gettext("failed to read job: %s: %s"),
13508569SJonathan.Ca@Sun.COM metadata_file, strerror(errno));
13510Sstevel@tonic-gate return (PAPI_DEVICE_ERROR);
13520Sstevel@tonic-gate }
13530Sstevel@tonic-gate
13540Sstevel@tonic-gate status = lpsched_end_change(svc, dest, id);
13550Sstevel@tonic-gate freerequest(r);
13560Sstevel@tonic-gate
13570Sstevel@tonic-gate return (status);
13580Sstevel@tonic-gate }
13590Sstevel@tonic-gate
13600Sstevel@tonic-gate papi_status_t
papiJobStreamAdd(papi_service_t handle,char * printer,int32_t id,papi_stream_t * stream)13610Sstevel@tonic-gate papiJobStreamAdd(papi_service_t handle, char *printer, int32_t id,
13620Sstevel@tonic-gate papi_stream_t *stream)
13630Sstevel@tonic-gate {
13640Sstevel@tonic-gate papi_status_t status;
13650Sstevel@tonic-gate service_t *svc = handle;
13660Sstevel@tonic-gate job_stream_t *s = NULL;
13670Sstevel@tonic-gate char *metadata_file = NULL;
13680Sstevel@tonic-gate char *dest;
13690Sstevel@tonic-gate char path[MAXPATHLEN];
13700Sstevel@tonic-gate
13710Sstevel@tonic-gate /* allocate space for the stream */
13720Sstevel@tonic-gate if ((*stream = s = calloc(1, sizeof (*s))) == NULL)
13730Sstevel@tonic-gate return (PAPI_TEMPORARY_ERROR);
13740Sstevel@tonic-gate
13750Sstevel@tonic-gate dest = printer_name_from_uri_id(printer, id);
13760Sstevel@tonic-gate /* create/open data file (only root or lp can really do this */
13770Sstevel@tonic-gate snprintf(path, sizeof (path), "/var/spool/lp/temp/%d-XXXXXX", id);
13780Sstevel@tonic-gate if ((s->fd = mkstemp(path)) < 0) {
13790Sstevel@tonic-gate detailed_error(svc, gettext("unable to create sink (%s): %s"),
13808569SJonathan.Ca@Sun.COM path, strerror(errno));
13810Sstevel@tonic-gate free(s);
13820Sstevel@tonic-gate return (PAPI_NOT_AUTHORIZED);
13830Sstevel@tonic-gate }
13840Sstevel@tonic-gate
13850Sstevel@tonic-gate /* add data file to job */
13860Sstevel@tonic-gate status = lpsched_start_change(svc, dest, id, &metadata_file);
13870Sstevel@tonic-gate if (status != PAPI_OK) {
13880Sstevel@tonic-gate close(s->fd);
13890Sstevel@tonic-gate free(s);
13900Sstevel@tonic-gate unlink(path);
13910Sstevel@tonic-gate return (status);
13920Sstevel@tonic-gate }
13930Sstevel@tonic-gate
13940Sstevel@tonic-gate if ((s->request = getrequest(metadata_file)) == NULL) {
13950Sstevel@tonic-gate detailed_error(svc, gettext("unable to load request: %s: %s"),
13968569SJonathan.Ca@Sun.COM metadata_file, strerror(errno));
13970Sstevel@tonic-gate close(s->fd);
13980Sstevel@tonic-gate free(s);
13990Sstevel@tonic-gate unlink(path);
14000Sstevel@tonic-gate return (PAPI_NOT_POSSIBLE);
14010Sstevel@tonic-gate }
14020Sstevel@tonic-gate
14030Sstevel@tonic-gate addlist(&(s->request->file_list), path);
14040Sstevel@tonic-gate
14050Sstevel@tonic-gate if (putrequest(metadata_file, s->request) < 0) {
14060Sstevel@tonic-gate detailed_error(svc, gettext("unable to save request: %s: %s"),
14078569SJonathan.Ca@Sun.COM metadata_file, strerror(errno));
14080Sstevel@tonic-gate close(s->fd);
14090Sstevel@tonic-gate free(s);
14100Sstevel@tonic-gate unlink(path);
14110Sstevel@tonic-gate return (PAPI_NOT_POSSIBLE);
14120Sstevel@tonic-gate }
14130Sstevel@tonic-gate
14140Sstevel@tonic-gate status = lpsched_end_change(svc, dest, id);
14150Sstevel@tonic-gate
14160Sstevel@tonic-gate if (status != PAPI_OK)
14170Sstevel@tonic-gate return (status);
14180Sstevel@tonic-gate
14190Sstevel@tonic-gate return (PAPI_OK);
14200Sstevel@tonic-gate }
1421