From: "Serge E. Hallyn" <serue-r/Jw6+rmf7HQT0dZR+AlfA@public.gmane.org>
To: Oren Laadan <orenl-RdfvBDnrOixBDgjK7y7TUQ@public.gmane.org>
Cc: containers-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA@public.gmane.org
Subject: Re: [PATCH] user-cr: add --output-fd to write output to a specific fd
Date: Mon, 26 Oct 2009 11:50:54 -0500 [thread overview]
Message-ID: <20091026165054.GE23564@us.ibm.com> (raw)
In-Reply-To: <1256508801-2426-1-git-send-email-orenl-RdfvBDnrOixBDgjK7y7TUQ@public.gmane.org>
Quoting Oren Laadan (orenl-RdfvBDnrOixBDgjK7y7TUQ@public.gmane.org):
> This is useful if the user would like redirect the output to
> e.g, a socket or any other already open file descriptor when
> invoking 'checkpoint'.
>
> Also useful if the user would like to append an existing file.
>
> Signed-off-by: Oren Laadan <orenl-eQaUEPhvms7ENvBUuze7eA@public.gmane.org>
Acked-by: Serge Hallyn <serue-r/Jw6+rmf7HQT0dZR+AlfA@public.gmane.org>
> ---
> checkpoint.c | 47 +++++++++++++++++++++++++++++++++++++++++------
> 1 files changed, 41 insertions(+), 6 deletions(-)
>
> diff --git a/checkpoint.c b/checkpoint.c
> index c116daf..aef954b 100644
> --- a/checkpoint.c
> +++ b/checkpoint.c
> @@ -32,12 +32,14 @@ static char usage_str[] =
> "\tOptions:\n"
> " -h,--help print this help message\n"
> " -o,--output=FILE write data to FILE instead of standard output\n"
> +" --output-fd=FD write data to file descriptor FD instead of stdout\n"
> " -c,--container require the PID is a container-init\n"
> " -v,--verbose verbose output\n"
> "";
>
> struct args {
> char *output;
> + int outputfd;
> int container;
> int verbose;
> };
> @@ -53,17 +55,33 @@ static void usage(char *str)
> exit(1);
> }
>
> +/* negative retval means error */
> +static int str2num(char *str)
> +{
> + char *nptr;
> + int num;
> +
> + num = strtol(str, &nptr, 10);
> + if (nptr - str != strlen(str))
> + num = -1;
> + return num;
> +}
> +
> static void parse_args(struct args *args, int argc, char *argv[])
> {
> static struct option opts[] = {
> { "help", no_argument, NULL, 'h' },
> { "output", required_argument, NULL, 'o' },
> + { "output-fd", required_argument, NULL, 1 },
> { "container", no_argument, NULL, 'c' },
> { "verbose", no_argument, NULL, 'v' },
> { NULL, 0, NULL, 0 }
> };
> static char optc[] = "hvco:";
>
> + /* defaults */
> + args->outputfd = -1;
> +
> while (1) {
> int c = getopt_long(argc, argv, optc, opts, NULL);
> if (c == -1)
> @@ -76,6 +94,13 @@ static void parse_args(struct args *args, int argc, char *argv[])
> case 'o':
> args->output = optarg;
> break;
> + case 1:
> + args->outputfd = str2num(optarg);
> + if (args->outputfd < 0) {
> + printf("checkpoint: invalid file descriptor\n");
> + exit(1);
> + }
> + break;
> case 'c':
> args->container = 1;
> break;
> @@ -86,6 +111,12 @@ static void parse_args(struct args *args, int argc, char *argv[])
> usage(usage_str);
> }
> }
> +
> + if (args->output && args->outputfd >= 0) {
> + printf("Invalid used of both -o/--output and --output-fd\n");
> + exit(1);
> + }
> +
> }
>
> int main(int argc, char *argv[])
> @@ -111,19 +142,23 @@ int main(int argc, char *argv[])
> exit(1);
> }
>
> - /* output file (default: stdout) */
> + /* output file */
> if (args.output) {
> - ret = open(args.output, O_RDWR | O_CREAT, 0);
> - if (ret < 0) {
> + args.outputfd = open(args.output, O_RDWR | O_CREAT, 0);
> + if (args.outputfd < 0) {
> perror("open output file");
> exit(1);
> }
> - if (dup2(ret, STDOUT_FILENO) < 0) {
> + }
> +
> + /* output file descriptor (default: stdout) */
> + if (args.outputfd >= 0) {
> + if (dup2(args.outputfd, STDOUT_FILENO) < 0) {
> perror("dup2 output file");
> exit(1);
> }
> - if (ret != STDOUT_FILENO)
> - close(ret);
> + if (args.outputfd != STDOUT_FILENO)
> + close(args.outputfd);
> }
>
> ret = checkpoint(pid, STDOUT_FILENO, flags);
> --
> 1.6.0.4
>
> _______________________________________________
> Containers mailing list
> Containers-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA@public.gmane.org
> https://lists.linux-foundation.org/mailman/listinfo/containers
next prev parent reply other threads:[~2009-10-26 16:50 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-10-25 22:13 [PATCH] user-cr: add --output-fd to write output to a specific fd Oren Laadan
[not found] ` <1256508801-2426-1-git-send-email-orenl-RdfvBDnrOixBDgjK7y7TUQ@public.gmane.org>
2009-10-26 16:50 ` Serge E. Hallyn [this message]
2009-10-26 17:43 ` Matt Helsley
[not found] ` <20091026174305.GI31446-52DBMbEzqgQ/wnmkkaCWp/UQ3DHhIser@public.gmane.org>
2009-10-26 19:39 ` Oren Laadan
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20091026165054.GE23564@us.ibm.com \
--to=serue-r/jw6+rmf7hqt0dzr+alfa@public.gmane.org \
--cc=containers-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA@public.gmane.org \
--cc=orenl-RdfvBDnrOixBDgjK7y7TUQ@public.gmane.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox