Skip to contentSkip to conversation

master - log: separate output and make it possible to use given FDs

PATCH Peter Rajnoha 1 participant 1 message Aug 9, 2016

lore
Share & tools
lore
#0 lore raw
Gitweb:        http://git.fedorahosted.org/git/?p=lvm2.git;a=commitdiff;h=06ce9b4e4238da58d6c2acda7878dbc51245a8fc
Commit:        06ce9b4e4238da58d6c2acda7878dbc51245a8fc
Parent:        bb9789f2b3404893dcb92cfc2d1eeec002c90140
Author:        Peter Rajnoha <[email protected]>
AuthorDate:    Fri Jul 8 16:47:51 2016 +0200
Committer:     Peter Rajnoha <[email protected]>
CommitterDate: Tue Aug 9 18:24:45 2016 +0200

log: separate output and make it possible to use given FDs

Currently, the output is separated in 3 parts and each part can go into a separate and user-defined file descriptor:

- common output (stdout by default, customizable by LVM_OUT_FD environment variable)
- error output (stderr by default, customizable by LVM_ERR_FD environment variable)
- report output (stdout by default, customizable by LVM_REPORT_FD environment variable)

For example, each type of output goes to different output file:

[0] fedora/~ # export LVM_REPORT_FD=3

[0] fedora/~ # lvs fedora vg/abc 1>out 2>err 3>report

[0] fedora/~ # cat out

[0] fedora/~ # cat err
Volume group "vg" not found
Cannot process volume group vg

[0] fedora/~ # cat report

    LV   VG     Attr       LSize   Layout     Role       CTime
    root fedora -wi-ao----  19.00g linear     public     Wed May 27 2015 08:09:21
    swap fedora -wi-ao---- 500.00m linear     public     Wed May 27 2015 08:09:21

Another example in LVM shell where the report goes to "report" file:

[0] fedora/~ # export LVM_REPORT_FD=3
[0] fedora/~ # lvm 3>report

(in lvm shell)
lvm> vgs

(content of "report" file)
[1] fedora/~ # cat report
VG #PV #LV #SN Attr VSize VFree
fedora 1 2 0 wz--n- 19.49g 0

(in lvm shell)
lvm> lvs

(content of "report" file)
[1] fedora/~ # cat report

    VG     #PV #LV #SN Attr   VSize  VFree
    fedora   1   2   0 wz--n- 19.49g    0
    LV   VG     Attr       LSize   Layout     Role       CTime
    root fedora -wi-ao----  19.00g linear     public     Wed May 27 2015 08:09:21
    swap fedora -wi-ao---- 500.00m linear     public     Wed May 27 2015 08:09:21
---
 lib/commands/toolcontext.c |   50 ++-----------
 lib/log/log.c              |  172 +++++++++++++++++++++++++++++++++++++++++---
 lib/log/lvm-logging.h      |    5 ++
 lib/misc/lvm-file.h        |    6 ++
 man/lvm.8.in               |    9 +++
 tools/lvmcmdline.c         |   62 ++++++++++++++--
 6 files changed, 245 insertions(+), 59 deletions(-)
lib/commands/toolcontext.c +6 -44
index d3bd5bc..82e990e 100644
@@ -1650,37 +1650,6 @@ static void _init_globals(struct cmd_context *cmd)
 }
 
 /*
- * Close and reopen stream on file descriptor fd.
- */
-static int _reopen_stream(FILE *stream, int fd, const char *mode, const char *name, FILE **new_stream)
-{
-	int fd_copy, new_fd;
-
-	if ((fd_copy = dup(fd)) < 0) {
-		log_sys_error("dup", name);
-		return 0;
-	}
-
-	if (fclose(stream))
-		log_sys_error("fclose", name);
-
-	if ((new_fd = dup2(fd_copy, fd)) < 0)
-		log_sys_error("dup2", name);
-	else if (new_fd != fd)
-		log_error("dup2(%d, %d) returned %d", fd_copy, fd, new_fd);
-
-	if (close(fd_copy) < 0)
-		log_sys_error("close", name);
-
-	if (!(*new_stream = fdopen(fd, mode))) {
-		log_sys_error("fdopen", name);
-		return 0;
-	}
-
-	return 1;
-}
-
-/*
  * init_connections();
  *   _init_lvmetad();
  *     lvmetad_disconnect();  (close previous connection)
@@ -1835,7 +1804,6 @@ struct cmd_context *create_toolcontext(unsigned is_long_lived,
 				       unsigned set_filters)
 {
 	struct cmd_context *cmd;
-	FILE *new_stream;
 	int flags;
 
 #ifdef M_MMAP_MAX
@@ -1885,9 +1853,8 @@ struct cmd_context *create_toolcontext(unsigned is_long_lived,
 		if (is_valid_fd(STDIN_FILENO) &&
 		    ((flags = fcntl(STDIN_FILENO, F_GETFL)) > 0) &&
 		    (flags & O_ACCMODE) != O_WRONLY) {
-			if (!_reopen_stream(stdin, STDIN_FILENO, "r", "stdin", &new_stream))
+			if (!reopen_standard_stream(&stdin, "r"))
 				goto_out;
-			stdin = new_stream;
 			if (setvbuf(stdin, cmd->linebuffer, _IOLBF, linebuffer_size)) {
 				log_sys_error("setvbuf", "");
 				goto out;
@@ -1897,9 +1864,8 @@ struct cmd_context *create_toolcontext(unsigned is_long_lived,
 		if (is_valid_fd(STDOUT_FILENO) &&
 		    ((flags = fcntl(STDOUT_FILENO, F_GETFL)) > 0) &&
 		    (flags & O_ACCMODE) != O_RDONLY) {
-			if (!_reopen_stream(stdout, STDOUT_FILENO, "w", "stdout", &new_stream))
+			if (!reopen_standard_stream(&stdout, "w"))
 				goto_out;
-			stdout = new_stream;
 			if (setvbuf(stdout, cmd->linebuffer + linebuffer_size,
 				     _IOLBF, linebuffer_size)) {
 				log_sys_error("setvbuf", "");
@@ -1911,7 +1877,6 @@ struct cmd_context *create_toolcontext(unsigned is_long_lived,
 		/* Without buffering, must not use stdin/stdout */
 		init_silent(1);
 #endif
