2005-02-25 Roland McGrath <roland@redhat.com>
[glibc.git] / nscd / nscd_helper.c
blobc99cb430aa5fc61a6a3c1deff81ca1e89be7acfe
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/uio.h>
30 #include <sys/un.h>
31 #include <not-cancel.h>
32 #include <nis/rpcsvc/nis.h>
34 #include "nscd-client.h"
37 ssize_t
38 __readall (int fd, void *buf, size_t len)
40 size_t n = len;
41 ssize_t ret;
44 ret = TEMP_FAILURE_RETRY (__read (fd, buf, n));
45 if (ret <= 0)
46 break;
47 buf = (char *) buf + ret;
48 n -= ret;
50 while (n > 0);
51 return ret < 0 ? ret : len - n;
55 ssize_t
56 __readvall (int fd, const struct iovec *iov, int iovcnt)
58 ssize_t ret = TEMP_FAILURE_RETRY (__readv (fd, iov, iovcnt));
59 if (ret <= 0)
60 return ret;
62 size_t total = 0;
63 for (int i = 0; i < iovcnt; ++i)
64 total += iov[i].iov_len;
66 if (ret < total)
68 struct iovec iov_buf[iovcnt];
69 ssize_t r = ret;
71 struct iovec *iovp = memcpy (iov_buf, iov, iovcnt * sizeof (*iov));
74 while (iovp->iov_len <= r)
76 r -= iovp->iov_len;
77 --iovcnt;
78 ++iovp;
80 iovp->iov_base = (char *) iovp->iov_base + r;
81 iovp->iov_len -= r;
82 r = TEMP_FAILURE_RETRY (__readv (fd, iovp, iovcnt));
83 if (r <= 0)
84 break;
85 ret += r;
87 while (ret < total);
88 if (r < 0)
89 ret = r;
91 return ret;
95 static int
96 open_socket (void)
98 int sock = __socket (PF_UNIX, SOCK_STREAM, 0);
99 if (sock < 0)
100 return -1;
102 /* Make socket non-blocking. */
103 int fl = __fcntl (sock, F_GETFL);
104 if (fl != -1)
105 __fcntl (sock, F_SETFL, fl | O_NONBLOCK);
107 struct sockaddr_un sun;
108 sun.sun_family = AF_UNIX;
109 strcpy (sun.sun_path, _PATH_NSCDSOCKET);
110 if (__connect (sock, (struct sockaddr *) &sun, sizeof (sun)) < 0
111 && errno != EINPROGRESS)
112 goto out;
114 struct pollfd fds[1];
115 fds[0].fd = sock;
116 fds[0].events = POLLOUT | POLLERR | POLLHUP;
117 if (__poll (fds, 1, 5 * 1000) > 0)
118 /* Success. We do not check for success of the connect call here.
119 If it failed, the following operations will fail. */
120 return sock;
122 out:
123 close_not_cancel_no_status (sock);
125 return -1;
129 void
130 __nscd_unmap (struct mapped_database *mapped)
132 assert (mapped->counter == 0);
133 __munmap ((void *) mapped->head, mapped->mapsize);
134 free (mapped);
138 /* Try to get a file descriptor for the shared meory segment
139 containing the database. */
140 static struct mapped_database *
141 get_mapping (request_type type, const char *key,
142 struct mapped_database **mappedp)
144 struct mapped_database *result = NO_MAPPING;
145 #ifdef SCM_RIGHTS
146 const size_t keylen = strlen (key) + 1;
147 char resdata[keylen];
148 int saved_errno = errno;
150 int mapfd = -1;
152 /* Send the request. */
153 struct iovec iov[2];
154 request_header req;
156 int sock = open_socket ();
157 if (sock < 0)
158 goto out;
160 req.version = NSCD_VERSION;
161 req.type = type;
162 req.key_len = keylen;
164 iov[0].iov_base = &req;
165 iov[0].iov_len = sizeof (req);
166 iov[1].iov_base = (void *) key;
167 iov[1].iov_len = keylen;
169 if (TEMP_FAILURE_RETRY (__writev (sock, iov, 2))
170 != iov[0].iov_len + iov[1].iov_len)
171 /* We cannot even write the request. */
172 goto out_close2;
174 /* Room for the data sent along with the file descriptor. We expect
175 the key name back. */
176 iov[0].iov_base = resdata;
177 iov[0].iov_len = keylen;
179 char buf[CMSG_SPACE (sizeof (int))];
180 struct msghdr msg = { .msg_iov = iov, .msg_iovlen = 1,
181 .msg_control = buf, .msg_controllen = sizeof (buf) };
182 struct cmsghdr *cmsg = CMSG_FIRSTHDR (&msg);
184 cmsg->cmsg_level = SOL_SOCKET;
185 cmsg->cmsg_type = SCM_RIGHTS;
186 cmsg->cmsg_len = CMSG_LEN (sizeof (int));
188 *(int *) CMSG_DATA (cmsg) = -1;
190 msg.msg_controllen = cmsg->cmsg_len;
192 struct pollfd fds[1];
193 fds[0].fd = sock;
194 fds[0].events = POLLIN | POLLERR | POLLHUP;
195 if (__poll (fds, 1, 5 * 1000) <= 0)
196 /* Failure or timeout. */
197 goto out_close2;
199 #ifndef MSG_NOSIGNAL
200 # define MSG_NOSIGNAL 0
201 #endif
202 if (TEMP_FAILURE_RETRY (__recvmsg (sock, &msg, MSG_NOSIGNAL)) != keylen)
203 goto out_close2;
205 mapfd = *(int *) CMSG_DATA (cmsg);
207 if (CMSG_FIRSTHDR (&msg)->cmsg_len != CMSG_LEN (sizeof (int)))
208 goto out_close;
210 struct stat64 st;
211 if (strcmp (resdata, key) != 0
212 || fstat64 (mapfd, &st) != 0
213 || st.st_size < sizeof (struct database_pers_head))
214 goto out_close;
216 struct database_pers_head head;
217 if (TEMP_FAILURE_RETRY (__pread (mapfd, &head, sizeof (head), 0))
218 != sizeof (head))
219 goto out_close;
221 if (head.version != DB_VERSION || head.header_size != sizeof (head)
222 /* This really should not happen but who knows, maybe the update
223 thread got stuck. */
224 || (! head.nscd_certainly_running
225 && head.timestamp + MAPPING_TIMEOUT < time (NULL)))
226 goto out_close;
228 size_t size = (sizeof (head) + roundup (head.module * sizeof (ref_t), ALIGN)
229 + head.data_size);
231 if (st.st_size < size)
232 goto out_close;
234 /* The file is large enough, map it now. */
235 void *mapping = __mmap (NULL, size, PROT_READ, MAP_SHARED, mapfd, 0);
236 if (mapping != MAP_FAILED)
238 /* Allocate a record for the mapping. */
239 struct mapped_database *newp;
241 newp = malloc (sizeof (*newp));
242 if (newp == NULL)
244 /* Ugh, after all we went through the memory allocation failed. */
245 __munmap (result, size);
246 goto out_close;
249 newp->head = mapping;
250 newp->data = ((char *) mapping + head.header_size
251 + roundup (head.module * sizeof (ref_t), ALIGN));
252 newp->mapsize = size;
253 /* Set counter to 1 to show it is usable. */
254 newp->counter = 1;
256 result = newp;
259 out_close:
260 __close (mapfd);
261 out_close2:
262 __close (sock);
263 out:
264 __set_errno (saved_errno);
265 #endif /* SCM_RIGHTS */
267 struct mapped_database *oldval = *mappedp;
268 *mappedp = result;
270 if (oldval != NULL && atomic_decrement_val (&oldval->counter) == 0)
271 __nscd_unmap (oldval);
273 return result;
277 struct mapped_database *
278 __nscd_get_map_ref (request_type type, const char *name,
279 struct locked_map_ptr *mapptr, int *gc_cyclep)
281 struct mapped_database *cur = mapptr->mapped;
282 if (cur == NO_MAPPING)
283 return cur;
285 int cnt = 0;
286 while (atomic_compare_and_exchange_val_acq (&mapptr->lock, 1, 0) != 0)
288 // XXX Best number of rounds?
289 if (++cnt > 5)
290 return NO_MAPPING;
292 atomic_delay ();
295 cur = mapptr->mapped;
297 if (__builtin_expect (cur != NO_MAPPING, 1))
299 /* If not mapped or timestamp not updated, request new map. */
300 if (cur == NULL
301 || (cur->head->nscd_certainly_running == 0
302 && cur->head->timestamp + MAPPING_TIMEOUT < time (NULL)))
303 cur = get_mapping (type, name, &mapptr->mapped);
305 if (__builtin_expect (cur != NO_MAPPING, 1))
307 if (__builtin_expect (((*gc_cyclep = cur->head->gc_cycle) & 1) != 0,
309 cur = NO_MAPPING;
310 else
311 atomic_increment (&cur->counter);
315 mapptr->lock = 0;
317 return cur;
321 const struct datahead *
322 __nscd_cache_search (request_type type, const char *key, size_t keylen,
323 const struct mapped_database *mapped)
325 unsigned long int hash = __nis_hash (key, keylen) % mapped->head->module;
327 ref_t work = mapped->head->array[hash];
328 while (work != ENDREF)
330 struct hashentry *here = (struct hashentry *) (mapped->data + work);
332 if (type == here->type && keylen == here->len
333 && memcmp (key, mapped->data + here->key, keylen) == 0)
335 /* We found the entry. Increment the appropriate counter. */
336 const struct datahead *dh
337 = (struct datahead *) (mapped->data + here->packet);
339 /* See whether we must ignore the entry or whether something
340 is wrong because garbage collection is in progress. */
341 if (dh->usable && ((char *) dh + dh->allocsize
342 <= (char *) mapped->head + mapped->mapsize))
343 return dh;
346 work = here->next;
349 return NULL;
353 /* Create a socket connected to a name. */
355 __nscd_open_socket (const char *key, size_t keylen, request_type type,
356 void *response, size_t responselen)
358 int saved_errno = errno;
360 int sock = open_socket ();
361 if (sock >= 0)
363 request_header req;
364 req.version = NSCD_VERSION;
365 req.type = type;
366 req.key_len = keylen;
368 struct iovec vec[2];
369 vec[0].iov_base = &req;
370 vec[0].iov_len = sizeof (request_header);
371 vec[1].iov_base = (void *) key;
372 vec[1].iov_len = keylen;
374 ssize_t nbytes = TEMP_FAILURE_RETRY (__writev (sock, vec, 2));
375 if (nbytes == (ssize_t) (sizeof (request_header) + keylen))
377 /* Wait for data. */
378 struct pollfd fds[1];
379 fds[0].fd = sock;
380 fds[0].events = POLLIN | POLLERR | POLLHUP;
381 if (__poll (fds, 1, 5 * 1000) > 0)
383 nbytes = TEMP_FAILURE_RETRY (__read (sock, response,
384 responselen));
385 if (nbytes == (ssize_t) responselen)
386 return sock;
390 close_not_cancel_no_status (sock);
393 __set_errno (saved_errno);
395 return -1;