summaryrefslogtreecommitdiffstats
path: root/tools
diff options
context:
space:
mode:
authorZdenek Kabelac <zkabelac@redhat.com>2012-09-07 11:13:41 +0200
committerZdenek Kabelac <zkabelac@redhat.com>2012-10-03 15:02:26 +0200
commit739092e64ac2776a0b0b0796349041b659d816b0 (patch)
tree51e65b077b682bd1cecc86ae8ee6a816aa7af37a /tools
parent1f30e048bd8b387e25baad4f82223b63dc516cf5 (diff)
downloadlvm2-739092e64ac2776a0b0b0796349041b659d816b0.tar.gz
lvm2-739092e64ac2776a0b0b0796349041b659d816b0.tar.xz
lvm2-739092e64ac2776a0b0b0796349041b659d816b0.zip
liblvm2cmd: ensure standard descriptors are ready
Check if FDs 0,1,2 are available, and in case they are missing, use /dev/null for them.
Diffstat (limited to 'tools')
-rw-r--r--tools/lvmcmdline.c39
1 files changed, 38 insertions, 1 deletions
diff --git a/tools/lvmcmdline.c b/tools/lvmcmdline.c
index e015a127..bccf7a64 100644
--- a/tools/lvmcmdline.c
+++ b/tools/lvmcmdline.c
@@ -1,6 +1,6 @@
/*
* Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
- * Copyright (C) 2004-2009 Red Hat, Inc. All rights reserved.
+ * Copyright (C) 2004-2012 Red Hat, Inc. All rights reserved.
*
* This file is part of LVM2.
*
@@ -26,6 +26,7 @@
#include <time.h>
#include <sys/resource.h>
#include <dirent.h>
+#include <paths.h>
#ifdef HAVE_GETOPTLONG
# include <getopt.h>
@@ -1186,6 +1187,39 @@ int lvm_split(char *str, int *argc, char **argv, int max)
return *argc;
}
+/* Make sure we have always valid filedescriptors 0,1,2 */
+static int _check_standard_fds(void)
+{
+ int err = is_valid_fd(STDERR_FILENO);
+
+ if (!is_valid_fd(STDIN_FILENO) &&
+ !(stdin = fopen(_PATH_DEVNULL, "r"))) {
+ if (err)
+ perror("stdin stream open");
+ else
+ printf("stdin stream open: %s\n",
+ strerror(errno));
+ return 0;
+ }
+
+ if (!is_valid_fd(STDOUT_FILENO) &&
+ !(stdout = fopen(_PATH_DEVNULL, "w"))) {
+ if (err)
+ perror("stdout stream open");
+ /* else no stdout */
+ return 0;
+ }
+
+ if (!is_valid_fd(STDERR_FILENO) &&
+ !(stderr = fopen(_PATH_DEVNULL, "w"))) {
+ printf("stderr stream open: %s\n",
+ strerror(errno));
+ return 0;
+ }
+
+ return 1;
+}
+
static const char *_get_cmdline(pid_t pid)
{
static char _proc_cmdline[32];
@@ -1450,6 +1484,9 @@ int lvm2_main(int argc, char **argv)
strcmp(base, "initrd-lvm"))
alias = 1;
+ if (!_check_standard_fds())
+ return -1;
+
if (!_close_stray_fds(base))
return -1;