Skip to content

Commit

Permalink
support bsds
Browse files Browse the repository at this point in the history
  • Loading branch information
dixyes committed Jun 1, 2024
1 parent 76db2cb commit bc574ef
Showing 1 changed file with 57 additions and 5 deletions.
62 changes: 57 additions & 5 deletions php_micro_fileinfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ limitations under the License.
# include "win32/codepage.h"
# include <windows.h>
#elif defined(__linux) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
// all the things linux used should also work for other unix-like using ELF,
// but not well tested
# include <elf.h>
# include <sys/auxv.h>
# if defined(__linux) || defined(__FreeBSD__)
# include <sys/auxv.h> // for getauxval / elf_aux_info
# else
# include <sys/sysctl.h> // for sysctl
# endif // defined(__linux) || defined(__FreeBSD__)
# if defined(__LP64__)
typedef Elf64_Ehdr Elf_Ehdr;
typedef Elf64_Shdr Elf_Shdr;
Expand Down Expand Up @@ -428,7 +430,7 @@ int _micro_init_sfxsize(void) {
dbgprintf("using __DATA,__micro_sfxsize section: %zd, %zd\n", _final_sfxsize, _sfxsize_limit);

return SUCCESS;
#elif defined(__linux) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
#elif defined(__linux) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
// get section header
Elf_Ehdr ehdr;
Elf_Shdr *shdrs = NULL, *shdr, *strtabhdr;
Expand Down Expand Up @@ -682,7 +684,8 @@ const char *micro_get_filename(void) {
return self_filename;
}

#elif defined(__linux) || defined(__NetBSD__) || defined(__OpenBSD__)
#elif defined(__linux)
// use getauxval AT_EXECFN
const char *micro_get_filename(void) {
static char *self_filename = NULL;
if (NULL == self_filename) {
Expand All @@ -692,6 +695,7 @@ const char *micro_get_filename(void) {
return self_filename;
}
#elif defined(__FreeBSD__)
// use elf_aux_info AT_EXECPATH
const char *micro_get_filename(void) {
static char *self_filename = NULL;
char filename[PATH_MAX];
Expand All @@ -704,6 +708,54 @@ const char *micro_get_filename(void) {
}
return self_filename;
}
#elif defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
// use sysctl
const char *micro_get_filename(void) {
static char *self_filename = NULL;
int mib[4];
size_t len;
char *buf;
if (NULL == self_filename) {
mib[0] = CTL_KERN;
mib[1] = KERN_PROC_ARGS;
mib[2] = getpid();
# if defined(KERN_PROC_PATHNAME)
mib[3] = KERN_PROC_PATHNAME;
# elif defined(KERN_PROC_ARGV)
// fail back to argv[0], this is fragile
// if argv is changed or parent sets argv[0] to another value,
// there will be a wrong value
mib[3] = KERN_PROC_ARGV;
# else
# error "not support this system yet"
# endif

if (sysctl(mib, 4, NULL, &len, NULL, 0) != 0) {
return NULL;
}

buf = malloc(len);
if (NULL == buf) {
// impossible
return NULL;
}

if (sysctl(mib, 4, buf, &len, NULL, 0) != 0) {
free(buf);
return NULL;
}

# if defined(KERN_PROC_PATHNAME)
(void)realpath(buf, self_filename);
# elif defined(KERN_PROC_ARGV) && defined(__OpenBSD__)
(void)realpath((char **)buf[0], self_filename);
# else
# error "not support this system yet"
# endif
free(buf);
}
return self_filename;
}
#elif defined(__APPLE__)
const char *micro_get_filename(void) {
static char *self_path = NULL;
Expand Down

0 comments on commit bc574ef

Please sign in to comment.