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 549331SSonam.Gupta@Sun.COM static int32_t 559331SSonam.Gupta@Sun.COM get_job_id_requested(papi_job_t job) { 569331SSonam.Gupta@Sun.COM int32_t rid = -1; 579331SSonam.Gupta@Sun.COM 589331SSonam.Gupta@Sun.COM papi_attribute_t **list = papiJobGetAttributeList(job); 599331SSonam.Gupta@Sun.COM papiAttributeListGetInteger(list, NULL, 609331SSonam.Gupta@Sun.COM "job-id-requested", &rid); 619331SSonam.Gupta@Sun.COM 629331SSonam.Gupta@Sun.COM return (rid); 639331SSonam.Gupta@Sun.COM } 649331SSonam.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*10472SSonam.Gupta@Sun.COM int32_t rid = -1; 1169426SKeerthi.Kondaka@Sun.COM int first_dest = 0; 1179426SKeerthi.Kondaka@Sun.COM 1182264Sjacobs 1192264Sjacobs (void) setlocale(LC_ALL, ""); 1202264Sjacobs (void) textdomain("SUNW_OST_OSCMD"); 1212264Sjacobs 1226826Sjc144527 if (ac == 1) 1236826Sjc144527 usage(av[0]); 1246826Sjc144527 1252264Sjacobs while ((c = getopt(ac, av, "Eu:")) != EOF) 1262264Sjacobs switch (c) { 1272264Sjacobs case 'E': 1282264Sjacobs encryption = PAPI_ENCRYPT_REQUIRED; 1292264Sjacobs break; 1302264Sjacobs case 'u': 1312264Sjacobs user = optarg; 1322264Sjacobs break; 1332264Sjacobs default: 1342264Sjacobs usage(av[0]); 1352264Sjacobs } 1362264Sjacobs 1372264Sjacobs for (c = optind; c < ac; c++) { 1382264Sjacobs papi_status_t status; 1392264Sjacobs papi_service_t svc = NULL; 1402264Sjacobs papi_job_t *jobs = NULL; 1412264Sjacobs char *printer = NULL; 1422264Sjacobs int32_t id = -1; 1432264Sjacobs 1449426SKeerthi.Kondaka@Sun.COM status = papiServiceCreate(&svc, av[c], NULL, NULL, 1456826Sjc144527 cli_auth_callback, encryption, NULL); 1462264Sjacobs if (status != PAPI_OK) { 1479426SKeerthi.Kondaka@Sun.COM if (first_dest == 0) { 1489426SKeerthi.Kondaka@Sun.COM (void) get_printer_id(av[c], &printer, &id); 1499426SKeerthi.Kondaka@Sun.COM status = papiServiceCreate(&svc, printer, NULL, 1509426SKeerthi.Kondaka@Sun.COM NULL, cli_auth_callback, encryption, NULL); 1519426SKeerthi.Kondaka@Sun.COM } 1529426SKeerthi.Kondaka@Sun.COM if (status != PAPI_OK) { 1539426SKeerthi.Kondaka@Sun.COM fprintf(stderr, 1549426SKeerthi.Kondaka@Sun.COM gettext("Failed to contact service for %s:" 1559426SKeerthi.Kondaka@Sun.COM " %s\n"), printer, 1569426SKeerthi.Kondaka@Sun.COM verbose_papi_message(svc, status)); 1579426SKeerthi.Kondaka@Sun.COM exit(1); 1589426SKeerthi.Kondaka@Sun.COM } 1599426SKeerthi.Kondaka@Sun.COM } else { 1609426SKeerthi.Kondaka@Sun.COM first_dest = 1; 1619426SKeerthi.Kondaka@Sun.COM printer = av[c]; 1622264Sjacobs } 1632264Sjacobs 1642264Sjacobs #define OUT ((status == PAPI_OK) ? stdout : stderr) 1652264Sjacobs 1662264Sjacobs if (id != -1) { /* it's a job */ 1672264Sjacobs char *mesg = "cancelled"; 1682264Sjacobs 1699331SSonam.Gupta@Sun.COM /* 1709331SSonam.Gupta@Sun.COM * Check if the job-id is job-id-requested 1719331SSonam.Gupta@Sun.COM * or job-id. If it is job-id-requested then find 1729331SSonam.Gupta@Sun.COM * corresponding job-id and send it to cancel 1739331SSonam.Gupta@Sun.COM */ 1749331SSonam.Gupta@Sun.COM rid = job_to_be_queried(svc, printer, id); 175*10472SSonam.Gupta@Sun.COM if (rid >= 0) 1769331SSonam.Gupta@Sun.COM status = papiJobCancel(svc, printer, rid); 1779331SSonam.Gupta@Sun.COM else 1789331SSonam.Gupta@Sun.COM status = papiJobCancel(svc, printer, id); 1799331SSonam.Gupta@Sun.COM 1806826Sjc144527 if (status == PAPI_NOT_AUTHORIZED) { 1816826Sjc144527 mesg = papiStatusString(status); 1826826Sjc144527 exit_code = 1; 1836826Sjc144527 } else if (status != PAPI_OK) { 1842264Sjacobs mesg = verbose_papi_message(svc, status); 1852264Sjacobs exit_code = 1; 1862264Sjacobs } 1872264Sjacobs fprintf(OUT, "%s-%d: %s\n", printer, id, mesg); 1886826Sjc144527 1892264Sjacobs } else { /* it's a printer */ 1906826Sjc144527 if (user == NULL) { 1916826Sjc144527 1926826Sjc144527 /* Remove first job from printer */ 1936826Sjc144527 1946826Sjc144527 status = papiPrinterListJobs(svc, printer, 1956826Sjc144527 NULL, NULL, 0, &jobs); 1966826Sjc144527 1976826Sjc144527 if (status != PAPI_OK) { 1986826Sjc144527 fprintf(stderr, gettext( 1996826Sjc144527 "ListJobs %s: %s\n"), printer, 2006826Sjc144527 verbose_papi_message(svc, status)); 2016826Sjc144527 exit_code = 1; 2026826Sjc144527 } 2036826Sjc144527 2046826Sjc144527 if (jobs != NULL && *jobs != NULL) { 2058015SJonathan.Ca@Sun.COM char *mesg = "cancelled"; 2068015SJonathan.Ca@Sun.COM id = papiJobGetId(*jobs); 2078015SJonathan.Ca@Sun.COM 2088015SJonathan.Ca@Sun.COM status = papiJobCancel(svc, 2098015SJonathan.Ca@Sun.COM printer, id); 2106826Sjc144527 2118015SJonathan.Ca@Sun.COM if (status == PAPI_NOT_AUTHORIZED) { 2128015SJonathan.Ca@Sun.COM mesg = papiStatusString(status); 2138015SJonathan.Ca@Sun.COM exit_code = 1; 2148015SJonathan.Ca@Sun.COM } else if (status != PAPI_OK) { 2158015SJonathan.Ca@Sun.COM mesg = verbose_papi_message( 2168015SJonathan.Ca@Sun.COM svc, status); 2178015SJonathan.Ca@Sun.COM exit_code = 1; 2188015SJonathan.Ca@Sun.COM } 2199331SSonam.Gupta@Sun.COM /* 2209331SSonam.Gupta@Sun.COM * If job-id-requested exists for this 2219331SSonam.Gupta@Sun.COM * job-id then that should be displayed 2229331SSonam.Gupta@Sun.COM */ 2239331SSonam.Gupta@Sun.COM rid = get_job_id_requested(*jobs); 224*10472SSonam.Gupta@Sun.COM if (rid >= 0) 2259331SSonam.Gupta@Sun.COM fprintf(OUT, "%s-%d: %s\n", 2269331SSonam.Gupta@Sun.COM printer, rid, mesg); 2279331SSonam.Gupta@Sun.COM else 2289331SSonam.Gupta@Sun.COM fprintf(OUT, "%s-%d: %s\n", 2299331SSonam.Gupta@Sun.COM printer, id, mesg); 2306826Sjc144527 } 2316826Sjc144527 papiJobListFree(jobs); 2326826Sjc144527 2336826Sjc144527 } else { 2346826Sjc144527 /* Purging user's print jobs */ 2356826Sjc144527 exit_code = cancel_jobs_for_user(user, 2366826Sjc144527 encryption, printer); 2372264Sjacobs } 2382264Sjacobs } 2392264Sjacobs papiServiceDestroy(svc); 2402264Sjacobs } 2412264Sjacobs 2426826Sjc144527 if (optind == ac) 2436826Sjc144527 exit_code = cancel_jobs_for_user(user, encryption, NULL); 2446826Sjc144527 2452264Sjacobs return (exit_code); 2462264Sjacobs } 247