Coverage Report

Created: 2026-10-31 06:36

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/dovecot/src/lib/lib.c
Line
Count
Source
1
/* Copyright (c) Dovecot authors, see top-level COPYING file */
2
3
#include "lib.h"
4
#include "dovecot-version.h"
5
#include "array.h"
6
#include "event-filter.h"
7
#include "env-util.h"
8
#include "hash.h"
9
#include "hostpid.h"
10
#include "ipwd.h"
11
#include "process-title.h"
12
#include "restrict-access.h"
13
#include "randgen.h"
14
15
#include <fcntl.h>
16
#include <unistd.h>
17
#include <sys/time.h>
18
#ifdef HAVE_FACCESSAT2
19
#  include <asm/unistd.h>
20
#endif
21
22
/* Mainly for including the full version information in core dumps.
23
   NOTE: Don't set this const - otherwise it won't end up in core dumps. */
24
char dovecot_build_info[] = DOVECOT_BUILD_INFO;
25
26
static bool lib_initialized = FALSE;
27
int dev_null_fd = -1;
28
29
struct atexit_callback {
30
  int priority;
31
  lib_atexit_callback_t *callback;
32
};
33
34
static ARRAY(struct atexit_callback) atexit_callbacks = ARRAY_INIT;
35
static bool lib_registered_atexit = FALSE;
36
static bool lib_clean_exit;
37
38
/* The original faccessat() syscall didn't handle the flags parameter.  glibc
39
   v2.33's faccessat() started using the new Linux faccessat2() syscall for that
40
   reason.  However, we can still use the faccessat2() syscall directly in some
41
   Linux distros to avoid this problem, so just do it here when possible. */
42
int i_faccessat2(int dirfd, const char *pathname, int mode, int flags)
43
0
{
44
#ifdef HAVE_FACCESSAT2
45
  static bool faccessat2_unavailable = FALSE;
46
  if (!faccessat2_unavailable) {
47
    /* On bullseye the syscall is available,
48
       but the glibc wrapping function is not. */
49
    long ret = syscall(__NR_faccessat2, dirfd, pathname, mode, flags);
50
    faccessat2_unavailable = ret == -1 && errno == ENOSYS;
51
    if (!faccessat2_unavailable)
52
      return (int)ret;
53
  }
54
#endif
55
0
  return faccessat(dirfd, pathname, mode, flags);
56
0
}
57
58
#undef i_unlink
59
int i_unlink(const char *path, const char *source_fname,
60
       unsigned int source_linenum)
61
0
{
62
0
  if (unlink(path) < 0) {
63
0
    i_error("unlink(%s) failed: %m (in %s:%u)",
64
0
      path, source_fname, source_linenum);
65
0
    return -1;
66
0
  }
67
0
  return 0;
68
0
}
69
70
#undef i_unlink_if_exists
71
int i_unlink_if_exists(const char *path, const char *source_fname,
72
           unsigned int source_linenum)
73
0
{
74
0
  if (unlink(path) == 0)
75
0
    return 1;
76
0
  else if (errno == ENOENT)
77
0
    return 0;
78
0
  else {
79
0
    i_error("unlink(%s) failed: %m (in %s:%u)",
80
0
      path, source_fname, source_linenum);
81
0
    return -1;
82
0
  }
83
0
}
84
85
void i_getopt_reset(void)
86
0
{
87
0
#ifdef __GLIBC__
88
  /* a) for subcommands allow -options anywhere in command line
89
     b) this is actually required for the reset to work (glibc bug?) */
90
0
  optind = 0;
91
#else
92
  optind = 1;
93
#endif
94
0
}
95
96
void lib_atexit(lib_atexit_callback_t *callback)
97
0
{
98
0
  lib_atexit_priority(callback, 0);
99
0
}
100
101
void lib_atexit_priority(lib_atexit_callback_t *callback, int priority)
102
0
{
103
0
  struct atexit_callback *cb;
104
0
  const struct atexit_callback *callbacks;
105
0
  unsigned int i, count;
106
107
0
  if (!array_is_created(&atexit_callbacks))
108
0
    i_array_init(&atexit_callbacks, 8);
109
0
  else {
110
    /* skip if it's already added */
111
0
    callbacks = array_get(&atexit_callbacks, &count);
112
0
    for (i = count; i > 0; i--) {
113
0
      if (callbacks[i-1].callback == callback) {
114
0
        i_assert(callbacks[i-1].priority == priority);
115
0
        return;
116
0
      }
117
0
    }
118
0
  }
119
0
  cb = array_append_space(&atexit_callbacks);
120
0
  cb->priority = priority;
121
0
  cb->callback = callback;
122
0
}
123
124
static int atexit_callback_priority_cmp(const struct atexit_callback *cb1,
125
          const struct atexit_callback *cb2)
