summaryrefslogtreecommitdiff
path: root/ldrestart.cpp
blob: 872ed68801579723ab82684292a50bca851bf33c (plain)
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/* Cydia - iPhone UIKit Front-End for Debian APT
 * Copyright (C) 2008-2015  Jay Freeman (saurik)
*/

/* GNU General Public License, Version 3 {{{ */
/*
 * Cydia is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published
 * by the Free Software Foundation, either version 3 of the License,
 * or (at your option) any later version.
 *
 * Cydia is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Cydia.  If not, see <http://www.gnu.org/licenses/>.
**/
/* }}} */

#include <cstdio>
#include <cstdlib>

#include <errno.h>
#include <signal.h>
#include <sysexits.h>
#include <unistd.h>

#include <launch.h>

#include <sys/stat.h>
/* Set platform binary flag */
#define FLAG_PLATFORMIZE (1 << 1)
#include <dlfcn.h>

const char *first[] = {
    NULL
};

const char *last[] = {
    "com.apple.assertiond",
    "com.apple.backboardd",
    NULL
};

const char *skip[] = {
    "jailbreakd",
    "science.xnu.substituted",
    "com.saurik.substrated",
    "com.apple.MobileFileIntegrity",
    "com.openssh.sshd.",
    "com.apple.SpringBoard",
    "com.apple.securityd",
    "com.apple.trustd",
    "com.apple.diagnosticd",
    NULL
};

void platformizeme() {
	void* handle = dlopen("/usr/lib/libjailbreak.dylib", RTLD_LAZY);
	if (!handle) return;

	// Reset errors
	dlerror();
	typedef void (*fix_entitle_prt_t)(pid_t pid, uint32_t what);
	fix_entitle_prt_t ptr = (fix_entitle_prt_t)dlsym(handle, "jb_oneshot_entitle_now");

	const char *dlsym_error = dlerror();
	if (dlsym_error) {
		return;
	}

	ptr(getpid(), FLAG_PLATFORMIZE);
}

bool launch_stop(const char * job)
{
    launch_data_t resp;
    launch_data_t msg;

    msg = launch_data_alloc(LAUNCH_DATA_DICTIONARY);
    if (msg == NULL) {
        fprintf(stderr, "out of memory");
        exit(-1);
    }

    launch_data_dict_insert(msg, launch_data_new_string(job),
            LAUNCH_KEY_STOPJOB);

    resp = launch_msg(msg);
    launch_data_free(msg);

    if (resp == NULL) {
        return false;
    }

    switch (launch_data_get_type(resp)) {
        case LAUNCH_DATA_ERRNO:
            errno = launch_data_get_errno(resp);
            launch_data_free(resp);
            if (errno != 0) {
                return false;
            }

            return true;

        default:
            launch_data_free(resp);
            return false;
    }
}

void process(launch_data_t value, const char *name, void *baton) {
    if (launch_data_get_type(value) != LAUNCH_DATA_DICTIONARY)
        return;

    auto integer(launch_data_dict_lookup(value, LAUNCH_JOBKEY_PID));
    if (integer == NULL || launch_data_get_type(integer) != LAUNCH_DATA_INTEGER)
        return;

    auto pid(launch_data_get_integer(integer));
    if (kill(pid, 0) == -1)
        return;

    auto string(launch_data_dict_lookup(value, LAUNCH_JOBKEY_LABEL));
    if (string == NULL || launch_data_get_type(string) != LAUNCH_DATA_STRING)
        return;
    auto label(launch_data_get_string(string));

    for (const char **skipped = first; *skipped != NULL; skipped++) {
        if (strcmp(label, *skipped) == 0)
            return;
    }
    for (const char **skipped = skip; *skipped != NULL; skipped++) {
        if (strncmp(label, *skipped, strlen(*skipped)) == 0)
            return;
    }
    for (const char **skipped = last; *skipped != NULL; skipped++) {
        if (strcmp(label, *skipped) == 0)
            return;
    }

    auto stop(launch_data_alloc(LAUNCH_DATA_DICTIONARY));
    launch_data_dict_insert(stop, string, LAUNCH_KEY_STOPJOB);

    auto result(launch_msg(stop));
    launch_data_free(stop);
    if (result == NULL)
        return;

    if (launch_data_get_type(result) != LAUNCH_DATA_ERRNO)
        fprintf(stderr, "%s\n", label);
    else if (auto number = launch_data_get_errno(result))
        fprintf(stderr, "%s: %s\n", label, strerror(number));

    launch_data_free(result);
}

int main(int argc, char *argv[]) {
    platformizeme();
    for (const char **service = first; *service != NULL; service++) {
        launch_stop(*service);
    }
    auto request(launch_data_new_string(LAUNCH_KEY_GETJOBS));
    auto response(launch_msg(request));
    launch_data_free(request);

    if (response == NULL)
        return EX_UNAVAILABLE;
    if (launch_data_get_type(response) != LAUNCH_DATA_DICTIONARY)
        return EX_SOFTWARE;

    launch_data_dict_iterate(response, &process, NULL);
    for (const char **service = last; *service != NULL; service++) {
        launch_stop(*service);
    }
    return EX_OK;
}