aboutsummaryrefslogtreecommitdiff
path: root/ios-bootstrap/unrestrict.c
blob: b77bdf5b8bab363b95d0bfde7c46cddec8c69b81 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include "substitute.h"
#include "substitute-internal.h"
#include <stdlib.h>
#include <syslog.h>
#include <errno.h>
#include <stdio.h>

int main(int argc, char **argv) {
    char filename[128];
    sprintf(filename, "/tmp/wtf.%ld",
            (long) getpid());
    FILE *fp = fopen(filename, "w");
    if (!fp)
        return 1;
    #define syslog(a, b...) fprintf(fp, b)
    if (argc != 3) {
        syslog(LOG_EMERG, "unrestrict: wrong number of args");
        return 1;
    }
    const char *pids = argv[1];
    char *end;
    long pid = strtol(pids, &end, 10);
    if (!pids[0] || *end) {
        syslog(LOG_EMERG, "unrestrict: pid not an integer");
        return 1;
    }

    const char *should_resume = argv[2];
    if (strcmp(should_resume, "0") && strcmp(should_resume, "1")) {
        syslog(LOG_EMERG, "unrestrict: should_resume not 0 or 1");
        return 1;
    }

    /* double fork to avoid zombies */
    int ret = fork();
    if (ret == -1) {
        syslog(LOG_EMERG, "unrestrict: fork: %s", strerror(errno));
        return 1;
    } else if (ret) {
        return 0;
    }

    char *err = NULL;
    syslog(LOG_EMERG, "unrestrict: unrestricting %d (%s)", (int) pid, should_resume);
    int sret = substitute_ios_unrestrict((pid_t) pid, should_resume[0] == '1', &err);
    if (sret) {
        syslog(LOG_EMERG, "unrestrict: substitute_ios_unrestrict => %d (%s)",
               sret, err);
        return 1;
    }

    return 0;
}