1 /* Copyright (C) 1998-2002,2003,2004,2005,2006 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
28 #include <sys/socket.h>
33 #include <not-cancel.h>
34 #include <nis/rpcsvc/nis.h>
36 #include "nscd-client.h"
40 __readall (int fd
, void *buf
, size_t len
)
46 ret
= TEMP_FAILURE_RETRY (__read (fd
, buf
, n
));
49 buf
= (char *) buf
+ ret
;
53 return ret
< 0 ? ret
: len
- n
;
58 __readvall (int fd
, const struct iovec
*iov
, int iovcnt
)
60 ssize_t ret
= TEMP_FAILURE_RETRY (__readv (fd
, iov
, iovcnt
));
65 for (int i
= 0; i
< iovcnt
; ++i
)
66 total
+= iov
[i
].iov_len
;
70 struct iovec iov_buf
[iovcnt
];
73 struct iovec
*iovp
= memcpy (iov_buf
, iov
, iovcnt
* sizeof (*iov
));
76 while (iovp
->iov_len
<= r
)
82 iovp
->iov_base
= (char *) iovp
->iov_base
+ r
;
84 r
= TEMP_FAILURE_RETRY (__readv (fd
, iovp
, iovcnt
));
100 int sock
= __socket (PF_UNIX
, SOCK_STREAM
, 0);
104 /* Make socket non-blocking. */
105 int fl
= __fcntl (sock
, F_GETFL
);
107 __fcntl (sock
, F_SETFL
, fl
| O_NONBLOCK
);
109 struct sockaddr_un sun
;
110 sun
.sun_family
= AF_UNIX
;
111 strcpy (sun
.sun_path
, _PATH_NSCDSOCKET
);
112 if (__connect (sock
, (struct sockaddr
*) &sun
, sizeof (sun
)) < 0
113 && errno
!= EINPROGRESS
)
116 struct pollfd fds
[1];
118 fds
[0].events
= POLLOUT
| POLLERR
| POLLHUP
;
119 if (__poll (fds
, 1, 5 * 1000) > 0)
120 /* Success. We do not check for success of the connect call here.
121 If it failed, the following operations will fail. */
125 close_not_cancel_no_status (sock
);
132 __nscd_unmap (struct mapped_database
*mapped
)
134 assert (mapped
->counter
== 0);
135 __munmap ((void *) mapped
->head
, mapped
->mapsize
);
141 wait_on_socket (int sock
)
143 struct pollfd fds
[1];
145 fds
[0].events
= POLLIN
| POLLERR
| POLLHUP
;
146 int n
= __poll (fds
, 1, 5 * 1000);
147 if (n
== -1 && __builtin_expect (errno
== EINTR
, 0))
149 /* Handle the case where the poll() call is interrupted by a
150 signal. We cannot just use TEMP_FAILURE_RETRY since it might
151 lead to infinite loops. */
153 (void) __gettimeofday (&now
, NULL
);
154 long int end
= (now
.tv_sec
+ 5) * 1000 + (now
.tv_usec
+ 500) / 1000;
157 long int timeout
= end
- (now
.tv_sec
* 1000
158 + (now
.tv_usec
+ 500) / 1000);
159 n
= __poll (fds
, 1, timeout
);
160 if (n
!= -1 || errno
!= EINTR
)
162 (void) __gettimeofday (&now
, NULL
);
170 /* Try to get a file descriptor for the shared meory segment
171 containing the database. */
172 static struct mapped_database
*
173 get_mapping (request_type type
, const char *key
,
174 struct mapped_database
**mappedp
)
176 struct mapped_database
*result
= NO_MAPPING
;
178 const size_t keylen
= strlen (key
) + 1;
179 char resdata
[keylen
];
180 int saved_errno
= errno
;
184 /* Send the request. */
188 int sock
= open_socket ();
192 req
.version
= NSCD_VERSION
;
194 req
.key_len
= keylen
;
196 iov
[0].iov_base
= &req
;
197 iov
[0].iov_len
= sizeof (req
);
198 iov
[1].iov_base
= (void *) key
;
199 iov
[1].iov_len
= keylen
;
201 if (__builtin_expect (TEMP_FAILURE_RETRY (__writev (sock
, iov
, 2))
202 != iov
[0].iov_len
+ iov
[1].iov_len
, 0))
203 /* We cannot even write the request. */
206 /* Room for the data sent along with the file descriptor. We expect
207 the key name back. */
208 iov
[0].iov_base
= resdata
;
209 iov
[0].iov_len
= keylen
;
214 char bytes
[CMSG_SPACE (sizeof (int))];
216 struct msghdr msg
= { .msg_iov
= iov
, .msg_iovlen
= 1,
217 .msg_control
= buf
.bytes
,
218 .msg_controllen
= sizeof (buf
) };
219 struct cmsghdr
*cmsg
= CMSG_FIRSTHDR (&msg
);
221 cmsg
->cmsg_level
= SOL_SOCKET
;
222 cmsg
->cmsg_type
= SCM_RIGHTS
;
223 cmsg
->cmsg_len
= CMSG_LEN (sizeof (int));
225 /* This access is well-aligned since BUF is correctly aligned for an
226 int and CMSG_DATA preserves this alignment. */
227 *(int *) CMSG_DATA (cmsg
) = -1;
229 msg
.msg_controllen
= cmsg
->cmsg_len
;
231 if (wait_on_socket (sock
) <= 0)
234 # ifndef MSG_NOSIGNAL
235 # define MSG_NOSIGNAL 0
237 if (__builtin_expect (TEMP_FAILURE_RETRY (__recvmsg (sock
, &msg
,
242 mapfd
= *(int *) CMSG_DATA (cmsg
);
244 if (__builtin_expect (CMSG_FIRSTHDR (&msg
)->cmsg_len
245 != CMSG_LEN (sizeof (int)), 0))
249 if (__builtin_expect (strcmp (resdata
, key
) != 0, 0)
250 || __builtin_expect (fstat64 (mapfd
, &st
) != 0, 0)
251 || __builtin_expect (st
.st_size
< sizeof (struct database_pers_head
), 0))
254 struct database_pers_head head
;
255 if (__builtin_expect (TEMP_FAILURE_RETRY (__pread (mapfd
, &head
,
257 != sizeof (head
), 0))
260 if (__builtin_expect (head
.version
!= DB_VERSION
, 0)
261 || __builtin_expect (head
.header_size
!= sizeof (head
), 0)
262 /* This really should not happen but who knows, maybe the update
264 || __builtin_expect (! head
.nscd_certainly_running
265 && head
.timestamp
+ MAPPING_TIMEOUT
< time (NULL
),
269 size_t size
= (sizeof (head
) + roundup (head
.module
* sizeof (ref_t
), ALIGN
)
272 if (__builtin_expect (st
.st_size
< size
, 0))
275 /* The file is large enough, map it now. */
276 void *mapping
= __mmap (NULL
, size
, PROT_READ
, MAP_SHARED
, mapfd
, 0);
277 if (__builtin_expect (mapping
!= MAP_FAILED
, 1))
279 /* Allocate a record for the mapping. */
280 struct mapped_database
*newp
= malloc (sizeof (*newp
));
283 /* Ugh, after all we went through the memory allocation failed. */
284 __munmap (mapping
, size
);
288 newp
->head
= mapping
;
289 newp
->data
= ((char *) mapping
+ head
.header_size
290 + roundup (head
.module
* sizeof (ref_t
), ALIGN
));
291 newp
->mapsize
= size
;
292 /* Set counter to 1 to show it is usable. */
303 __set_errno (saved_errno
);
304 #endif /* SCM_RIGHTS */
306 struct mapped_database
*oldval
= *mappedp
;
309 if (oldval
!= NULL
&& atomic_decrement_val (&oldval
->counter
) == 0)
310 __nscd_unmap (oldval
);
316 struct mapped_database
*
317 __nscd_get_map_ref (request_type type
, const char *name
,
318 struct locked_map_ptr
*mapptr
, int *gc_cyclep
)
320 struct mapped_database
*cur
= mapptr
->mapped
;
321 if (cur
== NO_MAPPING
)
325 while (atomic_compare_and_exchange_val_acq (&mapptr
->lock
, 1, 0) != 0)
327 // XXX Best number of rounds?
334 cur
= mapptr
->mapped
;
336 if (__builtin_expect (cur
!= NO_MAPPING
, 1))
338 /* If not mapped or timestamp not updated, request new map. */
340 || (cur
->head
->nscd_certainly_running
== 0
341 && cur
->head
->timestamp
+ MAPPING_TIMEOUT
< time (NULL
)))
342 cur
= get_mapping (type
, name
, &mapptr
->mapped
);
344 if (__builtin_expect (cur
!= NO_MAPPING
, 1))
346 if (__builtin_expect (((*gc_cyclep
= cur
->head
->gc_cycle
) & 1) != 0,
350 atomic_increment (&cur
->counter
);
360 const struct datahead
*
361 __nscd_cache_search (request_type type
, const char *key
, size_t keylen
,
362 const struct mapped_database
*mapped
)
364 unsigned long int hash
= __nis_hash (key
, keylen
) % mapped
->head
->module
;
366 ref_t work
= mapped
->head
->array
[hash
];
367 while (work
!= ENDREF
)
369 struct hashentry
*here
= (struct hashentry
*) (mapped
->data
+ work
);
371 if (type
== here
->type
&& keylen
== here
->len
372 && memcmp (key
, mapped
->data
+ here
->key
, keylen
) == 0)
374 /* We found the entry. Increment the appropriate counter. */
375 const struct datahead
*dh
376 = (struct datahead
*) (mapped
->data
+ here
->packet
);
378 /* See whether we must ignore the entry or whether something
379 is wrong because garbage collection is in progress. */
380 if (dh
->usable
&& ((char *) dh
+ dh
->allocsize
381 <= (char *) mapped
->head
+ mapped
->mapsize
))
392 /* Create a socket connected to a name. */
394 __nscd_open_socket (const char *key
, size_t keylen
, request_type type
,
395 void *response
, size_t responselen
)
397 int saved_errno
= errno
;
399 int sock
= open_socket ();
403 req
.version
= NSCD_VERSION
;
405 req
.key_len
= keylen
;
408 vec
[0].iov_base
= &req
;
409 vec
[0].iov_len
= sizeof (request_header
);
410 vec
[1].iov_base
= (void *) key
;
411 vec
[1].iov_len
= keylen
;
413 ssize_t nbytes
= TEMP_FAILURE_RETRY (__writev (sock
, vec
, 2));
414 if (nbytes
== (ssize_t
) (sizeof (request_header
) + keylen
)
416 && wait_on_socket (sock
) > 0)
418 nbytes
= TEMP_FAILURE_RETRY (__read (sock
, response
, responselen
));
419 if (nbytes
== (ssize_t
) responselen
)
423 close_not_cancel_no_status (sock
);
426 __set_errno (saved_errno
);