1 /*#############################################################################
3 # Pakfire - The IPFire package management system #
4 # Copyright (C) 2022 Pakfire development team #
6 # This program is free software: you can redistribute it and/or modify #
7 # it under the terms of the GNU General Public License as published by #
8 # the Free Software Foundation, either version 3 of the License, or #
9 # (at your option) any later version. #
11 # This program is distributed in the hope that it will be useful, #
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
14 # GNU General Public License for more details. #
16 # You should have received a copy of the GNU General Public License #
17 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
19 #############################################################################*/
23 #include <linux/capability.h>
24 #include <linux/sched.h>
26 #include <linux/wait.h>
31 #include <sys/capability.h>
32 #include <sys/epoll.h>
33 #include <sys/eventfd.h>
34 #include <sys/mount.h>
35 #include <sys/personality.h>
36 #include <sys/prctl.h>
37 #include <sys/resource.h>
38 #include <sys/timerfd.h>
39 #include <sys/types.h>
44 #include <netlink/route/link.h>
52 #include <pakfire/arch.h>
53 #include <pakfire/cgroup.h>
54 #include <pakfire/jail.h>
55 #include <pakfire/logging.h>
56 #include <pakfire/mount.h>
57 #include <pakfire/pakfire.h>
58 #include <pakfire/path.h>
59 #include <pakfire/private.h>
60 #include <pakfire/pwd.h>
61 #include <pakfire/string.h>
62 #include <pakfire/util.h>
64 #define BUFFER_SIZE 1024 * 64
65 #define ENVIRON_SIZE 128
66 #define EPOLL_MAX_EVENTS 2
67 #define MAX_MOUNTPOINTS 8
69 // The default environment that will be set for every command
70 static const struct environ
{
75 { "LANG", "C.utf-8" },
76 { "PATH", "/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin", },
79 // Tell everything that it is running inside a Pakfire container
80 { "container", "pakfire" },
84 struct pakfire_jail_mountpoint
{
85 char source
[PATH_MAX
];
86 char target
[PATH_MAX
];
91 struct pakfire_ctx
* ctx
;
92 struct pakfire
* pakfire
;
95 // A unique ID for each jail
97 char __uuid
[UUID_STR_LEN
];
103 struct itimerspec timeout
;
106 struct pakfire_cgroup
* cgroup
;
109 char* env
[ENVIRON_SIZE
];
112 struct pakfire_jail_mountpoint mountpoints
[MAX_MOUNTPOINTS
];
113 unsigned int num_mountpoints
;
116 struct pakfire_jail_callbacks
{
118 pakfire_jail_log_callback log
;
123 struct pakfire_log_buffer
{
124 char data
[BUFFER_SIZE
];
128 struct pakfire_jail_exec
{
131 // PID (of the child)
135 // Process status (from waitid)
138 // FD to notify the client that the parent has finished initialization
142 struct pakfire_jail_pipes
{
152 #endif /* ENABLE_DEBUG */
156 struct pakfire_jail_communicate
{
157 pakfire_jail_communicate_in in
;
158 pakfire_jail_communicate_out out
;
163 struct pakfire_jail_buffers
{
164 struct pakfire_log_buffer stdout
;
165 struct pakfire_log_buffer stderr
;
168 struct pakfire_log_buffer log_INFO
;
169 struct pakfire_log_buffer log_ERROR
;
171 struct pakfire_log_buffer log_DEBUG
;
172 #endif /* ENABLE_DEBUG */
175 struct pakfire_cgroup
* cgroup
;
176 struct pakfire_cgroup_stats cgroup_stats
;
179 char console
[PATH_MAX
];
183 static int clone3(struct clone_args
* args
, size_t size
) {
184 return syscall(__NR_clone3
, args
, size
);
187 static int pidfd_send_signal(int pidfd
, int sig
, siginfo_t
* info
, unsigned int flags
) {
188 return syscall(SYS_pidfd_send_signal
, pidfd
, sig
, info
, flags
);
191 static int pivot_root(const char* new_root
, const char* old_root
) {
192 return syscall(SYS_pivot_root
, new_root
, old_root
);
195 static int pakfire_jail_exec_has_flag(
196 const struct pakfire_jail_exec
* ctx
, const enum pakfire_jail_exec_flags flag
) {
197 return ctx
->flags
& flag
;
200 static void pakfire_jail_free(struct pakfire_jail
* jail
) {
201 DEBUG(jail
->pakfire
, "Freeing jail at %p\n", jail
);
204 for (unsigned int i
= 0; jail
->env
[i
]; i
++)
208 pakfire_cgroup_unref(jail
->cgroup
);
210 pakfire_unref(jail
->pakfire
);
212 pakfire_ctx_unref(jail
->ctx
);
217 Passes any log messages on to the default pakfire log callback
219 static int pakfire_jail_default_log_callback(struct pakfire
* pakfire
, void* data
,
220 int priority
, const char* line
, size_t length
) {
223 INFO(pakfire
, "%s", line
);
227 ERROR(pakfire
, "%s", line
);
232 DEBUG(pakfire
, "%s", line
);
240 static const char* pakfire_jail_uuid(struct pakfire_jail
* jail
) {
242 uuid_unparse_lower(jail
->uuid
, jail
->__uuid
);
247 static int pakfire_jail_setup_interactive_env(struct pakfire_jail
* jail
) {
249 int r
= pakfire_jail_set_env(jail
, "PS1", "pakfire-jail \\w> ");
254 char* TERM
= secure_getenv("TERM");
256 r
= pakfire_jail_set_env(jail
, "TERM", TERM
);
262 char* LANG
= secure_getenv("LANG");
264 r
= pakfire_jail_set_env(jail
, "LANG", LANG
);
272 PAKFIRE_EXPORT
int pakfire_jail_create(struct pakfire_jail
** jail
, struct pakfire
* pakfire
) {
275 const char* arch
= pakfire_get_effective_arch(pakfire
);
277 // Allocate a new jail
278 struct pakfire_jail
* j
= calloc(1, sizeof(*j
));
283 j
->ctx
= pakfire_ctx(pakfire
);
286 j
->pakfire
= pakfire_ref(pakfire
);
288 // Initialize reference counter
291 // Generate a random UUID
292 uuid_generate_random(j
->uuid
);
294 DEBUG(j
->pakfire
, "Allocated new jail at %p\n", j
);
296 // Set the default logging callback
297 pakfire_jail_set_log_callback(j
, pakfire_jail_default_log_callback
, NULL
);
299 // Set default environment
300 for (const struct environ
* e
= ENV
; e
->key
; e
++) {
301 r
= pakfire_jail_set_env(j
, e
->key
, e
->val
);
306 // Enable all CPU features that CPU has to offer
307 if (!pakfire_arch_is_supported_by_host(arch
)) {
308 r
= pakfire_jail_set_env(j
, "QEMU_CPU", "max");
313 // Set container UUID
314 r
= pakfire_jail_set_env(j
, "container_uuid", pakfire_jail_uuid(j
));
318 // Disable systemctl to talk to systemd
319 if (!pakfire_on_root(j
->pakfire
)) {
320 r
= pakfire_jail_set_env(j
, "SYSTEMD_OFFLINE", "1");
330 pakfire_jail_free(j
);
335 PAKFIRE_EXPORT
struct pakfire_jail
* pakfire_jail_ref(struct pakfire_jail
* jail
) {
341 PAKFIRE_EXPORT
struct pakfire_jail
* pakfire_jail_unref(struct pakfire_jail
* jail
) {
342 if (--jail
->nrefs
> 0)
345 pakfire_jail_free(jail
);
351 PAKFIRE_EXPORT
void pakfire_jail_set_log_callback(struct pakfire_jail
* jail
,
352 pakfire_jail_log_callback callback
, void* data
) {
353 jail
->callbacks
.log
= callback
;
354 jail
->callbacks
.log_data
= data
;
359 PAKFIRE_EXPORT
int pakfire_jail_nice(struct pakfire_jail
* jail
, int nice
) {
360 // Check if nice level is in range
361 if (nice
< -19 || nice
> 20) {
372 int pakfire_jail_set_cgroup(struct pakfire_jail
* jail
, struct pakfire_cgroup
* cgroup
) {
373 // Free any previous cgroup
375 pakfire_cgroup_unref(jail
->cgroup
);
379 // Set any new cgroup
381 DEBUG(jail
->pakfire
, "Setting cgroup %p\n", cgroup
);
383 jail
->cgroup
= pakfire_cgroup_ref(cgroup
);
392 // Returns the length of the environment
393 static unsigned int pakfire_jail_env_length(struct pakfire_jail
* jail
) {
396 // Count everything in the environment
397 for (char** e
= jail
->env
; *e
; e
++)
403 // Finds an existing environment variable and returns its index or -1 if not found
404 static int pakfire_jail_find_env(struct pakfire_jail
* jail
, const char* key
) {
410 const size_t length
= strlen(key
);
412 for (unsigned int i
= 0; jail
->env
[i
]; i
++) {
413 if ((pakfire_string_startswith(jail
->env
[i
], key
)
414 && *(jail
->env
[i
] + length
) == '=')) {
423 // Returns the value of an environment variable or NULL
424 PAKFIRE_EXPORT
const char* pakfire_jail_get_env(struct pakfire_jail
* jail
,
426 int i
= pakfire_jail_find_env(jail
, key
);
430 return jail
->env
[i
] + strlen(key
) + 1;
433 // Sets an environment variable
434 PAKFIRE_EXPORT
int pakfire_jail_set_env(struct pakfire_jail
* jail
,
435 const char* key
, const char* value
) {
436 // Find the index where to write this value to
437 int i
= pakfire_jail_find_env(jail
, key
);
439 i
= pakfire_jail_env_length(jail
);
441 // Return -ENOSPC when the environment is full
442 if (i
>= ENVIRON_SIZE
) {
447 // Free any previous value
451 // Format and set environment variable
452 asprintf(&jail
->env
[i
], "%s=%s", key
, value
);
454 DEBUG(jail
->pakfire
, "Set environment variable: %s\n", jail
->env
[i
]);
459 // Imports an environment
460 PAKFIRE_EXPORT
int pakfire_jail_import_env(struct pakfire_jail
* jail
, const char* env
[]) {
468 // Copy environment variables
469 for (unsigned int i
= 0; env
[i
]; i
++) {
470 r
= pakfire_string_partition(env
[i
], "=", &key
, &val
);
475 r
= pakfire_jail_set_env(jail
, key
, val
);
492 PAKFIRE_EXPORT
int pakfire_jail_set_timeout(
493 struct pakfire_jail
* jail
, unsigned int timeout
) {
495 jail
->timeout
.it_value
.tv_sec
= timeout
;
498 DEBUG(jail
->pakfire
, "Timeout set to %u second(s)\n", timeout
);
500 DEBUG(jail
->pakfire
, "Timeout disabled\n");
505 static int pakfire_jail_create_timer(struct pakfire_jail
* jail
) {
508 // Nothing to do if no timeout has been set
509 if (!jail
->timeout
.it_value
.tv_sec
)
512 // Create a new timer
513 const int fd
= timerfd_create(CLOCK_MONOTONIC
, 0);
515 ERROR(jail
->pakfire
, "Could not create timer: %m\n");
520 r
= timerfd_settime(fd
, 0, &jail
->timeout
, NULL
);
522 ERROR(jail
->pakfire
, "Could not arm timer: %m\n");
536 This function replaces any logging in the child process.
538 All log messages will be sent to the parent process through their respective pipes.
540 static void pakfire_jail_log_redirect(void* data
, int priority
, const char* file
,
541 int line
, const char* fn
, const char* format
, va_list args
) {
542 struct pakfire_jail_pipes
* pipes
= (struct pakfire_jail_pipes
*)data
;
547 fd
= pipes
->log_INFO
[1];
551 fd
= pipes
->log_ERROR
[1];
556 fd
= pipes
->log_DEBUG
[1];
558 #endif /* ENABLE_DEBUG */
560 // Ignore any messages of an unknown priority
565 // Send the log message
567 vdprintf(fd
, format
, args
);
570 static int pakfire_jail_log_buffer_is_full(const struct pakfire_log_buffer
* buffer
) {
571 return (sizeof(buffer
->data
) == buffer
->used
);
575 This function reads as much data as it can from the file descriptor.
576 If it finds a whole line in it, it will send it to the logger and repeat the process.
577 If not newline character is found, it will try to read more data until it finds one.
579 static int pakfire_jail_handle_log(struct pakfire_jail
* jail
,
580 struct pakfire_jail_exec
* ctx
, int priority
, int fd
,
581 struct pakfire_log_buffer
* buffer
, pakfire_jail_communicate_out callback
, void* data
) {
582 char line
[BUFFER_SIZE
+ 1];
584 // Fill up buffer from fd
585 if (buffer
->used
< sizeof(buffer
->data
)) {
586 ssize_t bytes_read
= read(fd
, buffer
->data
+ buffer
->used
,
587 sizeof(buffer
->data
) - buffer
->used
);
590 if (bytes_read
< 0) {
591 ERROR(jail
->pakfire
, "Could not read from fd %d: %m\n", fd
);
595 // Update buffer size
596 buffer
->used
+= bytes_read
;
599 // See if we have any lines that we can write
600 while (buffer
->used
) {
601 // Search for the end of the first line
602 char* eol
= memchr(buffer
->data
, '\n', buffer
->used
);
606 // If the buffer is full, we send the content to the logger and try again
607 // This should not happen in practise
608 if (pakfire_jail_log_buffer_is_full(buffer
)) {
609 DEBUG(jail
->pakfire
, "Logging buffer is full. Sending all content\n");
611 eol
= buffer
->data
+ sizeof(buffer
->data
) - 1;
613 // Otherwise we might have only read parts of the output
618 // Find the length of the string
619 size_t length
= eol
- buffer
->data
+ 1;
621 // Copy the line into the buffer
622 memcpy(line
, buffer
->data
, length
);
624 // Terminate the string
629 int r
= callback(jail
->pakfire
, data
, priority
, line
, length
);
631 ERROR(jail
->pakfire
, "The logging callback returned an error: %d\n", r
);
636 // Remove line from buffer
637 memmove(buffer
->data
, buffer
->data
+ length
, buffer
->used
- length
);
638 buffer
->used
-= length
;
644 static int pakfire_jail_stream_stdin(struct pakfire_jail
* jail
,
645 struct pakfire_jail_exec
* ctx
, const int fd
) {
648 // Nothing to do if there is no stdin callback set
649 if (!ctx
->communicate
.in
) {
650 DEBUG(jail
->pakfire
, "Callback for standard input is not set\n");
654 // Skip if the writing pipe has already been closed
655 if (!ctx
->pipes
.stdin
[1])
658 DEBUG(jail
->pakfire
, "Streaming standard input...\n");
660 // Calling the callback
661 r
= ctx
->communicate
.in(jail
->pakfire
, ctx
->communicate
.data
, fd
);
663 DEBUG(jail
->pakfire
, "Standard input callback finished: %d\n", r
);
665 // The callback signaled that it has written everything
667 DEBUG(jail
->pakfire
, "Closing standard input pipe\n");
669 // Close the file-descriptor
672 // Reset the file-descriptor so it won't be closed again later
673 ctx
->pipes
.stdin
[1] = -1;
682 static int pakfire_jail_setup_pipe(struct pakfire_jail
* jail
, int (*fds
)[2], const int flags
) {
683 int r
= pipe2(*fds
, flags
);
685 ERROR(jail
->pakfire
, "Could not setup pipe: %m\n");
692 static void pakfire_jail_close_pipe(struct pakfire_jail
* jail
, int fds
[2]) {
693 for (unsigned int i
= 0; i
< 2; i
++)
699 This is a convenience function to fetch the reading end of a pipe and
700 closes the write end.
702 static int pakfire_jail_get_pipe_to_read(struct pakfire_jail
* jail
, int (*fds
)[2]) {
703 // Give the variables easier names to avoid confusion
704 int* fd_read
= &(*fds
)[0];
705 int* fd_write
= &(*fds
)[1];
707 // Close the write end of the pipe
708 if (*fd_write
>= 0) {
713 // Return the read end
720 static int pakfire_jail_get_pipe_to_write(struct pakfire_jail
* jail
, int (*fds
)[2]) {
721 // Give the variables easier names to avoid confusion
722 int* fd_read
= &(*fds
)[0];
723 int* fd_write
= &(*fds
)[1];
725 // Close the read end of the pipe
731 // Return the write end
738 static int pakfire_jail_log(struct pakfire
* pakfire
, void* data
, int priority
,
739 const char* line
, const size_t length
) {
740 // Pass everything to the parent logger
741 pakfire_log_condition(pakfire
, priority
, 0, "%.*s", (int)length
, line
);
746 static int pakfire_jail_wait(struct pakfire_jail
* jail
, struct pakfire_jail_exec
* ctx
) {
748 struct epoll_event ev
;
749 struct epoll_event events
[EPOLL_MAX_EVENTS
];
753 // Fetch file descriptors from context
754 const int stdin
= pakfire_jail_get_pipe_to_write(jail
, &ctx
->pipes
.stdin
);
755 const int stdout
= pakfire_jail_get_pipe_to_read(jail
, &ctx
->pipes
.stdout
);
756 const int stderr
= pakfire_jail_get_pipe_to_read(jail
, &ctx
->pipes
.stderr
);
757 const int pidfd
= ctx
->pidfd
;
760 const int timerfd
= pakfire_jail_create_timer(jail
);
763 const int log_INFO
= pakfire_jail_get_pipe_to_read(jail
, &ctx
->pipes
.log_INFO
);
764 const int log_ERROR
= pakfire_jail_get_pipe_to_read(jail
, &ctx
->pipes
.log_ERROR
);
766 const int log_DEBUG
= pakfire_jail_get_pipe_to_read(jail
, &ctx
->pipes
.log_DEBUG
);
767 #endif /* ENABLE_DEBUG */
769 // Make a list of all file descriptors we are interested in
780 #endif /* ENABLE_DEBUG */
784 epollfd
= epoll_create1(0);
786 ERROR(jail
->pakfire
, "Could not initialize epoll(): %m\n");
791 // Turn file descriptors into non-blocking mode and add them to epoll()
792 for (unsigned int i
= 0; i
< sizeof(fds
) / sizeof(*fds
); i
++) {
795 // Skip fds which were not initialized
799 ev
.events
= EPOLLHUP
;
802 ev
.events
|= EPOLLOUT
;
804 ev
.events
|= EPOLLIN
;
807 int flags
= fcntl(fd
, F_GETFL
, 0);
809 // Set modified flags
810 if (fcntl(fd
, F_SETFL
, flags
|O_NONBLOCK
) < 0) {
812 "Could not set file descriptor %d into non-blocking mode: %m\n", fd
);
819 if (epoll_ctl(epollfd
, EPOLL_CTL_ADD
, fd
, &ev
) < 0) {
820 ERROR(jail
->pakfire
, "Could not add file descriptor %d to epoll(): %m\n", fd
);
828 // Loop for as long as the process is alive
830 int num
= epoll_wait(epollfd
, events
, EPOLL_MAX_EVENTS
, -1);
832 // Ignore if epoll_wait() has been interrupted
836 ERROR(jail
->pakfire
, "epoll_wait() failed: %m\n");
842 for (int i
= 0; i
< num
; i
++) {
843 int e
= events
[i
].events
;
844 int fd
= events
[i
].data
.fd
;
846 struct pakfire_log_buffer
* buffer
= NULL
;
847 pakfire_jail_communicate_out callback
= NULL
;
851 // Check if there is any data to be read
853 // Handle any changes to the PIDFD
855 // Call waidid() and store the result
856 r
= waitid(P_PIDFD
, ctx
->pidfd
, &ctx
->status
, WEXITED
);
858 ERROR(jail
->pakfire
, "waitid() failed: %m\n");
862 // Mark that we have ended so that we will process the remaining
863 // events from epoll() now, but won't restart the outer loop.
867 // Handle timer events
868 } else if (fd
== timerfd
) {
869 DEBUG(jail
->pakfire
, "Timer event received\n");
872 r
= read(timerfd
, garbage
, sizeof(garbage
));
874 ERROR(jail
->pakfire
, "Could not disarm timer: %m\n");
879 // Terminate the process if it hasn't already ended
881 DEBUG(jail
->pakfire
, "Terminating process...\n");
883 // Send SIGTERM to the process
884 r
= pidfd_send_signal(pidfd
, SIGKILL
, NULL
, 0);
886 ERROR(jail
->pakfire
, "Could not kill process: %m\n");
891 // Don't fall through to log processing
894 // Handle logging messages
895 } else if (fd
== log_INFO
) {
896 buffer
= &ctx
->buffers
.log_INFO
;
899 callback
= pakfire_jail_log
;
901 } else if (fd
== log_ERROR
) {
902 buffer
= &ctx
->buffers
.log_ERROR
;
905 callback
= pakfire_jail_log
;
908 } else if (fd
== log_DEBUG
) {
909 buffer
= &ctx
->buffers
.log_DEBUG
;
910 priority
= LOG_DEBUG
;
912 callback
= pakfire_jail_log
;
913 #endif /* ENABLE_DEBUG */
915 // Handle anything from the log pipes
916 } else if (fd
== stdout
) {
917 buffer
= &ctx
->buffers
.stdout
;
920 // Send any output to the default logger if no callback is set
921 if (ctx
->communicate
.out
) {
922 callback
= ctx
->communicate
.out
;
923 data
= ctx
->communicate
.data
;
925 callback
= jail
->callbacks
.log
;
926 data
= jail
->callbacks
.log_data
;
929 } else if (fd
== stderr
) {
930 buffer
= &ctx
->buffers
.stderr
;
933 // Send any output to the default logger if no callback is set
934 if (ctx
->communicate
.out
) {
935 callback
= ctx
->communicate
.out
;
936 data
= ctx
->communicate
.data
;
938 callback
= jail
->callbacks
.log
;
939 data
= jail
->callbacks
.log_data
;
943 DEBUG(jail
->pakfire
, "Received invalid file descriptor %d\n", fd
);
948 r
= pakfire_jail_handle_log(jail
, ctx
, priority
, fd
, buffer
, callback
, data
);
954 // Handle standard input
956 r
= pakfire_jail_stream_stdin(jail
, ctx
, fd
);
959 // Ignore if we filled up the buffer
964 ERROR(jail
->pakfire
, "Could not write to stdin: %m\n");
971 // Check if any file descriptors have been closed
973 // Remove the file descriptor
974 r
= epoll_ctl(epollfd
, EPOLL_CTL_DEL
, fd
, NULL
);
976 ERROR(jail
->pakfire
, "Could not remove closed file-descriptor %d: %m\n", fd
);
992 int pakfire_jail_capture_stdout(struct pakfire
* pakfire
, void* data
,
993 int priority
, const char* line
, size_t length
) {
994 char** output
= (char**)data
;
997 // Append everything from stdout to a buffer
998 if (output
&& priority
== LOG_INFO
) {
999 r
= asprintf(output
, "%s%s", (output
&& *output
) ? *output
: "", line
);
1005 // Send everything else to the default logger
1006 return pakfire_jail_default_log_callback(pakfire
, NULL
, priority
, line
, length
);
1011 // Logs all capabilities of the current process
1012 static int pakfire_jail_show_capabilities(struct pakfire_jail
* jail
) {
1015 cap_flag_value_t value_e
;
1016 cap_flag_value_t value_i
;
1017 cap_flag_value_t value_p
;
1021 pid_t pid
= getpid();
1023 // Fetch all capabilities
1024 caps
= cap_get_proc();
1026 ERROR(jail
->pakfire
, "Could not fetch capabilities: %m\n");
1031 DEBUG(jail
->pakfire
, "Capabilities of PID %d:\n", pid
);
1033 // Iterate over all capabilities
1034 for (unsigned int cap
= 0; cap_valid(cap
); cap
++) {
1035 name
= cap_to_name(cap
);
1037 // Fetch effective value
1038 r
= cap_get_flag(caps
, cap
, CAP_EFFECTIVE
, &value_e
);
1042 // Fetch inheritable value
1043 r
= cap_get_flag(caps
, cap
, CAP_INHERITABLE
, &value_i
);
1047 // Fetch permitted value
1048 r
= cap_get_flag(caps
, cap
, CAP_PERMITTED
, &value_p
);
1052 DEBUG(jail
->pakfire
,
1053 " %-24s : %c%c%c\n",
1055 (value_e
== CAP_SET
) ? 'e' : '-',
1056 (value_i
== CAP_SET
) ? 'i' : '-',
1057 (value_p
== CAP_SET
) ? 'p' : '-'
1077 static int pakfire_jail_set_capabilities(struct pakfire_jail
* jail
) {
1082 // Fetch capabilities
1083 caps
= cap_get_proc();
1085 ERROR(jail
->pakfire
, "Could not read capabilities: %m\n");
1090 // Walk through all capabilities
1091 for (cap_value_t cap
= 0; cap_valid(cap
); cap
++) {
1092 cap_value_t _caps
[] = { cap
};
1094 // Fetch the name of the capability
1095 name
= cap_to_name(cap
);
1097 r
= cap_set_flag(caps
, CAP_EFFECTIVE
, 1, _caps
, CAP_SET
);
1099 ERROR(jail
->pakfire
, "Could not set %s: %m\n", name
);
1103 r
= cap_set_flag(caps
, CAP_INHERITABLE
, 1, _caps
, CAP_SET
);
1105 ERROR(jail
->pakfire
, "Could not set %s: %m\n", name
);
1109 r
= cap_set_flag(caps
, CAP_PERMITTED
, 1, _caps
, CAP_SET
);
1111 ERROR(jail
->pakfire
, "Could not set %s: %m\n", name
);
1120 // Restore all capabilities
1121 r
= cap_set_proc(caps
);
1123 ERROR(jail
->pakfire
, "Restoring capabilities failed: %m\n");
1127 // Add all capabilities to the ambient set
1128 for (unsigned int cap
= 0; cap_valid(cap
); cap
++) {
1129 name
= cap_to_name(cap
);
1131 // Raise the capability
1132 r
= prctl(PR_CAP_AMBIENT
, PR_CAP_AMBIENT_RAISE
, cap
, 0, 0);
1134 ERROR(jail
->pakfire
, "Could not set ambient capability %s: %m\n", name
);
1157 static int pakfire_jail_limit_syscalls(struct pakfire_jail
* jail
) {
1158 const int syscalls
[] = {
1159 // The kernel's keyring isn't namespaced
1162 SCMP_SYS(request_key
),
1164 // Disable userfaultfd
1165 SCMP_SYS(userfaultfd
),
1167 // Disable perf which could leak a lot of information about the host
1168 SCMP_SYS(perf_event_open
),
1174 DEBUG(jail
->pakfire
, "Applying syscall filter...\n");
1176 // Setup a syscall filter which allows everything by default
1177 scmp_filter_ctx ctx
= seccomp_init(SCMP_ACT_ALLOW
);
1179 ERROR(jail
->pakfire
, "Could not setup seccomp filter: %m\n");
1184 for (const int* syscall
= syscalls
; *syscall
; syscall
++) {
1185 r
= seccomp_rule_add(ctx
, SCMP_ACT_ERRNO(EPERM
), *syscall
, 0);
1187 ERROR(jail
->pakfire
, "Could not configure syscall %d: %m\n", *syscall
);
1192 // Load syscall filter into the kernel
1193 r
= seccomp_load(ctx
);
1195 ERROR(jail
->pakfire
, "Could not load syscall filter into the kernel: %m\n");
1201 seccomp_release(ctx
);
1208 PAKFIRE_EXPORT
int pakfire_jail_bind(struct pakfire_jail
* jail
,
1209 const char* source
, const char* target
, int flags
) {
1210 struct pakfire_jail_mountpoint
* mp
= NULL
;
1213 // Check if there is any space left
1214 if (jail
->num_mountpoints
>= MAX_MOUNTPOINTS
) {
1219 // Check for valid inputs
1220 if (!source
|| !target
) {
1225 // Select the next free slot
1226 mp
= &jail
->mountpoints
[jail
->num_mountpoints
];
1229 r
= pakfire_string_set(mp
->source
, source
);
1231 ERROR(jail
->pakfire
, "Could not copy source: %m\n");
1236 r
= pakfire_string_set(mp
->target
, target
);
1238 ERROR(jail
->pakfire
, "Could not copy target: %m\n");
1245 // Increment counter
1246 jail
->num_mountpoints
++;
1251 static int pakfire_jail_mount_networking(struct pakfire_jail
* jail
) {
1254 const char* paths
[] = {
1260 // Bind-mount all paths read-only
1261 for (const char** path
= paths
; *path
; path
++) {
1262 r
= pakfire_bind(jail
->pakfire
, *path
, NULL
, MS_RDONLY
);
1265 // Ignore if we don't have permission
1280 Mounts everything that we require in the new namespace
1282 static int pakfire_jail_mount(struct pakfire_jail
* jail
, struct pakfire_jail_exec
* ctx
) {
1283 struct pakfire_jail_mountpoint
* mp
= NULL
;
1287 // Enable loop devices
1288 if (pakfire_jail_exec_has_flag(ctx
, PAKFIRE_JAIL_HAS_LOOP_DEVICES
))
1289 flags
|= PAKFIRE_MOUNT_LOOP_DEVICES
;
1291 // Mount all default stuff
1292 r
= pakfire_mount_all(jail
->pakfire
, PAKFIRE_MNTNS_OUTER
, flags
);
1297 r
= pakfire_populate_dev(jail
->pakfire
, flags
);
1301 // Mount the interpreter (if needed)
1302 r
= pakfire_mount_interpreter(jail
->pakfire
);
1306 // Mount networking stuff
1307 if (pakfire_jail_exec_has_flag(ctx
, PAKFIRE_JAIL_HAS_NETWORKING
)) {
1308 r
= pakfire_jail_mount_networking(jail
);
1313 // Mount all custom stuff
1314 for (unsigned int i
= 0; i
< jail
->num_mountpoints
; i
++) {
1316 mp
= &jail
->mountpoints
[i
];
1319 r
= pakfire_bind(jail
->pakfire
, mp
->source
, mp
->target
, mp
->flags
);
1329 static int pakfire_jail_setup_loopback(struct pakfire_jail
* jail
) {
1330 struct nl_sock
* nl
= NULL
;
1331 struct nl_cache
* cache
= NULL
;
1332 struct rtnl_link
* link
= NULL
;
1333 struct rtnl_link
* change
= NULL
;
1336 DEBUG(jail
->pakfire
, "Setting up loopback...\n");
1338 // Allocate a netlink socket
1339 nl
= nl_socket_alloc();
1341 ERROR(jail
->pakfire
, "Could not allocate a netlink socket: %m\n");
1346 // Connect the socket
1347 r
= nl_connect(nl
, NETLINK_ROUTE
);
1349 ERROR(jail
->pakfire
, "Could not connect netlink socket: %s\n", nl_geterror(r
));
1353 // Allocate the netlink cache
1354 r
= rtnl_link_alloc_cache(nl
, AF_UNSPEC
, &cache
);
1356 ERROR(jail
->pakfire
, "Unable to allocate netlink cache: %s\n", nl_geterror(r
));
1360 // Fetch loopback interface
1361 link
= rtnl_link_get_by_name(cache
, "lo");
1363 ERROR(jail
->pakfire
, "Could not find lo interface. Ignoring.\n");
1368 // Allocate a new link
1369 change
= rtnl_link_alloc();
1371 ERROR(jail
->pakfire
, "Could not allocate change link\n");
1376 // Set the link to UP
1377 rtnl_link_set_flags(change
, IFF_UP
);
1379 // Apply any changes
1380 r
= rtnl_link_change(nl
, link
, change
, 0);
1382 ERROR(jail
->pakfire
, "Unable to activate loopback: %s\n", nl_geterror(r
));
1398 static int pakfire_jail_setup_uid_mapping(struct pakfire_jail
* jail
, pid_t pid
) {
1399 char path
[PATH_MAX
];
1402 // Skip mapping anything when running on /
1403 if (pakfire_on_root(jail
->pakfire
))
1407 r
= pakfire_string_format(path
, "/proc/%d/uid_map", pid
);
1412 const uid_t uid
= pakfire_uid(jail
->pakfire
);
1415 const struct pakfire_subid
* subuid
= pakfire_subuid(jail
->pakfire
);
1419 /* When running as root, we will map the entire range.
1421 When running as a non-privileged user, we will map the root user inside the jail
1422 to the user's UID outside of the jail, and we will map the rest starting from one.
1427 r
= pakfire_file_write(jail
->pakfire
, path
, 0, 0, 0,
1428 "0 %lu %lu\n", subuid
->id
, subuid
->length
);
1430 r
= pakfire_file_write(jail
->pakfire
, path
, 0, 0, 0,
1431 "0 %lu 1\n1 %lu %lu\n", uid
, subuid
->id
, subuid
->length
);
1435 ERROR(jail
->pakfire
, "Could not map UIDs: %m\n");
1442 static int pakfire_jail_setup_gid_mapping(struct pakfire_jail
* jail
, pid_t pid
) {
1443 char path
[PATH_MAX
];
1446 // Skip mapping anything when running on /
1447 if (pakfire_on_root(jail
->pakfire
))
1451 const gid_t gid
= pakfire_gid(jail
->pakfire
);
1454 const struct pakfire_subid
* subgid
= pakfire_subgid(jail
->pakfire
);
1459 r
= pakfire_string_format(path
, "/proc/%d/gid_map", pid
);
1465 r
= pakfire_file_write(jail
->pakfire
, path
, 0, 0, 0,
1466 "0 %lu %lu\n", subgid
->id
, subgid
->length
);
1468 r
= pakfire_file_write(jail
->pakfire
, path
, 0, 0, 0,
1469 "0 %lu 1\n1 %lu %lu\n", gid
, subgid
->id
, subgid
->length
);
1473 ERROR(jail
->pakfire
, "Could not map GIDs: %m\n");
1480 static int pakfire_jail_setgroups(struct pakfire_jail
* jail
, pid_t pid
) {
1481 char path
[PATH_MAX
];
1485 r
= pakfire_string_format(path
, "/proc/%d/setgroups", pid
);
1489 r
= pakfire_file_write(jail
->pakfire
, path
, 0, 0, 0, "deny\n");
1491 CTX_ERROR(jail
->ctx
, "Could not set setgroups to deny: %s\n", strerror(errno
));
1498 static int pakfire_jail_send_signal(struct pakfire_jail
* jail
, int fd
) {
1499 const uint64_t val
= 1;
1502 DEBUG(jail
->pakfire
, "Sending signal...\n");
1504 // Write to the file descriptor
1505 r
= eventfd_write(fd
, val
);
1507 ERROR(jail
->pakfire
, "Could not send signal: %s\n", strerror(errno
));
1511 // Close the file descriptor
1517 static int pakfire_jail_wait_for_signal(struct pakfire_jail
* jail
, int fd
) {
1521 DEBUG(jail
->pakfire
, "Waiting for signal...\n");
1523 r
= eventfd_read(fd
, &val
);
1525 ERROR(jail
->pakfire
, "Error waiting for signal: %s\n", strerror(errno
));
1529 // Close the file descriptor
1536 Performs the initialisation that needs to happen in the parent part
1538 static int pakfire_jail_parent(struct pakfire_jail
* jail
, struct pakfire_jail_exec
* ctx
) {
1541 // Setup UID mapping
1542 r
= pakfire_jail_setup_uid_mapping(jail
, ctx
->pid
);
1546 // Write "deny" to /proc/PID/setgroups
1547 r
= pakfire_jail_setgroups(jail
, ctx
->pid
);
1551 // Setup GID mapping
1552 r
= pakfire_jail_setup_gid_mapping(jail
, ctx
->pid
);
1556 // Parent has finished initialisation
1557 DEBUG(jail
->pakfire
, "Parent has finished initialization\n");
1559 // Send signal to client
1560 r
= pakfire_jail_send_signal(jail
, ctx
->completed_fd
);
1567 static int pakfire_jail_switch_root(struct pakfire_jail
* jail
, const char* root
) {
1570 // Change to the new root
1573 ERROR(jail
->pakfire
, "chdir(%s) failed: %m\n", root
);
1578 r
= pivot_root(".", ".");
1580 ERROR(jail
->pakfire
, "Failed changing into the new root directory %s: %m\n", root
);
1584 // Umount the old root
1585 r
= umount2(".", MNT_DETACH
);
1587 ERROR(jail
->pakfire
, "Could not umount the old root filesystem: %m\n");
1595 static int pakfire_jail_open_pty(struct pakfire_jail
* jail
, struct pakfire_jail_exec
* ctx
) {
1598 // Allocate a new PTY
1599 ctx
->consolefd
= posix_openpt(O_RDWR
|O_NONBLOCK
|O_NOCTTY
|O_CLOEXEC
);
1600 if (ctx
->consolefd
< 0)
1604 r
= ptsname_r(ctx
->consolefd
, ctx
->console
, sizeof(ctx
->console
));
1608 CTX_DEBUG(jail
->ctx
, "Allocated console at %s (%d)\n", ctx
->console
, ctx
->consolefd
);
1611 r
= pakfire_symlink(jail
->ctx
, "/dev/console", ctx
->console
);
1619 static int pakfire_jail_child(struct pakfire_jail
* jail
, struct pakfire_jail_exec
* ctx
,
1620 const char* argv
[]) {
1623 // Redirect any logging to our log pipe
1624 pakfire_ctx_set_log_callback(jail
->ctx
, pakfire_jail_log_redirect
, &ctx
->pipes
);
1627 pid_t pid
= getpid();
1629 DEBUG(jail
->pakfire
, "Launched child process in jail with PID %d\n", pid
);
1631 // Wait for the parent to finish initialization
1632 r
= pakfire_jail_wait_for_signal(jail
, ctx
->completed_fd
);
1637 r
= prctl(PR_SET_PDEATHSIG
, SIGKILL
, 0, 0, 0);
1639 ERROR(jail
->pakfire
, "Could not configure to die with parent: %m\n");
1643 // Make this process dumpable
1644 r
= prctl (PR_SET_DUMPABLE
, 1, 0, 0, 0);
1646 ERROR(jail
->pakfire
, "Could not make the process dumpable: %m\n");
1650 // Don't drop any capabilities on setuid()
1651 r
= prctl(PR_SET_KEEPCAPS
, 1);
1653 ERROR(jail
->pakfire
, "Could not set PR_SET_KEEPCAPS: %m\n");
1658 uid_t uid
= getuid();
1659 gid_t gid
= getgid();
1662 uid_t euid
= geteuid();
1663 gid_t egid
= getegid();
1665 DEBUG(jail
->pakfire
, " UID: %u (effective %u)\n", uid
, euid
);
1666 DEBUG(jail
->pakfire
, " GID: %u (effective %u)\n", gid
, egid
);
1668 // Log all mountpoints
1669 pakfire_mount_list(jail
->ctx
);
1671 // Fail if we are not PID 1
1673 CTX_ERROR(jail
->ctx
, "Child process is not PID 1\n");
1677 // Fail if we are not running as root
1678 if (uid
|| gid
|| euid
|| egid
) {
1679 ERROR(jail
->pakfire
, "Child process is not running as root\n");
1683 // Mount all default stuff
1684 r
= pakfire_mount_all(jail
->pakfire
, PAKFIRE_MNTNS_INNER
, 0);
1689 // Create a new session
1692 CTX_ERROR(jail
->ctx
, "Could not create a new session: %s\n", strerror(errno
));
1696 // Allocate a new PTY
1697 r
= pakfire_jail_open_pty(jail
, ctx
);
1699 CTX_ERROR(jail
->ctx
, "Could not allocate a new PTY: %s\n", strerror(-r
));
1704 const char* root
= pakfire_get_path(jail
->pakfire
);
1705 const char* arch
= pakfire_get_effective_arch(jail
->pakfire
);
1707 // Change mount propagation to slave to receive anything from the parent namespace
1708 r
= pakfire_mount_change_propagation(jail
->ctx
, "/", MS_SLAVE
);
1712 // Make root a mountpoint in the new mount namespace
1713 r
= pakfire_mount_make_mounpoint(jail
->pakfire
, root
);
1717 // Change mount propagation to private
1718 r
= pakfire_mount_change_propagation(jail
->ctx
, root
, MS_PRIVATE
);
1722 // Change root (unless root is /)
1723 if (!pakfire_on_root(jail
->pakfire
)) {
1725 r
= pakfire_jail_mount(jail
, ctx
);
1730 r
= pakfire_jail_switch_root(jail
, root
);
1736 unsigned long persona
= pakfire_arch_personality(arch
);
1738 r
= personality(persona
);
1740 ERROR(jail
->pakfire
, "Could not set personality (%x)\n", (unsigned int)persona
);
1746 if (!pakfire_jail_exec_has_flag(ctx
, PAKFIRE_JAIL_HAS_NETWORKING
)) {
1747 r
= pakfire_jail_setup_loopback(jail
);
1754 DEBUG(jail
->pakfire
, "Setting nice level to %d\n", jail
->nice
);
1756 r
= setpriority(PRIO_PROCESS
, pid
, jail
->nice
);
1758 ERROR(jail
->pakfire
, "Could not set nice level: %m\n");
1763 // Close other end of log pipes
1764 close(ctx
->pipes
.log_INFO
[0]);
1765 close(ctx
->pipes
.log_ERROR
[0]);
1767 close(ctx
->pipes
.log_DEBUG
[0]);
1768 #endif /* ENABLE_DEBUG */
1770 // Connect standard input
1771 if (ctx
->pipes
.stdin
[0] >= 0) {
1772 r
= dup2(ctx
->pipes
.stdin
[0], STDIN_FILENO
);
1774 ERROR(jail
->pakfire
, "Could not connect fd %d to stdin: %m\n",
1775 ctx
->pipes
.stdin
[0]);
1781 // Connect standard output and error
1782 if (ctx
->pipes
.stdout
[1] >= 0 && ctx
->pipes
.stderr
[1] >= 0) {
1783 r
= dup2(ctx
->pipes
.stdout
[1], STDOUT_FILENO
);
1785 ERROR(jail
->pakfire
, "Could not connect fd %d to stdout: %m\n",
1786 ctx
->pipes
.stdout
[1]);
1791 r
= dup2(ctx
->pipes
.stderr
[1], STDERR_FILENO
);
1793 ERROR(jail
->pakfire
, "Could not connect fd %d to stderr: %m\n",
1794 ctx
->pipes
.stderr
[1]);
1799 // Close the pipe (as we have moved the original file descriptors)
1800 pakfire_jail_close_pipe(jail
, ctx
->pipes
.stdin
);
1801 pakfire_jail_close_pipe(jail
, ctx
->pipes
.stdout
);
1802 pakfire_jail_close_pipe(jail
, ctx
->pipes
.stderr
);
1805 // Reset open file limit (http://0pointer.net/blog/file-descriptor-limits.html)
1806 r
= pakfire_rlimit_reset_nofile(jail
->pakfire
);
1811 r
= pakfire_jail_set_capabilities(jail
);
1815 // Show capabilities
1816 r
= pakfire_jail_show_capabilities(jail
);
1821 r
= pakfire_jail_limit_syscalls(jail
);
1825 DEBUG(jail
->pakfire
, "Child process initialization done\n");
1826 DEBUG(jail
->pakfire
, "Launching command:\n");
1829 for (unsigned int i
= 0; argv
[i
]; i
++)
1830 DEBUG(jail
->pakfire
, " argv[%u] = %s\n", i
, argv
[i
]);
1833 r
= execvpe(argv
[0], (char**)argv
, jail
->env
);
1835 // Translate errno into regular exit code
1838 // Ignore if the command doesn't exist
1839 if (ctx
->flags
& PAKFIRE_JAIL_NOENT_OK
)
1850 ERROR(jail
->pakfire
, "Could not execve(%s): %m\n", argv
[0]);
1853 // We should not get here
1857 // Run a command in the jail
1858 static int __pakfire_jail_exec(struct pakfire_jail
* jail
, const char* argv
[],
1859 const int interactive
,
1860 pakfire_jail_communicate_in communicate_in
,
1861 pakfire_jail_communicate_out communicate_out
,
1862 void* data
, int flags
) {
1866 // Check if argv is valid
1867 if (!argv
|| !argv
[0]) {
1872 // Initialize context for this call
1873 struct pakfire_jail_exec ctx
= {
1877 .stdin
= { -1, -1 },
1878 .stdout
= { -1, -1 },
1879 .stderr
= { -1, -1 },
1880 .log_INFO
= { -1, -1 },
1881 .log_ERROR
= { -1, -1 },
1883 .log_DEBUG
= { -1, -1 },
1884 #endif /* ENABLE_DEBUG */
1888 .in
= communicate_in
,
1889 .out
= communicate_out
,
1896 DEBUG(jail
->pakfire
, "Executing jail...\n");
1898 // Enable networking in interactive mode
1900 ctx
.flags
|= PAKFIRE_JAIL_HAS_NETWORKING
;
1903 Setup a file descriptor which can be used to notify the client that the parent
1904 has completed configuration.
1906 ctx
.completed_fd
= eventfd(0, EFD_CLOEXEC
);
1907 if (ctx
.completed_fd
< 0) {
1908 ERROR(jail
->pakfire
, "eventfd() failed: %m\n");
1912 // Create pipes to communicate with child process if we are not running interactively
1914 // stdin (only if callback is set)
1915 if (ctx
.communicate
.in
) {
1916 r
= pakfire_jail_setup_pipe(jail
, &ctx
.pipes
.stdin
, 0);
1922 r
= pakfire_jail_setup_pipe(jail
, &ctx
.pipes
.stdout
, 0);
1927 r
= pakfire_jail_setup_pipe(jail
, &ctx
.pipes
.stderr
, 0);
1932 // Setup pipes for logging
1934 r
= pakfire_jail_setup_pipe(jail
, &ctx
.pipes
.log_INFO
, O_CLOEXEC
);
1939 r
= pakfire_jail_setup_pipe(jail
, &ctx
.pipes
.log_ERROR
, O_CLOEXEC
);
1945 r
= pakfire_jail_setup_pipe(jail
, &ctx
.pipes
.log_DEBUG
, O_CLOEXEC
);
1948 #endif /* ENABLE_DEBUG */
1950 // Configure child process
1951 struct clone_args args
= {
1961 .exit_signal
= SIGCHLD
,
1962 .pidfd
= (long long unsigned int)&ctx
.pidfd
,
1965 // Launch the process in a cgroup that is a leaf of the configured cgroup
1967 args
.flags
|= CLONE_INTO_CGROUP
;
1970 const char* uuid
= pakfire_jail_uuid(jail
);
1972 // Create a temporary cgroup
1973 r
= pakfire_cgroup_child(&ctx
.cgroup
, jail
->cgroup
, uuid
, 0);
1975 ERROR(jail
->pakfire
, "Could not create cgroup for jail: %m\n");
1979 // Clone into this cgroup
1980 args
.cgroup
= pakfire_cgroup_fd(ctx
.cgroup
);
1984 if (!pakfire_jail_exec_has_flag(&ctx
, PAKFIRE_JAIL_HAS_NETWORKING
)) {
1985 args
.flags
|= CLONE_NEWNET
;
1988 // Fork this process
1989 ctx
.pid
= clone3(&args
, sizeof(args
));
1991 ERROR(jail
->pakfire
, "Could not clone: %m\n");
1995 } else if (ctx
.pid
== 0) {
1996 r
= pakfire_jail_child(jail
, &ctx
, argv
);
2001 r
= pakfire_jail_parent(jail
, &ctx
);
2005 DEBUG(jail
->pakfire
, "Waiting for PID %d to finish its work\n", ctx
.pid
);
2007 // Read output of the child process
2008 r
= pakfire_jail_wait(jail
, &ctx
);
2012 // Handle exit status
2013 switch (ctx
.status
.si_code
) {
2015 DEBUG(jail
->pakfire
, "The child process exited with code %d\n",
2016 ctx
.status
.si_status
);
2019 exit
= ctx
.status
.si_status
;
2023 ERROR(jail
->pakfire
, "The child process was killed\n");
2028 ERROR(jail
->pakfire
, "The child process terminated abnormally\n");
2031 // Log anything else
2033 ERROR(jail
->pakfire
, "Unknown child exit code: %d\n", ctx
.status
.si_code
);
2038 // Destroy the temporary cgroup (if any)
2040 // Read cgroup stats
2041 pakfire_cgroup_stat(ctx
.cgroup
, &ctx
.cgroup_stats
);
2042 pakfire_cgroup_stat_dump(ctx
.cgroup
, &ctx
.cgroup_stats
);
2043 pakfire_cgroup_destroy(ctx
.cgroup
);
2044 pakfire_cgroup_unref(ctx
.cgroup
);
2047 // Close any file descriptors
2048 pakfire_jail_close_pipe(jail
, ctx
.pipes
.stdin
);
2049 pakfire_jail_close_pipe(jail
, ctx
.pipes
.stdout
);
2050 pakfire_jail_close_pipe(jail
, ctx
.pipes
.stderr
);
2053 pakfire_jail_close_pipe(jail
, ctx
.pipes
.log_INFO
);
2054 pakfire_jail_close_pipe(jail
, ctx
.pipes
.log_ERROR
);
2056 pakfire_jail_close_pipe(jail
, ctx
.pipes
.log_DEBUG
);
2057 #endif /* ENABLE_DEBUG */
2062 PAKFIRE_EXPORT
int pakfire_jail_exec(
2063 struct pakfire_jail
* jail
,
2065 pakfire_jail_communicate_in callback_in
,
2066 pakfire_jail_communicate_out callback_out
,
2067 void* data
, int flags
) {
2068 return __pakfire_jail_exec(jail
, argv
, 0, callback_in
, callback_out
, data
, flags
);
2071 static int pakfire_jail_exec_interactive(
2072 struct pakfire_jail
* jail
, const char* argv
[], int flags
) {
2075 // Setup interactive stuff
2076 r
= pakfire_jail_setup_interactive_env(jail
);
2080 return __pakfire_jail_exec(jail
, argv
, 1, NULL
, NULL
, NULL
, flags
);
2083 int pakfire_jail_exec_script(struct pakfire_jail
* jail
,
2087 pakfire_jail_communicate_in callback_in
,
2088 pakfire_jail_communicate_out callback_out
,
2090 char path
[PATH_MAX
];
2091 const char** argv
= NULL
;
2095 const char* root
= pakfire_get_path(jail
->pakfire
);
2097 // Write the scriptlet to disk
2098 r
= pakfire_path_append(path
, root
, PAKFIRE_TMP_DIR
"/pakfire-script.XXXXXX");
2102 // Create a temporary file
2103 f
= pakfire_mktemp(path
, 0700);
2105 ERROR(jail
->pakfire
, "Could not create temporary file: %m\n");
2109 DEBUG(jail
->pakfire
, "Writing script to %s:\n%.*s\n", path
, (int)size
, script
);
2112 r
= fprintf(f
, "%s", script
);
2114 ERROR(jail
->pakfire
, "Could not write script to file %s: %m\n", path
);
2121 ERROR(jail
->pakfire
, "Could not close script file %s: %m\n", path
);
2127 // Count how many arguments were passed
2128 unsigned int argc
= 1;
2130 for (const char** arg
= args
; *arg
; arg
++)
2134 argv
= calloc(argc
+ 1, sizeof(*argv
));
2136 ERROR(jail
->pakfire
, "Could not allocate argv: %m\n");
2141 argv
[0] = (root
) ? pakfire_path_relpath(root
, path
) : path
;
2144 for (unsigned int i
= 1; i
< argc
; i
++)
2145 argv
[i
] = args
[i
-1];
2148 r
= pakfire_jail_exec(jail
, argv
, callback_in
, callback_out
, data
, 0);
2156 // Remove script from disk
2164 A convenience function that creates a new jail, runs the given command and destroys
2167 int pakfire_jail_run(struct pakfire
* pakfire
, const char* argv
[], int flags
, char** output
) {
2168 struct pakfire_jail
* jail
= NULL
;
2171 // Create a new jail
2172 r
= pakfire_jail_create(&jail
, pakfire
);
2176 // Execute the command
2177 r
= pakfire_jail_exec(jail
, argv
, NULL
, pakfire_jail_capture_stdout
, output
, 0);
2181 pakfire_jail_unref(jail
);
2186 int pakfire_jail_run_script(struct pakfire
* pakfire
,
2187 const char* script
, const size_t length
, const char* argv
[], int flags
) {
2188 struct pakfire_jail
* jail
= NULL
;
2191 // Create a new jail
2192 r
= pakfire_jail_create(&jail
, pakfire
);
2196 // Execute the command
2197 r
= pakfire_jail_exec_script(jail
, script
, length
, argv
, NULL
, NULL
, NULL
);
2201 pakfire_jail_unref(jail
);
2206 int pakfire_jail_shell(struct pakfire_jail
* jail
) {
2209 const char* argv
[] = {
2210 "/bin/bash", "--login", NULL
,
2213 // Execute /bin/bash
2214 r
= pakfire_jail_exec_interactive(jail
, argv
, 0);
2220 // Ignore any return codes from the shell
2224 static int pakfire_jail_run_if_possible(struct pakfire
* pakfire
, const char** argv
) {
2225 char path
[PATH_MAX
];
2228 r
= pakfire_path(pakfire
, path
, "%s", *argv
);
2232 // Check if the file is executable
2233 r
= access(path
, X_OK
);
2235 DEBUG(pakfire
, "%s is not executable. Skipping...\n", *argv
);
2239 return pakfire_jail_run(pakfire
, argv
, 0, NULL
);
2242 int pakfire_jail_ldconfig(struct pakfire
* pakfire
) {
2243 const char* argv
[] = {
2248 return pakfire_jail_run_if_possible(pakfire
, argv
);
2251 int pakfire_jail_run_systemd_tmpfiles(struct pakfire
* pakfire
) {
2252 const char* argv
[] = {
2253 "/usr/bin/systemd-tmpfiles",
2258 return pakfire_jail_run_if_possible(pakfire
, argv
);