xref: /dflybsd-src/lib/libc/include/nscache.h (revision 86d7f5d305c6adaa56ff4582ece9859d73106103)
1*86d7f5d3SJohn Marino /*-
2*86d7f5d3SJohn Marino  * Copyright (c) 2005 Michael Bushkov <bushman@rsu.ru>
3*86d7f5d3SJohn Marino  * All rights reserved.
4*86d7f5d3SJohn Marino  *
5*86d7f5d3SJohn Marino  * Redistribution and use in source and binary forms, with or without
6*86d7f5d3SJohn Marino  * modification, are permitted provided that the following conditions
7*86d7f5d3SJohn Marino  * are met:
8*86d7f5d3SJohn Marino  * 1. Redistributions of source code must retain the above copyright
9*86d7f5d3SJohn Marino  *    notice, this list of conditions and the following disclaimer.
10*86d7f5d3SJohn Marino  * 2. Redistributions in binary form must reproduce the above copyright
11*86d7f5d3SJohn Marino  *    notice, this list of conditions and the following disclaimer in the
12*86d7f5d3SJohn Marino  *    documentation and/or other materials provided with the distribution.
13*86d7f5d3SJohn Marino  *
14*86d7f5d3SJohn Marino  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15*86d7f5d3SJohn Marino  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16*86d7f5d3SJohn Marino  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17*86d7f5d3SJohn Marino  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18*86d7f5d3SJohn Marino  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19*86d7f5d3SJohn Marino  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20*86d7f5d3SJohn Marino  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21*86d7f5d3SJohn Marino  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22*86d7f5d3SJohn Marino  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23*86d7f5d3SJohn Marino  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24*86d7f5d3SJohn Marino  * SUCH DAMAGE.
25*86d7f5d3SJohn Marino  *
26*86d7f5d3SJohn Marino  * $FreeBSD: src/lib/libc/include/nscache.h,v 1.1 2006/04/28 12:03:35 ume Exp $
27*86d7f5d3SJohn Marino  */
28*86d7f5d3SJohn Marino 
29*86d7f5d3SJohn Marino #ifndef __NS_CACHE_H__
30*86d7f5d3SJohn Marino #define __NS_CACHE_H__
31*86d7f5d3SJohn Marino 
32*86d7f5d3SJohn Marino #include "nscachedcli.h"
33*86d7f5d3SJohn Marino 
34*86d7f5d3SJohn Marino typedef int (*nss_cache_id_func_t)(char *, size_t *, va_list, void *);
35*86d7f5d3SJohn Marino typedef int (*nss_cache_marshal_func_t)(char *, size_t *, void *, va_list,
36*86d7f5d3SJohn Marino 	void *);
37*86d7f5d3SJohn Marino typedef int (*nss_cache_unmarshal_func_t)(char *, size_t, void *, va_list,
38*86d7f5d3SJohn Marino 	void *);
39*86d7f5d3SJohn Marino 
40*86d7f5d3SJohn Marino typedef	void (*nss_set_mp_ws_func_t)(cached_mp_write_session);
41*86d7f5d3SJohn Marino typedef	cached_mp_write_session	(*nss_get_mp_ws_func_t)(void);
42*86d7f5d3SJohn Marino 
43*86d7f5d3SJohn Marino typedef void (*nss_set_mp_rs_func_t)(cached_mp_read_session);
44*86d7f5d3SJohn Marino typedef cached_mp_read_session	(*nss_get_mp_rs_func_t)(void);
45*86d7f5d3SJohn Marino 
46*86d7f5d3SJohn Marino typedef struct _nss_cache_info {
47*86d7f5d3SJohn Marino 	char	*entry_name;
48*86d7f5d3SJohn Marino 	void	*mdata;
49*86d7f5d3SJohn Marino 
50*86d7f5d3SJohn Marino 	/*
51*86d7f5d3SJohn Marino 	 * These 3 functions should be implemented specifically for each
52*86d7f5d3SJohn Marino 	 * nsswitch database.
53*86d7f5d3SJohn Marino 	 */
54*86d7f5d3SJohn Marino 	nss_cache_id_func_t id_func;	/* marshals the request parameters */
55*86d7f5d3SJohn Marino 	nss_cache_marshal_func_t marshal_func;	   /* marshals response */
56*86d7f5d3SJohn Marino 	nss_cache_unmarshal_func_t unmarshal_func; /* unmarshals response */
57*86d7f5d3SJohn Marino 
58*86d7f5d3SJohn Marino 	/*
59*86d7f5d3SJohn Marino 	 * These 4 functions should be generated with NSS_MP_CACHE_HANDLING
60*86d7f5d3SJohn Marino 	 * macro.
61*86d7f5d3SJohn Marino 	 */
62*86d7f5d3SJohn Marino 	nss_set_mp_ws_func_t set_mp_ws_func; /* sets current write session */
63*86d7f5d3SJohn Marino 	nss_get_mp_ws_func_t get_mp_ws_func; /* gets current write session */
64*86d7f5d3SJohn Marino 
65*86d7f5d3SJohn Marino 	nss_set_mp_rs_func_t set_mp_rs_func; /* sets current read session */
66*86d7f5d3SJohn Marino 	nss_get_mp_rs_func_t get_mp_rs_func; /* gets current read session */
67*86d7f5d3SJohn Marino } nss_cache_info;
68*86d7f5d3SJohn Marino 
69*86d7f5d3SJohn Marino /*
70*86d7f5d3SJohn Marino  * NSS_MP_CACHE_HANDLING implements the set_mp_ws, get_mp_ws, set_mp_rs,
71*86d7f5d3SJohn Marino  * get_mp_rs functions, that are used in _nss_cache_info. It uses
72*86d7f5d3SJohn Marino  * NSS_TLS_HANDLING macro to organize thread local storage.
73*86d7f5d3SJohn Marino  */
74*86d7f5d3SJohn Marino #define NSS_MP_CACHE_HANDLING(name)					\
75*86d7f5d3SJohn Marino struct name##_mp_state {						\
76*86d7f5d3SJohn Marino 	cached_mp_write_session	mp_write_session;			\
77*86d7f5d3SJohn Marino 	cached_mp_read_session	mp_read_session;			\
78*86d7f5d3SJohn Marino };									\
79*86d7f5d3SJohn Marino 									\
80*86d7f5d3SJohn Marino static void								\
81*86d7f5d3SJohn Marino name##_mp_endstate(void *s) {						\
82*86d7f5d3SJohn Marino 	struct name##_mp_state	*mp_state;				\
83*86d7f5d3SJohn Marino 									\
84*86d7f5d3SJohn Marino 	mp_state = (struct name##_mp_state *)s;				\
85*86d7f5d3SJohn Marino 	if (mp_state->mp_write_session != INVALID_CACHED_MP_WRITE_SESSION)\
86*86d7f5d3SJohn Marino 		__abandon_cached_mp_write_session(mp_state->mp_write_session);\
87*86d7f5d3SJohn Marino 									\
88*86d7f5d3SJohn Marino 	if (mp_state->mp_read_session != INVALID_CACHED_MP_READ_SESSION)\
89*86d7f5d3SJohn Marino 		__close_cached_mp_read_session(mp_state->mp_read_session);\
90*86d7f5d3SJohn Marino }									\
91*86d7f5d3SJohn Marino NSS_TLS_HANDLING(name##_mp);						\
92*86d7f5d3SJohn Marino 									\
93*86d7f5d3SJohn Marino static void								\
94*86d7f5d3SJohn Marino name##_set_mp_ws(cached_mp_write_session ws)				\
95*86d7f5d3SJohn Marino {									\
96*86d7f5d3SJohn Marino 	struct name##_mp_state	*mp_state;				\
97*86d7f5d3SJohn Marino 	int	res;							\
98*86d7f5d3SJohn Marino 									\
99*86d7f5d3SJohn Marino 	res = name##_mp_getstate(&mp_state);				\
100*86d7f5d3SJohn Marino 	if (res != 0)							\
101*86d7f5d3SJohn Marino 		return;							\
102*86d7f5d3SJohn Marino 									\
103*86d7f5d3SJohn Marino 	mp_state->mp_write_session = ws;				\
104*86d7f5d3SJohn Marino }									\
105*86d7f5d3SJohn Marino 									\
106*86d7f5d3SJohn Marino static cached_mp_write_session						\
107*86d7f5d3SJohn Marino name##_get_mp_ws(void)							\
108*86d7f5d3SJohn Marino {									\
109*86d7f5d3SJohn Marino 	struct name##_mp_state	*mp_state;				\
110*86d7f5d3SJohn Marino 	int	res;							\
111*86d7f5d3SJohn Marino 									\
112*86d7f5d3SJohn Marino 	res = name##_mp_getstate(&mp_state);				\
113*86d7f5d3SJohn Marino 	if (res != 0)							\
114*86d7f5d3SJohn Marino 		return (INVALID_CACHED_MP_WRITE_SESSION);		\
115*86d7f5d3SJohn Marino 									\
116*86d7f5d3SJohn Marino 	return (mp_state->mp_write_session);				\
117*86d7f5d3SJohn Marino }									\
118*86d7f5d3SJohn Marino 									\
119*86d7f5d3SJohn Marino static void								\
120*86d7f5d3SJohn Marino name##_set_mp_rs(cached_mp_read_session rs)				\
121*86d7f5d3SJohn Marino {									\
122*86d7f5d3SJohn Marino 	struct name##_mp_state	*mp_state;				\
123*86d7f5d3SJohn Marino 	int	res;							\
124*86d7f5d3SJohn Marino 									\
125*86d7f5d3SJohn Marino 	res = name##_mp_getstate(&mp_state);				\
126*86d7f5d3SJohn Marino 	if (res != 0)							\
127*86d7f5d3SJohn Marino 		return;							\
128*86d7f5d3SJohn Marino 									\
129*86d7f5d3SJohn Marino 	mp_state->mp_read_session = rs;					\
130*86d7f5d3SJohn Marino }									\
131*86d7f5d3SJohn Marino 									\
132*86d7f5d3SJohn Marino static cached_mp_read_session						\
133*86d7f5d3SJohn Marino name##_get_mp_rs(void)							\
134*86d7f5d3SJohn Marino {									\
135*86d7f5d3SJohn Marino 	struct name##_mp_state	*mp_state;				\
136*86d7f5d3SJohn Marino 	int	res;							\
137*86d7f5d3SJohn Marino 									\
138*86d7f5d3SJohn Marino 	res = name##_mp_getstate(&mp_state);				\
139*86d7f5d3SJohn Marino 	if (res != 0)							\
140*86d7f5d3SJohn Marino 		return (INVALID_CACHED_MP_READ_SESSION);		\
141*86d7f5d3SJohn Marino 									\
142*86d7f5d3SJohn Marino 	return (mp_state->mp_read_session);				\
143*86d7f5d3SJohn Marino }
144*86d7f5d3SJohn Marino 
145*86d7f5d3SJohn Marino /*
146*86d7f5d3SJohn Marino  * These macros should be used to initialize _nss_cache_info structure. For
147*86d7f5d3SJohn Marino  * multipart queries in setXXXent and getXXXent functions mf and uf
148*86d7f5d3SJohn Marino  * (marshal function and unmarshal function) should be both NULL.
149*86d7f5d3SJohn Marino  */
150*86d7f5d3SJohn Marino #define NS_COMMON_CACHE_INFO_INITIALIZER(name, mdata, if, mf, uf)	\
151*86d7f5d3SJohn Marino 	{#name, mdata, if, mf, uf, NULL, NULL, NULL, NULL}
152*86d7f5d3SJohn Marino #define NS_MP_CACHE_INFO_INITIALIZER(name, mdata, mf, uf)		\
153*86d7f5d3SJohn Marino 	{#name, mdata, NULL, mf, uf, name##_set_mp_ws, name##_get_mp_ws,\
154*86d7f5d3SJohn Marino 		name##_set_mp_rs, name##_get_mp_rs }
155*86d7f5d3SJohn Marino 
156*86d7f5d3SJohn Marino /*
157*86d7f5d3SJohn Marino  * Analog of other XXX_CB macros. Has the pointer to _nss_cache_info
158*86d7f5d3SJohn Marino  * structure as the only argument.
159*86d7f5d3SJohn Marino  */
160*86d7f5d3SJohn Marino #define NS_CACHE_CB(cinfo) {NSSRC_CACHE, __nss_cache_handler, (void *)(cinfo) },
161*86d7f5d3SJohn Marino 
162*86d7f5d3SJohn Marino /* args are: current pointer, current buffer, initial buffer, pointer type */
163*86d7f5d3SJohn Marino #define NS_APPLY_OFFSET(cp, cb, ib, p_type)				\
164*86d7f5d3SJohn Marino 	if ((cp) != NULL)						\
165*86d7f5d3SJohn Marino 		(cp) = (p_type)((char *)(cb) + (size_t)(cp) - (size_t)(ib))
166*86d7f5d3SJohn Marino /*
167*86d7f5d3SJohn Marino  * Gets new pointer from the marshalled buffer by uisng initial address
168*86d7f5d3SJohn Marino  * and initial buffer address
169*86d7f5d3SJohn Marino  */
170*86d7f5d3SJohn Marino #define NS_GET_NEWP(cp, cb, ib)						\
171*86d7f5d3SJohn Marino 	((char *)(cb) + (size_t)(cp) - (size_t)(ib))
172*86d7f5d3SJohn Marino 
173*86d7f5d3SJohn Marino typedef struct _nss_cache_data {
174*86d7f5d3SJohn Marino 	char	*key;
175*86d7f5d3SJohn Marino 	size_t	key_size;
176*86d7f5d3SJohn Marino 
177*86d7f5d3SJohn Marino 	nss_cache_info const	*info;
178*86d7f5d3SJohn Marino } nss_cache_data;
179*86d7f5d3SJohn Marino 
180*86d7f5d3SJohn Marino __BEGIN_DECLS
181*86d7f5d3SJohn Marino /* dummy function, which is needed to make nss_method_lookup happy */
182*86d7f5d3SJohn Marino extern	int	__nss_cache_handler(void *, void *, va_list);
183*86d7f5d3SJohn Marino 
184*86d7f5d3SJohn Marino #ifdef _NS_PRIVATE
185*86d7f5d3SJohn Marino extern	int	__nss_common_cache_read(void *, void *, va_list);
186*86d7f5d3SJohn Marino extern	int	__nss_common_cache_write(void *, void *, va_list);
187*86d7f5d3SJohn Marino extern	int	__nss_common_cache_write_negative(void *);
188*86d7f5d3SJohn Marino 
189*86d7f5d3SJohn Marino extern	int	__nss_mp_cache_read(void *, void *, va_list);
190*86d7f5d3SJohn Marino extern	int	__nss_mp_cache_write(void *, void *, va_list);
191*86d7f5d3SJohn Marino extern	int	__nss_mp_cache_write_submit(void *, void *, va_list);
192*86d7f5d3SJohn Marino extern	int	__nss_mp_cache_end(void *, void *, va_list);
193*86d7f5d3SJohn Marino #endif /* _NS_PRIVATE */
194*86d7f5d3SJohn Marino 
195*86d7f5d3SJohn Marino __END_DECLS
196*86d7f5d3SJohn Marino 
197*86d7f5d3SJohn Marino #endif
198