mirror of
https://github.com/troglobit/finit.git
synced 2026-11-02 13:20:32 +00:00
Add support for clone3() to replace fork()
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
3 files changed
+129
No files matched your search
@@ -56,6 +56,7 @@ logit_LDADD = $(lite_LIBS)
|
|||||||
|
|
||||||
finit_SOURCES = api.c cgroup.c cgroup.h \
|
finit_SOURCES = api.c cgroup.c cgroup.h \
|
||||||
client.c client.h \
|
client.c client.h \
|
||||||
|
clone3.c clone3.h \
|
||||||
cond.c cond-w.c cond.h \
|
cond.c cond-w.c cond.h \
|
||||||
conf.c conf.h \
|
conf.c conf.h \
|
||||||
devmon.c devmon.h \
|
devmon.c devmon.h \
|
||||||
|
|||||||
@@ -0,0 +1,95 @@
|
|||||||
|
/* Portable clone3() wrapper with fallback to fork()
|
||||||
|
*
|
||||||
|
* Copyright (c) 2025 Joachim Wiberg <troglobit@gmail.com>
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in
|
||||||
|
* all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
* THE SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "config.h"
|
||||||
|
|
||||||
|
#include <errno.h>
|
||||||
|
#include <sched.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <sys/syscall.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/wait.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#include "clone3.h"
|
||||||
|
#include "finit.h"
|
||||||
|
#include "log.h"
|
||||||
|
|
||||||
|
/* Clone3 flags from linux/sched.h */
|
||||||
|
#ifndef CLONE_INTO_CGROUP
|
||||||
|
#define CLONE_INTO_CGROUP 0x200000000ULL
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* Linux kernel struct clone_args for clone3() syscall */
|
||||||
|
struct clone3_args {
|
||||||
|
uint64_t flags; /* Flags bit mask */
|
||||||
|
uint64_t pidfd; /* File descriptor for PID */
|
||||||
|
uint64_t child_tid; /* Child TID */
|
||||||
|
uint64_t parent_tid; /* Parent TID */
|
||||||
|
uint64_t exit_signal; /* Signal to deliver on exit */
|
||||||
|
uint64_t stack; /* Stack address */
|
||||||
|
uint64_t stack_size; /* Stack size */
|
||||||
|
uint64_t tls; /* TLS descriptor */
|
||||||
|
uint64_t set_tid; /* Set PID */
|
||||||
|
uint64_t set_tid_size; /* Number of set_tid entries */
|
||||||
|
uint64_t cgroup; /* Cgroup file descriptor */
|
||||||
|
} __attribute__((aligned(8)));
|
||||||
|
|
||||||
|
#ifdef __NR_clone3
|
||||||
|
static int use_clone3 = 1;
|
||||||
|
#else
|
||||||
|
static int use_clone3 = 0;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
int has_clone3(void)
|
||||||
|
{
|
||||||
|
return use_clone3 == 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
pid_t call_clone3(uint64_t flags, int cgroup_fd)
|
||||||
|
{
|
||||||
|
struct clone3_args cl = {
|
||||||
|
.flags = flags,
|
||||||
|
.exit_signal = SIGCHLD,
|
||||||
|
};
|
||||||
|
pid_t pid;
|
||||||
|
|
||||||
|
if (!use_clone3)
|
||||||
|
goto fallback;
|
||||||
|
|
||||||
|
if (cgroup_fd >= 0) {
|
||||||
|
cl.flags |= CLONE_INTO_CGROUP;
|
||||||
|
cl.cgroup = cgroup_fd;
|
||||||
|
}
|
||||||
|
|
||||||
|
pid = syscall(__NR_clone3, &cl, sizeof(cl));
|
||||||
|
if (pid != -1)
|
||||||
|
return pid;
|
||||||
|
|
||||||
|
logit(LOG_WARNING, "clone3() failed, falling back to fork(): %s", strerror(errno));
|
||||||
|
use_clone3 = 0;
|
||||||
|
fallback:
|
||||||
|
return fork();
|
||||||
|
}
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
/* Portable clone3() wrapper with fallback to fork()
|
||||||
|
*
|
||||||
|
* Copyright (c) 2025 Joachim Wiberg <troglobit@gmail.com>
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in
|
||||||
|
* all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
* THE SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef FINIT_CLONE3_H_
|
||||||
|
#define FINIT_CLONE3_H_
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
|
||||||
|
pid_t call_clone3 (uint64_t flags, int cgroup_fd);
|
||||||
|
int has_clone3 (void);
|
||||||
|
|
||||||
|
#endif /* FINIT_CLONE3_H_ */
|
||||||
Reference in new issue
Block a user