xref: /titanic_50/usr/src/lib/libsocket/inet/getservent_r.c (revision 174bc6499d233e329ecd3d98a880a7b07df16bfa)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  *
25  * lib/libsocket/inet/getservent_r.c
26  *
27  * This file defines and implements the re-entrant enumeration routines for
28  *   services: setservent(), getservent_r(), and endservent(). They consult
29  *   the switch policy directly and do not "share" their enumeration state
30  *   nor the stayopen flag with the implentation of the more common
31  *   getservbyname_r()/getservbyport_r(). The latter follows a tortuous
32  *   route in order to be consistent with netdir_getbyYY() (see
33  *   getservbyname_r.c and lib/libnsl/nss/netdir_inet.c).
34  */
35 
36 #pragma ident	"%Z%%M%	%I%	%E% SMI"
37 
38 #include <sys/types.h>
39 #include <nss_dbdefs.h>
40 
41 /*
42  * str2servent is implemented in libnsl, libnsl/nss/netdir_inet.c, since
43  * the "engine" of the new gethost/getserv/netdir lives in libnsl.
44  */
45 int str2servent(const char *, int, void *, char *, int);
46 
47 /*
48  * Unsynchronized, but it affects only
49  * efficiency, not correctness.
50  */
51 static int services_stayopen;
52 static DEFINE_NSS_DB_ROOT(db_root);
53 static DEFINE_NSS_GETENT(context);
54 
55 void
56 _nss_initf_services(nss_db_params_t *p)
57 {
58 	p->name	= NSS_DBNAM_SERVICES;
59 	p->default_config = NSS_DEFCONF_SERVICES;
60 }
61 
62 int
63 setservent(int stay)
64 {
65 	services_stayopen |= stay;
66 	nss_setent(&db_root, _nss_initf_services, &context);
67 	return (0);
68 }
69 
70 int
71 endservent()
72 {
73 	services_stayopen = 0;
74 	nss_endent(&db_root, _nss_initf_services, &context);
75 	nss_delete(&db_root);
76 	return (0);
77 }
78 
79 struct servent *
80 getservent_r(struct servent *result, char *buffer, int buflen)
81 {
82 	nss_XbyY_args_t arg;
83 	nss_status_t	res;
84 
85 	NSS_XbyY_INIT(&arg, result, buffer, buflen, str2servent);
86 	/*
87 	 * Setting proto to NULL here is a bit of a hack since we share
88 	 * the parsing code in the NIS+ backend with our getservbyYY()
89 	 * brethren who can search on 1-1/2 key. If they pass a NULL
90 	 * proto, the parsing code deals with it by picking the protocol
91 	 * from the first NIS+ matching object and combining all entries
92 	 * with "that" proto field. NIS+ is the only name service, so far,
93 	 * that can return multiple entries on a lookup.
94 	 */
95 	arg.key.serv.proto	= NULL;
96 	/* === No stayopen flag;  of course you stay open for iteration */
97 	res = nss_getent(&db_root, _nss_initf_services, &context, &arg);
98 	arg.status = res;
99 	return (struct servent *)NSS_XbyY_FINI(&arg);
100 }
101