Use one generic routine to access /proc/cmdline
authorMichel Stam <m.stam@fugro.nl>
Mon, 13 Oct 2014 14:14:34 +0000 (16:14 +0200)
committerJohn Crispin <blogic@openwrt.org>
Sun, 12 Oct 2014 11:24:11 +0000 (13:24 +0200)
Signed-off-by: Michel Stam <m.stam@fugro.nl>
CMakeLists.txt
initd/init.c
inittab.c
utils/utils.c
utils/utils.h

index dc7ecd6..a8a7517 100644 (file)
@@ -30,7 +30,8 @@ INSTALL(TARGETS procd
 )
 
 
 )
 
 
-ADD_EXECUTABLE(init initd/init.c initd/early.c initd/preinit.c initd/mkdev.c watchdog.c)
+ADD_EXECUTABLE(init initd/init.c initd/early.c initd/preinit.c initd/mkdev.c watchdog.c
+       utils/utils.c)
 TARGET_LINK_LIBRARIES(init ${LIBS})
 INSTALL(TARGETS init
        RUNTIME DESTINATION sbin
 TARGET_LINK_LIBRARIES(init ${LIBS})
 INSTALL(TARGETS init
        RUNTIME DESTINATION sbin
index 153b3c2..d8490f8 100644 (file)
@@ -20,6 +20,7 @@
 #include <libubox/uloop.h>
 #include <libubus.h>
 
 #include <libubox/uloop.h>
 #include <libubus.h>
 
+#include <limits.h>
 #include <stdlib.h>
 #include <fcntl.h>
 #include <getopt.h>
 #include <stdlib.h>
 #include <fcntl.h>
 #include <getopt.h>
@@ -28,6 +29,7 @@
 #include <unistd.h>
 #include <stdio.h>
 
 #include <unistd.h>
 #include <stdio.h>
 
+#include "../utils/utils.h"
 #include "init.h"
 #include "../watchdog.h"
 
 #include "init.h"
 #include "../watchdog.h"
 
@@ -53,24 +55,16 @@ static struct sigaction sa_shutdown = {
 static void
 cmdline(void)
 {
 static void
 cmdline(void)
 {
-       char line[1024];
-       int r, fd = open("/proc/cmdline", O_RDONLY);
-       regex_t pat_cmdline;
-       regmatch_t matches[2];
-
-       if (fd < 0)
-               return;
-
-       r = read(fd, line, sizeof(line) - 1);
-       line[r] = '\0';
-       close(fd);
-
-       regcomp(&pat_cmdline, "init_debug=([0-9]+)", REG_EXTENDED);
-       if (!regexec(&pat_cmdline, line, 2, matches, 0)) {
-               line[matches[1].rm_eo] = '\0';
-               debug = atoi(&line[matches[1].rm_so]);
+       char line[20];
+       char* res;
+       long    r;
+
+       res = get_cmdline_val("init_debug", line, sizeof(line));
+       if (res != NULL) {
+               r = strtol(line, NULL, 10);
+               if ((r != LONG_MIN) && (r != LONG_MAX))
+                       debug = (int) r;
        }
        }
-       regfree(&pat_cmdline);
 }
 
 int
 }
 
 int
index c8540b1..2d9a1e0 100644 (file)
--- a/inittab.c
+++ b/inittab.c
@@ -25,6 +25,7 @@
 #include <libubox/utils.h>
 #include <libubox/list.h>
 
 #include <libubox/utils.h>
 #include <libubox/list.h>
 
+#include "utils/utils.h"
 #include "procd.h"
 #include "rcS.h"
 
 #include "procd.h"
 #include "rcS.h"
 
@@ -135,30 +136,20 @@ static void askfirst(struct init_action *a)
 static void askconsole(struct init_action *a)
 {
        struct stat s;
 static void askconsole(struct init_action *a)
 {
        struct stat s;
-       char line[256], *tty;
-       int i, r, fd = open("/proc/cmdline", O_RDONLY);
-       regex_t pat_cmdline;
-       regmatch_t matches[2];
-
-       if (fd < 0)
-               return;
-
-       r = read(fd, line, sizeof(line) - 1);
-       line[r] = '\0';
-       close(fd);
-
-       regcomp(&pat_cmdline, "console=([a-zA-Z0-9]*)", REG_EXTENDED);
-       if (regexec(&pat_cmdline, line, 2, matches, 0))
-               goto err_out;
-       line[matches[1].rm_eo] = '\0';
-       tty = &line[matches[1].rm_so];
+       char line[256], *tty, *split;
+       int i;
 
 
+       tty = get_cmdline_val("console", line, sizeof(line));
+       split=strchr(tty, ',');
+       if (split != NULL)
+               split = '\0';
+       
        chdir("/dev");
        i = stat(tty, &s);
        chdir("/");
        if (i) {
                DEBUG(4, "skipping %s\n", tty);
        chdir("/dev");
        i = stat(tty, &s);
        chdir("/");
        if (i) {
                DEBUG(4, "skipping %s\n", tty);
-               goto err_out;
+               return;
        }
        console = strdup(tty);
 
        }
        console = strdup(tty);
 
@@ -171,9 +162,6 @@ static void askconsole(struct init_action *a)
 
        a->proc.cb = child_exit;
        fork_worker(a);
 
        a->proc.cb = child_exit;
        fork_worker(a);
-
-err_out:
-       regfree(&pat_cmdline);
 }
 
 static void rcrespawn(struct init_action *a)
 }
 
 static void rcrespawn(struct init_action *a)
index 59d02f1..5e67310 100644 (file)
 #include <libubox/avl.h>
 #include <libubox/avl-cmp.h>
 #include "utils.h"
 #include <libubox/avl.h>
 #include <libubox/avl-cmp.h>
 #include "utils.h"
+#include <asm-generic/setup.h>
+#include <regex.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
 
 void
 __blobmsg_list_init(struct blobmsg_list *list, int offset, int len, blobmsg_list_cmp cmp)
 
 void
 __blobmsg_list_init(struct blobmsg_list *list, int offset, int len, blobmsg_list_cmp cmp)
@@ -120,3 +126,39 @@ blobmsg_list_equal(struct blobmsg_list *l1, struct blobmsg_list *l2)
 
        return true;
 }
 
        return true;
 }
+
+char* get_cmdline_val(const char* name, char* out, int len)
+{
+       char pattern[COMMAND_LINE_SIZE + 1];
+       char line[COMMAND_LINE_SIZE + 1];
+       char *res = NULL, *tty;
+       int r, fd;
+       regex_t pat_cmdline;
+       regmatch_t matches[2];
+
+       fd = open("/proc/cmdline", O_RDONLY);
+       if (fd < 0)
+               return NULL;
+
+       r = read(fd, line, COMMAND_LINE_SIZE);
+       if ( r <= 0 ) {
+               close(fd);
+               return NULL;
+       }
+       line[r] = '\0';
+       close(fd);
+
+       sprintf( pattern, "%s=([^ \n]*)", name);
+    regcomp(&pat_cmdline, pattern, REG_EXTENDED);
+    if (!regexec(&pat_cmdline, line, 2, matches, 0)) {
+       line[matches[1].rm_eo] = '\0';
+               tty = (line + matches[1].rm_so);
+               strncpy(out, tty, len);
+               tty[len-1] = '\0';
+               res = out;
+       }
+
+    regfree(&pat_cmdline);
+
+       return res;
+}
index 37fa216..c2c1cb4 100644 (file)
@@ -49,5 +49,6 @@ int blobmsg_list_fill(struct blobmsg_list *list, void *data, int len, bool array
 void blobmsg_list_free(struct blobmsg_list *list);
 bool blobmsg_list_equal(struct blobmsg_list *l1, struct blobmsg_list *l2);
 void blobmsg_list_move(struct blobmsg_list *list, struct blobmsg_list *src);
 void blobmsg_list_free(struct blobmsg_list *list);
 bool blobmsg_list_equal(struct blobmsg_list *l1, struct blobmsg_list *l2);
 void blobmsg_list_move(struct blobmsg_list *list, struct blobmsg_list *src);
+char* get_cmdline_val(const char* name, char* out, int len);
 
 #endif
 
 #endif