getservice.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*
  2. ** services.c /etc/services access functions
  3. **
  4. ** This file is part of the NYS Library.
  5. **
  6. ** The NYS Library is free software; you can redistribute it and/or
  7. ** modify it under the terms of the GNU Library General Public License as
  8. ** published by the Free Software Foundation; either version 2 of the
  9. ** License, or (at your option) any later version.
  10. **
  11. ** The NYS Library is distributed in the hope that it will be useful,
  12. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. ** Library General Public License for more details.
  15. **
  16. ** You should have received a copy of the GNU Library General Public
  17. ** License along with the NYS Library; see the file COPYING.LIB. If
  18. ** not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  19. ** Cambridge, MA 02139, USA.
  20. **
  21. **
  22. ** Copyright (c) 1983 Regents of the University of California.
  23. ** All rights reserved.
  24. **
  25. ** Redistribution and use in source and binary forms, with or without
  26. ** modification, are permitted provided that the following conditions
  27. ** are met:
  28. ** 1. Redistributions of source code must retain the above copyright
  29. ** notice, this list of conditions and the following disclaimer.
  30. ** 2. Redistributions in binary form must reproduce the above copyright
  31. ** notice, this list of conditions and the following disclaimer in the
  32. ** documentation and/or other materials provided with the distribution.
  33. ** 3. All advertising materials mentioning features or use of this software
  34. ** must display the following acknowledgement:
  35. ** This product includes software developed by the University of
  36. ** California, Berkeley and its contributors.
  37. ** 4. Neither the name of the University nor the names of its contributors
  38. ** may be used to endorse or promote products derived from this software
  39. ** without specific prior written permission.
  40. **
  41. ** THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  42. ** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  43. ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  44. ** ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  45. ** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  46. ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  47. ** OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. ** HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  49. ** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50. ** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  51. ** SUCH DAMAGE.
  52. */
  53. #define strpbrk __strpbrk
  54. #define atoi __atoi
  55. #define __FORCE_GLIBC
  56. #define _GNU_SOURCE
  57. #include <features.h>
  58. #include <sys/types.h>
  59. #include <sys/socket.h>
  60. #include <netdb.h>
  61. #include <stdio.h>
  62. #include <string.h>
  63. #include <stdlib.h>
  64. #include <netinet/in.h>
  65. #include <arpa/inet.h>
  66. #include <errno.h>
  67. #ifdef __UCLIBC_HAS_THREADS__
  68. # include <pthread.h>
  69. static pthread_mutex_t mylock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
  70. #endif
  71. #define LOCK __pthread_mutex_lock(&mylock)
  72. #define UNLOCK __pthread_mutex_unlock(&mylock)
  73. #define MAXALIASES 35
  74. #define SBUFSIZE (BUFSIZ + 1 + (sizeof(char *) * MAXALIASES))
  75. static FILE *servf = NULL;
  76. static struct servent serv;
  77. static char *servbuf = NULL;
  78. static int serv_stayopen;
  79. static void __initbuf(void)
  80. {
  81. if (!servbuf) {
  82. servbuf = malloc(SBUFSIZE);
  83. if (!servbuf)
  84. abort();
  85. }
  86. }
  87. extern void attribute_hidden __setservent(int f)
  88. {
  89. LOCK;
  90. if (servf == NULL)
  91. servf = fopen(_PATH_SERVICES, "r" );
  92. else
  93. rewind(servf);
  94. serv_stayopen |= f;
  95. UNLOCK;
  96. }
  97. strong_alias(__setservent,setservent)
  98. extern void attribute_hidden __endservent(void)
  99. {
  100. LOCK;
  101. if (servf) {
  102. fclose(servf);
  103. servf = NULL;
  104. }
  105. serv_stayopen = 0;
  106. UNLOCK;
  107. }
  108. strong_alias(__endservent,endservent)
  109. extern int attribute_hidden __getservent_r(struct servent * result_buf,
  110. char * buf, size_t buflen,
  111. struct servent ** result)
  112. {
  113. char *p;
  114. register char *cp, **q;
  115. char **serv_aliases;
  116. char *line;
  117. *result=NULL;
  118. if (buflen < sizeof(*serv_aliases)*MAXALIASES) {
  119. errno=ERANGE;
  120. return errno;
  121. }
  122. LOCK;
  123. serv_aliases=(char **)buf;
  124. buf+=sizeof(*serv_aliases)*MAXALIASES;
  125. buflen-=sizeof(*serv_aliases)*MAXALIASES;
  126. if (buflen < BUFSIZ+1) {
  127. UNLOCK;
  128. errno=ERANGE;
  129. return errno;
  130. }
  131. line=buf;
  132. buf+=BUFSIZ+1;
  133. buflen-=BUFSIZ+1;
  134. if (servf == NULL && (servf = fopen(_PATH_SERVICES, "r" )) == NULL) {
  135. UNLOCK;
  136. errno=EIO;
  137. return errno;
  138. }
  139. again:
  140. if ((p = fgets(line, BUFSIZ, servf)) == NULL) {
  141. UNLOCK;
  142. errno=EIO;
  143. return errno;
  144. }
  145. if (*p == '#')
  146. goto again;
  147. cp = strpbrk(p, "#\n");
  148. if (cp == NULL)
  149. goto again;
  150. *cp = '\0';
  151. result_buf->s_name = p;
  152. p = strpbrk(p, " \t");
  153. if (p == NULL)
  154. goto again;
  155. *p++ = '\0';
  156. while (*p == ' ' || *p == '\t')
  157. p++;
  158. cp = strpbrk(p, ",/");
  159. if (cp == NULL)
  160. goto again;
  161. *cp++ = '\0';
  162. result_buf->s_port = htons((u_short)atoi(p));
  163. result_buf->s_proto = cp;
  164. q = result_buf->s_aliases = serv_aliases;
  165. cp = strpbrk(cp, " \t");
  166. if (cp != NULL)
  167. *cp++ = '\0';
  168. while (cp && *cp) {
  169. if (*cp == ' ' || *cp == '\t') {
  170. cp++;
  171. continue;
  172. }
  173. if (q < &serv_aliases[MAXALIASES - 1])
  174. *q++ = cp;
  175. cp = strpbrk(cp, " \t");
  176. if (cp != NULL)
  177. *cp++ = '\0';
  178. }
  179. *q = NULL;
  180. *result=result_buf;
  181. UNLOCK;
  182. return 0;
  183. }
  184. strong_alias(__getservent_r,getservent_r)
  185. struct servent * getservent(void)
  186. {
  187. struct servent *result;
  188. __initbuf();
  189. __getservent_r(&serv, servbuf, SBUFSIZE, &result);
  190. return result;
  191. }
  192. extern int attribute_hidden __getservbyname_r(const char *name, const char *proto,
  193. struct servent * result_buf, char * buf, size_t buflen,
  194. struct servent ** result)
  195. {
  196. register char **cp;
  197. int ret;
  198. LOCK;
  199. __setservent(serv_stayopen);
  200. while (!(ret=__getservent_r(result_buf, buf, buflen, result))) {
  201. if (__strcmp(name, result_buf->s_name) == 0)
  202. goto gotname;
  203. for (cp = result_buf->s_aliases; *cp; cp++)
  204. if (__strcmp(name, *cp) == 0)
  205. goto gotname;
  206. continue;
  207. gotname:
  208. if (proto == 0 || __strcmp(result_buf->s_proto, proto) == 0)
  209. break;
  210. }
  211. if (!serv_stayopen)
  212. __endservent();
  213. UNLOCK;
  214. return *result?0:ret;
  215. }
  216. strong_alias(__getservbyname_r,getservbyname_r)
  217. struct servent *getservbyname(const char *name, const char *proto)
  218. {
  219. struct servent *result;
  220. __initbuf();
  221. __getservbyname_r(name, proto, &serv, servbuf, SBUFSIZE, &result);
  222. return result;
  223. }
  224. extern int attribute_hidden __getservbyport_r(int port, const char *proto,
  225. struct servent * result_buf, char * buf,
  226. size_t buflen, struct servent ** result)
  227. {
  228. int ret;
  229. LOCK;
  230. __setservent(serv_stayopen);
  231. while (!(ret=__getservent_r(result_buf, buf, buflen, result))) {
  232. if (result_buf->s_port != port)
  233. continue;
  234. if (proto == 0 || __strcmp(result_buf->s_proto, proto) == 0)
  235. break;
  236. }
  237. if (!serv_stayopen)
  238. __endservent();
  239. UNLOCK;
  240. return *result?0:ret;
  241. }
  242. strong_alias(__getservbyport_r,getservbyport_r)
  243. struct servent attribute_hidden * __getservbyport(int port, const char *proto)
  244. {
  245. struct servent *result;
  246. __initbuf();
  247. __getservbyport_r(port, proto, &serv, servbuf, SBUFSIZE, &result);
  248. return result;
  249. }
  250. strong_alias(__getservbyport,getservbyport)