Update.
[glibc.git] / nscd / nscd-client.h
blob953610adc1cd7e51f3b9bf9b9189362d78adfbd5
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>
33 /* Version number of the daemon interface */
34 #define NSCD_VERSION 2
36 /* Path of the file where the PID of the running system is stored. */
37 #define _PATH_NSCDPID "/var/run/nscd/nscd.pid"
39 /* Path for the Unix domain socket. */
40 #define _PATH_NSCDSOCKET "/var/run/nscd/socket"
42 /* Path for the configuration file. */
43 #define _PATH_NSCDCONF "/etc/nscd.conf"
46 /* Available services. */
47 typedef enum
49 GETPWBYNAME,
50 GETPWBYUID,
51 GETGRBYNAME,
52 GETGRBYGID,
53 GETHOSTBYNAME,
54 GETHOSTBYNAMEv6,
55 GETHOSTBYADDR,
56 GETHOSTBYADDRv6,
57 LASTDBREQ = GETHOSTBYADDRv6,
58 SHUTDOWN, /* Shut the server down. */
59 GETSTAT, /* Get the server statistic. */
60 INVALIDATE, /* Invalidate one special cache. */
61 GETFDPW,
62 GETFDGR,
63 GETFDHST,
64 LASTREQ
65 } request_type;
68 /* Header common to all requests */
69 typedef struct
71 int32_t version; /* Version number of the daemon interface. */
72 request_type type; /* Service requested. */
73 int32_t key_len; /* Key length. */
74 } request_header;
77 /* Structure sent in reply to password query. Note that this struct is
78 sent also if the service is disabled or there is no record found. */
79 typedef struct
81 int32_t version;
82 int32_t found;
83 nscd_ssize_t pw_name_len;
84 nscd_ssize_t pw_passwd_len;
85 uid_t pw_uid;
86 gid_t pw_gid;
87 nscd_ssize_t pw_gecos_len;
88 nscd_ssize_t pw_dir_len;
89 nscd_ssize_t pw_shell_len;
90 } pw_response_header;
93 /* Structure sent in reply to group query. Note that this struct is
94 sent also if the service is disabled or there is no record found. */
95 typedef struct
97 int32_t version;
98 int32_t found;
99 nscd_ssize_t gr_name_len;
100 nscd_ssize_t gr_passwd_len;
101 gid_t gr_gid;
102 nscd_ssize_t gr_mem_cnt;
103 } gr_response_header;
106 /* Structure sent in reply to host query. Note that this struct is
107 sent also if the service is disabled or there is no record found. */
108 typedef struct
110 int32_t version;
111 int32_t found;
112 nscd_ssize_t h_name_len;
113 nscd_ssize_t h_aliases_cnt;
114 int32_t h_addrtype;
115 int32_t h_length;
116 nscd_ssize_t h_addr_list_cnt;
117 int32_t error;
118 } hst_response_header;
121 /* Type for offsets in data part of database. */
122 typedef uint32_t ref_t;
123 /* Value for invalid/no reference. */
124 #define ENDREF UINT32_MAX
126 /* Alignment requirement of the beginning of the data region. */
127 #define ALIGN 16
130 /* Head of record in data part of database. */
131 struct datahead
133 size_t allocsize; /* Allocated Bytes. */
134 size_t recsize; /* Size of the record. */
135 time_t timeout; /* Time when this entry becomes invalid. */
136 bool notfound; /* Nonzero if data for key has not been found. */
137 uint8_t nreloads; /* Reloads without use. */
138 bool usable; /* False if the entry must be ignored. */
140 /* We need to have the following element aligned for the response
141 header data types and their use in the 'struct dataset' types
142 defined in the XXXcache.c files. */
143 union
145 pw_response_header pwdata;
146 gr_response_header grdata;
147 hst_response_header hstdata;
148 ssize_t align1;
149 time_t align2;
150 } data[0];
154 /* Structure for one hash table entry. */
155 struct hashentry
157 request_type type:8; /* Which type of dataset. */
158 bool first; /* True if this was the original key. */
159 size_t len; /* Length of key. */
160 ref_t key; /* Pointer to key. */
161 uid_t owner; /* If secure table, this is the owner. */
162 ref_t next; /* Next entry in this hash bucket list. */
163 ref_t packet; /* Records for the result. */
164 union
166 struct hashentry *dellist; /* Next record to be deleted. This can be a
167 pointer since only nscd uses this field. */
168 ref_t *prevp; /* Pointer to field containing forward
169 reference. */
174 /* Current persistent database version. */
175 #define DB_VERSION 1
177 /* Maximum time allowed between updates of the timestamp. */
178 #define MAPPING_TIMEOUT (5 * 60)
181 /* Header of persistent database file. */
182 struct database_pers_head
184 int version;
185 int header_size;
186 volatile int gc_cycle;
187 volatile int nscd_certainly_running;
188 volatile time_t timestamp;
190 size_t module;
191 size_t data_size;
193 size_t first_free; /* Offset of first free byte in data area. */
195 size_t nentries;
196 size_t maxnentries;
197 size_t maxnsearched;
199 uintmax_t poshit;
200 uintmax_t neghit;
201 uintmax_t posmiss;
202 uintmax_t negmiss;
204 uintmax_t rdlockdelayed;
205 uintmax_t wrlockdelayed;
207 uintmax_t addfailed;
209 ref_t array[0];
213 /* Mapped database record. */
214 struct mapped_database
216 const struct database_pers_head *head;
217 const char *data;
218 size_t mapsize;
219 int counter; /* > 0 indicates it isusable. */
221 #define NO_MAPPING ((struct mapped_database *) -1l)
223 struct locked_map_ptr
225 int lock;
226 struct mapped_database *mapped;
228 #define libc_locked_map_ptr(name) static struct locked_map_ptr name
231 /* Open socket connection to nscd server. */
232 extern int __nscd_open_socket (const char *key, size_t keylen,
233 request_type type, void *response,
234 size_t responselen) attribute_hidden;
236 /* Get reference of mapping. */
237 extern struct mapped_database *__nscd_get_map_ref (request_type type,
238 const char *name,
239 struct locked_map_ptr *mapptr,
240 volatile int *gc_cyclep);
242 /* Unmap database. */
243 extern void __nscd_unmap (struct mapped_database *mapped);
245 /* Drop reference of mapping. */
246 static inline int __nscd_drop_map_ref (struct mapped_database *map,
247 int gc_cycle)
249 if (map != NO_MAPPING)
251 if (__builtin_expect (map->head->gc_cycle != gc_cycle, 0))
252 /* We might have read inconsistent data. */
253 return -1;
255 if (atomic_decrement_val (&map->counter) == 0)
256 __nscd_unmap (map);
259 return 0;
263 /* Search the mapped database. */
264 extern const struct datahead *__nscd_cache_search (request_type type,
265 const char *key,
266 size_t keylen,
267 const struct mapped_database *mapped);
269 #endif /* nscd.h */