-
Notifications
You must be signed in to change notification settings - Fork 305
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
126 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
/* | ||
* Copyright (C) 2024 Colin Walters <[email protected]> | ||
* | ||
* SPDX-License-Identifier: LGPL-2.0+ | ||
* | ||
* This library is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 2 of the License, or (at your option) any later version. | ||
* | ||
* This library is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this library. If not, see <https://www.gnu.org/licenses/>. | ||
* | ||
* Author: Misaki Kasumi <[email protected]> | ||
*/ | ||
|
||
#include "config.h" | ||
|
||
#include "libglnx.h" | ||
#include "ostree.h" | ||
#include "ot-admin-builtins.h" | ||
#include "ot-admin-functions.h" | ||
|
||
#include <spawn.h> | ||
#include <sys/wait.h> | ||
|
||
static gboolean opt_lock; | ||
|
||
static GOptionEntry options[] = { | ||
{ "lock", 0, 0, G_OPTION_ARG_NONE, &opt_lock, | ||
"Make /sysroot writable in the mount namespace and acquire an exclusive multi-process write lock", NULL }, | ||
{ NULL } }; | ||
|
||
gboolean | ||
ot_admin_builtin_nsenter (int argc, char **argv, OstreeCommandInvocation *invocation, | ||
GCancellable *cancellable, GError **error) | ||
{ | ||
g_autoptr (GOptionContext) context = NULL; | ||
g_autoptr (OstreeSysroot) sysroot = NULL; | ||
g_autofree char **arguments = NULL; | ||
|
||
context = g_option_context_new ("[PROGRAM [ARGUMENTS...]]"); | ||
|
||
int new_argc = argc; | ||
char **new_argv = argv; | ||
|
||
for (int i = 1; i < argc; i++) | ||
{ | ||
if (g_str_equal (argv[i], "--")) | ||
{ | ||
new_argc -= i; | ||
argc = i; | ||
new_argv = argv + i; | ||
argv[i] = NULL; | ||
break; | ||
} | ||
} | ||
|
||
if (!ostree_admin_option_context_parse (context, options, &argc, &argv, | ||
OSTREE_ADMIN_BUILTIN_FLAG_UNLOCKED, invocation, &sysroot, | ||
cancellable, error)) | ||
return FALSE; | ||
|
||
argc = new_argc; | ||
argv = new_argv; | ||
if (argc <= 1) | ||
{ | ||
arguments = g_malloc_n (2, sizeof (char *)); | ||
if ((arguments[0] = getenv ("SHELL")) == NULL) | ||
arguments[0] = "/bin/sh"; | ||
arguments[1] = NULL; | ||
} | ||
else | ||
{ | ||
arguments = g_malloc_n (argc, sizeof (char *)); | ||
memcpy (arguments, argv + 1, (argc - 1) * sizeof (char *)); | ||
arguments[argc - 1] = NULL; | ||
} | ||
|
||
if (opt_lock) | ||
{ | ||
if (!ostree_sysroot_lock (sysroot, error)) | ||
return FALSE; | ||
} | ||
|
||
pid_t child_pid; | ||
if (posix_spawnp (&child_pid, arguments[0], NULL, NULL, arguments, environ) != 0) | ||
{ | ||
glnx_throw_errno_prefix (error, "posix_spawnp"); | ||
return FALSE; | ||
} | ||
|
||
int status; | ||
while (waitpid (child_pid, &status, 0) < 0) | ||
{ | ||
if (errno != EINTR) | ||
{ | ||
glnx_throw_errno_prefix (error, "waitpid"); | ||
return FALSE; | ||
} | ||
} | ||
|
||
if (opt_lock) | ||
ostree_sysroot_unlock (sysroot); | ||
|
||
if (!WIFEXITED (status)) | ||
{ | ||
glnx_throw (error, "child process killed by signal"); | ||
return FALSE; | ||
} | ||
|
||
int exit_status = WEXITSTATUS (status); | ||
if (exit_status != EXIT_SUCCESS) | ||
exit (exit_status); | ||
|
||
return TRUE; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters