xref: /illumos-gate/usr/src/lib/print/libpapi-lpd/common/job.c (revision 89b2a9fbeabf42fa54594df0e5927bcc50a07cc9)
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 		char buf[256];
147 
148 		strcpy(dfname, "/tmp/stdin-XXXXX");
149 
150 		if ((s->fd = mkstemp(dfname)) >= 0)
151 			s->dfname = strdup(dfname);
152 		if (s->job->attributes)
153 			papiAttributeListFree(s->job->attributes);
154 		s->job->attributes = NULL;
155 		papiAttributeListToString(attributes, " ", buf, sizeof (buf));
156 		papiAttributeListFromString(&(s->job->attributes),
157 		    PAPI_ATTR_APPEND, buf);
158 	}
159 	s->metadata = metadata;
160 
161 	return (status);
162 }
163 
164 
165 papi_status_t
166 papiJobStreamWrite(papi_service_t handle, papi_stream_t stream,
167 		void *buffer, size_t buflen)
168 {
169 	service_t *svc = handle;
170 	stream_t *s = stream;
171 
172 	if ((svc == NULL) || (stream == NULL) || (buffer == NULL) ||
173 	    (buflen == 0))
174 		return (PAPI_BAD_ARGUMENT);
175 
176 	if (write(s->fd, buffer, buflen) != buflen)
177 		return (PAPI_DEVICE_ERROR);
178 
179 	return (PAPI_OK);
180 }
181 
182 papi_status_t
183 papiJobStreamClose(papi_service_t handle, papi_stream_t stream, papi_job_t *job)
184 {
185 	papi_status_t status = PAPI_INTERNAL_ERROR;
186 	service_t *svc = handle;
187 	job_t *j = NULL;
188 	stream_t *s = stream;
189 	int ret;
190 
191 	if ((svc == NULL) || (stream == NULL) || (job == NULL))
192 		return (PAPI_BAD_ARGUMENT);
193 
194 	close(s->fd);	/* close the stream */
195 
196 	if (s->dfname != NULL) {	/* if it is a tmpfile, print it */
197 		char *files[2];
198 
199 		files[0] = s->dfname;
200 		files[1] = NULL;
201 
202 		lpd_job_add_files(svc, s->job->attributes, files, &s->metadata,
203 		    &(s->job->attributes));
204 		status = lpd_submit_job(svc, s->metadata,
205 		    &(s->job->attributes), NULL);
206 		unlink(s->dfname);
207 		free(s->dfname);
208 	} else
209 		status = PAPI_OK;
210 
211 	if (s->metadata != NULL)
212 		free(s->metadata);
213 
214 	*job = s->job;
215 
216 	return (status);
217 }
218 
219 papi_status_t
220 papiJobQuery(papi_service_t handle, char *name, int32_t job_id,
221 		char **job_attributes, papi_job_t *job)
222 {
223 	papi_status_t status = PAPI_OK;
224 	service_t *svc = handle;
225 
226 	if ((svc == NULL) || (name == NULL) || job_id < 0)
227 		return (PAPI_BAD_ARGUMENT);
228 
229 	if ((status = service_fill_in(svc, name)) == PAPI_OK)
230 		status = lpd_find_job_info(svc, job_id, (job_t **)job);
231 
232 	return (status);
233 }
234 
235 papi_status_t
236 papiJobCancel(papi_service_t handle, char *name, int32_t job_id)
237 {
238 	papi_status_t status;
239 	service_t *svc = handle;
240 
241 	if ((svc == NULL) || (name == NULL) || (job_id < 0))
242 		return (PAPI_BAD_ARGUMENT);
243 
244 	if ((status = service_fill_in(svc, name)) == PAPI_OK)
245 		status = lpd_cancel_job(svc, job_id);
246 
247 	return (status);
248 }
249 
250 papi_attribute_t **
251 papiJobGetAttributeList(papi_job_t job)
252 {
253 	job_t *j = (job_t *)job;
254 
255 	if (j != NULL)
256 		return ((papi_attribute_t **)j->attributes);
257 
258 	return (NULL);
259 }
260 
261 char *
262 papiJobGetPrinterName(papi_job_t job)
263 {
264 	char *result = NULL;
265 	job_t *j = (job_t *)job;
266 
267 	if (j != NULL)
268 		papiAttributeListGetString(j->attributes, NULL,
269 		    "printer-name", &result);
270 
271 	return (result);
272 }
273 
274 int
275 papiJobGetId(papi_job_t job)
276 {
277 	int result = -1;
278 	job_t *j = (job_t *)job;
279 
280 	if (j != NULL)
281 		papiAttributeListGetInteger(j->attributes, NULL,
282 		    "job-id", &result);
283 
284 	return (result);
285 }
286 
287 void
288 papiJobFree(papi_job_t job)
289 {
290 	job_t *j = (job_t *)job;
291 
292 
293 	if (j != NULL) {
294 		papiAttributeListFree(j->attributes);
295 		free(j);
296 	}
297 }
298 
299 void
300 papiJobListFree(papi_job_t *jobs)
301 {
302 	if (jobs != NULL) {
303 		int i;
304 
305 		for (i = 0; jobs[i] != NULL; i++)
306 			papiJobFree(jobs[i]);
307 		free(jobs);
308 	}
309 }
310