[RFC] crtools: flush early log using destructor

Submitted by Radostin Stoyanov on Sept. 11, 2019, 2:38 p.m.

Details

Message ID 20190911143814.25185-1-rstoyanov1@gmail.com
State New
Series "crtools: flush early log using destructor"
Headers show

Commit Message

Radostin Stoyanov Sept. 11, 2019, 2:38 p.m.
Print error messages to stderr (instead of stdout) when the early log
is not initialised.

This patch also removes changes introduced with commit 970198a

Suggested-by: Dmitry Safonov <0x7f454c46@gmail.com>
Signed-off-by: Radostin Stoyanov <rstoyanov1@gmail.com>
---
 criu/crtools.c    | 31 ++++++++++++++++++-------------
 criu/proc_parse.c |  2 +-
 2 files changed, 19 insertions(+), 14 deletions(-)

--
2.21.0

Patch hide | download patch | download mbox

diff --git a/criu/crtools.c b/criu/crtools.c
index 97a6d6d6c..9db0bc486 100644
--- a/criu/crtools.c
+++ b/criu/crtools.c
@@ -48,6 +48,13 @@ 
 #include "sysctl.h"
 #include "img-remote.h"

+void flush_early_log_to_stderr() __attribute__((destructor));
+
+void flush_early_log_to_stderr(void)
+{
+	flush_early_log_buffer(STDERR_FILENO);
+}
+
 int main(int argc, char *argv[], char *envp[])
 {
 	int ret = -1;
@@ -96,10 +103,8 @@  int main(int argc, char *argv[], char *envp[])
 		return cr_service_work(atoi(argv[2]));
 	}

-	if (check_options()) {
-		flush_early_log_buffer(STDERR_FILENO);
+	if (check_options())
 		return 1;
-	}

 	if (opts.imgs_dir == NULL)
 		SET_CHAR_OPTS(imgs_dir, ".");
@@ -108,7 +113,7 @@  int main(int argc, char *argv[], char *envp[])
 		SET_CHAR_OPTS(work_dir, opts.imgs_dir);

 	if (optind >= argc) {
-		pr_msg("Error: command is required\n");
+		pr_err("command is required\n");
 		goto usage;
 	}

