]> git.ipfire.org Git - thirdparty/glibc.git/blob - nscd/nscd-client.h
Include sys/uio.h. (__readall, __readvall, writeall): New prototypes.
[thirdparty/glibc.git] / nscd / nscd-client.h
1 /* Copyright (c) 1998, 1999, 2000, 2003, 2004, 2005
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Thorsten Kukuk <kukuk@suse.de>, 1998.
5
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
10
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
20
21 /* This file defines everything that client code should need to
22 know to talk to the nscd daemon. */
23
24 #ifndef _NSCD_CLIENT_H
25 #define _NSCD_CLIENT_H 1
26
27 #include <stdbool.h>
28 #include <stdint.h>
29 #include <sys/types.h>
30 #include <atomic.h>
31 #include <nscd-types.h>
32 #include <sys/uio.h>
33
34
35 /* Version number of the daemon interface */
36 #define NSCD_VERSION 2
37
38 /* Path of the file where the PID of the running system is stored. */
39 #define _PATH_NSCDPID "/var/run/nscd/nscd.pid"
40
41 /* Path for the Unix domain socket. */
42 #define _PATH_NSCDSOCKET "/var/run/nscd/socket"
43
44 /* Path for the configuration file. */
45 #define _PATH_NSCDCONF "/etc/nscd.conf"
46
47
48 /* Available services. */
49 typedef enum
50 {
51 GETPWBYNAME,
52 GETPWBYUID,
53 GETGRBYNAME,
54 GETGRBYGID,
55 GETHOSTBYNAME,
56 GETHOSTBYNAMEv6,
57 GETHOSTBYADDR,
58 GETHOSTBYADDRv6,
59 LASTDBREQ = GETHOSTBYADDRv6,
60 SHUTDOWN, /* Shut the server down. */
61 GETSTAT, /* Get the server statistic. */
62 INVALIDATE, /* Invalidate one special cache. */
63 GETFDPW,
64 GETFDGR,
65 GETFDHST,
66 GETAI,
67 INITGROUPS,
68 LASTREQ
69 } request_type;
70
71
72 /* Header common to all requests */
73 typedef struct
74 {
75 int32_t version; /* Version number of the daemon interface. */
76 request_type type; /* Service requested. */
77 int32_t key_len; /* Key length. */
78 } request_header;
79
80
81 /* Structure sent in reply to password query. Note that this struct is
82 sent also if the service is disabled or there is no record found. */
83 typedef struct
84 {
85 int32_t version;
86 int32_t found;
87 nscd_ssize_t pw_name_len;
88 nscd_ssize_t pw_passwd_len;
89 uid_t pw_uid;
90 gid_t pw_gid;
91 nscd_ssize_t pw_gecos_len;
92 nscd_ssize_t pw_dir_len;
93 nscd_ssize_t pw_shell_len;
94 } pw_response_header;
95
96
97 /* Structure sent in reply to group query. Note that this struct is
98 sent also if the service is disabled or there is no record found. */
99 typedef struct
100 {
101 int32_t version;
102 int32_t found;
103 nscd_ssize_t gr_name_len;
104 nscd_ssize_t gr_passwd_len;
105 gid_t gr_gid;
106 nscd_ssize_t gr_mem_cnt;
107 } gr_response_header;
108
109
110 /* Structure sent in reply to host query. Note that this struct is
111 sent also if the service is disabled or there is no record found. */
112 typedef struct
113 {
114 int32_t version;
115 int32_t found;
116 nscd_ssize_t h_name_len;
117 nscd_ssize_t h_aliases_cnt;
118 int32_t h_addrtype;
119 int32_t h_length;
120 nscd_ssize_t h_addr_list_cnt;
121 int32_t error;
122 } hst_response_header;
123
124
125 /* Structure sent in reply to addrinfo query. Note that this struct is
126 sent also if the service is disabled or there is no record found. */
127 typedef struct
128 {
129 int32_t version;
130 int32_t found;
131 nscd_ssize_t naddrs;
132 nscd_ssize_t addrslen;
133 nscd_ssize_t canonlen;
134 int32_t error;
135 } ai_response_header;
136
137 /* Structure filled in by __nscd_getai. */
138 struct nscd_ai_result
139 {
140 int naddrs;
141 char *canon;
142 uint8_t *family;
143 char *addrs;
144 };
145
146 /* Structure sent in reply to initgroups query. Note that this struct is
147 sent also if the service is disabled or there is no record found. */
148 typedef struct
149 {
150 int32_t version;
151 int32_t found;
152 nscd_ssize_t ngrps;
153 } initgr_response_header;
154
155
156 /* Type for offsets in data part of database. */
157 typedef uint32_t ref_t;
158 /* Value for invalid/no reference. */
159 #define ENDREF UINT32_MAX
160
161 /* Timestamp type. */
162 typedef uint64_t nscd_time_t;
163
164 /* Alignment requirement of the beginning of the data region. */
165 #define ALIGN 16
166
167
168 /* Head of record in data part of database. */
169 struct datahead
170 {
171 nscd_ssize_t allocsize; /* Allocated Bytes. */
172 nscd_ssize_t recsize; /* Size of the record. */
173 nscd_time_t timeout; /* Time when this entry becomes invalid. */
174 uint8_t notfound; /* Nonzero if data has not been found. */
175 uint8_t nreloads; /* Reloads without use. */
176 uint8_t usable; /* False if the entry must be ignored. */
177 uint64_t :40; /* Alignment. */
178
179 /* We need to have the following element aligned for the response
180 header data types and their use in the 'struct dataset' types
181 defined in the XXXcache.c files. */
182 union
183 {
184 pw_response_header pwdata;
185 gr_response_header grdata;
186 hst_response_header hstdata;
187 ai_response_header aidata;
188 initgr_response_header initgrdata;
189 nscd_ssize_t align1;
190 nscd_time_t align2;
191 } data[0];
192 };
193
194
195 /* Structure for one hash table entry. */
196 struct hashentry
197 {
198 request_type type:8; /* Which type of dataset. */
199 bool first; /* True if this was the original key. */
200 nscd_ssize_t len; /* Length of key. */
201 ref_t key; /* Pointer to key. */
202 int32_t owner; /* If secure table, this is the owner. */
203 ref_t next; /* Next entry in this hash bucket list. */
204 ref_t packet; /* Records for the result. */
205 union
206 {
207 struct hashentry *dellist; /* Next record to be deleted. This can be a
208 pointer since only nscd uses this field. */
209 ref_t *prevp; /* Pointer to field containing forward
210 reference. */
211 };
212 };
213
214
215 /* Current persistent database version. */
216 #define DB_VERSION 1
217
218 /* Maximum time allowed between updates of the timestamp. */
219 #define MAPPING_TIMEOUT (5 * 60)
220
221
222 /* Header of persistent database file. */
223 struct database_pers_head
224 {
225 int32_t version;
226 int32_t header_size;
227 volatile int32_t gc_cycle;
228 volatile int32_t nscd_certainly_running;
229 volatile nscd_time_t timestamp;
230
231 nscd_ssize_t module;
232 nscd_ssize_t data_size;
233
234 nscd_ssize_t first_free; /* Offset of first free byte in data area. */
235
236 nscd_ssize_t nentries;
237 nscd_ssize_t maxnentries;
238 nscd_ssize_t maxnsearched;
239
240 uint64_t poshit;
241 uint64_t neghit;
242 uint64_t posmiss;
243 uint64_t negmiss;
244
245 uint64_t rdlockdelayed;
246 uint64_t wrlockdelayed;
247
248 uint64_t addfailed;
249
250 ref_t array[0];
251 };
252
253
254 /* Mapped database record. */
255 struct mapped_database
256 {
257 const struct database_pers_head *head;
258 const char *data;
259 size_t mapsize;
260 int counter; /* > 0 indicates it is usable. */
261 };
262 #define NO_MAPPING ((struct mapped_database *) -1l)
263
264 struct locked_map_ptr
265 {
266 int lock;
267 struct mapped_database *mapped;
268 };
269 #define libc_locked_map_ptr(class, name) class struct locked_map_ptr name
270
271
272 /* Open socket connection to nscd server. */
273 extern int __nscd_open_socket (const char *key, size_t keylen,
274 request_type type, void *response,
275 size_t responselen) attribute_hidden;
276
277 /* Get reference of mapping. */
278 extern struct mapped_database *__nscd_get_map_ref (request_type type,
279 const char *name,
280 struct locked_map_ptr *mapptr,
281 int *gc_cyclep);
282
283 /* Unmap database. */
284 extern void __nscd_unmap (struct mapped_database *mapped);
285
286 /* Drop reference of mapping. */
287 static inline int __nscd_drop_map_ref (struct mapped_database *map,
288 int *gc_cycle)
289 {
290 if (map != NO_MAPPING)
291 {
292 int now_cycle = map->head->gc_cycle;
293 if (__builtin_expect (now_cycle != *gc_cycle, 0))
294 {
295 /* We might have read inconsistent data. */
296 *gc_cycle = now_cycle;
297 return -1;
298 }
299
300 if (atomic_decrement_val (&map->counter) == 0)
301 __nscd_unmap (map);
302 }
303
304 return 0;
305 }
306
307
308 /* Search the mapped database. */
309 extern const struct datahead *__nscd_cache_search (request_type type,
310 const char *key,
311 size_t keylen,
312 const struct mapped_database *mapped);
313
314 /* Wrappers around read, readv and write that only read/write less than LEN
315 bytes on error or EOF. */
316 extern ssize_t __readall (int fd, void *buf, size_t len)
317 attribute_hidden;
318 extern ssize_t __readvall (int fd, const struct iovec *iov, int iovcnt)
319 attribute_hidden;
320 extern ssize_t writeall (int fd, const void *buf, size_t len)
321 attribute_hidden;
322
323 #endif /* nscd.h */