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
28 #include <sys/types.h>
30 #include <nscd-types.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. */
61 SHUTDOWN
, /* Shut the server down. */
62 GETSTAT
, /* Get the server statistic. */
63 INVALIDATE
, /* Invalidate one special cache. */
79 /* Header common to all requests */
82 int32_t version
; /* Version number of the daemon interface. */
83 request_type type
; /* Service requested. */
84 int32_t key_len
; /* Key length. */
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. */
94 nscd_ssize_t pw_name_len
;
95 nscd_ssize_t pw_passwd_len
;
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. */
110 nscd_ssize_t gr_name_len
;
111 nscd_ssize_t gr_passwd_len
;
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. */
123 nscd_ssize_t h_name_len
;
124 nscd_ssize_t h_aliases_cnt
;
127 nscd_ssize_t h_addr_list_cnt
;
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. */
139 nscd_ssize_t addrslen
;
140 nscd_ssize_t canonlen
;
142 } ai_response_header
;
144 /* Structure filled in by __nscd_getai. */
145 struct nscd_ai_result
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. */
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. */
169 nscd_ssize_t s_name_len
;
170 nscd_ssize_t s_proto_len
;
171 nscd_ssize_t s_aliases_cnt
;
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. */
182 nscd_ssize_t nresults
;
183 nscd_ssize_t result_len
;
184 } netgroup_response_header
;
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. */
210 /* Head of record in data part of database. */
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. */
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
;
241 /* Structure for one hash table entry. */
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. */
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
261 /* Current persistent database version. */
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
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];
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
;
298 uint64_t rdlockdelayed
;
299 uint64_t wrlockdelayed
;
307 /* Mapped database record. */
308 struct mapped_database
310 const struct database_pers_head
*head
;
313 int counter
; /* > 0 indicates it is usable. */
316 #define NO_MAPPING ((struct mapped_database *) -1l)
318 struct locked_map_ptr
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
,
335 struct mapped_database
**mappedp
) attribute_hidden
;
337 /* Get reference of mapping. */
338 extern struct mapped_database
*__nscd_get_map_ref (request_type type
,
340 volatile struct locked_map_ptr
*mapptr
,
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
,
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
;
360 if (atomic_decrement_val (&map
->counter
) == 0)
368 /* Search the mapped database. */
369 extern struct datahead
*__nscd_cache_search (request_type type
,
372 const struct mapped_database
*mapped
,
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
)
379 extern ssize_t
__readvall (int fd
, const struct iovec
*iov
, int iovcnt
)
381 extern ssize_t
writeall (int fd
, const void *buf
, size_t len
)
383 extern ssize_t
sendfileall (int tofd
, int fromfd
, off_t off
, size_t len
)
386 /* Get netlink timestamp counter from mapped area or zero. */
387 extern uint32_t __nscd_get_nl_timestamp (void);