]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/async.c
tree-wide: introduce new safe_fork() helper and port everything over
[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 pthread_attr_t a;
36 pthread_t t;
37 int r;
38
39 /* It kinda sucks that we have to resort to threads to
40 * implement an asynchronous sync(), but well, such is
41 * life.
42 *
43 * Note that issuing this command right before exiting a
44 * process will cause the process to wait for the sync() to
45 * complete. This function hence is nicely asynchronous really
46 * only in long running processes. */
47
48 r = pthread_attr_init(&a);
49 if (r > 0)
50 return -r;
51
52 r = pthread_attr_setdetachstate(&a, PTHREAD_CREATE_DETACHED);
53 if (r > 0)
54 goto finish;
55
56 r = pthread_create(&t, &a, func, arg);
57
58 finish:
59 pthread_attr_destroy(&a);
60 return -r;
61 }
62
63 int asynchronous_sync(void) {
64 int r;
65
66 /* This forks off an invocation of fork() as a child process, in order to initiate synchronization to
67 * disk. Note that we implement this as helper process rather than thread as we don't want the sync() to hang our
68 * original process ever, and a thread would do that as the process can't exit with threads hanging in blocking
69 * syscalls. */
70
71 r = safe_fork("(sd-sync)", FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS, NULL);
72 if (r < 0)
73 return r;
74 if (r == 0) {
75 /* Child process */
76 (void) sync();
77 _exit(EXIT_SUCCESS);
78 }
79
80 /* We don' really care about the PID from here on. It will exit when it's done. */
81 return 0;
82 }
83
84 static void *close_thread(void *p) {
85 assert_se(close_nointr(PTR_TO_FD(p)) != -EBADF);
86 return NULL;
87 }
88
89 int asynchronous_close(int fd) {
90 int r;
91
92 /* This is supposed to behave similar to safe_close(), but
93 * actually invoke close() asynchronously, so that it will
94 * never block. Ideally the kernel would have an API for this,
95 * but it doesn't, so we work around it, and hide this as a
96 * far away as we can. */
97
98 if (fd >= 0) {
99 PROTECT_ERRNO;
100
101 r = asynchronous_job(close_thread, FD_TO_PTR(fd));
102 if (r < 0)
103 assert_se(close_nointr(fd) != -EBADF);
104 }
105
106 return -1;
107 }