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 2008 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 #pragma ident "%Z%%M% %I% %E% SMI" 31 32 #include <stdio.h> 33 #include <stdlib.h> 34 #include <unistd.h> 35 #include <string.h> 36 #include <locale.h> 37 #include <libintl.h> 38 #include <papi.h> 39 #include "common.h" 40 41 static void 42 usage(char *program) 43 { 44 char *name; 45 46 if ((name = strrchr(program, '/')) == NULL) 47 name = program; 48 else 49 name++; 50 51 fprintf(stdout, "Usage: %s [-u user] (printer|request-id ...)\n", name); 52 exit(1); 53 } 54 55 int 56 cancel_jobs_for_user(char *user, papi_encryption_t encryption, char *pname) { 57 58 papi_status_t status; 59 papi_service_t svc = NULL; 60 char **printers = NULL; 61 int i, exit_code; 62 63 if (pname == NULL) { 64 status = papiServiceCreate(&svc, NULL, user, NULL, 65 cli_auth_callback, encryption, NULL); 66 printers = interest_list(svc); 67 papiServiceDestroy(svc); 68 } else { 69 list_append(&printers, strdup(pname)); 70 } 71 72 if (printers == NULL) 73 exit(0); 74 75 for (i = 0; printers[i] != NULL; i++) { 76 char *printer = printers[i]; 77 78 status = papiServiceCreate(&svc, printer, user, NULL, 79 cli_auth_callback, encryption, NULL); 80 81 if (status != PAPI_OK) { 82 fprintf(stderr, gettext( 83 "Failed to contact service for %s: %s\n"), 84 printer, verbose_papi_message(svc, status)); 85 exit(1); 86 } 87 exit_code = berkeley_cancel_request(svc, stdout, printer, 1, 88 &user); 89 90 papiServiceDestroy(svc); 91 if (exit_code != 0) 92 break; 93 } 94 free(printers); 95 return (exit_code); 96 } 97 98 int 99 main(int ac, char *av[]) 100 { 101 int exit_code = 0; 102 char *user = NULL; 103 papi_encryption_t encryption = PAPI_ENCRYPT_NEVER; 104 int c; 105 106 (void) setlocale(LC_ALL, ""); 107 (void) textdomain("SUNW_OST_OSCMD"); 108 109 if (ac == 1) 110 usage(av[0]); 111 112 while ((c = getopt(ac, av, "Eu:")) != EOF) 113 switch (c) { 114 case 'E': 115 encryption = PAPI_ENCRYPT_REQUIRED; 116 break; 117 case 'u': 118 user = optarg; 119 break; 120 default: 121 usage(av[0]); 122 } 123 124 for (c = optind; c < ac; c++) { 125 papi_status_t status; 126 papi_service_t svc = NULL; 127 papi_job_t *jobs = NULL; 128 char *printer = NULL; 129 int32_t id = -1; 130 131 (void) get_printer_id(av[c], &printer, &id); 132 133 status = papiServiceCreate(&svc, printer, user, NULL, 134 cli_auth_callback, encryption, NULL); 135 if (status != PAPI_OK) { 136 fprintf(stderr, 137 gettext("Failed to contact service for %s: %s\n"), 138 printer, verbose_papi_message(svc, status)); 139 exit(1); 140 } 141 142 #define OUT ((status == PAPI_OK) ? stdout : stderr) 143 144 if (id != -1) { /* it's a job */ 145 char *mesg = "cancelled"; 146 147 status = papiJobCancel(svc, printer, id); 148 if (status == PAPI_NOT_AUTHORIZED) { 149 mesg = papiStatusString(status); 150 exit_code = 1; 151 } else if (status != PAPI_OK) { 152 mesg = verbose_papi_message(svc, status); 153 exit_code = 1; 154 } 155 fprintf(OUT, "%s-%d: %s\n", printer, id, mesg); 156 157 } else { /* it's a printer */ 158 if (user == NULL) { 159 160 /* Remove first job from printer */ 161 162 status = papiPrinterListJobs(svc, printer, 163 NULL, NULL, 0, &jobs); 164 165 if (status != PAPI_OK) { 166 fprintf(stderr, gettext( 167 "ListJobs %s: %s\n"), printer, 168 verbose_papi_message(svc, status)); 169 exit_code = 1; 170 } 171 172 if (jobs != NULL && *jobs != NULL) { 173 char jobid[32]; 174 char *jid; 175 176 snprintf(jobid, sizeof (jobid), "%u", 177 papiJobGetId(*jobs)); 178 179 jid = jobid; 180 exit_code = berkeley_cancel_request(svc, 181 stdout, printer, 1, &jid); 182 183 } 184 papiJobListFree(jobs); 185 186 } else { 187 /* Purging user's print jobs */ 188 exit_code = cancel_jobs_for_user(user, 189 encryption, printer); 190 } 191 } 192 papiServiceDestroy(svc); 193 } 194 195 if (optind == ac) 196 exit_code = cancel_jobs_for_user(user, encryption, NULL); 197 198 return (exit_code); 199 } 200