xref: /titanic_41/usr/src/cmd/lp/lib/papi/service.c (revision 70025d765b044c6d8594bb965a2247a61e991a99)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*LINTLIBRARY*/
30 
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <stdarg.h>
34 #include <string.h>
35 #include <alloca.h>
36 #include <libintl.h>
37 #include <papi_impl.h>
38 
39 papi_status_t
40 papiServiceCreate(papi_service_t *handle, const char *service_name,
41 		const char *user_name, const char *password,
42 		const int (*authCB)(papi_service_t svc),
43 		const papi_encryption_t encryption, void *app_data)
44 {
45 	service_t *svc = NULL;
46 	char *path = Lp_FIFO;
47 
48 	if (handle == NULL)
49 		return (PAPI_BAD_ARGUMENT);
50 
51 	if ((*handle = svc = calloc(1, sizeof (*svc))) == NULL)
52 		return (PAPI_TEMPORARY_ERROR);
53 
54 	svc->md = mconnect(path, 0, 0);
55 	if (svc->md == NULL) {
56 		detailed_error(svc,
57 			gettext("can't connect to spooler for %s: %s"),
58 			(service_name ? service_name : ""), strerror(errno));
59 		return (PAPI_SERVICE_UNAVAILABLE);
60 	}
61 
62 	svc->msgbuf_size = MSGMAX;
63 	if ((svc->msgbuf = calloc(1, svc->msgbuf_size)) == NULL)
64 		return (PAPI_TEMPORARY_ERROR);
65 
66 	if (service_name != NULL)
67 		papiAttributeListAddString(&svc->attributes, PAPI_ATTR_EXCL,
68 				"service-name", service_name);
69 
70 	(void) papiServiceSetUserName(svc, user_name);
71 	(void) papiServiceSetPassword(svc, password);
72 	(void) papiServiceSetAuthCB(svc, authCB);
73 	(void) papiServiceSetAppData(svc, app_data);
74 	(void) papiServiceSetEncryption(svc, encryption);
75 
76 	return (PAPI_OK);
77 }
78 
79 void
80 papiServiceDestroy(papi_service_t handle)
81 {
82 	service_t *svc = handle;
83 
84 	if (svc != NULL) {
85 		if (svc->md != NULL)
86 			mdisconnect(svc->md);
87 		if (svc->msgbuf != NULL)
88 			free(svc->msgbuf);
89 		papiAttributeListFree(svc->attributes);
90 		free(svc);
91 	}
92 }
93 
94 papi_status_t
95 papiServiceSetUserName(papi_service_t handle, const char *user_name)
96 {
97 	service_t *svc = handle;
98 
99 	if (svc == NULL)
100 		return (PAPI_BAD_ARGUMENT);
101 
102 	return (papiAttributeListAddString(&svc->attributes, PAPI_ATTR_REPLACE,
103 				"user-name", user_name));
104 }
105 
106 papi_status_t
107 papiServiceSetPassword(papi_service_t handle, const char *password)
108 {
109 	service_t *svc = handle;
110 
111 	if (svc == NULL)
112 		return (PAPI_BAD_ARGUMENT);
113 
114 	return (papiAttributeListAddString(&svc->attributes, PAPI_ATTR_REPLACE,
115 				"password", password));
116 }
117 
118 papi_status_t
119 papiServiceSetEncryption(papi_service_t handle,
120 			const papi_encryption_t encryption)
121 {
122 	service_t *svc = handle;
123 
124 	if (svc == NULL)
125 		return (PAPI_BAD_ARGUMENT);
126 
127 	return (papiAttributeListAddInteger(&svc->attributes, PAPI_ATTR_REPLACE,
128 				"encryption", (int)encryption));
129 }
130 
131 papi_status_t
132 papiServiceSetAuthCB(papi_service_t handle,
133 			const int (*authCB)(papi_service_t svc))
134 {
135 	service_t *svc = handle;
136 
137 	if (svc == NULL)
138 		return (PAPI_BAD_ARGUMENT);
139 
140 	svc->authCB = (int (*)(papi_service_t svc))authCB;
141 
142 	return (PAPI_OK);
143 }
144 
145 papi_status_t
146 papiServiceSetAppData(papi_service_t handle, const void *app_data)
147 {
148 	service_t *svc = handle;
149 
150 	if (svc == NULL)
151 		return (PAPI_BAD_ARGUMENT);
152 
153 	svc->app_data = (void *)app_data;
154 
155 	return (PAPI_OK);
156 }
157 
158 char *
159 papiServiceGetServiceName(papi_service_t handle)
160 {
161 	service_t *svc = handle;
162 	char *result = NULL;
163 
164 	if (svc != NULL)
165 		papiAttributeListGetString(svc->attributes, NULL,
166 					"service-name", &result);
167 
168 	return (result);
169 }
170 
171 char *
172 papiServiceGetUserName(papi_service_t handle)
173 {
174 	service_t *svc = handle;
175 	char *result = NULL;
176 
177 	if (svc != NULL)
178 		papiAttributeListGetString(svc->attributes, NULL,
179 					"user-name", &result);
180 
181 	return (result);
182 }
183 
184 char *
185 papiServiceGetPassword(papi_service_t handle)
186 {
187 	service_t *svc = handle;
188 	char *result = NULL;
189 
190 	if (svc != NULL)
191 		papiAttributeListGetString(svc->attributes, NULL,
192 					"password", &result);
193 
194 	return (result);
195 }
196 
197 papi_encryption_t
198 papiServiceGetEncryption(papi_service_t handle)
199 {
200 	service_t *svc = handle;
201 	papi_encryption_t result = PAPI_ENCRYPT_NEVER;
202 
203 	if (svc != NULL)
204 		papiAttributeListGetInteger(svc->attributes, NULL,
205 					"encryption", (int *)&result);
206 
207 	return (result);
208 }
209 
210 void *
211 papiServiceGetAppData(papi_service_t handle)
212 {
213 	service_t *svc = handle;
214 	void *result = NULL;
215 
216 	if (svc != NULL)
217 		result = svc->app_data;
218 
219 	return (result);
220 }
221 
222 char *
223 papiServiceGetStatusMessage(papi_service_t handle)
224 {
225 	service_t *svc = handle;
226 	char *result = NULL;
227 
228 	if (svc != NULL)
229 		papiAttributeListGetString(svc->attributes, NULL,
230 					"detailed-status-message", &result);
231 
232 	return (result);
233 }
234 
235 void
236 detailed_error(service_t *svc, char *fmt, ...)
237 {
238 	if ((svc != NULL) && (fmt != NULL)) {
239 		va_list ap;
240 		size_t size;
241 		char *message = alloca(BUFSIZ);
242 
243 		va_start(ap, fmt);
244 		/*
245 		 * fill in the message.  If the buffer is too small, allocate
246 		 * one that is large enough and fill it in.
247 		 */
248 		if ((size = vsnprintf(message, BUFSIZ, fmt, ap)) >= BUFSIZ)
249 			if ((message = alloca(size)) != NULL)
250 				vsnprintf(message, size, fmt, ap);
251 		va_end(ap);
252 
253 		papiAttributeListAddString(&svc->attributes, PAPI_ATTR_APPEND,
254 					"detailed-status-message", message);
255 	}
256 }
257