summaryrefslogtreecommitdiffstats
path: root/src/lib/hostpid.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/hostpid.c')
-rw-r--r--src/lib/hostpid.c69
1 files changed, 69 insertions, 0 deletions
diff --git a/src/lib/hostpid.c b/src/lib/hostpid.c
new file mode 100644
index 0000000..3e91ade
--- /dev/null
+++ b/src/lib/hostpid.c
@@ -0,0 +1,69 @@
+/* Copyright (c) 2002-2018 Dovecot authors, see the included COPYING file */
+
+#include "lib.h"
+#include "hostpid.h"
+
+#include <unistd.h>
+#include <netdb.h>
+
+#define HOSTNAME_DISALLOWED_CHARS "/\r\n\t"
+
+const char *my_hostname = NULL;
+const char *my_pid = NULL;
+
+static char *my_hostname_dup = NULL;
+static char *my_domain = NULL;
+
+void hostpid_init(void)
+{
+ static char pid[MAX_INT_STRLEN];
+ char hostname[256];
+ const char *value;
+
+ /* allow calling hostpid_init() multiple times to reset hostname */
+ i_free_and_null(my_hostname_dup);
+ i_free_and_null(my_domain);
+
+ value = getenv(MY_HOSTNAME_ENV);
+ if (value == NULL) {
+ if (gethostname(hostname, sizeof(hostname)-1) < 0)
+ i_fatal("gethostname() failed: %m");
+ hostname[sizeof(hostname)-1] = '\0';
+ value = hostname;
+ }
+
+ if (value[0] == '\0' ||
+ strcspn(value, HOSTNAME_DISALLOWED_CHARS) != strlen(value))
+ i_fatal("Invalid system hostname: '%s'", value);
+ my_hostname_dup = i_strdup(value);
+ my_hostname = my_hostname_dup;
+
+ i_snprintf(pid, sizeof(pid), "%lld", (long long)getpid());
+ my_pid = pid;
+}
+
+void hostpid_deinit(void)
+{
+ i_free(my_hostname_dup);
+ i_free(my_domain);
+}
+
+const char *my_hostdomain(void)
+{
+ struct hostent *hent;
+ const char *name;
+
+ if (my_domain == NULL) {
+ name = getenv(MY_HOSTDOMAIN_ENV);
+ if (name == NULL) {
+ hent = gethostbyname(my_hostname);
+ name = hent != NULL ? hent->h_name : NULL;
+ if (name == NULL) {
+ /* failed, use just the hostname */
+ name = my_hostname;
+ }
+ }
+ my_domain = i_strdup(name);
+ }
+ return my_domain;
+}