procd: fix compilation with uClibc-ng
[project/procd.git] / trace / trace.c
index 76b6b7f260de41e2471c1ddaa6954d5db7291dee..93554f73e922d605c4313812d3b7172c6298f3db 100644 (file)
@@ -25,6 +25,7 @@
 #include <errno.h>
 #include <string.h>
 #include <syslog.h>
+#include <limits.h>
 
 #ifndef PTRACE_EVENT_STOP
 /* PTRACE_EVENT_STOP is defined in linux/ptrace.h, but this header
 #define PTRACE_EVENT_STOP 128
 #endif
 
+#ifndef PTRACE_EVENT_SECCOMP
+/* undefined with uClibc-ng */
+#define PTRACE_EVENT_SECCOMP 7
+#endif
+
 #include <libubox/ulog.h>
 #include <libubox/uloop.h>
 #include <libubox/blobmsg.h>
@@ -57,6 +63,9 @@
 # if defined(__ARM_EABI__)
 # define reg_retval_nr _offsetof(struct user, regs.uregs[0])
 # endif
+#elif defined(__PPC__)
+#define reg_syscall_nr _offsetof(struct user, regs.gpr[0])
+#define reg_retval_nr  _offsetof(struct user, regs.gpr[3])
 #else
 #error tracing is not supported on this architecture
 #endif
@@ -210,7 +219,9 @@ static void tracer_cb(struct uloop_process *c, int ret)
                           (ret >> 8) == (SIGTRAP | (PTRACE_EVENT_CLONE << 8))) {
                        struct tracee *child = calloc(1, sizeof(struct tracee));
 
-                       ptrace(PTRACE_GETEVENTMSG, c->pid, 0, &child->proc.pid);
+                       unsigned long msg;
+                       ptrace(PTRACE_GETEVENTMSG, c->pid, 0, &msg);
+                       child->proc.pid = msg;
                        child->proc.cb = tracer_cb;
                        ptrace(ptrace_restart, child->proc.pid, 0, 0);
                        uloop_process_add(&child->proc);
@@ -316,17 +327,21 @@ int main(int argc, char **argv, char **envp)
                case SECCOMP_TRACE:
                        preload = "/lib/libpreload-seccomp.so";
                        newenv = 2;
-                       asprintf(&_envp[1], "SECCOMP_FILE=%s", json ? json : "");
+                       if (asprintf(&_envp[1], "SECCOMP_FILE=%s", json ? json : "") < 0)
+                               ULOG_ERR("failed to allocate SECCOMP_FILE env: %m\n");
+
                        kill(getpid(), SIGSTOP);
                        break;
                }
-               asprintf(&_envp[0], "LD_PRELOAD=%s%s%s", preload,
-                        old_preload ? ":" : "",
-                        old_preload ? old_preload : "");
+               if (asprintf(&_envp[0], "LD_PRELOAD=%s%s%s", preload,
+                            old_preload ? ":" : "",
+                             old_preload ? old_preload : "") < 0)
+                       ULOG_ERR("failed to allocate LD_PRELOAD env: %m\n");
+
                memcpy(&_envp[newenv], envp, envc * sizeof(char *));
 
                ret = execve(_argv[0], _argv, _envp);
-               ULOG_ERR("failed to exec %s: %s\n", _argv[0], strerror(errno));
+               ULOG_ERR("failed to exec %s: %m\n", _argv[0]);
 
                free(_argv);
                free(_envp);
@@ -357,11 +372,11 @@ int main(int argc, char **argv, char **envp)
                break;
        }
        if (ptrace(PTRACE_SEIZE, child, 0, ptrace_options) == -1) {
-               ULOG_ERR("PTRACE_SEIZE: %s\n", strerror(errno));
+               ULOG_ERR("PTRACE_SEIZE: %m\n");
                return -1;
        }
        if (ptrace(ptrace_restart, child, 0, SIGCONT) == -1) {
-               ULOG_ERR("ptrace_restart: %s\n", strerror(errno));
+               ULOG_ERR("ptrace_restart: %m\n");
                return -1;
        }
 
@@ -377,12 +392,13 @@ int main(int argc, char **argv, char **envp)
        case UTRACE:
                if (!json)
                        if (asprintf(&json, "/tmp/%s.%u.json", basename(*argv), child) < 0)
-                               ULOG_ERR("failed to allocate output path: %s\n", strerror(errno));
+                               ULOG_ERR("failed to allocate output path: %m\n");
                break;
        case SECCOMP_TRACE:
                if (!violation_count)
                        return 0;
-               asprintf(&json, "/tmp/%s.%u.violations.json", basename(*argv), child);
+               if (asprintf(&json, "/tmp/%s.%u.violations.json", basename(*argv), child) < 0)
+                       ULOG_ERR("failed to allocate violations output path: %m\n");
                break;
        }
        print_syscalls(policy, json);