hostpid.c revision 90d105df6e2b73ef085abb12fb55287eaac62146
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen/* Copyright (c) 2002-2013 Dovecot authors, see the included COPYING file */
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen#include "lib.h"
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen#include "hostpid.h"
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen#include <stdlib.h>
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen#include <unistd.h>
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen#include <netdb.h>
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen#define HOSTNAME_DISALLOWED_CHARS "/\r\n\t"
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainenconst char *my_hostname = NULL;
0f39a57760d93cddbce3ca43096d78e0fe2f42fdTimo Sirainenconst char *my_pid = NULL;
0f39a57760d93cddbce3ca43096d78e0fe2f42fdTimo Sirainen
0f39a57760d93cddbce3ca43096d78e0fe2f42fdTimo Sirainenstatic char *my_hostname_dup = NULL;
0f39a57760d93cddbce3ca43096d78e0fe2f42fdTimo Sirainenstatic char *my_domain = NULL;
0f39a57760d93cddbce3ca43096d78e0fe2f42fdTimo Sirainen
0f39a57760d93cddbce3ca43096d78e0fe2f42fdTimo Sirainenvoid hostpid_init(void)
0f39a57760d93cddbce3ca43096d78e0fe2f42fdTimo Sirainen{
0f39a57760d93cddbce3ca43096d78e0fe2f42fdTimo Sirainen static char pid[MAX_INT_STRLEN];
6fabfb7bbfd88d0c1de66981e52850f26067623bTimo Sirainen char hostname[256];
6fabfb7bbfd88d0c1de66981e52850f26067623bTimo Sirainen const char *value;
0f39a57760d93cddbce3ca43096d78e0fe2f42fdTimo Sirainen
0f39a57760d93cddbce3ca43096d78e0fe2f42fdTimo Sirainen /* allow calling hostpid_init() multiple times to reset hostname */
0f39a57760d93cddbce3ca43096d78e0fe2f42fdTimo Sirainen i_free_and_null(my_hostname_dup);
58bc77731bb25e900498a28409337e747f622722Timo Sirainen i_free_and_null(my_domain);
c2f24d55319fad0b6c03425f402f0cb0cb1a318bTimo Sirainen
6fabfb7bbfd88d0c1de66981e52850f26067623bTimo Sirainen value = getenv(MY_HOSTNAME_ENV);
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen if (value == NULL) {
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen if (gethostname(hostname, sizeof(hostname)-1) < 0)
e9503210d3521a6833ed62dc332fc42ffb0e7a13Timo Sirainen i_fatal("gethostname() failed: %m");
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen hostname[sizeof(hostname)-1] = '\0';
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen value = hostname;
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen }
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen
e9503210d3521a6833ed62dc332fc42ffb0e7a13Timo Sirainen if (value[0] == '\0' ||
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen strcspn(value, HOSTNAME_DISALLOWED_CHARS) != strlen(value))
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen i_fatal("Invalid system hostname: '%s'", value);
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen my_hostname_dup = i_strdup(value);
2e29e4797a48d78d669821722bdb54fd0a1d3b94Timo Sirainen 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;
}