Skip to content

Commit

Permalink
Improve logging code.
Browse files Browse the repository at this point in the history
- Write to log buffer ourselves instead of calling log, so the printed
  pid is correct
- Reduce debug spam

Change-Id: I9afe7249fc589268ea61419a7cfc724897981e9e
  • Loading branch information
maniac103 committed Nov 22, 2013
1 parent e48e124 commit ed55b0e
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 56 deletions.
20 changes: 10 additions & 10 deletions Superuser/jni/su/daemon.c
Original file line number Diff line number Diff line change
Expand Up @@ -192,13 +192,13 @@ static void pump_async(int input, int output) {
static int daemon_accept(int fd) {
is_daemon = 1;
int pid = read_int(fd);
LOGD("remote pid: %d", pid);
LOGV("remote pid: %d", pid);
int atty = read_int(fd);
LOGD("remote atty: %d", atty);
LOGV("remote atty: %d", atty);
daemon_from_uid = read_int(fd);
LOGD("remote uid: %d", daemon_from_uid);
LOGV("remote uid: %d", daemon_from_uid);
daemon_from_pid = read_int(fd);
LOGD("remote req pid: %d", daemon_from_pid);
LOGV("remote req pid: %d", daemon_from_pid);

struct ucred credentials;
int ucred_length = sizeof(struct ucred);
Expand All @@ -221,7 +221,7 @@ static int daemon_accept(int fd) {
LOGE("unable to allocate args: %d", argc);
exit(-1);
}
LOGD("remote args: %d", argc);
LOGV("remote args: %d", argc);
char** argv = (char**)malloc(sizeof(char*) * (argc + 1));
argv[argc] = NULL;
int i;
Expand Down Expand Up @@ -272,7 +272,7 @@ static int daemon_accept(int fd) {
close(ptm);
exit(-1);
}
LOGD("devname: %s", devname);
LOGV("devname: %s", devname);
}

int outfd = open(outfile, O_WRONLY);
Expand Down Expand Up @@ -359,7 +359,7 @@ static int daemon_accept(int fd) {
// wait for the child to exit, and send the exit code
// across the wire.
int status;
LOGD("waiting for child exit");
LOGV("waiting for child exit");
if (waitpid(child, &status, 0) > 0) {
code = WEXITSTATUS(status);
}
Expand All @@ -370,7 +370,7 @@ static int daemon_accept(int fd) {
done:
write(fd, &code, sizeof(int));
close(fd);
LOGD("child exited");
LOGV("child exited");
return code;
}

Expand Down Expand Up @@ -473,7 +473,7 @@ int connect_daemon(int argc, char *argv[]) {
exit(-1);
}

LOGD("connecting client %d", getpid());
LOGV("connecting client %d", getpid());

int mount_storage = getenv("MOUNT_EMULATED_STORAGE") != NULL;

Expand Down Expand Up @@ -516,6 +516,6 @@ int connect_daemon(int argc, char *argv[]) {
pump(outfd, STDOUT_FILENO);

int code = read_int(socketfd);
LOGD("client exited %d", code);
LOGV("client exited %d", code);
return code;
}
56 changes: 20 additions & 36 deletions Superuser/jni/su/su.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/uio.h>
#include <sys/un.h>
#include <sys/wait.h>
#include <sys/select.h>
Expand Down Expand Up @@ -81,48 +82,31 @@ int fork_zero_fucks() {
}
}

void exec_log(char *priority, char* logline) {
int pid;
if ((pid = fork()) == 0) {
int null = open("/dev/null", O_WRONLY | O_CLOEXEC);
dup2(null, STDIN_FILENO);
dup2(null, STDOUT_FILENO);
dup2(null, STDERR_FILENO);
execl("/system/bin/log", "/system/bin/log", "-p", priority, "-t", LOG_TAG, logline, NULL);
_exit(0);
}
int status;
waitpid(pid, &status, 0);
}

void exec_loge(const char* fmt, ...) {
void exec_log(int priority, const char* fmt, ...) {
static int log_fd = -1;
struct iovec vec[3];
va_list args;
char msg[PATH_MAX];

char logline[PATH_MAX];
va_start(args, fmt);
vsnprintf(logline, PATH_MAX, fmt, args);
va_end(args);
exec_log("e", logline);
}

void exec_logw(const char* fmt, ...) {
va_list args;
if (log_fd < 0) {
log_fd = open("/dev/log/main", O_WRONLY);
if (log_fd < 0) {
return;
}
}

char logline[PATH_MAX];
va_start(args, fmt);
vsnprintf(logline, PATH_MAX, fmt, args);
vsnprintf(msg, PATH_MAX, fmt, args);
va_end(args);
exec_log("w", logline);
}

void exec_logd(const char* fmt, ...) {
va_list args;
vec[0].iov_base = (unsigned char *) &priority;
vec[0].iov_len = 1;
vec[1].iov_base = (void *) LOG_TAG;
vec[1].iov_len = strlen(LOG_TAG) + 1;
vec[2].iov_base = (void *) msg;
vec[2].iov_len = strlen(msg) + 1;

char logline[PATH_MAX];
va_start(args, fmt);
vsnprintf(logline, PATH_MAX, fmt, args);
va_end(args);
exec_log("d", logline);
writev(log_fd, vec, 3);
}

static int from_init(struct su_initiator *from) {
Expand Down Expand Up @@ -427,7 +411,7 @@ do { \
static int socket_receive_result(int fd, char *result, ssize_t result_len) {
ssize_t len;

LOGD("waiting for user");
LOGV("waiting for user");
len = read(fd, result, result_len-1);
if (len < 0) {
PLOGE("read(result)");
Expand Down
31 changes: 21 additions & 10 deletions Superuser/jni/su/su.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,6 @@ static inline char *get_command(const struct su_request *to)
return DEFAULT_SHELL;
}

void exec_loge(const char* fmt, ...);
void exec_logw(const char* fmt, ...);
void exec_logd(const char* fmt, ...);

int run_daemon();
int connect_daemon(int argc, char *argv[]);
int su_main(int argc, char *argv[], int need_client);
Expand All @@ -180,16 +176,31 @@ int su_main(int argc, char *argv[], int need_client);
// deadbeat dad fork.
int fork_zero_fucks();

// fallback to using /system/bin/log.
// can't use liblog.so because this is a static binary.
// can't use liblog.so because this is a static binary, so we need
// to implement this ourselves
#include <android/log.h>

void exec_log(int priority, const char* fmt, ...);

#ifndef LOG_NDEBUG
#define LOG_NDEBUG 1
#endif

#ifndef LOGE
#define LOGE exec_loge
#define LOGE(fmt,args...) exec_log(ANDROID_LOG_ERROR, fmt, ##args)
#endif
#ifndef LOGW
#define LOGW(fmt,args...) exec_log(ANDROID_LOG_WARN, fmt, ##args)
#endif
#ifndef LOGD
#define LOGD exec_logd
#define LOGD(fmt,args...) exec_log(ANDROID_LOG_DEBUG, fmt, ##args)
#endif
#ifndef LOGV
#if LOG_NDEBUG
#define LOGV(...) ((void)0)
#else
#define LOGV(fmt,args...) exec_log(ANDROID_LOG_VERBOSE, fmt, ##args)
#endif
#ifndef LOGW
#define LOGW exec_logw
#endif

#if 0
Expand Down

0 comments on commit ed55b0e

Please sign in to comment.