]> git.ipfire.org Git - thirdparty/glibc.git/blob - nscd/nscd_helper.c
Update.
[thirdparty/glibc.git] / nscd / nscd_helper.c
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.
4
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.
9
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.
14
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. */
19
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>
33
34 #include "nscd-client.h"
35
36
37 static int
38 open_socket (void)
39 {
40 int sock = __socket (PF_UNIX, SOCK_STREAM, 0);
41 if (sock < 0)
42 return -1;
43
44 /* Make socket non-blocking. */
45 int fl = __fcntl (sock, F_GETFL);
46 if (fl != -1)
47 __fcntl (sock, F_SETFL, fl | O_NONBLOCK);
48
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;
55
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;
63
64 out:
65 close_not_cancel_no_status (sock);
66
67 return -1;
68 }
69
70
71 void
72 __nscd_unmap (struct mapped_database *mapped)
73 {
74 assert (mapped->counter == 0);
75 __munmap ((void *) mapped->head, mapped->mapsize);
76 free (mapped);
77 }
78
79
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)
85 {
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;
91
92 int mapfd = -1;
93
94 /* Send the request. */
95 struct iovec iov[2];
96 request_header req;
97
98 int sock = open_socket ();
99 if (sock < 0)
100 goto out;
101
102 req.version = NSCD_VERSION;
103 req.type = type;
104 req.key_len = keylen;
105
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;
110
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;
115
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;
120
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);
125
126 cmsg->cmsg_level = SOL_SOCKET;
127 cmsg->cmsg_type = SCM_RIGHTS;
128 cmsg->cmsg_len = CMSG_LEN (sizeof (int));
129
130 *(int *) CMSG_DATA (cmsg) = -1;
131
132 msg.msg_controllen = cmsg->cmsg_len;
133
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;
140
141 if (TEMP_FAILURE_RETRY (__recvmsg (sock, &msg, 0)) != keylen)
142 goto out_close2;
143
144 mapfd = *(int *) CMSG_DATA (cmsg);
145
146 if (CMSG_FIRSTHDR (&msg)->cmsg_len != CMSG_LEN (sizeof (int)))
147 goto out_close;
148
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;
154
155 struct database_pers_head head;
156 if (TEMP_FAILURE_RETRY (__pread (mapfd, &head, sizeof (head), 0))
157 != sizeof (head))
158 goto out_close;
159
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.timestamp + MAPPING_TIMEOUT < time (NULL))
164 goto out_close;
165
166 size_t size = (sizeof (head) + roundup (head.module * sizeof (ref_t), ALIGN)
167 + head.data_size);
168
169 if (st.st_size < size)
170 goto out_close;
171
172 /* The file is large enough, map it now. */
173 void *mapping = __mmap (NULL, size, PROT_READ, MAP_SHARED, mapfd, 0);
174 if (mapping != MAP_FAILED)
175 {
176 /* Allocate a record for the mapping. */
177 struct mapped_database *newp;
178
179 newp = malloc (sizeof (*newp));
180 if (newp == NULL)
181 {
182 /* Ugh, after all we went through the memory allocation failed. */
183 __munmap (result, size);
184 goto out_close;
185 }
186
187 newp->head = mapping;
188 newp->data = ((char *) mapping + head.header_size
189 + roundup (head.module * sizeof (ref_t), ALIGN));
190 newp->mapsize = size;
191 /* Set counter to 1 to show it is usable. */
192 newp->counter = 1;
193
194 result = newp;
195 }
196
197 out_close:
198 __close (mapfd);
199 out_close2:
200 __close (sock);
201 out:
202 __set_errno (saved_errno);
203 #endif /* SCM_RIGHTS */
204
205 struct mapped_database *oldval = *mappedp;
206 *mappedp = result;
207
208 if (oldval != NULL && atomic_decrement_val (&oldval->counter) == 0)
209 __nscd_unmap (oldval);
210
211 return result;
212 }
213
214
215 struct mapped_database *
216 __nscd_get_map_ref (request_type type, const char *name,
217 struct locked_map_ptr *mapptr, int *gc_cyclep)
218 {
219 struct mapped_database *cur = mapptr->mapped;
220 if (cur == NO_MAPPING)
221 return cur;
222
223 int cnt = 0;
224 while (atomic_compare_and_exchange_val_acq (&mapptr->lock, 1, 0) != 0)
225 {
226 // XXX Best number of rounds?
227 if (++cnt > 5)
228 return NO_MAPPING;
229
230 atomic_delay ();
231 }
232
233 cur = mapptr->mapped;
234
235 if (__builtin_expect (cur != NO_MAPPING, 1))
236 {
237 /* If not mapped or timestamp not updated, request new map. */
238 if (cur == NULL
239 || (cur->head->nscd_certainly_running == 0
240 && cur->head->timestamp + MAPPING_TIMEOUT < time (NULL)))
241 cur = get_mapping (type, name, &mapptr->mapped);
242
243 if (__builtin_expect (cur != NO_MAPPING, 1))
244 {
245 if (__builtin_expect (((*gc_cyclep = cur->head->gc_cycle) & 1) != 0,
246 0))
247 cur = NO_MAPPING;
248 else
249 atomic_increment (&cur->counter);
250 }
251 }
252
253 mapptr->lock = 0;
254
255 return cur;
256 }
257
258
259 const struct datahead *
260 __nscd_cache_search (request_type type, const char *key, size_t keylen,
261 const struct mapped_database *mapped)
262 {
263 unsigned long int hash = __nis_hash (key, keylen) % mapped->head->module;
264
265 ref_t work = mapped->head->array[hash];
266 while (work != ENDREF)
267 {
268 struct hashentry *here = (struct hashentry *) (mapped->data + work);
269
270 if (type == here->type && keylen == here->len
271 && memcmp (key, mapped->data + here->key, keylen) == 0)
272 {
273 /* We found the entry. Increment the appropriate counter. */
274 const struct datahead *dh
275 = (struct datahead *) (mapped->data + here->packet);
276
277 /* See whether we must ignore the entry or whether something
278 is wrong because garbage collection is in progress. */
279 if (dh->usable && ((char *) dh + dh->allocsize
280 <= (char *) mapped->head + mapped->mapsize))
281 return dh;
282 }
283
284 work = here->next;
285 }
286
287 return NULL;
288 }
289
290
291 /* Create a socket connected to a name. */
292 int
293 __nscd_open_socket (const char *key, size_t keylen, request_type type,
294 void *response, size_t responselen)
295 {
296 int saved_errno = errno;
297
298 int sock = open_socket ();
299 if (sock >= 0)
300 {
301 request_header req;
302 req.version = NSCD_VERSION;
303 req.type = type;
304 req.key_len = keylen;
305
306 struct iovec vec[2];
307 vec[0].iov_base = &req;
308 vec[0].iov_len = sizeof (request_header);
309 vec[1].iov_base = (void *) key;
310 vec[1].iov_len = keylen;
311
312 ssize_t nbytes = TEMP_FAILURE_RETRY (__writev (sock, vec, 2));
313 if (nbytes == (ssize_t) (sizeof (request_header) + keylen))
314 {
315 /* Wait for data. */
316 struct pollfd fds[1];
317 fds[0].fd = sock;
318 fds[0].events = POLLIN | POLLERR | POLLHUP;
319 if (__poll (fds, 1, 5 * 1000) > 0)
320 {
321 nbytes = TEMP_FAILURE_RETRY (__read (sock, response,
322 responselen));
323 if (nbytes == (ssize_t) responselen)
324 return sock;
325 }
326 }
327
328 close_not_cancel_no_status (sock);
329 }
330
331 __set_errno (saved_errno);
332
333 return -1;
334 }