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 /*
23*11699SKeerthi.Kondaka@Sun.COM * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
242264Sjacobs * Use is subject to license terms.
252264Sjacobs */
262264Sjacobs
272383Sjacobs /* $Id: job.c 179 2006-07-17 18:24:07Z njacobs $ */
282264Sjacobs
292264Sjacobs #include <stdlib.h>
302264Sjacobs #include <stdio.h>
312264Sjacobs #include <string.h>
322264Sjacobs #include <errno.h>
332264Sjacobs #include <unistd.h>
342264Sjacobs #include <limits.h>
352264Sjacobs #include <libintl.h>
362264Sjacobs #include <sys/types.h>
372264Sjacobs #include <sys/stat.h>
382264Sjacobs #include <fcntl.h>
392264Sjacobs #include <papi_impl.h>
402264Sjacobs #include <uri.h>
412264Sjacobs
422264Sjacobs /*
432264Sjacobs * must copy files before leaving routine
442264Sjacobs */
452264Sjacobs papi_status_t
papiJobSubmit(papi_service_t handle,char * name,papi_attribute_t ** attributes,papi_job_ticket_t * job_ticket,char ** files,papi_job_t * job)462264Sjacobs papiJobSubmit(papi_service_t handle, char *name, papi_attribute_t **attributes,
472264Sjacobs papi_job_ticket_t *job_ticket, char **files, papi_job_t *job)
482264Sjacobs {
492264Sjacobs papi_status_t status = PAPI_OK;
502264Sjacobs service_t *svc = handle;
512264Sjacobs job_t *j = NULL;
522264Sjacobs char *metadata = NULL;
532264Sjacobs
542264Sjacobs if ((svc == NULL) || (name == NULL) || (files == NULL) ||
552264Sjacobs (job == NULL))
562264Sjacobs return (PAPI_BAD_ARGUMENT);
572264Sjacobs
582264Sjacobs if (job_ticket != NULL) {
592264Sjacobs detailed_error(svc,
604390Sceastha gettext("papiJobSubmit: job ticket not supported"));
612264Sjacobs return (PAPI_OPERATION_NOT_SUPPORTED);
622264Sjacobs }
632264Sjacobs
642264Sjacobs if ((status = service_fill_in(svc, name)) != PAPI_OK)
652264Sjacobs return (status);
662264Sjacobs
672264Sjacobs if ((*job = j = (job_t *)calloc(1, sizeof (*j))) == NULL) {
682264Sjacobs detailed_error(svc,
694390Sceastha gettext("calloc() failed"));
702264Sjacobs return (PAPI_TEMPORARY_ERROR);
712264Sjacobs }
722264Sjacobs
739166SSonam.Gupta@Sun.COM /* before creating a control file add the job-name */
749166SSonam.Gupta@Sun.COM if ((files != NULL) && (files[0] != NULL))
759166SSonam.Gupta@Sun.COM papiAttributeListAddString(&attributes, PAPI_ATTR_EXCL,
769166SSonam.Gupta@Sun.COM "job-name", files[0]);
779166SSonam.Gupta@Sun.COM
782264Sjacobs /* create a control file */
794390Sceastha (void) lpd_job_add_attributes(svc, attributes, &metadata,
804390Sceastha &j->attributes);
814390Sceastha
824390Sceastha if ((status = lpd_job_add_files(svc, attributes, files, &metadata,
834390Sceastha &j->attributes)) != PAPI_OK) {
844390Sceastha return (status);
854390Sceastha }
862264Sjacobs
872264Sjacobs /* send the job to the server */
882264Sjacobs status = lpd_submit_job(svc, metadata, &j->attributes, NULL);
892264Sjacobs free(metadata);
902264Sjacobs
912264Sjacobs return (status);
922264Sjacobs
932264Sjacobs }
942264Sjacobs
952264Sjacobs
962264Sjacobs papi_status_t
papiJobSubmitByReference(papi_service_t handle,char * name,papi_attribute_t ** job_attributes,papi_job_ticket_t * job_ticket,char ** files,papi_job_t * job)972264Sjacobs papiJobSubmitByReference(papi_service_t handle, char *name,
982264Sjacobs papi_attribute_t **job_attributes,
992264Sjacobs papi_job_ticket_t *job_ticket, char **files, papi_job_t *job)
1002264Sjacobs {
1012264Sjacobs return (papiJobSubmit(handle, name, job_attributes,
1024390Sceastha job_ticket, files, job));
1032264Sjacobs }
1042264Sjacobs
1052264Sjacobs papi_status_t
papiJobStreamOpen(papi_service_t handle,char * name,papi_attribute_t ** attributes,papi_job_ticket_t * job_ticket,papi_stream_t * stream)1062264Sjacobs papiJobStreamOpen(papi_service_t handle, char *name,
1072264Sjacobs papi_attribute_t **attributes,
1082264Sjacobs papi_job_ticket_t *job_ticket, papi_stream_t *stream)
1092264Sjacobs {
1102264Sjacobs papi_status_t status = PAPI_OK;
1112264Sjacobs service_t *svc = handle;
1122264Sjacobs char *metadata = NULL;
1132264Sjacobs stream_t *s = NULL;
1142264Sjacobs
1152264Sjacobs if ((svc == NULL) || (name == NULL) || (stream == NULL))
1162264Sjacobs return (PAPI_BAD_ARGUMENT);
1172264Sjacobs
1182264Sjacobs if (job_ticket != NULL)
1192264Sjacobs return (PAPI_OPERATION_NOT_SUPPORTED);
1202264Sjacobs
1212264Sjacobs if ((status = service_fill_in(svc, name)) != PAPI_OK)
1222264Sjacobs return (status);
1232264Sjacobs
1242264Sjacobs /* create the stream container */
1252264Sjacobs if ((*stream = s = calloc(1, sizeof (*s))) == NULL)
1262264Sjacobs return (PAPI_TEMPORARY_ERROR);
1272264Sjacobs
1282264Sjacobs /* create the job */
1292264Sjacobs if ((s->job = calloc(1, sizeof (*(s->job)))) == NULL)
1302264Sjacobs return (PAPI_TEMPORARY_ERROR);
1312264Sjacobs
132*11699SKeerthi.Kondaka@Sun.COM papiAttributeListAddString(&attributes, PAPI_ATTR_EXCL,
133*11699SKeerthi.Kondaka@Sun.COM "job-name", "standard input");
134*11699SKeerthi.Kondaka@Sun.COM
1352264Sjacobs /* process the attribute list */
1362264Sjacobs lpd_job_add_attributes(svc, attributes, &metadata, &s->job->attributes);
1372264Sjacobs
1382264Sjacobs /* if we can stream, do it */
1392264Sjacobs if ((svc->uri->fragment != NULL) &&
1402264Sjacobs (strcasecmp(svc->uri->fragment, "streaming") == 0)) {
1412264Sjacobs char *files[] = { "standard input", NULL };
1422264Sjacobs
1432264Sjacobs lpd_job_add_files(svc, attributes, files, &metadata,
1444390Sceastha &(s->job->attributes));
1452264Sjacobs status = lpd_submit_job(svc, metadata, &(s->job->attributes),
1464390Sceastha &s->fd);
1472264Sjacobs } else {
1482264Sjacobs char dfname[18];
1499410SKeerthi.Kondaka@Sun.COM char buf[256];
1502264Sjacobs
1512264Sjacobs strcpy(dfname, "/tmp/stdin-XXXXX");
1522264Sjacobs
1532264Sjacobs if ((s->fd = mkstemp(dfname)) >= 0)
1542264Sjacobs s->dfname = strdup(dfname);
1559410SKeerthi.Kondaka@Sun.COM if (s->job->attributes)
1569410SKeerthi.Kondaka@Sun.COM papiAttributeListFree(s->job->attributes);
1579410SKeerthi.Kondaka@Sun.COM s->job->attributes = NULL;
1589410SKeerthi.Kondaka@Sun.COM papiAttributeListToString(attributes, " ", buf, sizeof (buf));
1599410SKeerthi.Kondaka@Sun.COM papiAttributeListFromString(&(s->job->attributes),
1609410SKeerthi.Kondaka@Sun.COM PAPI_ATTR_APPEND, buf);
1612264Sjacobs }
1622264Sjacobs s->metadata = metadata;
1632264Sjacobs
1642264Sjacobs return (status);
1652264Sjacobs }
1662264Sjacobs
1672264Sjacobs
1682264Sjacobs papi_status_t
papiJobStreamWrite(papi_service_t handle,papi_stream_t stream,void * buffer,size_t buflen)1692264Sjacobs papiJobStreamWrite(papi_service_t handle, papi_stream_t stream,
1702264Sjacobs void *buffer, size_t buflen)
1712264Sjacobs {
1722264Sjacobs service_t *svc = handle;
1732264Sjacobs stream_t *s = stream;
1742264Sjacobs
1752264Sjacobs if ((svc == NULL) || (stream == NULL) || (buffer == NULL) ||
1762264Sjacobs (buflen == 0))
1772264Sjacobs return (PAPI_BAD_ARGUMENT);
1782264Sjacobs
1792264Sjacobs if (write(s->fd, buffer, buflen) != buflen)
1802264Sjacobs return (PAPI_DEVICE_ERROR);
1812264Sjacobs
1822264Sjacobs return (PAPI_OK);
1832264Sjacobs }
1842264Sjacobs
1852264Sjacobs papi_status_t
papiJobStreamClose(papi_service_t handle,papi_stream_t stream,papi_job_t * job)1862264Sjacobs papiJobStreamClose(papi_service_t handle, papi_stream_t stream, papi_job_t *job)
1872264Sjacobs {
1882264Sjacobs papi_status_t status = PAPI_INTERNAL_ERROR;
1892264Sjacobs service_t *svc = handle;
1902264Sjacobs job_t *j = NULL;
1912264Sjacobs stream_t *s = stream;
1922264Sjacobs int ret;
1932264Sjacobs
1942264Sjacobs if ((svc == NULL) || (stream == NULL) || (job == NULL))
1952264Sjacobs return (PAPI_BAD_ARGUMENT);
1962264Sjacobs
1972264Sjacobs close(s->fd); /* close the stream */
1982264Sjacobs
1992264Sjacobs if (s->dfname != NULL) { /* if it is a tmpfile, print it */
2002264Sjacobs char *files[2];
2012264Sjacobs
2022264Sjacobs files[0] = s->dfname;
2032264Sjacobs files[1] = NULL;
2042264Sjacobs
2052264Sjacobs lpd_job_add_files(svc, s->job->attributes, files, &s->metadata,
2064390Sceastha &(s->job->attributes));
2072264Sjacobs status = lpd_submit_job(svc, s->metadata,
2084390Sceastha &(s->job->attributes), NULL);
2092264Sjacobs unlink(s->dfname);
2102264Sjacobs free(s->dfname);
2112264Sjacobs } else
2122264Sjacobs status = PAPI_OK;
2132264Sjacobs
2142264Sjacobs if (s->metadata != NULL)
2152264Sjacobs free(s->metadata);
2162264Sjacobs
2172264Sjacobs *job = s->job;
2182264Sjacobs
2192264Sjacobs return (status);
2202264Sjacobs }
2212264Sjacobs
2222264Sjacobs papi_status_t
papiJobQuery(papi_service_t handle,char * name,int32_t job_id,char ** job_attributes,papi_job_t * job)2232264Sjacobs papiJobQuery(papi_service_t handle, char *name, int32_t job_id,
2242264Sjacobs char **job_attributes, papi_job_t *job)
2252264Sjacobs {
2262264Sjacobs papi_status_t status = PAPI_OK;
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_find_job_info(svc, job_id, (job_t **)job);
2342264Sjacobs
2352264Sjacobs return (status);
2362264Sjacobs }
2372264Sjacobs
2382264Sjacobs papi_status_t
papiJobCancel(papi_service_t handle,char * name,int32_t job_id)2392264Sjacobs papiJobCancel(papi_service_t handle, char *name, int32_t job_id)
2402264Sjacobs {
2412264Sjacobs papi_status_t status;
2422264Sjacobs service_t *svc = handle;
2432264Sjacobs
2442264Sjacobs if ((svc == NULL) || (name == NULL) || (job_id < 0))
2452264Sjacobs return (PAPI_BAD_ARGUMENT);
2462264Sjacobs
2472264Sjacobs if ((status = service_fill_in(svc, name)) == PAPI_OK)
2482264Sjacobs status = lpd_cancel_job(svc, job_id);
2492264Sjacobs
2502264Sjacobs return (status);
2512264Sjacobs }
2522264Sjacobs
2532264Sjacobs papi_attribute_t **
papiJobGetAttributeList(papi_job_t job)2542264Sjacobs papiJobGetAttributeList(papi_job_t job)
2552264Sjacobs {
2562264Sjacobs job_t *j = (job_t *)job;
2572264Sjacobs
2582264Sjacobs if (j != NULL)
2592264Sjacobs return ((papi_attribute_t **)j->attributes);
2602264Sjacobs
2612264Sjacobs return (NULL);
2622264Sjacobs }
2632264Sjacobs
2642264Sjacobs char *
papiJobGetPrinterName(papi_job_t job)2652264Sjacobs papiJobGetPrinterName(papi_job_t job)
2662264Sjacobs {
2672264Sjacobs char *result = NULL;
2682264Sjacobs job_t *j = (job_t *)job;
2692264Sjacobs
2702264Sjacobs if (j != NULL)
2712264Sjacobs papiAttributeListGetString(j->attributes, NULL,
2724390Sceastha "printer-name", &result);
2732264Sjacobs
2742264Sjacobs return (result);
2752264Sjacobs }
2762264Sjacobs
2772264Sjacobs int
papiJobGetId(papi_job_t job)2782264Sjacobs papiJobGetId(papi_job_t job)
2792264Sjacobs {
2802264Sjacobs int result = -1;
2812264Sjacobs job_t *j = (job_t *)job;
2822264Sjacobs
2832264Sjacobs if (j != NULL)
2842264Sjacobs papiAttributeListGetInteger(j->attributes, NULL,
2854390Sceastha "job-id", &result);
2862264Sjacobs
2872264Sjacobs return (result);
2882264Sjacobs }
2892264Sjacobs
2902264Sjacobs void
papiJobFree(papi_job_t job)2912264Sjacobs papiJobFree(papi_job_t job)
2922264Sjacobs {
2932264Sjacobs job_t *j = (job_t *)job;
2942264Sjacobs
2952264Sjacobs
2962264Sjacobs if (j != NULL) {
2972264Sjacobs papiAttributeListFree(j->attributes);
2982264Sjacobs free(j);
2992264Sjacobs }
3002264Sjacobs }
3012264Sjacobs
3022264Sjacobs void
papiJobListFree(papi_job_t * jobs)3032264Sjacobs papiJobListFree(papi_job_t *jobs)
3042264Sjacobs {
3052264Sjacobs if (jobs != NULL) {
3062264Sjacobs int i;
3072264Sjacobs
3082264Sjacobs for (i = 0; jobs[i] != NULL; i++)
3092264Sjacobs papiJobFree(jobs[i]);
3102264Sjacobs free(jobs);
3112264Sjacobs }
3122264Sjacobs }
313