@@ -116,17 +121,17 @@  int main(int argc, char *argv[], char *envp[])

 	if (has_exec_cmd) {
 		if (!has_sub_command) {
-			pr_msg("Error: --exec-cmd requires a command\n");
+			pr_err("--exec-cmd requires a command\n");
 			goto usage;
 		}

 		if (strcmp(argv[optind], "restore")) {
-			pr_msg("Error: --exec-cmd is available for the restore command only\n");
+			pr_err("--exec-cmd is available for the restore command only\n");
 			goto usage;
 		}

 		if (opts.restore_detach) {
-			pr_msg("Error: --restore-detached and --exec-cmd cannot be used together\n");
+			pr_err("--restore-detached and --exec-cmd cannot be used together\n");
 			goto usage;
 		}

@@ -138,7 +143,7 @@  int main(int argc, char *argv[], char *envp[])
 	} else {
 		/* No subcommands except for cpuinfo and restore --exec-cmd */
 		if (strcmp(argv[optind], "cpuinfo") && has_sub_command) {
-			pr_msg("Error: excessive parameter%s for command %s\n",
+			pr_err("excessive parameter%s for command %s\n",
 				(argc - optind) > 2 ? "s" : "", argv[optind]);
 			goto usage;
 		}
@@ -237,7 +242,7 @@  int main(int argc, char *argv[], char *envp[])

 	if (!strcmp(argv[optind], "image-proxy")) {
 		if (!opts.addr) {
-			pr_msg("Error: address not specified\n");
+			pr_err("address not specified\n");
 			return 1;
 		}
 		if (!opts.port)
@@ -253,7 +258,7 @@  int main(int argc, char *argv[], char *envp[])

 	if (!strcmp(argv[optind], "cpuinfo")) {
 		if (!argv[optind + 1]) {
-			pr_msg("Error: cpuinfo requires an action: dump or check\n");
+			pr_err("cpuinfo requires an action: dump or check\n");
 			goto usage;
 		}
 		if (!strcmp(argv[optind + 1], "dump"))
@@ -273,7 +278,7 @@  int main(int argc, char *argv[], char *envp[])
 		return -1;
 	}

-	pr_msg("Error: unknown command: %s\n", argv[optind]);
+	pr_err("unknown command: %s\n", argv[optind]);
 usage:
 	pr_msg("\n"
 "Usage:\n"
@@ -469,10 +474,10 @@  usage:
 	return 0;

 opt_port_missing:
-	pr_msg("Error: port not specified\n");
+	pr_err("port not specified\n");
 	return 1;

 opt_pid_missing:
-	pr_msg("Error: pid not specified\n");
+	pr_err("pid not specified\n");
 	return 1;
 }
diff --git a/criu/proc_parse.c b/criu/proc_parse.c
index 0e8b6f209..97f82ee01 100644
--- a/criu/proc_parse.c
+++ b/criu/proc_parse.c
@@ -932,7 +932,7 @@  int prepare_loginuid(unsigned int value, unsigned int loglevel)

 	if (write(fd, buf, 11) < 0) {
 		print_on_level(loglevel,
-			"Write %s to /proc/self/loginuid failed: %s",
+			"Write %s to /proc/self/loginuid failed: %s\n",
 			buf, strerror(errno));
 		ret = -1;
 	}

Comments

Andrei Vagin Sept. 15, 2019, 4:43 a.m.
On Wed, Sep 11, 2019 at 03:38:14PM +0100, Radostin Stoyanov wrote:
> Print error messages to stderr (instead of stdout) when the early log
> is not initialised.
> 
> This patch also removes changes introduced with commit 970198a
> 
> Suggested-by: Dmitry Safonov <0x7f454c46@gmail.com>
> Signed-off-by: Radostin Stoyanov <rstoyanov1@gmail.com>
> ---
>  criu/crtools.c    | 31 ++++++++++++++++++-------------
>  criu/proc_parse.c |  2 +-
>  2 files changed, 19 insertions(+), 14 deletions(-)
> 
> diff --git a/criu/crtools.c b/criu/crtools.c
> index 97a6d6d6c..9db0bc486 100644
> --- a/criu/crtools.c
> +++ b/criu/crtools.c
> @@ -48,6 +48,13 @@
>  #include "sysctl.h"
>  #include "img-remote.h"
> 
> +void flush_early_log_to_stderr() __attribute__((destructor));
> +
> +void flush_early_log_to_stderr(void)
> +{
> +	flush_early_log_buffer(STDERR_FILENO);
> +}
> +
>  int main(int argc, char *argv[], char *envp[])
>  {
>  	int ret = -1;
> @@ -96,10 +103,8 @@ int main(int argc, char *argv[], char *envp[])
>  		return cr_service_work(atoi(argv[2]));
>  	}
> 
> -	if (check_options()) {
> -		flush_early_log_buffer(STDERR_FILENO);
> +	if (check_options())
>  		return 1;
> -	}
> 
>  	if (opts.imgs_dir == NULL)
>  		SET_CHAR_OPTS(imgs_dir, ".");
> @@ -108,7 +113,7 @@ int main(int argc, char *argv[], char *envp[])
>  		SET_CHAR_OPTS(work_dir, opts.imgs_dir);
> 
>  	if (optind >= argc) {
> -		pr_msg("Error: command is required\n");
> +		pr_err("command is required\n");

Could you split this path on two:
* Introduce the flush_early_log_to_stderr destructor
* convert pr_msg to pr_err.

>  		goto usage;
>  	}
> 
> @@ -116,17 +121,17 @@ int main(int argc, char *argv[], char *envp[])
> 
>  	if (has_exec_cmd) {
>  		if (!has_sub_command) {
> -			pr_msg("Error: --exec-cmd requires a command\n");
> +			pr_err("--exec-cmd requires a command\n");
>  			goto usage;
>  		}
> 
>  		if (strcmp(argv[optind], "restore")) {
> -			pr_msg("Error: --exec-cmd is available for the restore command only\n");
> +			pr_err("--exec-cmd is available for the restore command only\n");
>  			goto usage;
>  		}
> 
>  		if (opts.restore_detach) {
> -			pr_msg("Error: --restore-detached and --exec-cmd cannot be used together\n");
> +			pr_err("--restore-detached and --exec-cmd cannot be used together\n");
>  			goto usage;
>  		}
> 
> @@ -138,7 +143,7 @@ int main(int argc, char *argv[], char *envp[])
>  	} else {
>  		/* No subcommands except for cpuinfo and restore --exec-cmd */
>  		if (strcmp(argv[optind], "cpuinfo") && has_sub_command) {
> -			pr_msg("Error: excessive parameter%s for command %s\n",
> +			pr_err("excessive parameter%s for command %s\n",
>  				(argc - optind) > 2 ? "s" : "", argv[optind]);
>  			goto usage;
>  		}
> @@ -237,7 +242,7 @@ int main(int argc, char *argv[], char *envp[])
> 
>  	if (!strcmp(argv[optind], "image-proxy")) {
>  		if (!opts.addr) {
> -			pr_msg("Error: address not specified\n");
> +			pr_err("address not specified\n");
>  			return 1;
>  		}
>  		if (!opts.port)
> @@ -253,7 +258,7 @@ int main(int argc, char *argv[], char *envp[])
> 
>  	if (!strcmp(argv[optind], "cpuinfo")) {
>  		if (!argv[optind + 1]) {
> -			pr_msg("Error: cpuinfo requires an action: dump or check\n");
> +			pr_err("cpuinfo requires an action: dump or check\n");
>  			goto usage;
>  		}
>  		if (!strcmp(argv[optind + 1], "dump"))
> @@ -273,7 +278,7 @@ int main(int argc, char *argv[], char *envp[])
>  		return -1;
>  	}
> 
> -	pr_msg("Error: unknown command: %s\n", argv[optind]);
> +	pr_err("unknown command: %s\n", argv[optind]);
>  usage:
>  	pr_msg("\n"
>  "Usage:\n"
> @@ -469,10 +474,10 @@ usage:
>  	return 0;
> 
>  opt_port_missing:
> -	pr_msg("Error: port not specified\n");
> +	pr_err("port not specified\n");
>  	return 1;
> 
>  opt_pid_missing:
> -	pr_msg("Error: pid not specified\n");
> +	pr_err("pid not specified\n");
>  	return 1;
>  }
> diff --git a/criu/proc_parse.c b/criu/proc_parse.c
> index 0e8b6f209..97f82ee01 100644
> --- a/criu/proc_parse.c
> +++ b/criu/proc_parse.c
> @@ -932,7 +932,7 @@ int prepare_loginuid(unsigned int value, unsigned int loglevel)
> 
>  	if (write(fd, buf, 11) < 0) {
>  		print_on_level(loglevel,
> -			"Write %s to /proc/self/loginuid failed: %s",
> +			"Write %s to /proc/self/loginuid failed: %s\n",
>  			buf, strerror(errno));
>  		ret = -1;
>  	}
> --
> 2.21.0
>