]> git.saurik.com Git - uikittools.git/blob - ldrestart.cpp
Add ldrestart for safe restart from app jailbreak.
[uikittools.git] / ldrestart.cpp
1 /* Cydia - iPhone UIKit Front-End for Debian APT
2 * Copyright (C) 2008-2015 Jay Freeman (saurik)
3 */
4
5 /* GNU General Public License, Version 3 {{{ */
6 /*
7 * Cydia is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published
9 * by the Free Software Foundation, either version 3 of the License,
10 * or (at your option) any later version.
11 *
12 * Cydia is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with Cydia. If not, see <http://www.gnu.org/licenses/>.
19 **/
20 /* }}} */
21
22 #include <cstdio>
23 #include <cstdlib>
24
25 #include <errno.h>
26 #include <signal.h>
27 #include <sysexits.h>
28 #include <unistd.h>
29
30 #include <launch.h>
31
32 #include <sys/stat.h>
33
34 void process(launch_data_t value, const char *name, void *baton) {
35 if (launch_data_get_type(value) != LAUNCH_DATA_DICTIONARY)
36 return;
37
38 auto integer(launch_data_dict_lookup(value, LAUNCH_JOBKEY_PID));
39 if (integer == NULL || launch_data_get_type(integer) != LAUNCH_DATA_INTEGER)
40 return;
41
42 auto pid(launch_data_get_integer(integer));
43 if (kill(pid, 0) == -1)
44 return;
45
46 auto string(launch_data_dict_lookup(value, LAUNCH_JOBKEY_LABEL));
47 if (string == NULL || launch_data_get_type(string) != LAUNCH_DATA_STRING)
48 return;
49 auto label(launch_data_get_string(string));
50
51 auto stop(launch_data_alloc(LAUNCH_DATA_DICTIONARY));
52 launch_data_dict_insert(stop, string, LAUNCH_KEY_STOPJOB);
53
54 auto result(launch_msg(stop));
55 launch_data_free(stop);
56 if (result == NULL)
57 return;
58
59 if (launch_data_get_type(result) != LAUNCH_DATA_ERRNO)
60 fprintf(stderr, "%s\n", label);
61 else if (auto number = launch_data_get_errno(result))
62 fprintf(stderr, "%s: %s\n", label, strerror(number));
63
64 launch_data_free(result);
65 }
66
67 int main(int argc, char *argv[]) {
68 auto request(launch_data_new_string(LAUNCH_KEY_GETJOBS));
69 auto response(launch_msg(request));
70 launch_data_free(request);
71
72 if (response == NULL)
73 return EX_UNAVAILABLE;
74 if (launch_data_get_type(response) != LAUNCH_DATA_DICTIONARY)
75 return EX_SOFTWARE;
76
77 launch_data_dict_iterate(response, &process, NULL);
78 return EX_OK;
79 }