1 .\" Copyright 2008, Linux Foundation, written by Michael Kerrisk
2 .\" <mtk.manpages@gmail.com>
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date. The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein. The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
24 .\"*******************************************************************
26 .\" This file was generated with po4a. Translate the source file.
28 .\"*******************************************************************
29 .TH GETSERVENT_R 3 2010\-09\-10 GNU "Linux Programmer's Manual"
31 getservent_r, getservbyname_r, getservbyport_r \- get service entry
35 \fB#include <netdb.h>\fP
37 \fBint getservent_r(struct servent *\fP\fIresult_buf\fP\fB, char *\fP\fIbuf\fP\fB,\fP
38 \fB size_t \fP\fIbuflen\fP\fB, struct servent **\fP\fIresult\fP\fB);\fP
40 \fBint getservbyname_r(const char *\fP\fIname\fP\fB, const char *\fP\fIproto\fP\fB,\fP
41 \fB struct servent *\fP\fIresult_buf\fP\fB, char *\fP\fIbuf\fP\fB,\fP
42 \fB size_t \fP\fIbuflen\fP\fB, struct servent **\fP\fIresult\fP\fB);\fP
44 \fBint getservbyport_r(int \fP\fIport\fP\fB, const char *\fP\fIproto\fP\fB,\fP
45 \fB struct servent *\fP\fIresult_buf\fP\fB, char *\fP\fIbuf\fP\fB,\fP
46 \fB size_t \fP\fIbuflen\fP\fB, struct servent **\fP\fIresult\fP\fB);\fP
50 glibc 向けの機能検査マクロの要件 (\fBfeature_test_macros\fP(7) 参照):
54 \fBgetservent_r\fP(), \fBgetservbyname_r\fP(), \fBgetservbyport_r\fP():
56 _BSD_SOURCE || _SVID_SOURCE
60 The \fBgetservent_r\fP(), \fBgetservbyname_r\fP(), and \fBgetservbyport_r\fP()
61 functions are the reentrant equivalents of, respectively, \fBgetservent\fP(3),
62 \fBgetservbyname\fP(3), and \fBgetservbyport\fP(3). They differ in the way that
63 the \fIservent\fP structure is returned, and in the function calling signature
64 and return value. This manual page describes just the differences from the
65 nonreentrant functions.
67 Instead of returning a pointer to a statically allocated \fIservent\fP
68 structure as the function result, these functions copy the structure into
69 the location pointed to by \fIresult_buf\fP.
71 .\" I can find no information on the required/recommended buffer size;
72 .\" the nonreentrant functions use a 1024 byte buffer -- mtk.
73 The \fIbuf\fP array is used to store the string fields pointed to by the
74 returned \fIservent\fP structure. (The nonreentrant functions allocate these
75 strings in static storage.) The size of this array is specified in
76 \fIbuflen\fP. If \fIbuf\fP is too small, the call fails with the error \fBERANGE\fP,
77 and the caller must try again with a larger buffer. (A buffer of length
78 1024 bytes should be sufficient for most applications.)
80 If the function call successfully obtains a service record, then \fI*result\fP
81 is set pointing to \fIresult_buf\fP; otherwise, \fI*result\fP is set to NULL.
83 On success, these functions return 0. On error, they return one of the
84 positive error numbers listed in errors.
86 On error, record not found (\fBgetservbyname_r\fP(), \fBgetservbyport_r\fP()), or
87 end of input (\fBgetservent_r\fP()) \fIresult\fP is set to NULL.
91 (\fBgetservent_r\fP()) No more records in database.
94 \fIbuf\fP is too small. Try again with a larger buffer (and increased
97 These functions are GNU extensions. Functions with similar names exist on
98 some other systems, though typically with different calling signatures.
100 The program below uses \fBgetservbyport_r\fP() to retrieve the service record
101 for the port and protocol named in its first command\-line argument. If a
102 third (integer) command\-line argument is supplied, it is used as the initial
103 value for \fIbuflen\fP; if \fBgetservbyport_r\fP() fails with the error
104 \fBERANGE\fP, the program retries with larger buffer sizes. The following
105 shell session shows a couple of sample runs:
109 $\fB ./a.out 7 tcp 1\fP
110 ERANGE! Retrying with larger buffer
111 getservbyport_r() returned: 0 (success) (buflen=87)
112 s_name=echo; s_proto=tcp; s_port=7; aliases=
113 $\fB ./a.out 77777 tcp\fP
114 getservbyport_r() returned: 0 (success) (buflen=1024)
115 Call failed/record not found
129 #define MAX_BUF 10000
132 main(int argc, char *argv[])
134 int buflen, erange_cnt, port, s;
135 struct servent result_buf;
136 struct servent *result;
142 printf("Usage: %s port\-num proto\-name [buflen]\en", argv[0]);
146 port = htons(atoi(argv[1]));
147 protop = (strcmp(argv[2], "null") == 0 ||
148 strcmp(argv[2], "NULL") == 0) ? NULL : argv[2];
152 buflen = atoi(argv[3]);
154 if (buflen > MAX_BUF) {
155 printf("Exceeded buffer limit (%d)\en", MAX_BUF);
161 s = getservbyport_r(port, protop, &result_buf,
162 buf, buflen, &result);
165 printf("ERANGE! Retrying with larger buffer\en");
168 /* Increment a byte at a time so we can see exactly
169 what size buffer was required */
173 if (buflen > MAX_BUF) {
174 printf("Exceeded buffer limit (%d)\en", MAX_BUF);
178 } while (s == ERANGE);
180 printf("getservbyport_r() returned: %s (buflen=%d)\en",
181 (s == 0) ? "0 (success)" : (s == ENOENT) ? "ENOENT" :
182 strerror(s), buflen);
184 if (s != 0 || result == NULL) {
185 printf("Call failed/record not found\en");
189 printf("s_name=%s; s_proto=%s; s_port=%d; aliases=",
190 result_buf.s_name, result_buf.s_proto,
191 ntohs(result_buf.s_port));
192 for (p = result_buf.s_aliases; *p != NULL; p++)
200 \fBgetservent\fP(3), \fBservices\fP(5)