xref: /netbsd-src/lib/libc/yp/ypclnt.3 (revision 60ab2ca5c0570c0013b39de285ddaa91fe27d029)
1.\"	$NetBSD: ypclnt.3,v 1.23 2008/04/30 13:10:51 martin Exp $
2.\"
3.\" Copyright (c) 1996 The NetBSD Foundation, Inc.
4.\" All rights reserved.
5.\"
6.\" This code is derived from software contributed to The NetBSD Foundation
7.\" by Jason R. Thorpe.
8.\"
9.\" Redistribution and use in source and binary forms, with or without
10.\" modification, are permitted provided that the following conditions
11.\" are met:
12.\" 1. Redistributions of source code must retain the above copyright
13.\"    notice, this list of conditions and the following disclaimer.
14.\" 2. Redistributions in binary form must reproduce the above copyright
15.\"    notice, this list of conditions and the following disclaimer in the
16.\"    documentation and/or other materials provided with the distribution.
17.\"
18.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28.\" POSSIBILITY OF SUCH DAMAGE.
29.\"
30.Dd May 21, 1997
31.Dt YPCLNT 3
32.Os
33.Sh NAME
34.Nm yp_all ,
35.Nm yp_bind ,
36.Nm yp_first ,
37.Nm yp_get_default_domain ,
38.Nm yp_master ,
39.Nm yp_match ,
40.Nm yp_next ,
41.Nm yp_order ,
42.Nm yp_unbind ,
43.Nm yperr_string ,
44.Nm ypprot_err
45.Nd Interface to the YP subsystem
46.Sh LIBRARY
47.Lb libc
48.Sh SYNOPSIS
49.In sys/types.h
50.In rpc/rpc.h
51.In rpcsvc/ypclnt.h
52.In rpcsvc/yp_prot.h
53.Ft int
54.Fn yp_all "const char *indomain" "const char *inmap" "struct ypall_callback *incallback"
55.Ft int
56.Fn yp_bind "const char *dom"
57.Ft int
58.Fn yp_first "const char *indomain" "const char *inmap" "char **outkey" "int *outkeylen" "char **outval" "int *outvallen"
59.Ft int
60.Fn yp_get_default_domain "char **outdomain"
61.Ft int
62.Fn yp_master "const char *indomain" "const char *inmap" "char **outname"
63.Ft int
64.Fn yp_match "const char *indomain" "const char *inmap" "const char *inkey" "int inkeylen" "char **outval" "int *outvallen"
65.Ft int
66.Fn yp_next "const char *indomain" "const char *inmap" "const char *inkey" "int inkeylen" "char **outkey" "int *outkeylen" "char **outval" "int *outvallen"
67.Ft int
68.Fn yp_order "const char *indomain" "const char *inmap" "int *outorder"
69.Ft void
70.Fn yp_unbind "const char *dom"
71.Ft char *
72.Fn yperr_string "int incode"
73.Ft int
74.Fn ypprot_err "unsigned int incode"
75.Sh DESCRIPTION
76The
77.Nm ypclnt
78suite provides an interface to the
79.Tn YP
80subsystem.
81For a general description of the
82.Tn YP
83subsystem, see
84.Xr yp 8 .
85.Pp
86For all functions, input values begin with
87.Pa in
88and output values begin with
89.Pa out .
90.Pp
91Any output values of type
92.Em char **
93should be the addresses of uninitialized character pointers.
94These values will be reset to the null pointer (unless the address
95itself is the null pointer, in which case the error
96.Er YPERR_BADARGS
97will be returned).
98If necessary,
99memory will be allocated by the
100.Tn YP
101client routines using
102.Fn malloc ,
103and the result will be stored in the appropriate output value.
104If the invocation of a
105.Tn YP
106client routine doesn't return an error,
107and an output value is not the null pointer, then this memory
108should be freed by the user when there is no additional need for
109the data stored there.
110For
111.Pa outkey
112and
113.Pa outval ,
114two extra bytes of memory are allocated for a
115.Ql \en
116and
117.Ql \e0 ,
118which are not
119reflected in the values of
120.Pa outkeylen
121or
122.Pa outvallen .
123.Pp
124All occurrences of
125.Pa indomain
126and
127.Pa inmap
128must be non-null, NUL-terminated strings.
129All input strings which also have
130a corresponding length parameter cannot be the null pointer unless the
131corresponding length value is zero.
132Such strings need not be NUL-terminated.
133.Pp
134All
135.Tn YP
136lookup calls (the functions
137.Fn yp_all ,
138.Fn yp_first ,
139.Fn yp_master ,
140.Fn yp_match ,
141.Fn yp_next ,
142.Fn yp_order )
143require a
144.Tn YP
145domain name and a
146.Tn YP
147map name.
148The default domain name may be obtained by calling
149.Fn yp_get_default_domain ,
150and should thus be used before all other
151.Tn YP
152calls in a client program.
153The value it places
154.Pa outdomain
155is suitable for use as the
156.Pa indomain
157parameter to all subsequent
158.Tn YP
159calls.
160.Pp
161In order for
162.Tn YP
163lookup calls to succeed, the client process must be bound
164to a
165.Tn YP
166server process.
167The client process need not explicitly bind to
168the server, as it happens automatically whenever a lookup occurs.
169The function
170.Fn yp_bind
171is provided for a backup strategy, e.g. a local file, when a
172.Tn YP
173server process is not available.
174Each binding uses one socket descriptor on the client
175process, which may be explicitly freed using
176.Fn yp_unbind ,
177which frees all per-process and per-node resources to bind the domain and
178marks the domain unbound.
179.Pp
180If, during a
181.Tn YP
182lookup, an RPC failure occurs, the domain used in the lookup
183is automatically marked unbound and the
184.Nm ypclnt
185layer retries the lookup as long as
186.Xr ypbind 8
187is running and either the client process cannot bind to a server for the domain
188specified in the lookup, or RPC requests to the
189.Tn YP
190server process fail.
191If an error is not RPC-related, one of the
192.Tn YP
193error codes described below
194is returned and control given back to the user code.
195.Pp
196The
197.Nm ypclnt
198suite provides the following functionality:
199.Bl -tag -width yp_match()xx
200.It Fn yp_match
201Provides the value associated with the given key.
202.It Fn yp_first
203Provides the first key-value pair from the given map in the named domain.
204.It Fn yp_next
205Provides the next key-value pair in the given map.
206To obtain the second pair,
207the
208.Pa inkey
209value should be the
210.Pa outkey
211value provided by the initial call to
212.Fn yp_first .
213In the general case, the next key-value pair may be obtained by using the
214.Pa outkey
215value from the previous call to
216.Fn yp_next
217as the value for
218.Pa inkey .
219.Pp
220Of course, the notions of
221.Dq first
222and
223.Dq next
224are particular to the
225type of
226.Tn YP
227map being accessed, and thus there is no guarantee of lexical order.
228The only guarantees provided with
229.Fn yp_first
230and
231.Fn yp_next ,
232providing that the same map on the same server is polled repeatedly until
233.Fn yp_next
234returns YPERR_NOMORE, are that all key-value pairs in that map will be accessed
235exactly once, and if the entire procedure is repeated, the order will be
236the same.
237.Pp
238If the server is heavily loaded or the server fails for some reason, the
239domain being used may become unbound.
240If this happens, and the client process re-binds, the retrieval rules
241will break: some entries may be seen twice, and others not at all.
242For this reason, the function
243.Fn yp_all
244provides a better solution for reading all of the entries in a particular map.
245.It Fn yp_all
246This function provides a way to transfer an entire map from
247the server to the client process with a single request.
248This transfer uses TCP, unlike all other functions in the
249.Nm ypclnt
250suite, which use UDP.
251The entire transaction occurs in a single RPC request-response.
252The third argument to this function provides a way to supply
253the name of a function to process each key-value pair in the map.
254.Fn yp_all
255returns after the entire transaction is complete, or the
256.Pa foreach
257function decides that it does not want any more key-value pairs.
258The third argument to
259.Fn yp_all
260is:
261.Bd -literal -offset indent
262struct ypall_callback *incallback {
263	int (*foreach)();
264	char *data;
265};
266.Ed
267.Pp
268The
269.Em char *data
270argument is an opaque pointer for use by the callback function.
271The
272.Pa foreach
273function should return non-zero when it no longer wishes to process
274key-value pairs, at which time
275.Fn yp_all
276returns a value of 0, and is called with the following arguments:
277.Pp
278.Bd -literal -offset indent
279int foreach (
280	int instatus,
281	char *inkey,
282	int inkeylen,
283	char *inval,
284	int invallen,
285	char *indata
286);
287.Ed
288.Pp
289Where:
290.Bl -tag -width "inkey, inval"
291.It Fa instatus
292Holds one of the return status values described in
293.Aq Pa rpcsvc/yp_prot.h :
294see
295.Fn ypprot_err
296below for a function that will translate
297.Tn YP
298protocol errors into a
299.Nm ypclnt
300layer error code as described in
301.Aq Pa rpcsvc/ypclnt.h .
302.It Fa inkey, inval
303The key and value arguments are somewhat different here than described
304above.
305In this case, the memory pointed to by
306.Fa inkey
307and
308.Fa inval
309is private to
310.Fn yp_all ,
311and is overwritten with each subsequent key-value pair, thus the
312.Pa foreach
313function should do something useful with the contents of that memory during
314each iteration.
315If the key-value pairs are not terminated with either
316.Ql \en
317or
318.Ql \e0
319in the map, then they will not be terminated as such when given to the
320.Pa foreach
321function, either.
322.It Fa indata
323This is the contents of the
324.Pa incallback-\*[Gt]data
325element of the callback structure.
326It is provided as a means to share state between the
327.Pa foreach
328function and the user code.
329Its use is completely optional: cast it to
330something useful or simply ignore it.
331.El
332.It Fn yp_order
333Returns the order number for a map.
334.It Fn yp_master
335Returns the hostname for the machine on which the master
336.Tn YP
337server process for
338a map is running.
339.It Fn yperr_string
340Returns a pointer to a NUL-terminated error string that does not contain a
341.Ql \&.
342or
343.Ql \en .
344.It Fn ypprot_err
345Converts a
346.Tn YP
347protocol error code to a
348.Nm ypclnt
349error code suitable for
350.Fn yperr_string .
351.El
352.Sh RETURN VALUES
353All functions in the
354.Nm ypclnt
355suite which are of type
356.Em int
357return 0 upon success or one of the following error codes upon failure:
358.Bl -tag -width "YPERR_BADARGS   "
359.It Bq Er YPERR_BADARGS
360The passed arguments to the function are invalid.
361.It Bq Er YPERR_BADDB
362The
363.Tn YP
364map that was polled is defective.
365.It Bq Er YPERR_DOMAIN
366Client process cannot bind to server on this
367.Tn YP
368domain.
369.It Bq Er YPERR_KEY
370The key passed does not exist.
371.It Bq Er YPERR_MAP
372There is no such map in the server's domain.
373.It Bq Er YPERR_DOM
374The local
375.Tn YP
376domain is not set.
377.It Bq Er YPERR_NOMORE
378There are no more records in the queried map.
379.It Bq Er YPERR_PMAP
380Cannot communicate with portmapper (see
381.Xr rpcbind 8 ) .
382.It Bq Er YPERR_RESRC
383A resource allocation failure occurred.
384.It Bq Er YPERR_RPC
385An RPC failure has occurred.
386The domain has been marked unbound.
387.It Bq Er YPERR_VERS
388Client/server version mismatch.
389If the server is running version 1 of the
390.Tn YP
391protocol,
392.Fn yp_all
393functionality does not exist.
394.It Bq Er YPERR_BIND
395Cannot communicate with
396.Xr ypbind 8 .
397.It Bq Er YPERR_YPERR
398An internal server or client error has occurred.
399.It Bq Er YPERR_YPSERV
400The client cannot communicate with the
401.Tn YP
402server process.
403.El
404.Sh SEE ALSO
405.Xr malloc 3 ,
406.Xr yp 8 ,
407.Xr ypbind 8 ,
408.Xr ypserv 8
409.Sh AUTHORS
410.An Theo De Raadt
411