1 //===-- sanitizer_openbsd.cpp ---------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file is shared between various sanitizers' runtime libraries and
10 // implements Solaris-specific functions.
11 //===----------------------------------------------------------------------===//
13 #include "sanitizer_platform.h"
18 #include "sanitizer_common.h"
19 #include "sanitizer_flags.h"
20 #include "sanitizer_internal_defs.h"
21 #include "sanitizer_libc.h"
22 #include "sanitizer_placement_new.h"
23 #include "sanitizer_platform_limits_posix.h"
24 #include "sanitizer_procmaps.h"
36 #include <sys/sysctl.h>
37 #include <sys/types.h>
40 extern char **environ
;
42 namespace __sanitizer
{
44 uptr
internal_mmap(void *addr
, size_t length
, int prot
, int flags
, int fd
,
46 return (uptr
)mmap(addr
, length
, prot
, flags
, fd
, offset
);
49 uptr
internal_munmap(void *addr
, uptr length
) { return munmap(addr
, length
); }
51 int internal_mprotect(void *addr
, uptr length
, int prot
) {
52 return mprotect(addr
, length
, prot
);
55 int internal_sysctlbyname(const char *sname
, void *oldp
, uptr
*oldlenp
,
56 const void *newp
, uptr newlen
) {
57 Printf("internal_sysctlbyname not implemented for OpenBSD");
62 uptr
ReadBinaryName(/*out*/char *buf
, uptr buf_len
) {
63 // On OpenBSD we cannot get the full path
66 const int Mib
[4] = {CTL_KERN
, KERN_PROC
, KERN_PROC_PID
, getpid()};
67 if (internal_sysctl(Mib
, ARRAY_SIZE(Mib
), &kp
, &kl
, NULL
, 0) != -1)
68 return internal_snprintf(buf
,
69 (KI_MAXCOMLEN
< buf_len
? KI_MAXCOMLEN
: buf_len
),
74 static void GetArgsAndEnv(char ***argv
, char ***envp
) {
77 int argvmib
[4] = {CTL_KERN
, KERN_PROC_ARGS
, getpid(), KERN_PROC_ARGV
};
78 int envmib
[4] = {CTL_KERN
, KERN_PROC_ARGS
, getpid(), KERN_PROC_ENV
};
79 if (internal_sysctl(argvmib
, 4, NULL
, &nargv
, NULL
, 0) == -1) {
80 Printf("sysctl KERN_PROC_NARGV failed\n");
83 if (internal_sysctl(envmib
, 4, NULL
, &nenv
, NULL
, 0) == -1) {
84 Printf("sysctl KERN_PROC_NENV failed\n");
87 if (internal_sysctl(argvmib
, 4, &argv
, &nargv
, NULL
, 0) == -1) {
88 Printf("sysctl KERN_PROC_ARGV failed\n");
91 if (internal_sysctl(envmib
, 4, &envp
, &nenv
, NULL
, 0) == -1) {
92 Printf("sysctl KERN_PROC_ENV failed\n");
99 GetArgsAndEnv(&argv
, &envp
);
103 char **GetEnviron() {
105 GetArgsAndEnv(&argv
, &envp
);
113 } // namespace __sanitizer
115 #endif // SANITIZER_OPENBSD