[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[PATCH 6/6] Use readvars_parse_file in loader/init.c



---
 loader/init.c |  168 ++++++++++++++++++++-------------------------------------
 1 files changed, 58 insertions(+), 110 deletions(-)

diff --git a/loader/init.c b/loader/init.c
index aa1802e..a386631 100644
--- a/loader/init.c
+++ b/loader/init.c
@@ -55,6 +55,7 @@
 #include "devt.h"
 #include "devices.h"
 #include "modules.h"
+#include "readvars.h"
 
 #include <asm/types.h>
 #include <linux/serial.h>
@@ -107,11 +108,13 @@ char * env[] = {
 
 void shutDown(int doKill, reboot_action rebootAction);
 static int getKillPolicy(void);
-static void getSyslog(char *, char *);
+static gchar *getSyslog(gchar **);
 struct termios ts;
 
 static int expected_exit = 0;
 
+static GHashTable *cmdline = NULL;
+
 static void doExit(int) __attribute__ ((noreturn));
 static void doExit(int result)
 {
@@ -137,45 +140,48 @@ static void fatal_error(int usePerror) {
 static void startSyslog(void) {
     int conf_fd;
     int ret;
-    char addr[128];
-    char virtiolog[128];
+    gchar *addr = NULL, *virtiolog = NULL;
     const char *forward_tcp = "*.* @@";
     const char *forward_format_tcp = "\n";
     const char *forward_virtio = "*.* /dev/virtio-ports/";
     const char *forward_format_virtio = ";virtio_ForwardFormat\n";
-    
+
     /* update the config file with command line arguments first */
-    getSyslog(addr, virtiolog);
-    if (strlen(addr) > 0 || strlen(virtiolog) > 0) {
+    addr = getSyslog(&virtiolog);
+    if (addr != NULL) {
         conf_fd = open("/etc/rsyslog.conf", O_WRONLY|O_APPEND);
         if (conf_fd < 0) {
             printf("error opening /etc/rsyslog.conf: %d\n", errno);
             printf("syslog forwarding will not be enabled\n");
             sleep(5);
         } else {
-            if (strlen(addr) > 0) {
-                ret = write(conf_fd, forward_tcp, strlen(forward_tcp));
-                ret = write(conf_fd, addr, strlen(addr));
-                ret = write(conf_fd, forward_format_tcp, strlen(forward_format_tcp));
-            }
-            if (strlen(virtiolog) > 0) {
+            ret = write(conf_fd, forward_tcp, strlen(forward_tcp));
+            ret = write(conf_fd, addr, strlen(addr));
+            ret = write(conf_fd, forward_format_tcp, strlen(forward_format_tcp));
+
+            if (virtiolog != NULL) {
                 ret = write(conf_fd, forward_virtio, strlen(forward_virtio));
                 ret = write(conf_fd, virtiolog, strlen(virtiolog));
                 ret = write(conf_fd, forward_format_virtio, strlen(forward_format_virtio));
             }
+
             close(conf_fd);
         }
+
+        g_free(addr);
     }
-    
-    if (strlen(virtiolog)) {
+
+    if (virtiolog != NULL) {
         printf("Loading virtio_pci module... ");
         if (mlLoadModule("virtio_pci", NULL)) {
             printf("Error loading virtio_pci module. ");
             printf("Virtio logging will not work.\n.");
             sleep(5);
         } else {
-            printf("done.\n");            
+            printf("done.\n");
         }
+
+        g_free(virtiolog);
     }
 
     /* rsyslog is going to take care of things, so disable console logging */
@@ -193,8 +199,7 @@ static void startSyslog(void) {
 
 static int setupTerminal(int fd) {
     struct winsize winsize;
-    int fdn, len;
-    char buf[65535];
+    gpointer value = NULL;
 
     if (ioctl(fd, TIOCGWINSZ, &winsize)) {
         printf("failed to get winsize");
@@ -217,11 +222,8 @@ static int setupTerminal(int fd) {
         env[ENV_TERM] = "TERM=vt100-nav";
 
         /* unless the user specifies that they want utf8 */
-        if ((fdn = open("/proc/cmdline", O_RDONLY, 0)) != -1) {
-            len = read(fdn, buf, sizeof(buf) - 1);
-            close(fdn);
-            if (len > 0 && strstr(buf, "utf8"))
-                env[ENV_TERM] = "TERM=vt100";
+        if (g_hash_table_lookup_extended(cmdline, "utf8", NULL, value)) {
+            env[ENV_TERM] = "TERM=vt100";
         }
     }
 
@@ -318,79 +320,54 @@ static void sigUsr2Handler(int signum) {
 }
 
 static int getKillPolicy(void) {
-    int fd;
-    int len;
-    char buf[1024];
+    gpointer value = NULL;
 
     /* look through /proc/cmdline for special options */
-    if ((fd = open("/proc/cmdline", O_RDONLY,0)) > 0) {
-        len = read(fd, buf, sizeof(buf) - 1);
-        close(fd);
-        if ((len > 0) && strstr(buf, "nokill"))
-            return 0;
+    if (g_hash_table_lookup_extended(cmdline, "nokill", NULL, value)) {
+        return 0;
     }
+
     return 1;
 }
 
 /* Looks through /proc/cmdline for remote syslog paramters. */
-static void getSyslog(char *addr, char *virtiolog) {
-    int fd;
-    int len;
-    char buf[1024];
-
-    /* assume nothing gets found */
-    addr[0] = '\0';
-    virtiolog[0] = '\0';
-    if ((fd = open("/proc/cmdline", O_RDONLY,0)) <= 0) {
-        return;
-    }
-    len = read(fd, buf, sizeof(buf) - 1);
-    close(fd);
-    buf[len] = '\0';
-    
-    /* Parse the command line into argument vector using glib */
-    int i;
-    int argc;
-    char** argv;
-    GError* err = NULL;
-    if (!g_shell_parse_argv(buf, &argc, &argv, &err )) {
-        g_error_free(err);
-        return;
+static gchar *getSyslog(gchar **virtiolog) {
+    gpointer value = NULL;
+    gchar *addr = NULL;
+
+    if (!g_hash_table_lookup_extended(cmdline, "syslog", NULL, value)) {
+        return NULL;
+    } else {
+        addr = (gchar *) value;
     }
-    for (i = 0; i < argc; ++i) {
-        /* find what we are looking for */
-        if (!strncmp(argv[i], "syslog=", 7)) {
-            strncpy(addr, argv[i] + 7, 127);
-            addr[127] = '\0';
-            continue;
-        }
-        if (!strncmp(argv[i], "virtiolog=", 10)) {
-            strncpy(virtiolog, argv[i] + 10, 127);
-            virtiolog[127] = '\0';
-            continue;
+
+    if (!g_hash_table_lookup_extended(cmdline, "syslog", NULL, value)) {
+        *virtiolog = NULL;
+    } else {
+        *virtiolog = (gchar *) value;
+
+        /* virtiolog can only be letters and digits, dots, dashes and underscores */
+        if (!g_regex_match_simple("^[\\w.-_]*$", *virtiolog, 0, 0)) {
+            /* the parameter is malformed, disable its use */
+            *virtiolog = NULL;
+            printf("The virtiolog= command line parameter is malformed and will\n");
+            printf("be ignored by the installer.\n");
+            sleep(5);
         }
     }
-    g_strfreev(argv);
 
     /* address can be either a hostname or IPv4 or IPv6, with or without port;
        thus we only allow the following characters in the address: letters and
        digits, dots, colons, slashes, dashes and square brackets */
     if (!g_regex_match_simple("^[\\w.:/\\-\\[\\]]*$", addr, 0, 0)) {
         /* the parameter is malformed, disable its use */
-        addr[0] = '\0';
+        addr = NULL;
         printf("The syslog= command line parameter is malformed and will be\n");
         printf("ignored by the installer.\n");
         sleep(5);
     }
 
-    /* virtiolog can only be letters and digits, dots, dashes and underscores */
-    if (!g_regex_match_simple("^[\\w.-_]*$", addr, 0, 0)) {
-        /* the parameter is malformed, disable its use */
-        virtiolog[0] = '\0';
-        printf("The virtiolog= command line parameter is malformed and will\n");
-        printf("be ignored by the installer.\n");
-        sleep(5);
-    }
+    return addr;
 }
 
 static int getInitPid(void) {
@@ -470,16 +447,16 @@ int main(int argc, char **argv) {
     int doShutdown =0;
     reboot_action shutdown_method = HALT;
     int isSerial = 0;
-    int isDevelMode = 0;
+    gboolean isDevelMode = FALSE;
     char * console = NULL;
     int doKill = 1;
     char * argvc[15];
-    char buf[1024];
     char ** argvp = argvc;
     char twelve = 12;
     struct serial_struct si;
     int i, disable_keys;
     int ret;
+    gpointer value = NULL;
 
     if (!strncmp(basename(argv[0]), "poweroff", 8)) {
         printf("Running poweroff...\n");
@@ -519,41 +496,12 @@ int main(int argc, char **argv) {
         fatal_error(1);
     printf("done\n");
 
+    cmdline = readvars_parse_file("/proc/cmdline");
+
     /* check for development mode early */
-    int fdn;
-    if ((fdn = open("/proc/cmdline", O_RDONLY, 0)) != -1) {
-
-        /* get cmdline info */
-        int len = read(fdn, buf, sizeof(buf) - 1);
-        char *develstart;
-        close(fdn);
-
-        /* check the arguments */
-        if (len > 0) {
-            develstart = buf;
-            while (develstart && (*develstart) != '\0') {
-                
-                /* strip spaces */
-		while(*develstart == ' ') develstart++;
-		if(*develstart == '\0') break;
-                
-                /* not the word we are looking for */
-                if (strncmp(develstart, "devel", 5)) {
-                    develstart = strchr(develstart, ' ');
-                    continue;
-		}
-                
-                /* is it isolated? */
-                if(((*(develstart+5)) == ' ' || (*(develstart+5)) == '\0')) {
-                    printf("Enabling development mode - cores will be dumped\n");
-                    isDevelMode++;
-                    break;
-                }
-                
-                /* Find next argument */
-                develstart = strchr(develstart, ' ');
-            }
-        }
+    if (g_hash_table_lookup_extended(cmdline, "devel", NULL, value)) {
+        printf("Enabling development mode - cores will be dumped\n");
+        isDevelMode = TRUE;
     }
 
     /* these args are only for testing from commandline */
-- 
1.7.1.1


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]