Updated to fedora-glibc-20050721T0814
[glibc.git] / nscd / nscd_helper.c
blob65e78a1174689cf4e9f58452aac4852d858dfb09
1 /* Copyright (C) 1998-2002, 2003, 2004, 2005 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 Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the 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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <assert.h>
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <stdbool.h>
24 #include <unistd.h>
25 #include <sys/mman.h>
26 #include <sys/poll.h>
27 #include <sys/socket.h>
28 #include <sys/stat.h>
29 #include <sys/time.h>
30 #include <sys/uio.h>
31 #include <sys/un.h>
32 #include <not-cancel.h>
33 #include <nis/rpcsvc/nis.h>
35 #include "nscd-client.h"
38 ssize_t
39 __readall (int fd, void *buf, size_t len)
41 size_t n = len;
42 ssize_t ret;
45 ret = TEMP_FAILURE_RETRY (__read (fd, buf, n));
46 if (ret <= 0)
47 break;
48 buf = (char *) buf + ret;
49 n -= ret;
51 while (n > 0);
52 return ret < 0 ? ret : len - n;
56 ssize_t
57 __readvall (int fd, const struct iovec *iov, int iovcnt)
59 ssize_t ret = TEMP_FAILURE_RETRY (__readv (fd, iov, iovcnt));
60 if (ret <= 0)
61 return ret;
63 size_t total = 0;
64 for (int i = 0; i < iovcnt; ++i)
65 total += iov[i].iov_len;
67 if (ret < total)
69 struct iovec iov_buf[iovcnt];
70 ssize_t r = ret;
72 struct iovec *iovp = memcpy (iov_buf, iov, iovcnt * sizeof (*iov));
75 while (iovp->iov_len <= r)
77 r -= iovp->iov_len;
78 --iovcnt;
79 ++iovp;
81 iovp->iov_base = (char *) iovp->iov_base + r;
82 iovp->iov_len -= r;
83 r = TEMP_FAILURE_RETRY (__readv (fd, iovp, iovcnt));
84 if (r <= 0)
85 break;
86 ret += r;
88 while (ret < total);
89 if (r < 0)
90 ret = r;
92 return ret;
96 static int
97 open_socket (void)
99 int sock = __socket (PF_UNIX, SOCK_STREAM, 0);
100 if (sock < 0)
101 return -1;
103 /* Make socket non-blocking. */
104 int fl = __fcntl (sock, F_GETFL);
105 if (fl != -1)
106 __fcntl (sock, F_SETFL, fl | O_NONBLOCK);
108 struct sockaddr_un sun;
109 sun.sun_family = AF_UNIX;
110 strcpy (sun.sun_path, _PATH_NSCDSOCKET);
111 if (__connect (sock, (struct sockaddr *) &sun, sizeof (sun)) < 0
112 && errno != EINPROGRESS)
113 goto out;
115 struct pollfd fds[1];
116 fds[0].fd = sock;
117 fds[0].events = POLLOUT | POLLERR | POLLHUP;
118 if (__poll (fds, 1, 5 * 1000) > 0)
119 /* Success. We do not check for success of the connect call here.
120 If it failed, the following operations will fail. */
121 return sock;
123 out:
124 close_not_cancel_no_status (sock);
126 return -1;
130 void
131 __nscd_unmap (struct mapped_database *mapped)
133 assert (mapped->counter == 0);
134 __munmap ((void *) mapped->head, mapped->mapsize);
135 free (mapped);
139 static int
140 wait_on_socket (int sock)
142 struct pollfd fds[1];
143 fds[0].fd = sock;
144 fds[0].events = POLLIN | POLLERR | POLLHUP;
145 int n = __poll (fds, 1, 5 * 1000);
146 if (n == -1 && __builtin_expect (errno == EINTR, 0))
148 /* Handle the case where the poll() call is interrupted by a
149 signal. We cannot just use TEMP_FAILURE_RETRY since it might
150 lead to infinite loops. */
151 struct timeval now;
152 (void) __gettimeofday (&now, NULL);
153 long int end = (now.tv_sec + 5) * 1000 + (now.tv_usec + 500) / 1000;
154 while (1)
156 long int timeout = end - (now.tv_sec * 1000
157 + (now.tv_usec + 500) / 1000);
158 n = __poll (fds, 1, timeout);
159 if (n != -1 || errno != EINTR)
160 break;
161 (void) __gettimeofday (&now, NULL);
165 return n;
169 /* Try to get a file descriptor for the shared meory segment
170 containing the database. */
171 static struct mapped_database *
172 get_mapping (request_type type, const char *key,
173 struct mapped_database **mappedp)
175 struct mapped_database *result = NO_MAPPING;
176 #ifdef SCM_RIGHTS
177 const size_t keylen = strlen (key) + 1;
178 char resdata[keylen];
179 int saved_errno = errno;
181 int mapfd = -1;
183 /* Send the request. */
184 struct iovec iov[2];
185 request_header req;
187 int sock = open_socket ();
188 if (sock < 0)
189 goto out;
191 req.version = NSCD_VERSION;
192 req.type = type;
193 req.key_len = keylen;
195 iov[0].iov_base = &req;
196 iov[0].iov_len = sizeof (req);
197 iov[1].iov_base = (void *) key;
198 iov[1].iov_len = keylen;
200 if (__builtin_expect (TEMP_FAILURE_RETRY (__writev (sock, iov, 2))
201 != iov[0].iov_len + iov[1].iov_len, 0))
202 /* We cannot even write the request. */
203 goto out_close2;
205 /* Room for the data sent along with the file descriptor. We expect
206 the key name back. */
207 iov[0].iov_base = resdata;
208 iov[0].iov_len = keylen;
210 union
212 struct cmsghdr hdr;
213 char bytes[CMSG_SPACE (sizeof (int))];
214 } buf;
215 struct msghdr msg = { .msg_iov = iov, .msg_iovlen = 1,
216 .msg_control = buf.bytes,
217 .msg_controllen = sizeof (buf) };
218 struct cmsghdr *cmsg = CMSG_FIRSTHDR (&msg);
220 cmsg->cmsg_level = SOL_SOCKET;
221 cmsg->cmsg_type = SCM_RIGHTS;
222 cmsg->cmsg_len = CMSG_LEN (sizeof (int));
224 /* This access is well-aligned since BUF is correctly aligned for an
225 int and CMSG_DATA preserves this alignment. */
226 *(int *) CMSG_DATA (cmsg) = -1;
228 msg.msg_controllen = cmsg->cmsg_len;
230 if (wait_on_socket (sock) <= 0)
231 goto out_close2;
233 #ifndef MSG_NOSIGNAL
234 # define MSG_NOSIGNAL 0
235 #endif
236 if (__builtin_expect (TEMP_FAILURE_RETRY (__recvmsg (sock, &msg,
237 MSG_NOSIGNAL))
238 != keylen, 0))
239 goto out_close2;
241 mapfd = *(int *) CMSG_DATA (cmsg);
243 if (__builtin_expect (CMSG_FIRSTHDR (&msg)->cmsg_len
244 != CMSG_LEN (sizeof (int)), 0))
245 goto out_close;
247 struct stat64 st;
248 if (__builtin_expect (strcmp (resdata, key) != 0, 0)
249 || __builtin_expect (fstat64 (mapfd, &st) != 0, 0)
250 || __builtin_expect (st.st_size < sizeof (struct database_pers_head), 0))
251 goto out_close;
253 struct database_pers_head head;
254 if (__builtin_expect (TEMP_FAILURE_RETRY (__pread (mapfd, &head,
255 sizeof (head), 0))
256 != sizeof (head), 0))
257 goto out_close;
259 if (__builtin_expect (head.version != DB_VERSION, 0)
260 || __builtin_expect (head.header_size != sizeof (head), 0)
261 /* This really should not happen but who knows, maybe the update
262 thread got stuck. */
263 || __builtin_expect (! head.nscd_certainly_running
264 && head.timestamp + MAPPING_TIMEOUT < time (NULL),
266 goto out_close;
268 size_t size = (sizeof (head) + roundup (head.module * sizeof (ref_t), ALIGN)
269 + head.data_size);
271 if (__builtin_expect (st.st_size < size, 0))
272 goto out_close;
274 /* The file is large enough, map it now. */
275 void *mapping = __mmap (NULL, size, PROT_READ, MAP_SHARED, mapfd, 0);
276 if (__builtin_expect (mapping != MAP_FAILED, 1))
278 /* Allocate a record for the mapping. */
279 struct mapped_database *newp = malloc (sizeof (*newp));
280 if (newp == NULL)
282 /* Ugh, after all we went through the memory allocation failed. */
283 __munmap (mapping, size);
284 goto out_close;
287 newp->head = mapping;
288 newp->data = ((char *) mapping + head.header_size
289 + roundup (head.module * sizeof (ref_t), ALIGN));
290 newp->mapsize = size;
291 /* Set counter to 1 to show it is usable. */
292 newp->counter = 1;
294 result = newp;
297 out_close:
298 __close (mapfd);
299 out_close2:
300 __close (sock);
301 out:
302 __set_errno (saved_errno);
303 #endif /* SCM_RIGHTS */
305 struct mapped_database *oldval = *mappedp;
306 *mappedp = result;
308 if (oldval != NULL && atomic_decrement_val (&oldval->counter) == 0)
309 __nscd_unmap (oldval);
311 return result;
315 struct mapped_database *
316 __nscd_get_map_ref (request_type type, const char *name,
317 struct locked_map_ptr *mapptr, int *gc_cyclep)
319 struct mapped_database *cur = mapptr->mapped;
320 if (cur == NO_MAPPING)
321 return cur;
323 int cnt = 0;
324 while (atomic_compare_and_exchange_val_acq (&mapptr->lock, 1, 0) != 0)
326 // XXX Best number of rounds?
327 if (++cnt > 5)
328 return NO_MAPPING;
330 atomic_delay ();
333 cur = mapptr->mapped;
335 if (__builtin_expect (cur != NO_MAPPING, 1))
337 /* If not mapped or timestamp not updated, request new map. */
338 if (cur == NULL
339 || (cur->head->nscd_certainly_running == 0
340 && cur->head->timestamp + MAPPING_TIMEOUT < time (NULL)))
341 cur = get_mapping (type, name, &mapptr->mapped);
343 if (__builtin_expect (cur != NO_MAPPING, 1))
345 if (__builtin_expect (((*gc_cyclep = cur->head->gc_cycle) & 1) != 0,
347 cur = NO_MAPPING;
348 else
349 atomic_increment (&cur->counter);
353 mapptr->lock = 0;
355 return cur;
359 const struct datahead *
360 __nscd_cache_search (request_type type, const char *key, size_t keylen,
361 const struct mapped_database *mapped)
363 unsigned long int hash = __nis_hash (key, keylen) % mapped->head->module;
365 ref_t work = mapped->head->array[hash];
366 while (work != ENDREF)
368 struct hashentry *here = (struct hashentry *) (mapped->data + work);
370 if (type == here->type && keylen == here->len
371 && memcmp (key, mapped->data + here->key, keylen) == 0)
373 /* We found the entry. Increment the appropriate counter. */
374 const struct datahead *dh
375 = (struct datahead *) (mapped->data + here->packet);
377 /* See whether we must ignore the entry or whether something
378 is wrong because garbage collection is in progress. */
379 if (dh->usable && ((char *) dh + dh->allocsize
380 <= (char *) mapped->head + mapped->mapsize))
381 return dh;
384 work = here->next;
387 return NULL;
391 /* Create a socket connected to a name. */
393 __nscd_open_socket (const char *key, size_t keylen, request_type type,
394 void *response, size_t responselen)
396 int saved_errno = errno;
398 int sock = open_socket ();
399 if (sock >= 0)
401 request_header req;
402 req.version = NSCD_VERSION;
403 req.type = type;
404 req.key_len = keylen;
406 struct iovec vec[2];
407 vec[0].iov_base = &req;
408 vec[0].iov_len = sizeof (request_header);
409 vec[1].iov_base = (void *) key;
410 vec[1].iov_len = keylen;
412 ssize_t nbytes = TEMP_FAILURE_RETRY (__writev (sock, vec, 2));
413 if (nbytes == (ssize_t) (sizeof (request_header) + keylen)
414 /* Wait for data. */
415 && wait_on_socket (sock) > 0)
417 nbytes = TEMP_FAILURE_RETRY (__read (sock, response, responselen));
418 if (nbytes == (ssize_t) responselen)
419 return sock;
422 close_not_cancel_no_status (sock);
425 __set_errno (saved_errno);
427 return -1;