12264Sjacobs /*
22264Sjacobs * CDDL HEADER START
32264Sjacobs *
42264Sjacobs * The contents of this file are subject to the terms of the
52264Sjacobs * Common Development and Distribution License (the "License").
62264Sjacobs * You may not use this file except in compliance with the License.
72264Sjacobs *
82264Sjacobs * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
92264Sjacobs * or http://www.opensolaris.org/os/licensing.
102264Sjacobs * See the License for the specific language governing permissions
112264Sjacobs * and limitations under the License.
122264Sjacobs *
132264Sjacobs * When distributing Covered Code, include this CDDL HEADER in each
142264Sjacobs * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
152264Sjacobs * If applicable, add the following below this CDDL HEADER, with the
162264Sjacobs * fields enclosed by brackets "[]" replaced with your own identifying
172264Sjacobs * information: Portions Copyright [yyyy] [name of copyright owner]
182264Sjacobs *
192264Sjacobs * CDDL HEADER END
202264Sjacobs */
212264Sjacobs
222264Sjacobs /*
23*12214SSonam.Gupta@Sun.COM * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
242264Sjacobs *
252264Sjacobs */
262264Sjacobs
272264Sjacobs /* $Id: cancel.c 147 2006-04-25 16:51:06Z njacobs $ */
282264Sjacobs
292264Sjacobs
302264Sjacobs #include <stdio.h>
312264Sjacobs #include <stdlib.h>
322264Sjacobs #include <unistd.h>
332264Sjacobs #include <string.h>
342264Sjacobs #include <locale.h>
352264Sjacobs #include <libintl.h>
362264Sjacobs #include <papi.h>
372264Sjacobs #include "common.h"
382264Sjacobs
392264Sjacobs static void
usage(char * program)402264Sjacobs usage(char *program)
412264Sjacobs {
422264Sjacobs char *name;
432264Sjacobs
442264Sjacobs if ((name = strrchr(program, '/')) == NULL)
452264Sjacobs name = program;
462264Sjacobs else
472264Sjacobs name++;
482264Sjacobs
492264Sjacobs fprintf(stdout, "Usage: %s [-u user] (printer|request-id ...)\n", name);
502264Sjacobs exit(1);
512264Sjacobs }
522264Sjacobs
539331SSonam.Gupta@Sun.COM static int32_t
get_job_id_requested(papi_job_t job)549331SSonam.Gupta@Sun.COM get_job_id_requested(papi_job_t job) {
559331SSonam.Gupta@Sun.COM int32_t rid = -1;
569331SSonam.Gupta@Sun.COM
579331SSonam.Gupta@Sun.COM papi_attribute_t **list = papiJobGetAttributeList(job);
589331SSonam.Gupta@Sun.COM papiAttributeListGetInteger(list, NULL,
599331SSonam.Gupta@Sun.COM "job-id-requested", &rid);
609331SSonam.Gupta@Sun.COM
619331SSonam.Gupta@Sun.COM return (rid);
629331SSonam.Gupta@Sun.COM }
639331SSonam.Gupta@Sun.COM
642264Sjacobs int
cancel_jobs_for_user(char * user,papi_encryption_t encryption,char * pname)656826Sjc144527 cancel_jobs_for_user(char *user, papi_encryption_t encryption, char *pname) {
666826Sjc144527
676826Sjc144527 papi_status_t status;
686826Sjc144527 papi_service_t svc = NULL;
696826Sjc144527 char **printers = NULL;
706826Sjc144527 int i, exit_code;
716826Sjc144527
726826Sjc144527 if (pname == NULL) {
738966SSonam.Gupta@Sun.COM status = papiServiceCreate(&svc, NULL, NULL, NULL,
746826Sjc144527 cli_auth_callback, encryption, NULL);
756826Sjc144527 printers = interest_list(svc);
766826Sjc144527 papiServiceDestroy(svc);
776826Sjc144527 } else {
786826Sjc144527 list_append(&printers, strdup(pname));
796826Sjc144527 }
806826Sjc144527
816826Sjc144527 if (printers == NULL)
826826Sjc144527 exit(0);
836826Sjc144527
846826Sjc144527 for (i = 0; printers[i] != NULL; i++) {
856826Sjc144527 char *printer = printers[i];
866826Sjc144527
878966SSonam.Gupta@Sun.COM status = papiServiceCreate(&svc, printer, NULL, NULL,
886826Sjc144527 cli_auth_callback, encryption, NULL);
896826Sjc144527
906826Sjc144527 if (status != PAPI_OK) {
916826Sjc144527 fprintf(stderr, gettext(
926826Sjc144527 "Failed to contact service for %s: %s\n"),
936826Sjc144527 printer, verbose_papi_message(svc, status));
946826Sjc144527 exit(1);
956826Sjc144527 }
966826Sjc144527 exit_code = berkeley_cancel_request(svc, stdout, printer, 1,
976826Sjc144527 &user);
986826Sjc144527
996826Sjc144527 papiServiceDestroy(svc);
1006826Sjc144527 if (exit_code != 0)
1016826Sjc144527 break;
1026826Sjc144527 }
1036826Sjc144527 free(printers);
1046826Sjc144527 return (exit_code);
1056826Sjc144527 }
1066826Sjc144527
1076826Sjc144527 int
main(int ac,char * av[])1082264Sjacobs main(int ac, char *av[])
1092264Sjacobs {
1102264Sjacobs int exit_code = 0;
1112264Sjacobs char *user = NULL;
1122264Sjacobs papi_encryption_t encryption = PAPI_ENCRYPT_NEVER;
1132264Sjacobs int c;
11410472SSonam.Gupta@Sun.COM int32_t rid = -1;
1159426SKeerthi.Kondaka@Sun.COM int first_dest = 0;
1169426SKeerthi.Kondaka@Sun.COM
1172264Sjacobs
1182264Sjacobs (void) setlocale(LC_ALL, "");
1192264Sjacobs (void) textdomain("SUNW_OST_OSCMD");
1202264Sjacobs
1216826Sjc144527 if (ac == 1)
1226826Sjc144527 usage(av[0]);
1236826Sjc144527
1242264Sjacobs while ((c = getopt(ac, av, "Eu:")) != EOF)
1252264Sjacobs switch (c) {
1262264Sjacobs case 'E':
1272264Sjacobs encryption = PAPI_ENCRYPT_REQUIRED;
1282264Sjacobs break;
1292264Sjacobs case 'u':
1302264Sjacobs user = optarg;
1312264Sjacobs break;
1322264Sjacobs default:
1332264Sjacobs usage(av[0]);
1342264Sjacobs }
1352264Sjacobs
1362264Sjacobs for (c = optind; c < ac; c++) {
1372264Sjacobs papi_status_t status;
1382264Sjacobs papi_service_t svc = NULL;
1392264Sjacobs papi_job_t *jobs = NULL;
1402264Sjacobs char *printer = NULL;
1412264Sjacobs int32_t id = -1;
1422264Sjacobs
1439426SKeerthi.Kondaka@Sun.COM status = papiServiceCreate(&svc, av[c], NULL, NULL,
1446826Sjc144527 cli_auth_callback, encryption, NULL);
1452264Sjacobs if (status != PAPI_OK) {
1469426SKeerthi.Kondaka@Sun.COM if (first_dest == 0) {
1479426SKeerthi.Kondaka@Sun.COM (void) get_printer_id(av[c], &printer, &id);
1489426SKeerthi.Kondaka@Sun.COM status = papiServiceCreate(&svc, printer, NULL,
1499426SKeerthi.Kondaka@Sun.COM NULL, cli_auth_callback, encryption, NULL);
1509426SKeerthi.Kondaka@Sun.COM }
1519426SKeerthi.Kondaka@Sun.COM if (status != PAPI_OK) {
152*12214SSonam.Gupta@Sun.COM fprintf(stderr, gettext(
153*12214SSonam.Gupta@Sun.COM "Failed to contact service for %s: %s\n"),
154*12214SSonam.Gupta@Sun.COM printer,
1559426SKeerthi.Kondaka@Sun.COM verbose_papi_message(svc, status));
1569426SKeerthi.Kondaka@Sun.COM exit(1);
1579426SKeerthi.Kondaka@Sun.COM }
1589426SKeerthi.Kondaka@Sun.COM } else {
1599426SKeerthi.Kondaka@Sun.COM first_dest = 1;
1609426SKeerthi.Kondaka@Sun.COM printer = av[c];
1612264Sjacobs }
1622264Sjacobs
1632264Sjacobs #define OUT ((status == PAPI_OK) ? stdout : stderr)
1642264Sjacobs
1652264Sjacobs if (id != -1) { /* it's a job */
166*12214SSonam.Gupta@Sun.COM char *mesg = gettext("cancelled");
1672264Sjacobs
1689331SSonam.Gupta@Sun.COM /*
1699331SSonam.Gupta@Sun.COM * Check if the job-id is job-id-requested
1709331SSonam.Gupta@Sun.COM * or job-id. If it is job-id-requested then find
1719331SSonam.Gupta@Sun.COM * corresponding job-id and send it to cancel
1729331SSonam.Gupta@Sun.COM */
1739331SSonam.Gupta@Sun.COM rid = job_to_be_queried(svc, printer, id);
17411397SSonam.Gupta@Sun.COM if (rid < 0) {
17511397SSonam.Gupta@Sun.COM /*
17611397SSonam.Gupta@Sun.COM * Either it is a remote job which cannot be
17711397SSonam.Gupta@Sun.COM * cancelled based on job-id or job-id is
17811397SSonam.Gupta@Sun.COM * not found
17911397SSonam.Gupta@Sun.COM */
18011397SSonam.Gupta@Sun.COM exit_code = 1;
181*12214SSonam.Gupta@Sun.COM fprintf(OUT, "%s-%d: %s\n",
182*12214SSonam.Gupta@Sun.COM printer, id, gettext("not-found"));
18311397SSonam.Gupta@Sun.COM } else {
1849331SSonam.Gupta@Sun.COM status = papiJobCancel(svc, printer, rid);
18511397SSonam.Gupta@Sun.COM if (status == PAPI_NOT_AUTHORIZED) {
18611397SSonam.Gupta@Sun.COM mesg = papiStatusString(status);
18711397SSonam.Gupta@Sun.COM exit_code = 1;
18811397SSonam.Gupta@Sun.COM } else if (status != PAPI_OK) {
189*12214SSonam.Gupta@Sun.COM mesg = gettext(
190*12214SSonam.Gupta@Sun.COM verbose_papi_message(
191*12214SSonam.Gupta@Sun.COM svc, status));
19211397SSonam.Gupta@Sun.COM exit_code = 1;
19311397SSonam.Gupta@Sun.COM }
19411397SSonam.Gupta@Sun.COM fprintf(OUT, "%s-%d: %s\n", printer, id, mesg);
1952264Sjacobs }
1966826Sjc144527
1972264Sjacobs } else { /* it's a printer */
1986826Sjc144527 if (user == NULL) {
1996826Sjc144527
2006826Sjc144527 /* Remove first job from printer */
2016826Sjc144527
2026826Sjc144527 status = papiPrinterListJobs(svc, printer,
2036826Sjc144527 NULL, NULL, 0, &jobs);
2046826Sjc144527
2056826Sjc144527 if (status != PAPI_OK) {
2066826Sjc144527 fprintf(stderr, gettext(
2076826Sjc144527 "ListJobs %s: %s\n"), printer,
2086826Sjc144527 verbose_papi_message(svc, status));
2096826Sjc144527 exit_code = 1;
2106826Sjc144527 }
2116826Sjc144527
2126826Sjc144527 if (jobs != NULL && *jobs != NULL) {
213*12214SSonam.Gupta@Sun.COM char *mesg = gettext("cancelled");
2148015SJonathan.Ca@Sun.COM id = papiJobGetId(*jobs);
2158015SJonathan.Ca@Sun.COM
2168015SJonathan.Ca@Sun.COM status = papiJobCancel(svc,
2178015SJonathan.Ca@Sun.COM printer, id);
2186826Sjc144527
2198015SJonathan.Ca@Sun.COM if (status == PAPI_NOT_AUTHORIZED) {
2208015SJonathan.Ca@Sun.COM mesg = papiStatusString(status);
2218015SJonathan.Ca@Sun.COM exit_code = 1;
2228015SJonathan.Ca@Sun.COM } else if (status != PAPI_OK) {
223*12214SSonam.Gupta@Sun.COM mesg = gettext(
224*12214SSonam.Gupta@Sun.COM verbose_papi_message(
225*12214SSonam.Gupta@Sun.COM svc, status));
2268015SJonathan.Ca@Sun.COM exit_code = 1;
2278015SJonathan.Ca@Sun.COM }
2289331SSonam.Gupta@Sun.COM /*
2299331SSonam.Gupta@Sun.COM * If job-id-requested exists for this
2309331SSonam.Gupta@Sun.COM * job-id then that should be displayed
2319331SSonam.Gupta@Sun.COM */
2329331SSonam.Gupta@Sun.COM rid = get_job_id_requested(*jobs);
23310472SSonam.Gupta@Sun.COM if (rid >= 0)
2349331SSonam.Gupta@Sun.COM fprintf(OUT, "%s-%d: %s\n",
2359331SSonam.Gupta@Sun.COM printer, rid, mesg);
2369331SSonam.Gupta@Sun.COM else
2379331SSonam.Gupta@Sun.COM fprintf(OUT, "%s-%d: %s\n",
2389331SSonam.Gupta@Sun.COM printer, id, mesg);
2396826Sjc144527 }
2406826Sjc144527 papiJobListFree(jobs);
2416826Sjc144527
2426826Sjc144527 } else {
2436826Sjc144527 /* Purging user's print jobs */
2446826Sjc144527 exit_code = cancel_jobs_for_user(user,
2456826Sjc144527 encryption, printer);
2462264Sjacobs }
2472264Sjacobs }
2482264Sjacobs papiServiceDestroy(svc);
2492264Sjacobs }
2502264Sjacobs
2516826Sjc144527 if (optind == ac)
2526826Sjc144527 exit_code = cancel_jobs_for_user(user, encryption, NULL);
2536826Sjc144527
2542264Sjacobs return (exit_code);
2552264Sjacobs }
256