]>
Commit | Line | Data |
---|---|---|
bfff8b1b | 1 | /* Copyright (c) 1998-2017 Free Software Foundation, Inc. |
d67281a7 | 2 | This file is part of the GNU C Library. |
a1c542bf | 3 | Contributed by Thorsten Kukuk <kukuk@suse.de>, 1998. |
d67281a7 UD |
4 | |
5 | The GNU C Library is free software; you can redistribute it and/or | |
41bdb6e2 AJ |
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. | |
d67281a7 UD |
9 | |
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 | |
41bdb6e2 | 13 | Lesser General Public License for more details. |
d67281a7 | 14 | |
41bdb6e2 | 15 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 PE |
16 | License along with the GNU C Library; if not, see |
17 | <http://www.gnu.org/licenses/>. */ | |
d67281a7 UD |
18 | |
19 | #ifndef _NSCD_H | |
67479a70 UD |
20 | #define _NSCD_H 1 |
21 | ||
22 | #include <pthread.h> | |
a95a08b4 | 23 | #include <stdbool.h> |
67479a70 UD |
24 | #include <time.h> |
25 | #include <sys/uio.h> | |
d67281a7 | 26 | |
4f6bfa80 RM |
27 | /* The declarations for the request and response types are in the file |
28 | "nscd-client.h", which should contain everything needed by client | |
29 | functions. */ | |
30 | #include "nscd-client.h" | |
67479a70 | 31 | |
d67281a7 | 32 | |
67479a70 UD |
33 | /* Handle databases. */ |
34 | typedef enum | |
35 | { | |
36 | pwddb, | |
37 | grpdb, | |
38 | hstdb, | |
b21fa963 | 39 | servdb, |
684ae515 | 40 | netgrdb, |
67479a70 UD |
41 | lastdb |
42 | } dbtype; | |
d67281a7 | 43 | |
67479a70 | 44 | |
a95a08b4 UD |
45 | /* Default limit on the number of times a value gets reloaded without |
46 | being used in the meantime. NSCD does not throw a value out as | |
47 | soon as it times out. It tries to reload the value from the | |
48 | server. Only if the value has not been used for so many rounds it | |
49 | is removed. */ | |
50 | #define DEFAULT_RELOAD_LIMIT 5 | |
51 | ||
52 | ||
4401d759 UD |
53 | /* Time before restarting the process in paranoia mode. */ |
54 | #define RESTART_INTERVAL (60 * 60) | |
55 | ||
56 | ||
7ea8eb02 UD |
57 | /* Stack size for worker threads. */ |
58 | #define NSCD_THREAD_STACKSIZE 1024 * 1024 * (sizeof (void *) / 4) | |
59 | ||
30294ea4 UD |
60 | /* Maximum size of stack frames we allow the thread to use. We use |
61 | 80% of the thread stack size. */ | |
62 | #define MAX_STACK_USE ((8 * NSCD_THREAD_STACKSIZE) / 10) | |
63 | ||
cf9313e7 CD |
64 | /* Records the file registered per database that when changed |
65 | or modified requires invalidating the database. */ | |
319b9ad4 UD |
66 | struct traced_file |
67 | { | |
cf9313e7 | 68 | /* Tracks the last modified time of the traced file. */ |
319b9ad4 | 69 | time_t mtime; |
cf9313e7 | 70 | /* Support multiple registered files per database. */ |
319b9ad4 UD |
71 | struct traced_file *next; |
72 | int call_res_init; | |
cf9313e7 CD |
73 | /* Requires Inotify support to do anything useful. */ |
74 | #define TRACED_FILE 0 | |
75 | #define TRACED_DIR 1 | |
76 | int inotify_descr[2]; | |
77 | # ifndef PATH_MAX | |
78 | # define PATH_MAX 1024 | |
79 | # endif | |
80 | /* The parent directory is used to scan for creation/deletion. */ | |
81 | char dname[PATH_MAX]; | |
82 | /* Just the name of the file with no directory component. */ | |
83 | char *sfname; | |
84 | /* The full-path name of the registered file. */ | |
319b9ad4 UD |
85 | char fname[]; |
86 | }; | |
87 | ||
cf9313e7 CD |
88 | /* Initialize a `struct traced_file`. As input we need the name |
89 | of the file, and if invalidation requires calling res_init. | |
90 | If CRINIT is 1 then res_init will be called after invalidation | |
91 | or if the traced file is changed in any way, otherwise it will | |
92 | not. */ | |
93 | static inline void | |
94 | init_traced_file(struct traced_file *file, const char *fname, int crinit) | |
95 | { | |
96 | char *dname; | |
97 | file->mtime = 0; | |
98 | file->inotify_descr[TRACED_FILE] = -1; | |
99 | file->inotify_descr[TRACED_DIR] = -1; | |
100 | strcpy (file->fname, fname); | |
101 | /* Compute the parent directory name and store a copy. The copy makes | |
102 | it much faster to add/remove watches while nscd is running instead | |
103 | of computing this over and over again in a temp buffer. */ | |
104 | file->dname[0] = '\0'; | |
105 | dname = strrchr (fname, '/'); | |
106 | if (dname != NULL) | |
107 | { | |
108 | size_t len = (size_t)(dname - fname); | |
109 | if (len > sizeof (file->dname)) | |
110 | abort (); | |
111 | strncpy (file->dname, file->fname, len); | |
112 | file->dname[len] = '\0'; | |
113 | } | |
114 | /* The basename is the name just after the last forward slash. */ | |
115 | file->sfname = &dname[1]; | |
116 | file->call_res_init = crinit; | |
117 | } | |
118 | ||
119 | #define define_traced_file(id, filename) \ | |
120 | static union \ | |
121 | { \ | |
122 | struct traced_file file; \ | |
123 | char buf[sizeof (struct traced_file) + sizeof (filename)]; \ | |
124 | } id##_traced_file; | |
319b9ad4 | 125 | |
a95a08b4 UD |
126 | /* Structure describing dynamic part of one database. */ |
127 | struct database_dyn | |
67479a70 UD |
128 | { |
129 | pthread_rwlock_t lock; | |
ffb1b882 UD |
130 | pthread_cond_t prune_cond; |
131 | pthread_mutex_t prune_lock; | |
cd72adeb | 132 | pthread_mutex_t prune_run_lock; |
ffb1b882 | 133 | time_t wakeup_time; |
67479a70 UD |
134 | |
135 | int enabled; | |
136 | int check_file; | |
5228ba2f | 137 | int clear_cache; |
a95a08b4 | 138 | int persistent; |
c207f23b | 139 | int shared; |
797ed6f7 | 140 | int propagate; |
319b9ad4 | 141 | struct traced_file *traced_files; |
a95a08b4 | 142 | const char *db_filename; |
a95a08b4 | 143 | size_t suggested_module; |
eaa27aca | 144 | size_t max_db_size; |
67479a70 | 145 | |
a95a08b4 UD |
146 | unsigned long int postimeout; /* In seconds. */ |
147 | unsigned long int negtimeout; /* In seconds. */ | |
67479a70 | 148 | |
a95a08b4 UD |
149 | int wr_fd; /* Writable file descriptor. */ |
150 | int ro_fd; /* Unwritable file descriptor. */ | |
67479a70 | 151 | |
a95a08b4 | 152 | const struct iovec *disabled_iov; |
67479a70 | 153 | |
a95a08b4 UD |
154 | struct database_pers_head *head; |
155 | char *data; | |
156 | size_t memsize; | |
157 | pthread_mutex_t memlock; | |
158 | bool mmap_used; | |
159 | bool last_alloc_failed; | |
160 | }; | |
c86e6aec | 161 | |
c86e6aec | 162 | |
a95a08b4 | 163 | /* Paths of the file for the persistent storage. */ |
67881f5e AS |
164 | #define _PATH_NSCD_PASSWD_DB "/var/db/nscd/passwd" |
165 | #define _PATH_NSCD_GROUP_DB "/var/db/nscd/group" | |
166 | #define _PATH_NSCD_HOSTS_DB "/var/db/nscd/hosts" | |
167 | #define _PATH_NSCD_SERVICES_DB "/var/db/nscd/services" | |
168 | #define _PATH_NSCD_NETGROUP_DB "/var/db/nscd/netgroup" | |
457962e5 UD |
169 | |
170 | /* Path used when not using persistent storage. */ | |
171 | #define _PATH_NSCD_XYZ_DB_TMP "/var/run/nscd/dbXXXXXX" | |
67479a70 | 172 | |
fa546abe UD |
173 | /* Maximum alignment requirement we will encounter. */ |
174 | #define BLOCK_ALIGN_LOG 3 | |
175 | #define BLOCK_ALIGN (1 << BLOCK_ALIGN_LOG) | |
176 | #define BLOCK_ALIGN_M1 (BLOCK_ALIGN - 1) | |
177 | ||
2c210d1e UD |
178 | /* Default value for the maximum size of the database files. */ |
179 | #define DEFAULT_MAX_DB_SIZE (32 * 1024 * 1024) | |
180 | ||
181 | /* Number of bytes of data we initially reserve for each hash table bucket. */ | |
182 | #define DEFAULT_DATASIZE_PER_BUCKET 1024 | |
183 | ||
27c377dd UD |
184 | /* Default module of hash table. */ |
185 | #define DEFAULT_SUGGESTED_MODULE 211 | |
186 | ||
67479a70 | 187 | |
ffb1b882 UD |
188 | /* Number of seconds between two cache pruning runs if we do not have |
189 | better information when it is really needed. */ | |
190 | #define CACHE_PRUNE_INTERVAL 15 | |
191 | ||
192 | ||
67479a70 | 193 | /* Global variables. */ |
7eb5e6c9 | 194 | extern struct database_dyn dbs[lastdb] attribute_hidden; |
b21fa963 UD |
195 | extern const char *const dbnames[lastdb]; |
196 | extern const char *const serv2str[LASTREQ]; | |
67479a70 UD |
197 | |
198 | extern const struct iovec pwd_iov_disabled; | |
199 | extern const struct iovec grp_iov_disabled; | |
200 | extern const struct iovec hst_iov_disabled; | |
b21fa963 | 201 | extern const struct iovec serv_iov_disabled; |
684ae515 | 202 | extern const struct iovec netgroup_iov_disabled; |
67479a70 | 203 | |
a95a08b4 | 204 | |
27e82856 | 205 | /* Initial number of threads to run. */ |
67479a70 | 206 | extern int nthreads; |
27e82856 UD |
207 | /* Maximum number of threads to use. */ |
208 | extern int max_nthreads; | |
67479a70 | 209 | |
319b9ad4 UD |
210 | /* Inotify descriptor. */ |
211 | extern int inotify_fd; | |
212 | ||
a12ce44f | 213 | /* User name to run server processes as. */ |
adcf0e4a UD |
214 | extern const char *server_user; |
215 | ||
a12ce44f UD |
216 | /* Name and UID of user who is allowed to request statistics. */ |
217 | extern const char *stat_user; | |
218 | extern uid_t stat_uid; | |
219 | ||
c86e6aec UD |
220 | /* Time the server was started. */ |
221 | extern time_t start_time; | |
222 | ||
a16e3585 UD |
223 | /* Number of times clients had to wait. */ |
224 | extern unsigned long int client_queued; | |
225 | ||
a95a08b4 UD |
226 | /* Maximum needed alignment. */ |
227 | extern const size_t block_align; | |
228 | ||
229 | /* Number of times a value is reloaded without being used. UINT_MAX | |
230 | means unlimited. */ | |
231 | extern unsigned int reload_count; | |
232 | ||
3418007e UD |
233 | /* Pagesize minus one. */ |
234 | extern uintptr_t pagesize_m1; | |
235 | ||
4401d759 UD |
236 | /* Nonzero if paranoia mode is enabled. */ |
237 | extern int paranoia; | |
238 | /* Time after which the process restarts. */ | |
239 | extern time_t restart_time; | |
240 | /* How much time between restarts. */ | |
241 | extern time_t restart_interval; | |
242 | /* Old current working directory. */ | |
243 | extern const char *oldcwd; | |
244 | /* Old user and group ID. */ | |
245 | extern uid_t old_uid; | |
246 | extern gid_t old_gid; | |
247 | ||
248 | ||
67479a70 UD |
249 | /* Prototypes for global functions. */ |
250 | ||
684ae515 | 251 | /* Wrapper functions with error checking for standard functions. */ |
6ff444c4 | 252 | #include <programs/xmalloc.h> |
684ae515 | 253 | |
67479a70 | 254 | /* nscd.c */ |
59553897 | 255 | extern void termination_handler (int signum) __attribute__ ((__noreturn__)); |
67479a70 | 256 | extern int nscd_open_socket (void); |
532a6035 SP |
257 | void notify_parent (int child_ret); |
258 | void do_exit (int child_ret, int errnum, const char *format, ...); | |
67479a70 UD |
259 | |
260 | /* connections.c */ | |
a12ce44f | 261 | extern void nscd_init (void); |
319b9ad4 | 262 | extern void register_traced_file (size_t dbidx, struct traced_file *finfo); |
cf9313e7 CD |
263 | #ifdef HAVE_INOTIFY |
264 | extern void install_watches (struct traced_file *finfo); | |
265 | #endif | |
67479a70 | 266 | extern void close_sockets (void); |
8f11d6f9 | 267 | extern void start_threads (void) __attribute__ ((__noreturn__)); |
67479a70 UD |
268 | |
269 | /* nscd_conf.c */ | |
a95a08b4 UD |
270 | extern int nscd_parse_file (const char *fname, |
271 | struct database_dyn dbs[lastdb]); | |
67479a70 UD |
272 | |
273 | /* nscd_stat.c */ | |
a95a08b4 | 274 | extern void send_stats (int fd, struct database_dyn dbs[lastdb]); |
59553897 | 275 | extern int receive_print_stats (void) __attribute__ ((__noreturn__)); |
67479a70 UD |
276 | |
277 | /* cache.c */ | |
684ae515 UD |
278 | extern struct datahead *cache_search (request_type, const void *key, |
279 | size_t len, struct database_dyn *table, | |
a95a08b4 UD |
280 | uid_t owner); |
281 | extern int cache_add (int type, const void *key, size_t len, | |
282 | struct datahead *packet, bool first, | |
528741cb UD |
283 | struct database_dyn *table, uid_t owner, |
284 | bool prune_wakeup); | |
ffb1b882 | 285 | extern time_t prune_cache (struct database_dyn *table, time_t now, int fd); |
67479a70 UD |
286 | |
287 | /* pwdcache.c */ | |
a95a08b4 | 288 | extern void addpwbyname (struct database_dyn *db, int fd, request_header *req, |
a1c542bf | 289 | void *key, uid_t uid); |
a95a08b4 | 290 | extern void addpwbyuid (struct database_dyn *db, int fd, request_header *req, |
a1c542bf | 291 | void *key, uid_t uid); |
a4c7ea7b UD |
292 | extern time_t readdpwbyname (struct database_dyn *db, struct hashentry *he, |
293 | struct datahead *dh); | |
294 | extern time_t readdpwbyuid (struct database_dyn *db, struct hashentry *he, | |
295 | struct datahead *dh); | |
67479a70 UD |
296 | |
297 | /* grpcache.c */ | |
a95a08b4 | 298 | extern void addgrbyname (struct database_dyn *db, int fd, request_header *req, |
a1c542bf | 299 | void *key, uid_t uid); |
a95a08b4 | 300 | extern void addgrbygid (struct database_dyn *db, int fd, request_header *req, |
a1c542bf | 301 | void *key, uid_t uid); |
a4c7ea7b UD |
302 | extern time_t readdgrbyname (struct database_dyn *db, struct hashentry *he, |
303 | struct datahead *dh); | |
304 | extern time_t readdgrbygid (struct database_dyn *db, struct hashentry *he, | |
305 | struct datahead *dh); | |
67479a70 UD |
306 | |
307 | /* hstcache.c */ | |
a95a08b4 | 308 | extern void addhstbyname (struct database_dyn *db, int fd, request_header *req, |
a1c542bf | 309 | void *key, uid_t uid); |
a95a08b4 | 310 | extern void addhstbyaddr (struct database_dyn *db, int fd, request_header *req, |
a1c542bf | 311 | void *key, uid_t uid); |
a95a08b4 UD |
312 | extern void addhstbynamev6 (struct database_dyn *db, int fd, |
313 | request_header *req, void *key, uid_t uid); | |
314 | extern void addhstbyaddrv6 (struct database_dyn *db, int fd, | |
315 | request_header *req, void *key, uid_t uid); | |
a4c7ea7b | 316 | extern time_t readdhstbyname (struct database_dyn *db, struct hashentry *he, |
a95a08b4 | 317 | struct datahead *dh); |
a4c7ea7b | 318 | extern time_t readdhstbyaddr (struct database_dyn *db, struct hashentry *he, |
a95a08b4 | 319 | struct datahead *dh); |
a4c7ea7b UD |
320 | extern time_t readdhstbynamev6 (struct database_dyn *db, struct hashentry *he, |
321 | struct datahead *dh); | |
322 | extern time_t readdhstbyaddrv6 (struct database_dyn *db, struct hashentry *he, | |
323 | struct datahead *dh); | |
a95a08b4 | 324 | |
d19687d6 UD |
325 | /* aicache.c */ |
326 | extern void addhstai (struct database_dyn *db, int fd, request_header *req, | |
327 | void *key, uid_t uid); | |
a4c7ea7b UD |
328 | extern time_t readdhstai (struct database_dyn *db, struct hashentry *he, |
329 | struct datahead *dh); | |
a95a08b4 | 330 | |
26235c7c UD |
331 | |
332 | /* initgrcache.c */ | |
333 | extern void addinitgroups (struct database_dyn *db, int fd, | |
334 | request_header *req, void *key, uid_t uid); | |
a4c7ea7b UD |
335 | extern time_t readdinitgroups (struct database_dyn *db, struct hashentry *he, |
336 | struct datahead *dh); | |
26235c7c | 337 | |
b21fa963 UD |
338 | /* servicecache.c */ |
339 | extern void addservbyname (struct database_dyn *db, int fd, | |
340 | request_header *req, void *key, uid_t uid); | |
a4c7ea7b UD |
341 | extern time_t readdservbyname (struct database_dyn *db, struct hashentry *he, |
342 | struct datahead *dh); | |
b21fa963 UD |
343 | extern void addservbyport (struct database_dyn *db, int fd, |
344 | request_header *req, void *key, uid_t uid); | |
a4c7ea7b UD |
345 | extern time_t readdservbyport (struct database_dyn *db, struct hashentry *he, |
346 | struct datahead *dh); | |
b21fa963 | 347 | |
684ae515 UD |
348 | /* netgroupcache.c */ |
349 | extern void addinnetgr (struct database_dyn *db, int fd, request_header *req, | |
350 | void *key, uid_t uid); | |
351 | extern time_t readdinnetgr (struct database_dyn *db, struct hashentry *he, | |
352 | struct datahead *dh); | |
353 | extern void addgetnetgrent (struct database_dyn *db, int fd, | |
354 | request_header *req, void *key, uid_t uid); | |
355 | extern time_t readdgetnetgrent (struct database_dyn *db, struct hashentry *he, | |
356 | struct datahead *dh); | |
357 | ||
a95a08b4 | 358 | /* mem.c */ |
c52137d3 | 359 | extern void *mempool_alloc (struct database_dyn *db, size_t len, |
20e498bd | 360 | int data_alloc); |
a95a08b4 | 361 | extern void gc (struct database_dyn *db); |
4f6bfa80 | 362 | |
081fc592 UD |
363 | |
364 | /* nscd_setup_thread.c */ | |
ffb1b882 | 365 | extern int setup_thread (struct database_dyn *db); |
081fc592 | 366 | |
2c210d1e UD |
367 | |
368 | /* Special version of TEMP_FAILURE_RETRY for functions returning error | |
369 | values. */ | |
370 | #define TEMP_FAILURE_RETRY_VAL(expression) \ | |
371 | (__extension__ \ | |
372 | ({ long int __result; \ | |
373 | do __result = (long int) (expression); \ | |
374 | while (__result == EINTR); \ | |
375 | __result; })) | |
376 | ||
67479a70 | 377 | #endif /* nscd.h */ |