1355b4669Sjacobs /* 2355b4669Sjacobs * CDDL HEADER START 3355b4669Sjacobs * 4355b4669Sjacobs * The contents of this file are subject to the terms of the 5355b4669Sjacobs * Common Development and Distribution License (the "License"). 6355b4669Sjacobs * You may not use this file except in compliance with the License. 7355b4669Sjacobs * 8355b4669Sjacobs * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9355b4669Sjacobs * or http://www.opensolaris.org/os/licensing. 10355b4669Sjacobs * See the License for the specific language governing permissions 11355b4669Sjacobs * and limitations under the License. 12355b4669Sjacobs * 13355b4669Sjacobs * When distributing Covered Code, include this CDDL HEADER in each 14355b4669Sjacobs * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15355b4669Sjacobs * If applicable, add the following below this CDDL HEADER, with the 16355b4669Sjacobs * fields enclosed by brackets "[]" replaced with your own identifying 17355b4669Sjacobs * information: Portions Copyright [yyyy] [name of copyright owner] 18355b4669Sjacobs * 19355b4669Sjacobs * CDDL HEADER END 20355b4669Sjacobs */ 21355b4669Sjacobs 22355b4669Sjacobs /* 23*47129df6SKeerthi Kondaka * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved. 24355b4669Sjacobs */ 25355b4669Sjacobs 26355b4669Sjacobs /* $Id: lpmove.c 146 2006-03-24 00:26:54Z njacobs $ */ 27355b4669Sjacobs 28355b4669Sjacobs #include <stdio.h> 29355b4669Sjacobs #include <stdlib.h> 30355b4669Sjacobs #include <unistd.h> 31355b4669Sjacobs #include <string.h> 32355b4669Sjacobs #include <locale.h> 33355b4669Sjacobs #include <libintl.h> 34355b4669Sjacobs #include <papi.h> 35355b4669Sjacobs #include "common.h" 36355b4669Sjacobs 37355b4669Sjacobs static void 38355b4669Sjacobs usage(char *program) 39355b4669Sjacobs { 40355b4669Sjacobs char *name; 41355b4669Sjacobs 42355b4669Sjacobs if ((name = strrchr(program, '/')) == NULL) 43355b4669Sjacobs name = program; 44355b4669Sjacobs else 45355b4669Sjacobs name++; 46355b4669Sjacobs 47355b4669Sjacobs fprintf(stdout, 48355b4669Sjacobs gettext("Usage: %s [request-id] (destination)\n" 4937acf26aS"Nagaraj Yedathore - Sun Microsystems - Bangalore India" " %s (source) (destination)\n"), name, name); 50355b4669Sjacobs exit(1); 51355b4669Sjacobs } 52355b4669Sjacobs 53355b4669Sjacobs static int 54355b4669Sjacobs move_job(papi_service_t svc, char *src, int32_t id, char *dest) 55355b4669Sjacobs { 56355b4669Sjacobs int result = 0; 57355b4669Sjacobs papi_status_t status; 58355b4669Sjacobs char *mesg = gettext("moved"); 59355b4669Sjacobs 60355b4669Sjacobs status = papiJobMove(svc, src, id, dest); 61355b4669Sjacobs if (status != PAPI_OK) { 62355b4669Sjacobs mesg = (char *)verbose_papi_message(svc, status); 63355b4669Sjacobs result = -1; 64355b4669Sjacobs } 65355b4669Sjacobs fprintf(stderr, gettext("%s-%d to %s: %s\n"), src, id, dest, mesg); 66355b4669Sjacobs 67355b4669Sjacobs return (result); 68355b4669Sjacobs } 69355b4669Sjacobs 70355b4669Sjacobs int 71355b4669Sjacobs main(int ac, char *av[]) 72355b4669Sjacobs { 73355b4669Sjacobs int exit_code = 0; 74355b4669Sjacobs papi_encryption_t encryption = PAPI_ENCRYPT_NEVER; 75355b4669Sjacobs char *destination = NULL; 76355b4669Sjacobs int c; 77355b4669Sjacobs 78355b4669Sjacobs (void) setlocale(LC_ALL, ""); 79355b4669Sjacobs (void) textdomain("SUNW_OST_OSCMD"); 80355b4669Sjacobs 81355b4669Sjacobs while ((c = getopt(ac, av, "E:")) != EOF) 82355b4669Sjacobs switch (c) { 83355b4669Sjacobs case 'E': 84355b4669Sjacobs encryption = PAPI_ENCRYPT_REQUIRED; 85355b4669Sjacobs break; 86355b4669Sjacobs default: 87355b4669Sjacobs usage(av[0]); 88355b4669Sjacobs } 89355b4669Sjacobs 90355b4669Sjacobs if (optind >= ac - 1) 91355b4669Sjacobs usage(av[0]); 92355b4669Sjacobs 93355b4669Sjacobs destination = av[--ac]; 94355b4669Sjacobs 95355b4669Sjacobs for (c = optind; c < ac; c++) { 96355b4669Sjacobs papi_status_t status; 97355b4669Sjacobs papi_service_t svc = NULL; 98355b4669Sjacobs papi_job_t *jobs = NULL; 99355b4669Sjacobs char *printer = NULL; 100355b4669Sjacobs int32_t id = -1; 101355b4669Sjacobs 102355b4669Sjacobs (void) get_printer_id(av[c], &printer, &id); 103355b4669Sjacobs 104355b4669Sjacobs status = papiServiceCreate(&svc, printer, NULL, NULL, 105355b4669Sjacobs cli_auth_callback, encryption, NULL); 106355b4669Sjacobs if (status != PAPI_OK) { 107355b4669Sjacobs fprintf(stderr, gettext( 108355b4669Sjacobs "Failed to contact service for %s: %s\n"), 109355b4669Sjacobs printer, verbose_papi_message(svc, status)); 110355b4669Sjacobs exit(1); 111355b4669Sjacobs } 112355b4669Sjacobs 113355b4669Sjacobs if (id != -1) { /* it's a job */ 114355b4669Sjacobs if (move_job(svc, printer, id, destination) < 0) 115355b4669Sjacobs exit_code = 1; 116355b4669Sjacobs } else { /* it's a printer */ 117355b4669Sjacobs char message[128]; 118355b4669Sjacobs int count = 0; 119355b4669Sjacobs 120355b4669Sjacobs snprintf(message, sizeof (message), "moved jobs to %s", 121355b4669Sjacobs destination); 122027158a4SKeerthi Kondaka status = papiPrinterPause(svc, printer, message); 123355b4669Sjacobs if (status != PAPI_OK) { 1243ac6dcfaSsonam gupta - Sun Microsystems - Bangalore India /* 1253ac6dcfaSsonam gupta - Sun Microsystems - Bangalore India * If the user is denied the permission 1263ac6dcfaSsonam gupta - Sun Microsystems - Bangalore India * to disable then return appropriate msg 1273ac6dcfaSsonam gupta - Sun Microsystems - Bangalore India */ 1283ac6dcfaSsonam gupta - Sun Microsystems - Bangalore India char *result = NULL; 1293ac6dcfaSsonam gupta - Sun Microsystems - Bangalore India 1303ac6dcfaSsonam gupta - Sun Microsystems - Bangalore India result = papiServiceGetStatusMessage(svc); 1313ac6dcfaSsonam gupta - Sun Microsystems - Bangalore India 1323ac6dcfaSsonam gupta - Sun Microsystems - Bangalore India if (result != NULL) { 1333ac6dcfaSsonam gupta - Sun Microsystems - Bangalore India /* 1343ac6dcfaSsonam gupta - Sun Microsystems - Bangalore India * Check if user is denied 1353ac6dcfaSsonam gupta - Sun Microsystems - Bangalore India * the permission 1363ac6dcfaSsonam gupta - Sun Microsystems - Bangalore India */ 1373ac6dcfaSsonam gupta - Sun Microsystems - Bangalore India if (strstr(result, "permission denied") 1383ac6dcfaSsonam gupta - Sun Microsystems - Bangalore India != NULL) { 1393ac6dcfaSsonam gupta - Sun Microsystems - Bangalore India /* 1403ac6dcfaSsonam gupta - Sun Microsystems - Bangalore India * user is denied 1413ac6dcfaSsonam gupta - Sun Microsystems - Bangalore India * permission 1423ac6dcfaSsonam gupta - Sun Microsystems - Bangalore India */ 143*47129df6SKeerthi Kondaka fprintf(stderr, "UX:lpmove: "); 144*47129df6SKeerthi Kondaka fprintf(stderr, 145*47129df6SKeerthi Kondaka gettext("ERROR: ")); 146*47129df6SKeerthi Kondaka fprintf(stderr, gettext("You " 147*47129df6SKeerthi Kondaka "aren't allowed to do" 148*47129df6SKeerthi Kondaka " that.")); 149*47129df6SKeerthi Kondaka fprintf(stderr, "\n\t"); 150*47129df6SKeerthi Kondaka fprintf(stderr, 151*47129df6SKeerthi Kondaka gettext("TO FIX")); 152*47129df6SKeerthi Kondaka fprintf(stderr, ": "); 153*47129df6SKeerthi Kondaka fprintf(stderr, gettext("You " 154*47129df6SKeerthi Kondaka "must be logged in as " 155*47129df6SKeerthi Kondaka "\"lp\" or \"root\".")); 156*47129df6SKeerthi Kondaka fprintf(stderr, "\n"); 157355b4669Sjacobs exit_code = 1; 1583ac6dcfaSsonam gupta - Sun Microsystems - Bangalore India } else { 1593ac6dcfaSsonam gupta - Sun Microsystems - Bangalore India fprintf(stderr, gettext( 160027158a4SKeerthi Kondaka "Reject %s: %s\n"), 1613ac6dcfaSsonam gupta - Sun Microsystems - Bangalore India printer, 1623ac6dcfaSsonam gupta - Sun Microsystems - Bangalore India verbose_papi_message( 1633ac6dcfaSsonam gupta - Sun Microsystems - Bangalore India svc, status)); 1643ac6dcfaSsonam gupta - Sun Microsystems - Bangalore India exit_code = 1; 1653ac6dcfaSsonam gupta - Sun Microsystems - Bangalore India } 1663ac6dcfaSsonam gupta - Sun Microsystems - Bangalore India } else { 1673ac6dcfaSsonam gupta - Sun Microsystems - Bangalore India fprintf(stderr, gettext( 168027158a4SKeerthi Kondaka "Reject %s: %s\n"), 1693ac6dcfaSsonam gupta - Sun Microsystems - Bangalore India printer, 1703ac6dcfaSsonam gupta - Sun Microsystems - Bangalore India verbose_papi_message(svc, status)); 1713ac6dcfaSsonam gupta - Sun Microsystems - Bangalore India exit_code = 1; 1723ac6dcfaSsonam gupta - Sun Microsystems - Bangalore India } 1733ac6dcfaSsonam gupta - Sun Microsystems - Bangalore India } else { 174355b4669Sjacobs printf(gettext( 1753ac6dcfaSsonam gupta - Sun Microsystems - Bangalore India "destination %s is not accepting"\ 17637acf26aS"Nagaraj Yedathore - Sun Microsystems - Bangalore India" " requests\n"), printer); 177355b4669Sjacobs 178355b4669Sjacobs status = papiPrinterListJobs(svc, printer, NULL, 179355b4669Sjacobs 0, 0, &jobs); 180355b4669Sjacobs if (status != PAPI_OK) { 1813ac6dcfaSsonam gupta - Sun Microsystems - Bangalore India fprintf(stderr, gettext("Jobs %s:"\ 1823ac6dcfaSsonam gupta - Sun Microsystems - Bangalore India " %s\n"), 1833ac6dcfaSsonam gupta - Sun Microsystems - Bangalore India printer, 1843ac6dcfaSsonam gupta - Sun Microsystems - Bangalore India verbose_papi_message(svc, status)); 185355b4669Sjacobs exit_code = 1; 186355b4669Sjacobs } 187355b4669Sjacobs 188355b4669Sjacobs printf(gettext("move in progress ...\n")); 189355b4669Sjacobs while ((jobs != NULL) && (*jobs != NULL)) { 190355b4669Sjacobs id = papiJobGetId(*jobs++); 1913ac6dcfaSsonam gupta - Sun Microsystems - Bangalore India if (move_job(svc, printer, 1923ac6dcfaSsonam gupta - Sun Microsystems - Bangalore India id, destination) < 0) 193355b4669Sjacobs exit_code = 1; 194355b4669Sjacobs else 195355b4669Sjacobs count++; 196355b4669Sjacobs } 197355b4669Sjacobs printf(gettext( 1983ac6dcfaSsonam gupta - Sun Microsystems - Bangalore India "total of %d requests moved"\ 1993ac6dcfaSsonam gupta - Sun Microsystems - Bangalore India " from %s to %s\n"), 200355b4669Sjacobs count, printer, destination); 201355b4669Sjacobs 202355b4669Sjacobs papiJobListFree(jobs); 203355b4669Sjacobs } 2043ac6dcfaSsonam gupta - Sun Microsystems - Bangalore India } 205355b4669Sjacobs 206355b4669Sjacobs papiServiceDestroy(svc); 207355b4669Sjacobs } 208355b4669Sjacobs 209355b4669Sjacobs return (exit_code); 210355b4669Sjacobs } 211