This patch works arounds the portability problems in getservbyname() and getservbyport() on musl libc. Also, on native Windows, it removes the requirement to call WSAStartup() first.
2026-11-24 Bruno Haible <[email protected]> servent: Fix portability problems on musl libc and native Windows. * lib/netdb.in.h (getservbyname, getservbyport): New declarations. * lib/servent.c: New file. * m4/netdb_h.m4 (gl_NETDB_H_REQUIRE_DEFAULTS): Initialize GNULIB_SERVENT. (gl_NETDB_H_DEFAULTS): Initialize REPLACE_GETSERVBYNAME, REPLACE_GETSERVBYPORT. * m4/servent.m4 (gl_SERVENT): Test whether getservbyport works reasonably. Set REPLACE_GETSERVBYNAME, REPLACE_GETSERVBYPORT. * modules/netdb-h (Makefile.am): Substitute GNULIB_SERVENT, REPLACE_GETSERVBYNAME, REPLACE_GETSERVBYPORT. * modules/servent (Files): Add lib/servent.c. (Depends-on): Add sockets, bool, fopen-gnu, streq. (configure.ac): Define conditional and module indicator. (Makefile.am): Compile servent.c. * tests/test-netdb-h-c++.cc: Check the signature of getservbyname(), getservbyport(). * modules/netdb-h-c++-tests (Makefile.am): Link test-netdb-h-c++ with $(SERVENT_LIB). * tests/test-servent.c: Enable the signature checks unconditionally. Include macros.h. (is_aliases_member): New function. (main): Add more tests. * modules/servent-tests (Files): Add macros.h. (Depends-on): Add bool, streq. * doc/posix-functions/getservbyname.texi: Mention the now-fixed problems. * doc/posix-functions/getservbyport.texi: Likewise. diff --git a/doc/posix-functions/getservbyname.texi b/doc/posix-functions/getservbyname.texi index 6b32a06517..28cb859cc1 100644 --- a/doc/posix-functions/getservbyname.texi +++ b/doc/posix-functions/getservbyname.texi @@ -13,15 +13,18 @@ This function is declared in @code{<winsock2.h>} instead of @code{<netdb.h>} on some platforms: mingw, MSVC 14. -@end itemize - -Portability problems not fixed by Gnulib: -@itemize @item -On Windows, in 32-bit mode, this function is defined with a calling -convention that is different from @code{cdecl}. +On Windows, this function returns @code{NULL} +if @code{WSAStartup} has not been called yet. @item The returned @code{struct servent} has a nonsensical @code{s_aliases} value on some platforms: musl libc. +@item +On Windows, in 32-bit mode, this function is defined with a calling +convention that is different from @code{cdecl}. +@end itemize + +Portability problems not fixed by Gnulib: +@itemize @end itemize diff --git a/doc/posix-functions/getservbyport.texi b/doc/posix-functions/getservbyport.texi index 262482dd26..6323afe9a7 100644 --- a/doc/posix-functions/getservbyport.texi +++ b/doc/posix-functions/getservbyport.texi @@ -13,15 +13,18 @@ This function is declared in @code{<winsock2.h>} instead of @code{<netdb.h>} on some platforms: mingw, MSVC 14. -@end itemize - -Portability problems not fixed by Gnulib: -@itemize @item -On Windows, in 32-bit mode, this function is defined with a calling -convention that is different from @code{cdecl}. +On Windows, this function returns @code{NULL} +if @code{WSAStartup} has not been called yet. @item The returned @code{struct servent} has a nonsensical @code{s_aliases} value on some platforms: musl libc. +@item +On Windows, in 32-bit mode, this function is defined with a calling +convention that is different from @code{cdecl}. +@end itemize + +Portability problems not fixed by Gnulib: +@itemize @end itemize diff --git a/lib/netdb.in.h b/lib/netdb.in.h index 9b6c94e68b..125c9eadf8 100644 --- a/lib/netdb.in.h +++ b/lib/netdb.in.h @@ -299,5 +299,45 @@ _GL_WARN_ON_USE (getnameinfo, "getnameinfo is unportable - " #endif +#if @GNULIB_SERVENT@ + +# if @REPLACE_GETSERVBYNAME@ +# if !(defined __cplusplus && defined GNULIB_NAMESPACE) +# undef getservbyname +# define getservbyname rpl_getservbyname +# endif +_GL_FUNCDECL_RPL (getservbyname, struct servent *, + (const char *name, const char *protocol), + _GL_ARG_NONNULL ((1, 2))); +_GL_CXXALIAS_RPL (getservbyname, struct servent *, + (const char *name, const char *protocol)); +# else +_GL_CXXALIAS_SYS (getservbyname, struct servent *, + (const char *name, const char *protocol)); +# endif +# if __GLIBC__ >= 2 +_GL_CXXALIASWARN (getservbyname); +# endif + +# if @REPLACE_GETSERVBYPORT@ +# if !(defined __cplusplus && defined GNULIB_NAMESPACE) +# undef getservbyport +# define getservbyport rpl_getservbyport +# endif +_GL_FUNCDECL_RPL (getservbyport, struct servent *, + (int n_port, const char *protocol), + _GL_ARG_NONNULL ((2))); +_GL_CXXALIAS_RPL (getservbyport, struct servent *, + (int n_port, const char *protocol)); +# else +_GL_CXXALIAS_SYS (getservbyport, struct servent *, + (int n_port, const char *protocol)); +# endif +# if __GLIBC__ >= 2 +_GL_CXXALIASWARN (getservbyport); +# endif + +#endif + #endif /* _@GUARD_PREFIX@_NETDB_H */ #endif /* _@GUARD_PREFIX@_NETDB_H */ diff --git a/lib/servent.c b/lib/servent.c new file mode 100644 index 0000000000..bb2105db97 --- /dev/null +++ b/lib/servent.c @@ -0,0 +1,217 @@ +/* Functions getservbyname(), getservbyport(). + Copyright (C) 2026 Free Software Foundation, Inc. + + This file is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as + published by the Free Software Foundation; either version 2.1 of the + License, or (at your option) any later version. + + This file is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see <https://www.gnu.org/licenses/>. */ + +/* Written by Bruno Haible. */ + +#include <config.h> + +/* Specification. */ +#include <netdb.h> + +#if defined _WIN32 && !defined __CYGWIN__ + +# include "sockets.h" +# include <winsock2.h> + +struct servent * +getservbyname (const char *name, const char *protocol) +# undef getservbyname +{ + (void) gl_sockets_startup (SOCKETS_1_1); + return getservbyname (name, protocol); +} + +struct servent * +getservbyport (int n_port, const char *protocol) +# undef getservbyport +{ + (void) gl_sockets_startup (SOCKETS_1_1); + return getservbyport (n_port, protocol); +} + +#else + +# include <arpa/inet.h> +# include <stdio.h> +# include <stdlib.h> +# include <string.h> + +/* Cache of the contents of /etc/services. */ +static struct servent *db /* = NULL */; +static size_t db_length /* = 0 */; +static size_t db_allocated /* = 0 */; +static bool db_initialized /* = false */; + +static void +initialize (void) +{ + FILE *fp = fopen ("/etc/services", "re"); + if (fp != NULL) + { + while (!(ferror (fp) || feof (fp))) + { + /* It's OK to use fixed-length buffers here, because /etc/services + is under control of the system administrator, not of an arbitrary + user. */ + char name[32+1]; + unsigned int num; + char proto[32+1]; + char aliases[8][32+1]; + char rest[256+1]; + int n_aliases; + int ret; + + /* Parse a line. */ + ret = fscanf (fp, "%32s" + "%*[ \t]%u/%32s" + "%*[ \t]%32[^ \t#\n]" + "%*[ \t]%32[^ \t#\n]" + "%*[ \t]%32[^ \t#\n]" + "%*[ \t]%32[^ \t#\n]" + "%*[ \t]%32[^ \t#\n]" + "%*[ \t]%32[^ \t#\n]" + "%*[ \t]%32[^ \t#\n]" + "%*[ \t]%32[^ \t#\n]" + "%*[ \t]%256[^#\n]", + name, &num, proto, + aliases[0], aliases[1], aliases[2], aliases[3], + aliases[4], aliases[5], aliases[6], aliases[7], + rest); + if (ret >= 3) + { + for (n_aliases = 0; n_aliases < 8 && n_aliases < ret - 3; n_aliases++) + if (aliases[n_aliases][0] == '\0') + break; + + /* Successfully parsed an entry. Now allocate memory. */ + if (db_length == db_allocated) + { + size_t new_allocated = 2 * db_allocated; + if (new_allocated < 10) + new_allocated = 10; + db = (struct servent *) realloc (db, new_allocated * sizeof (struct servent)); + if (db == NULL) + { + db_length = 0; + db_allocated = 0; + break; + } + db_allocated = new_allocated; + } + size_t need = + (n_aliases > 0 ? n_aliases + 1 : 0) * sizeof (char *) + + (strlen (name) + 1) + (strlen (proto) + 1); + for (int i = 0; i < n_aliases; i++) + need += strlen (aliases[i]) + 1; + char *memory = (char *) malloc (need); + if (memory == NULL) + { + db = NULL; + db_length = 0; + db_allocated = 0; + break; + } + + /* Fill the new 'struct servent'. */ + struct servent *se = &db[db_length++]; + + if (n_aliases > 0) + { + se->s_aliases = (char **) memory; + memory += (n_aliases + 1) * sizeof (char *); + } + else + { + static char *no_aliases /* = NULL */; + se->s_aliases = &no_aliases; + } + + memcpy (memory, name, strlen (name) + 1); + se->s_name = memory; + memory += strlen (name) + 1; + + memcpy (memory, proto, strlen (proto) + 1); + se->s_proto = memory; + memory += strlen (proto) + 1; + + for (int i = 0; i < n_aliases; i++) + { + memcpy (memory, aliases[i], strlen (aliases[i]) + 1); + se->s_aliases[i] = memory; + memory += strlen (aliases[i]) + 1; + } + se->s_aliases[n_aliases] = NULL; + + se->s_port = htons (num); + } + + for (;;) + { + int c = fgetc (fp); + if (c == EOF || c == '\n') + break; + } + } + + fclose (fp); + } + + db_initialized = true; +} + +static bool +is_aliases_member (const struct servent *se, const char *name) +{ + for (int i = 0; se->s_aliases[i] != NULL; i++) + if (streq (se->s_aliases[i], name)) + return true; + return false; +} + +struct servent * +getservbyname (const char *name, const char *protocol) +{ + if (!db_initialized) + initialize (); + + size_t n = db_length; + for (size_t i = 0; i < n; i++) + { + struct servent *se = &db[i]; + if ((streq (se->s_name, name) || is_aliases_member (se, name)) + && streq (se->s_proto, protocol)) + return se; + } + return NULL; +} + +struct servent * +getservbyport (int n_port, const char *protocol) +{ + if (!db_initialized) + initialize (); + + size_t n = db_length; + for (size_t i = 0; i < n; i++) + { + struct servent *se = &db[i]; + if (se->s_port == n_port && streq (se->s_proto, protocol)) + return se; + } + return NULL; +} + +#endif diff --git a/m4/netdb_h.m4 b/m4/netdb_h.m4 index 2e471c2c5f..24c4f0debc 100644 --- a/m4/netdb_h.m4 +++ b/m4/netdb_h.m4 @@ -1,5 +1,5 @@ # netdb_h.m4 -# serial 16 +# serial 17 dnl Copyright (C) 2008-2026 Free Software Foundation, Inc. dnl This file is free software; the Free Software Foundation dnl gives unlimited permission to copy and/or distribute it, @@ -63,6 +63,7 @@ AC_DEFUN([gl_NETDB_H_REQUIRE_DEFAULTS] [ m4_defun(GL_MODULE_INDICATOR_PREFIX[_NETDB_H_MODULE_INDICATOR_DEFAULTS], [ gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_GETADDRINFO]) + gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_SERVENT]) ]) m4_require(GL_MODULE_INDICATOR_PREFIX[_NETDB_H_MODULE_INDICATOR_DEFAULTS]) AC_REQUIRE([gl_NETDB_H_DEFAULTS]) @@ -77,4 +78,6 @@ AC_DEFUN([gl_NETDB_H_DEFAULTS] HAVE_DECL_GETNAMEINFO=1; AC_SUBST([HAVE_DECL_GETNAMEINFO]) REPLACE_GAI_STRERROR=0; AC_SUBST([REPLACE_GAI_STRERROR]) REPLACE_GETADDRINFO=0; AC_SUBST([REPLACE_GETADDRINFO]) + REPLACE_GETSERVBYNAME=0; AC_SUBST([REPLACE_GETSERVBYNAME]) + REPLACE_GETSERVBYPORT=0; AC_SUBST([REPLACE_GETSERVBYPORT]) ]) diff --git a/m4/servent.m4 b/m4/servent.m4 index 97bcad0fa7..29abb1f3f4 100644 --- a/m4/servent.m4 +++ b/m4/servent.m4 @@ -1,5 +1,5 @@ # servent.m4 -# serial 5 +# serial 6 dnl Copyright (C) 2008, 2010-2026 Free Software Foundation, Inc. dnl This file is free software; the Free Software Foundation dnl gives unlimited permission to copy and/or distribute it, @@ -50,4 +50,67 @@ AC_DEFUN([gl_SERVENT] ]) fi AC_SUBST([SERVENT_LIB]) + AC_CACHE_CHECK([whether getservbyname and getservbyport work reasonably], + [gl_cv_func_getservbyport_works], + [case "$host_os" in + mingw* | windows*) + # On native Windows, the functions return NULL if WSAStartup has not + # been called yet. + gl_cv_func_getservbyport_works=no + ;; + *) + saved_LIBS="$LIBS" + LIBS="$LIBS $SERVENT_LIB" + AC_RUN_IFELSE( + [AC_LANG_PROGRAM( + [[ +#ifdef HAVE_WINSOCK2_H +#include <winsock2.h> +#else +#include <netdb.h> +#include <arpa/inet.h> +#endif +#include <string.h> + static int + is_aliases_member (const struct servent *se, const char *name) + { + int i; + for (i = 0; se->s_aliases[i] != NULL; i++) + if (strcmp (se->s_aliases[i], name) == 0) + return 1; + return 0; + } + ]], + [[int result = 0; + /* This test fails on musl libc. */ + { + struct servent *se = getservbyport (htons (80), "tcp"); + if (!(se != NULL + && se->s_port == htons (80) + && strcmp (se->s_proto, "tcp") == 0 + && ((strcmp (se->s_name, "http") == 0 + && is_aliases_member (se, "www")) + || (strcmp (se->s_name, "www") == 0 + && is_aliases_member (se, "http"))))) + result |= 1; + } + return result; + ]])], + [gl_cv_func_getservbyport_works=yes], + [gl_cv_func_getservbyport_works=no], + [case "$host_os" in + # Guess no on musl. + linux* | midipix*) gl_cv_func_getservbyport_works="guessing no" ;; + # If we don't know, obey --enable-cross-guesses. + *) gl_cv_func_getservbyport_works="$gl_cross_guess_normal" ;; + esac + ]) + LIBS="$saved_LIBS" + ;; + esac + ]) + case "$gl_cv_func_getservbyport_works" in + *yes) ;; + *) REPLACE_GETSERVBYNAME=1; REPLACE_GETSERVBYPORT=1 ;; + esac ]) diff --git a/modules/netdb-h b/modules/netdb-h index 6f303c99be..5ada3584e6 100644 --- a/modules/netdb-h +++ b/modules/netdb-h @@ -33,6 +33,7 @@ netdb.h: netdb.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(ARG_NONNULL_H) -e 's|@''NEXT_NETDB_H''@|$(NEXT_NETDB_H)|g' \ -e 's|@''HAVE_NETDB_H''@|$(HAVE_NETDB_H)|g' \ -e 's/@''GNULIB_GETADDRINFO''@/$(GNULIB_GETADDRINFO)/g' \ + -e 's/@''GNULIB_SERVENT''@/$(GNULIB_SERVENT)/g' \ -e 's|@''HAVE_STRUCT_ADDRINFO''@|$(HAVE_STRUCT_ADDRINFO)|g' \ -e 's|@''HAVE_DECL_FREEADDRINFO''@|$(HAVE_DECL_FREEADDRINFO)|g' \ -e 's|@''HAVE_DECL_GAI_STRERROR''@|$(HAVE_DECL_GAI_STRERROR)|g' \ @@ -40,6 +41,8 @@ netdb.h: netdb.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(ARG_NONNULL_H) -e 's|@''HAVE_DECL_GETNAMEINFO''@|$(HAVE_DECL_GETNAMEINFO)|g' \ -e 's|@''REPLACE_GAI_STRERROR''@|$(REPLACE_GAI_STRERROR)|g' \ -e 's|@''REPLACE_GETADDRINFO''@|$(REPLACE_GETADDRINFO)|g' \ + -e 's|@''REPLACE_GETSERVBYNAME''@|$(REPLACE_GETSERVBYNAME)|g' \ + -e 's|@''REPLACE_GETSERVBYPORT''@|$(REPLACE_GETSERVBYPORT)|g' \ -e '/definitions of _GL_FUNCDECL_RPL/r $(CXXDEFS_H)' \ -e '/definition of _GL_ARG_NONNULL/r $(ARG_NONNULL_H)' \ -e '/definition of _GL_WARN_ON_USE/r $(WARN_ON_USE_H)' \ diff --git a/modules/netdb-h-c++-tests b/modules/netdb-h-c++-tests index 593ec5b27a..3add0feaf1 100644 --- a/modules/netdb-h-c++-tests +++ b/modules/netdb-h-c++-tests @@ -15,5 +15,5 @@ if ANSICXX TESTS += test-netdb-h-c++ check_PROGRAMS += test-netdb-h-c++ test_netdb_h_c___SOURCES = test-netdb-h-c++.cc -test_netdb_h_c___LDADD = $(LDADD) $(LIBINTL) $(GETADDRINFO_LIB) +test_netdb_h_c___LDADD = $(LDADD) $(LIBINTL) $(GETADDRINFO_LIB) $(SERVENT_LIB) endif diff --git a/modules/servent b/modules/servent index 1c2412e72b..57c57c7e10 100644 --- a/modules/servent +++ b/modules/servent @@ -7,15 +7,26 @@ getprotobyname() function, getprotobynumber() function. Files: m4/servent.m4 +lib/servent.c Depends-on: sys_socket-h netdb-h +sockets [test $REPLACE_GETSERVBYNAME = 1 || test $REPLACE_GETSERVBYPORT = 1] +bool [test $REPLACE_GETSERVBYNAME = 1 || test $REPLACE_GETSERVBYPORT = 1] +fopen-gnu [test $REPLACE_GETSERVBYNAME = 1 || test $REPLACE_GETSERVBYPORT = 1] +streq [test $REPLACE_GETSERVBYNAME = 1 || test $REPLACE_GETSERVBYPORT = 1] configure.ac: gl_SERVENT +gl_CONDITIONAL([GL_COND_OBJ_SERVENT], + [test $REPLACE_GETSERVBYNAME = 1 || test $REPLACE_GETSERVBYPORT = 1]) +gl_NETDB_MODULE_INDICATOR([servent]) Makefile.am: +if GL_COND_OBJ_SERVENT +lib_SOURCES += servent.c +endif Include: <netdb.h> diff --git a/modules/servent-tests b/modules/servent-tests index 98ef534879..6af9db1006 100644 --- a/modules/servent-tests +++ b/modules/servent-tests @@ -1,9 +1,12 @@ Files: tests/test-servent.c tests/signature.h +tests/macros.h Depends-on: +bool htonl +streq configure.ac: diff --git a/tests/test-netdb-h-c++.cc b/tests/test-netdb-h-c++.cc index a274d7e866..e56d4d272c 100644 --- a/tests/test-netdb-h-c++.cc +++ b/tests/test-netdb-h-c++.cc @@ -35,6 +35,13 @@ SIGNATURE_CHECK (GNULIB_NAMESPACE::getnameinfo, int, char *, socklen_t, int)); #endif +#if GNULIB_TEST_SERVENT +SIGNATURE_CHECK (GNULIB_NAMESPACE::getservbyname, struct servent *, + (const char *, const char *)); +SIGNATURE_CHECK (GNULIB_NAMESPACE::getservbyport, struct servent *, + (int, const char *)); +#endif + int main () diff --git a/tests/test-servent.c b/tests/test-servent.c index 9dc986fda1..463fe000a7 100644 --- a/tests/test-servent.c +++ b/tests/test-servent.c @@ -14,7 +14,7 @@ You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>. */ -/* Written by Collin Funk <[email protected]>, 2024. */ +/* Written by Collin Funk and Bruno Haible. */ #include <config.h> @@ -22,31 +22,61 @@ #include <netdb.h> #include "signature.h" - -#if !(defined _WIN32 && !defined _WIN64 && !defined __CYGWIN__) -/* On 32-bit native Windows, these two functions may have the __stdcall calling - convention. But the SIGNATURE_CHECK works only for functions with __cdecl - calling convention. */ -SIGNATURE_CHECK (getservbyname, struct servent *, - (char const *, char const *)); -SIGNATURE_CHECK (getservbyport, struct servent *, (int, char const *)); -#endif +SIGNATURE_CHECK (getservbyname, struct servent *, (const char *, const char *)); +SIGNATURE_CHECK (getservbyport, struct servent *, (int, const char *)); #include <stdio.h> +#include <string.h> #include <arpa/inet.h> +#include "macros.h" + +static bool +is_aliases_member (const struct servent *se, const char *name) +{ + for (int i = 0; se->s_aliases[i] != NULL; i++) + if (streq (se->s_aliases[i], name)) + return true; + return false; +} + int main (void) { - struct servent *result; - - result = getservbyname ("domain", "tcp"); - if (result == NULL) - fputs ("getservbyname failed\n", stderr); - - result = getservbyport (htons (53), "tcp"); - if (result == NULL) - fputs ("getservbyport failed\n", stderr); + { + struct servent *se = getservbyname ("domain", "tcp"); + if (se == NULL) + fputs ("getservbyname failed\n", stderr); + } + { + struct servent *se = getservbyport (htons (53), "tcp"); + if (se == NULL) + fputs ("getservbyport failed\n", stderr); + } + { + struct servent *se = getservbyport (htons (80), "tcp"); + ASSERT (se != NULL); + ASSERT (se->s_port == htons (80)); + ASSERT (streq (se->s_proto, "tcp")); + ASSERT ((streq (se->s_name, "http") && is_aliases_member (se, "www")) + || (streq (se->s_name, "www") && is_aliases_member (se, "http"))); + } + { + struct servent *se = getservbyname ("http", "tcp"); + ASSERT (se != NULL); + ASSERT (se->s_port == htons (80)); + ASSERT (streq (se->s_proto, "tcp")); + ASSERT ((streq (se->s_name, "http") && is_aliases_member (se, "www")) + || (streq (se->s_name, "www") && is_aliases_member (se, "http"))); + } + { + struct servent *se = getservbyname ("www", "tcp"); + ASSERT (se != NULL); + ASSERT (se->s_port == htons (80)); + ASSERT (streq (se->s_proto, "tcp")); + ASSERT ((streq (se->s_name, "http") && is_aliases_member (se, "www")) + || (streq (se->s_name, "www") && is_aliases_member (se, "http"))); + } - return 0; + return test_exit_status; }
