1 /* Copyright (c) 1998, 1999, 2000, 2001, 2003, 2004, 2005, 2006, 2007
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 /* Structure describing dynamic part of one database. */
61 pthread_rwlock_t lock
;
62 pthread_cond_t prune_cond
;
63 pthread_mutex_t prune_lock
;
72 const char filename
[16];
73 const char *db_filename
;
75 size_t suggested_module
;
78 unsigned long int postimeout
; /* In seconds. */
79 unsigned long int negtimeout
; /* In seconds. */
81 int wr_fd
; /* Writable file descriptor. */
82 int ro_fd
; /* Unwritable file descriptor. */
84 const struct iovec
*disabled_iov
;
86 struct database_pers_head
*head
;
89 pthread_mutex_t memlock
;
91 bool last_alloc_failed
;
95 /* Paths of the file for the persistent storage. */
96 #define _PATH_NSCD_PASSWD_DB "/var/db/nscd/passwd"
97 #define _PATH_NSCD_GROUP_DB "/var/db/nscd/group"
98 #define _PATH_NSCD_HOSTS_DB "/var/db/nscd/hosts"
99 #define _PATH_NSCD_SERVICES_DB "/var/db/nscd/services"
101 /* Path used when not using persistent storage. */
102 #define _PATH_NSCD_XYZ_DB_TMP "/var/run/nscd/dbXXXXXX"
104 /* Maximum alignment requirement we will encounter. */
105 #define BLOCK_ALIGN_LOG 3
106 #define BLOCK_ALIGN (1 << BLOCK_ALIGN_LOG)
107 #define BLOCK_ALIGN_M1 (BLOCK_ALIGN - 1)
109 /* Default value for the maximum size of the database files. */
110 #define DEFAULT_MAX_DB_SIZE (32 * 1024 * 1024)
112 /* Number of bytes of data we initially reserve for each hash table bucket. */
113 #define DEFAULT_DATASIZE_PER_BUCKET 1024
116 /* Number of seconds between two cache pruning runs if we do not have
117 better information when it is really needed. */
118 #define CACHE_PRUNE_INTERVAL 15
121 /* Global variables. */
122 extern struct database_dyn dbs
[lastdb
];
123 extern const char *const dbnames
[lastdb
];
124 extern const char *const serv2str
[LASTREQ
];
126 extern const struct iovec pwd_iov_disabled
;
127 extern const struct iovec grp_iov_disabled
;
128 extern const struct iovec hst_iov_disabled
;
129 extern const struct iovec serv_iov_disabled
;
132 /* Initial number of threads to run. */
134 /* Maximum number of threads to use. */
135 extern int max_nthreads
;
137 /* User name to run server processes as. */
138 extern const char *server_user
;
140 /* Name and UID of user who is allowed to request statistics. */
141 extern const char *stat_user
;
142 extern uid_t stat_uid
;
144 /* Time the server was started. */
145 extern time_t start_time
;
147 /* Number of times clients had to wait. */
148 extern unsigned long int client_queued
;
150 /* Maximum needed alignment. */
151 extern const size_t block_align
;
153 /* Number of times a value is reloaded without being used. UINT_MAX
155 extern unsigned int reload_count
;
157 /* Pagesize minus one. */
158 extern uintptr_t pagesize_m1
;
160 /* Nonzero if paranoia mode is enabled. */
162 /* Time after which the process restarts. */
163 extern time_t restart_time
;
164 /* How much time between restarts. */
165 extern time_t restart_interval
;
166 /* Old current working directory. */
167 extern const char *oldcwd
;
168 /* Old user and group ID. */
169 extern uid_t old_uid
;
170 extern gid_t old_gid
;
173 /* Prototypes for global functions. */
176 extern void termination_handler (int signum
) __attribute__ ((__noreturn__
));
177 extern int nscd_open_socket (void);
180 extern void nscd_init (void);
181 extern void close_sockets (void);
182 extern void start_threads (void) __attribute__ ((__noreturn__
));
185 extern int nscd_parse_file (const char *fname
,
186 struct database_dyn dbs
[lastdb
]);
189 extern void send_stats (int fd
, struct database_dyn dbs
[lastdb
]);
190 extern int receive_print_stats (void) __attribute__ ((__noreturn__
));
193 extern struct datahead
*cache_search (request_type
, void *key
, size_t len
,
194 struct database_dyn
*table
,
196 extern int cache_add (int type
, const void *key
, size_t len
,
197 struct datahead
*packet
, bool first
,
198 struct database_dyn
*table
, uid_t owner
);
199 extern time_t prune_cache (struct database_dyn
*table
, time_t now
, int fd
);
202 extern void addpwbyname (struct database_dyn
*db
, int fd
, request_header
*req
,
203 void *key
, uid_t uid
);
204 extern void addpwbyuid (struct database_dyn
*db
, int fd
, request_header
*req
,
205 void *key
, uid_t uid
);
206 extern void readdpwbyname (struct database_dyn
*db
, struct hashentry
*he
,
207 struct datahead
*dh
);
208 extern void readdpwbyuid (struct database_dyn
*db
, struct hashentry
*he
,
209 struct datahead
*dh
);
212 extern void addgrbyname (struct database_dyn
*db
, int fd
, request_header
*req
,
213 void *key
, uid_t uid
);
214 extern void addgrbygid (struct database_dyn
*db
, int fd
, request_header
*req
,
215 void *key
, uid_t uid
);
216 extern void readdgrbyname (struct database_dyn
*db
, struct hashentry
*he
,
217 struct datahead
*dh
);
218 extern void readdgrbygid (struct database_dyn
*db
, struct hashentry
*he
,
219 struct datahead
*dh
);
222 extern void addhstbyname (struct database_dyn
*db
, int fd
, request_header
*req
,
223 void *key
, uid_t uid
);
224 extern void addhstbyaddr (struct database_dyn
*db
, int fd
, request_header
*req
,
225 void *key
, uid_t uid
);
226 extern void addhstbynamev6 (struct database_dyn
*db
, int fd
,
227 request_header
*req
, void *key
, uid_t uid
);
228 extern void addhstbyaddrv6 (struct database_dyn
*db
, int fd
,
229 request_header
*req
, void *key
, uid_t uid
);
230 extern void readdhstbyname (struct database_dyn
*db
, struct hashentry
*he
,
231 struct datahead
*dh
);
232 extern void readdhstbyaddr (struct database_dyn
*db
, struct hashentry
*he
,
233 struct datahead
*dh
);
234 extern void readdhstbynamev6 (struct database_dyn
*db
, struct hashentry
*he
,
235 struct datahead
*dh
);
236 extern void readdhstbyaddrv6 (struct database_dyn
*db
, struct hashentry
*he
,
237 struct datahead
*dh
);
240 extern void addhstai (struct database_dyn
*db
, int fd
, request_header
*req
,
241 void *key
, uid_t uid
);
242 extern void readdhstai (struct database_dyn
*db
, struct hashentry
*he
,
243 struct datahead
*dh
);
247 extern void addinitgroups (struct database_dyn
*db
, int fd
,
248 request_header
*req
, void *key
, uid_t uid
);
249 extern void readdinitgroups (struct database_dyn
*db
, struct hashentry
*he
,
250 struct datahead
*dh
);
253 extern void addservbyname (struct database_dyn
*db
, int fd
,
254 request_header
*req
, void *key
, uid_t uid
);
255 extern void readdservbyname (struct database_dyn
*db
, struct hashentry
*he
,
256 struct datahead
*dh
);
257 extern void addservbyport (struct database_dyn
*db
, int fd
,
258 request_header
*req
, void *key
, uid_t uid
);
259 extern void readdservbyport (struct database_dyn
*db
, struct hashentry
*he
,
260 struct datahead
*dh
);
263 extern void *mempool_alloc (struct database_dyn
*db
, size_t len
);
264 extern void gc (struct database_dyn
*db
);
267 /* nscd_setup_thread.c */
268 extern int setup_thread (struct database_dyn
*db
);
271 /* Special version of TEMP_FAILURE_RETRY for functions returning error
273 #define TEMP_FAILURE_RETRY_VAL(expression) \
275 ({ long int __result; \
276 do __result = (long int) (expression); \
277 while (__result == EINTR); \