user-cr: add --output-fd to write output to a specific fd
PATCH 1/1 user-cr: add --output-fd to write output to a specific fd
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.
--- checkpoint.c | 47 +++++++++++++++++++++++++++++++++++++++++------ 1 files changed, 41 insertions(+), 6 deletions(-)
checkpoint.c +41 -6
@@ -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);
Signature · 1 line
1.6.0.4
A message that is not in the archive (sent off-list or not yet received): 1256508801-2426-1-git-send-email-orenl-RdfvBDnrOixBDgjK7y7TUQ@public.gmane.org
Serge E. Hallyn
Acked-byQuoted 7 lines · Oren Laadan
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 <[email protected]>
Quoted 121 lines
--- checkpoint.c | 47 +++++++++++++++++++++++++++++++++++++++++------ 1 files changed, 41 insertions(+), 6 deletions(-)checkpoint.c +41
-6@@ -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);Signature · 6 lines
1.6.0.4 _______________________________________________ Containers mailing list Containers-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA@public.gmane.org https://lists.linux-foundation.org/mailman/listinfo/containers
Quoted 35 lines · Oren Laadan
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 <[email protected]>--- checkpoint.c | 47 +++++++++++++++++++++++++++++++++++++++++------ 1 files changed, 41 insertions(+), 6 deletions(-)checkpoint.c +3
-0@@ -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 */
checkpoint.c static void usage(char *str) · line 68
Quoted 10 lines
checkpoint.c +10
-0+static int str2num (char *str) +{ +char *nptr; +int num; + + num =strtol (str, &nptr,10 ); +if (nptr - str !=strlen (str)) + num =-1 ; +return num; +}
It'd be nice to see common functions in a shared .o between checkpoint and restart.
Cheers,
-Matt Helsley
A message that is not in the archive (sent off-list or not yet received): 20091026174305.GI31446-52DBMbEzqgQ/wnmkkaCWp/[email protected]
yeah .. I was lazy .. will add another patch.
Oren.
Quoted 53 lines · Matt Helsley
Quoted 35 lines · Oren Laadan
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 <[email protected]>--- checkpoint.c | 47 +++++++++++++++++++++++++++++++++++++++++------ 1 files changed, 41 insertions(+), 6 deletions(-)checkpoint.c +3
-0@@ -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 */ checkpoint.c static void usage(char *str) · line 68
Quoted 10 lines
checkpoint.c +10
-0+static int str2num (char *str) +{ +char *nptr; +int num; + + num =strtol (str, &nptr,10 ); +if (nptr - str !=strlen (str)) + num =-1 ; +return num; +}It'd be nice to see common functions in a shared .o between checkpoint and restart.
Cheers,
-Matt Helsley