Updated to fedora-glibc-2_3-20050226T0141
[glibc.git] / nscd / nscd_helper.c
blob12c10664af3ec6ffbeb2c5f00023ad473ff6b64e
1 /* Copyright (C) 1998-2002, 2003, 2004 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 if (TEMP_FAILURE_RETRY (__recvmsg (sock, &msg, 0)) != keylen)
200 goto out_close2;
202 mapfd = *(int *) CMSG_DATA (cmsg);
204 if (CMSG_FIRSTHDR (&msg)->cmsg_len != CMSG_LEN (sizeof (int)))
205 goto out_close;
207 struct stat64 st;
208 if (strcmp (resdata, key) != 0
209 || fstat64 (mapfd, &st) != 0
210 || st.st_size < sizeof (struct database_pers_head))
211 goto out_close;
213 struct database_pers_head head;
214 if (TEMP_FAILURE_RETRY (__pread (mapfd, &head, sizeof (head), 0))
215 != sizeof (head))
216 goto out_close;
218 if (head.version != DB_VERSION || head.header_size != sizeof (head)
219 /* This really should not happen but who knows, maybe the update
220 thread got stuck. */
221 || (! head.nscd_certainly_running
222 && head.timestamp + MAPPING_TIMEOUT < time (NULL)))
223 goto out_close;
225 size_t size = (sizeof (head) + roundup (head.module * sizeof (ref_t), ALIGN)
226 + head.data_size);
228 if (st.st_size < size)
229 goto out_close;
231 /* The file is large enough, map it now. */
232 void *mapping = __mmap (NULL, size, PROT_READ, MAP_SHARED, mapfd, 0);
233 if (mapping != MAP_FAILED)
235 /* Allocate a record for the mapping. */
236 struct mapped_database *newp;
238 newp = malloc (sizeof (*newp));
239 if (newp == NULL)
241 /* Ugh, after all we went through the memory allocation failed. */
242 __munmap (result, size);
243 goto out_close;
246 newp->head = mapping;
247 newp->data = ((char *) mapping + head.header_size
248 + roundup (head.module * sizeof (ref_t), ALIGN));
249 newp->mapsize = size;
250 /* Set counter to 1 to show it is usable. */
251 newp->counter = 1;
253 result = newp;
256 out_close:
257 __close (mapfd);
258 out_close2:
259 __close (sock);
260 out:
261 __set_errno (saved_errno);
262 #endif /* SCM_RIGHTS */
264 struct mapped_database *oldval = *mappedp;
265 *mappedp = result;
267 if (oldval != NULL && atomic_decrement_val (&oldval->counter) == 0)
268 __nscd_unmap (oldval);
270 return result;
274 struct mapped_database *
275 __nscd_get_map_ref (request_type type, const char *name,
276 struct locked_map_ptr *mapptr, int *gc_cyclep)
278 struct mapped_database *cur = mapptr->mapped;
279 if (cur == NO_MAPPING)
280 return cur;
282 int cnt = 0;
283 while (atomic_compare_and_exchange_val_acq (&mapptr->lock, 1, 0) != 0)
285 // XXX Best number of rounds?
286 if (++cnt > 5)
287 return NO_MAPPING;
289 atomic_delay ();
292 cur = mapptr->mapped;
294 if (__builtin_expect (cur != NO_MAPPING, 1))
296 /* If not mapped or timestamp not updated, request new map. */
297 if (cur == NULL
298 || (cur->head->nscd_certainly_running == 0
299 && cur->head->timestamp + MAPPING_TIMEOUT < time (NULL)))
300 cur = get_mapping (type, name, &mapptr->mapped);
302 if (__builtin_expect (cur != NO_MAPPING, 1))
304 if (__builtin_expect (((*gc_cyclep = cur->head->gc_cycle) & 1) != 0,
306 cur = NO_MAPPING;
307 else
308 atomic_increment (&cur->counter);
312 mapptr->lock = 0;
314 return cur;
318 const struct datahead *
319 __nscd_cache_search (request_type type, const char *key, size_t keylen,
320 const struct mapped_database *mapped)
322 unsigned long int hash = __nis_hash (key, keylen) % mapped->head->module;
324 ref_t work = mapped->head->array[hash];
325 while (work != ENDREF)
327 struct hashentry *here = (struct hashentry *) (mapped->data + work);
329 if (type == here->type && keylen == here->len
330 && memcmp (key, mapped->data + here->key, keylen) == 0)
332 /* We found the entry. Increment the appropriate counter. */
333 const struct datahead *dh
334 = (struct datahead *) (mapped->data + here->packet);
336 /* See whether we must ignore the entry or whether something
337 is wrong because garbage collection is in progress. */
338 if (dh->usable && ((char *) dh + dh->allocsize
339 <= (char *) mapped->head + mapped->mapsize))
340 return dh;
343 work = here->next;
346 return NULL;
350 /* Create a socket connected to a name. */
352 __nscd_open_socket (const char *key, size_t keylen, request_type type,
353 void *response, size_t responselen)
355 int saved_errno = errno;
357 int sock = open_socket ();
358 if (sock >= 0)
360 request_header req;
361 req.version = NSCD_VERSION;
362 req.type = type;
363 req.key_len = keylen;
365 struct iovec vec[2];
366 vec[0].iov_base = &req;
367 vec[0].iov_len = sizeof (request_header);
368 vec[1].iov_base = (void *) key;
369 vec[1].iov_len = keylen;
371 ssize_t nbytes = TEMP_FAILURE_RETRY (__writev (sock, vec, 2));
372 if (nbytes == (ssize_t) (sizeof (request_header) + keylen))
374 /* Wait for data. */
375 struct pollfd fds[1];
376 fds[0].fd = sock;
377 fds[0].events = POLLIN | POLLERR | POLLHUP;
378 if (__poll (fds, 1, 5 * 1000) > 0)
380 nbytes = TEMP_FAILURE_RETRY (__read (sock, response,
381 responselen));
382 if (nbytes == (ssize_t) responselen)
383 return sock;
387 close_not_cancel_no_status (sock);
390 __set_errno (saved_errno);
392 return -1;