1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*
*/
/* $Id: job.c 148 2006-04-25 16:54:17Z njacobs $ */
/*LINTLIBRARY*/
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <papi_impl.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <libintl.h>
#ifndef OPID_CUPS_MOVE_JOB
#define OPID_CUPS_MOVE_JOB 0x400D
#endif
void
papiJobFree(papi_job_t job)
{
job_t *tmp = (job_t *)job;
if (tmp != NULL) {
if (tmp->attributes != NULL)
papiAttributeListFree(tmp->attributes);
free(tmp);
}
}
void
papiJobListFree(papi_job_t *jobs)
{
if (jobs != NULL) {
int i;
for (i = 0; jobs[i] != NULL; i++)
papiJobFree(jobs[i]);
free(jobs);
}
}
papi_attribute_t **
papiJobGetAttributeList(papi_job_t job)
{
papi_attribute_t **result = NULL;
job_t *j = job;
if (j != NULL)
result = j->attributes;
return (result);
}
char *
papiJobGetPrinterName(papi_job_t job)
{
char *result = NULL;
job_t *j = job;
if (j != NULL)
(void) papiAttributeListGetString(j->attributes, NULL,
"printer-name", &result);
return (result);
}
int32_t
papiJobGetId(papi_job_t job)
{
int32_t result = -1;
job_t *j = job;
if (j != NULL)
(void) papiAttributeListGetInteger(j->attributes, NULL,
"job-id", &result);
return (result);
}
papi_job_ticket_t *
papiJobGetJobTicket(papi_job_t job)
{
papi_job_ticket_t *result = NULL;
return (result);
}
static void
populate_job_request(service_t *svc, papi_attribute_t ***request,
papi_attribute_t **attributes, char *printer, uint16_t type)
{
papi_attribute_t **operational = NULL, **job = NULL;
static char *operational_names[] = {
"job-name", "ipp-attribute-fidelity", "document-name",
"compression", "document-format", "document-natural-language",
"job-k-octets", "job-impressions", "job-media-sheets", NULL
};
/* create the base IPP request */
ipp_initialize_request(svc, request, type);
/* create an operational attributes group */
ipp_initialize_operational_attributes(svc, &operational, printer, -1);
/* split up the attributes into operational and job attributes */
split_and_copy_attributes(operational_names, attributes,
&operational, &job);
/* add the operational attributes group to the request */
papiAttributeListAddCollection(request, PAPI_ATTR_REPLACE,
"operational-attributes-group", operational);
papiAttributeListFree(operational);
/* add the job attributes group to the request */
if (job != NULL) {
/*
* Add job-originating-host-name to attributes
* if not already set.
*/
char *hostname = NULL;
papiAttributeListGetString(job, NULL,
"job-originating-host-name", &hostname);
if (hostname == NULL) {
char host[BUFSIZ];
if (gethostname(host, sizeof (host)) == 0)
papiAttributeListAddString(&job, PAPI_ATTR_EXCL,
"job-originating-host-name", host);
}
papiAttributeListAddCollection(request, PAPI_ATTR_REPLACE,
"job-attributes-group", job);
papiAttributeListFree(job);
}
}
static papi_status_t
send_document_uri(service_t *svc, char *file, papi_attribute_t **attributes,
char *printer, int32_t id, char last, uint16_t type)
{
papi_status_t result = PAPI_INTERNAL_ERROR;
papi_attribute_t **request = NULL, **op = NULL, **response = NULL;
/* create the base IPP request */
ipp_initialize_request(svc, &request, type);
/* create an operational attributes group */
ipp_initialize_operational_attributes(svc, &op, printer, id);
papiAttributeListAddString(&op, PAPI_ATTR_REPLACE, "document-name",
file);
papiAttributeListAddBoolean(&op, PAPI_ATTR_REPLACE, "last-document",
(last ? PAPI_TRUE : PAPI_FALSE));
papiAttributeListAddCollection(&request, PAPI_ATTR_REPLACE,
"operational-attributes-group", op);
papiAttributeListFree(op);
/* send the IPP request to the server */
result = ipp_send_request_with_file(svc, request, &response, file);
papiAttributeListFree(request);
papiAttributeListFree(response);
return (result);
}
typedef enum {_WITH_DATA, _BY_REFERENCE, _VALIDATE} call_type_t;
papi_status_t
internal_job_submit(papi_service_t handle, char *printer,
papi_attribute_t **job_attributes,
papi_job_ticket_t *job_ticket,
char **files, papi_job_t *job,
call_type_t call_type)
{
papi_status_t result = PAPI_INTERNAL_ERROR;
service_t *svc = handle;
struct stat statbuf;
job_t *j = NULL;
int i;
uint16_t req_type = OPID_PRINT_JOB;
uint16_t data_type = OPID_SEND_DOCUMENT;
papi_attribute_t **request = NULL, **response = NULL;
if ((svc == NULL) || (printer == NULL) || (job == NULL))
return (PAPI_BAD_ARGUMENT);
switch (call_type) {
case _BY_REFERENCE:
#ifdef SOME_DAY_WE_WILL_BE_ABLE_TO_USE_URIS_FOR_JOB_DATA
/*
* For the time being, this is disabled. There are a number
* of issues to be dealt with before we can send a URI
* across the network to the server. For example, the file
* name(s) passed in are most likely relative to the current
* hosts filesystem. They also most likely will require some
* form of authentication information to be passed with the
* URI.
*/
req_type = OPID_PRINT_URI;
req_type = OPID_SEND_URI;
#endif
/* fall-through */
case _WITH_DATA:
if ((files == NULL) || (files[0] == NULL))
return (PAPI_BAD_ARGUMENT);
if (files[1] != NULL) /* more than 1 file */
req_type = OPID_CREATE_JOB;
break;
case _VALIDATE:
req_type = OPID_VALIDATE_JOB;
/* if we have files, validate access to them */
if (files != NULL) {
for (i = 0; files[i] != NULL; i++) {
if (access(files[i], R_OK) < 0) {
detailed_error(svc, "%s: %s", files[i],
strerror(errno));
return (PAPI_DOCUMENT_ACCESS_ERROR);
}
if (strcmp("standard input", files[i]) != 0) {
if (stat(files[i], &statbuf) < 0) {
detailed_error(svc, gettext(
"Cannot access file: %s:"
" %s"), files[i],
strerror(errno));
return (
PAPI_DOCUMENT_ACCESS_ERROR);
}
if (statbuf.st_size == 0) {
detailed_error(svc,
"Zero byte (empty) file: "
"%s",
files[i]);
return (PAPI_BAD_ARGUMENT);
}
}
}
files = NULL;
}
break;
}
/* if we are already connected, use that connection. */
if (svc->connection == NULL)
if ((result = service_connect(svc, printer)) != PAPI_OK)
return (result);
if ((*job = j = calloc(1, sizeof (*j))) == NULL)
return (PAPI_TEMPORARY_ERROR);
/*
* before creating IPP request
* add the job-name
*/
if ((files != NULL) && (files[0] != NULL))
papiAttributeListAddString(&job_attributes, PAPI_ATTR_EXCL,
"job-name", files[0]);
/* create IPP request */
populate_job_request(svc, &request, job_attributes, printer, req_type);
switch (req_type) {
case OPID_PRINT_JOB:
result = ipp_send_request_with_file(svc, request, &response,
files[0]);
break;
case OPID_CREATE_JOB:
case OPID_VALIDATE_JOB:
case OPID_PRINT_URI:
result = ipp_send_request(svc, request, &response);
break;
}
papiAttributeListFree(request);
if (result == PAPI_OK) {
papi_attribute_t **op = NULL;
/* retrieve the job attributes */
papiAttributeListGetCollection(response, NULL,
"job-attributes-group", &op);
copy_attributes(&j->attributes, op);
if (req_type == OPID_CREATE_JOB) {
int32_t id = 0;
papiAttributeListGetInteger(j->attributes, NULL,
"job-id", &id);
/* send each document */
for (i = 0; ((result == PAPI_OK) && (files[i] != NULL));
i++)
result = send_document_uri(svc, files[i],
job_attributes,
printer, id, (files[i+1]?0:1),
data_type);
}
}
papiAttributeListFree(response);
return (result);
}
papi_status_t
papiJobSubmit(papi_service_t handle, char *printer,
papi_attribute_t **job_attributes,
papi_job_ticket_t *job_ticket, char **files, papi_job_t *job)
{
return (internal_job_submit(handle, printer, job_attributes,
job_ticket, files, job, _WITH_DATA));
}
papi_status_t
papiJobSubmitByReference(papi_service_t handle, char *printer,
papi_attribute_t **job_attributes,
papi_job_ticket_t *job_ticket, char **files, papi_job_t *job)
{
return (internal_job_submit(handle, printer, job_attributes,
job_ticket, files, job, _BY_REFERENCE));
}
papi_status_t
papiJobValidate(papi_service_t handle, char *printer,
papi_attribute_t **job_attributes,
papi_job_ticket_t *job_ticket, char **files, papi_job_t *job)
{
return (internal_job_submit(handle, printer, job_attributes,
job_ticket, files, job, _VALIDATE));
}
papi_status_t
papiJobStreamOpen(papi_service_t handle, char *printer,
papi_attribute_t **job_attributes,
papi_job_ticket_t *job_ticket, papi_stream_t *stream)
{
papi_status_t result = PAPI_INTERNAL_ERROR;
papi_attribute_t **request = NULL;
service_t *svc = handle;
if ((svc == NULL) || (printer == NULL) || (stream == NULL))
return (PAPI_BAD_ARGUMENT);
/* if we are already connected, use that connection. */
if (svc->connection == NULL)
if ((result = service_connect(svc, printer)) != PAPI_OK)
return (result);
/* create job request */
populate_job_request(svc, &request, job_attributes, printer,
OPID_PRINT_JOB);
*stream = svc->connection;
result = ipp_send_initial_request_block(svc, request, 0);
papiAttributeListFree(request);
return (result);
}
papi_status_t
papiJobStreamWrite(papi_service_t handle,
papi_stream_t stream, void *buffer, size_t buflen)
{
papi_status_t result = PAPI_OK;
service_t *svc = handle;
size_t rc;
#ifdef DEBUG
printf("papiJobStreamWrite(0x%8.8x, 0x%8.8x, 0x%8.8x, %d)\n",
handle, stream, buffer, buflen);
httpDumpData(stdout, "papiJobStreamWrite:", buffer, buflen);
#endif
if ((svc == NULL) || (stream == NULL) || (buffer == NULL) ||
(buflen == 0))
return (PAPI_BAD_ARGUMENT);
while ((result == PAPI_OK) && (buflen > 0)) {
rc = ipp_request_write(svc, buffer, buflen);
if (rc < 0)
result = PAPI_TEMPORARY_ERROR;
else {
buffer = (char *)buffer + rc;
buflen -= rc;
}
}
#ifdef DEBUG
printf("papiJobStreamWrite(): %s\n", papiStatusString(result));
#endif
return (result);
}
papi_status_t
papiJobStreamClose(papi_service_t handle,
papi_stream_t stream, papi_job_t *job)
{
papi_status_t result = PAPI_INTERNAL_ERROR;
http_status_t status = HTTP_CONTINUE;
service_t *svc = handle;
papi_attribute_t **response = NULL;
job_t *j = NULL;
if ((svc == NULL) || (stream == NULL) || (job == NULL))
return (PAPI_BAD_ARGUMENT);
if ((*job = j = calloc(1, sizeof (*j))) == NULL)
return (PAPI_TEMPORARY_ERROR);
(void) ipp_request_write(svc, "", 0);
/* update our connection info */
while (status == HTTP_CONTINUE)
status = httpUpdate(svc->connection);
if (status != HTTP_OK)
return (http_to_papi_status(status));
httpWait(svc->connection, 1000);
/* read the IPP response */
result = ipp_read_message(&ipp_request_read, svc, &response,
IPP_TYPE_RESPONSE);
if (result == PAPI_OK)
result = ipp_status_info(svc, response);
if (result == PAPI_OK) {
papi_attribute_t **op = NULL;
papiAttributeListGetCollection(response, NULL,
"job-attributes-group", &op);
copy_attributes(&j->attributes, op);
}
papiAttributeListFree(response);
return (result);
}
papi_status_t
papiJobQuery(papi_service_t handle, char *printer, int32_t job_id,
char **requested_attrs,
papi_job_t *job)
{
papi_status_t result = PAPI_INTERNAL_ERROR;
service_t *svc = handle;
job_t *j = NULL;
papi_attribute_t **request = NULL, **op = NULL, **response = NULL;
if ((svc == NULL) || (printer == NULL))
return (PAPI_BAD_ARGUMENT);
/* if we are already connected, use that connection. */
if (svc->connection == NULL)
if ((result = service_connect(svc, printer)) != PAPI_OK)
return (result);
if ((*job = j = calloc(1, sizeof (*j))) == NULL)
return (PAPI_TEMPORARY_ERROR);
ipp_initialize_request(svc, &request, OPID_GET_JOB_ATTRIBUTES);
ipp_initialize_operational_attributes(svc, &op, printer, job_id);
if (requested_attrs != NULL) {
int i;
for (i = 0; requested_attrs[i] != NULL; i++)
papiAttributeListAddString(&op, PAPI_ATTR_APPEND,
"requested-attributes", requested_attrs[i]);
}
papiAttributeListAddCollection(&request, PAPI_ATTR_REPLACE,
"operational-attributes-group", op);
papiAttributeListFree(op);
result = ipp_send_request(svc, request, &response);
papiAttributeListFree(request);
op = NULL;
papiAttributeListGetCollection(response, NULL,
"job-attributes-group", &op);
copy_attributes(&j->attributes, op);
papiAttributeListFree(response);
return (result);
}
/* papiJob{Cancel|Hold|Release|Restart|Promote} are all the same */
static papi_status_t
_job_cancel_hold_release_restart_promote(papi_service_t handle,
char *printer, int32_t job_id, uint16_t type)
{
papi_status_t result = PAPI_INTERNAL_ERROR;
service_t *svc = handle;
papi_attribute_t **request = NULL, **op = NULL, **response = NULL;
if ((svc == NULL) || (printer == NULL) || (job_id < 0))
return (PAPI_BAD_ARGUMENT);
/* if we are already connected, use that connection. */
if (svc->connection == NULL)
if ((result = service_connect(svc, printer)) != PAPI_OK)
return (result);
ipp_initialize_request(svc, &request, type);
ipp_initialize_operational_attributes(svc, &op, printer, job_id);
papiAttributeListAddCollection(&request, PAPI_ATTR_REPLACE,
"operational-attributes-group", op);
papiAttributeListFree(op);
result = ipp_send_request(svc, request, &response);
papiAttributeListFree(request);
papiAttributeListFree(response);
return (result);
}
papi_status_t
papiJobCancel(papi_service_t handle, char *printer, int32_t job_id)
{
return (_job_cancel_hold_release_restart_promote(handle, printer,
job_id, OPID_CANCEL_JOB));
}
papi_status_t
papiJobHold(papi_service_t handle, char *printer, int32_t job_id)
{
return (_job_cancel_hold_release_restart_promote(handle, printer,
job_id, OPID_HOLD_JOB));
}
papi_status_t
papiJobRelease(papi_service_t handle, char *printer, int32_t job_id)
{
return (_job_cancel_hold_release_restart_promote(handle, printer,
job_id, OPID_RELEASE_JOB));
}
papi_status_t
papiJobRestart(papi_service_t handle, char *printer, int32_t job_id)
{
return (_job_cancel_hold_release_restart_promote(handle, printer,
job_id, OPID_RESTART_JOB));
}
papi_status_t
papiJobPromote(papi_service_t handle, char *printer, int32_t job_id)
{
return (_job_cancel_hold_release_restart_promote(handle, printer,
job_id, OPID_PROMOTE_JOB));
}
papi_status_t
papiJobMove(papi_service_t handle, char *printer, int32_t job_id,
char *destination)
{
papi_status_t result = PAPI_INTERNAL_ERROR;
service_t *svc = handle;
papi_attribute_t **request = NULL, **op = NULL, **response = NULL;
if ((svc == NULL) || (printer == NULL) || (job_id < 0) ||
(destination == NULL))
return (PAPI_BAD_ARGUMENT);
/* if we are already connected, use that connection. */
if (svc->connection == NULL)
if ((result = service_connect(svc, printer)) != PAPI_OK)
return (result);
ipp_initialize_request(svc, &request, OPID_CUPS_MOVE_JOB);
ipp_initialize_operational_attributes(svc, &op, printer, job_id);
papiAttributeListAddCollection(&request, PAPI_ATTR_REPLACE,
"operational-attributes-group", op);
papiAttributeListFree(op);
op = NULL;
papiAttributeListAddString(&op, PAPI_ATTR_EXCL,
"job-printer-uri", destination);
papiAttributeListAddCollection(&request, PAPI_ATTR_REPLACE,
"job-attributes-group", op);
papiAttributeListFree(op);
result = ipp_send_request(svc, request, &response);
papiAttributeListFree(request);
papiAttributeListFree(response);
return (result);
}
papi_status_t
papiJobModify(papi_service_t handle, char *printer, int32_t job_id,
papi_attribute_t **attributes, papi_job_t *job)
{
papi_status_t result = PAPI_INTERNAL_ERROR;
service_t *svc = handle;
papi_attribute_t **request = NULL, **op = NULL, **response = NULL;
job_t *j = NULL;
if ((svc == NULL) || (printer == NULL) || (job_id < 0) ||
(attributes == NULL))
return (PAPI_BAD_ARGUMENT);
if ((*job = j = calloc(1, sizeof (*j))) == NULL)
return (PAPI_TEMPORARY_ERROR);
/* if we are already connected, use that connection. */
if (svc->connection == NULL)
if ((result = service_connect(svc, printer)) != PAPI_OK)
return (result);
ipp_initialize_request(svc, &request, OPID_SET_JOB_ATTRIBUTES);
ipp_initialize_operational_attributes(svc, &op, printer, job_id);
papiAttributeListAddCollection(&request, PAPI_ATTR_REPLACE,
"operational-attributes-group", op);
papiAttributeListFree(op);
papiAttributeListAddCollection(&request, PAPI_ATTR_REPLACE,
"job-attributes-group", attributes);
result = ipp_send_request(svc, request, &response);
papiAttributeListFree(request);
op = NULL;
papiAttributeListGetCollection(response, NULL,
"job-attributes-group", &op);
copy_attributes(&j->attributes, op);
papiAttributeListFree(response);
return (result);
}
|