Mercurial > dovecot > core-2.2
annotate src/lib/hostpid.c @ 23007:36e01285b5b8
lib: buffer - Improve header comment for buffer_insert() and buffer_delete().
author | Stephan Bosch <stephan.bosch@dovecot.fi> |
---|---|
date | Mon, 18 Mar 2019 00:52:37 +0100 |
parents | cb108f786fb4 |
children |
rev | line source |
---|---|
22713
cb108f786fb4
Updated copyright notices to include the year 2018.
Stephan Bosch <stephan.bosch@dovecot.fi>
parents:
21390
diff
changeset
|
1 /* Copyright (c) 2002-2018 Dovecot authors, see the included COPYING file */ |
0 | 2 |
3 #include "lib.h" | |
4 #include "hostpid.h" | |
5 | |
6 #include <unistd.h> | |
8596
2609eca99495
Added my_hostdomain() function which mbox code now uses instead of doing that internally.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
7 #include <netdb.h> |
0 | 8 |
15365
626a9df21e62
hostpid_init(): Don't allow gethostname() to fail. Be more strict about what chars it can contain.
Timo Sirainen <tss@iki.fi>
parents:
15202
diff
changeset
|
9 #define HOSTNAME_DISALLOWED_CHARS "/\r\n\t" |
626a9df21e62
hostpid_init(): Don't allow gethostname() to fail. Be more strict about what chars it can contain.
Timo Sirainen <tss@iki.fi>
parents:
15202
diff
changeset
|
10 |
0 | 11 const char *my_hostname = NULL; |
12 const char *my_pid = NULL; | |
13 | |
15832
19e4448dee4f
my_hostname was broken because of recent change.
Timo Sirainen <tss@iki.fi>
parents:
15829
diff
changeset
|
14 static char *my_hostname_dup = NULL; |
8596
2609eca99495
Added my_hostdomain() function which mbox code now uses instead of doing that internally.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
15 static char *my_domain = NULL; |
2609eca99495
Added my_hostdomain() function which mbox code now uses instead of doing that internally.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
16 |
0 | 17 void hostpid_init(void) |
18 { | |
15832
19e4448dee4f
my_hostname was broken because of recent change.
Timo Sirainen <tss@iki.fi>
parents:
15829
diff
changeset
|
19 static char pid[MAX_INT_STRLEN]; |
19e4448dee4f
my_hostname was broken because of recent change.
Timo Sirainen <tss@iki.fi>
parents:
15829
diff
changeset
|
20 char hostname[256]; |
19e4448dee4f
my_hostname was broken because of recent change.
Timo Sirainen <tss@iki.fi>
parents:
15829
diff
changeset
|
21 const char *value; |
0 | 22 |
15832
19e4448dee4f
my_hostname was broken because of recent change.
Timo Sirainen <tss@iki.fi>
parents:
15829
diff
changeset
|
23 /* allow calling hostpid_init() multiple times to reset hostname */ |
19e4448dee4f
my_hostname was broken because of recent change.
Timo Sirainen <tss@iki.fi>
parents:
15829
diff
changeset
|
24 i_free_and_null(my_hostname_dup); |
19e4448dee4f
my_hostname was broken because of recent change.
Timo Sirainen <tss@iki.fi>
parents:
15829
diff
changeset
|
25 i_free_and_null(my_domain); |
19e4448dee4f
my_hostname was broken because of recent change.
Timo Sirainen <tss@iki.fi>
parents:
15829
diff
changeset
|
26 |
19e4448dee4f
my_hostname was broken because of recent change.
Timo Sirainen <tss@iki.fi>
parents:
15829
diff
changeset
|
27 value = getenv(MY_HOSTNAME_ENV); |
19e4448dee4f
my_hostname was broken because of recent change.
Timo Sirainen <tss@iki.fi>
parents:
15829
diff
changeset
|
28 if (value == NULL) { |
15829
a6b40687c0a4
Master looks up system hostdomain now and sends it to child processes via environment.
Timo Sirainen <tss@iki.fi>
parents:
15827
diff
changeset
|
29 if (gethostname(hostname, sizeof(hostname)-1) < 0) |
a6b40687c0a4
Master looks up system hostdomain now and sends it to child processes via environment.
Timo Sirainen <tss@iki.fi>
parents:
15827
diff
changeset
|
30 i_fatal("gethostname() failed: %m"); |
a6b40687c0a4
Master looks up system hostdomain now and sends it to child processes via environment.
Timo Sirainen <tss@iki.fi>
parents:
15827
diff
changeset
|
31 hostname[sizeof(hostname)-1] = '\0'; |
15832
19e4448dee4f
my_hostname was broken because of recent change.
Timo Sirainen <tss@iki.fi>
parents:
15829
diff
changeset
|
32 value = hostname; |
15829
a6b40687c0a4
Master looks up system hostdomain now and sends it to child processes via environment.
Timo Sirainen <tss@iki.fi>
parents:
15827
diff
changeset
|
33 } |
0 | 34 |
15832
19e4448dee4f
my_hostname was broken because of recent change.
Timo Sirainen <tss@iki.fi>
parents:
15829
diff
changeset
|
35 if (value[0] == '\0' || |
19e4448dee4f
my_hostname was broken because of recent change.
Timo Sirainen <tss@iki.fi>
parents:
15829
diff
changeset
|
36 strcspn(value, HOSTNAME_DISALLOWED_CHARS) != strlen(value)) |
15851
cce9a8ebb386
hostpid_init() should die if hostname is invalid.
Timo Sirainen <tss@iki.fi>
parents:
15832
diff
changeset
|
37 i_fatal("Invalid system hostname: '%s'", value); |
15832
19e4448dee4f
my_hostname was broken because of recent change.
Timo Sirainen <tss@iki.fi>
parents:
15829
diff
changeset
|
38 my_hostname_dup = i_strdup(value); |
19e4448dee4f
my_hostname was broken because of recent change.
Timo Sirainen <tss@iki.fi>
parents:
15829
diff
changeset
|
39 my_hostname = my_hostname_dup; |
8596
2609eca99495
Added my_hostdomain() function which mbox code now uses instead of doing that internally.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
40 |
15202
a446d8bc2be5
hostpid_init(): Don't waste memory from data stack.
Timo Sirainen <tss@iki.fi>
parents:
14682
diff
changeset
|
41 i_snprintf(pid, sizeof(pid), "%lld", (long long)getpid()); |
8065
c832be51c7ac
hostpid_init(): If called again, update the hostname/pid.
Timo Sirainen <tss@iki.fi>
parents:
7086
diff
changeset
|
42 my_pid = pid; |
0 | 43 } |
8596
2609eca99495
Added my_hostdomain() function which mbox code now uses instead of doing that internally.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
44 |
15827 | 45 void hostpid_deinit(void) |
46 { | |
15832
19e4448dee4f
my_hostname was broken because of recent change.
Timo Sirainen <tss@iki.fi>
parents:
15829
diff
changeset
|
47 i_free(my_hostname_dup); |
15827 | 48 i_free(my_domain); |
49 } | |
50 | |
8596
2609eca99495
Added my_hostdomain() function which mbox code now uses instead of doing that internally.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
51 const char *my_hostdomain(void) |
2609eca99495
Added my_hostdomain() function which mbox code now uses instead of doing that internally.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
52 { |
2609eca99495
Added my_hostdomain() function which mbox code now uses instead of doing that internally.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
53 struct hostent *hent; |
2609eca99495
Added my_hostdomain() function which mbox code now uses instead of doing that internally.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
54 const char *name; |
2609eca99495
Added my_hostdomain() function which mbox code now uses instead of doing that internally.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
55 |
2609eca99495
Added my_hostdomain() function which mbox code now uses instead of doing that internally.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
56 if (my_domain == NULL) { |
15829
a6b40687c0a4
Master looks up system hostdomain now and sends it to child processes via environment.
Timo Sirainen <tss@iki.fi>
parents:
15827
diff
changeset
|
57 name = getenv(MY_HOSTDOMAIN_ENV); |
8596
2609eca99495
Added my_hostdomain() function which mbox code now uses instead of doing that internally.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
58 if (name == NULL) { |
15829
a6b40687c0a4
Master looks up system hostdomain now and sends it to child processes via environment.
Timo Sirainen <tss@iki.fi>
parents:
15827
diff
changeset
|
59 hent = gethostbyname(my_hostname); |
a6b40687c0a4
Master looks up system hostdomain now and sends it to child processes via environment.
Timo Sirainen <tss@iki.fi>
parents:
15827
diff
changeset
|
60 name = hent != NULL ? hent->h_name : NULL; |
a6b40687c0a4
Master looks up system hostdomain now and sends it to child processes via environment.
Timo Sirainen <tss@iki.fi>
parents:
15827
diff
changeset
|
61 if (name == NULL) { |
a6b40687c0a4
Master looks up system hostdomain now and sends it to child processes via environment.
Timo Sirainen <tss@iki.fi>
parents:
15827
diff
changeset
|
62 /* failed, use just the hostname */ |
a6b40687c0a4
Master looks up system hostdomain now and sends it to child processes via environment.
Timo Sirainen <tss@iki.fi>
parents:
15827
diff
changeset
|
63 name = my_hostname; |
a6b40687c0a4
Master looks up system hostdomain now and sends it to child processes via environment.
Timo Sirainen <tss@iki.fi>
parents:
15827
diff
changeset
|
64 } |
8596
2609eca99495
Added my_hostdomain() function which mbox code now uses instead of doing that internally.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
65 } |
2609eca99495
Added my_hostdomain() function which mbox code now uses instead of doing that internally.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
66 my_domain = i_strdup(name); |
2609eca99495
Added my_hostdomain() function which mbox code now uses instead of doing that internally.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
67 } |
2609eca99495
Added my_hostdomain() function which mbox code now uses instead of doing that internally.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
68 return my_domain; |
2609eca99495
Added my_hostdomain() function which mbox code now uses instead of doing that internally.
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
69 } |