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 (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
24 *
25 */
26
27 /* $Id: cancel.c 147 2006-04-25 16:51:06Z njacobs $ */
28
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <unistd.h>
33 #include <string.h>
34 #include <locale.h>
35 #include <libintl.h>
36 #include <papi.h>
37 #include "common.h"
38
39 static void
usage(char * program)40 usage(char *program)
41 {
42 char *name;
43
44 if ((name = strrchr(program, '/')) == NULL)
45 name = program;
46 else
47 name++;
48
49 fprintf(stdout, "Usage: %s [-u user] (printer|request-id ...)\n", name);
50 exit(1);
51 }
52
53 static int32_t
get_job_id_requested(papi_job_t job)54 get_job_id_requested(papi_job_t job) {
55 int32_t rid = -1;
56
57 papi_attribute_t **list = papiJobGetAttributeList(job);
58 papiAttributeListGetInteger(list, NULL,
59 "job-id-requested", &rid);
60
61 return (rid);
62 }
63
64 int
cancel_jobs_for_user(char * user,papi_encryption_t encryption,char * pname)65 cancel_jobs_for_user(char *user, papi_encryption_t encryption, char *pname) {
66
67 papi_status_t status;
68 papi_service_t svc = NULL;
69 char **printers = NULL;
70 int i, exit_code;
71
72 if (pname == NULL) {
73 status = papiServiceCreate(&svc, NULL, NULL, NULL,
74 cli_auth_callback, encryption, NULL);
75 printers = interest_list(svc);
76 papiServiceDestroy(svc);
77 } else {
78 list_append(&printers, strdup(pname));
79 }
80
81 if (printers == NULL)
82 exit(0);
83
84 for (i = 0; printers[i] != NULL; i++) {
85 char *printer = printers[i];
86
87 status = papiServiceCreate(&svc, printer, NULL, NULL,
88 cli_auth_callback, encryption, NULL);
89
90 if (status != PAPI_OK) {
91 fprintf(stderr, gettext(
92 "Failed to contact service for %s: %s\n"),
93 printer, verbose_papi_message(svc, status));
94 exit(1);
95 }
96 exit_code = berkeley_cancel_request(svc, stdout, printer, 1,
97 &user);
98
99 papiServiceDestroy(svc);
100 if (exit_code != 0)
101 break;
102 }
103 free(printers);
104 return (exit_code);
105 }
106
107 int
main(int ac,char * av[])108 main(int ac, char *av[])
109 {
110 int exit_code = 0;
111 char *user = NULL;
112 papi_encryption_t encryption = PAPI_ENCRYPT_NEVER;
113 int c;
114 int32_t rid = -1;
115 int first_dest = 0;
116
117
118 (void) setlocale(LC_ALL, "");
119 (void) textdomain("SUNW_OST_OSCMD");
120
121 if (ac == 1)
122 usage(av[0]);
123
124 while ((c = getopt(ac, av, "Eu:")) != EOF)
125 switch (c) {
126 case 'E':
127 encryption = PAPI_ENCRYPT_REQUIRED;
128 break;
129 case 'u':
130 user = optarg;
131 break;
132 default:
133 usage(av[0]);
134 }
135
136 for (c = optind; c < ac; c++) {
137 papi_status_t status;
138 papi_service_t svc = NULL;
139 papi_job_t *jobs = NULL;
140 char *printer = NULL;
141 int32_t id = -1;
142
143 status = papiServiceCreate(&svc, av[c], NULL, NULL,
144 cli_auth_callback, encryption, NULL);
145 if (status != PAPI_OK) {
146 if (first_dest == 0) {
147 (void) get_printer_id(av[c], &printer, &id);
148 status = papiServiceCreate(&svc, printer, NULL,
149 NULL, cli_auth_callback, encryption, NULL);
150 }
151 if (status != PAPI_OK) {
152 fprintf(stderr, gettext(
153 "Failed to contact service for %s: %s\n"),
154 printer,
155 verbose_papi_message(svc, status));
156 exit(1);
157 }
158 } else {
159 first_dest = 1;
160 printer = av[c];
161 }
162
163 #define OUT ((status == PAPI_OK) ? stdout : stderr)
164
165 if (id != -1) { /* it's a job */
166 char *mesg = gettext("cancelled");
167
168 /*
169 * Check if the job-id is job-id-requested
170 * or job-id. If it is job-id-requested then find
171 * corresponding job-id and send it to cancel
172 */
173 rid = job_to_be_queried(svc, printer, id);
174 if (rid < 0) {
175 /*
176 * Either it is a remote job which cannot be
177 * cancelled based on job-id or job-id is
178 * not found
179 */
180 exit_code = 1;
181 fprintf(OUT, "%s-%d: %s\n",
182 printer, id, gettext("not-found"));
183 } else {
184 status = papiJobCancel(svc, printer, rid);
185 if (status == PAPI_NOT_AUTHORIZED) {
186 mesg = papiStatusString(status);
187 exit_code = 1;
188 } else if (status != PAPI_OK) {
189 mesg = gettext(
190 verbose_papi_message(
191 svc, status));
192 exit_code = 1;
193 }
194 fprintf(OUT, "%s-%d: %s\n", printer, id, mesg);
195 }
196
197 } else { /* it's a printer */
198 if (user == NULL) {
199
200 /* Remove first job from printer */
201
202 status = papiPrinterListJobs(svc, printer,
203 NULL, NULL, 0, &jobs);
204
205 if (status != PAPI_OK) {
206 fprintf(stderr, gettext(
207 "ListJobs %s: %s\n"), printer,
208 verbose_papi_message(svc, status));
209 exit_code = 1;
210 }
211
212 if (jobs != NULL && *jobs != NULL) {
213 char *mesg = gettext("cancelled");
214 id = papiJobGetId(*jobs);
215
216 status = papiJobCancel(svc,
217 printer, id);
218
219 if (status == PAPI_NOT_AUTHORIZED) {
220 mesg = papiStatusString(status);
221 exit_code = 1;
222 } else if (status != PAPI_OK) {
223 mesg = gettext(
224 verbose_papi_message(
225 svc, status));
226 exit_code = 1;
227 }
228 /*
229 * If job-id-requested exists for this
230 * job-id then that should be displayed
231 */
232 rid = get_job_id_requested(*jobs);
233 if (rid >= 0)
234 fprintf(OUT, "%s-%d: %s\n",
235 printer, rid, mesg);
236 else
237 fprintf(OUT, "%s-%d: %s\n",
238 printer, id, mesg);
239 }
240 papiJobListFree(jobs);
241
242 } else {
243 /* Purging user's print jobs */
244 exit_code = cancel_jobs_for_user(user,
245 encryption, printer);
246 }
247 }
248 papiServiceDestroy(svc);
249 }
250
251 if (optind == ac)
252 exit_code = cancel_jobs_for_user(user, encryption, NULL);
253
254 return (exit_code);
255 }
256