Postfix3.3.1
全て データ構造 ファイル 関数 変数 型定義 マクロ定義
get_hostname.c
[詳解]
1 /*++
2 /* NAME
3 /* get_hostname 3
4 /* SUMMARY
5 /* network name lookup
6 /* SYNOPSIS
7 /* #include <get_hostname.h>
8 /*
9 /* const char *get_hostname()
10 /* DESCRIPTION
11 /* get_hostname() returns the local hostname as obtained
12 /* via gethostname() or its moral equivalent. This routine
13 /* goes to great length to avoid dependencies on any network
14 /* services.
15 /* DIAGNOSTICS
16 /* Fatal errors: no hostname, invalid hostname.
17 /* SEE ALSO
18 /* valid_hostname(3)
19 /* LICENSE
20 /* .ad
21 /* .fi
22 /* The Secure Mailer license must be distributed with this software.
23 /* AUTHOR(S)
24 /* Wietse Venema
25 /* IBM T.J. Watson Research
26 /* P.O. Box 704
27 /* Yorktown Heights, NY 10598, USA
28 /*--*/
29 
30 /* System library. */
31 
32 #include <sys_defs.h>
33 #include <sys/param.h>
34 #include <string.h>
35 #include <unistd.h>
36 
37 #if (MAXHOSTNAMELEN < 256)
38 #undef MAXHOSTNAMELEN
39 #define MAXHOSTNAMELEN 256
40 #endif
41 
42 /* Utility library. */
43 
44 #include "mymalloc.h"
45 #include "msg.h"
46 #include "valid_hostname.h"
47 #include "get_hostname.h"
48 
49 /* Local stuff. */
50 
51 static char *my_host_name;
52 
53 /* get_hostname - look up my host name */
54 
55 const char *get_hostname(void)
56 {
57  char namebuf[MAXHOSTNAMELEN + 1];
58 
59  /*
60  * The gethostname() call is not (or not yet) in ANSI or POSIX, but it is
61  * part of the socket interface library. We avoid the more politically-
62  * correct uname() routine because that has no portable way of dealing
63  * with long (FQDN) hostnames.
64  *
65  * DO NOT CALL GETHOSTBYNAME FROM THIS FUNCTION. IT BREAKS MAILDIR DELIVERY
66  * AND OTHER THINGS WHEN THE MACHINE NAME IS NOT FOUND IN /ETC/HOSTS OR
67  * CAUSES PROCESSES TO HANG WHEN THE NETWORK IS DISCONNECTED.
68  *
69  * POSTFIX NO LONGER NEEDS A FULLY QUALIFIED HOSTNAME. INSTEAD POSTFIX WILL
70  * USE A DEFAULT DOMAIN NAME "LOCALDOMAIN".
71  */
72  if (my_host_name == 0) {
73  /* DO NOT CALL GETHOSTBYNAME FROM THIS FUNCTION */
74  if (gethostname(namebuf, sizeof(namebuf)) < 0)
75  msg_fatal("gethostname: %m");
76  namebuf[MAXHOSTNAMELEN] = 0;
77  /* DO NOT CALL GETHOSTBYNAME FROM THIS FUNCTION */
78  if (valid_hostname(namebuf, DO_GRIPE) == 0)
79  msg_fatal("unable to use my own hostname");
80  /* DO NOT CALL GETHOSTBYNAME FROM THIS FUNCTION */
81  my_host_name = mystrdup(namebuf);
82  }
83  return (my_host_name);
84 }
char * mystrdup(const char *str)
Definition: mymalloc.c:225
#define MAXHOSTNAMELEN
Definition: get_hostname.c:39
int valid_hostname(const char *name, int gripe)
NORETURN msg_fatal(const char *fmt,...)
Definition: msg.c:249
#define DO_GRIPE
Definition: haproxy_srvr.h:30
const char * get_hostname(void)
Definition: get_hostname.c:55