vm: fix failed alloc condition
[minix.git] / lib / libc / yp / yplib.c
blobceb8251eaa7bbf18bbd15cdab4a0ff3c17072016
1 /* $NetBSD: yplib.c,v 1.43 2006/11/03 20:18:49 christos Exp $ */
3 /*
4 * Copyright (c) 1992, 1993 Theo de Raadt <deraadt@fsa.ca>
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
17 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
29 #include <sys/cdefs.h>
30 #if defined(LIBC_SCCS) && !defined(lint)
31 __RCSID("$NetBSD: yplib.c,v 1.43 2006/11/03 20:18:49 christos Exp $");
32 #endif
34 #include "namespace.h"
35 #include "reentrant.h"
37 #include <sys/param.h>
38 #include <sys/socket.h>
39 #include <sys/file.h>
40 #include <sys/uio.h>
42 #include <arpa/nameser.h>
44 #include <assert.h>
45 #include <errno.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <unistd.h>
51 #include <rpc/rpc.h>
52 #include <rpc/xdr.h>
53 #include <rpcsvc/yp_prot.h>
54 #include <rpcsvc/ypclnt.h>
55 #include "local.h"
57 #define BINDINGDIR "/var/yp/binding"
58 #define YPBINDLOCK "/var/run/ypbind.lock"
60 struct dom_binding *_ypbindlist;
61 char _yp_domain[MAXHOSTNAMELEN];
63 #define YPLIB_TIMEOUT 10
64 #define YPLIB_RPC_RETRIES 4
66 struct timeval _yplib_timeout = { YPLIB_TIMEOUT, 0 };
67 struct timeval _yplib_rpc_timeout = { YPLIB_TIMEOUT / YPLIB_RPC_RETRIES,
68 1000000 * (YPLIB_TIMEOUT % YPLIB_RPC_RETRIES) / YPLIB_RPC_RETRIES };
69 int _yplib_nerrs = 5;
71 #ifdef __weak_alias
72 __weak_alias(yp_bind, _yp_bind)
73 __weak_alias(yp_unbind, _yp_unbind)
74 __weak_alias(yp_get_default_domain, _yp_get_default_domain)
75 #endif
77 #ifdef _REENTRANT
78 static mutex_t _ypmutex = MUTEX_INITIALIZER;
79 #define YPLOCK() mutex_lock(&_ypmutex)
80 #define YPUNLOCK() mutex_unlock(&_ypmutex)
81 #else
82 #define YPLOCK()
83 #define YPUNLOCK()
84 #endif
86 int
87 _yp_dobind(dom, ypdb)
88 const char *dom;
89 struct dom_binding **ypdb;
91 static int pid = -1;
92 char path[MAXPATHLEN];
93 struct dom_binding *ysd, *ysd2;
94 struct ypbind_resp ypbr;
95 struct sockaddr_in clnt_sin;
96 int clnt_sock, fd, gpid;
97 CLIENT *client;
98 int new = 0;
99 int nerrs = 0;
100 ssize_t r;
102 if (dom == NULL || *dom == '\0')
103 return YPERR_BADARGS;
106 * test if YP is running or not
108 if ((fd = open(YPBINDLOCK, O_RDONLY)) == -1)
109 return YPERR_YPBIND;
110 if (!(flock(fd, LOCK_EX | LOCK_NB) == -1 && errno == EWOULDBLOCK)) {
111 (void)close(fd);
112 return YPERR_YPBIND;
114 (void)close(fd);
116 gpid = getpid();
117 if (!(pid == -1 || pid == gpid)) {
118 ysd = _ypbindlist;
119 while (ysd) {
120 if (ysd->dom_client)
121 clnt_destroy(ysd->dom_client);
122 ysd2 = ysd->dom_pnext;
123 free(ysd);
124 ysd = ysd2;
126 _ypbindlist = NULL;
128 pid = gpid;
130 if (ypdb != NULL)
131 *ypdb = NULL;
133 for (ysd = _ypbindlist; ysd; ysd = ysd->dom_pnext)
134 if (strcmp(dom, ysd->dom_domain) == 0)
135 break;
136 if (ysd == NULL) {
137 if ((ysd = malloc(sizeof *ysd)) == NULL)
138 return YPERR_YPERR;
139 (void)memset(ysd, 0, sizeof *ysd);
140 ysd->dom_socket = -1;
141 ysd->dom_vers = 0;
142 new = 1;
144 again:
145 if (ysd->dom_vers == 0) {
146 (void) snprintf(path, sizeof(path), "%s/%s.%d",
147 BINDINGDIR, dom, 2);
148 if ((fd = open(path, O_RDONLY)) == -1) {
150 * no binding file, YP is dead, or not yet fully
151 * alive.
153 goto trynet;
155 if (flock(fd, LOCK_EX | LOCK_NB) == -1 &&
156 errno == EWOULDBLOCK) {
157 struct iovec iov[2];
158 struct ypbind_resp ybr;
159 u_short ypb_port;
160 struct ypbind_binding *bn;
162 iov[0].iov_base = &ypb_port;
163 iov[0].iov_len = sizeof ypb_port;
164 iov[1].iov_base = &ybr;
165 iov[1].iov_len = sizeof ybr;
167 r = readv(fd, iov, 2);
168 if (r != (ssize_t)(iov[0].iov_len + iov[1].iov_len)) {
169 (void)close(fd);
170 ysd->dom_vers = -1;
171 goto again;
173 (void)memset(&ysd->dom_server_addr, 0,
174 sizeof ysd->dom_server_addr);
175 #ifndef __minix
176 ysd->dom_server_addr.sin_len =
177 sizeof(struct sockaddr_in);
178 #endif /* !__minix */
179 ysd->dom_server_addr.sin_family = AF_INET;
180 bn = &ybr.ypbind_respbody.ypbind_bindinfo;
181 ysd->dom_server_addr.sin_port =
182 bn->ypbind_binding_port;
184 ysd->dom_server_addr.sin_addr =
185 bn->ypbind_binding_addr;
187 ysd->dom_server_port = ysd->dom_server_addr.sin_port;
188 (void)close(fd);
189 goto gotit;
190 } else {
191 /* no lock on binding file, YP is dead. */
192 (void)close(fd);
193 if (new)
194 free(ysd);
195 return YPERR_YPBIND;
198 trynet:
199 if (ysd->dom_vers == -1 || ysd->dom_vers == 0) {
200 struct ypbind_binding *bn;
201 (void)memset(&clnt_sin, 0, sizeof clnt_sin);
202 #ifndef __minix
203 clnt_sin.sin_len = sizeof(struct sockaddr_in);
204 #endif /* !__minix */
205 clnt_sin.sin_family = AF_INET;
206 clnt_sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
208 clnt_sock = RPC_ANYSOCK;
209 client = clnttcp_create(&clnt_sin, YPBINDPROG, YPBINDVERS,
210 &clnt_sock, 0, 0);
211 if (client == NULL) {
212 clnt_pcreateerror("clnttcp_create");
213 if (new)
214 free(ysd);
215 return YPERR_YPBIND;
217 r = clnt_call(client, (rpcproc_t)YPBINDPROC_DOMAIN,
218 (xdrproc_t)xdr_ypdomain_wrap_string, &dom,
219 (xdrproc_t)xdr_ypbind_resp, &ypbr, _yplib_timeout);
220 if (r != RPC_SUCCESS) {
221 if (new == 0 && ++nerrs == _yplib_nerrs) {
222 nerrs = 0;
223 fprintf(stderr,
224 "YP server for domain %s not responding, still trying\n",
225 dom);
227 clnt_destroy(client);
228 ysd->dom_vers = -1;
229 goto again;
231 clnt_destroy(client);
233 (void)memset(&ysd->dom_server_addr, 0,
234 sizeof ysd->dom_server_addr);
235 #ifndef __minix
236 ysd->dom_server_addr.sin_len = sizeof(struct sockaddr_in);
237 #endif /* !__minix */
238 ysd->dom_server_addr.sin_family = AF_INET;
239 bn = &ypbr.ypbind_respbody.ypbind_bindinfo;
240 ysd->dom_server_addr.sin_port =
241 bn->ypbind_binding_port;
242 ysd->dom_server_addr.sin_addr.s_addr =
243 bn->ypbind_binding_addr.s_addr;
244 ysd->dom_server_port =
245 bn->ypbind_binding_port;
246 gotit:
247 ysd->dom_vers = YPVERS;
248 (void)strlcpy(ysd->dom_domain, dom, sizeof(ysd->dom_domain));
250 if (ysd->dom_client)
251 clnt_destroy(ysd->dom_client);
252 ysd->dom_socket = RPC_ANYSOCK;
253 ysd->dom_client = clntudp_create(&ysd->dom_server_addr,
254 YPPROG, YPVERS, _yplib_rpc_timeout, &ysd->dom_socket);
255 if (ysd->dom_client == NULL) {
256 clnt_pcreateerror("clntudp_create");
257 ysd->dom_vers = -1;
258 goto again;
260 if (fcntl(ysd->dom_socket, F_SETFD, FD_CLOEXEC) == -1)
261 perror("fcntl: F_SETFD");
263 if (new) {
264 ysd->dom_pnext = _ypbindlist;
265 _ypbindlist = ysd;
267 if (ypdb != NULL)
268 *ypdb = ysd;
269 return 0;
272 void
273 __yp_unbind(ypb)
274 struct dom_binding *ypb;
277 _DIAGASSERT(ypb != NULL);
279 clnt_destroy(ypb->dom_client);
280 ypb->dom_client = NULL;
281 ypb->dom_socket = -1;
285 yp_bind(dom)
286 const char *dom;
288 if (_yp_invalid_domain(dom))
289 return YPERR_BADARGS;
291 return _yp_dobind(dom, NULL);
294 void
295 yp_unbind(dom)
296 const char *dom;
298 struct dom_binding *ypb, *ypbp;
300 if (_yp_invalid_domain(dom))
301 return;
303 ypbp = NULL;
304 for (ypb = _ypbindlist; ypb; ypb = ypb->dom_pnext) {
305 if (strcmp(dom, ypb->dom_domain) == 0) {
306 clnt_destroy(ypb->dom_client);
307 if (ypbp)
308 ypbp->dom_pnext = ypb->dom_pnext;
309 else
310 _ypbindlist = ypb->dom_pnext;
311 free(ypb);
312 return;
314 ypbp = ypb;
316 return;
320 yp_get_default_domain(domp)
321 char **domp;
323 if (domp == NULL)
324 return YPERR_BADARGS;
325 *domp = NULL;
326 if (_yp_domain[0] == '\0')
327 if (getdomainname(_yp_domain, sizeof _yp_domain))
328 return YPERR_NODOM;
329 *domp = _yp_domain;
330 return 0;
334 _yp_check(dom)
335 char **dom;
337 char *unused;
338 int good;
340 YPLOCK();
342 if (_yp_domain[0] == '\0')
343 if (yp_get_default_domain(&unused)) {
344 good = 0;
345 goto done;
347 if (dom)
348 *dom = _yp_domain;
350 good = yp_bind(_yp_domain) == 0;
351 done:
352 YPUNLOCK();
353 return good;
357 * _yp_invalid_domain: check if given domainname isn't legal.
358 * returns non-zero if invalid
361 _yp_invalid_domain(dom)
362 const char *dom;
364 if (dom == NULL || *dom == '\0')
365 return 1;
367 if (strlen(dom) > YPMAXDOMAIN)
368 return 1;
370 if (strchr(dom, '/') != NULL)
371 return 1;
373 return 0;