-
 	/*
 	 * Environment variable LVM_SYSTEM_DIR overrides this below.
 	 */
@@ -2227,7 +2192,6 @@ int refresh_toolcontext(struct cmd_context *cmd)
 void destroy_toolcontext(struct cmd_context *cmd)
 {
 	struct dm_config_tree *cft_cmdline;
-	FILE *new_stream;
 	int flags;
 
 	if (cmd->dump_filter && cmd->filter && cmd->filter->dump &&
@@ -2266,20 +2230,18 @@ void destroy_toolcontext(struct cmd_context *cmd)
 		if (is_valid_fd(STDIN_FILENO) &&
 		    ((flags = fcntl(STDIN_FILENO, F_GETFL)) > 0) &&
 		    (flags & O_ACCMODE) != O_WRONLY) {
-			if (_reopen_stream(stdin, STDIN_FILENO, "r", "stdin", &new_stream)) {
-				stdin = new_stream;
+			if (reopen_standard_stream(&stdin, "r"))
 				setlinebuf(stdin);
-			} else
+			else
 				cmd->linebuffer = NULL;	/* Leave buffer in place (deliberate leak) */
 		}
 
 		if (is_valid_fd(STDOUT_FILENO) &&
 		    ((flags = fcntl(STDOUT_FILENO, F_GETFL)) > 0) &&
 		    (flags & O_ACCMODE) != O_RDONLY) {
-			if (_reopen_stream(stdout, STDOUT_FILENO, "w", "stdout", &new_stream)) {
-				stdout = new_stream;
+			if (reopen_standard_stream(&stdout, "w"))
 				setlinebuf(stdout);
-			} else
+			else
 				cmd->linebuffer = NULL;	/* Leave buffer in place (deliberate leak) */
 		}
 
lib/log/log.c +163 -9

225 lines not on this page · raw message

lib/log/lvm-logging.h +5 -0
index 4101bc8..7ddbf2c 100644
@@ -16,6 +16,8 @@
 #ifndef _LVM_LOGGING_H
 #define _LVM_LOGGING_H
 
+#include "lvm-file.h"
+
 __attribute__ ((format(printf, 5, 6)))
 void print_log(int level, const char *file, int line, int dm_errno_or_class,
 	       const char *format, ...);
@@ -35,6 +37,9 @@ void print_log_libdm(int level, const char *file, int line, int dm_errno_or_clas
 
 #include "log.h"
 
+int init_custom_log_streams(struct custom_fds *custom_fds);
+int reopen_standard_stream(FILE **stream, const char *mode);
+
 typedef void (*lvm2_log_fn_t) (int level, const char *file, int line,
 			       int dm_errno_or_class, const char *message);
 
lib/misc/lvm-file.h +6 -0
index c92f783..6128abd 100644
@@ -16,6 +16,12 @@
 #ifndef _LVM_FILE_H
 #define _LVM_FILE_H
 
+struct custom_fds {
+	int out;
+	int err;
+	int report;
+};
+
 /*
  * Create a temporary filename, and opens a descriptor to the file.
  */
man/lvm.8.in +9 -0
index 0215783..76d05fd 100644
@@ -748,6 +748,15 @@ All tools return a status code of zero on success or non-zero on failure.
 Directory containing \fI.lvm_history\fP if the internal readline
 shell is invoked.
 .TP
+.B LVM_OUT_FD
+File descriptor to use for common output from LVM commands.
+.TP
+.B LVM_ERR_FD
+File descriptor to use for error output from LVM commands.
+.TP
+.B LVM_REPORT_FD
+File descriptor to use for report output from LVM commands.
+.TP
 .B LVM_COMMAND_PROFILE
 Name of default command profile to use for LVM commands. This profile
 is overriden by direct use of \fB\-\-commandprofile\fP command line option.
tools/lvmcmdline.c +56 -6

99 lines not on this page · raw message