* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
* USA
*
- * $Id: unix.c 26180 2008-11-11 00:00:12Z androsyn $
*/
-#include <libratbox_config.h>
-#include <ratbox_lib.h>
+#include <librb_config.h>
+#include <rb_lib.h>
#ifndef _WIN32
#include <sys/wait.h>
-
-#if defined(HAVE_SPAWN_H) && defined(HAVE_POSIX_SPAWN)
-#include <spawn.h>
+#ifdef HAVE_DLINFO
+# include <link.h>
+# include <dlfcn.h>
+#endif
#ifdef __APPLE__
+#include <mach-o/dyld.h>
#include <crt_externs.h>
#endif
+#if defined(__FreeBSD__) || defined(__DragonFly__)
+#include <sys/types.h>
+#include <sys/sysctl.h>
+#endif
+
+#if defined(HAVE_SPAWN_H) && defined(HAVE_POSIX_SPAWN)
+#include <spawn.h>
+
#ifndef __APPLE__
extern char **environ;
#endif
return getpid();
}
+const char *
+rb_path_to_self(void)
+{
+ static char path_buf[4096];
+ size_t path_len = sizeof(path_buf);
+#if defined(HAVE_GETEXECNAME)
+ char *s = getexecname();
+ if (s == NULL)
+ return NULL;
+ realpath(s, path_buf);
+ return path_buf;
+#elif defined(__linux__) || (defined(__FreeBSD__) && !defined(KERN_PROC_PATHNAME))
+ if (readlink("/proc/self/exe", path_buf, path_len) != -1)
+ return path_buf;
+ return NULL;
+#elif defined(__FreeBSD__) || defined(__DragonFly__)
+ int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 };
+ if (sysctl(mib, 4, path_buf, &path_len, NULL, 0) == 0)
+ return path_buf;
+ return NULL;
+#elif defined(HAVE_DLINFO)
+ struct link_map *map = NULL;
+ dlinfo(RTLD_SELF, RTLD_DI_LINKMAP, &map);
+ if (map == NULL)
+ return NULL;
+ realpath(map->l_name, path_buf);
+ return path_buf;
+#elif defined(__APPLE__)
+ char tmp_path[4096];
+ uint32_t pathlen = 4096;
+
+ if (_NSGetExecutablePath(tmp_path, &pathlen) < 0)
+ return NULL;
+
+ realpath(tmp_path, path_buf);
+ return path_buf;
+#else
+ return NULL;
+#endif
+}
#endif /* !WIN32 */