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 /* 238966SSonam.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 54*9331SSonam.Gupta@Sun.COM static int32_t 55*9331SSonam.Gupta@Sun.COM get_job_id_requested(papi_job_t job) { 56*9331SSonam.Gupta@Sun.COM int32_t rid = -1; 57*9331SSonam.Gupta@Sun.COM 58*9331SSonam.Gupta@Sun.COM papi_attribute_t **list = papiJobGetAttributeList(job); 59*9331SSonam.Gupta@Sun.COM papiAttributeListGetInteger(list, NULL, 60*9331SSonam.Gupta@Sun.COM "job-id-requested", &rid); 61*9331SSonam.Gupta@Sun.COM 62*9331SSonam.Gupta@Sun.COM return (rid); 63*9331SSonam.Gupta@Sun.COM } 64*9331SSonam.Gupta@Sun.COM 652264Sjacobs int 666826Sjc144527 cancel_jobs_for_user(char *user, papi_encryption_t encryption, char *pname) { 676826Sjc144527 686826Sjc144527 papi_status_t status; 696826Sjc144527 papi_service_t svc = NULL; 706826Sjc144527 char **printers = NULL; 716826Sjc144527 int i, exit_code; 726826Sjc144527 736826Sjc144527 if (pname == NULL) { 748966SSonam.Gupta@Sun.COM status = papiServiceCreate(&svc, NULL, NULL, NULL, 756826Sjc144527 cli_auth_callback, encryption, NULL); 766826Sjc144527 printers = interest_list(svc); 776826Sjc144527 papiServiceDestroy(svc); 786826Sjc144527 } else { 796826Sjc144527 list_append(&printers, strdup(pname)); 806826Sjc144527 } 816826Sjc144527 826826Sjc144527 if (printers == NULL) 836826Sjc144527 exit(0); 846826Sjc144527 856826Sjc144527 for (i = 0; printers[i] != NULL; i++) { 866826Sjc144527 char *printer = printers[i]; 876826Sjc144527 888966SSonam.Gupta@Sun.COM status = papiServiceCreate(&svc, printer, NULL, NULL, 896826Sjc144527 cli_auth_callback, encryption, NULL); 906826Sjc144527 916826Sjc144527 if (status != PAPI_OK) { 926826Sjc144527 fprintf(stderr, gettext( 936826Sjc144527 "Failed to contact service for %s: %s\n"), 946826Sjc144527 printer, verbose_papi_message(svc, status)); 956826Sjc144527 exit(1); 966826Sjc144527 } 976826Sjc144527 exit_code = berkeley_cancel_request(svc, stdout, printer, 1, 986826Sjc144527 &user); 996826Sjc144527 1006826Sjc144527 papiServiceDestroy(svc); 1016826Sjc144527 if (exit_code != 0) 1026826Sjc144527 break; 1036826Sjc144527 } 1046826Sjc144527 free(printers); 1056826Sjc144527 return (exit_code); 1066826Sjc144527 } 1076826Sjc144527 1086826Sjc144527 int 1092264Sjacobs main(int ac, char *av[]) 1102264Sjacobs { 1112264Sjacobs int exit_code = 0; 1122264Sjacobs char *user = NULL; 1132264Sjacobs papi_encryption_t encryption = PAPI_ENCRYPT_NEVER; 1142264Sjacobs int c; 115*9331SSonam.Gupta@Sun.COM int32_t rid = 0; 1162264Sjacobs 1172264Sjacobs (void) setlocale(LC_ALL, ""); 1182264Sjacobs (void) textdomain("SUNW_OST_OSCMD"); 1192264Sjacobs 1206826Sjc144527 if (ac == 1) 1216826Sjc144527 usage(av[0]); 1226826Sjc144527 1232264Sjacobs while ((c = getopt(ac, av, "Eu:")) != EOF) 1242264Sjacobs switch (c) { 1252264Sjacobs case 'E': 1262264Sjacobs encryption = PAPI_ENCRYPT_REQUIRED; 1272264Sjacobs break; 1282264Sjacobs case 'u': 1292264Sjacobs user = optarg; 1302264Sjacobs break; 1312264Sjacobs default: 1322264Sjacobs usage(av[0]); 1332264Sjacobs } 1342264Sjacobs 1352264Sjacobs for (c = optind; c < ac; c++) { 1362264Sjacobs papi_status_t status; 1372264Sjacobs papi_service_t svc = NULL; 1382264Sjacobs papi_job_t *jobs = NULL; 1392264Sjacobs char *printer = NULL; 1402264Sjacobs int32_t id = -1; 1412264Sjacobs 1422264Sjacobs (void) get_printer_id(av[c], &printer, &id); 1432264Sjacobs 1448966SSonam.Gupta@Sun.COM status = papiServiceCreate(&svc, printer, NULL, NULL, 1456826Sjc144527 cli_auth_callback, encryption, NULL); 1462264Sjacobs if (status != PAPI_OK) { 1476826Sjc144527 fprintf(stderr, 1486826Sjc144527 gettext("Failed to contact service for %s: %s\n"), 1496826Sjc144527 printer, verbose_papi_message(svc, status)); 1502264Sjacobs exit(1); 1512264Sjacobs } 1522264Sjacobs 1532264Sjacobs #define OUT ((status == PAPI_OK) ? stdout : stderr) 1542264Sjacobs 1552264Sjacobs if (id != -1) { /* it's a job */ 1562264Sjacobs char *mesg = "cancelled"; 1572264Sjacobs 158*9331SSonam.Gupta@Sun.COM /* 159*9331SSonam.Gupta@Sun.COM * Check if the job-id is job-id-requested 160*9331SSonam.Gupta@Sun.COM * or job-id. If it is job-id-requested then find 161*9331SSonam.Gupta@Sun.COM * corresponding job-id and send it to cancel 162*9331SSonam.Gupta@Sun.COM */ 163*9331SSonam.Gupta@Sun.COM rid = job_to_be_queried(svc, printer, id); 164*9331SSonam.Gupta@Sun.COM if (rid > 0) 165*9331SSonam.Gupta@Sun.COM status = papiJobCancel(svc, printer, rid); 166*9331SSonam.Gupta@Sun.COM else 167*9331SSonam.Gupta@Sun.COM status = papiJobCancel(svc, printer, id); 168*9331SSonam.Gupta@Sun.COM 1696826Sjc144527 if (status == PAPI_NOT_AUTHORIZED) { 1706826Sjc144527 mesg = papiStatusString(status); 1716826Sjc144527 exit_code = 1; 1726826Sjc144527 } else if (status != PAPI_OK) { 1732264Sjacobs mesg = verbose_papi_message(svc, status); 1742264Sjacobs exit_code = 1; 1752264Sjacobs } 1762264Sjacobs fprintf(OUT, "%s-%d: %s\n", printer, id, mesg); 1776826Sjc144527 1782264Sjacobs } else { /* it's a printer */ 1796826Sjc144527 if (user == NULL) { 1806826Sjc144527 1816826Sjc144527 /* Remove first job from printer */ 1826826Sjc144527 1836826Sjc144527 status = papiPrinterListJobs(svc, printer, 1846826Sjc144527 NULL, NULL, 0, &jobs); 1856826Sjc144527 1866826Sjc144527 if (status != PAPI_OK) { 1876826Sjc144527 fprintf(stderr, gettext( 1886826Sjc144527 "ListJobs %s: %s\n"), printer, 1896826Sjc144527 verbose_papi_message(svc, status)); 1906826Sjc144527 exit_code = 1; 1916826Sjc144527 } 1926826Sjc144527 1936826Sjc144527 if (jobs != NULL && *jobs != NULL) { 1948015SJonathan.Ca@Sun.COM char *mesg = "cancelled"; 1958015SJonathan.Ca@Sun.COM id = papiJobGetId(*jobs); 1968015SJonathan.Ca@Sun.COM 1978015SJonathan.Ca@Sun.COM status = papiJobCancel(svc, 1988015SJonathan.Ca@Sun.COM printer, id); 1996826Sjc144527 2008015SJonathan.Ca@Sun.COM if (status == PAPI_NOT_AUTHORIZED) { 2018015SJonathan.Ca@Sun.COM mesg = papiStatusString(status); 2028015SJonathan.Ca@Sun.COM exit_code = 1; 2038015SJonathan.Ca@Sun.COM } else if (status != PAPI_OK) { 2048015SJonathan.Ca@Sun.COM mesg = verbose_papi_message( 2058015SJonathan.Ca@Sun.COM svc, status); 2068015SJonathan.Ca@Sun.COM exit_code = 1; 2078015SJonathan.Ca@Sun.COM } 208*9331SSonam.Gupta@Sun.COM /* 209*9331SSonam.Gupta@Sun.COM * If job-id-requested exists for this 210*9331SSonam.Gupta@Sun.COM * job-id then that should be displayed 211*9331SSonam.Gupta@Sun.COM */ 212*9331SSonam.Gupta@Sun.COM rid = get_job_id_requested(*jobs); 213*9331SSonam.Gupta@Sun.COM if (rid > 0) 214*9331SSonam.Gupta@Sun.COM fprintf(OUT, "%s-%d: %s\n", 215*9331SSonam.Gupta@Sun.COM printer, rid, mesg); 216*9331SSonam.Gupta@Sun.COM else 217*9331SSonam.Gupta@Sun.COM fprintf(OUT, "%s-%d: %s\n", 218*9331SSonam.Gupta@Sun.COM printer, id, mesg); 2196826Sjc144527 } 2206826Sjc144527 papiJobListFree(jobs); 2216826Sjc144527 2226826Sjc144527 } else { 2236826Sjc144527 /* Purging user's print jobs */ 2246826Sjc144527 exit_code = cancel_jobs_for_user(user, 2256826Sjc144527 encryption, printer); 2262264Sjacobs } 2272264Sjacobs } 2282264Sjacobs papiServiceDestroy(svc); 2292264Sjacobs } 2302264Sjacobs 2316826Sjc144527 if (optind == ac) 2326826Sjc144527 exit_code = cancel_jobs_for_user(user, encryption, NULL); 2336826Sjc144527 2342264Sjacobs return (exit_code); 2352264Sjacobs } 236