xref: /titanic_44/usr/src/lib/print/libpapi-dynamic/common/job.c (revision 75ce41a57ff334bd8fe2cb9ed51eea835892f944)
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 2006 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  *
26  */
27 
28 /* $Id: job.c 146 2006-03-24 00:26:54Z njacobs $ */
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 /*LINTLIBRARY*/
33 
34 #include <stdlib.h>
35 #include <papi_impl.h>
36 
37 void
38 papiJobFree(papi_job_t job)
39 {
40 	job_t *tmp = (job_t *)job;
41 
42 	if (tmp != NULL) {
43 		void (*f)();
44 
45 		f = (void (*)())psm_sym(tmp->svc, "papiJobFree");
46 		if (f != NULL)
47 			f(tmp->job);
48 		free(tmp);
49 	}
50 }
51 
52 void
53 papiJobListFree(papi_job_t *jobs)
54 {
55 	if (jobs != NULL) {
56 		int i;
57 
58 		for (i = 0; jobs[i] != NULL; i++)
59 			papiJobFree(jobs[i]);
60 		free(jobs);
61 	}
62 }
63 
64 papi_attribute_t **
65 papiJobGetAttributeList(papi_job_t job)
66 {
67 	papi_attribute_t **result = NULL;
68 	job_t *j = job;
69 
70 	if (job != NULL) {
71 		papi_attribute_t **(*f)();
72 
73 		f = (papi_attribute_t **(*)())psm_sym(j->svc,
74 						"papiJobGetAttributeList");
75 		if (f != NULL)
76 			result = f(j->job);
77 	}
78 
79 	return (result);
80 }
81 
82 char *
83 papiJobGetPrinterName(papi_job_t job)
84 {
85 	char *result = NULL;
86 	job_t *j = job;
87 
88 	if (job != NULL) {
89 		char *(*f)();
90 
91 		f = (char *(*)())psm_sym(j->svc, "papiJobGetPrinterName");
92 		if (f != NULL)
93 			result = f(j->job);
94 	}
95 
96 	return (result);
97 }
98 
99 int32_t
100 papiJobGetId(papi_job_t job)
101 {
102 	int32_t result = -1;
103 	job_t *j = job;
104 
105 	if (job != NULL) {
106 		int32_t (*f)();
107 
108 		f = (int32_t (*)())psm_sym(j->svc, "papiJobGetId");
109 		if (f != NULL)
110 			result = f(j->job);
111 	}
112 
113 	return (result);
114 }
115 
116 papi_job_ticket_t *
117 papiJobGetJobTicket(papi_job_t job)
118 {
119 	papi_job_ticket_t *result = NULL;
120 	job_t *j = job;
121 
122 	if (job != NULL) {
123 		papi_job_ticket_t *(*f)();
124 
125 		f = (papi_job_ticket_t *(*)())psm_sym(j->svc,
126 						"papiJobGetJobTicket");
127 		if (f != NULL)
128 			result = f(j->job);
129 	}
130 
131 	return (result);
132 }
133 
134 /* common support for papiJob{Submit|SubmitByReference|Validate} */
135 static papi_status_t
136 _papi_job_submit_reference_or_validate(papi_service_t handle, char *printer,
137 		papi_attribute_t **job_attributes,
138 		papi_job_ticket_t *job_ticket, char **files, papi_job_t *job,
139 		char *function)
140 {
141 	papi_status_t result = PAPI_INTERNAL_ERROR;
142 	service_t *svc = handle;
143 	job_t *j = NULL;
144 	papi_status_t (*f)();
145 
146 	if ((svc == NULL) || (printer == NULL) || (files == NULL) ||
147 	    (job == NULL))
148 		return (PAPI_BAD_ARGUMENT);
149 
150 	if ((result = service_connect(svc, printer)) != PAPI_OK)
151 		return (result);
152 
153 	if ((*job = j = calloc(1, sizeof (*j))) == NULL)
154 		return (PAPI_TEMPORARY_ERROR);
155 
156 	j->svc = svc;
157 	f = (papi_status_t (*)())psm_sym(j->svc, function);
158 	if (f != NULL)
159 		result = f(svc->svc_handle, svc->name, job_attributes,
160 				job_ticket, files, &j->job);
161 
162 	return (result);
163 }
164 
165 papi_status_t
166 papiJobSubmit(papi_service_t handle, char *printer,
167 		papi_attribute_t **job_attributes,
168 		papi_job_ticket_t *job_ticket, char **files, papi_job_t *job)
169 {
170 	return (_papi_job_submit_reference_or_validate(handle, printer,
171 				job_attributes, job_ticket, files, job,
172 				"papiJobSubmit"));
173 }
174 
175 papi_status_t
176 papiJobSubmitByReference(papi_service_t handle, char *printer,
177 		papi_attribute_t **job_attributes,
178 		papi_job_ticket_t *job_ticket, char **files, papi_job_t *job)
179 {
180 	return (_papi_job_submit_reference_or_validate(handle, printer,
181 				job_attributes, job_ticket, files, job,
182 				"papiJobSubmitByReference"));
183 }
184 
185 papi_status_t
186 papiJobValidate(papi_service_t handle, char *printer,
187 		papi_attribute_t **job_attributes,
188 		papi_job_ticket_t *job_ticket, char **files, papi_job_t *job)
189 {
190 	return (_papi_job_submit_reference_or_validate(handle, printer,
191 				job_attributes, job_ticket, files, job,
192 				"papiJobValidate"));
193 }
194 
195 papi_status_t
196 papiJobStreamOpen(papi_service_t handle, char *printer,
197 		papi_attribute_t **job_attributes,
198 		papi_job_ticket_t *job_ticket, papi_stream_t *stream)
199 {
200 	papi_status_t result = PAPI_INTERNAL_ERROR;
201 	service_t *svc = handle;
202 	papi_status_t (*f)();
203 
204 	if ((svc == NULL) || (printer == NULL) || (stream == NULL))
205 		return (PAPI_BAD_ARGUMENT);
206 
207 	if ((result = service_connect(svc, printer)) != PAPI_OK)
208 		return (result);
209 
210 	f = (papi_status_t (*)())psm_sym(svc, "papiJobStreamOpen");
211 	if (f != NULL)
212 		result = f(svc->svc_handle, svc->name, job_attributes,
213 				job_ticket, stream);
214 
215 	return (result);
216 }
217 
218 papi_status_t
219 papiJobStreamWrite(papi_service_t handle,
220 		papi_stream_t stream, void *buffer, size_t buflen)
221 {
222 	papi_status_t result = PAPI_INTERNAL_ERROR;
223 	service_t *svc = handle;
224 	papi_status_t (*f)();
225 
226 	if ((svc == NULL) || (stream == NULL) || (buffer == NULL) ||
227 	    (buflen == 0))
228 		return (PAPI_BAD_ARGUMENT);
229 
230 	f = (papi_status_t (*)())psm_sym(svc, "papiJobStreamWrite");
231 	if (f != NULL)
232 		result = f(svc->svc_handle, stream, buffer, buflen);
233 
234 	return (result);
235 }
236 
237 papi_status_t
238 papiJobStreamClose(papi_service_t handle, papi_stream_t stream, papi_job_t *job)
239 {
240 	papi_status_t result = PAPI_INTERNAL_ERROR;
241 	service_t *svc = handle;
242 	job_t *j = NULL;
243 	papi_status_t (*f)();
244 
245 	if ((svc == NULL) || (stream == NULL) || (job == NULL))
246 		return (PAPI_BAD_ARGUMENT);
247 
248 	if ((*job = j = calloc(1, sizeof (*j))) == NULL)
249 		return (PAPI_TEMPORARY_ERROR);
250 
251 	j->svc = svc;
252 	f = (papi_status_t (*)())psm_sym(j->svc, "papiJobStreamClose");
253 	if (f != NULL)
254 		result = f(svc->svc_handle, stream, &j->job);
255 
256 	return (result);
257 }
258 
259 papi_status_t
260 papiJobQuery(papi_service_t handle, char *printer, int32_t job_id,
261 		char **requested_attrs, papi_job_t *job)
262 {
263 	papi_status_t result = PAPI_INTERNAL_ERROR;
264 	service_t *svc = handle;
265 	job_t *j = NULL;
266 	papi_status_t (*f)();
267 
268 	if ((svc == NULL) || (printer == NULL))
269 		return (PAPI_BAD_ARGUMENT);
270 
271 	if ((result = service_connect(svc, printer)) != PAPI_OK)
272 		return (result);
273 
274 	if ((*job = j = calloc(1, sizeof (*j))) == NULL)
275 		return (PAPI_TEMPORARY_ERROR);
276 
277 	j->svc = svc;
278 	f = (papi_status_t (*)())psm_sym(j->svc, "papiJobQuery");
279 	if (f != NULL)
280 		result = f(svc->svc_handle, svc->name, job_id,
281 				requested_attrs, &j->job);
282 
283 	return (result);
284 }
285 
286 papi_status_t
287 papiJobMove(papi_service_t handle, char *printer, int32_t job_id,
288 		char *destination)
289 {
290 	papi_status_t result = PAPI_INTERNAL_ERROR;
291 	service_t *svc = handle;
292 	papi_status_t (*f)();
293 
294 	if ((svc == NULL) || (printer == NULL) || (job_id < 0))
295 		return (PAPI_BAD_ARGUMENT);
296 
297 	if ((result = service_connect(svc, printer)) != PAPI_OK)
298 		return (result);
299 
300 	f = (papi_status_t (*)())psm_sym(svc, "papiJobMove");
301 	if (f != NULL) {
302 		papi_attribute_t **attrs = getprinterbyname(destination, NULL);
303 
304 		papiAttributeListGetString(attrs, NULL,
305 				"printer-uri-supported", &destination);
306 		result = f(svc->svc_handle, svc->name, job_id, destination);
307 		papiAttributeListFree(attrs);
308 	}
309 
310 	return (result);
311 }
312 
313 /* common support for papiJob{Cancel|Release|Restart|Promote} */
314 static papi_status_t
315 _papi_job_handle_printer_id(papi_service_t handle,
316 		char *printer, int32_t job_id, char *function)
317 {
318 	papi_status_t result = PAPI_INTERNAL_ERROR;
319 	service_t *svc = handle;
320 	papi_status_t (*f)();
321 
322 	if ((svc == NULL) || (printer == NULL) || (job_id < 0))
323 		return (PAPI_BAD_ARGUMENT);
324 
325 	if ((result = service_connect(svc, printer)) != PAPI_OK)
326 		return (result);
327 
328 	f = (papi_status_t (*)())psm_sym(svc, function);
329 	if (f != NULL)
330 		result = f(svc->svc_handle, svc->name, job_id);
331 
332 	return (result);
333 }
334 
335 papi_status_t
336 papiJobCancel(papi_service_t handle, char *printer, int32_t job_id)
337 {
338 	return (_papi_job_handle_printer_id(handle, printer, job_id,
339 				"papiJobCancel"));
340 }
341 
342 papi_status_t
343 papiJobRelease(papi_service_t handle, char *printer, int32_t job_id)
344 {
345 	return (_papi_job_handle_printer_id(handle, printer, job_id,
346 				"papiJobRelease"));
347 }
348 
349 papi_status_t
350 papiJobRestart(papi_service_t handle, char *printer, int32_t job_id)
351 {
352 	return (_papi_job_handle_printer_id(handle, printer, job_id,
353 				"papiJobRestart"));
354 }
355 
356 papi_status_t
357 papiJobPromote(papi_service_t handle, char *printer, int32_t job_id)
358 {
359 	return (_papi_job_handle_printer_id(handle, printer, job_id,
360 				"papiJobPromote"));
361 }
362 
363 papi_status_t
364 papiJobCommit(papi_service_t handle, char *printer, int32_t job_id)
365 {
366 	return (_papi_job_handle_printer_id(handle, printer, job_id,
367 				"papiJobCommit"));
368 }
369 
370 papi_status_t
371 papiJobHold(papi_service_t handle, char *printer, int32_t job_id)
372 {
373 	return (_papi_job_handle_printer_id(handle, printer, job_id,
374 				"papiJobHold"));
375 }
376 
377 papi_status_t
378 papiJobModify(papi_service_t handle, char *printer, int32_t job_id,
379 		papi_attribute_t **attributes, papi_job_t *job)
380 {
381 	papi_status_t result = PAPI_INTERNAL_ERROR;
382 	service_t *svc = handle;
383 	job_t *j = NULL;
384 	papi_status_t (*f)();
385 
386 	if ((svc == NULL) || (printer == NULL) || (job_id < 0) ||
387 	    (attributes == NULL))
388 		return (PAPI_BAD_ARGUMENT);
389 
390 	if ((result = service_connect(svc, printer)) != PAPI_OK)
391 		return (result);
392 
393 	if ((*job = j = calloc(1, sizeof (*j))) == NULL)
394 		return (PAPI_TEMPORARY_ERROR);
395 
396 	j->svc = svc;
397 	f = (papi_status_t (*)())psm_sym(j->svc, "papiJobModify");
398 	if (f != NULL)
399 		result = f(svc->svc_handle, svc->name, job_id, attributes,
400 				&j->job);
401 
402 	return (result);
403 }
404 
405 /*
406  * The functions defined below are private to Solaris.  They are here
407  * temporarily, until equivalent functionality makes it's way into the PAPI
408  * spec.  This is expected in the next minor version after v1.0.
409  */
410 papi_status_t
411 papiJobCreate(papi_service_t handle, char *printer,
412 		papi_attribute_t **job_attributes,
413 		papi_job_ticket_t *job_ticket, papi_job_t *job)
414 {
415 	papi_status_t result = PAPI_INTERNAL_ERROR;
416 	service_t *svc = handle;
417 	job_t *j = NULL;
418 	papi_status_t (*f)();
419 
420 	if ((svc == NULL) || (printer == NULL) || (job == NULL))
421 		return (PAPI_BAD_ARGUMENT);
422 
423 	if ((result = service_connect(svc, printer)) != PAPI_OK)
424 		return (result);
425 
426 	if ((*job = j = calloc(1, sizeof (*j))) == NULL)
427 		return (PAPI_TEMPORARY_ERROR);
428 
429 	j->svc = svc;
430 	f = (papi_status_t (*)())psm_sym(j->svc, "papiJobCreate");
431 	if (f != NULL)
432 		result = f(svc->svc_handle, svc->name, job_attributes,
433 				job_ticket, &j->job);
434 
435 	return (result);
436 }
437 
438 papi_status_t
439 papiJobStreamAdd(papi_service_t handle, char *printer, int32_t id,
440 		papi_stream_t *stream)
441 {
442 	papi_status_t result = PAPI_INTERNAL_ERROR;
443 	service_t *svc = handle;
444 	papi_status_t (*f)();
445 
446 	if ((svc == NULL) || (printer == NULL))
447 		return (PAPI_BAD_ARGUMENT);
448 
449 	if ((result = service_connect(svc, printer)) != PAPI_OK)
450 		return (result);
451 
452 	f = (papi_status_t (*)())psm_sym(svc, "papiJobStreamAdd");
453 	if (f != NULL)
454 		result = f(svc->svc_handle, svc->name, id, stream);
455 
456 	return (result);
457 }
458