[BZ #2510, BZ #2830, BZ #3137, BZ #3313, BZ #3426, BZ #3465, BZ #3480, BZ #3483,...
[glibc.git] / nscd / nscd_helper.c
blob7c45981586696f0f3f467bc2a9f4c26b0b945b3e
1 /* Copyright (C) 1998-2002,2003,2004,2005,2006 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 <time.h>
25 #include <unistd.h>
26 #include <sys/mman.h>
27 #include <sys/poll.h>
28 #include <sys/socket.h>
29 #include <sys/stat.h>
30 #include <sys/time.h>
31 #include <sys/uio.h>
32 #include <sys/un.h>
33 #include <not-cancel.h>
34 #include <nis/rpcsvc/nis.h>
36 #include "nscd-client.h"
39 ssize_t
40 __readall (int fd, void *buf, size_t len)
42 size_t n = len;
43 ssize_t ret;
46 ret = TEMP_FAILURE_RETRY (__read (fd, buf, n));
47 if (ret <= 0)
48 break;
49 buf = (char *) buf + ret;
50 n -= ret;
52 while (n > 0);
53 return ret < 0 ? ret : len - n;
57 ssize_t
58 __readvall (int fd, const struct iovec *iov, int iovcnt)
60 ssize_t ret = TEMP_FAILURE_RETRY (__readv (fd, iov, iovcnt));
61 if (ret <= 0)
62 return ret;
64 size_t total = 0;
65 for (int i = 0; i < iovcnt; ++i)
66 total += iov[i].iov_len;
68 if (ret < total)
70 struct iovec iov_buf[iovcnt];
71 ssize_t r = ret;
73 struct iovec *iovp = memcpy (iov_buf, iov, iovcnt * sizeof (*iov));
76 while (iovp->iov_len <= r)
78 r -= iovp->iov_len;
79 --iovcnt;
80 ++iovp;
82 iovp->iov_base = (char *) iovp->iov_base + r;
83 iovp->iov_len -= r;
84 r = TEMP_FAILURE_RETRY (__readv (fd, iovp, iovcnt));
85 if (r <= 0)
86 break;
87 ret += r;
89 while (ret < total);
90 if (r < 0)
91 ret = r;
93 return ret;
97 static int
98 open_socket (void)
100 int sock = __socket (PF_UNIX, SOCK_STREAM, 0);
101 if (sock < 0)
102 return -1;
104 /* Make socket non-blocking. */
105 int fl = __fcntl (sock, F_GETFL);
106 if (fl != -1)
107 __fcntl (sock, F_SETFL, fl | O_NONBLOCK);
109 struct sockaddr_un sun;
110 sun.sun_family = AF_UNIX;
111 strcpy (sun.sun_path, _PATH_NSCDSOCKET);
112 if (__connect (sock, (struct sockaddr *) &sun, sizeof (sun)) < 0
113 && errno != EINPROGRESS)
114 goto out;
116 struct pollfd fds[1];
117 fds[0].fd = sock;
118 fds[0].events = POLLOUT | POLLERR | POLLHUP;
119 if (__poll (fds, 1, 5 * 1000) > 0)
120 /* Success. We do not check for success of the connect call here.
121 If it failed, the following operations will fail. */
122 return sock;
124 out:
125 close_not_cancel_no_status (sock);
127 return -1;
131 void
132 __nscd_unmap (struct mapped_database *mapped)
134 assert (mapped->counter == 0);
135 __munmap ((void *) mapped->head, mapped->mapsize);
136 free (mapped);
140 static int
141 wait_on_socket (int sock)
143 struct pollfd fds[1];
144 fds[0].fd = sock;
145 fds[0].events = POLLIN | POLLERR | POLLHUP;
146 int n = __poll (fds, 1, 5 * 1000);
147 if (n == -1 && __builtin_expect (errno == EINTR, 0))
149 /* Handle the case where the poll() call is interrupted by a
150 signal. We cannot just use TEMP_FAILURE_RETRY since it might
151 lead to infinite loops. */
152 struct timeval now;
153 (void) __gettimeofday (&now, NULL);
154 long int end = (now.tv_sec + 5) * 1000 + (now.tv_usec + 500) / 1000;
155 while (1)
157 long int timeout = end - (now.tv_sec * 1000
158 + (now.tv_usec + 500) / 1000);
159 n = __poll (fds, 1, timeout);
160 if (n != -1 || errno != EINTR)
161 break;
162 (void) __gettimeofday (&now, NULL);
166 return n;
170 /* Try to get a file descriptor for the shared meory segment
171 containing the database. */
172 static struct mapped_database *
173 get_mapping (request_type type, const char *key,
174 struct mapped_database **mappedp)
176 struct mapped_database *result = NO_MAPPING;
177 #ifdef SCM_RIGHTS
178 const size_t keylen = strlen (key) + 1;
179 int saved_errno = errno;
181 int mapfd = -1;
183 /* Send the request. */
184 struct
186 request_header req;
187 char key[keylen];
188 } reqdata;
190 int sock = open_socket ();
191 if (sock < 0)
192 goto out;
194 reqdata.req.version = NSCD_VERSION;
195 reqdata.req.type = type;
196 reqdata.req.key_len = keylen;
197 memcpy (reqdata.key, key, keylen);
199 # ifndef MSG_NOSIGNAL
200 # define MSG_NOSIGNAL 0
201 # endif
202 if (__builtin_expect (TEMP_FAILURE_RETRY (__send (sock, &reqdata,
203 sizeof (reqdata),
204 MSG_NOSIGNAL))
205 != sizeof (reqdata), 0))
206 /* We cannot even write the request. */
207 goto out_close2;
209 /* Room for the data sent along with the file descriptor. We expect
210 the key name back. */
211 # define resdata reqdata.key
212 struct iovec iov[1];
213 iov[0].iov_base = resdata;
214 iov[0].iov_len = keylen;
216 union
218 struct cmsghdr hdr;
219 char bytes[CMSG_SPACE (sizeof (int))];
220 } buf;
221 struct msghdr msg = { .msg_iov = iov, .msg_iovlen = 1,
222 .msg_control = buf.bytes,
223 .msg_controllen = sizeof (buf) };
224 struct cmsghdr *cmsg = CMSG_FIRSTHDR (&msg);
226 cmsg->cmsg_level = SOL_SOCKET;
227 cmsg->cmsg_type = SCM_RIGHTS;
228 cmsg->cmsg_len = CMSG_LEN (sizeof (int));
230 /* This access is well-aligned since BUF is correctly aligned for an
231 int and CMSG_DATA preserves this alignment. */
232 *(int *) CMSG_DATA (cmsg) = -1;
234 msg.msg_controllen = cmsg->cmsg_len;
236 if (wait_on_socket (sock) <= 0)
237 goto out_close2;
239 if (__builtin_expect (TEMP_FAILURE_RETRY (__recvmsg (sock, &msg, 0))
240 != keylen, 0))
241 goto out_close2;
243 mapfd = *(int *) CMSG_DATA (cmsg);
245 if (__builtin_expect (CMSG_FIRSTHDR (&msg)->cmsg_len
246 != CMSG_LEN (sizeof (int)), 0))
247 goto out_close;
249 struct stat64 st;
250 if (__builtin_expect (strcmp (resdata, key) != 0, 0)
251 || __builtin_expect (fstat64 (mapfd, &st) != 0, 0)
252 || __builtin_expect (st.st_size < sizeof (struct database_pers_head), 0))
253 goto out_close;
255 struct database_pers_head head;
256 if (__builtin_expect (TEMP_FAILURE_RETRY (__pread (mapfd, &head,
257 sizeof (head), 0))
258 != sizeof (head), 0))
259 goto out_close;
261 if (__builtin_expect (head.version != DB_VERSION, 0)
262 || __builtin_expect (head.header_size != sizeof (head), 0)
263 /* This really should not happen but who knows, maybe the update
264 thread got stuck. */
265 || __builtin_expect (! head.nscd_certainly_running
266 && head.timestamp + MAPPING_TIMEOUT < time (NULL),
268 goto out_close;
270 size_t size = (sizeof (head) + roundup (head.module * sizeof (ref_t), ALIGN)
271 + head.data_size);
273 if (__builtin_expect (st.st_size < size, 0))
274 goto out_close;
276 /* The file is large enough, map it now. */
277 void *mapping = __mmap (NULL, size, PROT_READ, MAP_SHARED, mapfd, 0);
278 if (__builtin_expect (mapping != MAP_FAILED, 1))
280 /* Allocate a record for the mapping. */
281 struct mapped_database *newp = malloc (sizeof (*newp));
282 if (newp == NULL)
284 /* Ugh, after all we went through the memory allocation failed. */
285 __munmap (mapping, size);
286 goto out_close;
289 newp->head = mapping;
290 newp->data = ((char *) mapping + head.header_size
291 + roundup (head.module * sizeof (ref_t), ALIGN));
292 newp->mapsize = size;
293 newp->datasize = head.data_size;
294 /* Set counter to 1 to show it is usable. */
295 newp->counter = 1;
297 result = newp;
300 out_close:
301 __close (mapfd);
302 out_close2:
303 __close (sock);
304 out:
305 __set_errno (saved_errno);
306 #endif /* SCM_RIGHTS */
308 struct mapped_database *oldval = *mappedp;
309 *mappedp = result;
311 if (oldval != NULL && atomic_decrement_val (&oldval->counter) == 0)
312 __nscd_unmap (oldval);
314 return result;
318 struct mapped_database *
319 __nscd_get_map_ref (request_type type, const char *name,
320 volatile struct locked_map_ptr *mapptr, int *gc_cyclep)
322 struct mapped_database *cur = mapptr->mapped;
323 if (cur == NO_MAPPING)
324 return cur;
326 int cnt = 0;
327 while (__builtin_expect (atomic_compare_and_exchange_val_acq (&mapptr->lock,
328 1, 0) != 0, 0))
330 // XXX Best number of rounds?
331 if (__builtin_expect (++cnt > 5, 0))
332 return NO_MAPPING;
334 atomic_delay ();
337 cur = mapptr->mapped;
339 if (__builtin_expect (cur != NO_MAPPING, 1))
341 /* If not mapped or timestamp not updated, request new map. */
342 if (cur == NULL
343 || (cur->head->nscd_certainly_running == 0
344 && cur->head->timestamp + MAPPING_TIMEOUT < time (NULL))
345 || cur->head->data_size > cur->datasize)
346 cur = get_mapping (type, name,
347 (struct mapped_database **) &mapptr->mapped);
349 if (__builtin_expect (cur != NO_MAPPING, 1))
351 if (__builtin_expect (((*gc_cyclep = cur->head->gc_cycle) & 1) != 0,
353 cur = NO_MAPPING;
354 else
355 atomic_increment (&cur->counter);
359 mapptr->lock = 0;
361 return cur;
365 const struct datahead *
366 __nscd_cache_search (request_type type, const char *key, size_t keylen,
367 const struct mapped_database *mapped)
369 unsigned long int hash = __nis_hash (key, keylen) % mapped->head->module;
370 size_t datasize = mapped->datasize;
372 ref_t work = mapped->head->array[hash];
373 while (work != ENDREF && work + sizeof (struct hashentry) <= datasize)
375 struct hashentry *here = (struct hashentry *) (mapped->data + work);
377 if (type == here->type
378 && keylen == here->len
379 && here->key + here->len <= datasize
380 && memcmp (key, mapped->data + here->key, keylen) == 0
381 && here->packet + sizeof (struct datahead) <= datasize)
383 /* We found the entry. Increment the appropriate counter. */
384 const struct datahead *dh
385 = (struct datahead *) (mapped->data + here->packet);
387 /* See whether we must ignore the entry or whether something
388 is wrong because garbage collection is in progress. */
389 if (dh->usable && here->packet + dh->allocsize <= datasize)
390 return dh;
393 work = here->next;
396 return NULL;
400 /* Create a socket connected to a name. */
402 __nscd_open_socket (const char *key, size_t keylen, request_type type,
403 void *response, size_t responselen)
405 int saved_errno = errno;
407 int sock = open_socket ();
408 if (sock >= 0)
410 request_header req;
411 req.version = NSCD_VERSION;
412 req.type = type;
413 req.key_len = keylen;
415 struct iovec vec[2];
416 vec[0].iov_base = &req;
417 vec[0].iov_len = sizeof (request_header);
418 vec[1].iov_base = (void *) key;
419 vec[1].iov_len = keylen;
421 ssize_t nbytes = TEMP_FAILURE_RETRY (__writev (sock, vec, 2));
422 if (nbytes == (ssize_t) (sizeof (request_header) + keylen)
423 /* Wait for data. */
424 && wait_on_socket (sock) > 0)
426 nbytes = TEMP_FAILURE_RETRY (__read (sock, response, responselen));
427 if (nbytes == (ssize_t) responselen)
428 return sock;
431 close_not_cancel_no_status (sock);
434 __set_errno (saved_errno);
436 return -1;