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
27 #include <sys/socket.h>
31 #include <not-cancel.h>
32 #include <nis/rpcsvc/nis.h>
34 #include "nscd-client.h"
38 __readall (int fd
, void *buf
, size_t len
)
44 ret
= TEMP_FAILURE_RETRY (__read (fd
, buf
, n
));
47 buf
= (char *) buf
+ ret
;
51 return ret
< 0 ? ret
: len
- n
;
56 __readvall (int fd
, const struct iovec
*iov
, int iovcnt
)
58 ssize_t ret
= TEMP_FAILURE_RETRY (__readv (fd
, iov
, iovcnt
));
63 for (int i
= 0; i
< iovcnt
; ++i
)
64 total
+= iov
[i
].iov_len
;
68 struct iovec iov_buf
[iovcnt
];
71 struct iovec
*iovp
= memcpy (iov_buf
, iov
, iovcnt
* sizeof (*iov
));
74 while (iovp
->iov_len
<= r
)
80 iovp
->iov_base
= (char *) iovp
->iov_base
+ r
;
82 r
= TEMP_FAILURE_RETRY (__readv (fd
, iovp
, iovcnt
));
98 int sock
= __socket (PF_UNIX
, SOCK_STREAM
, 0);
102 /* Make socket non-blocking. */
103 int fl
= __fcntl (sock
, F_GETFL
);
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
)
114 struct pollfd fds
[1];
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. */
123 close_not_cancel_no_status (sock
);
130 __nscd_unmap (struct mapped_database
*mapped
)
132 assert (mapped
->counter
== 0);
133 __munmap ((void *) mapped
->head
, mapped
->mapsize
);
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
;
146 const size_t keylen
= strlen (key
) + 1;
147 char resdata
[keylen
];
148 int saved_errno
= errno
;
152 /* Send the request. */
156 int sock
= open_socket ();
160 req
.version
= NSCD_VERSION
;
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. */
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];
194 fds
[0].events
= POLLIN
| POLLERR
| POLLHUP
;
195 if (__poll (fds
, 1, 5 * 1000) <= 0)
196 /* Failure or timeout. */
200 # define MSG_NOSIGNAL 0
202 if (TEMP_FAILURE_RETRY (__recvmsg (sock
, &msg
, MSG_NOSIGNAL
)) != keylen
)
205 mapfd
= *(int *) CMSG_DATA (cmsg
);
207 if (CMSG_FIRSTHDR (&msg
)->cmsg_len
!= CMSG_LEN (sizeof (int)))
211 if (strcmp (resdata
, key
) != 0
212 || fstat64 (mapfd
, &st
) != 0
213 || st
.st_size
< sizeof (struct database_pers_head
))
216 struct database_pers_head head
;
217 if (TEMP_FAILURE_RETRY (__pread (mapfd
, &head
, sizeof (head
), 0))
221 if (head
.version
!= DB_VERSION
|| head
.header_size
!= sizeof (head
)
222 /* This really should not happen but who knows, maybe the update
224 || (! head
.nscd_certainly_running
225 && head
.timestamp
+ MAPPING_TIMEOUT
< time (NULL
)))
228 size_t size
= (sizeof (head
) + roundup (head
.module
* sizeof (ref_t
), ALIGN
)
231 if (st
.st_size
< size
)
234 /* The file is large enough, map it now. */
235 void *mapping
= __mmap (NULL
, size
, PROT_READ
, MAP_SHARED
, mapfd
, 0);
236 if (mapping
!= MAP_FAILED
)
238 /* Allocate a record for the mapping. */
239 struct mapped_database
*newp
;
241 newp
= malloc (sizeof (*newp
));
244 /* Ugh, after all we went through the memory allocation failed. */
245 __munmap (result
, size
);
249 newp
->head
= mapping
;
250 newp
->data
= ((char *) mapping
+ head
.header_size
251 + roundup (head
.module
* sizeof (ref_t
), ALIGN
));
252 newp
->mapsize
= size
;
253 /* Set counter to 1 to show it is usable. */
264 __set_errno (saved_errno
);
265 #endif /* SCM_RIGHTS */
267 struct mapped_database
*oldval
= *mappedp
;
270 if (oldval
!= NULL
&& atomic_decrement_val (&oldval
->counter
) == 0)
271 __nscd_unmap (oldval
);
277 struct mapped_database
*
278 __nscd_get_map_ref (request_type type
, const char *name
,
279 struct locked_map_ptr
*mapptr
, int *gc_cyclep
)
281 struct mapped_database
*cur
= mapptr
->mapped
;
282 if (cur
== NO_MAPPING
)
286 while (atomic_compare_and_exchange_val_acq (&mapptr
->lock
, 1, 0) != 0)
288 // XXX Best number of rounds?
295 cur
= mapptr
->mapped
;
297 if (__builtin_expect (cur
!= NO_MAPPING
, 1))
299 /* If not mapped or timestamp not updated, request new map. */
301 || (cur
->head
->nscd_certainly_running
== 0
302 && cur
->head
->timestamp
+ MAPPING_TIMEOUT
< time (NULL
)))
303 cur
= get_mapping (type
, name
, &mapptr
->mapped
);
305 if (__builtin_expect (cur
!= NO_MAPPING
, 1))
307 if (__builtin_expect (((*gc_cyclep
= cur
->head
->gc_cycle
) & 1) != 0,
311 atomic_increment (&cur
->counter
);
321 const struct datahead
*
322 __nscd_cache_search (request_type type
, const char *key
, size_t keylen
,
323 const struct mapped_database
*mapped
)
325 unsigned long int hash
= __nis_hash (key
, keylen
) % mapped
->head
->module
;
327 ref_t work
= mapped
->head
->array
[hash
];
328 while (work
!= ENDREF
)
330 struct hashentry
*here
= (struct hashentry
*) (mapped
->data
+ work
);
332 if (type
== here
->type
&& keylen
== here
->len
333 && memcmp (key
, mapped
->data
+ here
->key
, keylen
) == 0)
335 /* We found the entry. Increment the appropriate counter. */
336 const struct datahead
*dh
337 = (struct datahead
*) (mapped
->data
+ here
->packet
);
339 /* See whether we must ignore the entry or whether something
340 is wrong because garbage collection is in progress. */
341 if (dh
->usable
&& ((char *) dh
+ dh
->allocsize
342 <= (char *) mapped
->head
+ mapped
->mapsize
))
353 /* Create a socket connected to a name. */
355 __nscd_open_socket (const char *key
, size_t keylen
, request_type type
,
356 void *response
, size_t responselen
)
358 int saved_errno
= errno
;
360 int sock
= open_socket ();
364 req
.version
= NSCD_VERSION
;
366 req
.key_len
= keylen
;
369 vec
[0].iov_base
= &req
;
370 vec
[0].iov_len
= sizeof (request_header
);
371 vec
[1].iov_base
= (void *) key
;
372 vec
[1].iov_len
= keylen
;
374 ssize_t nbytes
= TEMP_FAILURE_RETRY (__writev (sock
, vec
, 2));
375 if (nbytes
== (ssize_t
) (sizeof (request_header
) + keylen
))
378 struct pollfd fds
[1];
380 fds
[0].events
= POLLIN
| POLLERR
| POLLHUP
;
381 if (__poll (fds
, 1, 5 * 1000) > 0)
383 nbytes
= TEMP_FAILURE_RETRY (__read (sock
, response
,
385 if (nbytes
== (ssize_t
) responselen
)
390 close_not_cancel_no_status (sock
);
393 __set_errno (saved_errno
);