Back to index

glibc  2.9
nscd.h
Go to the documentation of this file.
00001 /* Copyright (c) 1998, 1999, 2000, 2001, 2003, 2004, 2005, 2006, 2007, 2008
00002    Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Thorsten Kukuk <kukuk@suse.de>, 1998.
00005 
00006    The GNU C Library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Lesser General Public
00008    License as published by the Free Software Foundation; either
00009    version 2.1 of the License, or (at your option) any later version.
00010 
00011    The GNU C Library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Lesser General Public License for more details.
00015 
00016    You should have received a copy of the GNU Lesser General Public
00017    License along with the GNU C Library; if not, write to the Free
00018    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00019    02111-1307 USA.  */
00020 
00021 #ifndef _NSCD_H
00022 #define _NSCD_H      1
00023 
00024 #include <pthread.h>
00025 #include <stdbool.h>
00026 #include <time.h>
00027 #include <sys/uio.h>
00028 
00029 /* The declarations for the request and response types are in the file
00030    "nscd-client.h", which should contain everything needed by client
00031    functions.  */
00032 #include "nscd-client.h"
00033 
00034 
00035 /* Handle databases.  */
00036 typedef enum
00037 {
00038   pwddb,
00039   grpdb,
00040   hstdb,
00041   servdb,
00042   lastdb
00043 } dbtype;
00044 
00045 
00046 /* Default limit on the number of times a value gets reloaded without
00047    being used in the meantime.  NSCD does not throw a value out as
00048    soon as it times out.  It tries to reload the value from the
00049    server.  Only if the value has not been used for so many rounds it
00050    is removed.  */
00051 #define DEFAULT_RELOAD_LIMIT 5
00052 
00053 
00054 /* Time before restarting the process in paranoia mode.  */
00055 #define RESTART_INTERVAL (60 * 60)
00056 
00057 
00058 /* Stack size for worker threads.  */
00059 #define NSCD_THREAD_STACKSIZE 1024 * 1024 * (sizeof (void *) / 4)
00060 
00061 /* Maximum size of stack frames we allow the thread to use.  We use
00062    80% of the thread stack size.  */
00063 #define MAX_STACK_USE ((8 * NSCD_THREAD_STACKSIZE) / 10)
00064 
00065 
00066 /* Structure describing dynamic part of one database.  */
00067 struct database_dyn
00068 {
00069   pthread_rwlock_t lock;
00070   pthread_cond_t prune_cond;
00071   pthread_mutex_t prune_lock;
00072   time_t wakeup_time;
00073 
00074   int enabled;
00075   int check_file;
00076   int inotify_descr;
00077   int clear_cache;
00078   int persistent;
00079   int shared;
00080   int propagate;
00081   int reset_res;
00082   const char filename[16];
00083   const char *db_filename;
00084   time_t file_mtime;
00085   size_t suggested_module;
00086   size_t max_db_size;
00087 
00088   unsigned long int postimeout;    /* In seconds.  */
00089   unsigned long int negtimeout;    /* In seconds.  */
00090 
00091   int wr_fd;                /* Writable file descriptor.  */
00092   int ro_fd;                /* Unwritable file descriptor.  */
00093 
00094   const struct iovec *disabled_iov;
00095 
00096   struct database_pers_head *head;
00097   char *data;
00098   size_t memsize;
00099   pthread_mutex_t memlock;
00100   bool mmap_used;
00101   bool last_alloc_failed;
00102 };
00103 
00104 
00105 /* Paths of the file for the persistent storage.  */
00106 #define _PATH_NSCD_PASSWD_DB       "/var/db/nscd/passwd"
00107 #define _PATH_NSCD_GROUP_DB "/var/db/nscd/group"
00108 #define _PATH_NSCD_HOSTS_DB "/var/db/nscd/hosts"
00109 #define _PATH_NSCD_SERVICES_DB     "/var/db/nscd/services"
00110 
00111 /* Path used when not using persistent storage.  */
00112 #define _PATH_NSCD_XYZ_DB_TMP      "/var/run/nscd/dbXXXXXX"
00113 
00114 /* Maximum alignment requirement we will encounter.  */
00115 #define BLOCK_ALIGN_LOG 3
00116 #define BLOCK_ALIGN (1 << BLOCK_ALIGN_LOG)
00117 #define BLOCK_ALIGN_M1 (BLOCK_ALIGN - 1)
00118 
00119 /* Default value for the maximum size of the database files.  */
00120 #define DEFAULT_MAX_DB_SIZE (32 * 1024 * 1024)
00121 
00122 /* Number of bytes of data we initially reserve for each hash table bucket.  */
00123 #define DEFAULT_DATASIZE_PER_BUCKET 1024
00124 
00125 /* Default module of hash table.  */
00126 #define DEFAULT_SUGGESTED_MODULE 211
00127 
00128 
00129 /* Number of seconds between two cache pruning runs if we do not have
00130    better information when it is really needed.  */
00131 #define CACHE_PRUNE_INTERVAL       15
00132 
00133 
00134 /* Global variables.  */
00135 extern struct database_dyn dbs[lastdb] attribute_hidden;
00136 extern const char *const dbnames[lastdb];
00137 extern const char *const serv2str[LASTREQ];
00138 
00139 extern const struct iovec pwd_iov_disabled;
00140 extern const struct iovec grp_iov_disabled;
00141 extern const struct iovec hst_iov_disabled;
00142 extern const struct iovec serv_iov_disabled;
00143 
00144 
00145 /* Initial number of threads to run.  */
00146 extern int nthreads;
00147 /* Maximum number of threads to use.  */
00148 extern int max_nthreads;
00149 
00150 /* User name to run server processes as.  */
00151 extern const char *server_user;
00152 
00153 /* Name and UID of user who is allowed to request statistics.  */
00154 extern const char *stat_user;
00155 extern uid_t stat_uid;
00156 
00157 /* Time the server was started.  */
00158 extern time_t start_time;
00159 
00160 /* Number of times clients had to wait.  */
00161 extern unsigned long int client_queued;
00162 
00163 /* Maximum needed alignment.  */
00164 extern const size_t block_align;
00165 
00166 /* Number of times a value is reloaded without being used.  UINT_MAX
00167    means unlimited.  */
00168 extern unsigned int reload_count;
00169 
00170 /* Pagesize minus one.  */
00171 extern uintptr_t pagesize_m1;
00172 
00173 /* Nonzero if paranoia mode is enabled.  */
00174 extern int paranoia;
00175 /* Time after which the process restarts.  */
00176 extern time_t restart_time;
00177 /* How much time between restarts.  */
00178 extern time_t restart_interval;
00179 /* Old current working directory.  */
00180 extern const char *oldcwd;
00181 /* Old user and group ID.  */
00182 extern uid_t old_uid;
00183 extern gid_t old_gid;
00184 
00185 
00186 /* Memory allocation in flight.  Each thread can have a limited number
00187    of allocation in flight.  No need to create dynamic data
00188    structures.  We use fixed indices.  */
00189 enum in_flight
00190   {
00191     IDX_result_data = 0,
00192     /* Keep the IDX_record_data entry last at all times.  */
00193     IDX_record_data = 1,
00194     IDX_last
00195   };
00196 extern __thread struct mem_in_flight
00197 {
00198   struct
00199   {
00200     int dbidx;
00201     nscd_ssize_t blocklen;
00202     nscd_ssize_t blockoff;
00203   } block[IDX_last];
00204 
00205   struct mem_in_flight *next;
00206 } mem_in_flight attribute_tls_model_ie;
00207 /* Global list of the mem_in_flight variables of all the threads.  */
00208 extern struct mem_in_flight *mem_in_flight_list;
00209 
00210 
00211 /* Prototypes for global functions.  */
00212 
00213 /* nscd.c */
00214 extern void termination_handler (int signum) __attribute__ ((__noreturn__));
00215 extern int nscd_open_socket (void);
00216 
00217 /* connections.c */
00218 extern void nscd_init (void);
00219 extern void close_sockets (void);
00220 extern void start_threads (void) __attribute__ ((__noreturn__));
00221 
00222 /* nscd_conf.c */
00223 extern int nscd_parse_file (const char *fname,
00224                          struct database_dyn dbs[lastdb]);
00225 
00226 /* nscd_stat.c */
00227 extern void send_stats (int fd, struct database_dyn dbs[lastdb]);
00228 extern int receive_print_stats (void) __attribute__ ((__noreturn__));
00229 
00230 /* cache.c */
00231 extern struct datahead *cache_search (request_type, void *key, size_t len,
00232                                   struct database_dyn *table,
00233                                   uid_t owner);
00234 extern int cache_add (int type, const void *key, size_t len,
00235                     struct datahead *packet, bool first,
00236                     struct database_dyn *table, uid_t owner,
00237                     bool prune_wakeup);
00238 extern time_t prune_cache (struct database_dyn *table, time_t now, int fd);
00239 
00240 /* pwdcache.c */
00241 extern void addpwbyname (struct database_dyn *db, int fd, request_header *req,
00242                       void *key, uid_t uid);
00243 extern void addpwbyuid (struct database_dyn *db, int fd, request_header *req,
00244                      void *key, uid_t uid);
00245 extern void readdpwbyname (struct database_dyn *db, struct hashentry *he,
00246                         struct datahead *dh);
00247 extern void readdpwbyuid (struct database_dyn *db, struct hashentry *he,
00248                        struct datahead *dh);
00249 
00250 /* grpcache.c */
00251 extern void addgrbyname (struct database_dyn *db, int fd, request_header *req,
00252                       void *key, uid_t uid);
00253 extern void addgrbygid (struct database_dyn *db, int fd, request_header *req,
00254                      void *key, uid_t uid);
00255 extern void readdgrbyname (struct database_dyn *db, struct hashentry *he,
00256                         struct datahead *dh);
00257 extern void readdgrbygid (struct database_dyn *db, struct hashentry *he,
00258                        struct datahead *dh);
00259 
00260 /* hstcache.c */
00261 extern void addhstbyname (struct database_dyn *db, int fd, request_header *req,
00262                        void *key, uid_t uid);
00263 extern void addhstbyaddr (struct database_dyn *db, int fd, request_header *req,
00264                        void *key, uid_t uid);
00265 extern void addhstbynamev6 (struct database_dyn *db, int fd,
00266                          request_header *req, void *key, uid_t uid);
00267 extern void addhstbyaddrv6 (struct database_dyn *db, int fd,
00268                          request_header *req, void *key, uid_t uid);
00269 extern void readdhstbyname (struct database_dyn *db, struct hashentry *he,
00270                          struct datahead *dh);
00271 extern void readdhstbyaddr (struct database_dyn *db, struct hashentry *he,
00272                          struct datahead *dh);
00273 extern void readdhstbynamev6 (struct database_dyn *db, struct hashentry *he,
00274                            struct datahead *dh);
00275 extern void readdhstbyaddrv6 (struct database_dyn *db, struct hashentry *he,
00276                            struct datahead *dh);
00277 
00278 /* aicache.c */
00279 extern void addhstai (struct database_dyn *db, int fd, request_header *req,
00280                     void *key, uid_t uid);
00281 extern void readdhstai (struct database_dyn *db, struct hashentry *he,
00282                      struct datahead *dh);
00283 
00284 
00285 /* initgrcache.c */
00286 extern void addinitgroups (struct database_dyn *db, int fd,
00287                         request_header *req, void *key, uid_t uid);
00288 extern void readdinitgroups (struct database_dyn *db, struct hashentry *he,
00289                           struct datahead *dh);
00290 
00291 /* servicecache.c */
00292 extern void addservbyname (struct database_dyn *db, int fd,
00293                         request_header *req, void *key, uid_t uid);
00294 extern void readdservbyname (struct database_dyn *db, struct hashentry *he,
00295                           struct datahead *dh);
00296 extern void addservbyport (struct database_dyn *db, int fd,
00297                         request_header *req, void *key, uid_t uid);
00298 extern void readdservbyport (struct database_dyn *db, struct hashentry *he,
00299                           struct datahead *dh);
00300 
00301 /* mem.c */
00302 extern void *mempool_alloc (struct database_dyn *db, size_t len,
00303                          enum in_flight idx);
00304 extern void gc (struct database_dyn *db);
00305 
00306 
00307 /* nscd_setup_thread.c */
00308 extern int setup_thread (struct database_dyn *db);
00309 
00310 
00311 /* Special version of TEMP_FAILURE_RETRY for functions returning error
00312    values.  */
00313 #define TEMP_FAILURE_RETRY_VAL(expression) \
00314   (__extension__                                                     \
00315     ({ long int __result;                                            \
00316        do __result = (long int) (expression);                               \
00317        while (__result == EINTR);                                    \
00318        __result; }))
00319 
00320 #endif /* nscd.h */