Update.
[glibc.git] / nscd / grpcache.c
blob2806ddd52e1cf737a9b462b1a094b470bed05592
1 /* Copyright (c) 1998 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.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 Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 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 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 #include <errno.h>
21 #include <grp.h>
22 #include <pthread.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <rpcsvc/nis.h>
27 #include <sys/types.h>
29 #include "dbg_log.h"
30 #include "nscd.h"
32 static unsigned long modulo = 211;
33 static unsigned long postimeout = 3600;
34 static unsigned long negtimeout = 60;
36 static unsigned long poshit = 0;
37 static unsigned long posmiss = 0;
38 static unsigned long neghit = 0;
39 static unsigned long negmiss = 0;
41 struct grphash
43 time_t create;
44 struct grphash *next;
45 struct group *grp;
47 typedef struct grphash grphash;
49 struct gidhash
51 struct gidhash *next;
52 struct group *grptr;
54 typedef struct gidhash gidhash;
56 struct neghash
58 time_t create;
59 struct neghash *next;
60 char *key;
62 typedef struct neghash neghash;
64 static grphash *grptbl;
65 static gidhash *gidtbl;
66 static neghash *negtbl;
68 static pthread_rwlock_t grplock = PTHREAD_RWLOCK_INITIALIZER;
69 static pthread_rwlock_t neglock = PTHREAD_RWLOCK_INITIALIZER;
71 static void *grptable_update (void *);
72 static void *negtable_update (void *);
74 void
75 get_gr_stat (stat_response_header *stat)
77 stat->gr_poshit = poshit;
78 stat->gr_posmiss = posmiss;
79 stat->gr_neghit = neghit;
80 stat->gr_negmiss = negmiss;
81 stat->gr_size = modulo;
82 stat->gr_posttl = postimeout;
83 stat->gr_negttl = negtimeout;
86 void
87 set_grp_modulo (unsigned long mod)
89 modulo = mod;
92 void
93 set_pos_grp_ttl (unsigned long ttl)
95 postimeout = ttl;
98 void
99 set_neg_grp_ttl (unsigned long ttl)
101 negtimeout = ttl;
105 cache_grpinit ()
107 pthread_attr_t attr;
108 pthread_t thread;
110 grptbl = calloc (modulo, sizeof (grphash));
111 if (grptbl == NULL)
112 return -1;
113 gidtbl = calloc (modulo, sizeof (grphash));
114 if (gidtbl == NULL)
115 return -1;
116 negtbl = calloc (modulo, sizeof (neghash));
117 if (negtbl == NULL)
118 return -1;
120 pthread_attr_init (&attr);
121 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
123 pthread_create (&thread, NULL, grptable_update, &attr);
124 pthread_create (&thread, NULL, negtable_update, &attr);
126 pthread_attr_destroy (&attr);
128 return 0;
131 static struct group *
132 save_grp (struct group *src)
134 struct group *dest;
135 unsigned long int l;
137 dest = calloc (1, sizeof (struct group));
138 dest->gr_name = strdup (src->gr_name);
139 dest->gr_passwd = strdup (src->gr_passwd);
140 dest->gr_gid = src->gr_gid;
142 /* How many members does this group have? */
143 l = 0;
144 while (src->gr_mem[l])
145 ++l;
147 dest->gr_mem = calloc (1, sizeof (char *) * (l+1));
148 l = 0;
149 while (src->gr_mem[l])
151 dest->gr_mem[l] = strdup (src->gr_mem[l]);
152 ++l;
155 return dest;
158 static void
159 free_grp (struct group *src)
161 unsigned long int l;
163 free (src->gr_name);
164 free (src->gr_passwd);
166 l = 0;
167 while (src->gr_mem[l])
169 free (src->gr_mem[l]);
170 ++l;
172 free (src->gr_mem);
173 free (src);
176 static int
177 add_cache (struct group *grp)
179 grphash *work;
180 gidhash *gidwork;
181 unsigned long int hash = __nis_hash (grp->gr_name,
182 strlen (grp->gr_name)) % modulo;
184 if (debug_flag)
185 dbg_log (_("grp_add_cache (%s)"), grp->gr_name);
187 work = &grptbl[hash];
189 if (grptbl[hash].grp == NULL)
190 grptbl[hash].grp = save_grp (grp);
191 else
193 while (work->next != NULL)
194 work = work->next;
196 work->next = calloc (1, sizeof (grphash));
197 work->next->grp = save_grp (grp);
198 work = work->next;
201 time (&work->create);
202 gidwork = &gidtbl[grp->gr_gid % modulo];
203 if (gidwork->grptr == NULL)
204 gidwork->grptr = work->grp;
205 else
207 while (gidwork->next != NULL)
208 gidwork = gidwork->next;
210 gidwork->next = calloc (1, sizeof (gidhash));
211 gidwork->next->grptr = work->grp;
214 return 0;
217 static struct group *
218 cache_search_name (const char *name)
220 grphash *work;
221 unsigned long int hash = __nis_hash (name, strlen(name)) % modulo;
223 work = &grptbl[hash];
225 while (work->grp != NULL)
227 if (strcmp (work->grp->gr_name, name) == 0)
228 return work->grp;
229 if (work->next != NULL)
230 work = work->next;
231 else
232 return NULL;
234 return NULL;
237 static struct group *
238 cache_search_gid (gid_t gid)
240 gidhash *work;
242 work = &gidtbl[gid % modulo];
244 while (work->grptr != NULL)
246 if (work->grptr->gr_gid == gid)
247 return work->grptr;
248 if (work->next != NULL)
249 work = work->next;
250 else
251 return NULL;
253 return NULL;
256 static int
257 add_negcache (char *key)
259 neghash *work;
260 unsigned long int hash = __nis_hash (key, strlen (key)) % modulo;
262 if (debug_flag)
263 dbg_log (_("grp_add_netgache (%s|%ld)"), key, hash);
265 work = &negtbl[hash];
267 if (negtbl[hash].key == NULL)
269 negtbl[hash].key = strdup (key);
270 negtbl[hash].next = NULL;
272 else
274 while (work->next != NULL)
275 work = work->next;
277 work->next = calloc (1, sizeof (neghash));
278 work->next->key = strdup (key);
279 work = work->next;
282 time (&work->create);
283 return 0;
286 static int
287 cache_search_neg (const char *key)
289 neghash *work;
290 unsigned long int hash = __nis_hash (key, strlen (key)) % modulo;
292 if (debug_flag)
293 dbg_log (_("grp_cache_search_neg (%s|%ld)"), key, hash);
295 work = &negtbl[hash];
297 while (work->key != NULL)
299 if (strcmp (work->key, key) == 0)
300 return 1;
301 if (work->next != NULL)
302 work = work->next;
303 else
304 return 0;
306 return 0;
309 void *
310 cache_getgrnam (void *v_param)
312 param_t *param = (param_t *)v_param;
313 struct group *grp;
315 pthread_rwlock_rdlock (&grplock);
316 grp = cache_search_name (param->key);
318 /* I don't like it to hold the read only lock longer, but it is
319 necessary to avoid to much malloc/free/strcpy. */
321 if (grp != NULL)
323 if (debug_flag)
324 dbg_log (_("Found \"%s\" in cache !"), param->key);
326 ++poshit;
327 gr_send_answer (param->conn, grp);
328 close_socket (param->conn);
330 pthread_rwlock_unlock (&grplock);
332 else
334 int status;
335 int buflen = 1024;
336 char *buffer = calloc (1, buflen);
337 struct group resultbuf;
339 if (debug_flag)
340 dbg_log (_("Doesn't found \"%s\" in cache !"), param->key);
342 pthread_rwlock_unlock (&grplock);
344 pthread_rwlock_rdlock (&neglock);
345 status = cache_search_neg (param->key);
346 pthread_rwlock_unlock (&neglock);
348 if (status == 0)
350 while (buffer != NULL
351 && (getgrnam_r (param->key, &resultbuf, buffer, buflen, &grp)
352 != 0)
353 && errno == ERANGE)
355 errno = 0;
356 buflen += 1024;
357 buffer = realloc (buffer, buflen);
360 if (buffer != NULL && grp != NULL)
362 struct group *tmp;
364 ++poshit;
365 pthread_rwlock_wrlock (&grplock);
366 /* While we are waiting on the lock, somebody else could
367 add this entry. */
368 tmp = cache_search_name (param->key);
369 if (tmp == NULL)
370 add_cache (grp);
371 pthread_rwlock_unlock (&grplock);
373 else
375 pthread_rwlock_wrlock (&neglock);
376 add_negcache (param->key);
377 ++negmiss;
378 pthread_rwlock_unlock (&neglock);
381 else
382 ++neghit;
384 gr_send_answer (param->conn, grp);
385 close_socket (param->conn);
386 if (buffer != NULL)
387 free (buffer);
389 free (param->key);
390 free (param);
391 return NULL;
394 void *
395 cache_gr_disabled (void *v_param)
397 param_t *param = (param_t *)v_param;
399 if (debug_flag)
400 dbg_log (_("\tgroup cache is disabled\n"));
402 gr_send_disabled (param->conn);
403 return NULL;
406 void *
407 cache_getgrgid (void *v_param)
409 param_t *param = (param_t *)v_param;
410 struct group *grp, resultbuf;
411 gid_t gid = strtol (param->key, NULL, 10);
413 pthread_rwlock_rdlock (&grplock);
414 grp = cache_search_gid (gid);
416 /* I don't like it to hold the read only lock longer, but it is
417 necessary to avoid to much malloc/free/strcpy. */
419 if (grp != NULL)
421 if (debug_flag)
422 dbg_log (_("Found \"%d\" in cache !"), gid);
424 ++poshit;
425 gr_send_answer (param->conn, grp);
426 close_socket (param->conn);
428 pthread_rwlock_unlock (&grplock);
430 else
432 int buflen = 1024;
433 char *buffer = malloc (buflen);
434 int status;
436 if (debug_flag)
437 dbg_log (_("Doesn't found \"%d\" in cache !"), gid);
439 pthread_rwlock_unlock (&grplock);
441 pthread_rwlock_rdlock (&neglock);
442 status = cache_search_neg (param->key);
443 pthread_rwlock_unlock (&neglock);
445 if (status == 0)
447 while (buffer != NULL
448 && (getgrgid_r (gid, &resultbuf, buffer, buflen, &grp) != 0)
449 && errno == ERANGE)
451 errno = 0;
452 buflen += 1024;
453 buffer = realloc (buffer, buflen);
456 if (buffer != NULL && grp != NULL)
458 struct group *tmp;
460 ++posmiss;
461 pthread_rwlock_wrlock (&grplock);
462 /* While we are waiting on the lock, somebody else could
463 add this entry. */
464 tmp = cache_search_gid (gid);
465 if (tmp == NULL)
466 add_cache (grp);
467 pthread_rwlock_unlock (&grplock);
469 else
471 ++negmiss;
472 pthread_rwlock_wrlock (&neglock);
473 add_negcache (param->key);
474 pthread_rwlock_unlock (&neglock);
477 else
478 ++neghit;
480 gr_send_answer (param->conn, grp);
481 close_socket (param->conn);
482 if (buffer != NULL)
483 free (buffer);
485 free (param->key);
486 free (param);
487 return NULL;
490 static void *
491 grptable_update (void *v)
493 time_t now;
494 int i;
496 sleep (20);
498 while (!do_shutdown)
500 if (debug_flag > 2)
501 dbg_log (_("(grptable_update) Wait for write lock!"));
503 pthread_rwlock_wrlock (&grplock);
505 if (debug_flag > 2)
506 dbg_log (_("(grptable_update) Have write lock"));
508 time (&now);
509 for (i = 0; i < modulo; ++i)
511 grphash *work = &grptbl[i];
513 while (work && work->grp)
515 if ((now - work->create) >= postimeout)
517 gidhash *uh = &gidtbl[work->grp->gr_gid % modulo];
519 if (debug_flag)
520 dbg_log (_("Give \"%s\" free"), work->grp->gr_name);
522 while (uh && uh->grptr)
524 if (uh->grptr->gr_gid == work->grp->gr_gid)
526 if (debug_flag > 3)
527 dbg_log (_("Give gid for \"%s\" free"),
528 work->grp->gr_name);
529 if (uh->next != NULL)
531 gidhash *tmp = uh->next;
532 uh->grptr = tmp->grptr;
533 uh->next = tmp->next;
534 free (tmp);
536 else
537 uh->grptr = NULL;
539 uh = uh->next;
542 free_grp (work->grp);
543 if (work->next != NULL)
545 grphash *tmp = work->next;
546 work->create = tmp->create;
547 work->next = tmp->next;
548 work->grp = tmp->grp;
549 free (tmp);
551 else
552 work->grp = NULL;
554 work = work->next;
557 if (debug_flag > 2)
558 dbg_log (_("(grptable_update) Release wait lock"));
559 pthread_rwlock_unlock (&grplock);
560 sleep (20);
562 return NULL;
565 static void *
566 negtable_update (void *v)
568 time_t now;
569 int i;
571 sleep (30);
573 while (!do_shutdown)
575 if (debug_flag > 2)
576 dbg_log (_("(neggrptable_update) Wait for write lock!"));
578 pthread_rwlock_wrlock (&neglock);
580 if (debug_flag > 2)
581 dbg_log (_("(neggrptable_update) Have write lock"));
583 time (&now);
584 for (i = 0; i < modulo; ++i)
586 neghash *work = &negtbl[i];
588 while (work && work->key)
590 if ((now - work->create) >= negtimeout)
592 if (debug_flag)
593 dbg_log (_("Give \"%s\" free"), work->key);
595 free (work->key);
597 if (work->next != NULL)
599 neghash *tmp = work->next;
600 work->create = tmp->create;
601 work->next = tmp->next;
602 work->key = tmp->key;
603 free (tmp);
605 else
606 work->key = NULL;
608 work = work->next;
611 if (debug_flag > 2)
612 dbg_log (_("(neggrptable_update) Release wait lock"));
613 pthread_rwlock_unlock (&neglock);
614 sleep (10);
616 return NULL;