For PR 14055, add bounds check to _dl_show_auxv().
[glibc.git] / nscd / nscd-client.h
blobe57a23c057f07bf46868a57478f03b33ced45dab
1 /* Copyright (c) 1998, 1999, 2000, 2003, 2004, 2005, 2006, 2007, 2009, 2011
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Thorsten Kukuk <kukuk@suse.de>, 1998.
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.
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.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
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"
46 /* Maximum allowed length for the key. */
47 #define MAXKEYLEN 1024
50 /* Available services. */
51 typedef enum
53 GETPWBYNAME,
54 GETPWBYUID,
55 GETGRBYNAME,
56 GETGRBYGID,
57 GETHOSTBYNAME,
58 GETHOSTBYNAMEv6,
59 GETHOSTBYADDR,
60 GETHOSTBYADDRv6,
61 SHUTDOWN, /* Shut the server down. */
62 GETSTAT, /* Get the server statistic. */
63 INVALIDATE, /* Invalidate one special cache. */
64 GETFDPW,
65 GETFDGR,
66 GETFDHST,
67 GETAI,
68 INITGROUPS,
69 GETSERVBYNAME,
70 GETSERVBYPORT,
71 GETFDSERV,
72 GETNETGRENT,
73 INNETGR,
74 GETFDNETGR,
75 LASTREQ
76 } request_type;
79 /* Header common to all requests */
80 typedef struct
82 int32_t version; /* Version number of the daemon interface. */
83 request_type type; /* Service requested. */
84 int32_t key_len; /* Key length. */
85 } request_header;
88 /* Structure sent in reply to password query. Note that this struct is
89 sent also if the service is disabled or there is no record found. */
90 typedef struct
92 int32_t version;
93 int32_t found;
94 nscd_ssize_t pw_name_len;
95 nscd_ssize_t pw_passwd_len;
96 uid_t pw_uid;
97 gid_t pw_gid;
98 nscd_ssize_t pw_gecos_len;
99 nscd_ssize_t pw_dir_len;
100 nscd_ssize_t pw_shell_len;
101 } pw_response_header;
104 /* Structure sent in reply to group query. Note that this struct is
105 sent also if the service is disabled or there is no record found. */
106 typedef struct
108 int32_t version;
109 int32_t found;
110 nscd_ssize_t gr_name_len;
111 nscd_ssize_t gr_passwd_len;
112 gid_t gr_gid;
113 nscd_ssize_t gr_mem_cnt;
114 } gr_response_header;
117 /* Structure sent in reply to host query. Note that this struct is
118 sent also if the service is disabled or there is no record found. */
119 typedef struct
121 int32_t version;
122 int32_t found;
123 nscd_ssize_t h_name_len;
124 nscd_ssize_t h_aliases_cnt;
125 int32_t h_addrtype;
126 int32_t h_length;
127 nscd_ssize_t h_addr_list_cnt;
128 int32_t error;
129 } hst_response_header;
132 /* Structure sent in reply to addrinfo query. Note that this struct is
133 sent also if the service is disabled or there is no record found. */
134 typedef struct
136 int32_t version;
137 int32_t found;
138 nscd_ssize_t naddrs;
139 nscd_ssize_t addrslen;
140 nscd_ssize_t canonlen;
141 int32_t error;
142 } ai_response_header;
144 /* Structure filled in by __nscd_getai. */
145 struct nscd_ai_result
147 int naddrs;
148 char *canon;
149 uint8_t *family;
150 char *addrs;
153 /* Structure sent in reply to initgroups query. Note that this struct is
154 sent also if the service is disabled or there is no record found. */
155 typedef struct
157 int32_t version;
158 int32_t found;
159 nscd_ssize_t ngrps;
160 } initgr_response_header;
163 /* Structure sent in reply to services query. Note that this struct is
164 sent also if the service is disabled or there is no record found. */
165 typedef struct
167 int32_t version;
168 int32_t found;
169 nscd_ssize_t s_name_len;
170 nscd_ssize_t s_proto_len;
171 nscd_ssize_t s_aliases_cnt;
172 int32_t s_port;
173 } serv_response_header;
176 /* Structure send in reply to netgroup query. Note that this struct is
177 sent also if the service is disabled or there is no record found. */
178 typedef struct
180 int32_t version;
181 int32_t found;
182 nscd_ssize_t nresults;
183 nscd_ssize_t result_len;
184 } netgroup_response_header;
186 typedef struct
188 int32_t version;
189 int32_t found;
190 int32_t result;
191 } innetgroup_response_header;
194 /* Type for offsets in data part of database. */
195 typedef uint32_t ref_t;
196 /* Value for invalid/no reference. */
197 #define ENDREF UINT32_MAX
199 /* Timestamp type. */
200 typedef uint64_t nscd_time_t;
202 /* Maximum timestamp. */
203 #define MAX_TIMEOUT_VALUE \
204 (sizeof (time_t) == sizeof (long int) ? LONG_MAX : INT_MAX)
206 /* Alignment requirement of the beginning of the data region. */
207 #define ALIGN 16
210 /* Head of record in data part of database. */
211 struct datahead
213 nscd_ssize_t allocsize; /* Allocated Bytes. */
214 nscd_ssize_t recsize; /* Size of the record. */
215 nscd_time_t timeout; /* Time when this entry becomes invalid. */
216 uint8_t notfound; /* Nonzero if data has not been found. */
217 uint8_t nreloads; /* Reloads without use. */
218 uint8_t usable; /* False if the entry must be ignored. */
219 uint8_t unused; /* Unused. */
220 uint32_t ttl; /* TTL value used. */
222 /* We need to have the following element aligned for the response
223 header data types and their use in the 'struct dataset' types
224 defined in the XXXcache.c files. */
225 union
227 pw_response_header pwdata;
228 gr_response_header grdata;
229 hst_response_header hstdata;
230 ai_response_header aidata;
231 initgr_response_header initgrdata;
232 serv_response_header servdata;
233 netgroup_response_header netgroupdata;
234 innetgroup_response_header innetgroupdata;
235 nscd_ssize_t align1;
236 nscd_time_t align2;
237 } data[0];
241 /* Structure for one hash table entry. */
242 struct hashentry
244 request_type type:8; /* Which type of dataset. */
245 bool first; /* True if this was the original key. */
246 nscd_ssize_t len; /* Length of key. */
247 ref_t key; /* Pointer to key. */
248 int32_t owner; /* If secure table, this is the owner. */
249 ref_t next; /* Next entry in this hash bucket list. */
250 ref_t packet; /* Records for the result. */
251 union
253 struct hashentry *dellist; /* Next record to be deleted. This can be a
254 pointer since only nscd uses this field. */
255 ref_t *prevp; /* Pointer to field containing forward
256 reference. */
261 /* Current persistent database version. */
262 #define DB_VERSION 2
264 /* Maximum time allowed between updates of the timestamp. */
265 #define MAPPING_TIMEOUT (5 * 60)
268 /* Used indices for the EXTRA_DATA element of 'database_pers_head'.
269 Each database has its own indices. */
270 #define NSCD_HST_IDX_CONF_TIMESTAMP 0
273 /* Header of persistent database file. */
274 struct database_pers_head
276 int32_t version;
277 int32_t header_size;
278 volatile int32_t gc_cycle;
279 volatile int32_t nscd_certainly_running;
280 volatile nscd_time_t timestamp;
281 /* Room for extensions. */
282 volatile uint32_t extra_data[4];
284 nscd_ssize_t module;
285 nscd_ssize_t data_size;
287 nscd_ssize_t first_free; /* Offset of first free byte in data area. */
289 nscd_ssize_t nentries;
290 nscd_ssize_t maxnentries;
291 nscd_ssize_t maxnsearched;
293 uint64_t poshit;
294 uint64_t neghit;
295 uint64_t posmiss;
296 uint64_t negmiss;
298 uint64_t rdlockdelayed;
299 uint64_t wrlockdelayed;
301 uint64_t addfailed;
303 ref_t array[0];
307 /* Mapped database record. */
308 struct mapped_database
310 const struct database_pers_head *head;
311 const char *data;
312 size_t mapsize;
313 int counter; /* > 0 indicates it is usable. */
314 size_t datasize;
316 #define NO_MAPPING ((struct mapped_database *) -1l)
318 struct locked_map_ptr
320 int lock;
321 struct mapped_database *mapped;
323 #define libc_locked_map_ptr(class, name) class struct locked_map_ptr name
326 /* Open socket connection to nscd server. */
327 extern int __nscd_open_socket (const char *key, size_t keylen,
328 request_type type, void *response,
329 size_t responselen) attribute_hidden;
331 /* Try to get a file descriptor for the shared meory segment
332 containing the database. */
333 extern struct mapped_database *__nscd_get_mapping (request_type type,
334 const char *key,
335 struct mapped_database **mappedp) attribute_hidden;
337 /* Get reference of mapping. */
338 extern struct mapped_database *__nscd_get_map_ref (request_type type,
339 const char *name,
340 volatile struct locked_map_ptr *mapptr,
341 int *gc_cyclep);
343 /* Unmap database. */
344 extern void __nscd_unmap (struct mapped_database *mapped);
346 /* Drop reference of mapping. */
347 static inline int __nscd_drop_map_ref (struct mapped_database *map,
348 int *gc_cycle)
350 if (map != NO_MAPPING)
352 int now_cycle = map->head->gc_cycle;
353 if (__builtin_expect (now_cycle != *gc_cycle, 0))
355 /* We might have read inconsistent data. */
356 *gc_cycle = now_cycle;
357 return -1;
360 if (atomic_decrement_val (&map->counter) == 0)
361 __nscd_unmap (map);
364 return 0;
368 /* Search the mapped database. */
369 extern struct datahead *__nscd_cache_search (request_type type,
370 const char *key,
371 size_t keylen,
372 const struct mapped_database *mapped,
373 size_t datalen);
375 /* Wrappers around read, readv and write that only read/write less than LEN
376 bytes on error or EOF. */
377 extern ssize_t __readall (int fd, void *buf, size_t len)
378 attribute_hidden;
379 extern ssize_t __readvall (int fd, const struct iovec *iov, int iovcnt)
380 attribute_hidden;
381 extern ssize_t writeall (int fd, const void *buf, size_t len)
382 attribute_hidden;
383 extern ssize_t sendfileall (int tofd, int fromfd, off_t off, size_t len)
384 attribute_hidden;
386 /* Get netlink timestamp counter from mapped area or zero. */
387 extern uint32_t __nscd_get_nl_timestamp (void);
389 #endif /* nscd.h */