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*6826Sjc144527 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 242264Sjacobs * Use is subject to license terms. 252264Sjacobs * 262264Sjacobs */ 272264Sjacobs 282264Sjacobs /* $Id: cancel.c 147 2006-04-25 16:51:06Z njacobs $ */ 292264Sjacobs 302264Sjacobs #pragma ident "%Z%%M% %I% %E% SMI" 312264Sjacobs 322264Sjacobs #include <stdio.h> 332264Sjacobs #include <stdlib.h> 342264Sjacobs #include <unistd.h> 352264Sjacobs #include <string.h> 362264Sjacobs #include <locale.h> 372264Sjacobs #include <libintl.h> 382264Sjacobs #include <papi.h> 392264Sjacobs #include "common.h" 402264Sjacobs 412264Sjacobs static void 422264Sjacobs usage(char *program) 432264Sjacobs { 442264Sjacobs char *name; 452264Sjacobs 462264Sjacobs if ((name = strrchr(program, '/')) == NULL) 472264Sjacobs name = program; 482264Sjacobs else 492264Sjacobs name++; 502264Sjacobs 512264Sjacobs fprintf(stdout, "Usage: %s [-u user] (printer|request-id ...)\n", name); 522264Sjacobs exit(1); 532264Sjacobs } 542264Sjacobs 552264Sjacobs int 56*6826Sjc144527 cancel_jobs_for_user(char *user, papi_encryption_t encryption, char *pname) { 57*6826Sjc144527 58*6826Sjc144527 papi_status_t status; 59*6826Sjc144527 papi_service_t svc = NULL; 60*6826Sjc144527 char **printers = NULL; 61*6826Sjc144527 int i, exit_code; 62*6826Sjc144527 63*6826Sjc144527 if (pname == NULL) { 64*6826Sjc144527 status = papiServiceCreate(&svc, NULL, user, NULL, 65*6826Sjc144527 cli_auth_callback, encryption, NULL); 66*6826Sjc144527 printers = interest_list(svc); 67*6826Sjc144527 papiServiceDestroy(svc); 68*6826Sjc144527 } else { 69*6826Sjc144527 list_append(&printers, strdup(pname)); 70*6826Sjc144527 } 71*6826Sjc144527 72*6826Sjc144527 if (printers == NULL) 73*6826Sjc144527 exit(0); 74*6826Sjc144527 75*6826Sjc144527 for (i = 0; printers[i] != NULL; i++) { 76*6826Sjc144527 char *printer = printers[i]; 77*6826Sjc144527 78*6826Sjc144527 status = papiServiceCreate(&svc, printer, user, NULL, 79*6826Sjc144527 cli_auth_callback, encryption, NULL); 80*6826Sjc144527 81*6826Sjc144527 if (status != PAPI_OK) { 82*6826Sjc144527 fprintf(stderr, gettext( 83*6826Sjc144527 "Failed to contact service for %s: %s\n"), 84*6826Sjc144527 printer, verbose_papi_message(svc, status)); 85*6826Sjc144527 exit(1); 86*6826Sjc144527 } 87*6826Sjc144527 exit_code = berkeley_cancel_request(svc, stdout, printer, 1, 88*6826Sjc144527 &user); 89*6826Sjc144527 90*6826Sjc144527 papiServiceDestroy(svc); 91*6826Sjc144527 if (exit_code != 0) 92*6826Sjc144527 break; 93*6826Sjc144527 } 94*6826Sjc144527 free(printers); 95*6826Sjc144527 return (exit_code); 96*6826Sjc144527 } 97*6826Sjc144527 98*6826Sjc144527 int 992264Sjacobs main(int ac, char *av[]) 1002264Sjacobs { 1012264Sjacobs int exit_code = 0; 1022264Sjacobs char *user = NULL; 1032264Sjacobs papi_encryption_t encryption = PAPI_ENCRYPT_NEVER; 1042264Sjacobs int c; 1052264Sjacobs 1062264Sjacobs (void) setlocale(LC_ALL, ""); 1072264Sjacobs (void) textdomain("SUNW_OST_OSCMD"); 1082264Sjacobs 109*6826Sjc144527 if (ac == 1) 110*6826Sjc144527 usage(av[0]); 111*6826Sjc144527 1122264Sjacobs while ((c = getopt(ac, av, "Eu:")) != EOF) 1132264Sjacobs switch (c) { 1142264Sjacobs case 'E': 1152264Sjacobs encryption = PAPI_ENCRYPT_REQUIRED; 1162264Sjacobs break; 1172264Sjacobs case 'u': 1182264Sjacobs user = optarg; 1192264Sjacobs break; 1202264Sjacobs default: 1212264Sjacobs usage(av[0]); 1222264Sjacobs } 1232264Sjacobs 1242264Sjacobs for (c = optind; c < ac; c++) { 1252264Sjacobs papi_status_t status; 1262264Sjacobs papi_service_t svc = NULL; 1272264Sjacobs papi_job_t *jobs = NULL; 1282264Sjacobs char *printer = NULL; 1292264Sjacobs int32_t id = -1; 1302264Sjacobs 1312264Sjacobs (void) get_printer_id(av[c], &printer, &id); 1322264Sjacobs 1332264Sjacobs status = papiServiceCreate(&svc, printer, user, NULL, 134*6826Sjc144527 cli_auth_callback, encryption, NULL); 1352264Sjacobs if (status != PAPI_OK) { 136*6826Sjc144527 fprintf(stderr, 137*6826Sjc144527 gettext("Failed to contact service for %s: %s\n"), 138*6826Sjc144527 printer, verbose_papi_message(svc, status)); 1392264Sjacobs exit(1); 1402264Sjacobs } 1412264Sjacobs 1422264Sjacobs #define OUT ((status == PAPI_OK) ? stdout : stderr) 1432264Sjacobs 1442264Sjacobs if (id != -1) { /* it's a job */ 1452264Sjacobs char *mesg = "cancelled"; 1462264Sjacobs 1472264Sjacobs status = papiJobCancel(svc, printer, id); 148*6826Sjc144527 if (status == PAPI_NOT_AUTHORIZED) { 149*6826Sjc144527 mesg = papiStatusString(status); 150*6826Sjc144527 exit_code = 1; 151*6826Sjc144527 } else if (status != PAPI_OK) { 1522264Sjacobs mesg = verbose_papi_message(svc, status); 1532264Sjacobs exit_code = 1; 1542264Sjacobs } 1552264Sjacobs fprintf(OUT, "%s-%d: %s\n", printer, id, mesg); 156*6826Sjc144527 1572264Sjacobs } else { /* it's a printer */ 158*6826Sjc144527 if (user == NULL) { 159*6826Sjc144527 160*6826Sjc144527 /* Remove first job from printer */ 161*6826Sjc144527 162*6826Sjc144527 status = papiPrinterListJobs(svc, printer, 163*6826Sjc144527 NULL, NULL, 0, &jobs); 164*6826Sjc144527 165*6826Sjc144527 if (status != PAPI_OK) { 166*6826Sjc144527 fprintf(stderr, gettext( 167*6826Sjc144527 "ListJobs %s: %s\n"), printer, 168*6826Sjc144527 verbose_papi_message(svc, status)); 169*6826Sjc144527 exit_code = 1; 170*6826Sjc144527 } 171*6826Sjc144527 172*6826Sjc144527 if (jobs != NULL && *jobs != NULL) { 173*6826Sjc144527 char jobid[32]; 174*6826Sjc144527 char *jid; 175*6826Sjc144527 176*6826Sjc144527 snprintf(jobid, sizeof (jobid), "%u", 177*6826Sjc144527 papiJobGetId(*jobs)); 178*6826Sjc144527 179*6826Sjc144527 jid = jobid; 180*6826Sjc144527 exit_code = berkeley_cancel_request(svc, 181*6826Sjc144527 stdout, printer, 1, &jid); 182*6826Sjc144527 183*6826Sjc144527 } 184*6826Sjc144527 papiJobListFree(jobs); 185*6826Sjc144527 186*6826Sjc144527 } else { 187*6826Sjc144527 /* Purging user's print jobs */ 188*6826Sjc144527 exit_code = cancel_jobs_for_user(user, 189*6826Sjc144527 encryption, printer); 1902264Sjacobs } 1912264Sjacobs } 1922264Sjacobs papiServiceDestroy(svc); 1932264Sjacobs } 1942264Sjacobs 195*6826Sjc144527 if (optind == ac) 196*6826Sjc144527 exit_code = cancel_jobs_for_user(user, encryption, NULL); 197*6826Sjc144527 1982264Sjacobs return (exit_code); 1992264Sjacobs } 200