1355b4669Sjacobs /*
2355b4669Sjacobs * CDDL HEADER START
3355b4669Sjacobs *
4355b4669Sjacobs * The contents of this file are subject to the terms of the
5355b4669Sjacobs * Common Development and Distribution License (the "License").
6355b4669Sjacobs * You may not use this file except in compliance with the License.
7355b4669Sjacobs *
8355b4669Sjacobs * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9355b4669Sjacobs * or http://www.opensolaris.org/os/licensing.
10355b4669Sjacobs * See the License for the specific language governing permissions
11355b4669Sjacobs * and limitations under the License.
12355b4669Sjacobs *
13355b4669Sjacobs * When distributing Covered Code, include this CDDL HEADER in each
14355b4669Sjacobs * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15355b4669Sjacobs * If applicable, add the following below this CDDL HEADER, with the
16355b4669Sjacobs * fields enclosed by brackets "[]" replaced with your own identifying
17355b4669Sjacobs * information: Portions Copyright [yyyy] [name of copyright owner]
18355b4669Sjacobs *
19355b4669Sjacobs * CDDL HEADER END
20355b4669Sjacobs */
21355b4669Sjacobs
22355b4669Sjacobs /*
23*b6c573b6Ssonam gupta - Sun Microsystems - Bangalore India * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
24355b4669Sjacobs *
25355b4669Sjacobs */
26355b4669Sjacobs
27355b4669Sjacobs /* $Id: cancel.c 147 2006-04-25 16:51:06Z njacobs $ */
28355b4669Sjacobs
29355b4669Sjacobs
30355b4669Sjacobs #include <stdio.h>
31355b4669Sjacobs #include <stdlib.h>
32355b4669Sjacobs #include <unistd.h>
33355b4669Sjacobs #include <string.h>
34355b4669Sjacobs #include <locale.h>
35355b4669Sjacobs #include <libintl.h>
36355b4669Sjacobs #include <papi.h>
37355b4669Sjacobs #include "common.h"
38355b4669Sjacobs
39355b4669Sjacobs static void
usage(char * program)40355b4669Sjacobs usage(char *program)
41355b4669Sjacobs {
42355b4669Sjacobs char *name;
43355b4669Sjacobs
44355b4669Sjacobs if ((name = strrchr(program, '/')) == NULL)
45355b4669Sjacobs name = program;
46355b4669Sjacobs else
47355b4669Sjacobs name++;
48355b4669Sjacobs
49355b4669Sjacobs fprintf(stdout, "Usage: %s [-u user] (printer|request-id ...)\n", name);
50355b4669Sjacobs exit(1);
51355b4669Sjacobs }
52355b4669Sjacobs
53f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India static int32_t
get_job_id_requested(papi_job_t job)54f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India get_job_id_requested(papi_job_t job) {
55f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India int32_t rid = -1;
56f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India
57f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India papi_attribute_t **list = papiJobGetAttributeList(job);
58f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India papiAttributeListGetInteger(list, NULL,
59f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India "job-id-requested", &rid);
60f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India
61f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India return (rid);
62f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India }
63f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India
64355b4669Sjacobs int
cancel_jobs_for_user(char * user,papi_encryption_t encryption,char * pname)65d6083ba5Sjc144527 cancel_jobs_for_user(char *user, papi_encryption_t encryption, char *pname) {
66d6083ba5Sjc144527
67d6083ba5Sjc144527 papi_status_t status;
68d6083ba5Sjc144527 papi_service_t svc = NULL;
69d6083ba5Sjc144527 char **printers = NULL;
70d6083ba5Sjc144527 int i, exit_code;
71d6083ba5Sjc144527
72d6083ba5Sjc144527 if (pname == NULL) {
736b5764c3Ssonam gupta - Sun Microsystems - Bangalore India status = papiServiceCreate(&svc, NULL, NULL, NULL,
74d6083ba5Sjc144527 cli_auth_callback, encryption, NULL);
75d6083ba5Sjc144527 printers = interest_list(svc);
76d6083ba5Sjc144527 papiServiceDestroy(svc);
77d6083ba5Sjc144527 } else {
78d6083ba5Sjc144527 list_append(&printers, strdup(pname));
79d6083ba5Sjc144527 }
80d6083ba5Sjc144527
81d6083ba5Sjc144527 if (printers == NULL)
82d6083ba5Sjc144527 exit(0);
83d6083ba5Sjc144527
84d6083ba5Sjc144527 for (i = 0; printers[i] != NULL; i++) {
85d6083ba5Sjc144527 char *printer = printers[i];
86d6083ba5Sjc144527
876b5764c3Ssonam gupta - Sun Microsystems - Bangalore India status = papiServiceCreate(&svc, printer, NULL, NULL,
88d6083ba5Sjc144527 cli_auth_callback, encryption, NULL);
89d6083ba5Sjc144527
90d6083ba5Sjc144527 if (status != PAPI_OK) {
91d6083ba5Sjc144527 fprintf(stderr, gettext(
92d6083ba5Sjc144527 "Failed to contact service for %s: %s\n"),
93d6083ba5Sjc144527 printer, verbose_papi_message(svc, status));
94d6083ba5Sjc144527 exit(1);
95d6083ba5Sjc144527 }
96d6083ba5Sjc144527 exit_code = berkeley_cancel_request(svc, stdout, printer, 1,
97d6083ba5Sjc144527 &user);
98d6083ba5Sjc144527
99d6083ba5Sjc144527 papiServiceDestroy(svc);
100d6083ba5Sjc144527 if (exit_code != 0)
101d6083ba5Sjc144527 break;
102d6083ba5Sjc144527 }
103d6083ba5Sjc144527 free(printers);
104d6083ba5Sjc144527 return (exit_code);
105d6083ba5Sjc144527 }
106d6083ba5Sjc144527
107d6083ba5Sjc144527 int
main(int ac,char * av[])108355b4669Sjacobs main(int ac, char *av[])
109355b4669Sjacobs {
110355b4669Sjacobs int exit_code = 0;
111355b4669Sjacobs char *user = NULL;
112355b4669Sjacobs papi_encryption_t encryption = PAPI_ENCRYPT_NEVER;
113355b4669Sjacobs int c;
114dcf1b443Ssonam gupta - Sun Microsystems - Bangalore India int32_t rid = -1;
1152cb53ad6SKeerthi Kondaka int first_dest = 0;
1162cb53ad6SKeerthi Kondaka
117355b4669Sjacobs
118355b4669Sjacobs (void) setlocale(LC_ALL, "");
119355b4669Sjacobs (void) textdomain("SUNW_OST_OSCMD");
120355b4669Sjacobs
121d6083ba5Sjc144527 if (ac == 1)
122d6083ba5Sjc144527 usage(av[0]);
123d6083ba5Sjc144527
124355b4669Sjacobs while ((c = getopt(ac, av, "Eu:")) != EOF)
125355b4669Sjacobs switch (c) {
126355b4669Sjacobs case 'E':
127355b4669Sjacobs encryption = PAPI_ENCRYPT_REQUIRED;
128355b4669Sjacobs break;
129355b4669Sjacobs case 'u':
130355b4669Sjacobs user = optarg;
131355b4669Sjacobs break;
132355b4669Sjacobs default:
133355b4669Sjacobs usage(av[0]);
134355b4669Sjacobs }
135355b4669Sjacobs
136355b4669Sjacobs for (c = optind; c < ac; c++) {
137355b4669Sjacobs papi_status_t status;
138355b4669Sjacobs papi_service_t svc = NULL;
139355b4669Sjacobs papi_job_t *jobs = NULL;
140355b4669Sjacobs char *printer = NULL;
141355b4669Sjacobs int32_t id = -1;
142355b4669Sjacobs
1432cb53ad6SKeerthi Kondaka status = papiServiceCreate(&svc, av[c], NULL, NULL,
144355b4669Sjacobs cli_auth_callback, encryption, NULL);
145355b4669Sjacobs if (status != PAPI_OK) {
1462cb53ad6SKeerthi Kondaka if (first_dest == 0) {
1472cb53ad6SKeerthi Kondaka (void) get_printer_id(av[c], &printer, &id);
1482cb53ad6SKeerthi Kondaka status = papiServiceCreate(&svc, printer, NULL,
1492cb53ad6SKeerthi Kondaka NULL, cli_auth_callback, encryption, NULL);
1502cb53ad6SKeerthi Kondaka }
1512cb53ad6SKeerthi Kondaka if (status != PAPI_OK) {
152*b6c573b6Ssonam gupta - Sun Microsystems - Bangalore India fprintf(stderr, gettext(
153*b6c573b6Ssonam gupta - Sun Microsystems - Bangalore India "Failed to contact service for %s: %s\n"),
154*b6c573b6Ssonam gupta - Sun Microsystems - Bangalore India printer,
1552cb53ad6SKeerthi Kondaka verbose_papi_message(svc, status));
156355b4669Sjacobs exit(1);
157355b4669Sjacobs }
1582cb53ad6SKeerthi Kondaka } else {
1592cb53ad6SKeerthi Kondaka first_dest = 1;
1602cb53ad6SKeerthi Kondaka printer = av[c];
1612cb53ad6SKeerthi Kondaka }
162355b4669Sjacobs
163355b4669Sjacobs #define OUT ((status == PAPI_OK) ? stdout : stderr)
164355b4669Sjacobs
165355b4669Sjacobs if (id != -1) { /* it's a job */
166*b6c573b6Ssonam gupta - Sun Microsystems - Bangalore India char *mesg = gettext("cancelled");
167355b4669Sjacobs
168f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India /*
169f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India * Check if the job-id is job-id-requested
170f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India * or job-id. If it is job-id-requested then find
171f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India * corresponding job-id and send it to cancel
172f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India */
173f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India rid = job_to_be_queried(svc, printer, id);
17406f61b77Ssonam gupta - Sun Microsystems - Bangalore India if (rid < 0) {
17506f61b77Ssonam gupta - Sun Microsystems - Bangalore India /*
17606f61b77Ssonam gupta - Sun Microsystems - Bangalore India * Either it is a remote job which cannot be
17706f61b77Ssonam gupta - Sun Microsystems - Bangalore India * cancelled based on job-id or job-id is
17806f61b77Ssonam gupta - Sun Microsystems - Bangalore India * not found
17906f61b77Ssonam gupta - Sun Microsystems - Bangalore India */
18006f61b77Ssonam gupta - Sun Microsystems - Bangalore India exit_code = 1;
181*b6c573b6Ssonam gupta - Sun Microsystems - Bangalore India fprintf(OUT, "%s-%d: %s\n",
182*b6c573b6Ssonam gupta - Sun Microsystems - Bangalore India printer, id, gettext("not-found"));
18306f61b77Ssonam gupta - Sun Microsystems - Bangalore India } else {
184f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India status = papiJobCancel(svc, printer, rid);
185d6083ba5Sjc144527 if (status == PAPI_NOT_AUTHORIZED) {
186d6083ba5Sjc144527 mesg = papiStatusString(status);
187d6083ba5Sjc144527 exit_code = 1;
188d6083ba5Sjc144527 } else if (status != PAPI_OK) {
189*b6c573b6Ssonam gupta - Sun Microsystems - Bangalore India mesg = gettext(
190*b6c573b6Ssonam gupta - Sun Microsystems - Bangalore India verbose_papi_message(
191*b6c573b6Ssonam gupta - Sun Microsystems - Bangalore India svc, status));
192355b4669Sjacobs exit_code = 1;
193355b4669Sjacobs }
194355b4669Sjacobs fprintf(OUT, "%s-%d: %s\n", printer, id, mesg);
19506f61b77Ssonam gupta - Sun Microsystems - Bangalore India }
196d6083ba5Sjc144527
197355b4669Sjacobs } else { /* it's a printer */
198d6083ba5Sjc144527 if (user == NULL) {
199d6083ba5Sjc144527
200d6083ba5Sjc144527 /* Remove first job from printer */
201d6083ba5Sjc144527
202d6083ba5Sjc144527 status = papiPrinterListJobs(svc, printer,
203d6083ba5Sjc144527 NULL, NULL, 0, &jobs);
204d6083ba5Sjc144527
205355b4669Sjacobs if (status != PAPI_OK) {
206d6083ba5Sjc144527 fprintf(stderr, gettext(
207d6083ba5Sjc144527 "ListJobs %s: %s\n"), printer,
208355b4669Sjacobs verbose_papi_message(svc, status));
209355b4669Sjacobs exit_code = 1;
210355b4669Sjacobs }
211355b4669Sjacobs
212d6083ba5Sjc144527 if (jobs != NULL && *jobs != NULL) {
213*b6c573b6Ssonam gupta - Sun Microsystems - Bangalore India char *mesg = gettext("cancelled");
214d03e4ae8SJonathan Cowper-Andrewes id = papiJobGetId(*jobs);
215355b4669Sjacobs
216d03e4ae8SJonathan Cowper-Andrewes status = papiJobCancel(svc,
217d03e4ae8SJonathan Cowper-Andrewes printer, id);
218d6083ba5Sjc144527
219d03e4ae8SJonathan Cowper-Andrewes if (status == PAPI_NOT_AUTHORIZED) {
220d03e4ae8SJonathan Cowper-Andrewes mesg = papiStatusString(status);
221d03e4ae8SJonathan Cowper-Andrewes exit_code = 1;
222d03e4ae8SJonathan Cowper-Andrewes } else if (status != PAPI_OK) {
223*b6c573b6Ssonam gupta - Sun Microsystems - Bangalore India mesg = gettext(
224*b6c573b6Ssonam gupta - Sun Microsystems - Bangalore India verbose_papi_message(
225*b6c573b6Ssonam gupta - Sun Microsystems - Bangalore India svc, status));
226d03e4ae8SJonathan Cowper-Andrewes exit_code = 1;
227d03e4ae8SJonathan Cowper-Andrewes }
228f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India /*
229f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India * If job-id-requested exists for this
230f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India * job-id then that should be displayed
231f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India */
232f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India rid = get_job_id_requested(*jobs);
233dcf1b443Ssonam gupta - Sun Microsystems - Bangalore India if (rid >= 0)
234f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India fprintf(OUT, "%s-%d: %s\n",
235f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India printer, rid, mesg);
236f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India else
237f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India fprintf(OUT, "%s-%d: %s\n",
238f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India printer, id, mesg);
239355b4669Sjacobs }
240d6083ba5Sjc144527 papiJobListFree(jobs);
241355b4669Sjacobs
242d6083ba5Sjc144527 } else {
243d6083ba5Sjc144527 /* Purging user's print jobs */
244d6083ba5Sjc144527 exit_code = cancel_jobs_for_user(user,
245d6083ba5Sjc144527 encryption, printer);
246d6083ba5Sjc144527 }
247d6083ba5Sjc144527 }
248355b4669Sjacobs papiServiceDestroy(svc);
249355b4669Sjacobs }
250355b4669Sjacobs
251d6083ba5Sjc144527 if (optind == ac)
252d6083ba5Sjc144527 exit_code = cancel_jobs_for_user(user, encryption, NULL);
253d6083ba5Sjc144527
254355b4669Sjacobs return (exit_code);
255355b4669Sjacobs }
256