* sunrpc/svc_tcp.c (svctcp_create): Call listen with SOMAXCONN
[glibc.git] / nscd / nscd_helper.c
blob0e16cb8aeb6b5b21fd04898c67a4c0706b641c2d
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 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 if (TEMP_FAILURE_RETRY (__recvmsg (sock, &msg, 0)) != keylen)
142 goto out_close2;
144 mapfd = *(int *) CMSG_DATA (cmsg);
146 if (CMSG_FIRSTHDR (&msg)->cmsg_len != CMSG_LEN (sizeof (int)))
147 goto out_close;
149 struct stat64 st;
150 if (strcmp (resdata, key) != 0
151 || fstat64 (mapfd, &st) != 0
152 || st.st_size < sizeof (struct database_pers_head))
153 goto out_close;
155 struct database_pers_head head;
156 if (TEMP_FAILURE_RETRY (__pread (mapfd, &head, sizeof (head), 0))
157 != sizeof (head))
158 goto out_close;
160 if (head.version != DB_VERSION || head.header_size != sizeof (head)
161 /* This really should not happen but who knows, maybe the update
162 thread got stuck. */
163 || (! head.nscd_certainly_running
164 && head.timestamp + MAPPING_TIMEOUT < time (NULL)))
165 goto out_close;
167 size_t size = (sizeof (head) + roundup (head.module * sizeof (ref_t), ALIGN)
168 + head.data_size);
170 if (st.st_size < size)
171 goto out_close;
173 /* The file is large enough, map it now. */
174 void *mapping = __mmap (NULL, size, PROT_READ, MAP_SHARED, mapfd, 0);
175 if (mapping != MAP_FAILED)
177 /* Allocate a record for the mapping. */
178 struct mapped_database *newp;
180 newp = malloc (sizeof (*newp));
181 if (newp == NULL)
183 /* Ugh, after all we went through the memory allocation failed. */
184 __munmap (result, size);
185 goto out_close;
188 newp->head = mapping;
189 newp->data = ((char *) mapping + head.header_size
190 + roundup (head.module * sizeof (ref_t), ALIGN));
191 newp->mapsize = size;
192 /* Set counter to 1 to show it is usable. */
193 newp->counter = 1;
195 result = newp;
198 out_close:
199 __close (mapfd);
200 out_close2:
201 __close (sock);
202 out:
203 __set_errno (saved_errno);
204 #endif /* SCM_RIGHTS */
206 struct mapped_database *oldval = *mappedp;
207 *mappedp = result;
209 if (oldval != NULL && atomic_decrement_val (&oldval->counter) == 0)
210 __nscd_unmap (oldval);
212 return result;
216 struct mapped_database *
217 __nscd_get_map_ref (request_type type, const char *name,
218 struct locked_map_ptr *mapptr, int *gc_cyclep)
220 struct mapped_database *cur = mapptr->mapped;
221 if (cur == NO_MAPPING)
222 return cur;
224 int cnt = 0;
225 while (atomic_compare_and_exchange_val_acq (&mapptr->lock, 1, 0) != 0)
227 // XXX Best number of rounds?
228 if (++cnt > 5)
229 return NO_MAPPING;
231 atomic_delay ();
234 cur = mapptr->mapped;
236 if (__builtin_expect (cur != NO_MAPPING, 1))
238 /* If not mapped or timestamp not updated, request new map. */
239 if (cur == NULL
240 || (cur->head->nscd_certainly_running == 0
241 && cur->head->timestamp + MAPPING_TIMEOUT < time (NULL)))
242 cur = get_mapping (type, name, &mapptr->mapped);
244 if (__builtin_expect (cur != NO_MAPPING, 1))
246 if (__builtin_expect (((*gc_cyclep = cur->head->gc_cycle) & 1) != 0,
248 cur = NO_MAPPING;
249 else
250 atomic_increment (&cur->counter);
254 mapptr->lock = 0;
256 return cur;
260 const struct datahead *
261 __nscd_cache_search (request_type type, const char *key, size_t keylen,
262 const struct mapped_database *mapped)
264 unsigned long int hash = __nis_hash (key, keylen) % mapped->head->module;
266 ref_t work = mapped->head->array[hash];
267 while (work != ENDREF)
269 struct hashentry *here = (struct hashentry *) (mapped->data + work);
271 if (type == here->type && keylen == here->len
272 && memcmp (key, mapped->data + here->key, keylen) == 0)
274 /* We found the entry. Increment the appropriate counter. */
275 const struct datahead *dh
276 = (struct datahead *) (mapped->data + here->packet);
278 /* See whether we must ignore the entry or whether something
279 is wrong because garbage collection is in progress. */
280 if (dh->usable && ((char *) dh + dh->allocsize
281 <= (char *) mapped->head + mapped->mapsize))
282 return dh;
285 work = here->next;
288 return NULL;
292 /* Create a socket connected to a name. */
294 __nscd_open_socket (const char *key, size_t keylen, request_type type,
295 void *response, size_t responselen)
297 int saved_errno = errno;
299 int sock = open_socket ();
300 if (sock >= 0)
302 request_header req;
303 req.version = NSCD_VERSION;
304 req.type = type;
305 req.key_len = keylen;
307 struct iovec vec[2];
308 vec[0].iov_base = &req;
309 vec[0].iov_len = sizeof (request_header);
310 vec[1].iov_base = (void *) key;
311 vec[1].iov_len = keylen;
313 ssize_t nbytes = TEMP_FAILURE_RETRY (__writev (sock, vec, 2));
314 if (nbytes == (ssize_t) (sizeof (request_header) + keylen))
316 /* Wait for data. */
317 struct pollfd fds[1];
318 fds[0].fd = sock;
319 fds[0].events = POLLIN | POLLERR | POLLHUP;
320 if (__poll (fds, 1, 5 * 1000) > 0)
322 nbytes = TEMP_FAILURE_RETRY (__read (sock, response,
323 responselen));
324 if (nbytes == (ssize_t) responselen)
325 return sock;
329 close_not_cancel_no_status (sock);
332 __set_errno (saved_errno);
334 return -1;