From: Timo Sirainen Date: Wed, 7 Jan 2009 18:45:37 +0000 (-0500) Subject: Added my_hostdomain() function which mbox code now uses instead of doing that internally. X-Git-Tag: 1.2.beta1~147 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=ed9782b21a020daad7fed0b765e9a39a691e7e05;p=thirdparty%2Fdovecot%2Fcore.git Added my_hostdomain() function which mbox code now uses instead of doing that internally. Based on patch by Apple. --HG-- branch : HEAD --- diff --git a/src/lib-storage/index/mbox/mbox-save.c b/src/lib-storage/index/mbox/mbox-save.c index 5c7678cccc..226d66f83e 100644 --- a/src/lib-storage/index/mbox/mbox-save.c +++ b/src/lib-storage/index/mbox/mbox-save.c @@ -27,7 +27,6 @@ #include #include #include -#include #include #define MBOX_DELIVERY_ID_RAND_BYTES (64/8) @@ -58,8 +57,6 @@ struct mbox_save_context { unsigned int finished:1; }; -static char my_hostdomain[256] = ""; - static int write_error(struct mbox_save_context *ctx) { mbox_set_syscall_error(ctx->mbox, "write()"); @@ -114,23 +111,8 @@ static int mbox_append_lf(struct mbox_save_context *ctx) static int write_from_line(struct mbox_save_context *ctx, time_t received_date, const char *from_envelope) { - const char *name; int ret; - if (*my_hostdomain == '\0') { - struct hostent *hent; - - hent = gethostbyname(my_hostname); - - name = hent != NULL ? hent->h_name : NULL; - if (name == NULL) { - /* failed, use just the hostname */ - name = my_hostname; - } - - i_strocpy(my_hostdomain, name, sizeof(my_hostdomain)); - } - T_BEGIN { const char *line; @@ -139,7 +121,7 @@ static int write_from_line(struct mbox_save_context *ctx, time_t received_date, &ctx->mbox->storage->storage; from_envelope = t_strconcat(storage->ns->user->username, - "@", my_hostdomain, NULL); + "@", my_hostdomain(), NULL); } /* save in local timezone, no matter what it was given with */ diff --git a/src/lib/hostpid.c b/src/lib/hostpid.c index 1845d7d315..c616fd5cd8 100644 --- a/src/lib/hostpid.c +++ b/src/lib/hostpid.c @@ -4,10 +4,13 @@ #include "hostpid.h" #include +#include const char *my_hostname = NULL; const char *my_pid = NULL; +static char *my_domain = NULL; + void hostpid_init(void) { static char hostname[256], pid[MAX_INT_STRLEN]; @@ -17,6 +20,26 @@ void hostpid_init(void) hostname[sizeof(hostname)-1] = '\0'; my_hostname = hostname; + /* allow calling hostpid_init() multiple times to reset hostname */ + i_free_and_null(my_domain); + i_strocpy(pid, dec2str(getpid()), sizeof(pid)); my_pid = pid; } + +const char *my_hostdomain(void) +{ + struct hostent *hent; + const char *name; + + if (my_domain == 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; +} diff --git a/src/lib/hostpid.h b/src/lib/hostpid.h index f5eb2987b5..d6ccffeea8 100644 --- a/src/lib/hostpid.h +++ b/src/lib/hostpid.h @@ -4,9 +4,12 @@ extern const char *my_hostname; extern const char *my_pid; -/* Initializes my_hostname and my_pid. Done only once, so it's safe and - fast to call this function multiple times. */ +/* Initializes my_hostname and my_pid. */ void hostpid_init(void); +/* Returns the current host+domain, or if it fails fallback to returning + hostname. */ +const char *my_hostdomain(void); + #endif