getservent_r(3) Library Functions Manual getservent_r(3) getservent_r, getservbyname_r, getservbyport_r - ( ) C (libc, -lc) #include int getservent_r(struct servent *restrict result_buf, char buf[restrict .buflen], size_t buflen, struct servent **restrict result); int getservbyname_r(const char *restrict name, const char *restrict proto, struct servent *restrict result_buf, char buf[restrict .buflen], size_t buflen, struct servent **restrict result); int getservbyport_r(int port, const char *restrict proto, struct servent *restrict result_buf, char buf[restrict .buflen], size_t buflen, struct servent **restrict result); glibc (. feature_test_macros(7)): getservent_r(), getservbyname_r(), getservbyport_r(): glibc 2.19: _DEFAULT_SOURCE glibc 2.19 : _BSD_SOURCE || _SVID_SOURCE getservent_r(), getservbyname_r() getservbyport_r() getservent(3), getservbyname(3) getservbyport(3), . servent . . servent , result_buf. buf servent ( ). buflen. buf , ERANGE, ( 1024 ). , *result result_buf; *result NULL. 0. . , (getservbyname_r(), getservbyport_r()) (getservent_r()) result NULL. ENOENT (getservent_r()) . ERANGE buf . ( buflen). attributes(7). +----------------------------+----------------------------------------------------------+--------------------------+ | | | | +----------------------------+----------------------------------------------------------+--------------------------+ |getservent_r(), | | MT-Safe locale | |getservbyname_r(), | | | |getservbyport_r() | | | +----------------------------+----------------------------------------------------------+--------------------------+ , . GNU. , , getservbyport_r() , . (), buflen; getservbyport_r() ERANGE, . : $ ./a.out 7 tcp 1 ERANGE! getservbyport_r() returned: 0 (success) (buflen=87) s_name=echo; s_proto=tcp; s_port=7; aliases= $ ./a.out 77777 tcp getservbyport_r() returned: 0 (success) (buflen=1024) / #define _GNU_SOURCE #include #include #include #include #include #include #define MAX_BUF 10000 int main(int argc, char *argv[]) { int buflen, erange_cnt, port, s; struct servent result_buf; struct servent *result; char buf[MAX_BUF]; char *protop; if (argc < 3) { printf("Usage: %s port-num proto-name [buflen]\n", argv[0]); exit(EXIT_FAILURE); } port = htons(atoi(argv[1])); protop = (strcmp(argv[2], "null") == 0 || strcmp(argv[2], "NULL") == 0) ? NULL : argv[2]; buflen = 1024; if (argc > 3) buflen = atoi(argv[3]); if (buflen > MAX_BUF) { printf("Exceeded buffer limit (%d)\n", MAX_BUF); exit(EXIT_FAILURE); } erange_cnt = 0; do { s = getservbyport_r(port, protop, &result_buf, buf, buflen, &result); if (s == ERANGE) { if (erange_cnt == 0) printf("ERANGE! Retrying with larger buffer\n"); erange_cnt++; /* Increment a byte at a time so we can see exactly what size buffer was required. */ buflen++; if (buflen > MAX_BUF) { printf("Exceeded buffer limit (%d)\n", MAX_BUF); exit(EXIT_FAILURE); } } } while (s == ERANGE); printf("getservbyport_r() returned: %s (buflen=%d)\n", (s == 0) ? "0 (success)" : (s == ENOENT) ? "ENOENT" : strerror(s), buflen); if (s != 0 || result == NULL) { printf("Call failed/record not found\n"); exit(EXIT_FAILURE); } printf("s_name=%s; s_proto=%s; s_port=%d; aliases=", result_buf.s_name, result_buf.s_proto, ntohs(result_buf.s_port)); for (char **p = result_buf.s_aliases; *p != NULL; p++) printf("%s ", *p); printf("\n"); exit(EXIT_SUCCESS); } getservent(3), services(5) () Azamat Hackimov , Dmitry Bolkhovskikh , Vladislav , Yuri Kozlov ; GNU (GNU General Public License - GPL, 3 ) , - . - , , () () () <>. Linux 6.9.1 15 2024 . getservent_r(3)