Android-x86
Fork
Donation

  • R/O
  • HTTP
  • SSH
  • HTTPS

external-koush-Superuser: Commit

external/koush/Superuser


Commit MetaInfo

Revisionb11c596208a8c09dc391240ddf4524cb4bf15ad5 (tree)
Time2018-02-01 23:52:34
AuthorChih-Wei Huang <cwhuang@linu...>
CommiterChih-Wei Huang

Log Message

jni: fix log of su

Sync changes from upstream: git://github.com/koush/Superuser

Change Summary

Incremental Difference

--- a/Superuser/jni/su/daemon.c
+++ b/Superuser/jni/su/daemon.c
@@ -299,12 +299,12 @@ static int daemon_accept(int fd) {
299299 char *pts_slave = read_string(fd);
300300 LOGD("remote pts_slave: %s", pts_slave);
301301 daemon_from_uid = read_int(fd);
302- LOGV("remote uid: %d", daemon_from_uid);
302+ LOGD("remote uid: %d", daemon_from_uid);
303303 daemon_from_pid = read_int(fd);
304- LOGV("remote req pid: %d", daemon_from_pid);
304+ LOGD("remote req pid: %d", daemon_from_pid);
305305
306306 struct ucred credentials;
307- int ucred_length = sizeof(struct ucred);
307+ socklen_t ucred_length = sizeof(struct ucred);
308308 /* fill in the user data structure */
309309 if(getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &credentials, &ucred_length)) {
310310 LOGE("could obtain credentials from unix domain socket");
@@ -334,7 +334,7 @@ static int daemon_accept(int fd) {
334334 LOGE("unable to allocate args: %d", argc);
335335 exit(-1);
336336 }
337- LOGV("remote args: %d", argc);
337+ LOGD("remote args: %d", argc);
338338 char** argv = (char**)malloc(sizeof(char*) * (argc + 1));
339339 argv[argc] = NULL;
340340 int i;
@@ -416,9 +416,10 @@ static int daemon_accept(int fd) {
416416 errfd = ptsfd;
417417 }
418418 } 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+ }
422423 }
423424 free(pts_slave);
424425
@@ -465,6 +466,9 @@ int run_daemon() {
465466 int previous_umask = umask(027);
466467 mkdir(REQUESTOR_DAEMON_PATH, 0777);
467468
469+ memset(sun.sun_path, 0, sizeof(sun.sun_path));
470+ memcpy(sun.sun_path, "\0" "SUPERUSER", strlen("SUPERUSER") + 1);
471+
468472 if (bind(fd, (struct sockaddr*)&sun, sizeof(sun)) < 0) {
469473 PLOGE("daemon bind");
470474 goto err;
@@ -572,23 +576,28 @@ int connect_daemon(int argc, char *argv[], int ppid) {
572576 sun.sun_family = AF_LOCAL;
573577 sprintf(sun.sun_path, "%s/server", REQUESTOR_DAEMON_PATH);
574578
579+ memset(sun.sun_path, 0, sizeof(sun.sun_path));
580+ memcpy(sun.sun_path, "\0" "SUPERUSER", strlen("SUPERUSER") + 1);
581+
575582 if (0 != connect(socketfd, (struct sockaddr*)&sun, sizeof(sun))) {
576583 PLOGE("connect");
577584 exit(-1);
578585 }
579586
580- LOGV("connecting client %d", getpid());
587+ LOGD("connecting client %d", getpid());
581588
582589 int mount_storage = getenv("MOUNT_EMULATED_STORAGE") != NULL;
583590
584591 // Determine which one of our streams are attached to a TTY
585592 int atty = 0;
586593
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+ }
592601
593602 if (atty) {
594603 // We need a PTY. Get one.
--- a/Superuser/jni/su/su.c
+++ b/Superuser/jni/su/su.c
@@ -17,7 +17,6 @@
1717
1818 #include <sys/types.h>
1919 #include <sys/socket.h>
20-#include <sys/uio.h>
2120 #include <sys/un.h>
2221 #include <sys/wait.h>
2322 #include <sys/select.h>
@@ -83,31 +82,48 @@ int fork_zero_fucks() {
8382 }
8483 }
8584
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, ...) {
89100 va_list args;
90- char msg[PATH_MAX];
91101
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;
98111
112+ char logline[PATH_MAX];
99113 va_start(args, fmt);
100- vsnprintf(msg, PATH_MAX, fmt, args);
114+ vsnprintf(logline, PATH_MAX, fmt, args);
101115 va_end(args);
116+ exec_log("w", logline);
117+}
102118
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;
109121
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);
111127 }
112128
113129 static int from_init(struct su_initiator *from) {
@@ -412,7 +428,7 @@ do { \
412428 static int socket_receive_result(int fd, char *result, ssize_t result_len) {
413429 ssize_t len;
414430
415- LOGV("waiting for user");
431+ LOGD("waiting for user");
416432 len = read(fd, result, result_len-1);
417433 if (len < 0) {
418434 PLOGE("read(result)");
--- a/Superuser/jni/su/su.h
+++ b/Superuser/jni/su/su.h
@@ -168,6 +168,10 @@ static inline char *get_command(const struct su_request *to)
168168 return DEFAULT_SHELL;
169169 }
170170
171+void exec_loge(const char* fmt, ...);
172+void exec_logw(const char* fmt, ...);
173+void exec_logd(const char* fmt, ...);
174+
171175 int run_daemon();
172176 int connect_daemon(int argc, char *argv[], int ppid);
173177 int su_main(int argc, char *argv[], int need_client);
@@ -176,31 +180,16 @@ int su_main(int argc, char *argv[], int need_client);
176180 // deadbeat dad fork.
177181 int fork_zero_fucks();
178182
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.
189185 #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
194187 #endif
195188 #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
203190 #endif
191+#ifndef LOGW
192+#define LOGW exec_logw
204193 #endif
205194
206195 #if 0
Show on old repository browser