Home | History | Annotate | Line # | Download | only in sanitizer_common
      1 //===-- sanitizer_openbsd.cc ----------------------------------------------===//
      2 //
      3 // This file is distributed under the University of Illinois Open Source
      4 // License. See LICENSE.TXT for details.
      5 //
      6 //===----------------------------------------------------------------------===//
      7 //
      8 // This file is shared between various sanitizers' runtime libraries and
      9 // implements Solaris-specific functions.
     10 //===----------------------------------------------------------------------===//
     11 
     12 #include "sanitizer_platform.h"
     13 #if SANITIZER_OPENBSD
     14 
     15 #include <stdio.h>
     16 
     17 #include "sanitizer_common.h"
     18 #include "sanitizer_flags.h"
     19 #include "sanitizer_internal_defs.h"
     20 #include "sanitizer_libc.h"
     21 #include "sanitizer_placement_new.h"
     22 #include "sanitizer_platform_limits_posix.h"
     23 #include "sanitizer_procmaps.h"
     24 
     25 #include <errno.h>
     26 #include <fcntl.h>
     27 #include <limits.h>
     28 #include <pthread.h>
     29 #include <sched.h>
     30 #include <signal.h>
     31 #include <stdio.h>
     32 #include <stdlib.h>
     33 #include <sys/mman.h>
     34 #include <sys/shm.h>
     35 #include <sys/sysctl.h>
     36 #include <sys/types.h>
     37 #include <unistd.h>
     38 
     39 extern char **environ;
     40 
     41 namespace __sanitizer {
     42 
     43 uptr internal_mmap(void *addr, size_t length, int prot, int flags, int fd,
     44                    u64 offset) {
     45   return (uptr)mmap(addr, length, prot, flags, fd, offset);
     46 }
     47 
     48 uptr internal_munmap(void *addr, uptr length) { return munmap(addr, length); }
     49 
     50 int internal_mprotect(void *addr, uptr length, int prot) {
     51   return mprotect(addr, length, prot);
     52 }
     53 
     54 int internal_sysctlbyname(const char *sname, void *oldp, uptr *oldlenp,
     55                           const void *newp, uptr newlen) {
     56   Printf("internal_sysctlbyname not implemented for OpenBSD");
     57   Die();
     58   return 0;
     59 }
     60 
     61 uptr ReadBinaryName(/*out*/char *buf, uptr buf_len) {
     62   // On OpenBSD we cannot get the full path
     63   struct kinfo_proc kp;
     64   uptr kl;
     65   const int Mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PID, getpid()};
     66   if (internal_sysctl(Mib, ARRAY_SIZE(Mib), &kp, &kl, NULL, 0) != -1)
     67     return internal_snprintf(buf,
     68                              (KI_MAXCOMLEN < buf_len ? KI_MAXCOMLEN : buf_len),
     69                              "%s", kp.p_comm);
     70   return (uptr)0;
     71 }
     72 
     73 static void GetArgsAndEnv(char ***argv, char ***envp) {
     74   uptr nargv;
     75   uptr nenv;
     76   int argvmib[4] = {CTL_KERN, KERN_PROC_ARGS, getpid(), KERN_PROC_ARGV};
     77   int envmib[4] = {CTL_KERN, KERN_PROC_ARGS, getpid(), KERN_PROC_ENV};
     78   if (internal_sysctl(argvmib, 4, NULL, &nargv, NULL, 0) == -1) {
     79     Printf("sysctl KERN_PROC_NARGV failed\n");
     80     Die();
     81   }
     82   if (internal_sysctl(envmib, 4, NULL, &nenv, NULL, 0) == -1) {
     83     Printf("sysctl KERN_PROC_NENV failed\n");
     84     Die();
     85   }
     86   if (internal_sysctl(argvmib, 4, &argv, &nargv, NULL, 0) == -1) {
     87     Printf("sysctl KERN_PROC_ARGV failed\n");
     88     Die();
     89   }
     90   if (internal_sysctl(envmib, 4, &envp, &nenv, NULL, 0) == -1) {
     91     Printf("sysctl KERN_PROC_ENV failed\n");
     92     Die();
     93   }
     94 }
     95 
     96 char **GetArgv() {
     97   char **argv, **envp;
     98   GetArgsAndEnv(&argv, &envp);
     99   return argv;
    100 }
    101 
    102 char **GetEnviron() {
    103   char **argv, **envp;
    104   GetArgsAndEnv(&argv, &envp);
    105   return envp;
    106 }
    107 
    108 void ReExec() {
    109   UNIMPLEMENTED();
    110 }
    111 
    112 }  // namespace __sanitizer
    113 
    114 #endif  // SANITIZER_OPENBSD
    115