Use sysctl on FreeBSD

This commit is contained in:
rdb 2010-04-25 13:19:55 +00:00
parent 0e54f8b85b
commit bd5e89e4a5

View File

@ -38,6 +38,10 @@
#ifdef IS_FREEBSD #ifdef IS_FREEBSD
extern char **environ; extern char **environ;
// This is for sysctl.
#include <sys/types.h>
#include <sys/sysctl.h>
#endif #endif
#ifdef HAVE_PYTHON #ifdef HAVE_PYTHON
@ -565,6 +569,26 @@ read_args() {
_args.push_back(GLOBAL_ARGV[i]); _args.push_back(GLOBAL_ARGV[i]);
} }
#elif defined(IS_FREEBSD)
// In FreeBSD, we can use sysctl to determine the command-line arguments.
size_t bufsize = 4096;
char buffer[4096];
int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_ARGS, getpid()};
if (sysctl(mib, 4, (void*) &buffer, &bufsize, NULL, 0) == -1) {
perror("sysctl");
} else {
if (_binary_name.empty()) {
_binary_name = buffer;
}
int idx = strlen(buffer) + 1;
while (idx < bufsize) {
_args.push_back((char*)(buffer + idx));
int newidx = strlen(buffer + idx);
idx += newidx + 1;
}
}
#elif defined(HAVE_PROC_SELF_CMDLINE) || defined(HAVE_PROC_CURPROC_CMDLINE) #elif defined(HAVE_PROC_SELF_CMDLINE) || defined(HAVE_PROC_CURPROC_CMDLINE)
// In Linux, and possibly in other systems as well, we might not be // In Linux, and possibly in other systems as well, we might not be
// able to use the global ARGC/ARGV variables at static init time. // able to use the global ARGC/ARGV variables at static init time.