Hi,

This patch adds some command line options to s2disk/s2both and resume so that
they are a bit more script-friendly and sets up the infrastructure to add more
such options in the future easily.

It also cleans up the resume error paths.

Comments welcome.

Greetings,
Rafael


---
 config.c  |   47 ++++++++++++++----------------------
 config.h  |    9 ++----
 resume.c  |   80 +++++++++++++++++++++++++++++++++++++++++++++++++++-----------
 suspend.c |   60 ++++++++++++++++++++++++++++++++++++++++++++--
 4 files changed, 146 insertions(+), 50 deletions(-)

Index: suspend/config.c
===================================================================
--- suspend.orig/config.c       2006-11-05 20:55:21.000000000 +0100
+++ suspend/config.c    2006-11-05 21:32:54.000000000 +0100
@@ -16,6 +16,7 @@
 #include <stdio.h>
 #include <errno.h>
 #include <string.h>
+#include <getopt.h>
 
 #include "config.h"
 #include "encrypt.h"
@@ -24,17 +25,24 @@
  *     parse - read and parse the configuration file
  */
 
-static int parse(char *my_name, char *file_name, int parc, struct config_par 
*parv)
+int parse(char *my_name, char *file_name, int parc, struct config_par *parv)
 {
        char *str, *dst, *fmt, buf[MAX_STR_LEN];
+       struct stat stat_buf;
        FILE *file;
        int error, i, j, k;
 
+       if (stat(file_name, &stat_buf)) {
+               error = errno;
+               fprintf(stderr, "my_name: Could not stat configuration file\n");
+               return -error;
+       }
        file = fopen(file_name, "r");
        if (!file) {
+               error = errno;
                fprintf(stderr, "%s: Could not open configuration file\n",
                        my_name);
-               return -errno;
+               return -error;
        }
        error = 0;
        i = 0;
@@ -96,33 +104,16 @@ static int parse(char *my_name, char *fi
        return error;
 }
 
-int get_config(char *my_name, int argc, char *argv[],
-              int parc, struct config_par *parv, char *special)
+void usage(char *my_name, struct option *options)
 {
-       struct stat stat_buf;
-       int ret = 0;
-
-       if (argc <= 2) {
-               if (!stat(CONFIG_FILE, &stat_buf))
-                       ret = parse(my_name, CONFIG_FILE, parc, parv);
-               if (ret < 0 || argc < 2)
-                       return ret;
-               strncpy(special, argv[1], MAX_STR_LEN - 1);
-               return 0;
-       }
-
-       if (strncmp(argv[1], "-f", 2)) {
-               fprintf(stderr, "Usage: %s [-f config][resume_device]\n",
-                       my_name);
-               return -EINVAL;
-       }
-
-       ret = parse(my_name, argv[2], parc, parv);
-       if (ret)
-               return ret;
+       struct option *opt;
 
-       if (argc > 3)
-               strncpy(special, argv[3], MAX_STR_LEN - 1);
+       printf("Usage: %s ", my_name);
+       for (opt = options; opt->name; opt++)
+               if (opt->has_arg)
+                       printf("[-%c <%s>]", opt->val, opt->name);
+               else
+                       printf("[-%c]", opt->val);
 
-       return 0;
+       printf(" [<resume_device>]\n");
 }
Index: suspend/config.h
===================================================================
--- suspend.orig/config.h       2006-11-05 20:55:21.000000000 +0100
+++ suspend/config.h    2006-11-05 21:32:47.000000000 +0100
@@ -19,11 +19,8 @@ struct config_par {
        unsigned int len;
 };
 
-int get_config(char *my_name,
-               int argc,
-               char *argv[],
-               int parc,
-               struct config_par *parv,
-               char *special);
+int parse(char *my_name, char *file_name, int parc, struct config_par *parv);
+
+void usage(char *my_name, struct option options[]);
 
 #define CONFIG_FILE    "/etc/suspend.conf"
Index: suspend/resume.c
===================================================================
--- suspend.orig/resume.c       2006-11-05 20:55:21.000000000 +0100
+++ suspend/resume.c    2006-11-05 22:29:43.000000000 +0100
@@ -25,6 +25,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <getopt.h>
 #include <errno.h>
 #ifdef CONFIG_COMPRESS
 #include <lzf.h>
@@ -733,16 +734,67 @@ static void set_kernel_console_loglevel(
                umount("/proc");
 }
 
+/* Parse the command line and/or configuration file */
+static inline int get_config(int argc, char *argv[])
+{
+       static struct option options[] = {
+               { "help",               no_argument,            NULL, 'h'},
+               { "config",             required_argument,      NULL, 'f'},
+               { "resume_offset",      required_argument,      NULL, 'o'},
+               { NULL,                 0,                      NULL,  0 }
+       };
+       int i, error;
+       char *conf_name = CONFIG_FILE;
+       int set_off = 0;
+       unsigned long long int off = 0;
+
+       while ((i = getopt_long(argc, argv, "hf:o:", options, NULL)) != -1) {
+               switch (i) {
+               case 'h':
+                       usage("resume", options);
+                       exit(EXIT_SUCCESS);
+               case 'f':
+                       conf_name = optarg;
+                       break;
+               case 'o':
+                       off = atoll(optarg);
+                       set_off = 1;
+                       break;
+               default:
+                       usage("resume", options);
+                       return -EINVAL;
+               }
+       }
+       error = parse("resume", conf_name, PARAM_NO, parameters);
+       if (error) {
+               fprintf(stderr, "resume: Could not parse config file\n");
+               return error;
+       }
+       if (set_off)
+               resume_offset = off;
+
+       if (optind < argc)
+               strncpy(resume_dev_name, argv[optind], MAX_STR_LEN - 1);
+
+       return 0;
+}
+
 int main(int argc, char *argv[])
 {
        unsigned int mem_size;
        struct stat stat_buf;
        int dev;
-       int n, error = 0;
+       int n, error;
+
+       error = get_config(argc, argv);
+       if (error)
+               return -error;
+
+       if (splash_param != 'y' && splash_param != 'Y')
+               splash_param = 0;
 
        page_size = getpagesize();
        buffer_size = BUFFER_PAGES * page_size;
-
 #ifdef CONFIG_ENCRYPT
        printf("resume: libgcrypt version: %s\n", gcry_check_version(NULL));
        gcry_control(GCRYCTL_INIT_SECMEM, page_size, 0);
@@ -757,12 +809,6 @@ int main(int argc, char *argv[])
                return error;
        }
 
-       if (get_config("resume", argc, argv, PARAM_NO, parameters, 
resume_dev_name))
-               return EINVAL;
-
-       if (splash_param != 'y' && splash_param != 'Y')
-               splash_param = 0;
-
        while (stat(resume_dev_name, &stat_buf)) {
                fprintf(stderr, 
                        "resume: Could not stat the resume device file.\n"
@@ -770,8 +816,11 @@ int main(int argc, char *argv[])
                        "\tor press ENTER to boot the system: ");
                fgets(resume_dev_name, MAX_STR_LEN - 1, stdin);
                n = strlen(resume_dev_name) - 1;
-               if (n <= 0)
-                       return ENOENT;
+               if (n <= 0) {
+                       error = EINVAL;
+                       goto Free;
+               }
+
                if (resume_dev_name[n] == '\n')
                        resume_dev_name[n] = '\0';
        }
@@ -782,16 +831,19 @@ int main(int argc, char *argv[])
        setvbuf(stderr, NULL, _IONBF, 0);
 
        if (mlockall(MCL_CURRENT | MCL_FUTURE)) {
+               error = errno;
                fprintf(stderr, "resume: Could not lock myself\n");
-               return 1;
+               goto Free;
        }
 
        splash_prepare(&splash, splash_param);
        splash.progress(5);
 
        dev = open(snapshot_dev_name, O_WRONLY);
-       if (dev < 0)
-               return ENOENT;
+       if (dev < 0) {
+               error = ENOENT;
+               goto Free;
+       }
        error = read_image(dev, resume_dev_name);
        if (error) {
                fprintf(stderr, "resume: Could not read the image\n");
@@ -810,7 +862,7 @@ Close:
        close(dev);
 
        set_kernel_console_loglevel(max_loglevel);
-
+Free:
        free(mem_pool);
 
        return error;
Index: suspend/suspend.c
===================================================================
--- suspend.orig/suspend.c      2006-11-05 20:55:21.000000000 +0100
+++ suspend/suspend.c   2006-11-05 22:35:18.000000000 +0100
@@ -32,6 +32,7 @@
 #include <errno.h>
 #include <signal.h>
 #include <termios.h>
+#include <getopt.h>
 #ifdef CONFIG_COMPRESS
 #include <lzf.h>
 #else
@@ -1187,7 +1188,60 @@ static int lock_vt(void)
        return 0;
 }
 
+/* Parse the command line and/or configuration file */
+static inline int get_config(int argc, char *argv[])
+{
+       static struct option options[] = {
+               { "help",               no_argument,            NULL, 'h'},
+               { "config",             required_argument,      NULL, 'f'},
+               { "image_size",         required_argument,      NULL, 's'},
+               { "resume_offset",      required_argument,      NULL, 'o'},
+               { NULL,                 0,                      NULL,  0 }
+       };
+       int i, error;
+       char *conf_name = CONFIG_FILE;
+       int set_off = 0;
+       unsigned long long int off = 0;
+       int set_size = 0;
+       unsigned long int im_size = 0;
+
+       while ((i = getopt_long(argc, argv, "hf:s:o:", options, NULL)) != -1) {
+               switch (i) {
+               case 'h':
+                       usage("suspend", options);
+                       exit(0);
+               case 'f':
+                       conf_name = optarg;
+                       break;
+               case 's':
+                       im_size = atoll(optarg);
+                       set_size = 1;
+                       break;
+               case 'o':
+                       off = atoll(optarg);
+                       set_off = 1;
+                       break;
+               default:
+                       usage("suspend", options);
+                       return -EINVAL;
+               }
+       }
+       error = parse("suspend", conf_name, PARAM_NO, parameters);
+       if (error) {
+               fprintf(stderr, "suspend: Could not parse config file\n");
+               return error;
+       }
+       if (set_off)
+               resume_offset = off;
+
+       if (set_size)
+               pref_image_size = im_size;
 
+       if (optind < argc)
+               strncpy(resume_dev_name, argv[optind], MAX_STR_LEN - 1);
+
+       return 0;
+}
 
 int main(int argc, char *argv[])
 {
@@ -1212,8 +1266,10 @@ int main(int argc, char *argv[])
        } while (ret < 3);
        close(ret);
 
-       if (get_config("suspend", argc, argv, PARAM_NO, parameters, 
resume_dev_name))
-               return EINVAL;
+       ret = get_config(argc, argv);
+       if (ret)
+               return -ret;
+
        if (compute_checksum != 'y' && compute_checksum != 'Y')
                compute_checksum = 0;
 #ifdef CONFIG_COMPRESS

-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Suspend-devel mailing list
Suspend-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/suspend-devel

Reply via email to