Back to index

glibc  2.9
nscd-client.h
Go to the documentation of this file.
00001 /* Copyright (c) 1998, 1999, 2000, 2003, 2004, 2005, 2006, 2007
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 /* This file defines everything that client code should need to
00022    know to talk to the nscd daemon.  */
00023 
00024 #ifndef _NSCD_CLIENT_H
00025 #define _NSCD_CLIENT_H      1
00026 
00027 #include <stdbool.h>
00028 #include <stdint.h>
00029 #include <sys/types.h>
00030 #include <atomic.h>
00031 #include <nscd-types.h>
00032 #include <sys/uio.h>
00033 
00034 
00035 /* Version number of the daemon interface */
00036 #define NSCD_VERSION 2
00037 
00038 /* Path of the file where the PID of the running system is stored.  */
00039 #define _PATH_NSCDPID        "/var/run/nscd/nscd.pid"
00040 
00041 /* Path for the Unix domain socket.  */
00042 #define _PATH_NSCDSOCKET "/var/run/nscd/socket"
00043 
00044 /* Path for the configuration file.  */
00045 #define _PATH_NSCDCONF       "/etc/nscd.conf"
00046 
00047 /* Maximu allowed length for the key.  */
00048 #define MAXKEYLEN 1024
00049 
00050 
00051 /* Available services.  */
00052 typedef enum
00053 {
00054   GETPWBYNAME,
00055   GETPWBYUID,
00056   GETGRBYNAME,
00057   GETGRBYGID,
00058   GETHOSTBYNAME,
00059   GETHOSTBYNAMEv6,
00060   GETHOSTBYADDR,
00061   GETHOSTBYADDRv6,
00062   SHUTDOWN,          /* Shut the server down.  */
00063   GETSTAT,           /* Get the server statistic.  */
00064   INVALIDATE,           /* Invalidate one special cache.  */
00065   GETFDPW,
00066   GETFDGR,
00067   GETFDHST,
00068   GETAI,
00069   INITGROUPS,
00070   GETSERVBYNAME,
00071   GETSERVBYPORT,
00072   GETFDSERV,
00073   LASTREQ
00074 } request_type;
00075 
00076 
00077 /* Header common to all requests */
00078 typedef struct
00079 {
00080   int32_t version;   /* Version number of the daemon interface.  */
00081   request_type type; /* Service requested.  */
00082   int32_t key_len;   /* Key length.  */
00083 } request_header;
00084 
00085 
00086 /* Structure sent in reply to password query.  Note that this struct is
00087    sent also if the service is disabled or there is no record found.  */
00088 typedef struct
00089 {
00090   int32_t version;
00091   int32_t found;
00092   nscd_ssize_t pw_name_len;
00093   nscd_ssize_t pw_passwd_len;
00094   uid_t pw_uid;
00095   gid_t pw_gid;
00096   nscd_ssize_t pw_gecos_len;
00097   nscd_ssize_t pw_dir_len;
00098   nscd_ssize_t pw_shell_len;
00099 } pw_response_header;
00100 
00101 
00102 /* Structure sent in reply to group query.  Note that this struct is
00103    sent also if the service is disabled or there is no record found.  */
00104 typedef struct
00105 {
00106   int32_t version;
00107   int32_t found;
00108   nscd_ssize_t gr_name_len;
00109   nscd_ssize_t gr_passwd_len;
00110   gid_t gr_gid;
00111   nscd_ssize_t gr_mem_cnt;
00112 } gr_response_header;
00113 
00114 
00115 /* Structure sent in reply to host query.  Note that this struct is
00116    sent also if the service is disabled or there is no record found.  */
00117 typedef struct
00118 {
00119   int32_t version;
00120   int32_t found;
00121   nscd_ssize_t h_name_len;
00122   nscd_ssize_t h_aliases_cnt;
00123   int32_t h_addrtype;
00124   int32_t h_length;
00125   nscd_ssize_t h_addr_list_cnt;
00126   int32_t error;
00127 } hst_response_header;
00128 
00129 
00130 /* Structure sent in reply to addrinfo query.  Note that this struct is
00131    sent also if the service is disabled or there is no record found.  */
00132 typedef struct
00133 {
00134   int32_t version;
00135   int32_t found;
00136   nscd_ssize_t naddrs;
00137   nscd_ssize_t addrslen;
00138   nscd_ssize_t canonlen;
00139   int32_t error;
00140 } ai_response_header;
00141 
00142 /* Structure filled in by __nscd_getai.  */
00143 struct nscd_ai_result
00144 {
00145   int naddrs;
00146   char *canon;
00147   uint8_t *family;
00148   char *addrs;
00149 };
00150 
00151 /* Structure sent in reply to initgroups query.  Note that this struct is
00152    sent also if the service is disabled or there is no record found.  */
00153 typedef struct
00154 {
00155   int32_t version;
00156   int32_t found;
00157   nscd_ssize_t ngrps;
00158 } initgr_response_header;
00159 
00160 
00161 /* Structure sent in reply to services query.  Note that this struct is
00162    sent also if the service is disabled or there is no record found.  */
00163 typedef struct
00164 {
00165   int32_t version;
00166   int32_t found;
00167   nscd_ssize_t s_name_len;
00168   nscd_ssize_t s_proto_len;
00169   nscd_ssize_t s_aliases_cnt;
00170   int32_t s_port;
00171 } serv_response_header;
00172 
00173 
00174 /* Type for offsets in data part of database.  */
00175 typedef uint32_t ref_t;
00176 /* Value for invalid/no reference.  */
00177 #define ENDREF       UINT32_MAX
00178 
00179 /* Timestamp type.  */
00180 typedef uint64_t nscd_time_t;
00181 
00182 /* Alignment requirement of the beginning of the data region.  */
00183 #define ALIGN 16
00184 
00185 
00186 /* Head of record in data part of database.  */
00187 struct datahead
00188 {
00189   nscd_ssize_t allocsize;   /* Allocated Bytes.  */
00190   nscd_ssize_t recsize;            /* Size of the record.  */
00191   nscd_time_t timeout;             /* Time when this entry becomes invalid.  */
00192   uint8_t notfound;         /* Nonzero if data has not been found.  */
00193   uint8_t nreloads;         /* Reloads without use.  */
00194   uint8_t usable;           /* False if the entry must be ignored.  */
00195   uint64_t :40;                    /* Alignment.  */
00196 
00197   /* We need to have the following element aligned for the response
00198      header data types and their use in the 'struct dataset' types
00199      defined in the XXXcache.c files.  */
00200   union
00201   {
00202     pw_response_header pwdata;
00203     gr_response_header grdata;
00204     hst_response_header hstdata;
00205     ai_response_header aidata;
00206     initgr_response_header initgrdata;
00207     serv_response_header servdata;
00208     nscd_ssize_t align1;
00209     nscd_time_t align2;
00210   } data[0];
00211 };
00212 
00213 
00214 /* Structure for one hash table entry.  */
00215 struct hashentry
00216 {
00217   request_type type:8;             /* Which type of dataset.  */
00218   bool first;               /* True if this was the original key.  */
00219   nscd_ssize_t len;         /* Length of key.  */
00220   ref_t key;                /* Pointer to key.  */
00221   int32_t owner;            /* If secure table, this is the owner.  */
00222   ref_t next;               /* Next entry in this hash bucket list.  */
00223   ref_t packet;                    /* Records for the result.  */
00224   union
00225   {
00226     struct hashentry *dellist;     /* Next record to be deleted.  This can be a
00227                                pointer since only nscd uses this field.  */
00228     ref_t *prevp;           /* Pointer to field containing forward
00229                                reference.  */
00230   };
00231 };
00232 
00233 
00234 /* Current persistent database version.  */
00235 #define DB_VERSION   1
00236 
00237 /* Maximum time allowed between updates of the timestamp.  */
00238 #define MAPPING_TIMEOUT (5 * 60)
00239 
00240 
00241 /* Header of persistent database file.  */
00242 struct database_pers_head
00243 {
00244   int32_t version;
00245   int32_t header_size;
00246   volatile int32_t gc_cycle;
00247   volatile int32_t nscd_certainly_running;
00248   volatile nscd_time_t timestamp;
00249 
00250   nscd_ssize_t module;
00251   nscd_ssize_t data_size;
00252 
00253   nscd_ssize_t first_free;  /* Offset of first free byte in data area.  */
00254 
00255   nscd_ssize_t nentries;
00256   nscd_ssize_t maxnentries;
00257   nscd_ssize_t maxnsearched;
00258 
00259   uint64_t poshit;
00260   uint64_t neghit;
00261   uint64_t posmiss;
00262   uint64_t negmiss;
00263 
00264   uint64_t rdlockdelayed;
00265   uint64_t wrlockdelayed;
00266 
00267   uint64_t addfailed;
00268 
00269   ref_t array[0];
00270 };
00271 
00272 
00273 /* Mapped database record.  */
00274 struct mapped_database
00275 {
00276   const struct database_pers_head *head;
00277   const char *data;
00278   size_t mapsize;
00279   int counter;              /* > 0 indicates it is usable.  */
00280   size_t datasize;
00281 };
00282 #define NO_MAPPING ((struct mapped_database *) -1l)
00283 
00284 struct locked_map_ptr
00285 {
00286   int lock;
00287   struct mapped_database *mapped;
00288 };
00289 #define libc_locked_map_ptr(class, name) class struct locked_map_ptr name
00290 
00291 
00292 /* Open socket connection to nscd server.  */
00293 extern int __nscd_open_socket (const char *key, size_t keylen,
00294                             request_type type, void *response,
00295                             size_t responselen) attribute_hidden;
00296 
00297 /* Get reference of mapping.  */
00298 extern struct mapped_database *__nscd_get_map_ref (request_type type,
00299                                              const char *name,
00300                                              volatile struct locked_map_ptr *mapptr,
00301                                              int *gc_cyclep);
00302 
00303 /* Unmap database.  */
00304 extern void __nscd_unmap (struct mapped_database *mapped);
00305 
00306 /* Drop reference of mapping.  */
00307 static inline int __nscd_drop_map_ref (struct mapped_database *map,
00308                                    int *gc_cycle)
00309 {
00310   if (map != NO_MAPPING)
00311     {
00312       int now_cycle = map->head->gc_cycle;
00313       if (__builtin_expect (now_cycle != *gc_cycle, 0))
00314        {
00315          /* We might have read inconsistent data.  */
00316          *gc_cycle = now_cycle;
00317          return -1;
00318        }
00319 
00320       if (atomic_decrement_val (&map->counter) == 0)
00321        __nscd_unmap (map);
00322     }
00323 
00324   return 0;
00325 }
00326 
00327 
00328 /* Search the mapped database.  */
00329 extern struct datahead *__nscd_cache_search (request_type type,
00330                                         const char *key,
00331                                         size_t keylen,
00332                                         const struct mapped_database *mapped);
00333 
00334 /* Wrappers around read, readv and write that only read/write less than LEN
00335    bytes on error or EOF.  */
00336 extern ssize_t __readall (int fd, void *buf, size_t len)
00337   attribute_hidden;
00338 extern ssize_t __readvall (int fd, const struct iovec *iov, int iovcnt)
00339   attribute_hidden;
00340 extern ssize_t writeall (int fd, const void *buf, size_t len)
00341   attribute_hidden;
00342 extern ssize_t sendfileall (int tofd, int fromfd, off_t off, size_t len)
00343   attribute_hidden;
00344 
00345 #endif /* nscd.h */