[LEDE-DEV,procd,17/17] utrace: Switch all logging to ulog

Message ID 20170912111250.31576-20-sojkam1@fel.cvut.cz
State Accepted
Headers show
Series
  • [LEDE-DEV,procd,01/17] utrace: Fix environment initialization
Related show

Commit Message

Michal Sojka Sept. 12, 2017, 11:12 a.m.
This unifies all logs messages produced by utrace and removes
duplicated functionality.

Signed-off-by: Michal Sojka <sojkam1@fel.cvut.cz>
---
 trace/trace.c | 49 +++++++++++++++++++------------------------------
 1 file changed, 19 insertions(+), 30 deletions(-)

Comments

Michal Sojka Sept. 27, 2017, 8:58 a.m. | #1
Hi,

my automated tests revealed another bug (race condition) in utrace. I'm
sending a patch that fixes it as a reply to this message. Let me know if
it would be better to send the whole fixed patch series again as v2.

-Michal

Patch

diff --git a/trace/trace.c b/trace/trace.c
index 8228edf..2621430 100644
--- a/trace/trace.c
+++ b/trace/trace.c
@@ -25,7 +25,6 @@ 
 #include <errno.h>
 #include <string.h>
 #include <syslog.h>
-#include <err.h>
 
 #ifndef PTRACE_EVENT_STOP
 /* PTRACE_EVENT_STOP is defined in linux/ptrace.h, but this header
@@ -33,6 +32,7 @@ 
 #define PTRACE_EVENT_STOP 128
 #endif
 
+#include <libubox/ulog.h>
 #include <libubox/uloop.h>
 #include <libubox/blobmsg.h>
 #include <libubox/blobmsg_json.h>
@@ -62,21 +62,6 @@  enum mode {
 	SECCOMP_TRACE,
 } mode = UTRACE;
 
-#define PROC_NAME(mode) (mode == UTRACE ? "utrace" : "seccomp-trace")
-
-#define INFO(fmt, ...) do { \
-	fprintf(stderr, "%s: "fmt, PROC_NAME(mode), ## __VA_ARGS__); \
-} while (0)
-
-#define ERROR(fmt, ...) do { \
-	syslog(LOG_ERR, "%s: "fmt, PROC_NAME(mode), ## __VA_ARGS__); \
-	fprintf(stderr, "%s: "fmt, PROC_NAME(mode), ## __VA_ARGS__);  \
-} while (0)
-
-#define LOGERR(fmt, ...) do { \
-	syslog(LOG_ERR, "%s: "fmt, PROC_NAME(mode), ## __VA_ARGS__); \
-} while (0)
-
 struct tracee {
 	struct uloop_process proc;
 	int in_syscall;
@@ -148,7 +133,7 @@  static void print_syscalls(int policy, const char *json)
 				       sc, syscall_name(sc), sorted[i].count);
 			blobmsg_add_string(&b, NULL, syscall_name(sc));
 		} else {
-			ERROR("no name found for syscall(%d)\n", sc);
+			ULOG_ERR("no name found for syscall(%d)\n", sc);
 		}
 	}
 	blobmsg_close_array(&b, c);
@@ -158,9 +143,9 @@  static void print_syscalls(int policy, const char *json)
 		if (fp) {
 			fprintf(fp, "%s", blobmsg_format_json_indent(b.head, true, 0));
 			fclose(fp);
-			INFO("saving syscall trace to %s\n", json);
+			ULOG_INFO("saving syscall trace to %s\n", json);
 		} else {
-			ERROR("failed to open %s\n", json);
+			ULOG_ERR("failed to open %s\n", json);
 		}
 	} else {
 		printf("%s\n",
@@ -186,11 +171,11 @@  static void report_seccomp_vialation(pid_t pid, unsigned syscall)
 	int i = syscall_index(syscall);
 	if (i >= 0) {
 		syscall_count[i]++;
-		LOGERR("%s[%u] tried to call non-whitelisted syscall: %s (see %s)\n",
-		       buf, pid,  syscall_name(syscall), json);
+		ULOG_ERR("%s[%u] tried to call non-whitelisted syscall: %s (see %s)\n",
+			 buf, pid,  syscall_name(syscall), json);
 	} else {
-		LOGERR("%s[%u] tried to call non-whitelisted syscall: %d (see %s)\n",
-		       buf, pid,  syscall, json);
+		ULOG_ERR("%s[%u] tried to call non-whitelisted syscall: %d (see %s)\n",
+			 buf, pid,  syscall, json);
 	}
 }
 
@@ -332,7 +317,7 @@  int main(int argc, char **argv, char **envp)
 		memcpy(&_envp[newenv], envp, envc * sizeof(char *));
 
 		ret = execve(_argv[0], _argv, _envp);
-		ERROR("failed to exec %s: %s\n", _argv[0], strerror(errno));
+		ULOG_ERR("failed to exec %s: %s\n", _argv[0], strerror(errno));
 
 		free(_argv);
 		free(_envp);
@@ -344,7 +329,7 @@  int main(int argc, char **argv, char **envp)
 
 	waitpid(child, &status, WUNTRACED);
 	if (!WIFSTOPPED(status)) {
-		ERROR("failed to start %s\n", *argv);
+		ULOG_ERR("failed to start %s\n", *argv);
 		return -1;
 	}
 
@@ -359,10 +344,14 @@  int main(int argc, char **argv, char **envp)
 		ptrace_restart = PTRACE_CONT;
 		break;
 	}
-	if (ptrace(PTRACE_SEIZE, child, 0, ptrace_options) == -1)
-		err(1, "PTRACE_SEIZE");
-	if (ptrace(ptrace_restart, child, 0, SIGCONT) == -1)
-		err(1, "ptrace restart");
+	if (ptrace(PTRACE_SEIZE, child, 0, ptrace_options) == -1) {
+		ULOG_ERR("PTRACE_SEIZE: %s\n", strerror(errno));
+		return -1;
+	}
+	if (ptrace(ptrace_restart, child, 0, SIGCONT) == -1) {
+		ULOG_ERR("ptrace_restart: %s\n", strerror(errno));
+		return -1;
+	}
 
 	uloop_init();
 	tracer.proc.pid = child;
@@ -377,7 +366,7 @@  int main(int argc, char **argv, char **envp)
 	case UTRACE:
 		if (!json)
 			if (asprintf(&json, "/tmp/%s.%u.json", basename(*argv), child) < 0)
-				ERROR("failed to allocate output path: %s\n", strerror(errno));
+				ULOG_ERR("failed to allocate output path: %s\n", strerror(errno));
 		break;
 	case SECCOMP_TRACE:
 		if (!violation_count)