2.9
[glibc/nacl-glibc.git] / nscd / nscd.h
blob5c77dd3c4175c5d466bcb11afb02fda389f6f309
1 /* Copyright (c) 1998, 1999, 2000, 2001, 2003, 2004, 2005, 2006, 2007, 2008
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, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
21 #ifndef _NSCD_H
22 #define _NSCD_H 1
24 #include <pthread.h>
25 #include <stdbool.h>
26 #include <time.h>
27 #include <sys/uio.h>
29 /* The declarations for the request and response types are in the file
30 "nscd-client.h", which should contain everything needed by client
31 functions. */
32 #include "nscd-client.h"
35 /* Handle databases. */
36 typedef enum
38 pwddb,
39 grpdb,
40 hstdb,
41 servdb,
42 lastdb
43 } dbtype;
46 /* Default limit on the number of times a value gets reloaded without
47 being used in the meantime. NSCD does not throw a value out as
48 soon as it times out. It tries to reload the value from the
49 server. Only if the value has not been used for so many rounds it
50 is removed. */
51 #define DEFAULT_RELOAD_LIMIT 5
54 /* Time before restarting the process in paranoia mode. */
55 #define RESTART_INTERVAL (60 * 60)
58 /* Stack size for worker threads. */
59 #define NSCD_THREAD_STACKSIZE 1024 * 1024 * (sizeof (void *) / 4)
61 /* Maximum size of stack frames we allow the thread to use. We use
62 80% of the thread stack size. */
63 #define MAX_STACK_USE ((8 * NSCD_THREAD_STACKSIZE) / 10)
66 /* Structure describing dynamic part of one database. */
67 struct database_dyn
69 pthread_rwlock_t lock;
70 pthread_cond_t prune_cond;
71 pthread_mutex_t prune_lock;
72 time_t wakeup_time;
74 int enabled;
75 int check_file;
76 int inotify_descr;
77 int clear_cache;
78 int persistent;
79 int shared;
80 int propagate;
81 int reset_res;
82 const char filename[16];
83 const char *db_filename;
84 time_t file_mtime;
85 size_t suggested_module;
86 size_t max_db_size;
88 unsigned long int postimeout; /* In seconds. */
89 unsigned long int negtimeout; /* In seconds. */
91 int wr_fd; /* Writable file descriptor. */
92 int ro_fd; /* Unwritable file descriptor. */
94 const struct iovec *disabled_iov;
96 struct database_pers_head *head;
97 char *data;
98 size_t memsize;
99 pthread_mutex_t memlock;
100 bool mmap_used;
101 bool last_alloc_failed;
105 /* Paths of the file for the persistent storage. */
106 #define _PATH_NSCD_PASSWD_DB "/var/db/nscd/passwd"
107 #define _PATH_NSCD_GROUP_DB "/var/db/nscd/group"
108 #define _PATH_NSCD_HOSTS_DB "/var/db/nscd/hosts"
109 #define _PATH_NSCD_SERVICES_DB "/var/db/nscd/services"
111 /* Path used when not using persistent storage. */
112 #define _PATH_NSCD_XYZ_DB_TMP "/var/run/nscd/dbXXXXXX"
114 /* Maximum alignment requirement we will encounter. */
115 #define BLOCK_ALIGN_LOG 3
116 #define BLOCK_ALIGN (1 << BLOCK_ALIGN_LOG)
117 #define BLOCK_ALIGN_M1 (BLOCK_ALIGN - 1)
119 /* Default value for the maximum size of the database files. */
120 #define DEFAULT_MAX_DB_SIZE (32 * 1024 * 1024)
122 /* Number of bytes of data we initially reserve for each hash table bucket. */
123 #define DEFAULT_DATASIZE_PER_BUCKET 1024
125 /* Default module of hash table. */
126 #define DEFAULT_SUGGESTED_MODULE 211
129 /* Number of seconds between two cache pruning runs if we do not have
130 better information when it is really needed. */
131 #define CACHE_PRUNE_INTERVAL 15
134 /* Global variables. */
135 extern struct database_dyn dbs[lastdb] attribute_hidden;
136 extern const char *const dbnames[lastdb];
137 extern const char *const serv2str[LASTREQ];
139 extern const struct iovec pwd_iov_disabled;
140 extern const struct iovec grp_iov_disabled;
141 extern const struct iovec hst_iov_disabled;
142 extern const struct iovec serv_iov_disabled;
145 /* Initial number of threads to run. */
146 extern int nthreads;
147 /* Maximum number of threads to use. */
148 extern int max_nthreads;
150 /* User name to run server processes as. */
151 extern const char *server_user;
153 /* Name and UID of user who is allowed to request statistics. */
154 extern const char *stat_user;
155 extern uid_t stat_uid;
157 /* Time the server was started. */
158 extern time_t start_time;
160 /* Number of times clients had to wait. */
161 extern unsigned long int client_queued;
163 /* Maximum needed alignment. */
164 extern const size_t block_align;
166 /* Number of times a value is reloaded without being used. UINT_MAX
167 means unlimited. */
168 extern unsigned int reload_count;
170 /* Pagesize minus one. */
171 extern uintptr_t pagesize_m1;
173 /* Nonzero if paranoia mode is enabled. */
174 extern int paranoia;
175 /* Time after which the process restarts. */
176 extern time_t restart_time;
177 /* How much time between restarts. */
178 extern time_t restart_interval;
179 /* Old current working directory. */
180 extern const char *oldcwd;
181 /* Old user and group ID. */
182 extern uid_t old_uid;
183 extern gid_t old_gid;
186 /* Memory allocation in flight. Each thread can have a limited number
187 of allocation in flight. No need to create dynamic data
188 structures. We use fixed indices. */
189 enum in_flight
191 IDX_result_data = 0,
192 /* Keep the IDX_record_data entry last at all times. */
193 IDX_record_data = 1,
194 IDX_last
196 extern __thread struct mem_in_flight
198 struct
200 int dbidx;
201 nscd_ssize_t blocklen;
202 nscd_ssize_t blockoff;
203 } block[IDX_last];
205 struct mem_in_flight *next;
206 } mem_in_flight attribute_tls_model_ie;
207 /* Global list of the mem_in_flight variables of all the threads. */
208 extern struct mem_in_flight *mem_in_flight_list;
211 /* Prototypes for global functions. */
213 /* nscd.c */
214 extern void termination_handler (int signum) __attribute__ ((__noreturn__));
215 extern int nscd_open_socket (void);
217 /* connections.c */
218 extern void nscd_init (void);
219 extern void close_sockets (void);
220 extern void start_threads (void) __attribute__ ((__noreturn__));
222 /* nscd_conf.c */
223 extern int nscd_parse_file (const char *fname,
224 struct database_dyn dbs[lastdb]);
226 /* nscd_stat.c */
227 extern void send_stats (int fd, struct database_dyn dbs[lastdb]);
228 extern int receive_print_stats (void) __attribute__ ((__noreturn__));
230 /* cache.c */
231 extern struct datahead *cache_search (request_type, void *key, size_t len,
232 struct database_dyn *table,
233 uid_t owner);
234 extern int cache_add (int type, const void *key, size_t len,
235 struct datahead *packet, bool first,
236 struct database_dyn *table, uid_t owner,
237 bool prune_wakeup);
238 extern time_t prune_cache (struct database_dyn *table, time_t now, int fd);
240 /* pwdcache.c */
241 extern void addpwbyname (struct database_dyn *db, int fd, request_header *req,
242 void *key, uid_t uid);
243 extern void addpwbyuid (struct database_dyn *db, int fd, request_header *req,
244 void *key, uid_t uid);
245 extern void readdpwbyname (struct database_dyn *db, struct hashentry *he,
246 struct datahead *dh);
247 extern void readdpwbyuid (struct database_dyn *db, struct hashentry *he,
248 struct datahead *dh);
250 /* grpcache.c */
251 extern void addgrbyname (struct database_dyn *db, int fd, request_header *req,
252 void *key, uid_t uid);
253 extern void addgrbygid (struct database_dyn *db, int fd, request_header *req,
254 void *key, uid_t uid);
255 extern void readdgrbyname (struct database_dyn *db, struct hashentry *he,
256 struct datahead *dh);
257 extern void readdgrbygid (struct database_dyn *db, struct hashentry *he,
258 struct datahead *dh);
260 /* hstcache.c */
261 extern void addhstbyname (struct database_dyn *db, int fd, request_header *req,
262 void *key, uid_t uid);
263 extern void addhstbyaddr (struct database_dyn *db, int fd, request_header *req,
264 void *key, uid_t uid);
265 extern void addhstbynamev6 (struct database_dyn *db, int fd,
266 request_header *req, void *key, uid_t uid);
267 extern void addhstbyaddrv6 (struct database_dyn *db, int fd,
268 request_header *req, void *key, uid_t uid);
269 extern void readdhstbyname (struct database_dyn *db, struct hashentry *he,
270 struct datahead *dh);
271 extern void readdhstbyaddr (struct database_dyn *db, struct hashentry *he,
272 struct datahead *dh);
273 extern void readdhstbynamev6 (struct database_dyn *db, struct hashentry *he,
274 struct datahead *dh);
275 extern void readdhstbyaddrv6 (struct database_dyn *db, struct hashentry *he,
276 struct datahead *dh);
278 /* aicache.c */
279 extern void addhstai (struct database_dyn *db, int fd, request_header *req,
280 void *key, uid_t uid);
281 extern void readdhstai (struct database_dyn *db, struct hashentry *he,
282 struct datahead *dh);
285 /* initgrcache.c */
286 extern void addinitgroups (struct database_dyn *db, int fd,
287 request_header *req, void *key, uid_t uid);
288 extern void readdinitgroups (struct database_dyn *db, struct hashentry *he,
289 struct datahead *dh);
291 /* servicecache.c */
292 extern void addservbyname (struct database_dyn *db, int fd,
293 request_header *req, void *key, uid_t uid);
294 extern void readdservbyname (struct database_dyn *db, struct hashentry *he,
295 struct datahead *dh);
296 extern void addservbyport (struct database_dyn *db, int fd,
297 request_header *req, void *key, uid_t uid);
298 extern void readdservbyport (struct database_dyn *db, struct hashentry *he,
299 struct datahead *dh);
301 /* mem.c */
302 extern void *mempool_alloc (struct database_dyn *db, size_t len,
303 enum in_flight idx);
304 extern void gc (struct database_dyn *db);
307 /* nscd_setup_thread.c */
308 extern int setup_thread (struct database_dyn *db);
311 /* Special version of TEMP_FAILURE_RETRY for functions returning error
312 values. */
313 #define TEMP_FAILURE_RETRY_VAL(expression) \
314 (__extension__ \
315 ({ long int __result; \
316 do __result = (long int) (expression); \
317 while (__result == EINTR); \
318 __result; }))
320 #endif /* nscd.h */