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