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.
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.
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]
23 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* $Id: job.c 179 2006-07-17 18:24:07Z njacobs $ */
36 #include <sys/types.h>
39 #include <papi_impl.h>
43 * must copy files before leaving routine
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
)
49 papi_status_t status
= PAPI_OK
;
50 service_t
*svc
= handle
;
52 char *metadata
= NULL
;
54 if ((svc
== NULL
) || (name
== NULL
) || (files
== NULL
) ||
56 return (PAPI_BAD_ARGUMENT
);
58 if (job_ticket
!= NULL
) {
60 gettext("papiJobSubmit: job ticket not supported"));
61 return (PAPI_OPERATION_NOT_SUPPORTED
);
64 if ((status
= service_fill_in(svc
, name
)) != PAPI_OK
)
67 if ((*job
= j
= (job_t
*)calloc(1, sizeof (*j
))) == NULL
) {
69 gettext("calloc() failed"));
70 return (PAPI_TEMPORARY_ERROR
);
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]);
78 /* create a control file */
79 (void) lpd_job_add_attributes(svc
, attributes
, &metadata
,
82 if ((status
= lpd_job_add_files(svc
, attributes
, files
, &metadata
,
83 &j
->attributes
)) != PAPI_OK
) {
87 /* send the job to the server */
88 status
= lpd_submit_job(svc
, metadata
, &j
->attributes
, NULL
);
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
)
101 return (papiJobSubmit(handle
, name
, job_attributes
,
102 job_ticket
, files
, job
));
106 papiJobStreamOpen(papi_service_t handle
, char *name
,
107 papi_attribute_t
**attributes
,
108 papi_job_ticket_t
*job_ticket
, papi_stream_t
*stream
)
110 papi_status_t status
= PAPI_OK
;
111 service_t
*svc
= handle
;
112 char *metadata
= NULL
;
115 if ((svc
== NULL
) || (name
== NULL
) || (stream
== NULL
))
116 return (PAPI_BAD_ARGUMENT
);
118 if (job_ticket
!= NULL
)
119 return (PAPI_OPERATION_NOT_SUPPORTED
);
121 if ((status
= service_fill_in(svc
, name
)) != PAPI_OK
)
124 /* create the stream container */
125 if ((*stream
= s
= calloc(1, sizeof (*s
))) == NULL
)
126 return (PAPI_TEMPORARY_ERROR
);
129 if ((s
->job
= calloc(1, sizeof (*(s
->job
)))) == NULL
)
130 return (PAPI_TEMPORARY_ERROR
);
132 papiAttributeListAddString(&attributes
, PAPI_ATTR_EXCL
,
133 "job-name", "standard input");
135 /* process the attribute list */
136 lpd_job_add_attributes(svc
, attributes
, &metadata
, &s
->job
->attributes
);
138 /* if we can stream, do it */
139 if ((svc
->uri
->fragment
!= NULL
) &&
140 (strcasecmp(svc
->uri
->fragment
, "streaming") == 0)) {
141 char *files
[] = { "standard input", NULL
};
143 lpd_job_add_files(svc
, attributes
, files
, &metadata
,
144 &(s
->job
->attributes
));
145 status
= lpd_submit_job(svc
, metadata
, &(s
->job
->attributes
),
151 strcpy(dfname
, "/tmp/stdin-XXXXX");
153 if ((s
->fd
= mkstemp(dfname
)) >= 0)
154 s
->dfname
= strdup(dfname
);
155 if (s
->job
->attributes
)
156 papiAttributeListFree(s
->job
->attributes
);
157 s
->job
->attributes
= NULL
;
158 papiAttributeListToString(attributes
, " ", buf
, sizeof (buf
));
159 papiAttributeListFromString(&(s
->job
->attributes
),
160 PAPI_ATTR_APPEND
, buf
);
162 s
->metadata
= metadata
;
169 papiJobStreamWrite(papi_service_t handle
, papi_stream_t stream
,
170 void *buffer
, size_t buflen
)
172 service_t
*svc
= handle
;
173 stream_t
*s
= stream
;
175 if ((svc
== NULL
) || (stream
== NULL
) || (buffer
== NULL
) ||
177 return (PAPI_BAD_ARGUMENT
);
179 if (write(s
->fd
, buffer
, buflen
) != buflen
)
180 return (PAPI_DEVICE_ERROR
);
186 papiJobStreamClose(papi_service_t handle
, papi_stream_t stream
, papi_job_t
*job
)
188 papi_status_t status
= PAPI_INTERNAL_ERROR
;
189 service_t
*svc
= handle
;
191 stream_t
*s
= stream
;
194 if ((svc
== NULL
) || (stream
== NULL
) || (job
== NULL
))
195 return (PAPI_BAD_ARGUMENT
);
197 close(s
->fd
); /* close the stream */
199 if (s
->dfname
!= NULL
) { /* if it is a tmpfile, print it */
202 files
[0] = s
->dfname
;
205 lpd_job_add_files(svc
, s
->job
->attributes
, files
, &s
->metadata
,
206 &(s
->job
->attributes
));
207 status
= lpd_submit_job(svc
, s
->metadata
,
208 &(s
->job
->attributes
), NULL
);
214 if (s
->metadata
!= NULL
)
223 papiJobQuery(papi_service_t handle
, char *name
, int32_t job_id
,
224 char **job_attributes
, papi_job_t
*job
)
226 papi_status_t status
= PAPI_OK
;
227 service_t
*svc
= handle
;
229 if ((svc
== NULL
) || (name
== NULL
) || job_id
< 0)
230 return (PAPI_BAD_ARGUMENT
);
232 if ((status
= service_fill_in(svc
, name
)) == PAPI_OK
)
233 status
= lpd_find_job_info(svc
, job_id
, (job_t
**)job
);
239 papiJobCancel(papi_service_t handle
, char *name
, int32_t job_id
)
241 papi_status_t status
;
242 service_t
*svc
= handle
;
244 if ((svc
== NULL
) || (name
== NULL
) || (job_id
< 0))
245 return (PAPI_BAD_ARGUMENT
);
247 if ((status
= service_fill_in(svc
, name
)) == PAPI_OK
)
248 status
= lpd_cancel_job(svc
, job_id
);
254 papiJobGetAttributeList(papi_job_t job
)
256 job_t
*j
= (job_t
*)job
;
259 return ((papi_attribute_t
**)j
->attributes
);
265 papiJobGetPrinterName(papi_job_t job
)
268 job_t
*j
= (job_t
*)job
;
271 papiAttributeListGetString(j
->attributes
, NULL
,
272 "printer-name", &result
);
278 papiJobGetId(papi_job_t job
)
281 job_t
*j
= (job_t
*)job
;
284 papiAttributeListGetInteger(j
->attributes
, NULL
,
291 papiJobFree(papi_job_t job
)
293 job_t
*j
= (job_t
*)job
;
297 papiAttributeListFree(j
->attributes
);
303 papiJobListFree(papi_job_t
*jobs
)
308 for (i
= 0; jobs
[i
] != NULL
; i
++)
309 papiJobFree(jobs
[i
]);