2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)getservent.c 8.1 (Berkeley) 6/4/93
30 * $FreeBSD: src/lib/libc/net/getservent.c,v 1.23 2007/01/09 00:28:02 imp Exp $
31 * $DragonFly: src/lib/libc/net/getservent.c,v 1.7 2005/11/13 02:04:47 swildner Exp $
34 #include <sys/param.h>
35 #include <sys/types.h>
36 #include <sys/socket.h>
37 #include <arpa/inet.h>
48 #include <rpcsvc/yp_prot.h>
49 #include <rpcsvc/ypclnt.h>
51 #include "namespace.h"
52 #include "reentrant.h"
53 #include "un-namespace.h"
54 #include "netdb_private.h"
64 SERVENT_STORAGE_INITIAL
= 1 << 10, /* 1 KByte */
65 SERVENT_STORAGE_MAX
= 1 << 20, /* 1 MByte */
70 enum nss_lookup_type how
;
74 static const ns_src defaultsrc
[] = {
75 { NSSRC_COMPAT
, NS_SUCCESS
},
79 static int servent_unpack(char *, struct servent
*, char **, size_t, int *);
81 /* files backend declarations */
87 int compat_mode_active
;
89 static void files_endstate(void *);
90 NSS_TLS_HANDLING(files
);
92 static int files_servent(void *, void *, va_list);
93 static int files_setservent(void *, void *, va_list);
96 /* nis backend declarations */
97 static int nis_servent(void *, void *, va_list);
98 static int nis_setservent(void *, void *, va_list);
103 char yp_domain
[MAXHOSTNAMELEN
];
107 static void nis_endstate(void *);
108 NSS_TLS_HANDLING(nis
);
110 static int nis_servent(void *, void *, va_list);
111 static int nis_setservent(void *, void *, va_list);
114 /* compat backend declarations */
115 static int compat_setservent(void *, void *, va_list);
117 /* get** wrappers for get**_r functions declarations */
118 struct servent_state
{
123 static void servent_endstate(void *);
124 NSS_TLS_HANDLING(servent
);
134 static int wrap_getservbyname_r(struct key
, struct servent
*, char *, size_t,
136 static int wrap_getservbyport_r(struct key
, struct servent
*, char *, size_t,
138 static int wrap_getservent_r(struct key
, struct servent
*, char *, size_t,
140 static struct servent
*getserv(int (*fn
)(struct key
, struct servent
*, char *,
141 size_t, struct servent
**), struct key
);
144 static int serv_id_func(char *, size_t *, va_list, void *);
145 static int serv_marshal_func(char *, size_t *, void *, va_list, void *);
146 static int serv_unmarshal_func(char *, size_t, void *, va_list, void *);
150 servent_unpack(char *p
, struct servent
*serv
, char **aliases
,
151 size_t aliases_size
, int *errnop
)
153 char *cp
, **q
, *endp
;
159 memset(serv
, 0, sizeof(struct servent
));
161 cp
= strpbrk(p
, "#\n");
166 p
= strpbrk(p
, " \t");
170 while (*p
== ' ' || *p
== '\t')
172 cp
= strpbrk(p
, ",/");
177 l
= strtol(p
, &endp
, 10);
178 if (endp
== p
|| *endp
!= '\0' || l
< 0 || l
> USHRT_MAX
)
180 serv
->s_port
= htons((in_port_t
)l
);
183 q
= serv
->s_aliases
= aliases
;
184 cp
= strpbrk(cp
, " \t");
188 if (*cp
== ' ' || *cp
== '\t') {
192 if (q
< &aliases
[aliases_size
- 1]) {
199 cp
= strpbrk(cp
, " \t");
208 /* files backend implementation */
210 files_endstate(void *p
)
217 f
= ((struct files_state
*)p
)->fp
;
225 * compat structures. compat and files sources functionalities are almost
226 * equal, so they all are managed by files_servent function
229 files_servent(void *retval
, void *mdata
, va_list ap
)
231 static const ns_src compat_src
[] = {
233 { NSSRC_NIS
, NS_SUCCESS
},
237 ns_dtab compat_dtab
[] = {
239 { NSSRC_NIS
, nis_servent
,
240 (void *)((struct servent_mdata
*)mdata
)->how
},
245 struct files_state
*st
;
249 struct servent_mdata
*serv_mdata
;
254 struct servent
*serv
;
267 serv_mdata
= (struct servent_mdata
*)mdata
;
268 switch (serv_mdata
->how
) {
270 name
= va_arg(ap
, char *);
271 proto
= va_arg(ap
, char *);
274 port
= va_arg(ap
, int);
275 proto
= va_arg(ap
, char *);
283 serv
= va_arg(ap
, struct servent
*);
284 buffer
= va_arg(ap
, char *);
285 bufsize
= va_arg(ap
, size_t);
286 errnop
= va_arg(ap
,int *);
288 *errnop
= files_getstate(&st
);
293 st
->compat_mode_active
= 0;
295 if (st
->fp
== NULL
&& (st
->fp
= fopen(_PATH_SERVICES
, "r")) == NULL
) {
300 if (serv_mdata
->how
== nss_lt_all
)
304 stayopen
= st
->stayopen
;
309 if (!st
->compat_mode_active
) {
310 if ((line
= fgetln(st
->fp
, &linesize
)) == NULL
) {
317 if (serv_mdata
->compat_mode
!= 0)
318 st
->compat_mode_active
= 1;
320 if (bufsize
<= linesize
+ _ALIGNBYTES
+
326 aliases
= (char **)_ALIGN(&buffer
[linesize
+1]);
327 aliases_size
= (buffer
+ bufsize
-
328 (char *)aliases
) / sizeof(char *);
329 if (aliases_size
< 1) {
335 memcpy(buffer
, line
, linesize
);
336 buffer
[linesize
] = '\0';
340 if (st
->compat_mode_active
!= 0) {
341 switch (serv_mdata
->how
) {
343 rv
= nsdispatch(retval
, compat_dtab
,
344 NSDB_SERVICES_COMPAT
, "getservbyname_r",
345 compat_src
, name
, proto
, serv
, buffer
,
349 rv
= nsdispatch(retval
, compat_dtab
,
350 NSDB_SERVICES_COMPAT
, "getservbyport_r",
351 compat_src
, port
, proto
, serv
, buffer
,
355 rv
= nsdispatch(retval
, compat_dtab
,
356 NSDB_SERVICES_COMPAT
, "getservent_r",
357 compat_src
, serv
, buffer
, bufsize
, errnop
);
361 if (!(rv
& NS_TERMINATE
) ||
362 serv_mdata
->how
!= nss_lt_all
)
363 st
->compat_mode_active
= 0;
368 rv
= servent_unpack(buffer
, serv
, aliases
, aliases_size
,
382 switch (serv_mdata
->how
) {
384 if (strcmp(name
, serv
->s_name
) == 0)
386 for (cp
= serv
->s_aliases
; *cp
; cp
++)
387 if (strcmp(name
, *cp
) == 0)
392 if (proto
== 0 || strcmp(serv
->s_proto
, proto
) == 0)
396 if (port
!= serv
->s_port
)
399 if (proto
== 0 || strcmp(serv
->s_proto
, proto
) == 0)
407 } while (!(rv
& NS_TERMINATE
));
409 if (!stayopen
&& st
->fp
!= NULL
) {
414 if ((rv
== NS_SUCCESS
) && (retval
!= NULL
))
415 *(struct servent
**)retval
=serv
;
421 files_setservent(void *retval
, void *mdata
, va_list ap
)
423 struct files_state
*st
;
427 rv
= files_getstate(&st
);
431 switch ((enum constants
)mdata
) {
435 st
->fp
= fopen(_PATH_SERVICES
, "r");
441 if (st
->fp
!= NULL
) {
451 st
->compat_mode_active
= 0;
455 /* nis backend implementation */
458 nis_endstate(void *p
)
463 free(((struct nis_state
*)p
)->yp_key
);
468 nis_servent(void *retval
, void *mdata
, va_list ap
)
470 char *resultbuf
, *lastkey
;
472 char buf
[YPMAXRECORD
+ 2];
474 struct nis_state
*st
;
477 enum nss_lookup_type how
;
482 struct servent
*serv
;
492 how
= (enum nss_lookup_type
)mdata
;
495 name
= va_arg(ap
, char *);
496 proto
= va_arg(ap
, char *);
499 port
= va_arg(ap
, int);
500 proto
= va_arg(ap
, char *);
508 serv
= va_arg(ap
, struct servent
*);
509 buffer
= va_arg(ap
, char *);
510 bufsize
= va_arg(ap
, size_t);
511 errnop
= va_arg(ap
, int *);
513 *errnop
= nis_getstate(&st
);
517 if (st
->yp_domain
[0] == '\0') {
518 if (getdomainname(st
->yp_domain
, sizeof st
->yp_domain
)) {
527 snprintf(buf
, sizeof(buf
), "%s/%s", name
, proto
);
528 if (yp_match(st
->yp_domain
, "services.byname", buf
,
529 strlen(buf
), &resultbuf
, &resultbuflen
)) {
535 snprintf(buf
, sizeof(buf
), "%d/%s", ntohs(port
),
539 * We have to be a little flexible
540 * here. Ideally you're supposed to have both
541 * a services.byname and a services.byport
542 * map, but some systems have only
543 * services.byname. FreeBSD cheats a little by
544 * putting the services.byport information in
545 * the same map as services.byname so that
546 * either case will work. We allow for both
547 * possibilities here: if there is no
548 * services.byport map, we try services.byname
551 rv
= yp_match(st
->yp_domain
, "services.byport", buf
,
552 strlen(buf
), &resultbuf
, &resultbuflen
);
554 if (rv
== YPERR_MAP
) {
555 if (yp_match(st
->yp_domain
,
556 "services.byname", buf
,
557 strlen(buf
), &resultbuf
,
570 if (!st
->yp_stepping
) {
572 rv
= yp_first(st
->yp_domain
, "services.byname",
573 &st
->yp_key
, &st
->yp_keylen
, &resultbuf
,
581 lastkey
= st
->yp_key
;
582 rv
= yp_next(st
->yp_domain
, "services.byname",
583 st
->yp_key
, st
->yp_keylen
, &st
->yp_key
,
584 &st
->yp_keylen
, &resultbuf
, &resultbuflen
);
595 /* we need a room for additional \n symbol */
597 resultbuflen
+ 1 + _ALIGNBYTES
+ sizeof(char *)) {
603 aliases
= (char **)_ALIGN(&buffer
[resultbuflen
+ 2]);
605 (buffer
+ bufsize
- (char *)aliases
) / sizeof(char *);
606 if (aliases_size
< 1) {
613 * servent_unpack expects lines terminated with \n --
616 memcpy(buffer
, resultbuf
, resultbuflen
);
617 buffer
[resultbuflen
] = '\n';
618 buffer
[resultbuflen
+ 1] = '\0';
620 if (servent_unpack(buffer
, serv
, aliases
, aliases_size
,
630 } while (!(rv
& NS_TERMINATE
) && how
== nss_lt_all
);
633 if (rv
== NS_SUCCESS
&& retval
!= NULL
)
634 *(struct servent
**)retval
= serv
;
640 nis_setservent(void *result
, void *mdata
, va_list ap
)
642 struct nis_state
*st
;
645 rv
= nis_getstate(&st
);
649 switch ((enum constants
)mdata
) {
664 /* compat backend implementation */
666 compat_setservent(void *retval
, void *mdata
, va_list ap
)
668 static const ns_src compat_src
[] = {
670 { NSSRC_NIS
, NS_SUCCESS
},
674 ns_dtab compat_dtab
[] = {
676 { NSSRC_NIS
, nis_setservent
, mdata
},
682 files_setservent(retval
, mdata
, ap
);
684 switch ((enum constants
)mdata
) {
687 nsdispatch(retval
, compat_dtab
, NSDB_SERVICES_COMPAT
,
688 "setservent", compat_src
, f
);
691 nsdispatch(retval
, compat_dtab
, NSDB_SERVICES_COMPAT
,
692 "endservent", compat_src
);
703 serv_id_func(char *buffer
, size_t *buffer_size
, va_list ap
, void *cache_mdata
)
709 size_t desired_size
, size
, size2
;
710 enum nss_lookup_type lookup_type
;
711 int res
= NS_UNAVAIL
;
713 lookup_type
= (enum nss_lookup_type
)cache_mdata
;
714 switch (lookup_type
) {
716 name
= va_arg(ap
, char *);
717 proto
= va_arg(ap
, char *);
720 desired_size
= sizeof(enum nss_lookup_type
) + size
+ 1;
722 size2
= strlen(proto
);
723 desired_size
+= size2
+ 1;
727 if (desired_size
> *buffer_size
) {
732 memcpy(buffer
, &lookup_type
, sizeof(enum nss_lookup_type
));
733 memcpy(buffer
+ sizeof(enum nss_lookup_type
), name
, size
+ 1);
736 memcpy(buffer
+ sizeof(enum nss_lookup_type
) + size
+ 1,
742 port
= va_arg(ap
, int);
743 proto
= va_arg(ap
, char *);
745 desired_size
= sizeof(enum nss_lookup_type
) + sizeof(int);
747 size
= strlen(proto
);
748 desired_size
+= size
+ 1;
752 if (desired_size
> *buffer_size
) {
757 memcpy(buffer
, &lookup_type
, sizeof(enum nss_lookup_type
));
758 memcpy(buffer
+ sizeof(enum nss_lookup_type
), &port
,
762 memcpy(buffer
+ sizeof(enum nss_lookup_type
) +
763 sizeof(int), proto
, size
+ 1);
768 /* should be unreachable */
773 *buffer_size
= desired_size
;
778 serv_marshal_func(char *buffer
, size_t *buffer_size
, void *retval
, va_list ap
,
784 struct servent
*serv
;
786 size_t orig_buf_size
;
788 struct servent new_serv
;
795 switch ((enum nss_lookup_type
)cache_mdata
) {
797 name
= va_arg(ap
, char *);
798 proto
= va_arg(ap
, char *);
801 port
= va_arg(ap
, int);
802 proto
= va_arg(ap
, char *);
807 /* should be unreachable */
811 serv
= va_arg(ap
, struct servent
*);
812 orig_buf
= va_arg(ap
, char *);
813 orig_buf_size
= va_arg(ap
, size_t);
815 desired_size
= _ALIGNBYTES
+ sizeof(struct servent
) + sizeof(char *);
816 if (serv
->s_name
!= NULL
)
817 desired_size
+= strlen(serv
->s_name
) + 1;
818 if (serv
->s_proto
!= NULL
)
819 desired_size
+= strlen(serv
->s_proto
) + 1;
822 if (serv
->s_aliases
!= NULL
) {
823 for (alias
= serv
->s_aliases
; *alias
; ++alias
) {
824 desired_size
+= strlen(*alias
) + 1;
828 desired_size
+= _ALIGNBYTES
+
829 sizeof(char *) * (aliases_size
+ 1);
832 if (*buffer_size
< desired_size
) {
833 /* this assignment is here for future use */
834 *buffer_size
= desired_size
;
838 memcpy(&new_serv
, serv
, sizeof(struct servent
));
839 memset(buffer
, 0, desired_size
);
841 *buffer_size
= desired_size
;
842 p
= buffer
+ sizeof(struct servent
) + sizeof(char *);
843 memcpy(buffer
+ sizeof(struct servent
), &p
, sizeof(char *));
844 p
= (char *)_ALIGN(p
);
846 if (new_serv
.s_name
!= NULL
) {
847 size
= strlen(new_serv
.s_name
);
848 memcpy(p
, new_serv
.s_name
, size
);
853 if (new_serv
.s_proto
!= NULL
) {
854 size
= strlen(new_serv
.s_proto
);
855 memcpy(p
, new_serv
.s_proto
, size
);
856 new_serv
.s_proto
= p
;
860 if (new_serv
.s_aliases
!= NULL
) {
861 p
= (char *)_ALIGN(p
);
862 memcpy(p
, new_serv
.s_aliases
, sizeof(char *) * aliases_size
);
863 new_serv
.s_aliases
= (char **)p
;
864 p
+= sizeof(char *) * (aliases_size
+ 1);
866 for (alias
= new_serv
.s_aliases
; *alias
; ++alias
) {
867 size
= strlen(*alias
);
868 memcpy(p
, *alias
, size
);
874 memcpy(buffer
, &new_serv
, sizeof(struct servent
));
879 serv_unmarshal_func(char *buffer
, size_t buffer_size
, void *retval
, va_list ap
,
885 struct servent
*serv
;
889 size_t orig_buf_size
;
892 switch ((enum nss_lookup_type
)cache_mdata
) {
894 name
= va_arg(ap
, char *);
895 proto
= va_arg(ap
, char *);
898 port
= va_arg(ap
, int);
899 proto
= va_arg(ap
, char *);
904 /* should be unreachable */
908 serv
= va_arg(ap
, struct servent
*);
909 orig_buf
= va_arg(ap
, char *);
910 orig_buf_size
= va_arg(ap
, size_t);
911 ret_errno
= va_arg(ap
, int *);
914 buffer_size
- sizeof(struct servent
) - sizeof(char *)) {
919 memcpy(serv
, buffer
, sizeof(struct servent
));
920 memcpy(&p
, buffer
+ sizeof(struct servent
), sizeof(char *));
922 orig_buf
= (char *)_ALIGN(orig_buf
);
923 memcpy(orig_buf
, buffer
+ sizeof(struct servent
) + sizeof(char *) +
924 (_ALIGN(p
) - (size_t)p
),
925 buffer_size
- sizeof(struct servent
) - sizeof(char *) -
926 (_ALIGN(p
) - (size_t)p
));
927 p
= (char *)_ALIGN(p
);
929 NS_APPLY_OFFSET(serv
->s_name
, orig_buf
, p
, char *);
930 NS_APPLY_OFFSET(serv
->s_proto
, orig_buf
, p
, char *);
931 if (serv
->s_aliases
!= NULL
) {
932 NS_APPLY_OFFSET(serv
->s_aliases
, orig_buf
, p
, char **);
934 for (alias
= serv
->s_aliases
; *alias
; ++alias
)
935 NS_APPLY_OFFSET(*alias
, orig_buf
, p
, char *);
939 *((struct servent
**)retval
) = serv
;
943 NSS_MP_CACHE_HANDLING(services
);
944 #endif /* NS_CACHING */
946 /* get**_r functions implementation */
948 getservbyname_r(const char *name
, const char *proto
, struct servent
*serv
,
949 char *buffer
, size_t bufsize
, struct servent
**result
)
951 static const struct servent_mdata mdata
= { nss_lt_name
, 0 };
952 static const struct servent_mdata compat_mdata
= { nss_lt_name
, 1 };
954 static const nss_cache_info cache_info
=
955 NS_COMMON_CACHE_INFO_INITIALIZER(
956 services
, (void *)nss_lt_name
,
957 serv_id_func
, serv_marshal_func
, serv_unmarshal_func
);
958 #endif /* NS_CACHING */
959 static const ns_dtab dtab
[] = {
960 { NSSRC_FILES
, files_servent
, (void *)&mdata
},
962 { NSSRC_NIS
, nis_servent
, (void *)nss_lt_name
},
964 { NSSRC_COMPAT
, files_servent
, (void *)&compat_mdata
},
966 NS_CACHE_CB(&cache_info
)
974 rv
= nsdispatch(result
, dtab
, NSDB_SERVICES
, "getservbyname_r",
975 defaultsrc
, name
, proto
, serv
, buffer
, bufsize
, &ret_errno
);
977 if (rv
== NS_SUCCESS
)
984 getservbyport_r(int port
, const char *proto
, struct servent
*serv
,
985 char *buffer
, size_t bufsize
, struct servent
**result
)
987 static const struct servent_mdata mdata
= { nss_lt_id
, 0 };
988 static const struct servent_mdata compat_mdata
= { nss_lt_id
, 1 };
990 static const nss_cache_info cache_info
=
991 NS_COMMON_CACHE_INFO_INITIALIZER(
992 services
, (void *)nss_lt_id
,
993 serv_id_func
, serv_marshal_func
, serv_unmarshal_func
);
995 static const ns_dtab dtab
[] = {
996 { NSSRC_FILES
, files_servent
, (void *)&mdata
},
998 { NSSRC_NIS
, nis_servent
, (void *)nss_lt_id
},
1000 { NSSRC_COMPAT
, files_servent
, (void *)&compat_mdata
},
1002 NS_CACHE_CB(&cache_info
)
1004 { NULL
, NULL
, NULL
}
1010 rv
= nsdispatch(result
, dtab
, NSDB_SERVICES
, "getservbyport_r",
1011 defaultsrc
, port
, proto
, serv
, buffer
, bufsize
, &ret_errno
);
1013 if (rv
== NS_SUCCESS
)
1020 getservent_r(struct servent
*serv
, char *buffer
, size_t bufsize
,
1021 struct servent
**result
)
1023 static const struct servent_mdata mdata
= { nss_lt_all
, 0 };
1024 static const struct servent_mdata compat_mdata
= { nss_lt_all
, 1 };
1026 static const nss_cache_info cache_info
= NS_MP_CACHE_INFO_INITIALIZER(
1027 services
, (void *)nss_lt_all
,
1028 serv_marshal_func
, serv_unmarshal_func
);
1030 static const ns_dtab dtab
[] = {
1031 { NSSRC_FILES
, files_servent
, (void *)&mdata
},
1033 { NSSRC_NIS
, nis_servent
, (void *)nss_lt_all
},
1035 { NSSRC_COMPAT
, files_servent
, (void *)&compat_mdata
},
1037 NS_CACHE_CB(&cache_info
)
1039 { NULL
, NULL
, NULL
}
1045 rv
= nsdispatch(result
, dtab
, NSDB_SERVICES
, "getservent_r",
1046 defaultsrc
, serv
, buffer
, bufsize
, &ret_errno
);
1048 if (rv
== NS_SUCCESS
)
1055 setservent(int stayopen
)
1058 static const nss_cache_info cache_info
= NS_MP_CACHE_INFO_INITIALIZER(
1059 services
, (void *)nss_lt_all
,
1062 static const ns_dtab dtab
[] = {
1063 { NSSRC_FILES
, files_setservent
, (void *)SETSERVENT
},
1065 { NSSRC_NIS
, nis_setservent
, (void *)SETSERVENT
},
1067 { NSSRC_COMPAT
, compat_setservent
, (void *)SETSERVENT
},
1069 NS_CACHE_CB(&cache_info
)
1071 { NULL
, NULL
, NULL
}
1074 nsdispatch(NULL
, dtab
, NSDB_SERVICES
, "setservent", defaultsrc
,
1082 static const nss_cache_info cache_info
= NS_MP_CACHE_INFO_INITIALIZER(
1083 services
, (void *)nss_lt_all
,
1086 static const ns_dtab dtab
[] = {
1087 { NSSRC_FILES
, files_setservent
, (void *)ENDSERVENT
},
1089 { NSSRC_NIS
, nis_setservent
, (void *)ENDSERVENT
},
1091 { NSSRC_COMPAT
, compat_setservent
, (void *)ENDSERVENT
},
1093 NS_CACHE_CB(&cache_info
)
1095 { NULL
, NULL
, NULL
}
1098 nsdispatch(NULL
, dtab
, NSDB_SERVICES
, "endservent", defaultsrc
);
1101 /* get** wrappers for get**_r functions implementation */
1103 servent_endstate(void *p
)
1108 free(((struct servent_state
*)p
)->buffer
);
1113 wrap_getservbyname_r(struct key key
, struct servent
*serv
, char *buffer
,
1114 size_t bufsize
, struct servent
**res
)
1116 return (getservbyname_r(key
.name
, key
.proto
, serv
, buffer
, bufsize
,
1121 wrap_getservbyport_r(struct key key
, struct servent
*serv
, char *buffer
,
1122 size_t bufsize
, struct servent
**res
)
1124 return (getservbyport_r(key
.port
, key
.proto
, serv
, buffer
, bufsize
,
1129 wrap_getservent_r(struct key key
, struct servent
*serv
, char *buffer
,
1130 size_t bufsize
, struct servent
**res
)
1132 return (getservent_r(serv
, buffer
, bufsize
, res
));
1135 static struct servent
*
1136 getserv(int (*fn
)(struct key
, struct servent
*, char *, size_t,
1137 struct servent
**), struct key key
)
1140 struct servent
*res
;
1141 struct servent_state
* st
;
1143 rv
= servent_getstate(&st
);
1149 if (st
->buffer
== NULL
) {
1150 st
->buffer
= malloc(SERVENT_STORAGE_INITIAL
);
1151 if (st
->buffer
== NULL
)
1153 st
->bufsize
= SERVENT_STORAGE_INITIAL
;
1156 rv
= fn(key
, &st
->serv
, st
->buffer
, st
->bufsize
, &res
);
1157 if (res
== NULL
&& rv
== ERANGE
) {
1159 if ((st
->bufsize
<< 1) > SERVENT_STORAGE_MAX
) {
1165 st
->buffer
= malloc(st
->bufsize
);
1166 if (st
->buffer
== NULL
)
1169 } while (res
== NULL
&& rv
== ERANGE
);
1177 getservbyname(const char *name
, const char *proto
)
1184 return (getserv(wrap_getservbyname_r
, key
));
1188 getservbyport(int port
, const char *proto
)
1195 return (getserv(wrap_getservbyport_r
, key
));
1206 return (getserv(wrap_getservent_r
, key
));