chiark / gitweb /
util-lib: support various ppc archs in personality logic
[elogind.git] / src / basic / process-util.c
index 0c6a494e07931adcd70c7105c22f4d97f6cbd1a2..69a41d90b53da5786f07e48afce294c636c08030 100644 (file)
   along with systemd; If not, see <http://www.gnu.org/licenses/>.
 ***/
 
-#include <assert.h>
 #include <ctype.h>
 #include <errno.h>
+#include <limits.h>
+#include <linux/oom.h>
 #include <sched.h>
 #include <signal.h>
 #include <stdbool.h>
 #include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
 #include <sys/personality.h>
 #include <sys/prctl.h>
 #include <sys/types.h>
 #include <sys/wait.h>
+#include <syslog.h>
 #include <unistd.h>
+#ifdef HAVE_VALGRIND_VALGRIND_H
+#include <valgrind/valgrind.h>
+#endif
 
 #include "alloc-util.h"
+#include "architecture.h"
 #include "escape.h"
 #include "fd-util.h"
 #include "fileio.h"
 #include "fs-util.h"
 //#include "ioprio.h"
 #include "log.h"
+#include "macro.h"
+#include "missing.h"
 #include "process-util.h"
 #include "signal-util.h"
+//#include "stat-util.h"
 #include "string-table.h"
 #include "string-util.h"
 #include "user-util.h"
@@ -637,6 +647,19 @@ bool pid_is_alive(pid_t pid) {
         return true;
 }
 
+#if 0 /// UNNEEDED by elogind
+int pid_from_same_root_fs(pid_t pid) {
+        const char *root;
+
+        if (pid < 0)
+                return 0;
+
+        root = procfs_file_alloca(pid, "root");
+
+        return files_same(root, "/proc/1/root");
+}
+#endif // 0
+
 bool is_main_thread(void) {
         static thread_local int cached = 0;
 
@@ -663,75 +686,155 @@ bool oom_score_adjust_is_valid(int oa) {
 }
 
 unsigned long personality_from_string(const char *p) {
+        int architecture;
+
+        /* Parse a personality specifier. We use our own identifiers that indicate specific ABIs, rather than just
+         * hints regarding the register size, since we want to keep things open for multiple locally supported ABIs for
+         * the same register size. */
 
-        /* Parse a personality specifier. We introduce our own
-         * identifiers that indicate specific ABIs, rather than just
-         * hints regarding the register size, since we want to keep
-         * things open for multiple locally supported ABIs for the
-         * same register size. We try to reuse the ABI identifiers
-         * used by libseccomp. */
+        architecture = architecture_from_string(p);
+        if (architecture < 0)
+                return PERSONALITY_INVALID;
 
 #if defined(__x86_64__)
 
-        if (streq(p, "x86"))
+        if (architecture == ARCHITECTURE_X86)
                 return PER_LINUX32;
 
-        if (streq(p, "x86-64"))
+        if (architecture == ARCHITECTURE_X86_64)
                 return PER_LINUX;
 
 #elif defined(__i386__)
 
-        if (streq(p, "x86"))
+        if (architecture == ARCHITECTURE_X86)
                 return PER_LINUX;
 
 #elif defined(__s390x__)
 
-        if (streq(p, "s390"))
+        if (architecture == ARCHITECTURE_S390)
                 return PER_LINUX32;
 
-        if (streq(p, "s390x"))
+        if (architecture == ARCHITECTURE_S390X)
                 return PER_LINUX;
 
 #elif defined(__s390__)
 
-        if (streq(p, "s390"))
+        if (architecture == ARCHITECTURE_S390)
+                return PER_LINUX;
+
+#elif defined(__powerpc64__)
+#  if __BYTE_ORDER == __BIG_ENDIAN
+
+        if (architecture == ARCHITECTURE_PPC_LE)
+                return PER_LINUX32;
+
+        if (architecture == ARCHITECTURE_PPC64_LE)
+                return PER_LINUX;
+
+#  else
+
+        if (architecture == ARCHITECTURE_PPC)
+                return PER_LINUX32;
+
+        if (architecture == ARCHITECTURE_PPC64)
+                return PER_LINUX;
+
+#  endif
+#elif defined(__powerpc__)
+#  if __BYTE_ORDER == __BIG_ENDIAN
+
+        if (architecture == ARCHITECTURE_PPC)
                 return PER_LINUX;
+
+#  else
+
+        if (architecture == ARCHITECTURE_PPC_LE)
+                return PER_LINUX;
+
+#  endif
 #endif
 
         return PERSONALITY_INVALID;
 }
 
 const char* personality_to_string(unsigned long p) {
+        int architecture = _ARCHITECTURE_INVALID;
 
 #if defined(__x86_64__)
 
-        if (p == PER_LINUX32)
-                return "x86";
-
         if (p == PER_LINUX)
-                return "x86-64";
+                architecture = ARCHITECTURE_X86_64;
+        else if (p == PER_LINUX32)
+                architecture = ARCHITECTURE_X86;
 
 #elif defined(__i386__)
 
         if (p == PER_LINUX)
-                return "x86";
+                architecture = ARCHITECTURE_X86;
 
 #elif defined(__s390x__)
 
         if (p == PER_LINUX)
-                return "s390x";
-
-        if (p == PER_LINUX32)
-                return "s390";
+                architecture = ARCHITECTURE_S390X;
+        else if (p == PER_LINUX32)
+                architecture = ARCHITECTURE_S390;
 
 #elif defined(__s390__)
 
         if (p == PER_LINUX)
-                return "s390";
+                architecture = ARCHITECTURE_S390;
+
+#elif defined(__powerpc64__)
+#  if __BYTE_ORDER == __BIG_ENDIAN
+
+        if (p == PER_LINUX)
+                architecture = ARCHITECTURE_PPC64;
+        else if (p == PER_LINUX32)
+                 architecture = ARCHITECTURE_PPC;
+
+#  else
+
+        if (p == PER_LINUX)
+                architecture = ARCHITECTURE_PPC64_LE;
+        else if (p == PER_LINUX32)
+                architecture = ARCHITECTURE_PPC_LE;
 
+#  endif
+#elif defined(__powerpc__)
+#  if __BYTE_ORDER == __BIG_ENDIAN
+
+        if (p == PER_LINUX)
+                architecture = ARCHITECTURE_PPC;
+
+#  else
+
+        if (p == PER_LINUX)
+                architecture = ARCHITECTURE_PPC_LE;
+
+#  endif
 #endif
 
-        return NULL;
+        if (architecture < 0)
+                return NULL;
+
+        return architecture_to_string(architecture);
+}
+
+void valgrind_summary_hack(void) {
+#ifdef HAVE_VALGRIND_VALGRIND_H
+        if (getpid() == 1 && RUNNING_ON_VALGRIND) {
+                pid_t pid;
+                pid = raw_clone(SIGCHLD, NULL);
+                if (pid < 0)
+                        log_emergency_errno(errno, "Failed to fork off valgrind helper: %m");
+                else if (pid == 0)
+                        exit(EXIT_SUCCESS);
+                else {
+                        log_info("Spawned valgrind helper as PID "PID_FMT".", pid);
+                        (void) wait_for_terminate(pid, NULL);
+                }
+        }
+#endif
 }
 
 static const char *const ioprio_class_table[] = {