1355b4669Sjacobs /* 2355b4669Sjacobs * CDDL HEADER START 3355b4669Sjacobs * 4355b4669Sjacobs * The contents of this file are subject to the terms of the 5355b4669Sjacobs * Common Development and Distribution License (the "License"). 6355b4669Sjacobs * You may not use this file except in compliance with the License. 7355b4669Sjacobs * 8355b4669Sjacobs * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9355b4669Sjacobs * or http://www.opensolaris.org/os/licensing. 10355b4669Sjacobs * See the License for the specific language governing permissions 11355b4669Sjacobs * and limitations under the License. 12355b4669Sjacobs * 13355b4669Sjacobs * When distributing Covered Code, include this CDDL HEADER in each 14355b4669Sjacobs * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15355b4669Sjacobs * If applicable, add the following below this CDDL HEADER, with the 16355b4669Sjacobs * fields enclosed by brackets "[]" replaced with your own identifying 17355b4669Sjacobs * information: Portions Copyright [yyyy] [name of copyright owner] 18355b4669Sjacobs * 19355b4669Sjacobs * CDDL HEADER END 20355b4669Sjacobs */ 21355b4669Sjacobs 22355b4669Sjacobs /* 23*edafac1fSKeerthi Kondaka * Copyright 2010 Sun Microsystems, Inc. All rights reserved. 24355b4669Sjacobs * Use is subject to license terms. 25355b4669Sjacobs */ 26355b4669Sjacobs 27179184d3Sjacobs /* $Id: job.c 179 2006-07-17 18:24:07Z njacobs $ */ 28355b4669Sjacobs 29355b4669Sjacobs #include <stdlib.h> 30355b4669Sjacobs #include <stdio.h> 31355b4669Sjacobs #include <string.h> 32355b4669Sjacobs #include <errno.h> 33355b4669Sjacobs #include <unistd.h> 34355b4669Sjacobs #include <limits.h> 35355b4669Sjacobs #include <libintl.h> 36355b4669Sjacobs #include <sys/types.h> 37355b4669Sjacobs #include <sys/stat.h> 38355b4669Sjacobs #include <fcntl.h> 39355b4669Sjacobs #include <papi_impl.h> 40355b4669Sjacobs #include <uri.h> 41355b4669Sjacobs 42355b4669Sjacobs /* 43355b4669Sjacobs * must copy files before leaving routine 44355b4669Sjacobs */ 45355b4669Sjacobs papi_status_t 46355b4669Sjacobs papiJobSubmit(papi_service_t handle, char *name, papi_attribute_t **attributes, 47355b4669Sjacobs papi_job_ticket_t *job_ticket, char **files, papi_job_t *job) 48355b4669Sjacobs { 49355b4669Sjacobs papi_status_t status = PAPI_OK; 50355b4669Sjacobs service_t *svc = handle; 51355b4669Sjacobs job_t *j = NULL; 52355b4669Sjacobs char *metadata = NULL; 53355b4669Sjacobs 54355b4669Sjacobs if ((svc == NULL) || (name == NULL) || (files == NULL) || 55355b4669Sjacobs (job == NULL)) 56355b4669Sjacobs return (PAPI_BAD_ARGUMENT); 57355b4669Sjacobs 58355b4669Sjacobs if (job_ticket != NULL) { 59355b4669Sjacobs detailed_error(svc, 60355b4669Sjacobs gettext("papiJobSubmit: job ticket not supported")); 61355b4669Sjacobs return (PAPI_OPERATION_NOT_SUPPORTED); 62355b4669Sjacobs } 63355b4669Sjacobs 64355b4669Sjacobs if ((status = service_fill_in(svc, name)) != PAPI_OK) 65355b4669Sjacobs return (status); 66355b4669Sjacobs 67355b4669Sjacobs if ((*job = j = (job_t *)calloc(1, sizeof (*j))) == NULL) { 68355b4669Sjacobs detailed_error(svc, 69355b4669Sjacobs gettext("calloc() failed")); 70355b4669Sjacobs return (PAPI_TEMPORARY_ERROR); 71355b4669Sjacobs } 72355b4669Sjacobs 73e3ccc2c3Ssonam gupta - Sun Microsystems - Bangalore India /* before creating a control file add the job-name */ 74e3ccc2c3Ssonam gupta - Sun Microsystems - Bangalore India if ((files != NULL) && (files[0] != NULL)) 75e3ccc2c3Ssonam gupta - Sun Microsystems - Bangalore India papiAttributeListAddString(&attributes, PAPI_ATTR_EXCL, 76e3ccc2c3Ssonam gupta - Sun Microsystems - Bangalore India "job-name", files[0]); 77e3ccc2c3Ssonam gupta - Sun Microsystems - Bangalore India 78355b4669Sjacobs /* create a control file */ 7954f1e6d0Sceastha (void) lpd_job_add_attributes(svc, attributes, &metadata, 80355b4669Sjacobs &j->attributes); 8154f1e6d0Sceastha 8254f1e6d0Sceastha if ((status = lpd_job_add_files(svc, attributes, files, &metadata, 8354f1e6d0Sceastha &j->attributes)) != PAPI_OK) { 8454f1e6d0Sceastha return (status); 8554f1e6d0Sceastha } 86355b4669Sjacobs 87355b4669Sjacobs /* send the job to the server */ 88355b4669Sjacobs status = lpd_submit_job(svc, metadata, &j->attributes, NULL); 89355b4669Sjacobs free(metadata); 90355b4669Sjacobs 91355b4669Sjacobs return (status); 92355b4669Sjacobs 93355b4669Sjacobs } 94355b4669Sjacobs 95355b4669Sjacobs 96355b4669Sjacobs papi_status_t 97355b4669Sjacobs papiJobSubmitByReference(papi_service_t handle, char *name, 98355b4669Sjacobs papi_attribute_t **job_attributes, 99355b4669Sjacobs papi_job_ticket_t *job_ticket, char **files, papi_job_t *job) 100355b4669Sjacobs { 101355b4669Sjacobs return (papiJobSubmit(handle, name, job_attributes, 102355b4669Sjacobs job_ticket, files, job)); 103355b4669Sjacobs } 104355b4669Sjacobs 105355b4669Sjacobs papi_status_t 106355b4669Sjacobs papiJobStreamOpen(papi_service_t handle, char *name, 107355b4669Sjacobs papi_attribute_t **attributes, 108355b4669Sjacobs papi_job_ticket_t *job_ticket, papi_stream_t *stream) 109355b4669Sjacobs { 110355b4669Sjacobs papi_status_t status = PAPI_OK; 111355b4669Sjacobs service_t *svc = handle; 112355b4669Sjacobs char *metadata = NULL; 113355b4669Sjacobs stream_t *s = NULL; 114355b4669Sjacobs 115355b4669Sjacobs if ((svc == NULL) || (name == NULL) || (stream == NULL)) 116355b4669Sjacobs return (PAPI_BAD_ARGUMENT); 117355b4669Sjacobs 118355b4669Sjacobs if (job_ticket != NULL) 119355b4669Sjacobs return (PAPI_OPERATION_NOT_SUPPORTED); 120355b4669Sjacobs 121355b4669Sjacobs if ((status = service_fill_in(svc, name)) != PAPI_OK) 122355b4669Sjacobs return (status); 123355b4669Sjacobs 124355b4669Sjacobs /* create the stream container */ 125355b4669Sjacobs if ((*stream = s = calloc(1, sizeof (*s))) == NULL) 126355b4669Sjacobs return (PAPI_TEMPORARY_ERROR); 127355b4669Sjacobs 128355b4669Sjacobs /* create the job */ 129355b4669Sjacobs if ((s->job = calloc(1, sizeof (*(s->job)))) == NULL) 130355b4669Sjacobs return (PAPI_TEMPORARY_ERROR); 131355b4669Sjacobs 132*edafac1fSKeerthi Kondaka papiAttributeListAddString(&attributes, PAPI_ATTR_EXCL, 133*edafac1fSKeerthi Kondaka "job-name", "standard input"); 134*edafac1fSKeerthi Kondaka 135355b4669Sjacobs /* process the attribute list */ 136355b4669Sjacobs lpd_job_add_attributes(svc, attributes, &metadata, &s->job->attributes); 137355b4669Sjacobs 138355b4669Sjacobs /* if we can stream, do it */ 139355b4669Sjacobs if ((svc->uri->fragment != NULL) && 140355b4669Sjacobs (strcasecmp(svc->uri->fragment, "streaming") == 0)) { 141355b4669Sjacobs char *files[] = { "standard input", NULL }; 142355b4669Sjacobs 143355b4669Sjacobs lpd_job_add_files(svc, attributes, files, &metadata, 144355b4669Sjacobs &(s->job->attributes)); 145355b4669Sjacobs status = lpd_submit_job(svc, metadata, &(s->job->attributes), 146355b4669Sjacobs &s->fd); 147355b4669Sjacobs } else { 148355b4669Sjacobs char dfname[18]; 14937c30c18SKeerthi Kondaka char buf[256]; 150355b4669Sjacobs 151355b4669Sjacobs strcpy(dfname, "/tmp/stdin-XXXXX"); 152355b4669Sjacobs 153355b4669Sjacobs if ((s->fd = mkstemp(dfname)) >= 0) 154355b4669Sjacobs s->dfname = strdup(dfname); 15537c30c18SKeerthi Kondaka if (s->job->attributes) 15637c30c18SKeerthi Kondaka papiAttributeListFree(s->job->attributes); 15737c30c18SKeerthi Kondaka s->job->attributes = NULL; 15837c30c18SKeerthi Kondaka papiAttributeListToString(attributes, " ", buf, sizeof (buf)); 15937c30c18SKeerthi Kondaka papiAttributeListFromString(&(s->job->attributes), 16037c30c18SKeerthi Kondaka PAPI_ATTR_APPEND, buf); 161355b4669Sjacobs } 162355b4669Sjacobs s->metadata = metadata; 163355b4669Sjacobs 164355b4669Sjacobs return (status); 165355b4669Sjacobs } 166355b4669Sjacobs 167355b4669Sjacobs 168355b4669Sjacobs papi_status_t 169355b4669Sjacobs papiJobStreamWrite(papi_service_t handle, papi_stream_t stream, 170355b4669Sjacobs void *buffer, size_t buflen) 171355b4669Sjacobs { 172355b4669Sjacobs service_t *svc = handle; 173355b4669Sjacobs stream_t *s = stream; 174355b4669Sjacobs 175355b4669Sjacobs if ((svc == NULL) || (stream == NULL) || (buffer == NULL) || 176355b4669Sjacobs (buflen == 0)) 177355b4669Sjacobs return (PAPI_BAD_ARGUMENT); 178355b4669Sjacobs 179355b4669Sjacobs if (write(s->fd, buffer, buflen) != buflen) 180355b4669Sjacobs return (PAPI_DEVICE_ERROR); 181355b4669Sjacobs 182355b4669Sjacobs return (PAPI_OK); 183355b4669Sjacobs } 184355b4669Sjacobs 185355b4669Sjacobs papi_status_t 186355b4669Sjacobs papiJobStreamClose(papi_service_t handle, papi_stream_t stream, papi_job_t *job) 187355b4669Sjacobs { 188355b4669Sjacobs papi_status_t status = PAPI_INTERNAL_ERROR; 189355b4669Sjacobs service_t *svc = handle; 190355b4669Sjacobs job_t *j = NULL; 191355b4669Sjacobs stream_t *s = stream; 192355b4669Sjacobs int ret; 193355b4669Sjacobs 194355b4669Sjacobs if ((svc == NULL) || (stream == NULL) || (job == NULL)) 195355b4669Sjacobs return (PAPI_BAD_ARGUMENT); 196355b4669Sjacobs 197355b4669Sjacobs close(s->fd); /* close the stream */ 198355b4669Sjacobs 199355b4669Sjacobs if (s->dfname != NULL) { /* if it is a tmpfile, print it */ 200355b4669Sjacobs char *files[2]; 201355b4669Sjacobs 202355b4669Sjacobs files[0] = s->dfname; 203355b4669Sjacobs files[1] = NULL; 204355b4669Sjacobs 205355b4669Sjacobs lpd_job_add_files(svc, s->job->attributes, files, &s->metadata, 206355b4669Sjacobs &(s->job->attributes)); 207355b4669Sjacobs status = lpd_submit_job(svc, s->metadata, 208355b4669Sjacobs &(s->job->attributes), NULL); 209355b4669Sjacobs unlink(s->dfname); 210355b4669Sjacobs free(s->dfname); 211355b4669Sjacobs } else 212355b4669Sjacobs status = PAPI_OK; 213355b4669Sjacobs 214355b4669Sjacobs if (s->metadata != NULL) 215355b4669Sjacobs free(s->metadata); 216355b4669Sjacobs 217355b4669Sjacobs *job = s->job; 218355b4669Sjacobs 219355b4669Sjacobs return (status); 220355b4669Sjacobs } 221355b4669Sjacobs 222355b4669Sjacobs papi_status_t 223355b4669Sjacobs papiJobQuery(papi_service_t handle, char *name, int32_t job_id, 224355b4669Sjacobs char **job_attributes, papi_job_t *job) 225355b4669Sjacobs { 226355b4669Sjacobs papi_status_t status = PAPI_OK; 227355b4669Sjacobs service_t *svc = handle; 228355b4669Sjacobs 229355b4669Sjacobs if ((svc == NULL) || (name == NULL) || job_id < 0) 230355b4669Sjacobs return (PAPI_BAD_ARGUMENT); 231355b4669Sjacobs 232355b4669Sjacobs if ((status = service_fill_in(svc, name)) == PAPI_OK) 233355b4669Sjacobs status = lpd_find_job_info(svc, job_id, (job_t **)job); 234355b4669Sjacobs 235355b4669Sjacobs return (status); 236355b4669Sjacobs } 237355b4669Sjacobs 238355b4669Sjacobs papi_status_t 239355b4669Sjacobs papiJobCancel(papi_service_t handle, char *name, int32_t job_id) 240355b4669Sjacobs { 241355b4669Sjacobs papi_status_t status; 242355b4669Sjacobs service_t *svc = handle; 243355b4669Sjacobs 244355b4669Sjacobs if ((svc == NULL) || (name == NULL) || (job_id < 0)) 245355b4669Sjacobs return (PAPI_BAD_ARGUMENT); 246355b4669Sjacobs 247355b4669Sjacobs if ((status = service_fill_in(svc, name)) == PAPI_OK) 248355b4669Sjacobs status = lpd_cancel_job(svc, job_id); 249355b4669Sjacobs 250355b4669Sjacobs return (status); 251355b4669Sjacobs } 252355b4669Sjacobs 253355b4669Sjacobs papi_attribute_t ** 254355b4669Sjacobs papiJobGetAttributeList(papi_job_t job) 255355b4669Sjacobs { 256355b4669Sjacobs job_t *j = (job_t *)job; 257355b4669Sjacobs 258355b4669Sjacobs if (j != NULL) 259355b4669Sjacobs return ((papi_attribute_t **)j->attributes); 260355b4669Sjacobs 261355b4669Sjacobs return (NULL); 262355b4669Sjacobs } 263355b4669Sjacobs 264355b4669Sjacobs char * 265355b4669Sjacobs papiJobGetPrinterName(papi_job_t job) 266355b4669Sjacobs { 267355b4669Sjacobs char *result = NULL; 268355b4669Sjacobs job_t *j = (job_t *)job; 269355b4669Sjacobs 270355b4669Sjacobs if (j != NULL) 271355b4669Sjacobs papiAttributeListGetString(j->attributes, NULL, 272355b4669Sjacobs "printer-name", &result); 273355b4669Sjacobs 274355b4669Sjacobs return (result); 275355b4669Sjacobs } 276355b4669Sjacobs 277355b4669Sjacobs int 278355b4669Sjacobs papiJobGetId(papi_job_t job) 279355b4669Sjacobs { 280355b4669Sjacobs int result = -1; 281355b4669Sjacobs job_t *j = (job_t *)job; 282355b4669Sjacobs 283355b4669Sjacobs if (j != NULL) 284355b4669Sjacobs papiAttributeListGetInteger(j->attributes, NULL, 285355b4669Sjacobs "job-id", &result); 286355b4669Sjacobs 287355b4669Sjacobs return (result); 288355b4669Sjacobs } 289355b4669Sjacobs 290355b4669Sjacobs void 291355b4669Sjacobs papiJobFree(papi_job_t job) 292355b4669Sjacobs { 293355b4669Sjacobs job_t *j = (job_t *)job; 294355b4669Sjacobs 295355b4669Sjacobs 296355b4669Sjacobs if (j != NULL) { 297355b4669Sjacobs papiAttributeListFree(j->attributes); 298355b4669Sjacobs free(j); 299355b4669Sjacobs } 300355b4669Sjacobs } 301355b4669Sjacobs 302355b4669Sjacobs void 303355b4669Sjacobs papiJobListFree(papi_job_t *jobs) 304355b4669Sjacobs { 305355b4669Sjacobs if (jobs != NULL) { 306355b4669Sjacobs int i; 307355b4669Sjacobs 308355b4669Sjacobs for (i = 0; jobs[i] != NULL; i++) 309355b4669Sjacobs papiJobFree(jobs[i]); 310355b4669Sjacobs free(jobs); 311355b4669Sjacobs } 312355b4669Sjacobs } 313