Updated to fedora-glibc-20050208T0948
[glibc.git] / nscd / nscd_helper.c
blobea4fb968dbd61d2fb6dd1bf02474c45e18d0cd50
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 static int
38 open_socket (void)
40 int sock = __socket (PF_UNIX, SOCK_STREAM, 0);
41 if (sock < 0)
42 return -1;
44 /* Make socket non-blocking. */
45 int fl = __fcntl (sock, F_GETFL);
46 if (fl != -1)
47 __fcntl (sock, F_SETFL, fl | O_NONBLOCK);
49 struct sockaddr_un sun;
50 sun.sun_family = AF_UNIX;
51 strcpy (sun.sun_path, _PATH_NSCDSOCKET);
52 if (__connect (sock, (struct sockaddr *) &sun, sizeof (sun)) < 0
53 && errno != EINPROGRESS)
54 goto out;
56 struct pollfd fds[1];
57 fds[0].fd = sock;
58 fds[0].events = POLLOUT | POLLERR | POLLHUP;
59 if (__poll (fds, 1, 5 * 1000) > 0)
60 /* Success. We do not check for success of the connect call here.
61 If it failed, the following operations will fail. */
62 return sock;
64 out:
65 close_not_cancel_no_status (sock);
67 return -1;
71 void
72 __nscd_unmap (struct mapped_database *mapped)
74 assert (mapped->counter == 0);
75 __munmap ((void *) mapped->head, mapped->mapsize);
76 free (mapped);
80 /* Try to get a file descriptor for the shared meory segment
81 containing the database. */
82 static struct mapped_database *
83 get_mapping (request_type type, const char *key,
84 struct mapped_database **mappedp)
86 struct mapped_database *result = NO_MAPPING;
87 #ifdef SCM_RIGHTS
88 const size_t keylen = strlen (key) + 1;
89 char resdata[keylen];
90 int saved_errno = errno;
92 int mapfd = -1;
94 /* Send the request. */
95 struct iovec iov[2];
96 request_header req;
98 int sock = open_socket ();
99 if (sock < 0)
100 goto out;
102 req.version = NSCD_VERSION;
103 req.type = type;
104 req.key_len = keylen;
106 iov[0].iov_base = &req;
107 iov[0].iov_len = sizeof (req);
108 iov[1].iov_base = (void *) key;
109 iov[1].iov_len = keylen;
111 if (TEMP_FAILURE_RETRY (__writev (sock, iov, 2))
112 != iov[0].iov_len + iov[1].iov_len)
113 /* We cannot even write the request. */
114 goto out_close2;
116 /* Room for the data sent along with the file descriptor. We expect
117 the key name back. */
118 iov[0].iov_base = resdata;
119 iov[0].iov_len = keylen;
121 char buf[CMSG_SPACE (sizeof (int))];
122 struct msghdr msg = { .msg_iov = iov, .msg_iovlen = 1,
123 .msg_control = buf, .msg_controllen = sizeof (buf) };
124 struct cmsghdr *cmsg = CMSG_FIRSTHDR (&msg);
126 cmsg->cmsg_level = SOL_SOCKET;
127 cmsg->cmsg_type = SCM_RIGHTS;
128 cmsg->cmsg_len = CMSG_LEN (sizeof (int));
130 *(int *) CMSG_DATA (cmsg) = -1;
132 msg.msg_controllen = cmsg->cmsg_len;
134 struct pollfd fds[1];
135 fds[0].fd = sock;
136 fds[0].events = POLLIN | POLLERR | POLLHUP;
137 if (__poll (fds, 1, 5 * 1000) <= 0)
138 /* Failure or timeout. */
139 goto out_close2;
141 #ifndef MSG_NOSIGNAL
142 # define MSG_NOSIGNAL 0
143 #endif
144 if (TEMP_FAILURE_RETRY (__recvmsg (sock, &msg, MSG_NOSIGNAL)) != keylen)
145 goto out_close2;
147 mapfd = *(int *) CMSG_DATA (cmsg);
149 if (CMSG_FIRSTHDR (&msg)->cmsg_len != CMSG_LEN (sizeof (int)))
150 goto out_close;
152 struct stat64 st;
153 if (strcmp (resdata, key) != 0
154 || fstat64 (mapfd, &st) != 0
155 || st.st_size < sizeof (struct database_pers_head))
156 goto out_close;
158 struct database_pers_head head;
159 if (TEMP_FAILURE_RETRY (__pread (mapfd, &head, sizeof (head), 0))
160 != sizeof (head))
161 goto out_close;
163 if (head.version != DB_VERSION || head.header_size != sizeof (head)
164 /* This really should not happen but who knows, maybe the update
165 thread got stuck. */
166 || (! head.nscd_certainly_running
167 && head.timestamp + MAPPING_TIMEOUT < time (NULL)))
168 goto out_close;
170 size_t size = (sizeof (head) + roundup (head.module * sizeof (ref_t), ALIGN)
171 + head.data_size);
173 if (st.st_size < size)
174 goto out_close;
176 /* The file is large enough, map it now. */
177 void *mapping = __mmap (NULL, size, PROT_READ, MAP_SHARED, mapfd, 0);
178 if (mapping != MAP_FAILED)
180 /* Allocate a record for the mapping. */
181 struct mapped_database *newp;
183 newp = malloc (sizeof (*newp));
184 if (newp == NULL)
186 /* Ugh, after all we went through the memory allocation failed. */
187 __munmap (result, size);
188 goto out_close;
191 newp->head = mapping;
192 newp->data = ((char *) mapping + head.header_size
193 + roundup (head.module * sizeof (ref_t), ALIGN));
194 newp->mapsize = size;
195 /* Set counter to 1 to show it is usable. */
196 newp->counter = 1;
198 result = newp;
201 out_close:
202 __close (mapfd);
203 out_close2:
204 __close (sock);
205 out:
206 __set_errno (saved_errno);
207 #endif /* SCM_RIGHTS */
209 struct mapped_database *oldval = *mappedp;
210 *mappedp = result;
212 if (oldval != NULL && atomic_decrement_val (&oldval->counter) == 0)
213 __nscd_unmap (oldval);
215 return result;
219 struct mapped_database *
220 __nscd_get_map_ref (request_type type, const char *name,
221 struct locked_map_ptr *mapptr, int *gc_cyclep)
223 struct mapped_database *cur = mapptr->mapped;
224 if (cur == NO_MAPPING)
225 return cur;
227 int cnt = 0;
228 while (atomic_compare_and_exchange_val_acq (&mapptr->lock, 1, 0) != 0)
230 // XXX Best number of rounds?
231 if (++cnt > 5)
232 return NO_MAPPING;
234 atomic_delay ();
237 cur = mapptr->mapped;
239 if (__builtin_expect (cur != NO_MAPPING, 1))
241 /* If not mapped or timestamp not updated, request new map. */
242 if (cur == NULL
243 || (cur->head->nscd_certainly_running == 0
244 && cur->head->timestamp + MAPPING_TIMEOUT < time (NULL)))
245 cur = get_mapping (type, name, &mapptr->mapped);
247 if (__builtin_expect (cur != NO_MAPPING, 1))
249 if (__builtin_expect (((*gc_cyclep = cur->head->gc_cycle) & 1) != 0,
251 cur = NO_MAPPING;
252 else
253 atomic_increment (&cur->counter);
257 mapptr->lock = 0;
259 return cur;
263 const struct datahead *
264 __nscd_cache_search (request_type type, const char *key, size_t keylen,
265 const struct mapped_database *mapped)
267 unsigned long int hash = __nis_hash (key, keylen) % mapped->head->module;
269 ref_t work = mapped->head->array[hash];
270 while (work != ENDREF)
272 struct hashentry *here = (struct hashentry *) (mapped->data + work);
274 if (type == here->type && keylen == here->len
275 && memcmp (key, mapped->data + here->key, keylen) == 0)
277 /* We found the entry. Increment the appropriate counter. */
278 const struct datahead *dh
279 = (struct datahead *) (mapped->data + here->packet);
281 /* See whether we must ignore the entry or whether something
282 is wrong because garbage collection is in progress. */
283 if (dh->usable && ((char *) dh + dh->allocsize
284 <= (char *) mapped->head + mapped->mapsize))
285 return dh;
288 work = here->next;
291 return NULL;
295 /* Create a socket connected to a name. */
297 __nscd_open_socket (const char *key, size_t keylen, request_type type,
298 void *response, size_t responselen)
300 int saved_errno = errno;
302 int sock = open_socket ();
303 if (sock >= 0)
305 request_header req;
306 req.version = NSCD_VERSION;
307 req.type = type;
308 req.key_len = keylen;
310 struct iovec vec[2];
311 vec[0].iov_base = &req;
312 vec[0].iov_len = sizeof (request_header);
313 vec[1].iov_base = (void *) key;
314 vec[1].iov_len = keylen;
316 ssize_t nbytes = TEMP_FAILURE_RETRY (__writev (sock, vec, 2));
317 if (nbytes == (ssize_t) (sizeof (request_header) + keylen))
319 /* Wait for data. */
320 struct pollfd fds[1];
321 fds[0].fd = sock;
322 fds[0].events = POLLIN | POLLERR | POLLHUP;
323 if (__poll (fds, 1, 5 * 1000) > 0)
325 nbytes = TEMP_FAILURE_RETRY (__read (sock, response,
326 responselen));
327 if (nbytes == (ssize_t) responselen)
328 return sock;
332 close_not_cancel_no_status (sock);
335 __set_errno (saved_errno);
337 return -1;