-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathdump_runner.cpp
More file actions
222 lines (184 loc) · 4.36 KB
/
dump_runner.cpp
File metadata and controls
222 lines (184 loc) · 4.36 KB
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/_iovec.h>
#include <sys/mount.h>
#include <sys/stat.h>
#include <sys/sysctl.h>
#include <sys/user.h>
#include <sys/time.h>
#define IOVEC_ENTRY(x) {x ? (char *)x : 0, x ? strlen(x) + 1 : 0}
#define IOVEC_SIZE(x) (sizeof(x) / sizeof(struct iovec))
#ifndef KSTUFF_DEFAULT
#define KSTUFF_DEFAULT 0
#endif
typedef struct app_info {
uint32_t app_id;
uint64_t unknown1;
char title_id[14];
char unknown2[0x3c];
} app_info_t;
typedef struct app_launch_ctx
{
uint32_t structsize;
uint32_t user_id;
uint32_t app_opt;
uint64_t crash_report;
uint32_t check_flag;
} app_launch_ctx_t;
extern "C"
{
int sceUserServiceInitialize(void *);
int sceUserServiceGetForegroundUser(uint32_t *);
void sceUserServiceTerminate(void);
int sceSystemServiceLaunchApp(const char *, char **, app_launch_ctx_t *);
int sceKernelGetAppInfo(pid_t, app_info_t*);
}
int remount_system_ex(void)
{
struct iovec iov[] = {
IOVEC_ENTRY("from"),
IOVEC_ENTRY("/dev/ssd0.system_ex"),
IOVEC_ENTRY("fspath"),
IOVEC_ENTRY("/system_ex"),
IOVEC_ENTRY("fstype"),
IOVEC_ENTRY("exfatfs"),
IOVEC_ENTRY("large"),
IOVEC_ENTRY("yes"),
IOVEC_ENTRY("timezone"),
IOVEC_ENTRY("static"),
IOVEC_ENTRY("async"),
IOVEC_ENTRY(NULL),
IOVEC_ENTRY("ignoreacl"),
IOVEC_ENTRY(NULL),
};
return nmount(iov, IOVEC_SIZE(iov), MNT_UPDATE);
}
int mount_nullfs(const char *src, const char *dst)
{
struct iovec iov[] = {
IOVEC_ENTRY("fstype"),
IOVEC_ENTRY("nullfs"),
IOVEC_ENTRY("from"),
IOVEC_ENTRY(src),
IOVEC_ENTRY("fspath"),
IOVEC_ENTRY(dst),
};
return nmount(iov, IOVEC_SIZE(iov), 0);
}
int endswith(const char *string, const char *suffix)
{
size_t suffix_len = strlen(suffix);
size_t string_len = strlen(string);
if (string_len < suffix_len)
{
return 0;
}
return strncmp(string + string_len - suffix_len, suffix, suffix_len) != 0;
}
pid_t find_pid(const char* title_id)
{
int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PROC, 0};
app_info_t appinfo;
size_t buf_size;
char *buf;
pid_t pid;
if (sysctl(mib, 4, NULL, &buf_size, NULL, 0))
{
return -1;
}
buf = (char*) malloc(buf_size);
if (!buf)
{
return -1;
}
if (sysctl(mib, 4, buf, &buf_size, NULL, 0))
{
free(buf);
return -1;
}
pid = -1;
for (char *ptr = buf; ptr < (buf+buf_size);)
{
struct kinfo_proc *ki = (struct kinfo_proc*) ptr;
ptr += ki->ki_structsize;
if (sceKernelGetAppInfo(ki->ki_pid, &appinfo))
{
continue;
}
if (!strcmp(title_id, appinfo.title_id))
{
pid = ki->ki_pid;
break;
}
}
free(buf);
return pid;
}
int kstuff_toggle(int enable);
int main(int argc, char *argv[])
{
app_launch_ctx_t ctx = {.structsize = sizeof(app_launch_ctx_t)};
char src[PATH_MAX + 1];
char dst[PATH_MAX + 1];
const char *title_id;
pid_t pid;
int kstuff = KSTUFF_DEFAULT;
int counter = 0;
int kq;
struct kevent evt;
int nev = -1;
int arg_shift = 0;
if (argc < 2)
{
printf("Usage: %s TITLE_ID\n", argv[0]);
return -1;
}
if (argc >= 3)
{
size_t len = strlen("kstuff-toggle=");
if (strncmp(argv[2], "kstuff-toggle=", len) == 0) {
kstuff = atoi(argv[2] + len);
arg_shift = 1;
}
}
title_id = argv[1];
if (find_pid(title_id) != -1) {
printf("%s is already running", title_id);
return 0;
}
getcwd(src, PATH_MAX);
strcpy(dst, "/system_ex/app/");
strcat(dst, title_id);
sceUserServiceInitialize(0);
sceUserServiceGetForegroundUser(&ctx.user_id);
if (access(dst, F_OK) != 0)
{
remount_system_ex();
mkdir(dst, 0755);
}
mount_nullfs(src, dst);
sceSystemServiceLaunchApp(title_id, &argv[2 + arg_shift], &ctx);
pid = find_pid(title_id);
if (pid != -1 && (kq = kqueue()) != -1) {
EV_SET(&evt, pid, EVFILT_PROC, EV_ADD, NOTE_EXIT, 0, NULL);
if (kevent(kq, &evt, 1, NULL, 0, NULL) != -1) {
struct timespec tout = {1, 0};
while((nev = kevent(kq, NULL, 0, &evt, 1, &tout)) == 0) {
if(kstuff > 0 && counter >= 0 && ++counter == kstuff) {
kstuff_toggle(0);
counter = -1;
}
}
if(kstuff > 0) {
kstuff_toggle(1);
}
if (nev > 0) {
sleep(3);
}
}
}
return unmount(dst, 0);
}