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