Mercurial > dovecot > core-2.2
annotate src/lib/lib.c @ 22664:fea53c2725c0
director: Fix director_max_parallel_moves/kicks type
Should be uint, not time.
author | Timo Sirainen <timo.sirainen@dovecot.fi> |
---|---|
date | Thu, 09 Nov 2017 12:24:16 +0200 |
parents | f4076be3efe9 |
children | cb108f786fb4 |
rev | line source |
---|---|
21390
2e2563132d5f
Updated copyright notices to include the year 2017.
Stephan Bosch <stephan.bosch@dovecot.fi>
parents:
21117
diff
changeset
|
1 /* Copyright (c) 2001-2017 Dovecot authors, see the included COPYING file */ |
0 | 2 |
3 #include "lib.h" | |
22046
f4076be3efe9
lib: Add dovecot_build_info variable containing Dovecot build information.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21859
diff
changeset
|
4 #include "dovecot-version.h" |
15416
2aef2557bdf1
liblib: Added lib_atexit() to register process deinit callbacks.
Timo Sirainen <tss@iki.fi>
parents:
14691
diff
changeset
|
5 #include "array.h" |
9712
d48c8c0c1d29
lib_deinit() now frees memory used by env_put().
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
6 #include "env-util.h" |
1314 | 7 #include "hostpid.h" |
21113
38014afb65fc
lib: Make sure stdin/stdout/stderr have fd at startup and keep /dev/null always open
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21012
diff
changeset
|
8 #include "fd-close-on-exec.h" |
12859
02829f7f79c7
Replaced all getpw/gr*() to use i_getpw/gr*() with improved error handling.
Timo Sirainen <tss@iki.fi>
parents:
12822
diff
changeset
|
9 #include "ipwd.h" |
10431
32aba99c2e4f
process title hack: Free allocated memory at deinit.
Timo Sirainen <tss@iki.fi>
parents:
9712
diff
changeset
|
10 #include "process-title.h" |
21859
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21390
diff
changeset
|
11 #include "var-expand-private.h" |
0 | 12 |
21113
38014afb65fc
lib: Make sure stdin/stdout/stderr have fd at startup and keep /dev/null always open
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21012
diff
changeset
|
13 #include <fcntl.h> |
12492
8e30a07d8dd0
lib_init(): Call srand() with a bit better seed.
Timo Sirainen <tss@iki.fi>
parents:
10582
diff
changeset
|
14 #include <unistd.h> |
8e30a07d8dd0
lib_init(): Call srand() with a bit better seed.
Timo Sirainen <tss@iki.fi>
parents:
10582
diff
changeset
|
15 #include <sys/time.h> |
0 | 16 |
22046
f4076be3efe9
lib: Add dovecot_build_info variable containing Dovecot build information.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21859
diff
changeset
|
17 /* Mainly for including the full version information in core dumps. |
f4076be3efe9
lib: Add dovecot_build_info variable containing Dovecot build information.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21859
diff
changeset
|
18 NOTE: Don't set this const - otherwise it won't end up in core dumps. */ |
f4076be3efe9
lib: Add dovecot_build_info variable containing Dovecot build information.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21859
diff
changeset
|
19 char dovecot_build_info[] = DOVECOT_BUILD_INFO; |
f4076be3efe9
lib: Add dovecot_build_info variable containing Dovecot build information.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21859
diff
changeset
|
20 |
21012
5789122033bc
lib: Add function to see if lib is initialized
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
19552
diff
changeset
|
21 static bool lib_initialized = FALSE; |
21113
38014afb65fc
lib: Make sure stdin/stdout/stderr have fd at startup and keep /dev/null always open
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21012
diff
changeset
|
22 int dev_null_fd = -1; |
21012
5789122033bc
lib: Add function to see if lib is initialized
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
19552
diff
changeset
|
23 |
18047
54eb3436c83b
lib: Added lib_atexit_priority()
Timo Sirainen <tss@iki.fi>
parents:
17531
diff
changeset
|
24 struct atexit_callback { |
54eb3436c83b
lib: Added lib_atexit_priority()
Timo Sirainen <tss@iki.fi>
parents:
17531
diff
changeset
|
25 int priority; |
54eb3436c83b
lib: Added lib_atexit_priority()
Timo Sirainen <tss@iki.fi>
parents:
17531
diff
changeset
|
26 lib_atexit_callback_t *callback; |
54eb3436c83b
lib: Added lib_atexit_priority()
Timo Sirainen <tss@iki.fi>
parents:
17531
diff
changeset
|
27 }; |
54eb3436c83b
lib: Added lib_atexit_priority()
Timo Sirainen <tss@iki.fi>
parents:
17531
diff
changeset
|
28 |
54eb3436c83b
lib: Added lib_atexit_priority()
Timo Sirainen <tss@iki.fi>
parents:
17531
diff
changeset
|
29 static ARRAY(struct atexit_callback) atexit_callbacks = ARRAY_INIT; |
15416
2aef2557bdf1
liblib: Added lib_atexit() to register process deinit callbacks.
Timo Sirainen <tss@iki.fi>
parents:
14691
diff
changeset
|
30 |
2aef2557bdf1
liblib: Added lib_atexit() to register process deinit callbacks.
Timo Sirainen <tss@iki.fi>
parents:
14691
diff
changeset
|
31 int close_keep_errno(int *fd) |
2aef2557bdf1
liblib: Added lib_atexit() to register process deinit callbacks.
Timo Sirainen <tss@iki.fi>
parents:
14691
diff
changeset
|
32 { |
2aef2557bdf1
liblib: Added lib_atexit() to register process deinit callbacks.
Timo Sirainen <tss@iki.fi>
parents:
14691
diff
changeset
|
33 int ret, old_errno = errno; |
2aef2557bdf1
liblib: Added lib_atexit() to register process deinit callbacks.
Timo Sirainen <tss@iki.fi>
parents:
14691
diff
changeset
|
34 |
2aef2557bdf1
liblib: Added lib_atexit() to register process deinit callbacks.
Timo Sirainen <tss@iki.fi>
parents:
14691
diff
changeset
|
35 i_assert(*fd != -1); |
2aef2557bdf1
liblib: Added lib_atexit() to register process deinit callbacks.
Timo Sirainen <tss@iki.fi>
parents:
14691
diff
changeset
|
36 |
2aef2557bdf1
liblib: Added lib_atexit() to register process deinit callbacks.
Timo Sirainen <tss@iki.fi>
parents:
14691
diff
changeset
|
37 ret = close(*fd); |
2aef2557bdf1
liblib: Added lib_atexit() to register process deinit callbacks.
Timo Sirainen <tss@iki.fi>
parents:
14691
diff
changeset
|
38 *fd = -1; |
2aef2557bdf1
liblib: Added lib_atexit() to register process deinit callbacks.
Timo Sirainen <tss@iki.fi>
parents:
14691
diff
changeset
|
39 errno = old_errno; |
2aef2557bdf1
liblib: Added lib_atexit() to register process deinit callbacks.
Timo Sirainen <tss@iki.fi>
parents:
14691
diff
changeset
|
40 return ret; |
2aef2557bdf1
liblib: Added lib_atexit() to register process deinit callbacks.
Timo Sirainen <tss@iki.fi>
parents:
14691
diff
changeset
|
41 } |
2aef2557bdf1
liblib: Added lib_atexit() to register process deinit callbacks.
Timo Sirainen <tss@iki.fi>
parents:
14691
diff
changeset
|
42 |
21117
ad558e2f223e
lib: Add fd_close_maybe_stdio()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21113
diff
changeset
|
43 void fd_close_maybe_stdio(int *fd_in, int *fd_out) |
ad558e2f223e
lib: Add fd_close_maybe_stdio()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21113
diff
changeset
|
44 { |
ad558e2f223e
lib: Add fd_close_maybe_stdio()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21113
diff
changeset
|
45 int *fdp[2] = { fd_in, fd_out }; |
ad558e2f223e
lib: Add fd_close_maybe_stdio()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21113
diff
changeset
|
46 |
ad558e2f223e
lib: Add fd_close_maybe_stdio()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21113
diff
changeset
|
47 if (*fd_in == *fd_out) |
ad558e2f223e
lib: Add fd_close_maybe_stdio()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21113
diff
changeset
|
48 *fd_in = -1; |
ad558e2f223e
lib: Add fd_close_maybe_stdio()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21113
diff
changeset
|
49 |
ad558e2f223e
lib: Add fd_close_maybe_stdio()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21113
diff
changeset
|
50 for (unsigned int i = 0; i < N_ELEMENTS(fdp); i++) { |
ad558e2f223e
lib: Add fd_close_maybe_stdio()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21113
diff
changeset
|
51 if (*fdp[i] == -1) |
ad558e2f223e
lib: Add fd_close_maybe_stdio()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21113
diff
changeset
|
52 ; |
ad558e2f223e
lib: Add fd_close_maybe_stdio()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21113
diff
changeset
|
53 else if (*fdp[i] > 1) |
ad558e2f223e
lib: Add fd_close_maybe_stdio()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21113
diff
changeset
|
54 i_close_fd(fdp[i]); |
ad558e2f223e
lib: Add fd_close_maybe_stdio()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21113
diff
changeset
|
55 else if (dup2(dev_null_fd, *fdp[i]) == *fdp[i]) |
ad558e2f223e
lib: Add fd_close_maybe_stdio()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21113
diff
changeset
|
56 *fdp[i] = -1; |
ad558e2f223e
lib: Add fd_close_maybe_stdio()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21113
diff
changeset
|
57 else |
ad558e2f223e
lib: Add fd_close_maybe_stdio()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21113
diff
changeset
|
58 i_fatal("dup2(/dev/null, %d) failed: %m", *fdp[i]); |
ad558e2f223e
lib: Add fd_close_maybe_stdio()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21113
diff
changeset
|
59 } |
ad558e2f223e
lib: Add fd_close_maybe_stdio()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21113
diff
changeset
|
60 } |
ad558e2f223e
lib: Add fd_close_maybe_stdio()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21113
diff
changeset
|
61 |
19135
2ec2b0a51fec
lib: Added i_unlink() and i_unlink_if_exists()
Timo Sirainen <tss@iki.fi>
parents:
18137
diff
changeset
|
62 #undef i_unlink |
2ec2b0a51fec
lib: Added i_unlink() and i_unlink_if_exists()
Timo Sirainen <tss@iki.fi>
parents:
18137
diff
changeset
|
63 int i_unlink(const char *path, const char *source_fname, |
2ec2b0a51fec
lib: Added i_unlink() and i_unlink_if_exists()
Timo Sirainen <tss@iki.fi>
parents:
18137
diff
changeset
|
64 unsigned int source_linenum) |
2ec2b0a51fec
lib: Added i_unlink() and i_unlink_if_exists()
Timo Sirainen <tss@iki.fi>
parents:
18137
diff
changeset
|
65 { |
2ec2b0a51fec
lib: Added i_unlink() and i_unlink_if_exists()
Timo Sirainen <tss@iki.fi>
parents:
18137
diff
changeset
|
66 if (unlink(path) < 0) { |
2ec2b0a51fec
lib: Added i_unlink() and i_unlink_if_exists()
Timo Sirainen <tss@iki.fi>
parents:
18137
diff
changeset
|
67 i_error("unlink(%s) failed: %m (in %s:%u)", |
2ec2b0a51fec
lib: Added i_unlink() and i_unlink_if_exists()
Timo Sirainen <tss@iki.fi>
parents:
18137
diff
changeset
|
68 path, source_fname, source_linenum); |
2ec2b0a51fec
lib: Added i_unlink() and i_unlink_if_exists()
Timo Sirainen <tss@iki.fi>
parents:
18137
diff
changeset
|
69 return -1; |
2ec2b0a51fec
lib: Added i_unlink() and i_unlink_if_exists()
Timo Sirainen <tss@iki.fi>
parents:
18137
diff
changeset
|
70 } |
2ec2b0a51fec
lib: Added i_unlink() and i_unlink_if_exists()
Timo Sirainen <tss@iki.fi>
parents:
18137
diff
changeset
|
71 return 0; |
2ec2b0a51fec
lib: Added i_unlink() and i_unlink_if_exists()
Timo Sirainen <tss@iki.fi>
parents:
18137
diff
changeset
|
72 } |
2ec2b0a51fec
lib: Added i_unlink() and i_unlink_if_exists()
Timo Sirainen <tss@iki.fi>
parents:
18137
diff
changeset
|
73 |
2ec2b0a51fec
lib: Added i_unlink() and i_unlink_if_exists()
Timo Sirainen <tss@iki.fi>
parents:
18137
diff
changeset
|
74 #undef i_unlink_if_exists |
2ec2b0a51fec
lib: Added i_unlink() and i_unlink_if_exists()
Timo Sirainen <tss@iki.fi>
parents:
18137
diff
changeset
|
75 int i_unlink_if_exists(const char *path, const char *source_fname, |
2ec2b0a51fec
lib: Added i_unlink() and i_unlink_if_exists()
Timo Sirainen <tss@iki.fi>
parents:
18137
diff
changeset
|
76 unsigned int source_linenum) |
2ec2b0a51fec
lib: Added i_unlink() and i_unlink_if_exists()
Timo Sirainen <tss@iki.fi>
parents:
18137
diff
changeset
|
77 { |
2ec2b0a51fec
lib: Added i_unlink() and i_unlink_if_exists()
Timo Sirainen <tss@iki.fi>
parents:
18137
diff
changeset
|
78 if (unlink(path) == 0) |
2ec2b0a51fec
lib: Added i_unlink() and i_unlink_if_exists()
Timo Sirainen <tss@iki.fi>
parents:
18137
diff
changeset
|
79 return 1; |
2ec2b0a51fec
lib: Added i_unlink() and i_unlink_if_exists()
Timo Sirainen <tss@iki.fi>
parents:
18137
diff
changeset
|
80 else if (errno == ENOENT) |
2ec2b0a51fec
lib: Added i_unlink() and i_unlink_if_exists()
Timo Sirainen <tss@iki.fi>
parents:
18137
diff
changeset
|
81 return 0; |
2ec2b0a51fec
lib: Added i_unlink() and i_unlink_if_exists()
Timo Sirainen <tss@iki.fi>
parents:
18137
diff
changeset
|
82 else { |
2ec2b0a51fec
lib: Added i_unlink() and i_unlink_if_exists()
Timo Sirainen <tss@iki.fi>
parents:
18137
diff
changeset
|
83 i_error("unlink(%s) failed: %m (in %s:%u)", |
2ec2b0a51fec
lib: Added i_unlink() and i_unlink_if_exists()
Timo Sirainen <tss@iki.fi>
parents:
18137
diff
changeset
|
84 path, source_fname, source_linenum); |
2ec2b0a51fec
lib: Added i_unlink() and i_unlink_if_exists()
Timo Sirainen <tss@iki.fi>
parents:
18137
diff
changeset
|
85 return -1; |
2ec2b0a51fec
lib: Added i_unlink() and i_unlink_if_exists()
Timo Sirainen <tss@iki.fi>
parents:
18137
diff
changeset
|
86 } |
2ec2b0a51fec
lib: Added i_unlink() and i_unlink_if_exists()
Timo Sirainen <tss@iki.fi>
parents:
18137
diff
changeset
|
87 } |
2ec2b0a51fec
lib: Added i_unlink() and i_unlink_if_exists()
Timo Sirainen <tss@iki.fi>
parents:
18137
diff
changeset
|
88 |
19480
ed41702f14c2
doveadm: Fixed reseting getopt() with glibc when processing multiple commands in a single process.
Timo Sirainen <tss@iki.fi>
parents:
19191
diff
changeset
|
89 void i_getopt_reset(void) |
ed41702f14c2
doveadm: Fixed reseting getopt() with glibc when processing multiple commands in a single process.
Timo Sirainen <tss@iki.fi>
parents:
19191
diff
changeset
|
90 { |
ed41702f14c2
doveadm: Fixed reseting getopt() with glibc when processing multiple commands in a single process.
Timo Sirainen <tss@iki.fi>
parents:
19191
diff
changeset
|
91 #ifdef __GLIBC__ |
ed41702f14c2
doveadm: Fixed reseting getopt() with glibc when processing multiple commands in a single process.
Timo Sirainen <tss@iki.fi>
parents:
19191
diff
changeset
|
92 /* a) for subcommands allow -options anywhere in command line |
ed41702f14c2
doveadm: Fixed reseting getopt() with glibc when processing multiple commands in a single process.
Timo Sirainen <tss@iki.fi>
parents:
19191
diff
changeset
|
93 b) this is actually required for the reset to work (glibc bug?) */ |
ed41702f14c2
doveadm: Fixed reseting getopt() with glibc when processing multiple commands in a single process.
Timo Sirainen <tss@iki.fi>
parents:
19191
diff
changeset
|
94 optind = 0; |
ed41702f14c2
doveadm: Fixed reseting getopt() with glibc when processing multiple commands in a single process.
Timo Sirainen <tss@iki.fi>
parents:
19191
diff
changeset
|
95 #else |
ed41702f14c2
doveadm: Fixed reseting getopt() with glibc when processing multiple commands in a single process.
Timo Sirainen <tss@iki.fi>
parents:
19191
diff
changeset
|
96 optind = 1; |
ed41702f14c2
doveadm: Fixed reseting getopt() with glibc when processing multiple commands in a single process.
Timo Sirainen <tss@iki.fi>
parents:
19191
diff
changeset
|
97 #endif |
ed41702f14c2
doveadm: Fixed reseting getopt() with glibc when processing multiple commands in a single process.
Timo Sirainen <tss@iki.fi>
parents:
19191
diff
changeset
|
98 } |
ed41702f14c2
doveadm: Fixed reseting getopt() with glibc when processing multiple commands in a single process.
Timo Sirainen <tss@iki.fi>
parents:
19191
diff
changeset
|
99 |
15416
2aef2557bdf1
liblib: Added lib_atexit() to register process deinit callbacks.
Timo Sirainen <tss@iki.fi>
parents:
14691
diff
changeset
|
100 void lib_atexit(lib_atexit_callback_t *callback) |
2aef2557bdf1
liblib: Added lib_atexit() to register process deinit callbacks.
Timo Sirainen <tss@iki.fi>
parents:
14691
diff
changeset
|
101 { |
18047
54eb3436c83b
lib: Added lib_atexit_priority()
Timo Sirainen <tss@iki.fi>
parents:
17531
diff
changeset
|
102 lib_atexit_priority(callback, 0); |
54eb3436c83b
lib: Added lib_atexit_priority()
Timo Sirainen <tss@iki.fi>
parents:
17531
diff
changeset
|
103 } |
54eb3436c83b
lib: Added lib_atexit_priority()
Timo Sirainen <tss@iki.fi>
parents:
17531
diff
changeset
|
104 |
54eb3436c83b
lib: Added lib_atexit_priority()
Timo Sirainen <tss@iki.fi>
parents:
17531
diff
changeset
|
105 void lib_atexit_priority(lib_atexit_callback_t *callback, int priority) |
54eb3436c83b
lib: Added lib_atexit_priority()
Timo Sirainen <tss@iki.fi>
parents:
17531
diff
changeset
|
106 { |
54eb3436c83b
lib: Added lib_atexit_priority()
Timo Sirainen <tss@iki.fi>
parents:
17531
diff
changeset
|
107 struct atexit_callback *cb; |
54eb3436c83b
lib: Added lib_atexit_priority()
Timo Sirainen <tss@iki.fi>
parents:
17531
diff
changeset
|
108 const struct atexit_callback *callbacks; |
15416
2aef2557bdf1
liblib: Added lib_atexit() to register process deinit callbacks.
Timo Sirainen <tss@iki.fi>
parents:
14691
diff
changeset
|
109 unsigned int i, count; |
2aef2557bdf1
liblib: Added lib_atexit() to register process deinit callbacks.
Timo Sirainen <tss@iki.fi>
parents:
14691
diff
changeset
|
110 |
2aef2557bdf1
liblib: Added lib_atexit() to register process deinit callbacks.
Timo Sirainen <tss@iki.fi>
parents:
14691
diff
changeset
|
111 if (!array_is_created(&atexit_callbacks)) |
2aef2557bdf1
liblib: Added lib_atexit() to register process deinit callbacks.
Timo Sirainen <tss@iki.fi>
parents:
14691
diff
changeset
|
112 i_array_init(&atexit_callbacks, 8); |
2aef2557bdf1
liblib: Added lib_atexit() to register process deinit callbacks.
Timo Sirainen <tss@iki.fi>
parents:
14691
diff
changeset
|
113 else { |
2aef2557bdf1
liblib: Added lib_atexit() to register process deinit callbacks.
Timo Sirainen <tss@iki.fi>
parents:
14691
diff
changeset
|
114 /* skip if it's already added */ |
2aef2557bdf1
liblib: Added lib_atexit() to register process deinit callbacks.
Timo Sirainen <tss@iki.fi>
parents:
14691
diff
changeset
|
115 callbacks = array_get(&atexit_callbacks, &count); |
2aef2557bdf1
liblib: Added lib_atexit() to register process deinit callbacks.
Timo Sirainen <tss@iki.fi>
parents:
14691
diff
changeset
|
116 for (i = count; i > 0; i--) { |
18047
54eb3436c83b
lib: Added lib_atexit_priority()
Timo Sirainen <tss@iki.fi>
parents:
17531
diff
changeset
|
117 if (callbacks[i-1].callback == callback) { |
54eb3436c83b
lib: Added lib_atexit_priority()
Timo Sirainen <tss@iki.fi>
parents:
17531
diff
changeset
|
118 i_assert(callbacks[i-1].priority == priority); |
15416
2aef2557bdf1
liblib: Added lib_atexit() to register process deinit callbacks.
Timo Sirainen <tss@iki.fi>
parents:
14691
diff
changeset
|
119 return; |
18047
54eb3436c83b
lib: Added lib_atexit_priority()
Timo Sirainen <tss@iki.fi>
parents:
17531
diff
changeset
|
120 } |
15416
2aef2557bdf1
liblib: Added lib_atexit() to register process deinit callbacks.
Timo Sirainen <tss@iki.fi>
parents:
14691
diff
changeset
|
121 } |
2aef2557bdf1
liblib: Added lib_atexit() to register process deinit callbacks.
Timo Sirainen <tss@iki.fi>
parents:
14691
diff
changeset
|
122 } |
18047
54eb3436c83b
lib: Added lib_atexit_priority()
Timo Sirainen <tss@iki.fi>
parents:
17531
diff
changeset
|
123 cb = array_append_space(&atexit_callbacks); |
54eb3436c83b
lib: Added lib_atexit_priority()
Timo Sirainen <tss@iki.fi>
parents:
17531
diff
changeset
|
124 cb->priority = priority; |
54eb3436c83b
lib: Added lib_atexit_priority()
Timo Sirainen <tss@iki.fi>
parents:
17531
diff
changeset
|
125 cb->callback = callback; |
54eb3436c83b
lib: Added lib_atexit_priority()
Timo Sirainen <tss@iki.fi>
parents:
17531
diff
changeset
|
126 } |
54eb3436c83b
lib: Added lib_atexit_priority()
Timo Sirainen <tss@iki.fi>
parents:
17531
diff
changeset
|
127 |
54eb3436c83b
lib: Added lib_atexit_priority()
Timo Sirainen <tss@iki.fi>
parents:
17531
diff
changeset
|
128 static int atexit_callback_priority_cmp(const struct atexit_callback *cb1, |
54eb3436c83b
lib: Added lib_atexit_priority()
Timo Sirainen <tss@iki.fi>
parents:
17531
diff
changeset
|
129 const struct atexit_callback *cb2) |
54eb3436c83b
lib: Added lib_atexit_priority()
Timo Sirainen <tss@iki.fi>
parents:
17531
diff
changeset
|
130 { |
54eb3436c83b
lib: Added lib_atexit_priority()
Timo Sirainen <tss@iki.fi>
parents:
17531
diff
changeset
|
131 return cb1->priority - cb2->priority; |
15416
2aef2557bdf1
liblib: Added lib_atexit() to register process deinit callbacks.
Timo Sirainen <tss@iki.fi>
parents:
14691
diff
changeset
|
132 } |
2aef2557bdf1
liblib: Added lib_atexit() to register process deinit callbacks.
Timo Sirainen <tss@iki.fi>
parents:
14691
diff
changeset
|
133 |
16923
5eae6c491d44
Call lib_atexit() callbacks before destroying the lib-master ioloop.
Timo Sirainen <tss@iki.fi>
parents:
15827
diff
changeset
|
134 void lib_atexit_run(void) |
5eae6c491d44
Call lib_atexit() callbacks before destroying the lib-master ioloop.
Timo Sirainen <tss@iki.fi>
parents:
15827
diff
changeset
|
135 { |
18047
54eb3436c83b
lib: Added lib_atexit_priority()
Timo Sirainen <tss@iki.fi>
parents:
17531
diff
changeset
|
136 const struct atexit_callback *cb; |
16923
5eae6c491d44
Call lib_atexit() callbacks before destroying the lib-master ioloop.
Timo Sirainen <tss@iki.fi>
parents:
15827
diff
changeset
|
137 |
5eae6c491d44
Call lib_atexit() callbacks before destroying the lib-master ioloop.
Timo Sirainen <tss@iki.fi>
parents:
15827
diff
changeset
|
138 if (array_is_created(&atexit_callbacks)) { |
18047
54eb3436c83b
lib: Added lib_atexit_priority()
Timo Sirainen <tss@iki.fi>
parents:
17531
diff
changeset
|
139 array_sort(&atexit_callbacks, atexit_callback_priority_cmp); |
54eb3436c83b
lib: Added lib_atexit_priority()
Timo Sirainen <tss@iki.fi>
parents:
17531
diff
changeset
|
140 array_foreach(&atexit_callbacks, cb) |
54eb3436c83b
lib: Added lib_atexit_priority()
Timo Sirainen <tss@iki.fi>
parents:
17531
diff
changeset
|
141 (*cb->callback)(); |
16923
5eae6c491d44
Call lib_atexit() callbacks before destroying the lib-master ioloop.
Timo Sirainen <tss@iki.fi>
parents:
15827
diff
changeset
|
142 array_free(&atexit_callbacks); |
5eae6c491d44
Call lib_atexit() callbacks before destroying the lib-master ioloop.
Timo Sirainen <tss@iki.fi>
parents:
15827
diff
changeset
|
143 } |
5eae6c491d44
Call lib_atexit() callbacks before destroying the lib-master ioloop.
Timo Sirainen <tss@iki.fi>
parents:
15827
diff
changeset
|
144 } |
5eae6c491d44
Call lib_atexit() callbacks before destroying the lib-master ioloop.
Timo Sirainen <tss@iki.fi>
parents:
15827
diff
changeset
|
145 |
21113
38014afb65fc
lib: Make sure stdin/stdout/stderr have fd at startup and keep /dev/null always open
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21012
diff
changeset
|
146 static void lib_open_non_stdio_dev_null(void) |
38014afb65fc
lib: Make sure stdin/stdout/stderr have fd at startup and keep /dev/null always open
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21012
diff
changeset
|
147 { |
38014afb65fc
lib: Make sure stdin/stdout/stderr have fd at startup and keep /dev/null always open
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21012
diff
changeset
|
148 dev_null_fd = open("/dev/null", O_WRONLY); |
38014afb65fc
lib: Make sure stdin/stdout/stderr have fd at startup and keep /dev/null always open
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21012
diff
changeset
|
149 if (dev_null_fd == -1) |
38014afb65fc
lib: Make sure stdin/stdout/stderr have fd at startup and keep /dev/null always open
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21012
diff
changeset
|
150 i_fatal("open(/dev/null) failed: %m"); |
38014afb65fc
lib: Make sure stdin/stdout/stderr have fd at startup and keep /dev/null always open
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21012
diff
changeset
|
151 /* Make sure stdin, stdout and stderr fds exist. We especially rely on |
38014afb65fc
lib: Make sure stdin/stdout/stderr have fd at startup and keep /dev/null always open
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21012
diff
changeset
|
152 stderr being available and a lot of code doesn't like fd being 0. |
38014afb65fc
lib: Make sure stdin/stdout/stderr have fd at startup and keep /dev/null always open
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21012
diff
changeset
|
153 We'll open /dev/null as write-only also for stdin, since if any |
38014afb65fc
lib: Make sure stdin/stdout/stderr have fd at startup and keep /dev/null always open
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21012
diff
changeset
|
154 reads are attempted from it we'll want them to fail. */ |
38014afb65fc
lib: Make sure stdin/stdout/stderr have fd at startup and keep /dev/null always open
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21012
diff
changeset
|
155 while (dev_null_fd < STDERR_FILENO) { |
38014afb65fc
lib: Make sure stdin/stdout/stderr have fd at startup and keep /dev/null always open
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21012
diff
changeset
|
156 dev_null_fd = dup(dev_null_fd); |
38014afb65fc
lib: Make sure stdin/stdout/stderr have fd at startup and keep /dev/null always open
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21012
diff
changeset
|
157 if (dev_null_fd == -1) |
38014afb65fc
lib: Make sure stdin/stdout/stderr have fd at startup and keep /dev/null always open
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21012
diff
changeset
|
158 i_fatal("dup(/dev/null) failed: %m"); |
38014afb65fc
lib: Make sure stdin/stdout/stderr have fd at startup and keep /dev/null always open
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21012
diff
changeset
|
159 } |
38014afb65fc
lib: Make sure stdin/stdout/stderr have fd at startup and keep /dev/null always open
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21012
diff
changeset
|
160 /* close the actual /dev/null fd on exec*(), but keep it in stdio fds */ |
38014afb65fc
lib: Make sure stdin/stdout/stderr have fd at startup and keep /dev/null always open
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21012
diff
changeset
|
161 fd_close_on_exec(dev_null_fd, TRUE); |
38014afb65fc
lib: Make sure stdin/stdout/stderr have fd at startup and keep /dev/null always open
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21012
diff
changeset
|
162 } |
38014afb65fc
lib: Make sure stdin/stdout/stderr have fd at startup and keep /dev/null always open
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21012
diff
changeset
|
163 |
0 | 164 void lib_init(void) |
165 { | |
12492
8e30a07d8dd0
lib_init(): Call srand() with a bit better seed.
Timo Sirainen <tss@iki.fi>
parents:
10582
diff
changeset
|
166 struct timeval tv; |
21012
5789122033bc
lib: Add function to see if lib is initialized
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
19552
diff
changeset
|
167 i_assert(!lib_initialized); |
12492
8e30a07d8dd0
lib_init(): Call srand() with a bit better seed.
Timo Sirainen <tss@iki.fi>
parents:
10582
diff
changeset
|
168 |
0 | 169 /* standard way to get rand() return different values. */ |
12492
8e30a07d8dd0
lib_init(): Call srand() with a bit better seed.
Timo Sirainen <tss@iki.fi>
parents:
10582
diff
changeset
|
170 if (gettimeofday(&tv, NULL) < 0) |
8e30a07d8dd0
lib_init(): Call srand() with a bit better seed.
Timo Sirainen <tss@iki.fi>
parents:
10582
diff
changeset
|
171 i_fatal("gettimeofday(): %m"); |
17531
14f505c1c6f4
lib: use new srand() wrapper in lib
Phil Carmody <phil@dovecot.fi>
parents:
17447
diff
changeset
|
172 rand_set_seed((unsigned int) (tv.tv_sec ^ tv.tv_usec ^ getpid())); |
0 | 173 |
399
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
192
diff
changeset
|
174 data_stack_init(); |
1313
c4fa748b9f24
Call the hostpid_init() always at startup. Easier than doing it all around.
Timo Sirainen <tss@iki.fi>
parents:
801
diff
changeset
|
175 hostpid_init(); |
21113
38014afb65fc
lib: Make sure stdin/stdout/stderr have fd at startup and keep /dev/null always open
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21012
diff
changeset
|
176 lib_open_non_stdio_dev_null(); |
21859
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21390
diff
changeset
|
177 var_expand_extensions_init(); |
21113
38014afb65fc
lib: Make sure stdin/stdout/stderr have fd at startup and keep /dev/null always open
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21012
diff
changeset
|
178 |
21012
5789122033bc
lib: Add function to see if lib is initialized
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
19552
diff
changeset
|
179 lib_initialized = TRUE; |
5789122033bc
lib: Add function to see if lib is initialized
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
19552
diff
changeset
|
180 } |
5789122033bc
lib: Add function to see if lib is initialized
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
19552
diff
changeset
|
181 |
5789122033bc
lib: Add function to see if lib is initialized
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
19552
diff
changeset
|
182 bool lib_is_initialized(void) |
5789122033bc
lib: Add function to see if lib is initialized
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
19552
diff
changeset
|
183 { |
5789122033bc
lib: Add function to see if lib is initialized
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
19552
diff
changeset
|
184 return lib_initialized; |
0 | 185 } |
186 | |
187 void lib_deinit(void) | |
188 { | |
21012
5789122033bc
lib: Add function to see if lib is initialized
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
19552
diff
changeset
|
189 i_assert(lib_initialized); |
5789122033bc
lib: Add function to see if lib is initialized
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
19552
diff
changeset
|
190 lib_initialized = FALSE; |
16923
5eae6c491d44
Call lib_atexit() callbacks before destroying the lib-master ioloop.
Timo Sirainen <tss@iki.fi>
parents:
15827
diff
changeset
|
191 lib_atexit_run(); |
12859
02829f7f79c7
Replaced all getpw/gr*() to use i_getpw/gr*() with improved error handling.
Timo Sirainen <tss@iki.fi>
parents:
12822
diff
changeset
|
192 ipwd_deinit(); |
15827 | 193 hostpid_deinit(); |
21859
954374379116
lib: Add extension support for var-expand
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
21390
diff
changeset
|
194 var_expand_extensions_deinit(); |
21113
38014afb65fc
lib: Make sure stdin/stdout/stderr have fd at startup and keep /dev/null always open
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
21012
diff
changeset
|
195 i_close_fd(&dev_null_fd); |
399
383503837741
s/temporary memory pool/data stack/ which is the correct name for it.
Timo Sirainen <tss@iki.fi>
parents:
192
diff
changeset
|
196 data_stack_deinit(); |
9712
d48c8c0c1d29
lib_deinit() now frees memory used by env_put().
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
197 env_deinit(); |
d48c8c0c1d29
lib_deinit() now frees memory used by env_put().
Timo Sirainen <tss@iki.fi>
parents:
8590
diff
changeset
|
198 failures_deinit(); |
10431
32aba99c2e4f
process title hack: Free allocated memory at deinit.
Timo Sirainen <tss@iki.fi>
parents:
9712
diff
changeset
|
199 process_title_deinit(); |
0 | 200 } |