1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 /* $Id: job.c 179 2006-07-17 18:24:07Z njacobs $ */ 28 29 #include <stdlib.h> 30 #include <stdio.h> 31 #include <string.h> 32 #include <errno.h> 33 #include <unistd.h> 34 #include <limits.h> 35 #include <libintl.h> 36 #include <sys/types.h> 37 #include <sys/stat.h> 38 #include <fcntl.h> 39 #include <papi_impl.h> 40 #include <uri.h> 41 42 /* 43 * must copy files before leaving routine 44 */ 45 papi_status_t 46 papiJobSubmit(papi_service_t handle, char *name, papi_attribute_t **attributes, 47 papi_job_ticket_t *job_ticket, char **files, papi_job_t *job) 48 { 49 papi_status_t status = PAPI_OK; 50 service_t *svc = handle; 51 job_t *j = NULL; 52 char *metadata = NULL; 53 54 if ((svc == NULL) || (name == NULL) || (files == NULL) || 55 (job == NULL)) 56 return (PAPI_BAD_ARGUMENT); 57 58 if (job_ticket != NULL) { 59 detailed_error(svc, 60 gettext("papiJobSubmit: job ticket not supported")); 61 return (PAPI_OPERATION_NOT_SUPPORTED); 62 } 63 64 if ((status = service_fill_in(svc, name)) != PAPI_OK) 65 return (status); 66 67 if ((*job = j = (job_t *)calloc(1, sizeof (*j))) == NULL) { 68 detailed_error(svc, 69 gettext("calloc() failed")); 70 return (PAPI_TEMPORARY_ERROR); 71 } 72 73 /* before creating a control file add the job-name */ 74 if ((files != NULL) && (files[0] != NULL)) 75 papiAttributeListAddString(&attributes, PAPI_ATTR_EXCL, 76 "job-name", files[0]); 77 78 /* create a control file */ 79 (void) lpd_job_add_attributes(svc, attributes, &metadata, 80 &j->attributes); 81 82 if ((status = lpd_job_add_files(svc, attributes, files, &metadata, 83 &j->attributes)) != PAPI_OK) { 84 return (status); 85 } 86 87 /* send the job to the server */ 88 status = lpd_submit_job(svc, metadata, &j->attributes, NULL); 89 free(metadata); 90 91 return (status); 92 93 } 94 95 96 papi_status_t 97 papiJobSubmitByReference(papi_service_t handle, char *name, 98 papi_attribute_t **job_attributes, 99 papi_job_ticket_t *job_ticket, char **files, papi_job_t *job) 100 { 101 return (papiJobSubmit(handle, name, job_attributes, 102 job_ticket, files, job)); 103 } 104 105 papi_status_t 106 papiJobStreamOpen(papi_service_t handle, char *name, 107 papi_attribute_t **attributes, 108 papi_job_ticket_t *job_ticket, papi_stream_t *stream) 109 { 110 papi_status_t status = PAPI_OK; 111 service_t *svc = handle; 112 char *metadata = NULL; 113 stream_t *s = NULL; 114 115 if ((svc == NULL) || (name == NULL) || (stream == NULL)) 116 return (PAPI_BAD_ARGUMENT); 117 118 if (job_ticket != NULL) 119 return (PAPI_OPERATION_NOT_SUPPORTED); 120 121 if ((status = service_fill_in(svc, name)) != PAPI_OK) 122 return (status); 123 124 /* create the stream container */ 125 if ((*stream = s = calloc(1, sizeof (*s))) == NULL) 126 return (PAPI_TEMPORARY_ERROR); 127 128 /* create the job */ 129 if ((s->job = calloc(1, sizeof (*(s->job)))) == NULL) 130 return (PAPI_TEMPORARY_ERROR); 131 132 /* process the attribute list */ 133 lpd_job_add_attributes(svc, attributes, &metadata, &s->job->attributes); 134 135 /* if we can stream, do it */ 136 if ((svc->uri->fragment != NULL) && 137 (strcasecmp(svc->uri->fragment, "streaming") == 0)) { 138 char *files[] = { "standard input", NULL }; 139 140 lpd_job_add_files(svc, attributes, files, &metadata, 141 &(s->job->attributes)); 142 status = lpd_submit_job(svc, metadata, &(s->job->attributes), 143 &s->fd); 144 } else { 145 char dfname[18]; 146 147 strcpy(dfname, "/tmp/stdin-XXXXX"); 148 149 if ((s->fd = mkstemp(dfname)) >= 0) 150 s->dfname = strdup(dfname); 151 } 152 s->metadata = metadata; 153 154 return (status); 155 } 156 157 158 papi_status_t 159 papiJobStreamWrite(papi_service_t handle, papi_stream_t stream, 160 void *buffer, size_t buflen) 161 { 162 service_t *svc = handle; 163 stream_t *s = stream; 164 165 if ((svc == NULL) || (stream == NULL) || (buffer == NULL) || 166 (buflen == 0)) 167 return (PAPI_BAD_ARGUMENT); 168 169 if (write(s->fd, buffer, buflen) != buflen) 170 return (PAPI_DEVICE_ERROR); 171 172 return (PAPI_OK); 173 } 174 175 papi_status_t 176 papiJobStreamClose(papi_service_t handle, papi_stream_t stream, papi_job_t *job) 177 { 178 papi_status_t status = PAPI_INTERNAL_ERROR; 179 service_t *svc = handle; 180 job_t *j = NULL; 181 stream_t *s = stream; 182 int ret; 183 184 if ((svc == NULL) || (stream == NULL) || (job == NULL)) 185 return (PAPI_BAD_ARGUMENT); 186 187 close(s->fd); /* close the stream */ 188 189 if (s->dfname != NULL) { /* if it is a tmpfile, print it */ 190 char *files[2]; 191 192 files[0] = s->dfname; 193 files[1] = NULL; 194 195 lpd_job_add_files(svc, s->job->attributes, files, &s->metadata, 196 &(s->job->attributes)); 197 status = lpd_submit_job(svc, s->metadata, 198 &(s->job->attributes), NULL); 199 unlink(s->dfname); 200 free(s->dfname); 201 } else 202 status = PAPI_OK; 203 204 if (s->metadata != NULL) 205 free(s->metadata); 206 207 *job = s->job; 208 209 return (status); 210 } 211 212 papi_status_t 213 papiJobQuery(papi_service_t handle, char *name, int32_t job_id, 214 char **job_attributes, papi_job_t *job) 215 { 216 papi_status_t status = PAPI_OK; 217 service_t *svc = handle; 218 219 if ((svc == NULL) || (name == NULL) || job_id < 0) 220 return (PAPI_BAD_ARGUMENT); 221 222 if ((status = service_fill_in(svc, name)) == PAPI_OK) 223 status = lpd_find_job_info(svc, job_id, (job_t **)job); 224 225 return (status); 226 } 227 228 papi_status_t 229 papiJobCancel(papi_service_t handle, char *name, int32_t job_id) 230 { 231 papi_status_t status; 232 service_t *svc = handle; 233 234 if ((svc == NULL) || (name == NULL) || (job_id < 0)) 235 return (PAPI_BAD_ARGUMENT); 236 237 if ((status = service_fill_in(svc, name)) == PAPI_OK) 238 status = lpd_cancel_job(svc, job_id); 239 240 return (status); 241 } 242 243 papi_attribute_t ** 244 papiJobGetAttributeList(papi_job_t job) 245 { 246 job_t *j = (job_t *)job; 247 248 if (j != NULL) 249 return ((papi_attribute_t **)j->attributes); 250 251 return (NULL); 252 } 253 254 char * 255 papiJobGetPrinterName(papi_job_t job) 256 { 257 char *result = NULL; 258 job_t *j = (job_t *)job; 259 260 if (j != NULL) 261 papiAttributeListGetString(j->attributes, NULL, 262 "printer-name", &result); 263 264 return (result); 265 } 266 267 int 268 papiJobGetId(papi_job_t job) 269 { 270 int result = -1; 271 job_t *j = (job_t *)job; 272 273 if (j != NULL) 274 papiAttributeListGetInteger(j->attributes, NULL, 275 "job-id", &result); 276 277 return (result); 278 } 279 280 void 281 papiJobFree(papi_job_t job) 282 { 283 job_t *j = (job_t *)job; 284 285 286 if (j != NULL) { 287 papiAttributeListFree(j->attributes); 288 free(j); 289 } 290 } 291 292 void 293 papiJobListFree(papi_job_t *jobs) 294 { 295 if (jobs != NULL) { 296 int i; 297 298 for (i = 0; jobs[i] != NULL; i++) 299 papiJobFree(jobs[i]); 300 free(jobs); 301 } 302 } 303