如何在rootless环境下的launchdaemon执行bash脚本

之前被坑到了 忘记了bash目录不在根目录下 。。。 发个帖提醒一下
rootless 似乎不允许在设备不解锁的情况下启动要过AMFI的进程/。。

    extern char **environ;
    void run_cmd(char *incmd) {
    pid_t pid;
    
    NSString *run = [[NSString alloc] initWithUTF8String:incmd];
    run = [[NSString alloc] initWithFormat:@"'%@'", run];
    char *cmd = (char *)[run UTF8String];
    // aviod echo went wrong on rootless jb
    
    char *argv[] = {"bash", "-c", cmd, NULL, NULL};
    int status;
    
    NSString *cmdStr = [[NSString alloc] initWithUTF8String: cmd];
    NSLog(@"[Execute] bash -c %@", cmdStr);
    
    if (isRootless) {
        NSString *rtcmd = [[NSString alloc] initWithFormat: @"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/bin/X11:/usr/games:/var/containers/Bundle/iosbinpack64/usr/local/sbin:/var/containers/Bundle/iosbinpack64/usr/local/bin:/var/containers/Bundle/iosbinpack64/usr/sbin:/var/containers/Bundle/iosbinpack64/usr/bin:/var/containers/Bundle/iosbinpack64/sbin:/var/containers/Bundle/iosbinpack64/bin"];
        rtcmd = [rtcmd stringByAppendingString:@" "];
        rtcmd = [rtcmd stringByAppendingString: [[NSString alloc] initWithUTF8String:incmd]];
        char *rootlessARGS[] = {"bash", "-c", (char *)[rtcmd UTF8String], NULL, NULL};
        status = posix_spawn(&pid, "/var/containers/Bundle/tweaksupport/bin/bash", NULL, NULL, rootlessARGS, environ);
    } else {
        status = posix_spawn(&pid, "/bin/bash", NULL, NULL, argv, environ);
    }
    
    if (status == 0) {
        if (waitpid(pid, &status, 0) == -1) {
            perror("waitpid");
        }
    }
}
2 Likes