12264Sjacobs /* 22264Sjacobs * CDDL HEADER START 32264Sjacobs * 42264Sjacobs * The contents of this file are subject to the terms of the 52264Sjacobs * Common Development and Distribution License (the "License"). 62264Sjacobs * You may not use this file except in compliance with the License. 72264Sjacobs * 82264Sjacobs * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 92264Sjacobs * or http://www.opensolaris.org/os/licensing. 102264Sjacobs * See the License for the specific language governing permissions 112264Sjacobs * and limitations under the License. 122264Sjacobs * 132264Sjacobs * When distributing Covered Code, include this CDDL HEADER in each 142264Sjacobs * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 152264Sjacobs * If applicable, add the following below this CDDL HEADER, with the 162264Sjacobs * fields enclosed by brackets "[]" replaced with your own identifying 172264Sjacobs * information: Portions Copyright [yyyy] [name of copyright owner] 182264Sjacobs * 192264Sjacobs * CDDL HEADER END 202264Sjacobs */ 212264Sjacobs 222264Sjacobs /* 232264Sjacobs * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 242264Sjacobs * Use is subject to license terms. 252264Sjacobs * 262264Sjacobs */ 272264Sjacobs 28*2383Sjacobs /* $Id: job.c 179 2006-07-17 18:24:07Z njacobs $ */ 292264Sjacobs 302264Sjacobs #pragma ident "%Z%%M% %I% %E% SMI" 312264Sjacobs 322264Sjacobs #include <stdlib.h> 332264Sjacobs #include <stdio.h> 342264Sjacobs #include <string.h> 352264Sjacobs #include <errno.h> 362264Sjacobs #include <unistd.h> 372264Sjacobs #include <limits.h> 382264Sjacobs #include <libintl.h> 392264Sjacobs #include <sys/types.h> 402264Sjacobs #include <sys/stat.h> 412264Sjacobs #include <fcntl.h> 422264Sjacobs #include <papi_impl.h> 432264Sjacobs #include <uri.h> 442264Sjacobs 452264Sjacobs /* 462264Sjacobs * must copy files before leaving routine 472264Sjacobs */ 482264Sjacobs papi_status_t 492264Sjacobs papiJobSubmit(papi_service_t handle, char *name, papi_attribute_t **attributes, 502264Sjacobs papi_job_ticket_t *job_ticket, char **files, papi_job_t *job) 512264Sjacobs { 522264Sjacobs papi_status_t status = PAPI_OK; 532264Sjacobs service_t *svc = handle; 542264Sjacobs job_t *j = NULL; 552264Sjacobs char *metadata = NULL; 562264Sjacobs 572264Sjacobs if ((svc == NULL) || (name == NULL) || (files == NULL) || 582264Sjacobs (job == NULL)) 592264Sjacobs return (PAPI_BAD_ARGUMENT); 602264Sjacobs 612264Sjacobs if (job_ticket != NULL) { 622264Sjacobs detailed_error(svc, 632264Sjacobs gettext("papiJobSubmit: job ticket not supported")); 642264Sjacobs return (PAPI_OPERATION_NOT_SUPPORTED); 652264Sjacobs } 662264Sjacobs 672264Sjacobs if ((status = service_fill_in(svc, name)) != PAPI_OK) 682264Sjacobs return (status); 692264Sjacobs 702264Sjacobs if ((*job = j = (job_t *)calloc(1, sizeof (*j))) == NULL) { 712264Sjacobs detailed_error(svc, 722264Sjacobs gettext("calloc() failed")); 732264Sjacobs return (PAPI_TEMPORARY_ERROR); 742264Sjacobs } 752264Sjacobs 762264Sjacobs /* create a control file */ 772264Sjacobs status = lpd_job_add_attributes(svc, attributes, &metadata, 782264Sjacobs &j->attributes); 792264Sjacobs status = lpd_job_add_files(svc, attributes, files, &metadata, 802264Sjacobs &j->attributes); 812264Sjacobs 822264Sjacobs /* send the job to the server */ 832264Sjacobs status = lpd_submit_job(svc, metadata, &j->attributes, NULL); 842264Sjacobs free(metadata); 852264Sjacobs 862264Sjacobs return (status); 872264Sjacobs 882264Sjacobs } 892264Sjacobs 902264Sjacobs 912264Sjacobs papi_status_t 922264Sjacobs papiJobSubmitByReference(papi_service_t handle, char *name, 932264Sjacobs papi_attribute_t **job_attributes, 942264Sjacobs papi_job_ticket_t *job_ticket, char **files, papi_job_t *job) 952264Sjacobs { 962264Sjacobs return (papiJobSubmit(handle, name, job_attributes, 972264Sjacobs job_ticket, files, job)); 982264Sjacobs } 992264Sjacobs 1002264Sjacobs papi_status_t 1012264Sjacobs papiJobStreamOpen(papi_service_t handle, char *name, 1022264Sjacobs papi_attribute_t **attributes, 1032264Sjacobs papi_job_ticket_t *job_ticket, papi_stream_t *stream) 1042264Sjacobs { 1052264Sjacobs papi_status_t status = PAPI_OK; 1062264Sjacobs service_t *svc = handle; 1072264Sjacobs char *metadata = NULL; 1082264Sjacobs stream_t *s = NULL; 1092264Sjacobs 1102264Sjacobs if ((svc == NULL) || (name == NULL) || (stream == NULL)) 1112264Sjacobs return (PAPI_BAD_ARGUMENT); 1122264Sjacobs 1132264Sjacobs if (job_ticket != NULL) 1142264Sjacobs return (PAPI_OPERATION_NOT_SUPPORTED); 1152264Sjacobs 1162264Sjacobs if ((status = service_fill_in(svc, name)) != PAPI_OK) 1172264Sjacobs return (status); 1182264Sjacobs 1192264Sjacobs /* create the stream container */ 1202264Sjacobs if ((*stream = s = calloc(1, sizeof (*s))) == NULL) 1212264Sjacobs return (PAPI_TEMPORARY_ERROR); 1222264Sjacobs 1232264Sjacobs /* create the job */ 1242264Sjacobs if ((s->job = calloc(1, sizeof (*(s->job)))) == NULL) 1252264Sjacobs return (PAPI_TEMPORARY_ERROR); 1262264Sjacobs 1272264Sjacobs /* process the attribute list */ 1282264Sjacobs lpd_job_add_attributes(svc, attributes, &metadata, &s->job->attributes); 1292264Sjacobs 1302264Sjacobs /* if we can stream, do it */ 1312264Sjacobs if ((svc->uri->fragment != NULL) && 1322264Sjacobs (strcasecmp(svc->uri->fragment, "streaming") == 0)) { 1332264Sjacobs char *files[] = { "standard input", NULL }; 1342264Sjacobs 1352264Sjacobs lpd_job_add_files(svc, attributes, files, &metadata, 1362264Sjacobs &(s->job->attributes)); 1372264Sjacobs status = lpd_submit_job(svc, metadata, &(s->job->attributes), 1382264Sjacobs &s->fd); 1392264Sjacobs } else { 1402264Sjacobs char dfname[18]; 1412264Sjacobs 1422264Sjacobs strcpy(dfname, "/tmp/stdin-XXXXX"); 1432264Sjacobs 1442264Sjacobs if ((s->fd = mkstemp(dfname)) >= 0) 1452264Sjacobs s->dfname = strdup(dfname); 1462264Sjacobs } 1472264Sjacobs s->metadata = metadata; 1482264Sjacobs 1492264Sjacobs return (status); 1502264Sjacobs } 1512264Sjacobs 1522264Sjacobs 1532264Sjacobs papi_status_t 1542264Sjacobs papiJobStreamWrite(papi_service_t handle, papi_stream_t stream, 1552264Sjacobs void *buffer, size_t buflen) 1562264Sjacobs { 1572264Sjacobs service_t *svc = handle; 1582264Sjacobs stream_t *s = stream; 1592264Sjacobs 1602264Sjacobs if ((svc == NULL) || (stream == NULL) || (buffer == NULL) || 1612264Sjacobs (buflen == 0)) 1622264Sjacobs return (PAPI_BAD_ARGUMENT); 1632264Sjacobs 1642264Sjacobs if (write(s->fd, buffer, buflen) != buflen) 1652264Sjacobs return (PAPI_DEVICE_ERROR); 1662264Sjacobs 1672264Sjacobs return (PAPI_OK); 1682264Sjacobs } 1692264Sjacobs 1702264Sjacobs papi_status_t 1712264Sjacobs papiJobStreamClose(papi_service_t handle, papi_stream_t stream, papi_job_t *job) 1722264Sjacobs { 1732264Sjacobs papi_status_t status = PAPI_INTERNAL_ERROR; 1742264Sjacobs service_t *svc = handle; 1752264Sjacobs job_t *j = NULL; 1762264Sjacobs stream_t *s = stream; 1772264Sjacobs int ret; 1782264Sjacobs 1792264Sjacobs if ((svc == NULL) || (stream == NULL) || (job == NULL)) 1802264Sjacobs return (PAPI_BAD_ARGUMENT); 1812264Sjacobs 1822264Sjacobs close(s->fd); /* close the stream */ 1832264Sjacobs 1842264Sjacobs if (s->dfname != NULL) { /* if it is a tmpfile, print it */ 1852264Sjacobs char *files[2]; 1862264Sjacobs 1872264Sjacobs files[0] = s->dfname; 1882264Sjacobs files[1] = NULL; 1892264Sjacobs 1902264Sjacobs lpd_job_add_files(svc, s->job->attributes, files, &s->metadata, 1912264Sjacobs &(s->job->attributes)); 1922264Sjacobs status = lpd_submit_job(svc, s->metadata, 1932264Sjacobs &(s->job->attributes), NULL); 1942264Sjacobs unlink(s->dfname); 1952264Sjacobs free(s->dfname); 1962264Sjacobs } else 1972264Sjacobs status = PAPI_OK; 1982264Sjacobs 1992264Sjacobs if (s->metadata != NULL) 2002264Sjacobs free(s->metadata); 2012264Sjacobs 2022264Sjacobs *job = s->job; 2032264Sjacobs 2042264Sjacobs return (status); 2052264Sjacobs } 2062264Sjacobs 2072264Sjacobs papi_status_t 2082264Sjacobs papiJobQuery(papi_service_t handle, char *name, int32_t job_id, 2092264Sjacobs char **job_attributes, papi_job_t *job) 2102264Sjacobs { 2112264Sjacobs papi_status_t status = PAPI_OK; 2122264Sjacobs service_t *svc = handle; 2132264Sjacobs 2142264Sjacobs if ((svc == NULL) || (name == NULL) || job_id < 0) 2152264Sjacobs return (PAPI_BAD_ARGUMENT); 2162264Sjacobs 2172264Sjacobs if ((status = service_fill_in(svc, name)) == PAPI_OK) 2182264Sjacobs status = lpd_find_job_info(svc, job_id, (job_t **)job); 2192264Sjacobs 2202264Sjacobs return (status); 2212264Sjacobs } 2222264Sjacobs 2232264Sjacobs papi_status_t 2242264Sjacobs papiJobCancel(papi_service_t handle, char *name, int32_t job_id) 2252264Sjacobs { 2262264Sjacobs papi_status_t status; 2272264Sjacobs service_t *svc = handle; 2282264Sjacobs 2292264Sjacobs if ((svc == NULL) || (name == NULL) || (job_id < 0)) 2302264Sjacobs return (PAPI_BAD_ARGUMENT); 2312264Sjacobs 2322264Sjacobs if ((status = service_fill_in(svc, name)) == PAPI_OK) 2332264Sjacobs status = lpd_cancel_job(svc, job_id); 2342264Sjacobs 2352264Sjacobs return (status); 2362264Sjacobs } 2372264Sjacobs 2382264Sjacobs papi_attribute_t ** 2392264Sjacobs papiJobGetAttributeList(papi_job_t job) 2402264Sjacobs { 2412264Sjacobs job_t *j = (job_t *)job; 2422264Sjacobs 2432264Sjacobs if (j != NULL) 2442264Sjacobs return ((papi_attribute_t **)j->attributes); 2452264Sjacobs 2462264Sjacobs return (NULL); 2472264Sjacobs } 2482264Sjacobs 2492264Sjacobs char * 2502264Sjacobs papiJobGetPrinterName(papi_job_t job) 2512264Sjacobs { 2522264Sjacobs char *result = NULL; 2532264Sjacobs job_t *j = (job_t *)job; 2542264Sjacobs 2552264Sjacobs if (j != NULL) 2562264Sjacobs papiAttributeListGetString(j->attributes, NULL, 2572264Sjacobs "printer-name", &result); 2582264Sjacobs 2592264Sjacobs return (result); 2602264Sjacobs } 2612264Sjacobs 2622264Sjacobs int 2632264Sjacobs papiJobGetId(papi_job_t job) 2642264Sjacobs { 2652264Sjacobs int result = -1; 2662264Sjacobs job_t *j = (job_t *)job; 2672264Sjacobs 2682264Sjacobs if (j != NULL) 2692264Sjacobs papiAttributeListGetInteger(j->attributes, NULL, 2702264Sjacobs "job-id", &result); 2712264Sjacobs 2722264Sjacobs return (result); 2732264Sjacobs } 2742264Sjacobs 2752264Sjacobs void 2762264Sjacobs papiJobFree(papi_job_t job) 2772264Sjacobs { 2782264Sjacobs job_t *j = (job_t *)job; 2792264Sjacobs 2802264Sjacobs 2812264Sjacobs if (j != NULL) { 2822264Sjacobs papiAttributeListFree(j->attributes); 2832264Sjacobs free(j); 2842264Sjacobs } 2852264Sjacobs } 2862264Sjacobs 2872264Sjacobs void 2882264Sjacobs papiJobListFree(papi_job_t *jobs) 2892264Sjacobs { 2902264Sjacobs if (jobs != NULL) { 2912264Sjacobs int i; 2922264Sjacobs 2932264Sjacobs for (i = 0; jobs[i] != NULL; i++) 2942264Sjacobs papiJobFree(jobs[i]); 2952264Sjacobs free(jobs); 2962264Sjacobs } 2972264Sjacobs } 298