forked from rovo89/Xposed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xposed_logcat.cpp
193 lines (163 loc) · 5.5 KB
/
xposed_logcat.cpp
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
/**
* This file includes the Xposed service, which is especially used to work around SELinux restrictions.
*/
#define LOG_TAG "Xposed"
#include <cstring>
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/prctl.h>
#include <unistd.h>
#include "xposed.h"
#include "xposed_service.h"
#include "xposed_logcat.h"
namespace xposed {
namespace logcat {
////////////////////////////////////////////////////////////
// Declarations
////////////////////////////////////////////////////////////
#define AID_LOG 1007
#define CAP_SYSLOG 34
char marker[50];
////////////////////////////////////////////////////////////
// Functions
////////////////////////////////////////////////////////////
static void execLogcat() {
int8_t keep[] = { CAP_SYSLOG, -1 };
xposed::dropCapabilities(keep);
// Execute a logcat command that will keep running in the background
if (zygote_access(XPOSEDLOG_CONF_ALL, F_OK) == 0) {
execl("/system/bin/logcat", "logcat",
"-v", "time", // include timestamps in the log
(char*) 0);
} else {
execl("/system/bin/logcat", "logcat",
"-v", "time", // include timestamps in the log
"-s", // be silent by default, except for the following tags
"XposedStartupMarker:D", // marks the beginning of the current log
"Xposed:I", // Xposed framework and default logging
"appproc:I", // app_process
"XposedInstaller:I", // Xposed Installer
"art:F", // ART crashes
(char*) 0);
}
// We only get here in case of errors
ALOGE("Could not execute logcat: %s", strerror(errno));
exit(EXIT_FAILURE);
}
#ifndef dprintf
static inline int dprintf(int fd, const char *format, ...) {
char* message;
va_list args;
va_start(args, format);
int size = vasprintf(&message, format, args);
if (size > 0) {
write(fd, message, size);
free(message);
}
va_end(args);
return size;
}
#endif
static void runDaemon(int pipefd) {
xposed::setProcessName("xposed_logcat");
xposed::dropCapabilities();
umask(0);
int logfile = open(XPOSEDLOG, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
if (logfile < 0) {
ALOGE("Could not open %s: %s", XPOSEDLOG, strerror(errno));
exit(EXIT_FAILURE);
}
FILE* pipe = fdopen(pipefd, "r");
if (pipe == NULL) {
ALOGE("fdopen failed for pipe file descriptor %d: %s", pipefd, strerror(errno));
exit(EXIT_FAILURE);
}
char buf[512];
bool foundMarker = false;
long totalSize = 0;
while (fgets(buf, sizeof(buf), pipe) != NULL) {
if (buf[0] == '-')
continue; // beginning of <logbuffer type>
if (!foundMarker) {
if (strstr(buf, "XposedStartupMarker") != NULL && strstr(buf, marker) != NULL) {
foundMarker = true;
}
continue;
}
int len = strlen(buf);
write(logfile, buf, len);
totalSize += len;
if (totalSize > XPOSEDLOG_MAX_SIZE) {
dprintf(logfile, "\nReached maximum log size (%'d kB), further lines won't be logged.\n", XPOSEDLOG_MAX_SIZE / 1024);
exit(EXIT_FAILURE);
}
}
ALOGE("Broken pipe to logcat: %s", strerror(ferror(pipe)));
close(logfile);
exit(EXIT_FAILURE);
}
void printStartupMarker() {
sprintf(marker, "Current time: %d, PID: %d", (int) time(NULL), getpid());
ALOG(LOG_DEBUG, "XposedStartupMarker", marker, NULL);
}
void start() {
// Fork to create a daemon
pid_t pid;
if ((pid = fork()) < 0) {
ALOGE("Fork for Xposed logcat daemon failed: %s", strerror(errno));
return;
} else if (pid != 0) {
return;
}
// Ensure that we're allowed to read all log entries
if (prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0) < 0) {
ALOGE("Failed to keep capabilities: %s", strerror(errno));
}
const gid_t groups[] = { AID_LOG };
setgroups(1, groups);
if (!xposed::switchToXposedInstallerUidGid()) {
exit(EXIT_FAILURE);
}
#if XPOSED_WITH_SELINUX
if (xposed->isSELinuxEnabled) {
if (setcon(ctx_app) != 0) {
ALOGE("Could not switch to %s context", ctx_app);
exit(EXIT_FAILURE);
}
}
#endif // XPOSED_WITH_SELINUX
int err = rename(XPOSEDLOG, XPOSEDLOG_OLD);
if (err < 0 && errno != ENOENT) {
ALOGE("%s while renaming log file %s -> %s", strerror(errno), XPOSEDLOG, XPOSEDLOG_OLD);
exit(EXIT_FAILURE);
}
int pipeFds[2];
if (pipe(pipeFds) < 0) {
ALOGE("Could not allocate pipe for logcat output: %s", strerror(errno));
exit(EXIT_FAILURE);
}
fcntl(pipeFds[0], F_SETPIPE_SZ, 1048576);
if ((pid = fork()) < 0) {
ALOGE("Fork for logcat execution failed: %s", strerror(errno));
exit(EXIT_FAILURE);
} else if (pid == 0) {
close(pipeFds[0]);
if (dup2(pipeFds[1], STDOUT_FILENO) == -1) {
ALOGE("Could not redirect stdout: %s", strerror(errno));
exit(EXIT_FAILURE);
}
if (dup2(pipeFds[1], STDERR_FILENO) == -1) {
ALOGE("Could not redirect stdout: %s", strerror(errno));
exit(EXIT_FAILURE);
}
execLogcat();
} else {
close(pipeFds[1]);
runDaemon(pipeFds[0]);
}
// Should never reach this point
exit(EXIT_FAILURE);
}
} // namespace logcat
} // namespace xposed