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.
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
28 /* The declarations for the request and response types are in the file
29 "nscd-client.h", which should contain everything needed by client
31 #include "nscd-client.h"
34 /* Handle databases. */
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
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 /* Registered filename used to fill database. */
70 struct traced_file
*next
;
77 /* Structure describing dynamic part of one database. */
80 pthread_rwlock_t lock
;
81 pthread_cond_t prune_cond
;
82 pthread_mutex_t prune_lock
;
83 pthread_mutex_t prune_run_lock
;
92 struct traced_file
*traced_files
;
93 const char *db_filename
;
95 size_t suggested_module
;
98 unsigned long int postimeout
; /* In seconds. */
99 unsigned long int negtimeout
; /* In seconds. */
101 int wr_fd
; /* Writable file descriptor. */
102 int ro_fd
; /* Unwritable file descriptor. */
104 const struct iovec
*disabled_iov
;
106 struct database_pers_head
*head
;
109 pthread_mutex_t memlock
;
111 bool last_alloc_failed
;
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"
122 /* Path used when not using persistent storage. */
123 #define _PATH_NSCD_XYZ_DB_TMP "/var/run/nscd/dbXXXXXX"
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)
130 /* Default value for the maximum size of the database files. */
131 #define DEFAULT_MAX_DB_SIZE (32 * 1024 * 1024)
133 /* Number of bytes of data we initially reserve for each hash table bucket. */
134 #define DEFAULT_DATASIZE_PER_BUCKET 1024
136 /* Default module of hash table. */
137 #define DEFAULT_SUGGESTED_MODULE 211
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
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
];
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
;
157 /* Initial number of threads to run. */
159 /* Maximum number of threads to use. */
160 extern int max_nthreads
;
162 /* Inotify descriptor. */
163 extern int inotify_fd
;
165 /* User name to run server processes as. */
166 extern const char *server_user
;
168 /* Name and UID of user who is allowed to request statistics. */
169 extern const char *stat_user
;
170 extern uid_t stat_uid
;
172 /* Time the server was started. */
173 extern time_t start_time
;
175 /* Number of times clients had to wait. */
176 extern unsigned long int client_queued
;
178 /* Maximum needed alignment. */
179 extern const size_t block_align
;
181 /* Number of times a value is reloaded without being used. UINT_MAX
183 extern unsigned int reload_count
;
185 /* Pagesize minus one. */
186 extern uintptr_t pagesize_m1
;
188 /* Nonzero if paranoia mode is enabled. */
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
;
201 /* Prototypes for global functions. */
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);
212 extern void termination_handler (int signum
) __attribute__ ((__noreturn__
));
213 extern int nscd_open_socket (void);
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__
));
222 extern int nscd_parse_file (const char *fname
,
223 struct database_dyn dbs
[lastdb
]);
226 extern void send_stats (int fd
, struct database_dyn dbs
[lastdb
]);
227 extern int receive_print_stats (void) __attribute__ ((__noreturn__
));
230 extern struct datahead
*cache_search (request_type
, const void *key
,
231 size_t len
, struct database_dyn
*table
,
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
,
237 extern time_t prune_cache (struct database_dyn
*table
, time_t now
, int fd
);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
311 extern void *mempool_alloc (struct database_dyn
*db
, size_t len
,
313 extern void gc (struct database_dyn
*db
);
316 /* nscd_setup_thread.c */
317 extern int setup_thread (struct database_dyn
*db
);
320 /* Special version of TEMP_FAILURE_RETRY for functions returning error
322 #define TEMP_FAILURE_RETRY_VAL(expression) \
324 ({ long int __result; \
325 do __result = (long int) (expression); \
326 while (__result == EINTR); \