From 85cd86999f70339509692b92cf182ec36697edcf Mon Sep 17 00:00:00 2001 From: Andrey Vagin Date: Tue, 6 Jan 2015 09:17:48 +0300 Subject: [PATCH] nsenter: use %m instead of strerror(errno) Signed-off-by: Andrey Vagin --- namespaces/nsenter/nsenter.c | 40 +++++++++++++++--------------------- 1 file changed, 17 insertions(+), 23 deletions(-) diff --git a/namespaces/nsenter/nsenter.c b/namespaces/nsenter/nsenter.c index fbf44509..0999afde 100644 --- a/namespaces/nsenter/nsenter.c +++ b/namespaces/nsenter/nsenter.c @@ -101,8 +101,7 @@ void nsenter() #ifdef PR_SET_CHILD_SUBREAPER if (prctl(PR_SET_CHILD_SUBREAPER, 1, 0, 0, 0) == -1) { - fprintf(stderr, "nsenter: failed to set child subreaper: %s", - strerror(errno)); + fprintf(stderr, "nsenter: failed to set child subreaper: %m\n"); exit(1); } #endif @@ -135,8 +134,8 @@ 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)); + "nsenter: Failed to parse PID from \"%s\" with output \"%d\" and error: %m\n", + init_pid_str, init_pid); print_usage(); exit(1); } @@ -145,7 +144,7 @@ void nsenter() argv += 3; if (setsid() == -1) { - fprintf(stderr, "setsid failed. Error: %s\n", strerror(errno)); + fprintf(stderr, "setsid failed. Error: %m\n"); exit(1); } // before we setns we need to dup the console @@ -154,8 +153,8 @@ void nsenter() consolefd = open(console, O_RDWR); if (consolefd < 0) { fprintf(stderr, - "nsenter: failed to open console %s %s\n", - console, strerror(errno)); + "nsenter: failed to open console %s: %m\n", + console); exit(1); } } @@ -167,8 +166,7 @@ void nsenter() int ns_dir_fd; ns_dir_fd = open(ns_dir, O_RDONLY | O_DIRECTORY); if (ns_dir_fd < 0) { - fprintf(stderr, - "Unable to open %s: %m\n", ns_dir); + fprintf(stderr, "Unable to open %s: %m\n", ns_dir); exit(1); } @@ -182,23 +180,23 @@ void nsenter() if (errno == ENOENT) continue; fprintf(stderr, - "nsenter: Failed to stat ns file \"%s\" for ns \"%s\" with error: \"%s\"\n", - ns_dir, namespaces[i], strerror(errno)); + "nsenter: Failed to stat ns file \"%s\" for ns \"%s\" with error: %m\n", + ns_dir, namespaces[i]); exit(1); } int fd = openat(ns_dir_fd, namespaces[i], O_RDONLY); if (fd == -1) { fprintf(stderr, - "nsenter: Failed to open ns file \"%s\" for ns \"%s\" with error: \"%s\"\n", - ns_dir, namespaces[i], strerror(errno)); + "nsenter: Failed to open ns file \"%s\" for ns \"%s\" with error: %m\n", + ns_dir, namespaces[i]); 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)); + "nsenter: Failed to setns for \"%s\" with error: %m\n", + namespaces[i]); exit(1); } close(fd); @@ -214,18 +212,15 @@ 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: %m\n"); 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: %m\n"); 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: %m\n"); exit(1); } } @@ -236,8 +231,7 @@ void nsenter() int status = 0; if (waitpid(child, &status, 0) == -1) { fprintf(stderr, - "nsenter: Failed to waitpid with error: \"%s\"\n", - strerror(errno)); + "nsenter: Failed to waitpid with error: %m\n"); exit(1); } // Forward the child's exit code or re-send its death signal.