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*8966SSonam.Gupta@Sun.COM * Copyright 2009 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 312264Sjacobs #include <stdio.h> 322264Sjacobs #include <stdlib.h> 332264Sjacobs #include <unistd.h> 342264Sjacobs #include <string.h> 352264Sjacobs #include <locale.h> 362264Sjacobs #include <libintl.h> 372264Sjacobs #include <papi.h> 382264Sjacobs #include "common.h" 392264Sjacobs 402264Sjacobs static void 412264Sjacobs usage(char *program) 422264Sjacobs { 432264Sjacobs char *name; 442264Sjacobs 452264Sjacobs if ((name = strrchr(program, '/')) == NULL) 462264Sjacobs name = program; 472264Sjacobs else 482264Sjacobs name++; 492264Sjacobs 502264Sjacobs fprintf(stdout, "Usage: %s [-u user] (printer|request-id ...)\n", name); 512264Sjacobs exit(1); 522264Sjacobs } 532264Sjacobs 542264Sjacobs int 556826Sjc144527 cancel_jobs_for_user(char *user, papi_encryption_t encryption, char *pname) { 566826Sjc144527 576826Sjc144527 papi_status_t status; 586826Sjc144527 papi_service_t svc = NULL; 596826Sjc144527 char **printers = NULL; 606826Sjc144527 int i, exit_code; 616826Sjc144527 626826Sjc144527 if (pname == NULL) { 63*8966SSonam.Gupta@Sun.COM status = papiServiceCreate(&svc, NULL, NULL, NULL, 646826Sjc144527 cli_auth_callback, encryption, NULL); 656826Sjc144527 printers = interest_list(svc); 666826Sjc144527 papiServiceDestroy(svc); 676826Sjc144527 } else { 686826Sjc144527 list_append(&printers, strdup(pname)); 696826Sjc144527 } 706826Sjc144527 716826Sjc144527 if (printers == NULL) 726826Sjc144527 exit(0); 736826Sjc144527 746826Sjc144527 for (i = 0; printers[i] != NULL; i++) { 756826Sjc144527 char *printer = printers[i]; 766826Sjc144527 77*8966SSonam.Gupta@Sun.COM status = papiServiceCreate(&svc, printer, NULL, NULL, 786826Sjc144527 cli_auth_callback, encryption, NULL); 796826Sjc144527 806826Sjc144527 if (status != PAPI_OK) { 816826Sjc144527 fprintf(stderr, gettext( 826826Sjc144527 "Failed to contact service for %s: %s\n"), 836826Sjc144527 printer, verbose_papi_message(svc, status)); 846826Sjc144527 exit(1); 856826Sjc144527 } 866826Sjc144527 exit_code = berkeley_cancel_request(svc, stdout, printer, 1, 876826Sjc144527 &user); 886826Sjc144527 896826Sjc144527 papiServiceDestroy(svc); 906826Sjc144527 if (exit_code != 0) 916826Sjc144527 break; 926826Sjc144527 } 936826Sjc144527 free(printers); 946826Sjc144527 return (exit_code); 956826Sjc144527 } 966826Sjc144527 976826Sjc144527 int 982264Sjacobs main(int ac, char *av[]) 992264Sjacobs { 1002264Sjacobs int exit_code = 0; 1012264Sjacobs char *user = NULL; 1022264Sjacobs papi_encryption_t encryption = PAPI_ENCRYPT_NEVER; 1032264Sjacobs int c; 1042264Sjacobs 1052264Sjacobs (void) setlocale(LC_ALL, ""); 1062264Sjacobs (void) textdomain("SUNW_OST_OSCMD"); 1072264Sjacobs 1086826Sjc144527 if (ac == 1) 1096826Sjc144527 usage(av[0]); 1106826Sjc144527 1112264Sjacobs while ((c = getopt(ac, av, "Eu:")) != EOF) 1122264Sjacobs switch (c) { 1132264Sjacobs case 'E': 1142264Sjacobs encryption = PAPI_ENCRYPT_REQUIRED; 1152264Sjacobs break; 1162264Sjacobs case 'u': 1172264Sjacobs user = optarg; 1182264Sjacobs break; 1192264Sjacobs default: 1202264Sjacobs usage(av[0]); 1212264Sjacobs } 1222264Sjacobs 1232264Sjacobs for (c = optind; c < ac; c++) { 1242264Sjacobs papi_status_t status; 1252264Sjacobs papi_service_t svc = NULL; 1262264Sjacobs papi_job_t *jobs = NULL; 1272264Sjacobs char *printer = NULL; 1282264Sjacobs int32_t id = -1; 1292264Sjacobs 1302264Sjacobs (void) get_printer_id(av[c], &printer, &id); 1312264Sjacobs 132*8966SSonam.Gupta@Sun.COM status = papiServiceCreate(&svc, printer, NULL, NULL, 1336826Sjc144527 cli_auth_callback, encryption, NULL); 1342264Sjacobs if (status != PAPI_OK) { 1356826Sjc144527 fprintf(stderr, 1366826Sjc144527 gettext("Failed to contact service for %s: %s\n"), 1376826Sjc144527 printer, verbose_papi_message(svc, status)); 1382264Sjacobs exit(1); 1392264Sjacobs } 1402264Sjacobs 1412264Sjacobs #define OUT ((status == PAPI_OK) ? stdout : stderr) 1422264Sjacobs 1432264Sjacobs if (id != -1) { /* it's a job */ 1442264Sjacobs char *mesg = "cancelled"; 1452264Sjacobs 1462264Sjacobs status = papiJobCancel(svc, printer, id); 1476826Sjc144527 if (status == PAPI_NOT_AUTHORIZED) { 1486826Sjc144527 mesg = papiStatusString(status); 1496826Sjc144527 exit_code = 1; 1506826Sjc144527 } else if (status != PAPI_OK) { 1512264Sjacobs mesg = verbose_papi_message(svc, status); 1522264Sjacobs exit_code = 1; 1532264Sjacobs } 1542264Sjacobs fprintf(OUT, "%s-%d: %s\n", printer, id, mesg); 1556826Sjc144527 1562264Sjacobs } else { /* it's a printer */ 1576826Sjc144527 if (user == NULL) { 1586826Sjc144527 1596826Sjc144527 /* Remove first job from printer */ 1606826Sjc144527 1616826Sjc144527 status = papiPrinterListJobs(svc, printer, 1626826Sjc144527 NULL, NULL, 0, &jobs); 1636826Sjc144527 1646826Sjc144527 if (status != PAPI_OK) { 1656826Sjc144527 fprintf(stderr, gettext( 1666826Sjc144527 "ListJobs %s: %s\n"), printer, 1676826Sjc144527 verbose_papi_message(svc, status)); 1686826Sjc144527 exit_code = 1; 1696826Sjc144527 } 1706826Sjc144527 1716826Sjc144527 if (jobs != NULL && *jobs != NULL) { 1728015SJonathan.Ca@Sun.COM char *mesg = "cancelled"; 1738015SJonathan.Ca@Sun.COM id = papiJobGetId(*jobs); 1748015SJonathan.Ca@Sun.COM 1758015SJonathan.Ca@Sun.COM status = papiJobCancel(svc, 1768015SJonathan.Ca@Sun.COM printer, id); 1776826Sjc144527 1788015SJonathan.Ca@Sun.COM if (status == PAPI_NOT_AUTHORIZED) { 1798015SJonathan.Ca@Sun.COM mesg = papiStatusString(status); 1808015SJonathan.Ca@Sun.COM exit_code = 1; 1818015SJonathan.Ca@Sun.COM } else if (status != PAPI_OK) { 1828015SJonathan.Ca@Sun.COM mesg = verbose_papi_message( 1838015SJonathan.Ca@Sun.COM svc, status); 1848015SJonathan.Ca@Sun.COM exit_code = 1; 1858015SJonathan.Ca@Sun.COM } 1868015SJonathan.Ca@Sun.COM fprintf(OUT, "%s-%d: %s\n", printer, 1878015SJonathan.Ca@Sun.COM id, mesg); 1886826Sjc144527 1896826Sjc144527 } 1906826Sjc144527 papiJobListFree(jobs); 1916826Sjc144527 1926826Sjc144527 } else { 1936826Sjc144527 /* Purging user's print jobs */ 1946826Sjc144527 exit_code = cancel_jobs_for_user(user, 1956826Sjc144527 encryption, printer); 1962264Sjacobs } 1972264Sjacobs } 1982264Sjacobs papiServiceDestroy(svc); 1992264Sjacobs } 2002264Sjacobs 2016826Sjc144527 if (optind == ac) 2026826Sjc144527 exit_code = cancel_jobs_for_user(user, encryption, NULL); 2036826Sjc144527 2042264Sjacobs return (exit_code); 2052264Sjacobs } 206