Debian Codegraph @

src/shared/async.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <sched.h>
4 #include <sys/prctl.h>
5 #include <sys/wait.h>
6 #include <unistd.h>
7
8 #include "async.h"
9 #include "errno-util.h"
10 #include "fd-util.h"
11 #include "log.h"
12 #include "process-util.h"
13 #include "rm-rf.h"
14 #include "signal-util.h"
15
16 int asynchronous_sync(PidRef *ret_pid) {
17 int r;
18
19 /* This forks off an invocation of fork() as a child process, in order to initiate synchronization to
20 * disk. Note that we implement this as helper process rather than thread as we don't want the sync() to hang our
21 * original process ever, and a thread would do that as the process can't exit with threads hanging in blocking
22 * syscalls. */
23
24 r = pidref_safe_fork("(sd-sync)", FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|(ret_pid ? 0 : FORK_DETACH), ret_pid);
25 if (r < 0)
26 return r;
27 if (r == 0) {
28 /* Child process */
29 sync();
30 _exit(EXIT_SUCCESS);
31 }
32
33 return 0;
34 }
35
36 int asynchronous_fsync(int fd, PidRef *ret_pid) {
37 int r;
38
39 assert(fd >= 0);
40 /* Same as asynchronous_sync() above, but calls fsync() on a specific fd */
41
42 r = pidref_safe_fork_full(
43 "(sd-fsync)",
44 /* stdio_fds= */ NULL,
45 /* except_fds= */ &fd,
46 /* n_except_fds= */ 1,
47 FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|(ret_pid ? 0 : FORK_DETACH),
48 ret_pid);
49 if (r < 0)
50 return r;
51 if (r == 0) {
52 /* Child process */
53 (void) fsync(fd);
54 _exit(EXIT_SUCCESS);
55 }
56
57 return 0;
58 }
59
60 /* We encode the fd to close in the userdata pointer as an unsigned value. The highest bit indicates whether
61 * we need to fork again */
62 #define NEED_DOUBLE_FORK (1U << (sizeof(unsigned) * 8 - 1))
63
64 static int close_func(void *p) {
65 unsigned v = PTR_TO_UINT(p);
66
67 (void) prctl(PR_SET_NAME, (unsigned long*) "(sd-close)");
68
69 /* Note: 💣 This function is invoked in a child process created via glibc's clone() wrapper. In such
70 * children memory allocation is not allowed, since glibc does not release malloc mutexes in
71 * clone() 💣 */
72
73 if (v & NEED_DOUBLE_FORK) {
74 pid_t pid;
75
76 v &= ~NEED_DOUBLE_FORK;
77
78 /* This inner child will be reparented to the subreaper/PID 1. Here we turn on SIGCHLD, so
79 * that the reaper knows when it's time to reap. */
80 pid = clone_with_nested_stack(close_func, SIGCHLD|CLONE_FILES, UINT_TO_PTR(v));
81 if (pid >= 0)
82 return 0;
83 }
84
85 close((int) v); /* no assert() here, we are in the child and the result would be eaten up anyway */
86 return 0;
87 }
88
89 int asynchronous_close(int fd) {
90 unsigned v;
91 pid_t pid;
92 int r;
93
94 /* This is supposed to behave similar to safe_close(), but actually invoke close() asynchronously, so
95 * that it will never block. Ideally the kernel would have an API for this, but it doesn't, so we
96 * work around it, and hide this as a far away as we can.
97 *
98 * It is important to us that we don't use threads (via glibc pthread) in PID 1, hence we'll do a
99 * minimal subprocess instead which shares our fd table via CLONE_FILES. */
100
101 if (fd < 0)
102 return -EBADF; /* already invalid */
103
104 PROTECT_ERRNO;
105
106 v = (unsigned) fd;
107
108 /* We want to fork off a process that is automatically reaped. For that we'd usually double-fork. But
109 * we can optimize this a bit: if we are PID 1 or a subreaper anyway (the systemd service manager
110 * process qualifies as this), we can avoid the double forking, since the double forked process would
111 * be reparented back to us anyway. */
112 r = is_reaper_process();
113 if (r < 0)
114 log_debug_errno(r, "Cannot determine if we are a reaper process, assuming we are not: %m");
115 if (r <= 0)
116 v |= NEED_DOUBLE_FORK;
117
118 pid = clone_with_nested_stack(close_func, CLONE_FILES | ((v & NEED_DOUBLE_FORK) ? 0 : SIGCHLD), UINT_TO_PTR(v));
119 if (pid < 0)
120 safe_close(fd); /* local fallback */
121 else if (v & NEED_DOUBLE_FORK) {
122
123 /* Reap the intermediate child. Key here is that we specify __WCLONE, since we didn't ask for
124 * any signal to be sent to us on process exit, and otherwise waitid() would refuse waiting
125 * then.
126 *
127 * We usually prefer calling waitid(), but before kernel 4.7 it didn't support __WCLONE while
128 * waitpid() did. Hence let's use waitpid() here, it's good enough for our purposes here. */
129 for (;;)
130 if (waitpid(pid, NULL, __WCLONE) >= 0 || errno != EINTR)
131 break;
132 }
133
134 return -EBADF; /* return an invalidated fd */
135 }
136
137 void asynchronous_close_many(const int fds[], size_t n_fds) {
138 assert(fds || n_fds == 0);
139
140 FOREACH_ARRAY(i, fds, n_fds)
141 asynchronous_close(*i);
142 }
143
144 int asynchronous_rm_rf(const char *p, RemoveFlags flags) {
145 int r;
146
147 assert(p);
148
149 /* Forks off a child that destroys the specified path. This will be best effort only, i.e. the child
150 * will attempt to do its thing, but we won't wait for it or check its success. */
151
152 r = pidref_safe_fork("(sd-rmrf)", FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_DETACH, /* ret= */ NULL);
153 if (r != 0)
154 return r;
155
156 /* Child */
157
158 /* Let's block SIGTERM here, to grant the operation more time on e.g. final killing spree
159 * during shutdown. If this gets stalled pid1 would eventually send SIGKILL to us. */
160 BLOCK_SIGNALS(SIGTERM);
161
162 r = rm_rf(p, flags);
163 if (r < 0) {
164 log_debug_errno(r, "Failed to rm -rf '%s', ignoring: %m", p);
165 _exit(EXIT_FAILURE); /* This is a detached process, hence no one really cares, but who knows
166 * maybe it's good for debugging/tracing to return an exit code
167 * indicative of our failure here. */
168 }
169
170 _exit(EXIT_SUCCESS);
171 }