1 /* Copyright (c) 1998-2014 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, see
17 <http://www.gnu.org/licenses/>. */
19 /* This file defines everything that client code should need to
20 know to talk to the nscd daemon. */
22 #ifndef _NSCD_CLIENT_H
23 #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 datahead_init_common (struct datahead
*head
, nscd_ssize_t allocsize
,
242 nscd_ssize_t recsize
, uint32_t ttl
)
244 /* Initialize so that we don't write out junk in uninitialized data to the
246 memset (head
, 0, sizeof (*head
));
248 head
->allocsize
= allocsize
;
249 head
->recsize
= recsize
;
254 /* Compute and return the timeout time. */
255 return head
->timeout
= time (NULL
) + ttl
;
259 datahead_init_pos (struct datahead
*head
, nscd_ssize_t allocsize
,
260 nscd_ssize_t recsize
, uint8_t nreloads
, uint32_t ttl
)
262 time_t ret
= datahead_init_common (head
, allocsize
, recsize
, ttl
);
264 head
->notfound
= false;
265 head
->nreloads
= nreloads
;
271 datahead_init_neg (struct datahead
*head
, nscd_ssize_t allocsize
,
272 nscd_ssize_t recsize
, uint32_t ttl
)
274 time_t ret
= datahead_init_common (head
, allocsize
, recsize
, ttl
);
276 /* We don't need to touch nreloads here since it is set to our desired value
277 (0) when we clear the structure. */
278 head
->notfound
= true;
283 /* Structure for one hash table entry. */
286 request_type type
:8; /* Which type of dataset. */
287 bool first
; /* True if this was the original key. */
288 nscd_ssize_t len
; /* Length of key. */
289 ref_t key
; /* Pointer to key. */
290 int32_t owner
; /* If secure table, this is the owner. */
291 ref_t next
; /* Next entry in this hash bucket list. */
292 ref_t packet
; /* Records for the result. */
295 struct hashentry
*dellist
; /* Next record to be deleted. This can be a
296 pointer since only nscd uses this field. */
297 ref_t
*prevp
; /* Pointer to field containing forward
303 /* Current persistent database version. */
306 /* Maximum time allowed between updates of the timestamp. */
307 #define MAPPING_TIMEOUT (5 * 60)
310 /* Used indices for the EXTRA_DATA element of 'database_pers_head'.
311 Each database has its own indices. */
312 #define NSCD_HST_IDX_CONF_TIMESTAMP 0
315 /* Header of persistent database file. */
316 struct database_pers_head
320 volatile int32_t gc_cycle
;
321 volatile int32_t nscd_certainly_running
;
322 volatile nscd_time_t timestamp
;
323 /* Room for extensions. */
324 volatile uint32_t extra_data
[4];
327 nscd_ssize_t data_size
;
329 nscd_ssize_t first_free
; /* Offset of first free byte in data area. */
331 nscd_ssize_t nentries
;
332 nscd_ssize_t maxnentries
;
333 nscd_ssize_t maxnsearched
;
340 uint64_t rdlockdelayed
;
341 uint64_t wrlockdelayed
;
349 /* Mapped database record. */
350 struct mapped_database
352 const struct database_pers_head
*head
;
355 int counter
; /* > 0 indicates it is usable. */
358 #define NO_MAPPING ((struct mapped_database *) -1l)
360 struct locked_map_ptr
363 struct mapped_database
*mapped
;
365 #define libc_locked_map_ptr(class, name) class struct locked_map_ptr name
367 /* Try acquiring lock for mapptr, returns true if it succeeds, false
370 __nscd_acquire_maplock (volatile struct locked_map_ptr
*mapptr
)
373 while (__builtin_expect (atomic_compare_and_exchange_val_acq (&mapptr
->lock
,
376 // XXX Best number of rounds?
377 if (__glibc_unlikely (++cnt
> 5))
387 /* Open socket connection to nscd server. */
388 extern int __nscd_open_socket (const char *key
, size_t keylen
,
389 request_type type
, void *response
,
390 size_t responselen
) attribute_hidden
;
392 /* Try to get a file descriptor for the shared meory segment
393 containing the database. */
394 extern struct mapped_database
*__nscd_get_mapping (request_type type
,
396 struct mapped_database
**mappedp
) attribute_hidden
;
398 /* Get reference of mapping. */
399 extern struct mapped_database
*__nscd_get_map_ref (request_type type
,
401 volatile struct locked_map_ptr
*mapptr
,
404 /* Unmap database. */
405 extern void __nscd_unmap (struct mapped_database
*mapped
);
407 /* Drop reference of mapping. */
409 __attribute__ ((unused
))
410 __nscd_drop_map_ref (struct mapped_database
*map
, int *gc_cycle
)
412 if (map
!= NO_MAPPING
)
414 int now_cycle
= map
->head
->gc_cycle
;
415 if (__glibc_unlikely (now_cycle
!= *gc_cycle
))
417 /* We might have read inconsistent data. */
418 *gc_cycle
= now_cycle
;
422 if (atomic_decrement_val (&map
->counter
) == 0)
430 /* Search the mapped database. */
431 extern struct datahead
*__nscd_cache_search (request_type type
,
434 const struct mapped_database
*mapped
,
437 /* Wrappers around read, readv and write that only read/write less than LEN
438 bytes on error or EOF. */
439 extern ssize_t
__readall (int fd
, void *buf
, size_t len
)
441 extern ssize_t
__readvall (int fd
, const struct iovec
*iov
, int iovcnt
)
443 extern ssize_t
writeall (int fd
, const void *buf
, size_t len
)
445 extern ssize_t
sendfileall (int tofd
, int fromfd
, off_t off
, size_t len
)
448 /* Get netlink timestamp counter from mapped area or zero. */
449 extern uint32_t __nscd_get_nl_timestamp (void);