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

mkinitrd [PATCH] Unify nash between bash and non-bash branches



[PATCH] Unify nash between bash and non-bash branches

This patch makes nash work for both the upstream branch (where nash runs the init script) or the bash branch, where bash runs the init script and calls nash for various tools it contains.

nash for upstream mkinitrd behaves exactly like the current nash.
nash for bash-branch must be called with "nash -c" before the desired command.

I am not exactly sure why Jeremy wrote the part within condCommand().

Please merge this, because it makes it easier for people (including me) to test the bash-branch without replacing the standard nash.

Warren Togami
wtogami redhat com
diff -urN mkinitrd-pjones/nash/lib.h mkinitrd/nash/lib.h
--- mkinitrd-pjones/nash/lib.h	2007-11-12 23:07:14.000000000 -0500
+++ mkinitrd/nash/lib.h	2007-11-30 16:20:45.000000000 -0500
@@ -40,6 +40,7 @@
 
 struct nashContext_s {
     nashLogger_t logger;
+    int runcommand;
     int testing;
     int quiet;
     int reallyquiet;
diff -urN mkinitrd-pjones/nash/nash.c mkinitrd/nash/nash.c
--- mkinitrd-pjones/nash/nash.c	2007-11-12 23:07:14.000000000 -0500
+++ mkinitrd/nash/nash.c	2007-11-30 20:04:23.000000000 -0500
@@ -121,6 +121,8 @@
 static pid_t ocPid = -1;
 static int exit_status = 0;
 
+static int runCommand(char *command, char *argstart, char *argend, int builtinOnly);
+
 static int
 searchPath(char *bin, char **resolved)
 {
@@ -1445,7 +1447,7 @@
     if (strncmp(buf, "S1SUSP", 6) && strncmp(buf, "S2SUSP", 6)) {
         qprintf("No suspend signature on swap, not resuming.\n");
         close(fd);
-        return 1;
+        return 0;
     }
 
     if (fstat(fd, &sb)) {
@@ -2521,6 +2523,10 @@
     if (!conditional && exit_status != 0)
         return exit_status;
 
+    if (_nash_context->runcommand) {
+        return runCommand(op, cmd, end, 0);
+    }
+
     if (strncmp(op, "nash-", 5)) {
         char *fullPath = NULL;
 
@@ -2764,23 +2770,27 @@
         i = 0;
         rc = 1;
         *(chptr++) = '\0';
-        if (strncmp(start, "nash-", 5))  {
-            char *fullPath = NULL;
-            rc = searchPath(start, &fullPath);
-            if (rc >= 0) {
-                rc = otherCommand(fullPath, chptr, end, 1, 0);
-                free(fullPath);
-            } else
-                i = 1;
+        if (_nash_context->runcommand) {
+            rc = runCommand(start, chptr, end, 0);
         } else {
-            start += 5;
-            i = 1;
-        }
+            if (strncmp(start, "nash-", 5))  {
+                char *fullPath = NULL;
+                rc = searchPath(start, &fullPath);
+                if (rc >= 0) {
+                    rc = otherCommand(fullPath, chptr, end, 1, 0);
+                    free(fullPath);
+                } else
+                    i = 1;
+            } else {
+                start += 5;
+                i = 1;
+            }
 
-        if (i == 1) {
-            handler = getCommandHandler(start);
-            if (handler->name != NULL)
-                rc = (handler->fp)(chptr, end);
+            if (i == 1) {
+                handler = getCommandHandler(start);
+                if (handler->name != NULL)
+                    rc = (handler->fp)(chptr, end);
+            }
         }
         exit_status = rc;
         start = end + 1;
@@ -2790,6 +2800,30 @@
     return rc;
 }
 
+static int runCommand(char *command, char *argstart, char *argend, 
+                      int builtinOnly) {
+    const struct commandHandler * handler;
+    int rc = -1;
+
+    if (strncmp(command, "nash-", 5) && !builtinOnly)  {
+        char *fullPath = NULL;
+        rc = searchPath(command, &fullPath);
+        if (rc >= 0) {
+            rc = otherCommand(fullPath, argstart, argend, 1, 0);
+            free(fullPath);
+            return rc;
+        }
+    } else if (!strncmp(command, "nash-", 5)) {
+        command += 5;
+    }
+
+    handler = getCommandHandler(command);
+    if (handler->name != NULL)
+        rc = (handler->fp)(argstart, argend);
+
+    return rc;
+}
+
 void delayOnSignal(int signum) {
     if (ocPid != -1)
         kill(ocPid, SIGSTOP);
@@ -2871,6 +2905,7 @@
         exit(0);
     }
 
+    _nash_context->runcommand = 0;
     _nash_context->testing = (getppid() != 0) && (getppid() != 1);
     argv++, argc--;
 
@@ -2890,6 +2925,9 @@
         } else if (!strcmp(*argv, "--reallyquiet")) {
             _nash_context->reallyquiet = 1;
             argv++, argc--;
+        } else if (!strcmp(*argv, "-c")) {
+            _nash_context->runcommand = 1;
+            argv++, argc--;
         } else {
             eprintf("unknown argument %s\n", *argv);
             nashFreeContext(_nash_context);
@@ -2908,11 +2946,29 @@
     }
 
     if (*argv) {
-        fd = open(*argv, O_RDONLY, 0);
-        if (fd < 0) {
-            eprintf("nash: cannot open %s: %m\n", *argv);
+        if (_nash_context->runcommand) {
+            /* FIXME: make this dynamically sized... */
+            char * buf = malloc(1024);
+            char * cmd = strdup(*argv);
+
+            argc--;argv++;
+            while (argc) {
+                buf = strcat(buf, *argv);
+                buf = strcat(buf, " ");
+                argc--;argv++;
+            }
+            rc = runCommand(cmd, buf, buf + strlen(buf), 1);
+
+            nashHotplugKill(_nash_context);
             nashFreeContext(_nash_context);
-            exit(1);
+            return rc;
+        } else {
+            fd = open(*argv, O_RDONLY, 0);
+            if (fd < 0) {
+                eprintf("nash: cannot open %s: %m\n", *argv);
+                nashFreeContext(_nash_context);
+                exit(1);
+            }
         }
     }
 

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