1 /* Copyright (c) 1998-2019 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 <https://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
29 #include <sys/types.h>
31 #include <nscd-types.h>
35 /* Version number of the daemon interface */
36 #define NSCD_VERSION 2
38 /* Path of the file where the PID of the running system is stored. */
39 #define _PATH_NSCDPID "/var/run/nscd/nscd.pid"
41 /* Path for the Unix domain socket. */
42 #define _PATH_NSCDSOCKET "/var/run/nscd/socket"
44 /* Path for the configuration file. */
45 #define _PATH_NSCDCONF "/etc/nscd.conf"
47 /* Maximum allowed length for the key. */
48 #define MAXKEYLEN 1024
51 /* Available services. */
62 SHUTDOWN
, /* Shut the server down. */
63 GETSTAT
, /* Get the server statistic. */
64 INVALIDATE
, /* Invalidate one special cache. */
80 /* Header common to all requests */
83 int32_t version
; /* Version number of the daemon interface. */
84 request_type type
; /* Service requested. */
85 int32_t key_len
; /* Key length. */
89 /* Structure sent in reply to password query. Note that this struct is
90 sent also if the service is disabled or there is no record found. */
95 nscd_ssize_t pw_name_len
;
96 nscd_ssize_t pw_passwd_len
;
99 nscd_ssize_t pw_gecos_len
;
100 nscd_ssize_t pw_dir_len
;
101 nscd_ssize_t pw_shell_len
;
102 } pw_response_header
;
105 /* Structure sent in reply to group query. Note that this struct is
106 sent also if the service is disabled or there is no record found. */
111 nscd_ssize_t gr_name_len
;
112 nscd_ssize_t gr_passwd_len
;
114 nscd_ssize_t gr_mem_cnt
;
115 } gr_response_header
;
118 /* Structure sent in reply to host query. Note that this struct is
119 sent also if the service is disabled or there is no record found. */
124 nscd_ssize_t h_name_len
;
125 nscd_ssize_t h_aliases_cnt
;
128 nscd_ssize_t h_addr_list_cnt
;
130 } hst_response_header
;
133 /* Structure sent in reply to addrinfo query. Note that this struct is
134 sent also if the service is disabled or there is no record found. */
140 nscd_ssize_t addrslen
;
141 nscd_ssize_t canonlen
;
143 } ai_response_header
;
145 /* Structure filled in by __nscd_getai. */
146 struct nscd_ai_result
154 /* Structure sent in reply to initgroups query. Note that this struct is
155 sent also if the service is disabled or there is no record found. */
161 } initgr_response_header
;
164 /* Structure sent in reply to services query. Note that this struct is
165 sent also if the service is disabled or there is no record found. */
170 nscd_ssize_t s_name_len
;
171 nscd_ssize_t s_proto_len
;
172 nscd_ssize_t s_aliases_cnt
;
174 } serv_response_header
;
177 /* Structure send in reply to netgroup query. Note that this struct is
178 sent also if the service is disabled or there is no record found. */
183 nscd_ssize_t nresults
;
184 nscd_ssize_t result_len
;
185 } netgroup_response_header
;
192 } innetgroup_response_header
;
195 /* Type for offsets in data part of database. */
196 typedef uint32_t ref_t
;
197 /* Value for invalid/no reference. */
198 #define ENDREF UINT32_MAX
200 /* Timestamp type. */
201 typedef uint64_t nscd_time_t
;
203 /* Maximum timestamp. */
204 #define MAX_TIMEOUT_VALUE \
205 (sizeof (time_t) == sizeof (long int) ? LONG_MAX : INT_MAX)
207 /* Alignment requirement of the beginning of the data region. */
211 /* Head of record in data part of database. */
214 nscd_ssize_t allocsize
; /* Allocated Bytes. */
215 nscd_ssize_t recsize
; /* Size of the record. */
216 nscd_time_t timeout
; /* Time when this entry becomes invalid. */
217 uint8_t notfound
; /* Nonzero if data has not been found. */
218 uint8_t nreloads
; /* Reloads without use. */
219 uint8_t usable
; /* False if the entry must be ignored. */
220 uint8_t unused
; /* Unused. */
221 uint32_t ttl
; /* TTL value used. */
223 /* We need to have the following element aligned for the response
224 header data types and their use in the 'struct dataset' types
225 defined in the XXXcache.c files. */
228 pw_response_header pwdata
;
229 gr_response_header grdata
;
230 hst_response_header hstdata
;
231 ai_response_header aidata
;
232 initgr_response_header initgrdata
;
233 serv_response_header servdata
;
234 netgroup_response_header netgroupdata
;
235 innetgroup_response_header innetgroupdata
;
242 datahead_init_common (struct datahead
*head
, nscd_ssize_t allocsize
,
243 nscd_ssize_t recsize
, uint32_t ttl
)
245 /* Initialize so that we don't write out junk in uninitialized data to the
247 memset (head
, 0, sizeof (*head
));
249 head
->allocsize
= allocsize
;
250 head
->recsize
= recsize
;
255 /* Compute and return the timeout time. */
256 return head
->timeout
= time (NULL
) + ttl
;
260 datahead_init_pos (struct datahead
*head
, nscd_ssize_t allocsize
,
261 nscd_ssize_t recsize
, uint8_t nreloads
, uint32_t ttl
)
263 time_t ret
= datahead_init_common (head
, allocsize
, recsize
, ttl
);
265 head
->notfound
= false;
266 head
->nreloads
= nreloads
;
272 datahead_init_neg (struct datahead
*head
, nscd_ssize_t allocsize
,
273 nscd_ssize_t recsize
, uint32_t ttl
)
275 time_t ret
= datahead_init_common (head
, allocsize
, recsize
, ttl
);
277 /* We don't need to touch nreloads here since it is set to our desired value
278 (0) when we clear the structure. */
279 head
->notfound
= true;
284 /* Structure for one hash table entry. */
287 request_type type
:8; /* Which type of dataset. */
288 bool first
; /* True if this was the original key. */
289 nscd_ssize_t len
; /* Length of key. */
290 ref_t key
; /* Pointer to key. */
291 int32_t owner
; /* If secure table, this is the owner. */
292 ref_t next
; /* Next entry in this hash bucket list. */
293 ref_t packet
; /* Records for the result. */
296 struct hashentry
*dellist
; /* Next record to be deleted. This can be a
297 pointer since only nscd uses this field. */
298 ref_t
*prevp
; /* Pointer to field containing forward
304 /* Current persistent database version. */
307 /* Maximum time allowed between updates of the timestamp. */
308 #define MAPPING_TIMEOUT (5 * 60)
311 /* Used indices for the EXTRA_DATA element of 'database_pers_head'.
312 Each database has its own indices. */
313 #define NSCD_HST_IDX_CONF_TIMESTAMP 0
316 /* Header of persistent database file. */
317 struct database_pers_head
321 volatile int32_t gc_cycle
;
322 volatile int32_t nscd_certainly_running
;
323 volatile nscd_time_t timestamp
;
324 /* Room for extensions. */
325 volatile uint32_t extra_data
[4];
328 nscd_ssize_t data_size
;
330 nscd_ssize_t first_free
; /* Offset of first free byte in data area. */
332 nscd_ssize_t nentries
;
333 nscd_ssize_t maxnentries
;
334 nscd_ssize_t maxnsearched
;
341 uint64_t rdlockdelayed
;
342 uint64_t wrlockdelayed
;
350 /* Mapped database record. */
351 struct mapped_database
353 const struct database_pers_head
*head
;
356 int counter
; /* > 0 indicates it is usable. */
359 #define NO_MAPPING ((struct mapped_database *) -1l)
361 struct locked_map_ptr
364 struct mapped_database
*mapped
;
366 #define libc_locked_map_ptr(class, name) class struct locked_map_ptr name
368 /* Try acquiring lock for mapptr, returns true if it succeeds, false
371 __nscd_acquire_maplock (volatile struct locked_map_ptr
*mapptr
)
374 while (__builtin_expect (atomic_compare_and_exchange_val_acq (&mapptr
->lock
,
377 // XXX Best number of rounds?
378 if (__glibc_unlikely (++cnt
> 5))
388 /* Open socket connection to nscd server. */
389 extern int __nscd_open_socket (const char *key
, size_t keylen
,
390 request_type type
, void *response
,
391 size_t responselen
) attribute_hidden
;
393 /* Try to get a file descriptor for the shared meory segment
394 containing the database. */
395 extern struct mapped_database
*__nscd_get_mapping (request_type type
,
397 struct mapped_database
**mappedp
) attribute_hidden
;
399 /* Get reference of mapping. */
400 extern struct mapped_database
*__nscd_get_map_ref (request_type type
,
402 volatile struct locked_map_ptr
*mapptr
,
406 /* Unmap database. */
407 extern void __nscd_unmap (struct mapped_database
*mapped
)
410 /* Drop reference of mapping. */
412 __attribute__ ((unused
))
413 __nscd_drop_map_ref (struct mapped_database
*map
, int *gc_cycle
)
415 if (map
!= NO_MAPPING
)
417 int now_cycle
= map
->head
->gc_cycle
;
418 if (__glibc_unlikely (now_cycle
!= *gc_cycle
))
420 /* We might have read inconsistent data. */
421 *gc_cycle
= now_cycle
;
425 if (atomic_decrement_val (&map
->counter
) == 0)
433 /* Search the mapped database. */
434 extern struct datahead
*__nscd_cache_search (request_type type
,
437 const struct mapped_database
*mapped
,
441 /* Wrappers around read, readv and write that only read/write less than LEN
442 bytes on error or EOF. */
443 extern ssize_t
__readall (int fd
, void *buf
, size_t len
)
445 extern ssize_t
__readvall (int fd
, const struct iovec
*iov
, int iovcnt
)
447 extern ssize_t
writeall (int fd
, const void *buf
, size_t len
)
450 /* Get netlink timestamp counter from mapped area or zero. */
451 extern uint32_t __nscd_get_nl_timestamp (void)