]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/async.c
Merge pull request #7675 from shawnl/unaligned
[thirdparty/systemd.git] / src / basic / async.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 This file is part of systemd.
4
5 Copyright 2013 Lennart Poettering
6
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
11
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
19 ***/
20
21 #include <errno.h>
22 #include <pthread.h>
23 #include <stddef.h>
24 #include <unistd.h>
25
26 #include "async.h"
27 #include "fd-util.h"
28 #include "log.h"
29 #include "macro.h"
30 #include "process-util.h"
31 #include "signal-util.h"
32 #include "util.h"
33
34 int asynchronous_job(void* (*func)(void *p), void *arg) {
35 sigset_t ss, saved_ss;
36 pthread_attr_t a;
37 pthread_t t;
38 int r, k;
39
40 /* It kinda sucks that we have to resort to threads to implement an asynchronous close(), but well, such is
41 * life. */
42
43 r = pthread_attr_init(&a);
44 if (r > 0)
45 return -r;
46
47 r = pthread_attr_setdetachstate(&a, PTHREAD_CREATE_DETACHED);
48 if (r > 0) {
49 r = -r;
50 goto finish;
51 }
52
53 if (sigfillset(&ss) < 0) {
54 r = -errno;
55 goto finish;
56 }
57
58 /* Block all signals before forking off the thread, so that the new thread is started with all signals
59 * blocked. This way the existence of the new thread won't affect signal handling in other threads. */
60
61 r = pthread_sigmask(SIG_BLOCK, &ss, &saved_ss);
62 if (r > 0) {
63 r = -r;
64 goto finish;
65 }
66
67 r = pthread_create(&t, &a, func, arg);
68
69 k = pthread_sigmask(SIG_SETMASK, &saved_ss, NULL);
70
71 if (r > 0)
72 r = -r;
73 else if (k > 0)
74 r = -k;
75 else
76 r = 0;
77
78 finish:
79 pthread_attr_destroy(&a);
80 return r;
81 }
82
83 int asynchronous_sync(pid_t *ret_pid) {
84 int r;
85
86 /* This forks off an invocation of fork() as a child process, in order to initiate synchronization to
87 * disk. Note that we implement this as helper process rather than thread as we don't want the sync() to hang our
88 * original process ever, and a thread would do that as the process can't exit with threads hanging in blocking
89 * syscalls. */
90
91 r = safe_fork("(sd-sync)", FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS, ret_pid);
92 if (r < 0)
93 return r;
94 if (r == 0) {
95 /* Child process */
96 (void) sync();
97 _exit(EXIT_SUCCESS);
98 }
99
100 return 0;
101 }
102
103 static void *close_thread(void *p) {
104 (void) pthread_setname_np(pthread_self(), "close");
105
106 assert_se(close_nointr(PTR_TO_FD(p)) != -EBADF);
107 return NULL;
108 }
109
110 int asynchronous_close(int fd) {
111 int r;
112
113 /* This is supposed to behave similar to safe_close(), but
114 * actually invoke close() asynchronously, so that it will
115 * never block. Ideally the kernel would have an API for this,
116 * but it doesn't, so we work around it, and hide this as a
117 * far away as we can. */
118
119 if (fd >= 0) {
120 PROTECT_ERRNO;
121
122 r = asynchronous_job(close_thread, FD_TO_PTR(fd));
123 if (r < 0)
124 assert_se(close_nointr(fd) != -EBADF);
125 }
126
127 return -1;
128 }