* exit with code 1 instead of unusual -1 in the parent process
* exit using _exit() when child exec fails
* fix fork/exec error messages
* only uloop_run() after successful child fork (uloop_end() before
uloop_run() doesn't have any effect, so uloop_run() would hang forever)
* minor code cleanup
Signed-off-by: Matthias Schiffer <mschiffer@universe-factory.net>
static void sysupgrade(char *path, char *command)
{
static void sysupgrade(char *path, char *command)
{
- char *args[] = { "/lib/upgrade/stage2", NULL, NULL, NULL };
+ char *args[] = { "/lib/upgrade/stage2", path, command, NULL };
- args[1] = path;
- args[2] = command;
upgrade_proc.cb = upgrade_proc_cb;
upgrade_proc.pid = fork();
upgrade_proc.cb = upgrade_proc_cb;
upgrade_proc.pid = fork();
- if (!upgrade_proc.pid) {
- execvp(args[0], args);
+ if (upgrade_proc.pid < 0) {
fprintf(stderr, "Failed to fork sysupgrade\n");
fprintf(stderr, "Failed to fork sysupgrade\n");
- if (upgrade_proc.pid <= 0) {
- fprintf(stderr, "Failed to start sysupgrade\n");
- uloop_end();
+
+ if (!upgrade_proc.pid) {
+ /* Child */
+ execvp(args[0], args);
+ fprintf(stderr, "Failed to exec sysupgrade\n");
+ _exit(-1);
}
uloop_process_add(&upgrade_proc);
}
uloop_process_add(&upgrade_proc);
}
int main(int argc, char **argv)
}
int main(int argc, char **argv)
if (p != 1) {
fprintf(stderr, "this tool needs to run as pid 1\n");
if (p != 1) {
fprintf(stderr, "this tool needs to run as pid 1\n");
}
int fd = open("/", O_DIRECTORY|O_PATH);
if (fd < 0) {
fprintf(stderr, "unable to open prefix directory: %s\n", strerror(errno));
}
int fd = open("/", O_DIRECTORY|O_PATH);
if (fd < 0) {
fprintf(stderr, "unable to open prefix directory: %s\n", strerror(errno));
}
chroot(".");
if (fchdir(fd) == -1) {
fprintf(stderr, "failed to chdir to prefix directory: %s\n", strerror(errno));
}
chroot(".");
if (fchdir(fd) == -1) {
fprintf(stderr, "failed to chdir to prefix directory: %s\n", strerror(errno));
}
close(fd);
if (argc != 3) {
fprintf(stderr, "sysupgrade stage 2 failed, invalid command line\n");
}
close(fd);
if (argc != 3) {
fprintf(stderr, "sysupgrade stage 2 failed, invalid command line\n");
}
uloop_init();
watchdog_init(0);
sysupgrade(argv[1], argv[2]);
}
uloop_init();
watchdog_init(0);
sysupgrade(argv[1], argv[2]);