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
29 /* The declarations for the request and response types are in the file
30 "nscd-client.h", which should contain everything needed by client
32 #include "nscd-client.h"
35 /* 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 /* Structure describing dynamic part of one database. */
69 pthread_rwlock_t lock
;
70 pthread_cond_t prune_cond
;
71 pthread_mutex_t prune_lock
;
72 pthread_mutex_t prune_run_lock
;
83 const char filename
[16];
84 const char *db_filename
;
86 size_t suggested_module
;
89 unsigned long int postimeout
; /* In seconds. */
90 unsigned long int negtimeout
; /* In seconds. */
92 int wr_fd
; /* Writable file descriptor. */
93 int ro_fd
; /* Unwritable file descriptor. */
95 const struct iovec
*disabled_iov
;
97 struct database_pers_head
*head
;
100 pthread_mutex_t memlock
;
102 bool last_alloc_failed
;
106 /* Paths of the file for the persistent storage. */
107 #define _PATH_NSCD_PASSWD_DB "/var/db/nscd/passwd"
108 #define _PATH_NSCD_GROUP_DB "/var/db/nscd/group"
109 #define _PATH_NSCD_HOSTS_DB "/var/db/nscd/hosts"
110 #define _PATH_NSCD_SERVICES_DB "/var/db/nscd/services"
112 /* Path used when not using persistent storage. */
113 #define _PATH_NSCD_XYZ_DB_TMP "/var/run/nscd/dbXXXXXX"
115 /* Maximum alignment requirement we will encounter. */
116 #define BLOCK_ALIGN_LOG 3
117 #define BLOCK_ALIGN (1 << BLOCK_ALIGN_LOG)
118 #define BLOCK_ALIGN_M1 (BLOCK_ALIGN - 1)
120 /* Default value for the maximum size of the database files. */
121 #define DEFAULT_MAX_DB_SIZE (32 * 1024 * 1024)
123 /* Number of bytes of data we initially reserve for each hash table bucket. */
124 #define DEFAULT_DATASIZE_PER_BUCKET 1024
126 /* Default module of hash table. */
127 #define DEFAULT_SUGGESTED_MODULE 211
130 /* Number of seconds between two cache pruning runs if we do not have
131 better information when it is really needed. */
132 #define CACHE_PRUNE_INTERVAL 15
135 /* Global variables. */
136 extern struct database_dyn dbs
[lastdb
] attribute_hidden
;
137 extern const char *const dbnames
[lastdb
];
138 extern const char *const serv2str
[LASTREQ
];
140 extern const struct iovec pwd_iov_disabled
;
141 extern const struct iovec grp_iov_disabled
;
142 extern const struct iovec hst_iov_disabled
;
143 extern const struct iovec serv_iov_disabled
;
146 /* Initial number of threads to run. */
148 /* Maximum number of threads to use. */
149 extern int max_nthreads
;
151 /* User name to run server processes as. */
152 extern const char *server_user
;
154 /* Name and UID of user who is allowed to request statistics. */
155 extern const char *stat_user
;
156 extern uid_t stat_uid
;
158 /* Time the server was started. */
159 extern time_t start_time
;
161 /* Number of times clients had to wait. */
162 extern unsigned long int client_queued
;
164 /* Maximum needed alignment. */
165 extern const size_t block_align
;
167 /* Number of times a value is reloaded without being used. UINT_MAX
169 extern unsigned int reload_count
;
171 /* Pagesize minus one. */
172 extern uintptr_t pagesize_m1
;
174 /* Nonzero if paranoia mode is enabled. */
176 /* Time after which the process restarts. */
177 extern time_t restart_time
;
178 /* How much time between restarts. */
179 extern time_t restart_interval
;
180 /* Old current working directory. */
181 extern const char *oldcwd
;
182 /* Old user and group ID. */
183 extern uid_t old_uid
;
184 extern gid_t old_gid
;
187 /* Memory allocation in flight. Each thread can have a limited number
188 of allocation in flight. No need to create dynamic data
189 structures. We use fixed indices. */
193 /* Keep the IDX_record_data entry last at all times. */
197 extern __thread
struct mem_in_flight
202 nscd_ssize_t blocklen
;
203 nscd_ssize_t blockoff
;
206 struct mem_in_flight
*next
;
207 } mem_in_flight attribute_tls_model_ie
;
208 /* Global list of the mem_in_flight variables of all the threads. */
209 extern struct mem_in_flight
*mem_in_flight_list
;
212 /* Prototypes for global functions. */
215 extern void termination_handler (int signum
) __attribute__ ((__noreturn__
));
216 extern int nscd_open_socket (void);
219 extern void nscd_init (void);
220 extern void close_sockets (void);
221 extern void start_threads (void) __attribute__ ((__noreturn__
));
224 extern int nscd_parse_file (const char *fname
,
225 struct database_dyn dbs
[lastdb
]);
228 extern void send_stats (int fd
, struct database_dyn dbs
[lastdb
]);
229 extern int receive_print_stats (void) __attribute__ ((__noreturn__
));
232 extern struct datahead
*cache_search (request_type
, void *key
, size_t len
,
233 struct database_dyn
*table
,
235 extern int cache_add (int type
, const void *key
, size_t len
,
236 struct datahead
*packet
, bool first
,
237 struct database_dyn
*table
, uid_t owner
,
239 extern time_t prune_cache (struct database_dyn
*table
, time_t now
, int fd
);
242 extern void addpwbyname (struct database_dyn
*db
, int fd
, request_header
*req
,
243 void *key
, uid_t uid
);
244 extern void addpwbyuid (struct database_dyn
*db
, int fd
, request_header
*req
,
245 void *key
, uid_t uid
);
246 extern void readdpwbyname (struct database_dyn
*db
, struct hashentry
*he
,
247 struct datahead
*dh
);
248 extern void readdpwbyuid (struct database_dyn
*db
, struct hashentry
*he
,
249 struct datahead
*dh
);
252 extern void addgrbyname (struct database_dyn
*db
, int fd
, request_header
*req
,
253 void *key
, uid_t uid
);
254 extern void addgrbygid (struct database_dyn
*db
, int fd
, request_header
*req
,
255 void *key
, uid_t uid
);
256 extern void readdgrbyname (struct database_dyn
*db
, struct hashentry
*he
,
257 struct datahead
*dh
);
258 extern void readdgrbygid (struct database_dyn
*db
, struct hashentry
*he
,
259 struct datahead
*dh
);
262 extern void addhstbyname (struct database_dyn
*db
, int fd
, request_header
*req
,
263 void *key
, uid_t uid
);
264 extern void addhstbyaddr (struct database_dyn
*db
, int fd
, request_header
*req
,
265 void *key
, uid_t uid
);
266 extern void addhstbynamev6 (struct database_dyn
*db
, int fd
,
267 request_header
*req
, void *key
, uid_t uid
);
268 extern void addhstbyaddrv6 (struct database_dyn
*db
, int fd
,
269 request_header
*req
, void *key
, uid_t uid
);
270 extern void readdhstbyname (struct database_dyn
*db
, struct hashentry
*he
,
271 struct datahead
*dh
);
272 extern void readdhstbyaddr (struct database_dyn
*db
, struct hashentry
*he
,
273 struct datahead
*dh
);
274 extern void readdhstbynamev6 (struct database_dyn
*db
, struct hashentry
*he
,
275 struct datahead
*dh
);
276 extern void readdhstbyaddrv6 (struct database_dyn
*db
, struct hashentry
*he
,
277 struct datahead
*dh
);
280 extern void addhstai (struct database_dyn
*db
, int fd
, request_header
*req
,
281 void *key
, uid_t uid
);
282 extern void readdhstai (struct database_dyn
*db
, struct hashentry
*he
,
283 struct datahead
*dh
);
287 extern void addinitgroups (struct database_dyn
*db
, int fd
,
288 request_header
*req
, void *key
, uid_t uid
);
289 extern void readdinitgroups (struct database_dyn
*db
, struct hashentry
*he
,
290 struct datahead
*dh
);
293 extern void addservbyname (struct database_dyn
*db
, int fd
,
294 request_header
*req
, void *key
, uid_t uid
);
295 extern void readdservbyname (struct database_dyn
*db
, struct hashentry
*he
,
296 struct datahead
*dh
);
297 extern void addservbyport (struct database_dyn
*db
, int fd
,
298 request_header
*req
, void *key
, uid_t uid
);
299 extern void readdservbyport (struct database_dyn
*db
, struct hashentry
*he
,
300 struct datahead
*dh
);
303 extern void *mempool_alloc (struct database_dyn
*db
, size_t len
,
305 extern void gc (struct database_dyn
*db
);
308 /* nscd_setup_thread.c */
309 extern int setup_thread (struct database_dyn
*db
);
312 /* Special version of TEMP_FAILURE_RETRY for functions returning error
314 #define TEMP_FAILURE_RETRY_VAL(expression) \
316 ({ long int __result; \
317 do __result = (long int) (expression); \
318 while (__result == EINTR); \