nsenter: check errors from open(), read() and open()
Currently if nsenter is executed without /proc, it segfaulted. Signed-off-by: Andrey Vagin <avagin@openvz.org>
This commit is contained in:
parent
b7e54b0b41
commit
ecace12e5a
|
@ -22,6 +22,11 @@ void get_args(int *argc, char ***argv)
|
||||||
{
|
{
|
||||||
// Read argv
|
// Read argv
|
||||||
int fd = open("/proc/self/cmdline", O_RDONLY);
|
int fd = open("/proc/self/cmdline", O_RDONLY);
|
||||||
|
if (fd < 0) {
|
||||||
|
fprintf(stderr,
|
||||||
|
"nsenter: Unable to open /proc/self/cmdline: %m\n");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
// Read the whole commandline.
|
// Read the whole commandline.
|
||||||
ssize_t contents_size = 0;
|
ssize_t contents_size = 0;
|
||||||
|
@ -34,6 +39,11 @@ void get_args(int *argc, char ***argv)
|
||||||
bytes_read =
|
bytes_read =
|
||||||
read(fd, contents + contents_offset,
|
read(fd, contents + contents_offset,
|
||||||
contents_size - contents_offset);
|
contents_size - contents_offset);
|
||||||
|
if (bytes_read < 0) {
|
||||||
|
fprintf(stderr,
|
||||||
|
"nsenter: Unable to read from /proc/self/cmdline: %m\n");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
contents_offset += bytes_read;
|
contents_offset += bytes_read;
|
||||||
}
|
}
|
||||||
while (bytes_read > 0);
|
while (bytes_read > 0);
|
||||||
|
@ -184,6 +194,10 @@ void nsenter()
|
||||||
|
|
||||||
// We must fork to actually enter the PID namespace.
|
// We must fork to actually enter the PID namespace.
|
||||||
int child = fork();
|
int child = fork();
|
||||||
|
if (child == -1) {
|
||||||
|
fprintf(stderr, "nsenter: Unable to fork a process: %m\n");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
if (child == 0) {
|
if (child == 0) {
|
||||||
if (consolefd != -1) {
|
if (consolefd != -1) {
|
||||||
if (dup2(consolefd, STDIN_FILENO) != 0) {
|
if (dup2(consolefd, STDIN_FILENO) != 0) {
|
||||||
|
|
Loading…
Reference in New Issue