1 /* Copyright (c) 1998, 1999 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@cygnus.com>, 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 <atomicity.h>
27 #include <arpa/inet.h>
28 #include <rpcsvc/nis.h>
29 #include <sys/param.h>
36 /* Search the cache for a matching entry and return it when found. If
37 this fails search the negative cache and return (void *) -1 if this
38 search was successful. Otherwise return NULL.
40 This function must be called with the read-lock held. */
42 cache_search (int type
, void *key
, size_t len
, struct database
*table
,
45 unsigned long int hash
= __nis_hash (key
, len
) % table
->module
;
46 struct hashentry
*work
;
48 work
= table
->array
[hash
];
52 if (type
== work
->type
&& len
== work
->len
53 && memcmp (key
, work
->key
, len
) == 0 && work
->owner
== owner
)
55 /* We found the entry. Increment the appropriate counter. */
56 if (work
->data
== (void *) -1)
70 /* Add a new entry to the cache. The return value is zero if the function
73 This function must be called with the read-lock held.
75 We modify the table but we nevertheless only acquire a read-lock.
76 This is ok since we use operations which would be safe even without
77 locking, given that the `prune_cache' function never runs. Using
78 the readlock reduces the chance of conflicts. */
80 cache_add (int type
, void *key
, size_t len
, const void *packet
, size_t total
,
81 void *data
, int last
, time_t t
, struct database
*table
, uid_t owner
)
83 unsigned long int hash
= __nis_hash (key
, len
) % table
->module
;
84 struct hashentry
*newp
;
86 newp
= malloc (sizeof (struct hashentry
));
88 error (EXIT_FAILURE
, errno
, _("while allocating hash table entry"));
96 newp
->packet
= packet
;
101 /* Put the new entry in the first position. */
103 newp
->next
= table
->array
[hash
];
104 while (! compare_and_swap ((volatile long int *) &table
->array
[hash
],
105 (long int) newp
->next
, (long int) newp
));
107 /* Update the statistics. */
108 if (data
== (void *) -1)
114 /* Walk through the table and remove all entries which lifetime ended.
116 We have a problem here. To actually remove the entries we must get
117 the write-lock. But since we want to keep the time we have the
118 lock as short as possible we cannot simply acquire the lock when we
119 start looking for timedout entries.
121 Therefore we do it in two stages: first we look for entries which
122 must be invalidated and remember them. Then we get the lock and
123 actually remove them. This is complicated by the way we have to
124 free the data structures since some hash table entries share the same
127 prune_cache (struct database
*table
, time_t now
)
129 size_t cnt
= table
->module
;
132 size_t first
= cnt
+ 1;
135 /* If this table is not actually used don't do anything. */
139 /* If we check for the modification of the underlying file we invalidate
140 the entries also in this case. */
141 if (table
->check_file
)
145 if (stat (table
->filename
, &st
) < 0)
148 /* We cannot stat() the file, disable file checking if the
149 file does not exist. */
150 dbg_log (_("cannot stat() file `%s': %s"),
151 table
->filename
, strerror_r (errno
, buf
, sizeof (buf
)));
153 table
->check_file
= 0;
157 if (st
.st_mtime
!= table
->file_mtime
)
159 /* The file changed. Invalidate all entries. */
161 table
->file_mtime
= st
.st_mtime
;
166 /* We run through the table and find values which are not valid anymore.
168 Note that for the initial step, finding the entries to be removed,
169 we don't need to get any lock. It is at all timed assured that the
170 linked lists are set up correctly and that no second thread prunes
174 struct hashentry
*runp
= table
->array
[--cnt
];
180 if (runp
->timeout
< now
)
184 first
= MIN (first
, cnt
);
185 last
= MAX (last
, cnt
);
194 struct hashentry
*head
= NULL
;
196 /* Now we have to get the write lock since we are about to modify
198 pthread_rwlock_wrlock (&table
->lock
);
200 while (first
<= last
)
204 struct hashentry
*runp
;
206 while (table
->array
[first
]->timeout
< now
)
208 table
->array
[first
]->dellist
= head
;
209 head
= table
->array
[first
];
210 table
->array
[first
] = head
->next
;
211 if (--mark
[first
] == 0)
215 runp
= table
->array
[first
];
216 while (mark
[first
] > 0)
218 if (runp
->next
->timeout
< now
)
220 runp
->next
->dellist
= head
;
222 runp
->next
= head
->next
;
233 pthread_rwlock_unlock (&table
->lock
);
235 /* And another run to free the data. */
238 struct hashentry
*old
= head
;
242 char buf
[INET6_ADDRSTRLEN
];
245 if ((old
->type
== GETHOSTBYADDR
|| old
->type
== GETHOSTBYADDRv6
)
246 && (old
->last
|| old
->data
== (void *) -1))
248 inet_ntop (old
->type
== GETHOSTBYADDR
? AF_INET
: AF_INET6
,
249 old
->key
, buf
, sizeof (buf
));
253 str
= old
->last
? old
->key
: (old
->data
== (void *) -1
256 dbg_log ("remove %s entry \"%s\"", serv2str
[old
->type
], str
);
259 /* Free the data structures. */
260 if (old
->data
== (void *) -1)
265 head
= head
->dellist
;
269 while (head
!= NULL
);