1*999637b1S"Nagaraj Yedathore - Sun Microsystems - Bangalore India"
2355b4669Sjacobs /*
3355b4669Sjacobs * CDDL HEADER START
4355b4669Sjacobs *
5355b4669Sjacobs * The contents of this file are subject to the terms of the
6355b4669Sjacobs * Common Development and Distribution License (the "License").
7355b4669Sjacobs * You may not use this file except in compliance with the License.
8355b4669Sjacobs *
9355b4669Sjacobs * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10355b4669Sjacobs * or http://www.opensolaris.org/os/licensing.
11355b4669Sjacobs * See the License for the specific language governing permissions
12355b4669Sjacobs * and limitations under the License.
13355b4669Sjacobs *
14355b4669Sjacobs * When distributing Covered Code, include this CDDL HEADER in each
15355b4669Sjacobs * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16355b4669Sjacobs * If applicable, add the following below this CDDL HEADER, with the
17355b4669Sjacobs * fields enclosed by brackets "[]" replaced with your own identifying
18355b4669Sjacobs * information: Portions Copyright [yyyy] [name of copyright owner]
19355b4669Sjacobs *
20355b4669Sjacobs * CDDL HEADER END
21355b4669Sjacobs */
22355b4669Sjacobs
23355b4669Sjacobs /*
24c389c7f8SGowtham Thommandra * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
25355b4669Sjacobs * Use is subject to license terms.
26355b4669Sjacobs *
27355b4669Sjacobs */
28355b4669Sjacobs
29355b4669Sjacobs /* $Id: disable.c 146 2006-03-24 00:26:54Z njacobs $ */
30355b4669Sjacobs
31355b4669Sjacobs
32355b4669Sjacobs #include <stdio.h>
33355b4669Sjacobs #include <stdlib.h>
34355b4669Sjacobs #include <unistd.h>
35355b4669Sjacobs #include <string.h>
36355b4669Sjacobs #include <locale.h>
37355b4669Sjacobs #include <libintl.h>
38355b4669Sjacobs #include <papi.h>
39355b4669Sjacobs #include "common.h"
40355b4669Sjacobs
41355b4669Sjacobs static void
usage(char * program)42355b4669Sjacobs usage(char *program)
43355b4669Sjacobs {
44355b4669Sjacobs char *name;
45355b4669Sjacobs
46355b4669Sjacobs if ((name = strrchr(program, '/')) == NULL)
47355b4669Sjacobs name = program;
48355b4669Sjacobs else
49355b4669Sjacobs name++;
50355b4669Sjacobs
51355b4669Sjacobs fprintf(stdout,
52355b4669Sjacobs gettext("Usage: %s [-c] [-W] [-r reason] destination ...\n"),
53355b4669Sjacobs name);
54355b4669Sjacobs exit(1);
55355b4669Sjacobs }
56355b4669Sjacobs
57355b4669Sjacobs static void
cancel_active_job(papi_service_t svc,char * dest)58355b4669Sjacobs cancel_active_job(papi_service_t svc, char *dest)
59355b4669Sjacobs {
60355b4669Sjacobs papi_status_t status;
61355b4669Sjacobs papi_job_t *j = NULL;
62355b4669Sjacobs char *req_attrs[] = { "job-state", "job-id", NULL };
63355b4669Sjacobs
64355b4669Sjacobs status = papiPrinterListJobs(svc, dest, req_attrs, 0, 0, &j);
65355b4669Sjacobs if ((status == PAPI_OK) && (j != NULL)) {
66355b4669Sjacobs int i;
67355b4669Sjacobs
68355b4669Sjacobs for (i = 0; j[i] != NULL; j++) {
69355b4669Sjacobs papi_attribute_t **a = papiJobGetAttributeList(j[i]);
70355b4669Sjacobs int state = 0;
71355b4669Sjacobs
72355b4669Sjacobs if (a == NULL)
73355b4669Sjacobs continue;
74355b4669Sjacobs
75355b4669Sjacobs (void) papiAttributeListGetInteger(a, NULL,
76355b4669Sjacobs "job-state", &state);
77c389c7f8SGowtham Thommandra if (state & 0x082A) { /* If state is RS_ACTIVE */
78355b4669Sjacobs int32_t id = papiJobGetId(j[i]);
79355b4669Sjacobs
80355b4669Sjacobs (void) papiJobCancel(svc, dest, id);
81355b4669Sjacobs }
82355b4669Sjacobs }
83355b4669Sjacobs papiJobListFree(j);
84355b4669Sjacobs }
85355b4669Sjacobs }
86355b4669Sjacobs
87355b4669Sjacobs int
main(int ac,char * av[])88355b4669Sjacobs main(int ac, char *av[])
89355b4669Sjacobs {
90355b4669Sjacobs papi_status_t status;
91355b4669Sjacobs papi_service_t svc = NULL;
92355b4669Sjacobs papi_encryption_t encryption = PAPI_ENCRYPT_NEVER;
93355b4669Sjacobs int exit_status = 0;
94355b4669Sjacobs int cancel = 0;
95355b4669Sjacobs int pending = 0; /* not implemented */
96355b4669Sjacobs char *reason = NULL;
97355b4669Sjacobs int c;
98355b4669Sjacobs
99355b4669Sjacobs (void) setlocale(LC_ALL, "");
100355b4669Sjacobs (void) textdomain("SUNW_OST_OSCMD");
101355b4669Sjacobs
102355b4669Sjacobs while ((c = getopt(ac, av, "EcWr:")) != EOF)
103355b4669Sjacobs switch (c) {
104355b4669Sjacobs case 'c': /* cancel active job first */
105355b4669Sjacobs cancel = 1;
106355b4669Sjacobs break;
107355b4669Sjacobs case 'W': /* wait for active request, not implemented */
108355b4669Sjacobs pending = 1;
109355b4669Sjacobs break;
110355b4669Sjacobs case 'r': /* reason */
111355b4669Sjacobs reason = optarg;
112355b4669Sjacobs break;
113355b4669Sjacobs case 'E':
114355b4669Sjacobs encryption = PAPI_ENCRYPT_NEVER;
115355b4669Sjacobs break;
116355b4669Sjacobs default:
117355b4669Sjacobs usage(av[0]);
118355b4669Sjacobs }
119355b4669Sjacobs
120355b4669Sjacobs if (ac <= optind)
121355b4669Sjacobs usage(av[0]);
122355b4669Sjacobs
123355b4669Sjacobs while (optind < ac) {
124355b4669Sjacobs char *printer = av[optind++];
125355b4669Sjacobs
126355b4669Sjacobs status = papiServiceCreate(&svc, printer, NULL, NULL,
127355b4669Sjacobs cli_auth_callback, encryption, NULL);
128355b4669Sjacobs if (status != PAPI_OK) {
129355b4669Sjacobs fprintf(stderr, gettext(
130355b4669Sjacobs "Failed to contact service for %s: %s\n"),
131355b4669Sjacobs printer, verbose_papi_message(svc, status));
132355b4669Sjacobs exit_status = 1;
133355b4669Sjacobs }
134355b4669Sjacobs
135355b4669Sjacobs status = papiPrinterDisable(svc, printer, reason);
13698f04078SGowtham Thommandra if (status == PAPI_OK) {
13798f04078SGowtham Thommandra printf(gettext("printer \"%s\" now disabled\n"),
13898f04078SGowtham Thommandra printer);
13998f04078SGowtham Thommandra } else if (status == PAPI_NOT_ACCEPTING) {
14098f04078SGowtham Thommandra fprintf(stderr, gettext(
14198f04078SGowtham Thommandra "Destination \"%s\" was already disabled.\n"),
14298f04078SGowtham Thommandra printer);
14398f04078SGowtham Thommandra exit_status = 1;
14498f04078SGowtham Thommandra } else {
145*999637b1S"Nagaraj Yedathore - Sun Microsystems - Bangalore India" /* The operation is not supported in lpd protocol */
146*999637b1S"Nagaraj Yedathore - Sun Microsystems - Bangalore India" if (status == PAPI_OPERATION_NOT_SUPPORTED) {
147*999637b1S"Nagaraj Yedathore - Sun Microsystems - Bangalore India" fprintf(stderr,
148355b4669Sjacobs verbose_papi_message(svc, status));
149*999637b1S"Nagaraj Yedathore - Sun Microsystems - Bangalore India" } else {
150*999637b1S"Nagaraj Yedathore - Sun Microsystems - Bangalore India" fprintf(stderr, gettext("disable: %s: %s\n"),
151*999637b1S"Nagaraj Yedathore - Sun Microsystems - Bangalore India" printer, verbose_papi_message(svc, status));
152*999637b1S"Nagaraj Yedathore - Sun Microsystems - Bangalore India" }
153355b4669Sjacobs exit_status = 1;
154355b4669Sjacobs }
155355b4669Sjacobs
156355b4669Sjacobs if (cancel != 0)
157355b4669Sjacobs cancel_active_job(svc, printer);
158355b4669Sjacobs
159355b4669Sjacobs papiServiceDestroy(svc);
160355b4669Sjacobs }
161355b4669Sjacobs
162355b4669Sjacobs return (exit_status);
163355b4669Sjacobs }
164