From 961a754ec4912f477141e99b3e738e763ef2cbc5 Mon Sep 17 00:00:00 2001 From: rdb Date: Wed, 22 Jul 2009 05:52:38 +0000 Subject: [PATCH] Add support for /proc/curproc for FreeBSD --- dtool/Config.FreeBSD.pp | 8 ++++++- dtool/Config.Linux.pp | 9 ++++---- dtool/Config.OSX.pp | 11 +++++----- dtool/Config.Win32.pp | 9 ++++---- dtool/LocalSetup.pp | 12 ++++++----- dtool/src/dtoolutil/executionEnvironment.cxx | 22 +++++++++++++++++--- makepanda/makepanda.py | 16 ++++++++++---- 7 files changed, 58 insertions(+), 29 deletions(-) diff --git a/dtool/Config.FreeBSD.pp b/dtool/Config.FreeBSD.pp index 6d8bafc1ca..76eff05d65 100644 --- a/dtool/Config.FreeBSD.pp +++ b/dtool/Config.FreeBSD.pp @@ -84,9 +84,15 @@ #define GLOBAL_ARGV #define GLOBAL_ARGC -// Can we read the file /proc/self/cmdline to determine our +// Can we read the file /proc/curproc/* to determine our // command-line arguments at static init time? +#define HAVE_PROC_SELF_EXE +#define HAVE_PROC_SELF_MAPS +#define HAVE_PROC_SELF_ENVIRON #define HAVE_PROC_SELF_CMDLINE +#define HAVE_PROC_CURPROC_FILE 1 +#define HAVE_PROC_CURPROC_MAP 1 +#define HAVE_PROC_CURPROC_CMDLINE 1 // Should we include or ? Define HAVE_IOSTREAM // to nonempty if we should use , or empty if we should use diff --git a/dtool/Config.Linux.pp b/dtool/Config.Linux.pp index 0765c16ee1..61f90589f7 100644 --- a/dtool/Config.Linux.pp +++ b/dtool/Config.Linux.pp @@ -235,9 +235,12 @@ // Can we safely call getenv() at static init time? #define STATIC_INIT_GETENV 1 -// Can we read the file /proc/self/environ to determine our +// Can we read the files /proc/self/* to determine our // environment variables at static init time? +#define HAVE_PROC_SELF_EXE 1 +#define HAVE_PROC_SELF_MAPS 1 #define HAVE_PROC_SELF_ENVIRON 1 +#define HAVE_PROC_SELF_CMDLINE 1 // Do we have a global pair of argc/argv variables that we can read at // static init time? Should we prototype them? What are they called? @@ -246,10 +249,6 @@ #define GLOBAL_ARGV #define GLOBAL_ARGC -// Can we read the file /proc/self/cmdline to determine our -// command-line arguments at static init time? -#define HAVE_PROC_SELF_CMDLINE 1 - // Should we include or ? Define HAVE_IOSTREAM // to nonempty if we should use , or empty if we should use // . diff --git a/dtool/Config.OSX.pp b/dtool/Config.OSX.pp index bdaac8c7a3..6dc0a48ad7 100644 --- a/dtool/Config.OSX.pp +++ b/dtool/Config.OSX.pp @@ -187,9 +187,12 @@ // Can we safely call getenv() at static init time? #define STATIC_INIT_GETENV 1 -// Can we read the file /proc/self/environ to determine our +// Can we read the file /proc/self/* to determine our // environment variables at static init time? -#define HAVE_PROC_SELF_ENVIRON 1 +#define HAVE_PROC_SELF_EXE +#define HAVE_PROC_SELF_MAPS +#define HAVE_PROC_SELF_ENVIRON +#define HAVE_PROC_SELF_CMDLINE // Do we have a global pair of argc/argv variables that we can read at // static init time? Should we prototype them? What are they called? @@ -198,10 +201,6 @@ #define GLOBAL_ARGV __Argv #define GLOBAL_ARGC __Argc -// Can we read the file /proc/self/cmdline to determine our -// command-line arguments at static init time? -#define HAVE_PROC_SELF_CMDLINE - // Should we include or ? Define HAVE_IOSTREAM // to nonempty if we should use , or empty if we should use // . diff --git a/dtool/Config.Win32.pp b/dtool/Config.Win32.pp index ac680c3e9a..0c88c2859d 100644 --- a/dtool/Config.Win32.pp +++ b/dtool/Config.Win32.pp @@ -74,9 +74,12 @@ // Can we safely call getenv() at static init time? #define STATIC_INIT_GETENV 1 -// Can we read the file /proc/self/environ to determine our +// Can we read the file /proc/self/* to determine our // environment variables at static init time? +#define HAVE_PROC_SELF_EXE +#define HAVE_PROC_SELF_MAPS #define HAVE_PROC_SELF_ENVIRON +#define HAVE_PROC_SELF_CMDLINE // Do we have a global pair of argc/argv variables that we can read at // static init time? Should we prototype them? What are they called? @@ -85,10 +88,6 @@ #define GLOBAL_ARGV __argv #define GLOBAL_ARGC __argc -// Can we read the file /proc/self/cmdline to determine our -// command-line arguments at static init time? -#define HAVE_PROC_SELF_CMDLINE - // Should we include or ? Define HAVE_IOSTREAM // to nonempty if we should use , or empty if we should use // . diff --git a/dtool/LocalSetup.pp b/dtool/LocalSetup.pp index 84f868d8a4..accce517a5 100644 --- a/dtool/LocalSetup.pp +++ b/dtool/LocalSetup.pp @@ -529,9 +529,15 @@ $[cdefine HAVE_IOS_BINARY] /* Can we safely call getenv() at static init time? */ $[cdefine STATIC_INIT_GETENV] -/* Can we read the file /proc/self/environ to determine our +/* Can we read the file /proc/self/* to determine our environment variables at static init time? */ +$[cdefine HAVE_PROC_SELF_EXE] +$[cdefine HAVE_PROC_SELF_MAPS] $[cdefine HAVE_PROC_SELF_ENVIRON] +$[cdefine HAVE_PROC_SELF_CMDLINE] +$[cdefine HAVE_PROC_CURPROC_FILE] +$[cdefine HAVE_PROC_CURPROC_MAP] +$[cdefine HAVE_PROC_CURPROC_CMDLINE] /* Do we have a global pair of argc/argv variables that we can read at static init time? Should we prototype them? What are they called? */ @@ -540,10 +546,6 @@ $[cdefine PROTOTYPE_GLOBAL_ARGV] $[cdefine GLOBAL_ARGV] $[cdefine GLOBAL_ARGC] -/* Can we read the file /proc/self/cmdline to determine our - command-line arguments at static init time? */ -$[cdefine HAVE_PROC_SELF_CMDLINE] - /* Define if you have the header file. */ $[cdefine HAVE_IO_H] diff --git a/dtool/src/dtoolutil/executionEnvironment.cxx b/dtool/src/dtoolutil/executionEnvironment.cxx index a9139a050b..c2e5594f06 100644 --- a/dtool/src/dtoolutil/executionEnvironment.cxx +++ b/dtool/src/dtoolutil/executionEnvironment.cxx @@ -460,10 +460,14 @@ read_args() { } #endif -#if defined(HAVE_PROC_SELF_MAPS) +#if defined(HAVE_PROC_SELF_MAPS) || defined(HAVE_PROC_CURPROC_MAP) // This is how you tell whether or not libdtool.so is loaded, // and if so, where it was loaded from. +#ifdef HAVE_PROC_CURPROC_MAP + pifstream maps("/proc/curproc/map"); +#else pifstream maps("/proc/self/maps"); +#endif while (!maps.fail() && !maps.eof()) { char buffer[PATH_MAX]; buffer[0] = 0; @@ -502,11 +506,15 @@ read_args() { } #endif // WIN32_VC -#if defined(HAVE_PROC_SELF_EXE) +#if defined(HAVE_PROC_SELF_EXE) || defined(HAVE_PROC_CURPROC_FILE) // This is more reliable than using (argc,argv), so it given precedence. if (_binary_name.empty()) { char readlinkbuf[PATH_MAX]; +#ifdef HAVE_PROC_CURPROC_FILE + int pathlen = readlink("/proc/curproc/file",readlinkbuf,PATH_MAX-1); +#else int pathlen = readlink("/proc/self/exe",readlinkbuf,PATH_MAX-1); +#endif if (pathlen > 0) { readlinkbuf[pathlen] = 0; _binary_name = readlinkbuf; @@ -538,18 +546,26 @@ read_args() { _args.push_back(GLOBAL_ARGV[i]); } -#elif defined(HAVE_PROC_SELF_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 // able to use the global ARGC/ARGV variables at static init time. // However, we may be lucky and have a file called // /proc/self/cmdline that may be read to determine all of our // command-line arguments. +#ifdef HAVE_PROC_CURPROC_CMDLINE + pifstream proc("/proc/curproc/cmdline"); + if (proc.fail()) { + cerr << "Cannot read /proc/curproc/cmdline; command-line arguments unavailable to config.\n"; + return; + } +#else pifstream proc("/proc/self/cmdline"); if (proc.fail()) { cerr << "Cannot read /proc/self/cmdline; command-line arguments unavailable to config.\n"; return; } +#endif int ch = proc.get(); int index = 0; diff --git a/makepanda/makepanda.py b/makepanda/makepanda.py index ba47f01621..339eca5ed8 100755 --- a/makepanda/makepanda.py +++ b/makepanda/makepanda.py @@ -994,6 +994,9 @@ DTOOL_CONFIG=[ ("HAVE_PROC_SELF_MAPS", 'UNDEF', '1'), ("HAVE_PROC_SELF_ENVIRON", 'UNDEF', '1'), ("HAVE_PROC_SELF_CMDLINE", 'UNDEF', '1'), + ("HAVE_PROC_CURPROC_FILE", 'UNDEF', 'UNDEF'), + ("HAVE_PROC_CURPROC_MAP", 'UNDEF', 'UNDEF'), + ("HAVE_PROC_SELF_CMDLINE", 'UNDEF', 'UNDEF'), ("HAVE_GLOBAL_ARGV", '1', 'UNDEF'), ("PROTOTYPE_GLOBAL_ARGV", 'UNDEF', 'UNDEF'), ("GLOBAL_ARGV", '__argv', 'UNDEF'), @@ -1112,6 +1115,12 @@ def WriteConfigSettings(): dtool_config["HAVE_CGGL"] = '1' dtool_config["HAVE_CGDX9"] = '1' + if (not sys.platform.startswith("linux")): + dtool_config["HAVE_PROC_SELF_EXE"] = 'UNDEF' + dtool_config["HAVE_PROC_SELF_MAPS"] = 'UNDEF' + dtool_config["HAVE_PROC_SELF_CMDLINE"] = 'UNDEF' + dtool_config["HAVE_PROC_SELF_ENVIRON"] = 'UNDEF' + if (sys.platform == "darwin"): dtool_config["PYTHON_FRAMEWORK"] = 'Python' dtool_config["HAVE_MALLOC_H"] = 'UNDEF' @@ -1121,16 +1130,15 @@ def WriteConfigSettings(): dtool_config["HAVE_XF86DGA"] = 'UNDEF' dtool_config["IS_LINUX"] = 'UNDEF' dtool_config["IS_OSX"] = '1' - dtool_config["HAVE_PROC_SELF_EXE"] = 'UNDEF' - dtool_config["HAVE_PROC_SELF_MAPS"] = 'UNDEF' - dtool_config["HAVE_PROC_SELF_CMDLINE"] = 'UNDEF' - dtool_config["HAVE_PROC_SELF_ENVIRON"] = 'UNDEF' if (sys.platform.startswith("freebsd")): dtool_config["IS_LINUX"] = 'UNDEF' dtool_config["IS_FREEBSD"] = '1' dtool_config["HAVE_ALLOCA_H"] = 'UNDEF' dtool_config["HAVE_MALLOC_H"] = 'UNDEF' + dtool_config["HAVE_PROC_CURPROC_FILE"] = '1' + dtool_config["HAVE_PROC_CURPROC_MAP"] = '1' + dtool_config["HAVE_PROC_CURPROC_CMDLINE"] = '1' if (OPTIMIZE <= 3): if (dtool_config["HAVE_NET"] != 'UNDEF'):