Add linux style formatting
Signed-off-by: Michael Crosby <michael@docker.com>
This commit is contained in:
parent
f3b0a3a0e6
commit
8b6c0b7ec6
|
@ -1,3 +1,7 @@
|
|||
// +build cgo
|
||||
//
|
||||
// formated with indent -linux nsenter.c
|
||||
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <linux/limits.h>
|
||||
|
@ -13,7 +17,8 @@
|
|||
static const kBufSize = 256;
|
||||
static const char *kNsEnter = "nsenter";
|
||||
|
||||
void get_args(int *argc, char ***argv) {
|
||||
void get_args(int *argc, char ***argv)
|
||||
{
|
||||
// Read argv
|
||||
int fd = open("/proc/self/cmdline", O_RDONLY);
|
||||
|
||||
|
@ -24,10 +29,13 @@ void get_args(int *argc, char ***argv) {
|
|||
ssize_t bytes_read = 0;
|
||||
do {
|
||||
contents_size += kBufSize;
|
||||
contents = (char *) realloc(contents, contents_size);
|
||||
bytes_read = read(fd, contents + contents_offset, contents_size - contents_offset);
|
||||
contents = (char *)realloc(contents, contents_size);
|
||||
bytes_read =
|
||||
read(fd, contents + contents_offset,
|
||||
contents_size - contents_offset);
|
||||
contents_offset += bytes_read;
|
||||
} while (bytes_read > 0);
|
||||
}
|
||||
while (bytes_read > 0);
|
||||
close(fd);
|
||||
|
||||
// Parse the commandline into an argv. /proc/self/cmdline has \0 delimited args.
|
||||
|
@ -38,7 +46,7 @@ void get_args(int *argc, char ***argv) {
|
|||
(*argc)++;
|
||||
}
|
||||
}
|
||||
*argv = (char **) malloc(sizeof(char *) * ((*argc) + 1));
|
||||
*argv = (char **)malloc(sizeof(char *) * ((*argc) + 1));
|
||||
int idx;
|
||||
for (idx = 0; idx < (*argc); idx++) {
|
||||
(*argv)[idx] = contents;
|
||||
|
@ -53,17 +61,21 @@ void get_args(int *argc, char ***argv) {
|
|||
#include <sched.h>
|
||||
#include "syscall.h"
|
||||
#ifdef SYS_setns
|
||||
int setns(int fd, int nstype) {
|
||||
int setns(int fd, int nstype)
|
||||
{
|
||||
return syscall(SYS_setns, fd, nstype);
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
void print_usage() {
|
||||
fprintf(stderr, "<binary> nsenter --nspid <pid> --containerjson <container_json> -- cmd1 arg1 arg2...\n");
|
||||
void print_usage()
|
||||
{
|
||||
fprintf(stderr,
|
||||
"<binary> nsenter --nspid <pid> --containerjson <container_json> -- cmd1 arg1 arg2...\n");
|
||||
}
|
||||
|
||||
void nsenter() {
|
||||
void nsenter()
|
||||
{
|
||||
int argc, c;
|
||||
char **argv;
|
||||
get_args(&argc, &argv);
|
||||
|
@ -83,10 +95,10 @@ void nsenter() {
|
|||
return;
|
||||
}
|
||||
static const struct option longopts[] = {
|
||||
{ "nspid", required_argument, NULL, 'n' },
|
||||
{ "containerjson", required_argument, NULL, 'c' },
|
||||
{ "console", optional_argument, NULL, 't' },
|
||||
{ NULL, 0, NULL, 0 }
|
||||
{"nspid", required_argument, NULL, 'n'},
|
||||
{"containerjson", required_argument, NULL, 'c'},
|
||||
{"console", optional_argument, NULL, 't'},
|
||||
{NULL, 0, NULL, 0}
|
||||
};
|
||||
|
||||
pid_t init_pid = -1;
|
||||
|
@ -94,7 +106,9 @@ void nsenter() {
|
|||
char *container_json = NULL;
|
||||
char *console = NULL;
|
||||
opterr = 0;
|
||||
while ((c = getopt_long_only(argc, argv, "-n:s:c:", longopts, NULL)) != -1) {
|
||||
while ((c =
|
||||
getopt_long_only(argc, argv, "-n:s:c:", longopts,
|
||||
NULL)) != -1) {
|
||||
switch (c) {
|
||||
case 'n':
|
||||
init_pid_str = optarg;
|
||||
|
@ -119,7 +133,9 @@ void nsenter() {
|
|||
|
||||
init_pid = strtol(init_pid_str, NULL, 10);
|
||||
if ((init_pid == 0 && errno == EINVAL) || errno == ERANGE) {
|
||||
fprintf(stderr, "nsenter: Failed to parse PID from \"%s\" with output \"%d\" and error: \"%s\"\n", init_pid_str, init_pid, strerror(errno));
|
||||
fprintf(stderr,
|
||||
"nsenter: Failed to parse PID from \"%s\" with output \"%d\" and error: \"%s\"\n",
|
||||
init_pid_str, init_pid, strerror(errno));
|
||||
print_usage();
|
||||
exit(1);
|
||||
}
|
||||
|
@ -131,24 +147,24 @@ void nsenter() {
|
|||
fprintf(stderr, "setsid failed. Error: %s\n", strerror(errno));
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// before we setns we need to dup the console
|
||||
int consolefd = -1;
|
||||
if (console != NULL) {
|
||||
consolefd = open(console, O_RDWR);
|
||||
if (consolefd < 0) {
|
||||
fprintf(stderr, "nsenter: failed to open console %s %s\n", console, strerror(errno));
|
||||
fprintf(stderr,
|
||||
"nsenter: failed to open console %s %s\n",
|
||||
console, strerror(errno));
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
// Setns on all supported namespaces.
|
||||
char ns_dir[PATH_MAX];
|
||||
memset(ns_dir, 0, PATH_MAX);
|
||||
snprintf(ns_dir, PATH_MAX - 1, "/proc/%d/ns/", init_pid);
|
||||
|
||||
char* namespaces[] = {"ipc", "uts", "net", "pid", "mnt"};
|
||||
const int num = sizeof(namespaces) / sizeof(char*);
|
||||
char *namespaces[] = { "ipc", "uts", "net", "pid", "mnt" };
|
||||
const int num = sizeof(namespaces) / sizeof(char *);
|
||||
int i;
|
||||
for (i = 0; i < num; i++) {
|
||||
char buf[PATH_MAX];
|
||||
|
@ -160,13 +176,16 @@ void nsenter() {
|
|||
if (errno == ENOENT)
|
||||
continue;
|
||||
|
||||
fprintf(stderr, "nsenter: Failed to open ns file \"%s\" for ns \"%s\" with error: \"%s\"\n", buf, namespaces[i], strerror(errno));
|
||||
fprintf(stderr,
|
||||
"nsenter: Failed to open ns file \"%s\" for ns \"%s\" with error: \"%s\"\n",
|
||||
buf, namespaces[i], strerror(errno));
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// Set the namespace.
|
||||
if (setns(fd, 0) == -1) {
|
||||
fprintf(stderr, "nsenter: Failed to setns for \"%s\" with error: \"%s\"\n", namespaces[i], strerror(errno));
|
||||
fprintf(stderr,
|
||||
"nsenter: Failed to setns for \"%s\" with error: \"%s\"\n",
|
||||
namespaces[i], strerror(errno));
|
||||
exit(1);
|
||||
}
|
||||
close(fd);
|
||||
|
@ -177,29 +196,32 @@ void nsenter() {
|
|||
if (child == 0) {
|
||||
if (consolefd != -1) {
|
||||
if (dup2(consolefd, STDIN_FILENO) != 0) {
|
||||
fprintf(stderr, "nsenter: failed to dup 0 %s\n", strerror(errno));
|
||||
fprintf(stderr, "nsenter: failed to dup 0 %s\n",
|
||||
strerror(errno));
|
||||
exit(1);
|
||||
}
|
||||
if (dup2(consolefd, STDOUT_FILENO) != STDOUT_FILENO) {
|
||||
fprintf(stderr, "nsenter: failed to dup 1 %s\n", strerror(errno));
|
||||
fprintf(stderr, "nsenter: failed to dup 1 %s\n",
|
||||
strerror(errno));
|
||||
exit(1);
|
||||
}
|
||||
if (dup2(consolefd, STDERR_FILENO) != STDERR_FILENO) {
|
||||
fprintf(stderr, "nsenter: failed to dup 2 %s\n", strerror(errno));
|
||||
fprintf(stderr, "nsenter: failed to dup 2 %s\n",
|
||||
strerror(errno));
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
// Finish executing, let the Go runtime take over.
|
||||
return;
|
||||
} else {
|
||||
// Parent, wait for the child.
|
||||
int status = 0;
|
||||
if (waitpid(child, &status, 0) == -1) {
|
||||
fprintf(stderr, "nsenter: Failed to waitpid with error: \"%s\"\n", strerror(errno));
|
||||
fprintf(stderr,
|
||||
"nsenter: Failed to waitpid with error: \"%s\"\n",
|
||||
strerror(errno));
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// Forward the child's exit code or re-send its death signal.
|
||||
if (WIFEXITED(status)) {
|
||||
exit(WEXITSTATUS(status));
|
||||
|
|
Loading…
Reference in New Issue