upgraded: register stage2 process in uloop as intended
[project/procd.git] / upgraded / upgraded.c
index 1e4057a..6bc5ad8 100644 (file)
  * GNU General Public License for more details.
  */
 
+#define _GNU_SOURCE
+
 #include <sys/reboot.h>
 
+#include <fcntl.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include <errno.h>
 
 #include <libubox/uloop.h>
 
 #include "../watchdog.h"
 
+#ifndef O_PATH
+#define O_PATH         010000000
+#endif
+
 static struct uloop_process upgrade_proc;
 unsigned int debug = 2;
 
@@ -33,11 +41,12 @@ static void upgrade_proc_cb(struct uloop_process *proc, int ret)
        uloop_end();
 }
 
-static void sysupgarde(char *folder)
+static void sysupgrade(char *path, char *command)
 {
-       char *args[] = { "/sbin/sysupgrade", "nand", NULL, NULL };
+       char *args[] = { "/lib/upgrade/stage2", NULL, NULL, NULL };
 
-       args[2] = folder;
+       args[1] = path;
+       args[2] = command;
        upgrade_proc.cb = upgrade_proc_cb;
        upgrade_proc.pid = fork();
        if (!upgrade_proc.pid) {
@@ -46,29 +55,44 @@ static void sysupgarde(char *folder)
                exit(-1);
        }
        if (upgrade_proc.pid <= 0) {
-               fprintf(stderr, "Failed to start sysupgarde\n");
+               fprintf(stderr, "Failed to start sysupgrade\n");
                uloop_end();
        }
+
+       uloop_process_add(&upgrade_proc);
 }
 
 int main(int argc, char **argv)
 {
        pid_t p = getpid();
 
-       chdir("/tmp");
-
        if (p != 1) {
                fprintf(stderr, "this tool needs to run as pid 1\n");
                return -1;
        }
-       if (argc != 2) {
-               fprintf(stderr, "sysupgrade stage 2 failed, no folder specified\n");
+
+       int fd = open("/", O_DIRECTORY|O_PATH);
+       if (fd < 0) {
+               fprintf(stderr, "unable to open prefix directory: %s\n", strerror(errno));
+               return -1;
+       }
+
+       chroot(".");
+
+       if (fchdir(fd) == -1) {
+               fprintf(stderr, "failed to chdir to prefix directory: %s\n", strerror(errno));
+               return -1;
+       }
+       close(fd);
+
+       if (argc != 3) {
+               fprintf(stderr, "sysupgrade stage 2 failed, invalid command line\n");
                return -1;
        }
 
        uloop_init();
        watchdog_init(0);
-       sysupgarde(argv[1]);
+       sysupgrade(argv[1], argv[2]);
        uloop_run();
 
        reboot(RB_AUTOBOOT);