procd: fix compilation with uClibc-ng
[project/procd.git] / trace / trace.c
index 1c1e75753fc910fad78f4aec4c7a01f3a55d6f29..93554f73e922d605c4313812d3b7172c6298f3db 100644 (file)
 #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>
@@ -58,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
@@ -211,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);
@@ -317,13 +327,17 @@ 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);
@@ -383,7 +397,8 @@ int main(int argc, char **argv, char **envp)
        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);