]> git.ipfire.org Git - thirdparty/glibc.git/blob - nscd/nscd.h
Optimize xmalloc, xcalloc, xrealloc, and xstrdup
[thirdparty/glibc.git] / nscd / nscd.h
1 /* Copyright (c) 1998-2001, 2003-2009, 2011, 2012 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Thorsten Kukuk <kukuk@suse.de>, 1998.
4
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.
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
13 Lesser General Public License for more details.
14
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. */
19
20 #ifndef _NSCD_H
21 #define _NSCD_H 1
22
23 #include <pthread.h>
24 #include <stdbool.h>
25 #include <time.h>
26 #include <sys/uio.h>
27
28 /* The declarations for the request and response types are in the file
29 "nscd-client.h", which should contain everything needed by client
30 functions. */
31 #include "nscd-client.h"
32
33
34 /* Handle databases. */
35 typedef enum
36 {
37 pwddb,
38 grpdb,
39 hstdb,
40 servdb,
41 netgrdb,
42 lastdb
43 } dbtype;
44
45
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
52
53
54 /* Time before restarting the process in paranoia mode. */
55 #define RESTART_INTERVAL (60 * 60)
56
57
58 /* Stack size for worker threads. */
59 #define NSCD_THREAD_STACKSIZE 1024 * 1024 * (sizeof (void *) / 4)
60
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)
64
65
66 /* Registered filename used to fill database. */
67 struct traced_file
68 {
69 time_t mtime;
70 struct traced_file *next;
71 int call_res_init;
72 int inotify_descr;
73 char fname[];
74 };
75
76
77 /* Structure describing dynamic part of one database. */
78 struct database_dyn
79 {
80 pthread_rwlock_t lock;
81 pthread_cond_t prune_cond;
82 pthread_mutex_t prune_lock;
83 pthread_mutex_t prune_run_lock;
84 time_t wakeup_time;
85
86 int enabled;
87 int check_file;
88 int clear_cache;
89 int persistent;
90 int shared;
91 int propagate;
92 struct traced_file *traced_files;
93 const char *db_filename;
94 time_t file_mtime;
95 size_t suggested_module;
96 size_t max_db_size;
97
98 unsigned long int postimeout; /* In seconds. */
99 unsigned long int negtimeout; /* In seconds. */
100
101 int wr_fd; /* Writable file descriptor. */
102 int ro_fd; /* Unwritable file descriptor. */
103
104 const struct iovec *disabled_iov;
105
106 struct database_pers_head *head;
107 char *data;
108 size_t memsize;
109 pthread_mutex_t memlock;
110 bool mmap_used;
111 bool last_alloc_failed;
112 };
113
114
115 /* Paths of the file for the persistent storage. */
116 #define _PATH_NSCD_PASSWD_DB "/var/db/nscd/passwd"
117 #define _PATH_NSCD_GROUP_DB "/var/db/nscd/group"
118 #define _PATH_NSCD_HOSTS_DB "/var/db/nscd/hosts"
119 #define _PATH_NSCD_SERVICES_DB "/var/db/nscd/services"
120 #define _PATH_NSCD_NETGROUP_DB "/var/db/nscd/netgroup"
121
122 /* Path used when not using persistent storage. */
123 #define _PATH_NSCD_XYZ_DB_TMP "/var/run/nscd/dbXXXXXX"
124
125 /* Maximum alignment requirement we will encounter. */
126 #define BLOCK_ALIGN_LOG 3
127 #define BLOCK_ALIGN (1 << BLOCK_ALIGN_LOG)
128 #define BLOCK_ALIGN_M1 (BLOCK_ALIGN - 1)
129
130 /* Default value for the maximum size of the database files. */
131 #define DEFAULT_MAX_DB_SIZE (32 * 1024 * 1024)
132
133 /* Number of bytes of data we initially reserve for each hash table bucket. */
134 #define DEFAULT_DATASIZE_PER_BUCKET 1024
135
136 /* Default module of hash table. */
137 #define DEFAULT_SUGGESTED_MODULE 211
138
139
140 /* Number of seconds between two cache pruning runs if we do not have
141 better information when it is really needed. */
142 #define CACHE_PRUNE_INTERVAL 15
143
144
145 /* Global variables. */
146 extern struct database_dyn dbs[lastdb] attribute_hidden;
147 extern const char *const dbnames[lastdb];
148 extern const char *const serv2str[LASTREQ];
149
150 extern const struct iovec pwd_iov_disabled;
151 extern const struct iovec grp_iov_disabled;
152 extern const struct iovec hst_iov_disabled;
153 extern const struct iovec serv_iov_disabled;
154 extern const struct iovec netgroup_iov_disabled;
155
156
157 /* Initial number of threads to run. */
158 extern int nthreads;
159 /* Maximum number of threads to use. */
160 extern int max_nthreads;
161
162 /* Inotify descriptor. */
163 extern int inotify_fd;
164
165 /* User name to run server processes as. */
166 extern const char *server_user;
167
168 /* Name and UID of user who is allowed to request statistics. */
169 extern const char *stat_user;
170 extern uid_t stat_uid;
171
172 /* Time the server was started. */
173 extern time_t start_time;
174
175 /* Number of times clients had to wait. */
176 extern unsigned long int client_queued;
177
178 /* Maximum needed alignment. */
179 extern const size_t block_align;
180
181 /* Number of times a value is reloaded without being used. UINT_MAX
182 means unlimited. */
183 extern unsigned int reload_count;
184
185 /* Pagesize minus one. */
186 extern uintptr_t pagesize_m1;
187
188 /* Nonzero if paranoia mode is enabled. */
189 extern int paranoia;
190 /* Time after which the process restarts. */
191 extern time_t restart_time;
192 /* How much time between restarts. */
193 extern time_t restart_interval;
194 /* Old current working directory. */
195 extern const char *oldcwd;
196 /* Old user and group ID. */
197 extern uid_t old_uid;
198 extern gid_t old_gid;
199
200
201 /* Prototypes for global functions. */
202
203 /* Wrapper functions with error checking for standard functions. */
204 extern void *xmalloc (size_t n)
205 __attribute_malloc__ __attribute_alloc_size (1);
206 extern void *xcalloc (size_t n, size_t s)
207 __attribute_malloc__ __attribute_alloc_size (1, 2);
208 extern void *xrealloc (void *o, size_t n)
209 __attribute_malloc__ __attribute_alloc_size (2);
210
211 /* nscd.c */
212 extern void termination_handler (int signum) __attribute__ ((__noreturn__));
213 extern int nscd_open_socket (void);
214
215 /* connections.c */
216 extern void nscd_init (void);
217 extern void register_traced_file (size_t dbidx, struct traced_file *finfo);
218 extern void close_sockets (void);
219 extern void start_threads (void) __attribute__ ((__noreturn__));
220
221 /* nscd_conf.c */
222 extern int nscd_parse_file (const char *fname,
223 struct database_dyn dbs[lastdb]);
224
225 /* nscd_stat.c */
226 extern void send_stats (int fd, struct database_dyn dbs[lastdb]);
227 extern int receive_print_stats (void) __attribute__ ((__noreturn__));
228
229 /* cache.c */
230 extern struct datahead *cache_search (request_type, const void *key,
231 size_t len, struct database_dyn *table,
232 uid_t owner);
233 extern int cache_add (int type, const void *key, size_t len,
234 struct datahead *packet, bool first,
235 struct database_dyn *table, uid_t owner,
236 bool prune_wakeup);
237 extern time_t prune_cache (struct database_dyn *table, time_t now, int fd);
238
239 /* pwdcache.c */
240 extern void addpwbyname (struct database_dyn *db, int fd, request_header *req,
241 void *key, uid_t uid);
242 extern void addpwbyuid (struct database_dyn *db, int fd, request_header *req,
243 void *key, uid_t uid);
244 extern time_t readdpwbyname (struct database_dyn *db, struct hashentry *he,
245 struct datahead *dh);
246 extern time_t readdpwbyuid (struct database_dyn *db, struct hashentry *he,
247 struct datahead *dh);
248
249 /* grpcache.c */
250 extern void addgrbyname (struct database_dyn *db, int fd, request_header *req,
251 void *key, uid_t uid);
252 extern void addgrbygid (struct database_dyn *db, int fd, request_header *req,
253 void *key, uid_t uid);
254 extern time_t readdgrbyname (struct database_dyn *db, struct hashentry *he,
255 struct datahead *dh);
256 extern time_t readdgrbygid (struct database_dyn *db, struct hashentry *he,
257 struct datahead *dh);
258
259 /* hstcache.c */
260 extern void addhstbyname (struct database_dyn *db, int fd, request_header *req,
261 void *key, uid_t uid);
262 extern void addhstbyaddr (struct database_dyn *db, int fd, request_header *req,
263 void *key, uid_t uid);
264 extern void addhstbynamev6 (struct database_dyn *db, int fd,
265 request_header *req, void *key, uid_t uid);
266 extern void addhstbyaddrv6 (struct database_dyn *db, int fd,
267 request_header *req, void *key, uid_t uid);
268 extern time_t readdhstbyname (struct database_dyn *db, struct hashentry *he,
269 struct datahead *dh);
270 extern time_t readdhstbyaddr (struct database_dyn *db, struct hashentry *he,
271 struct datahead *dh);
272 extern time_t readdhstbynamev6 (struct database_dyn *db, struct hashentry *he,
273 struct datahead *dh);
274 extern time_t readdhstbyaddrv6 (struct database_dyn *db, struct hashentry *he,
275 struct datahead *dh);
276
277 /* aicache.c */
278 extern void addhstai (struct database_dyn *db, int fd, request_header *req,
279 void *key, uid_t uid);
280 extern time_t readdhstai (struct database_dyn *db, struct hashentry *he,
281 struct datahead *dh);
282
283
284 /* initgrcache.c */
285 extern void addinitgroups (struct database_dyn *db, int fd,
286 request_header *req, void *key, uid_t uid);
287 extern time_t readdinitgroups (struct database_dyn *db, struct hashentry *he,
288 struct datahead *dh);
289
290 /* servicecache.c */
291 extern void addservbyname (struct database_dyn *db, int fd,
292 request_header *req, void *key, uid_t uid);
293 extern time_t readdservbyname (struct database_dyn *db, struct hashentry *he,
294 struct datahead *dh);
295 extern void addservbyport (struct database_dyn *db, int fd,
296 request_header *req, void *key, uid_t uid);
297 extern time_t readdservbyport (struct database_dyn *db, struct hashentry *he,
298 struct datahead *dh);
299
300 /* netgroupcache.c */
301 extern void addinnetgr (struct database_dyn *db, int fd, request_header *req,
302 void *key, uid_t uid);
303 extern time_t readdinnetgr (struct database_dyn *db, struct hashentry *he,
304 struct datahead *dh);
305 extern void addgetnetgrent (struct database_dyn *db, int fd,
306 request_header *req, void *key, uid_t uid);
307 extern time_t readdgetnetgrent (struct database_dyn *db, struct hashentry *he,
308 struct datahead *dh);
309
310 /* mem.c */
311 extern void *mempool_alloc (struct database_dyn *db, size_t len,
312 int data_alloc);
313 extern void gc (struct database_dyn *db);
314
315
316 /* nscd_setup_thread.c */
317 extern int setup_thread (struct database_dyn *db);
318
319
320 /* Special version of TEMP_FAILURE_RETRY for functions returning error
321 values. */
322 #define TEMP_FAILURE_RETRY_VAL(expression) \
323 (__extension__ \
324 ({ long int __result; \
325 do __result = (long int) (expression); \
326 while (__result == EINTR); \
327 __result; }))
328
329 #endif /* nscd.h */