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
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. */
199 if (TEMP_FAILURE_RETRY (__recvmsg (sock
, &msg
, 0)) != keylen
)
202 mapfd
= *(int *) CMSG_DATA (cmsg
);
204 if (CMSG_FIRSTHDR (&msg
)->cmsg_len
!= CMSG_LEN (sizeof (int)))
208 if (strcmp (resdata
, key
) != 0
209 || fstat64 (mapfd
, &st
) != 0
210 || st
.st_size
< sizeof (struct database_pers_head
))
213 struct database_pers_head head
;
214 if (TEMP_FAILURE_RETRY (__pread (mapfd
, &head
, sizeof (head
), 0))
218 if (head
.version
!= DB_VERSION
|| head
.header_size
!= sizeof (head
)
219 /* This really should not happen but who knows, maybe the update
221 || (! head
.nscd_certainly_running
222 && head
.timestamp
+ MAPPING_TIMEOUT
< time (NULL
)))
225 size_t size
= (sizeof (head
) + roundup (head
.module
* sizeof (ref_t
), ALIGN
)
228 if (st
.st_size
< size
)
231 /* The file is large enough, map it now. */
232 void *mapping
= __mmap (NULL
, size
, PROT_READ
, MAP_SHARED
, mapfd
, 0);
233 if (mapping
!= MAP_FAILED
)
235 /* Allocate a record for the mapping. */
236 struct mapped_database
*newp
;
238 newp
= malloc (sizeof (*newp
));
241 /* Ugh, after all we went through the memory allocation failed. */
242 __munmap (result
, size
);
246 newp
->head
= mapping
;
247 newp
->data
= ((char *) mapping
+ head
.header_size
248 + roundup (head
.module
* sizeof (ref_t
), ALIGN
));
249 newp
->mapsize
= size
;
250 /* Set counter to 1 to show it is usable. */
261 __set_errno (saved_errno
);
262 #endif /* SCM_RIGHTS */
264 struct mapped_database
*oldval
= *mappedp
;
267 if (oldval
!= NULL
&& atomic_decrement_val (&oldval
->counter
) == 0)
268 __nscd_unmap (oldval
);
274 struct mapped_database
*
275 __nscd_get_map_ref (request_type type
, const char *name
,
276 struct locked_map_ptr
*mapptr
, int *gc_cyclep
)
278 struct mapped_database
*cur
= mapptr
->mapped
;
279 if (cur
== NO_MAPPING
)
283 while (atomic_compare_and_exchange_val_acq (&mapptr
->lock
, 1, 0) != 0)
285 // XXX Best number of rounds?
292 cur
= mapptr
->mapped
;
294 if (__builtin_expect (cur
!= NO_MAPPING
, 1))
296 /* If not mapped or timestamp not updated, request new map. */
298 || (cur
->head
->nscd_certainly_running
== 0
299 && cur
->head
->timestamp
+ MAPPING_TIMEOUT
< time (NULL
)))
300 cur
= get_mapping (type
, name
, &mapptr
->mapped
);
302 if (__builtin_expect (cur
!= NO_MAPPING
, 1))
304 if (__builtin_expect (((*gc_cyclep
= cur
->head
->gc_cycle
) & 1) != 0,
308 atomic_increment (&cur
->counter
);
318 const struct datahead
*
319 __nscd_cache_search (request_type type
, const char *key
, size_t keylen
,
320 const struct mapped_database
*mapped
)
322 unsigned long int hash
= __nis_hash (key
, keylen
) % mapped
->head
->module
;
324 ref_t work
= mapped
->head
->array
[hash
];
325 while (work
!= ENDREF
)
327 struct hashentry
*here
= (struct hashentry
*) (mapped
->data
+ work
);
329 if (type
== here
->type
&& keylen
== here
->len
330 && memcmp (key
, mapped
->data
+ here
->key
, keylen
) == 0)
332 /* We found the entry. Increment the appropriate counter. */
333 const struct datahead
*dh
334 = (struct datahead
*) (mapped
->data
+ here
->packet
);
336 /* See whether we must ignore the entry or whether something
337 is wrong because garbage collection is in progress. */
338 if (dh
->usable
&& ((char *) dh
+ dh
->allocsize
339 <= (char *) mapped
->head
+ mapped
->mapsize
))
350 /* Create a socket connected to a name. */
352 __nscd_open_socket (const char *key
, size_t keylen
, request_type type
,
353 void *response
, size_t responselen
)
355 int saved_errno
= errno
;
357 int sock
= open_socket ();
361 req
.version
= NSCD_VERSION
;
363 req
.key_len
= keylen
;
366 vec
[0].iov_base
= &req
;
367 vec
[0].iov_len
= sizeof (request_header
);
368 vec
[1].iov_base
= (void *) key
;
369 vec
[1].iov_len
= keylen
;
371 ssize_t nbytes
= TEMP_FAILURE_RETRY (__writev (sock
, vec
, 2));
372 if (nbytes
== (ssize_t
) (sizeof (request_header
) + keylen
))
375 struct pollfd fds
[1];
377 fds
[0].events
= POLLIN
| POLLERR
| POLLHUP
;
378 if (__poll (fds
, 1, 5 * 1000) > 0)
380 nbytes
= TEMP_FAILURE_RETRY (__read (sock
, response
,
382 if (nbytes
== (ssize_t
) responselen
)
387 close_not_cancel_no_status (sock
);
390 __set_errno (saved_errno
);