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"
40 int sock
= __socket (PF_UNIX
, SOCK_STREAM
, 0);
44 /* Make socket non-blocking. */
45 int fl
= __fcntl (sock
, F_GETFL
);
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
)
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. */
65 close_not_cancel_no_status (sock
);
72 __nscd_unmap (struct mapped_database
*mapped
)
74 assert (mapped
->counter
== 0);
75 __munmap ((void *) mapped
->head
, mapped
->mapsize
);
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
;
88 const size_t keylen
= strlen (key
) + 1;
90 int saved_errno
= errno
;
94 /* Send the request. */
98 int sock
= open_socket ();
102 req
.version
= NSCD_VERSION
;
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. */
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];
136 fds
[0].events
= POLLIN
| POLLERR
| POLLHUP
;
137 if (__poll (fds
, 1, 5 * 1000) <= 0)
138 /* Failure or timeout. */
141 if (TEMP_FAILURE_RETRY (__recvmsg (sock
, &msg
, 0)) != keylen
)
144 mapfd
= *(int *) CMSG_DATA (cmsg
);
146 if (CMSG_FIRSTHDR (&msg
)->cmsg_len
!= CMSG_LEN (sizeof (int)))
150 if (strcmp (resdata
, key
) != 0
151 || fstat64 (mapfd
, &st
) != 0
152 || st
.st_size
< sizeof (struct database_pers_head
))
155 struct database_pers_head head
;
156 if (TEMP_FAILURE_RETRY (__pread (mapfd
, &head
, sizeof (head
), 0))
160 if (head
.version
!= DB_VERSION
|| head
.header_size
!= sizeof (head
)
161 /* This really should not happen but who knows, maybe the update
163 || (! head
.nscd_certainly_running
164 && head
.timestamp
+ MAPPING_TIMEOUT
< time (NULL
)))
167 size_t size
= (sizeof (head
) + roundup (head
.module
* sizeof (ref_t
), ALIGN
)
170 if (st
.st_size
< size
)
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
));
183 /* Ugh, after all we went through the memory allocation failed. */
184 __munmap (result
, size
);
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. */
203 __set_errno (saved_errno
);
204 #endif /* SCM_RIGHTS */
206 struct mapped_database
*oldval
= *mappedp
;
209 if (oldval
!= NULL
&& atomic_decrement_val (&oldval
->counter
) == 0)
210 __nscd_unmap (oldval
);
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
)
225 while (atomic_compare_and_exchange_val_acq (&mapptr
->lock
, 1, 0) != 0)
227 // XXX Best number of rounds?
234 cur
= mapptr
->mapped
;
236 if (__builtin_expect (cur
!= NO_MAPPING
, 1))
238 /* If not mapped or timestamp not updated, request new map. */
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,
250 atomic_increment (&cur
->counter
);
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
))
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 ();
303 req
.version
= NSCD_VERSION
;
305 req
.key_len
= keylen
;
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
))
317 struct pollfd fds
[1];
319 fds
[0].events
= POLLIN
| POLLERR
| POLLHUP
;
320 if (__poll (fds
, 1, 5 * 1000) > 0)
322 nbytes
= TEMP_FAILURE_RETRY (__read (sock
, response
,
324 if (nbytes
== (ssize_t
) responselen
)
329 close_not_cancel_no_status (sock
);
332 __set_errno (saved_errno
);