files 读取/etc/hosts文件

gethostbyname #

源码 #

  • _nss_files_gethostbyname3_r开始
 1// nss/nss_files/files-hosts.c
 2enum nss_status
 3_nss_files_gethostbyname3_r (const char *name, int af, struct hostent *result,
 4			     char *buffer, size_t buflen, int *errnop,
 5			     int *herrnop, int32_t *ttlp, char **canonp)
 6{
 7  FILE *stream = NULL;
 8  uintptr_t pad = -(uintptr_t) buffer % __alignof__ (struct hostent_data);
 9  buffer += pad;
10  buflen = buflen > pad ? buflen - pad : 0;
11
12  /* Open file.  */
13  enum nss_status status = internal_setent (&stream);
14
15  if (status == NSS_STATUS_SUCCESS)
16    {
17      while ((status = internal_getent (stream, result, buffer, buflen, errnop,
18					herrnop, af))
19	     == NSS_STATUS_SUCCESS)
20	{
21	  LOOKUP_NAME_CASE (h_name, h_aliases)
22	}
23
24      if (status == NSS_STATUS_SUCCESS
25	  && _res_hconf.flags & HCONF_FLAG_MULTI)
26	status = gethostbyname3_multi
27	  (stream, name, af, result, buffer, buflen, errnop, herrnop);
28
29      fclose (stream);
30    }
31
32  if (canonp && status == NSS_STATUS_SUCCESS)
33    *canonp = result->h_name;
34
35  return status;
36}
37libc_hidden_def (_nss_files_gethostbyname3_r)
  • inernal_setent打开文件/etc/hosts
 1// nss/nss_files/files-XXX.c
 2#define DATAFILE	"/etc/" DATABASE
 3
 4// nss/nss_files/files-hosts.c
 5#define DATABASE	"hosts"
 6
 7// nss/nss_files/files-XXX.c
 8/* Maintenance of the stream open on the database file.  For getXXent
 9   operations the stream needs to be held open across calls, the other
10   getXXbyYY operations all use their own stream.  */
11
12/* Open database file if not already opened.  */
13static enum nss_status
14internal_setent (FILE **stream)
15{
16  enum nss_status status = NSS_STATUS_SUCCESS;
17
18  if (*stream == NULL)
19    {
20      *stream = __nss_files_fopen (DATAFILE);
21
22      if (*stream == NULL)
23	status = errno == EAGAIN ? NSS_STATUS_TRYAGAIN : NSS_STATUS_UNAVAIL;
24    }
25  else
26    rewind (*stream);
27
28  return status;
29}
  • 所以files就是处理/etc/hosts文件