126
0
{
127
0
  return cb1->priority - cb2->priority;
128
0
}
129
130
void lib_atexit_run(void)
131
0
{
132
0
  const struct atexit_callback *cb;
133
134
0
  if (array_is_created(&atexit_callbacks)) {
135
0
    array_sort(&atexit_callbacks, atexit_callback_priority_cmp);
136
0
    array_foreach(&atexit_callbacks, cb)
137
0
      (*cb->callback)();
138
0
    array_free(&atexit_callbacks);
139
0
  }
140
0
}
141
142
static void lib_open_non_stdio_dev_null(void)
143
0
{
144
0
  dev_null_fd = open("/dev/null", O_WRONLY);
145
0
  if (dev_null_fd == -1)
146
0
    i_fatal("open(/dev/null) failed: %m");
147
  /* Make sure stdin, stdout and stderr fds exist. We especially rely on
148
     stderr being available and a lot of code doesn't like fd being 0.
149
     We'll open /dev/null as write-only also for stdin, since if any
150
     reads are attempted from it we'll want them to fail. */
151
0
  while (dev_null_fd < STDERR_FILENO) {
152
0
    dev_null_fd = dup(dev_null_fd);
153
0
    if (dev_null_fd == -1)
154
0
      i_fatal("dup(/dev/null) failed: %m");
155
0
  }
156
  /* close the actual /dev/null fd on exec*(), but keep it in stdio fds */
157
0
  fd_close_on_exec(dev_null_fd, TRUE);
158
0
}
159
160
void lib_set_clean_exit(bool set)
161
0
{
162
0
  lib_clean_exit = set;
163
0
}
164
165
void lib_exit(int status)
166
0
{
167
0
  lib_set_clean_exit(TRUE);
168
0
  exit(status);
169
0
}
170
171
static void lib_atexit_handler(void)
172
0
{
173
0
  if (!lib_initialized)
174
0
    return;
175
176
  /* We're already in exit code path. Avoid using any functions that
177
     might cause strange breakage. Especially anything that could call
178
     exit() again could cause infinite looping in some OSes. */
179
0
  if (!lib_clean_exit) {
180
0
    const char *error = "Unexpected exit - converting to abort\n";
181
0
    if (write(STDERR_FILENO, error, strlen(error)) < 0) {
182
      /* ignore */
183
0
    }
184
0
    abort();
185
0
  }
186
0
}
187
188
void lib_init(void)
189
0
{
190
0
  i_assert(!lib_initialized);
191
0
  random_init();
192
0
  hash_init();
193
0
  data_stack_init();
194
0
  hostpid_init();
195
0
  lib_open_non_stdio_dev_null();
196
0
  lib_event_init();
197
0
  event_filter_init();
198
199
  /* Default to clean exit. Otherwise there would be too many accidents
200
     with e.g. command line parsing errors that try to return instead
201
     of using lib_exit(). master_service_init_finish() will change this
202
     again to be FALSE. */
203
0
  lib_set_clean_exit(TRUE);
204
0
  if (!lib_registered_atexit) {
205
0
    atexit(lib_atexit_handler);
206
0
    lib_registered_atexit = TRUE;
207
0
  }
208
209
0
  lib_initialized = TRUE;
210
0
}
211
212
bool lib_is_initialized(void)
213
0
{
214
0
  return lib_initialized;
215
0
}
216
217
void lib_deinit(void)
218
0
{
219
0
  i_assert(lib_initialized);
220
0
  lib_initialized = FALSE;
221
0
  lib_atexit_run();
222
0
  ipwd_deinit();
223
0
  hostpid_deinit();
224
0
  event_filter_deinit();
225
0
  data_stack_deinit_event();
226
0
  lib_event_deinit();
227
0
  restrict_access_deinit();
228
0
  i_close_fd(&dev_null_fd);
229
0
  data_stack_deinit();
230
0
  failures_deinit();
231
0
  process_title_deinit();
232
0
  random_deinit();
233
234
0
  lib_clean_exit = TRUE;
235
0
}