1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 * 26 */ 27 28 /* $Id: cancel.c 147 2006-04-25 16:51:06Z njacobs $ */ 29 30 31 #include <stdio.h> 32 #include <stdlib.h> 33 #include <unistd.h> 34 #include <string.h> 35 #include <locale.h> 36 #include <libintl.h> 37 #include <papi.h> 38 #include "common.h" 39 40 static void 41 usage(char *program) 42 { 43 char *name; 44 45 if ((name = strrchr(program, '/')) == NULL) 46 name = program; 47 else 48 name++; 49 50 fprintf(stdout, "Usage: %s [-u user] (printer|request-id ...)\n", name); 51 exit(1); 52 } 53 54 int 55 cancel_jobs_for_user(char *user, papi_encryption_t encryption, char *pname) { 56 57 papi_status_t status; 58 papi_service_t svc = NULL; 59 char **printers = NULL; 60 int i, exit_code; 61 62 if (pname == NULL) { 63 status = papiServiceCreate(&svc, NULL, NULL, NULL, 64 cli_auth_callback, encryption, NULL); 65 printers = interest_list(svc); 66 papiServiceDestroy(svc); 67 } else { 68 list_append(&printers, strdup(pname)); 69 } 70 71 if (printers == NULL) 72 exit(0); 73 74 for (i = 0; printers[i] != NULL; i++) { 75 char *printer = printers[i]; 76 77 status = papiServiceCreate(&svc, printer, NULL, NULL, 78 cli_auth_callback, encryption, NULL); 79 80 if (status != PAPI_OK) { 81 fprintf(stderr, gettext( 82 "Failed to contact service for %s: %s\n"), 83 printer, verbose_papi_message(svc, status)); 84 exit(1); 85 } 86 exit_code = berkeley_cancel_request(svc, stdout, printer, 1, 87 &user); 88 89 papiServiceDestroy(svc); 90 if (exit_code != 0) 91 break; 92 } 93 free(printers); 94 return (exit_code); 95 } 96 97 int 98 main(int ac, char *av[]) 99 { 100 int exit_code = 0; 101 char *user = NULL; 102 papi_encryption_t encryption = PAPI_ENCRYPT_NEVER; 103 int c; 104 105 (void) setlocale(LC_ALL, ""); 106 (void) textdomain("SUNW_OST_OSCMD"); 107 108 if (ac == 1) 109 usage(av[0]); 110 111 while ((c = getopt(ac, av, "Eu:")) != EOF) 112 switch (c) { 113 case 'E': 114 encryption = PAPI_ENCRYPT_REQUIRED; 115 break; 116 case 'u': 117 user = optarg; 118 break; 119 default: 120 usage(av[0]); 121 } 122 123 for (c = optind; c < ac; c++) { 124 papi_status_t status; 125 papi_service_t svc = NULL; 126 papi_job_t *jobs = NULL; 127 char *printer = NULL; 128 int32_t id = -1; 129 130 (void) get_printer_id(av[c], &printer, &id); 131 132 status = papiServiceCreate(&svc, printer, NULL, NULL, 133 cli_auth_callback, encryption, NULL); 134 if (status != PAPI_OK) { 135 fprintf(stderr, 136 gettext("Failed to contact service for %s: %s\n"), 137 printer, verbose_papi_message(svc, status)); 138 exit(1); 139 } 140 141 #define OUT ((status == PAPI_OK) ? stdout : stderr) 142 143 if (id != -1) { /* it's a job */ 144 char *mesg = "cancelled"; 145 146 status = papiJobCancel(svc, printer, id); 147 if (status == PAPI_NOT_AUTHORIZED) { 148 mesg = papiStatusString(status); 149 exit_code = 1; 150 } else if (status != PAPI_OK) { 151 mesg = verbose_papi_message(svc, status); 152 exit_code = 1; 153 } 154 fprintf(OUT, "%s-%d: %s\n", printer, id, mesg); 155 156 } else { /* it's a printer */ 157 if (user == NULL) { 158 159 /* Remove first job from printer */ 160 161 status = papiPrinterListJobs(svc, printer, 162 NULL, NULL, 0, &jobs); 163 164 if (status != PAPI_OK) { 165 fprintf(stderr, gettext( 166 "ListJobs %s: %s\n"), printer, 167 verbose_papi_message(svc, status)); 168 exit_code = 1; 169 } 170 171 if (jobs != NULL && *jobs != NULL) { 172 char *mesg = "cancelled"; 173 id = papiJobGetId(*jobs); 174 175 status = papiJobCancel(svc, 176 printer, id); 177 178 if (status == PAPI_NOT_AUTHORIZED) { 179 mesg = papiStatusString(status); 180 exit_code = 1; 181 } else if (status != PAPI_OK) { 182 mesg = verbose_papi_message( 183 svc, status); 184 exit_code = 1; 185 } 186 fprintf(OUT, "%s-%d: %s\n", printer, 187 id, mesg); 188 189 } 190 papiJobListFree(jobs); 191 192 } else { 193 /* Purging user's print jobs */ 194 exit_code = cancel_jobs_for_user(user, 195 encryption, printer); 196 } 197 } 198 papiServiceDestroy(svc); 199 } 200 201 if (optind == ac) 202 exit_code = cancel_jobs_for_user(user, encryption, NULL); 203 204 return (exit_code); 205 } 206