external/koush/Superuser
Révision | b11c596208a8c09dc391240ddf4524cb4bf15ad5 (tree) |
---|---|
l'heure | 2018-02-01 23:52:34 |
Auteur | Chih-Wei Huang <cwhuang@linu...> |
Commiter | Chih-Wei Huang |
jni: fix log of su
Sync changes from upstream: git://github.com/koush/Superuser
@@ -299,12 +299,12 @@ static int daemon_accept(int fd) { | ||
299 | 299 | char *pts_slave = read_string(fd); |
300 | 300 | LOGD("remote pts_slave: %s", pts_slave); |
301 | 301 | daemon_from_uid = read_int(fd); |
302 | - LOGV("remote uid: %d", daemon_from_uid); | |
302 | + LOGD("remote uid: %d", daemon_from_uid); | |
303 | 303 | daemon_from_pid = read_int(fd); |
304 | - LOGV("remote req pid: %d", daemon_from_pid); | |
304 | + LOGD("remote req pid: %d", daemon_from_pid); | |
305 | 305 | |
306 | 306 | struct ucred credentials; |
307 | - int ucred_length = sizeof(struct ucred); | |
307 | + socklen_t ucred_length = sizeof(struct ucred); | |
308 | 308 | /* fill in the user data structure */ |
309 | 309 | if(getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &credentials, &ucred_length)) { |
310 | 310 | LOGE("could obtain credentials from unix domain socket"); |
@@ -334,7 +334,7 @@ static int daemon_accept(int fd) { | ||
334 | 334 | LOGE("unable to allocate args: %d", argc); |
335 | 335 | exit(-1); |
336 | 336 | } |
337 | - LOGV("remote args: %d", argc); | |
337 | + LOGD("remote args: %d", argc); | |
338 | 338 | char** argv = (char**)malloc(sizeof(char*) * (argc + 1)); |
339 | 339 | argv[argc] = NULL; |
340 | 340 | int i; |
@@ -416,9 +416,10 @@ static int daemon_accept(int fd) { | ||
416 | 416 | errfd = ptsfd; |
417 | 417 | } |
418 | 418 | } else { |
419 | - // TODO: Check system property, if PTYs are disabled, | |
420 | - // made infd the CTTY using: | |
421 | - // ioctl(infd, TIOCSCTTY, 1); | |
419 | + // If a TTY was sent directly, make it the CTTY. | |
420 | + if (isatty(infd)) { | |
421 | + ioctl(infd, TIOCSCTTY, 1); | |
422 | + } | |
422 | 423 | } |
423 | 424 | free(pts_slave); |
424 | 425 |
@@ -465,6 +466,9 @@ int run_daemon() { | ||
465 | 466 | int previous_umask = umask(027); |
466 | 467 | mkdir(REQUESTOR_DAEMON_PATH, 0777); |
467 | 468 | |
469 | + memset(sun.sun_path, 0, sizeof(sun.sun_path)); | |
470 | + memcpy(sun.sun_path, "\0" "SUPERUSER", strlen("SUPERUSER") + 1); | |
471 | + | |
468 | 472 | if (bind(fd, (struct sockaddr*)&sun, sizeof(sun)) < 0) { |
469 | 473 | PLOGE("daemon bind"); |
470 | 474 | goto err; |
@@ -572,23 +576,28 @@ int connect_daemon(int argc, char *argv[], int ppid) { | ||
572 | 576 | sun.sun_family = AF_LOCAL; |
573 | 577 | sprintf(sun.sun_path, "%s/server", REQUESTOR_DAEMON_PATH); |
574 | 578 | |
579 | + memset(sun.sun_path, 0, sizeof(sun.sun_path)); | |
580 | + memcpy(sun.sun_path, "\0" "SUPERUSER", strlen("SUPERUSER") + 1); | |
581 | + | |
575 | 582 | if (0 != connect(socketfd, (struct sockaddr*)&sun, sizeof(sun))) { |
576 | 583 | PLOGE("connect"); |
577 | 584 | exit(-1); |
578 | 585 | } |
579 | 586 | |
580 | - LOGV("connecting client %d", getpid()); | |
587 | + LOGD("connecting client %d", getpid()); | |
581 | 588 | |
582 | 589 | int mount_storage = getenv("MOUNT_EMULATED_STORAGE") != NULL; |
583 | 590 | |
584 | 591 | // Determine which one of our streams are attached to a TTY |
585 | 592 | int atty = 0; |
586 | 593 | |
587 | - // TODO: Check a system property and never use PTYs if | |
588 | - // the property is set. | |
589 | - if (isatty(STDIN_FILENO)) atty |= ATTY_IN; | |
590 | - if (isatty(STDOUT_FILENO)) atty |= ATTY_OUT; | |
591 | - if (isatty(STDERR_FILENO)) atty |= ATTY_ERR; | |
594 | + // Send TTYs directly (instead of proxying with a PTY) if | |
595 | + // the SUPERUSER_SEND_TTY environment variable is set. | |
596 | + if (getenv("SUPERUSER_SEND_TTY") == NULL) { | |
597 | + if (isatty(STDIN_FILENO)) atty |= ATTY_IN; | |
598 | + if (isatty(STDOUT_FILENO)) atty |= ATTY_OUT; | |
599 | + if (isatty(STDERR_FILENO)) atty |= ATTY_ERR; | |
600 | + } | |
592 | 601 | |
593 | 602 | if (atty) { |
594 | 603 | // We need a PTY. Get one. |
@@ -17,7 +17,6 @@ | ||
17 | 17 | |
18 | 18 | #include <sys/types.h> |
19 | 19 | #include <sys/socket.h> |
20 | -#include <sys/uio.h> | |
21 | 20 | #include <sys/un.h> |
22 | 21 | #include <sys/wait.h> |
23 | 22 | #include <sys/select.h> |
@@ -83,31 +82,48 @@ int fork_zero_fucks() { | ||
83 | 82 | } |
84 | 83 | } |
85 | 84 | |
86 | -void exec_log(int priority, const char* fmt, ...) { | |
87 | - static int log_fd = -1; | |
88 | - struct iovec vec[3]; | |
85 | +void exec_log(char *priority, char* logline) { | |
86 | + int pid; | |
87 | + if ((pid = fork()) == 0) { | |
88 | + int null = open("/dev/null", O_WRONLY | O_CLOEXEC); | |
89 | + dup2(null, STDIN_FILENO); | |
90 | + dup2(null, STDOUT_FILENO); | |
91 | + dup2(null, STDERR_FILENO); | |
92 | + execl("/system/bin/log", "/system/bin/log", "-p", priority, "-t", LOG_TAG, logline, NULL); | |
93 | + _exit(0); | |
94 | + } | |
95 | + int status; | |
96 | + waitpid(pid, &status, 0); | |
97 | +} | |
98 | + | |
99 | +void exec_loge(const char* fmt, ...) { | |
89 | 100 | va_list args; |
90 | - char msg[PATH_MAX]; | |
91 | 101 | |
92 | - if (log_fd < 0) { | |
93 | - log_fd = open("/dev/log/main", O_WRONLY); | |
94 | - if (log_fd < 0) { | |
95 | - return; | |
96 | - } | |
97 | - } | |
102 | + char logline[PATH_MAX]; | |
103 | + va_start(args, fmt); | |
104 | + vsnprintf(logline, PATH_MAX, fmt, args); | |
105 | + va_end(args); | |
106 | + exec_log("e", logline); | |
107 | +} | |
108 | + | |
109 | +void exec_logw(const char* fmt, ...) { | |
110 | + va_list args; | |
98 | 111 | |
112 | + char logline[PATH_MAX]; | |
99 | 113 | va_start(args, fmt); |
100 | - vsnprintf(msg, PATH_MAX, fmt, args); | |
114 | + vsnprintf(logline, PATH_MAX, fmt, args); | |
101 | 115 | va_end(args); |
116 | + exec_log("w", logline); | |
117 | +} | |
102 | 118 | |
103 | - vec[0].iov_base = (unsigned char *) &priority; | |
104 | - vec[0].iov_len = 1; | |
105 | - vec[1].iov_base = (void *) LOG_TAG; | |
106 | - vec[1].iov_len = strlen(LOG_TAG) + 1; | |
107 | - vec[2].iov_base = (void *) msg; | |
108 | - vec[2].iov_len = strlen(msg) + 1; | |
119 | +void exec_logd(const char* fmt, ...) { | |
120 | + va_list args; | |
109 | 121 | |
110 | - writev(log_fd, vec, 3); | |
122 | + char logline[PATH_MAX]; | |
123 | + va_start(args, fmt); | |
124 | + vsnprintf(logline, PATH_MAX, fmt, args); | |
125 | + va_end(args); | |
126 | + exec_log("d", logline); | |
111 | 127 | } |
112 | 128 | |
113 | 129 | static int from_init(struct su_initiator *from) { |
@@ -412,7 +428,7 @@ do { \ | ||
412 | 428 | static int socket_receive_result(int fd, char *result, ssize_t result_len) { |
413 | 429 | ssize_t len; |
414 | 430 | |
415 | - LOGV("waiting for user"); | |
431 | + LOGD("waiting for user"); | |
416 | 432 | len = read(fd, result, result_len-1); |
417 | 433 | if (len < 0) { |
418 | 434 | PLOGE("read(result)"); |
@@ -168,6 +168,10 @@ static inline char *get_command(const struct su_request *to) | ||
168 | 168 | return DEFAULT_SHELL; |
169 | 169 | } |
170 | 170 | |
171 | +void exec_loge(const char* fmt, ...); | |
172 | +void exec_logw(const char* fmt, ...); | |
173 | +void exec_logd(const char* fmt, ...); | |
174 | + | |
171 | 175 | int run_daemon(); |
172 | 176 | int connect_daemon(int argc, char *argv[], int ppid); |
173 | 177 | int su_main(int argc, char *argv[], int need_client); |
@@ -176,31 +180,16 @@ int su_main(int argc, char *argv[], int need_client); | ||
176 | 180 | // deadbeat dad fork. |
177 | 181 | int fork_zero_fucks(); |
178 | 182 | |
179 | -// can't use liblog.so because this is a static binary, so we need | |
180 | -// to implement this ourselves | |
181 | -#include <android/log.h> | |
182 | - | |
183 | -void exec_log(int priority, const char* fmt, ...); | |
184 | - | |
185 | -#ifndef LOG_NDEBUG | |
186 | -#define LOG_NDEBUG 1 | |
187 | -#endif | |
188 | - | |
183 | +// fallback to using /system/bin/log. | |
184 | +// can't use liblog.so because this is a static binary. | |
189 | 185 | #ifndef LOGE |
190 | -#define LOGE(fmt,args...) exec_log(ANDROID_LOG_ERROR, fmt, ##args) | |
191 | -#endif | |
192 | -#ifndef LOGW | |
193 | -#define LOGW(fmt,args...) exec_log(ANDROID_LOG_WARN, fmt, ##args) | |
186 | +#define LOGE exec_loge | |
194 | 187 | #endif |
195 | 188 | #ifndef LOGD |
196 | -#define LOGD(fmt,args...) exec_log(ANDROID_LOG_DEBUG, fmt, ##args) | |
197 | -#endif | |
198 | -#ifndef LOGV | |
199 | -#if LOG_NDEBUG | |
200 | -#define LOGV(...) ((void)0) | |
201 | -#else | |
202 | -#define LOGV(fmt,args...) exec_log(ANDROID_LOG_VERBOSE, fmt, ##args) | |
189 | +#define LOGD exec_logd | |
203 | 190 | #endif |
191 | +#ifndef LOGW | |
192 | +#define LOGW exec_logw | |
204 | 193 | #endif |
205 | 194 | |
206 | 195 | #if 0 |