1*9855SNagaraj.Yedathore@Sun.COM 
22264Sjacobs /*
32264Sjacobs  * CDDL HEADER START
42264Sjacobs  *
52264Sjacobs  * The contents of this file are subject to the terms of the
62264Sjacobs  * Common Development and Distribution License (the "License").
72264Sjacobs  * You may not use this file except in compliance with the License.
82264Sjacobs  *
92264Sjacobs  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
102264Sjacobs  * or http://www.opensolaris.org/os/licensing.
112264Sjacobs  * See the License for the specific language governing permissions
122264Sjacobs  * and limitations under the License.
132264Sjacobs  *
142264Sjacobs  * When distributing Covered Code, include this CDDL HEADER in each
152264Sjacobs  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
162264Sjacobs  * If applicable, add the following below this CDDL HEADER, with the
172264Sjacobs  * fields enclosed by brackets "[]" replaced with your own identifying
182264Sjacobs  * information: Portions Copyright [yyyy] [name of copyright owner]
192264Sjacobs  *
202264Sjacobs  * CDDL HEADER END
212264Sjacobs  */
222264Sjacobs 
232264Sjacobs /*
249257SGowtham.Thommandra@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
252264Sjacobs  * Use is subject to license terms.
262264Sjacobs  *
272264Sjacobs  */
282264Sjacobs 
292264Sjacobs /* $Id: disable.c 146 2006-03-24 00:26:54Z njacobs $ */
302264Sjacobs 
312264Sjacobs 
322264Sjacobs #include <stdio.h>
332264Sjacobs #include <stdlib.h>
342264Sjacobs #include <unistd.h>
352264Sjacobs #include <string.h>
362264Sjacobs #include <locale.h>
372264Sjacobs #include <libintl.h>
382264Sjacobs #include <papi.h>
392264Sjacobs #include "common.h"
402264Sjacobs 
412264Sjacobs static void
422264Sjacobs usage(char *program)
432264Sjacobs {
442264Sjacobs 	char *name;
452264Sjacobs 
462264Sjacobs 	if ((name = strrchr(program, '/')) == NULL)
472264Sjacobs 		name = program;
482264Sjacobs 	else
492264Sjacobs 		name++;
502264Sjacobs 
512264Sjacobs 	fprintf(stdout,
529257SGowtham.Thommandra@Sun.COM 	    gettext("Usage: %s [-c] [-W] [-r reason] destination ...\n"),
539257SGowtham.Thommandra@Sun.COM 	    name);
542264Sjacobs 	exit(1);
552264Sjacobs }
562264Sjacobs 
572264Sjacobs static void
582264Sjacobs cancel_active_job(papi_service_t svc, char *dest)
592264Sjacobs {
602264Sjacobs 	papi_status_t status;
612264Sjacobs 	papi_job_t *j = NULL;
622264Sjacobs 	char *req_attrs[] = { "job-state", "job-id", NULL };
632264Sjacobs 
642264Sjacobs 	status = papiPrinterListJobs(svc, dest, req_attrs, 0, 0, &j);
652264Sjacobs 	if ((status == PAPI_OK) && (j != NULL)) {
662264Sjacobs 		int i;
672264Sjacobs 
682264Sjacobs 		for (i = 0; j[i] != NULL; j++) {
692264Sjacobs 			papi_attribute_t **a = papiJobGetAttributeList(j[i]);
702264Sjacobs 			int state = 0;
712264Sjacobs 
722264Sjacobs 			if (a == NULL)
732264Sjacobs 				continue;
742264Sjacobs 
752264Sjacobs 			(void) papiAttributeListGetInteger(a, NULL,
769257SGowtham.Thommandra@Sun.COM 			    "job-state", &state);
779257SGowtham.Thommandra@Sun.COM 			if (state & 0x082A) { /* If state is RS_ACTIVE */
782264Sjacobs 				int32_t id = papiJobGetId(j[i]);
792264Sjacobs 
802264Sjacobs 				(void) papiJobCancel(svc, dest, id);
812264Sjacobs 			}
822264Sjacobs 		}
832264Sjacobs 		papiJobListFree(j);
842264Sjacobs 	}
852264Sjacobs }
862264Sjacobs 
872264Sjacobs int
882264Sjacobs main(int ac, char *av[])
892264Sjacobs {
902264Sjacobs 	papi_status_t status;
912264Sjacobs 	papi_service_t svc = NULL;
922264Sjacobs 	papi_encryption_t encryption = PAPI_ENCRYPT_NEVER;
932264Sjacobs 	int exit_status = 0;
942264Sjacobs 	int cancel = 0;
952264Sjacobs 	int pending = 0;	/* not implemented */
962264Sjacobs 	char *reason = NULL;
972264Sjacobs 	int c;
982264Sjacobs 
992264Sjacobs 	(void) setlocale(LC_ALL, "");
1002264Sjacobs 	(void) textdomain("SUNW_OST_OSCMD");
1012264Sjacobs 
1022264Sjacobs 	while ((c = getopt(ac, av, "EcWr:")) != EOF)
1032264Sjacobs 		switch (c) {
1042264Sjacobs 		case 'c':	/* cancel active job first */
1052264Sjacobs 			cancel = 1;
1062264Sjacobs 			break;
1072264Sjacobs 		case 'W':	/* wait for active request, not implemented */
1082264Sjacobs 			pending = 1;
1092264Sjacobs 			break;
1102264Sjacobs 		case 'r':	/* reason */
1112264Sjacobs 			reason = optarg;
1122264Sjacobs 			break;
1132264Sjacobs 		case 'E':
1142264Sjacobs 			encryption = PAPI_ENCRYPT_NEVER;
1152264Sjacobs 			break;
1162264Sjacobs 		default:
1172264Sjacobs 			usage(av[0]);
1182264Sjacobs 		}
1192264Sjacobs 
1202264Sjacobs 	if (ac <= optind)
1212264Sjacobs 		usage(av[0]);
1222264Sjacobs 
1232264Sjacobs 	while (optind < ac) {
1242264Sjacobs 		char *printer = av[optind++];
1252264Sjacobs 
1262264Sjacobs 		status = papiServiceCreate(&svc, printer, NULL, NULL,
1279257SGowtham.Thommandra@Sun.COM 		    cli_auth_callback, encryption, NULL);
1282264Sjacobs 		if (status != PAPI_OK) {
1292264Sjacobs 			fprintf(stderr, gettext(
1309257SGowtham.Thommandra@Sun.COM 			    "Failed to contact service for %s: %s\n"),
1319257SGowtham.Thommandra@Sun.COM 			    printer, verbose_papi_message(svc, status));
1322264Sjacobs 			exit_status = 1;
1332264Sjacobs 		}
1342264Sjacobs 
1352264Sjacobs 		status = papiPrinterDisable(svc, printer, reason);
1369376SGowtham.Thommandra@Sun.COM 		if (status == PAPI_OK) {
1379376SGowtham.Thommandra@Sun.COM 			printf(gettext("printer \"%s\" now disabled\n"),
1389376SGowtham.Thommandra@Sun.COM 			    printer);
1399376SGowtham.Thommandra@Sun.COM 		} else if (status == PAPI_NOT_ACCEPTING) {
1409376SGowtham.Thommandra@Sun.COM 			fprintf(stderr, gettext(
1419376SGowtham.Thommandra@Sun.COM 			    "Destination \"%s\" was already disabled.\n"),
1429376SGowtham.Thommandra@Sun.COM 			    printer);
1439376SGowtham.Thommandra@Sun.COM 			exit_status = 1;
1449376SGowtham.Thommandra@Sun.COM 		} else {
145*9855SNagaraj.Yedathore@Sun.COM 			/* The operation is not supported in lpd protocol */
146*9855SNagaraj.Yedathore@Sun.COM 			if (status == PAPI_OPERATION_NOT_SUPPORTED) {
147*9855SNagaraj.Yedathore@Sun.COM 				fprintf(stderr,
148*9855SNagaraj.Yedathore@Sun.COM 				    verbose_papi_message(svc, status));
149*9855SNagaraj.Yedathore@Sun.COM 			} else {
150*9855SNagaraj.Yedathore@Sun.COM 				fprintf(stderr, gettext("disable: %s: %s\n"),
151*9855SNagaraj.Yedathore@Sun.COM 				    printer, verbose_papi_message(svc, status));
152*9855SNagaraj.Yedathore@Sun.COM 			}
1532264Sjacobs 			exit_status = 1;
1542264Sjacobs 		}
1552264Sjacobs 
1562264Sjacobs 		if (cancel != 0)
1572264Sjacobs 			cancel_active_job(svc, printer);
1582264Sjacobs 
1592264Sjacobs 		papiServiceDestroy(svc);
1602264Sjacobs 	}
1612264Sjacobs 
1622264Sjacobs 	return (exit_status);
1632264Sjacobs }
164