xref: /illumos-gate/usr/src/lib/print/libpapi-lpd/common/lpd-cancel.c (revision 6b5764c36d253d178caa447fa2a6d7e0c7dfd6e6)
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*6b5764c3Ssonam gupta - Sun Microsystems - Bangalore India  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24355b4669Sjacobs  * Use is subject to license terms.
25355b4669Sjacobs  *
26355b4669Sjacobs  */
27355b4669Sjacobs 
28355b4669Sjacobs /* $Id: lpd-cancel.c 155 2006-04-26 02:34:54Z ktou $ */
29355b4669Sjacobs 
30355b4669Sjacobs #define	__EXTENSIONS__	/* for strtok_r() */
31355b4669Sjacobs #include <stdio.h>
32355b4669Sjacobs #include <stdlib.h>
33355b4669Sjacobs #include <unistd.h>
34355b4669Sjacobs #include <string.h>
35355b4669Sjacobs #include <papi_impl.h>
36355b4669Sjacobs 
37355b4669Sjacobs papi_status_t
38355b4669Sjacobs lpd_cancel_job(service_t *svc, int id)
39355b4669Sjacobs {
40355b4669Sjacobs 	papi_status_t status = PAPI_INTERNAL_ERROR;
41355b4669Sjacobs 	int fd;
42752ec50eSjacobs 	char *list[2];
43355b4669Sjacobs 	char buf[128];	/* this should be overkill */
44355b4669Sjacobs 
45355b4669Sjacobs 	if (svc == NULL)
46355b4669Sjacobs 		return (PAPI_BAD_ARGUMENT);
47355b4669Sjacobs 
48355b4669Sjacobs 	snprintf(buf, sizeof (buf), "%d", id);
49355b4669Sjacobs 	list[0] = buf;
50355b4669Sjacobs 	list[1] = NULL;
51355b4669Sjacobs 
524bd2082fSceastha 	if ((fd = lpd_open(svc, 'c', list, 15)) < 0)
53355b4669Sjacobs 		return (PAPI_INTERNAL_ERROR);
54355b4669Sjacobs 
55355b4669Sjacobs 	memset(buf, 0, sizeof (buf));
56355b4669Sjacobs 	if (fdgets(buf, sizeof (buf), fd) != NULL) {
57355b4669Sjacobs 		if (buf[0] == '\0')
58355b4669Sjacobs 			status = PAPI_NOT_FOUND;
59*6b5764c3Ssonam gupta - Sun Microsystems - Bangalore India 		else if ((strstr(buf, "permission denied") != NULL) ||
60*6b5764c3Ssonam gupta - Sun Microsystems - Bangalore India 		    (strstr(buf, "not-authorized") != NULL))
61355b4669Sjacobs 			status = PAPI_NOT_AUTHORIZED;
62355b4669Sjacobs 		else if ((strstr(buf, "cancelled") != NULL) ||
63*6b5764c3Ssonam gupta - Sun Microsystems - Bangalore India 		    (strstr(buf, "removed") != NULL))
64355b4669Sjacobs 			status = PAPI_OK;
65355b4669Sjacobs 	} else
66355b4669Sjacobs 		status = PAPI_NOT_FOUND;
67355b4669Sjacobs 
68355b4669Sjacobs 	close(fd);
69355b4669Sjacobs 
70355b4669Sjacobs 	return (status);
71355b4669Sjacobs }
72355b4669Sjacobs 
73355b4669Sjacobs papi_status_t
74355b4669Sjacobs lpd_purge_jobs(service_t *svc, job_t ***jobs)
75355b4669Sjacobs {
76355b4669Sjacobs 	papi_status_t status = PAPI_INTERNAL_ERROR;
77355b4669Sjacobs 	int fd;
78355b4669Sjacobs 	char *queue;
79355b4669Sjacobs 	char buf[256];
80355b4669Sjacobs 
81355b4669Sjacobs 	if (svc == NULL)
82355b4669Sjacobs 		return (PAPI_BAD_ARGUMENT);
83355b4669Sjacobs 
844bd2082fSceastha 	if ((fd = lpd_open(svc, 'c', NULL, 15)) < 0)
85355b4669Sjacobs 		return (PAPI_INTERNAL_ERROR);
86355b4669Sjacobs 
87355b4669Sjacobs 	queue = queue_name_from_uri(svc->uri);
88355b4669Sjacobs 
89355b4669Sjacobs 	status = PAPI_OK;
90355b4669Sjacobs 	memset(buf, 0, sizeof (buf));
91355b4669Sjacobs 	while (fdgets(buf, sizeof (buf), fd) != NULL) {
92355b4669Sjacobs 		/* if we canceled it, add it to the list */
93355b4669Sjacobs 		if ((strstr(buf, "cancelled") != NULL) ||
94355b4669Sjacobs 		    (strstr(buf, "removed") != NULL)) {
95355b4669Sjacobs 			job_t *job;
96355b4669Sjacobs 			papi_attribute_t **attributes = NULL;
97355b4669Sjacobs 			char *ptr, *iter = NULL;
98355b4669Sjacobs 			int id;
99355b4669Sjacobs 
100355b4669Sjacobs 			ptr = strtok_r(buf, ":", &iter);
101355b4669Sjacobs 			papiAttributeListAddString(&attributes, PAPI_ATTR_EXCL,
102*6b5764c3Ssonam gupta - Sun Microsystems - Bangalore India 			    "job-name", ptr);
103355b4669Sjacobs 			id = atoi(ptr);
104355b4669Sjacobs 			papiAttributeListAddInteger(&attributes, PAPI_ATTR_EXCL,
105*6b5764c3Ssonam gupta - Sun Microsystems - Bangalore India 			    "job-id", id);
106355b4669Sjacobs 			papiAttributeListAddString(&attributes, PAPI_ATTR_EXCL,
107*6b5764c3Ssonam gupta - Sun Microsystems - Bangalore India 			    "job-printer", queue);
108355b4669Sjacobs 
109355b4669Sjacobs 			if ((job = (job_t *)calloc(1, (sizeof (*job))))
110*6b5764c3Ssonam gupta - Sun Microsystems - Bangalore India 			    != NULL) {
111355b4669Sjacobs 				job->attributes = attributes;
112355b4669Sjacobs 				list_append(jobs, job);
113355b4669Sjacobs 			} else
114355b4669Sjacobs 				papiAttributeListFree(attributes);
115355b4669Sjacobs 		} else if (strstr(buf, "permission denied") != NULL)
116355b4669Sjacobs 			status = PAPI_NOT_AUTHORIZED;
117355b4669Sjacobs 	}
118355b4669Sjacobs 	close(fd);
119355b4669Sjacobs 
120355b4669Sjacobs 	return (status);
121355b4669Sjacobs }
122