make sigar_main more portable

This commit is contained in:
Doug MacEachern 2005-12-31 18:29:55 +00:00
parent 831a7c3f23
commit 7820b451cc
2 changed files with 71 additions and 16 deletions

View File

@ -206,9 +206,8 @@
</target> </target>
<target name="sigar-exe-cpptasks"> <target name="sigar-exe-cpptasks">
<!-- XXX fuckers complain about '.' in libname -->
<cc outtype="executable" <cc outtype="executable"
outfile="${jni.objdir}/lib/${jni.libname}" outfile="${jni.bin}/lib/${jni.libname}"
objdir="${jni.objdir}"> objdir="${jni.objdir}">
<compiler name="${jni.compiler}" debug="${jni.debug}"/> <compiler name="${jni.compiler}" debug="${jni.debug}"/>
@ -218,6 +217,8 @@
libs="${jni.libname}"/> libs="${jni.libname}"/>
</linker> </linker>
<includepath location="../../include"/>
<fileset dir="${jni.source.dir}"> <fileset dir="${jni.source.dir}">
<include name="src/sigar_main.c"/> <include name="src/sigar_main.c"/>
</fileset> </fileset>

View File

@ -1,13 +1,31 @@
#define WIN32
#if defined(WIN32)
#define WINDOWS_LEAN_AND_MEAN
#define FILESEP '\\'
#define LIBPRE ""
#include "windows.h"
#else
#define FILESEP '/'
#define LIBPRE "lib"
#include <dlfcn.h> #include <dlfcn.h>
#include <strings.h>
#include <unistd.h>
#endif
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <strings.h>
#include <sys/types.h> #include <sys/types.h>
#include <unistd.h>
#include "sigar.h" #include "sigar.h"
#include "sigar_fileinfo.h" #include "sigar_fileinfo.h"
#if defined(WIN32)
# define LIBEXT "dll"
#elif defined(DARWIN)
# define LIBEXT "dylib"
#else
# define LIBEXT "so"
#endif
typedef struct { typedef struct {
const char *name; const char *name;
void *func; void *func;
@ -31,23 +49,23 @@ struct {
struct { struct {
const char *name; const char *name;
int (*func)(sigar_t *, pid_t, sigar_proc_exe_t *); int (*func)(sigar_t *, sigar_pid_t, sigar_proc_exe_t *);
} proc_exe; } proc_exe;
struct { struct {
const char *name; const char *name;
int (*func)(sigar_t *, pid_t, sigar_proc_args_t *); int (*func)(sigar_t *, sigar_pid_t, sigar_proc_args_t *);
} proc_args; } proc_args;
struct { struct {
const char *name; const char *name;
int (*func)(sigar_t *, pid_t, sigar_proc_env_t *); int (*func)(sigar_t *, sigar_pid_t, sigar_proc_env_t *);
} proc_env; } proc_env;
struct { struct {
const char *name; const char *name;
int (*func)(sigar_t *, pid_t, sigar_proc_modules_t *); int (*func)(sigar_t *, sigar_pid_t, sigar_proc_modules_t *);
} proc_modules; } proc_modules;
struct { struct {
const char *name; const char *name;
int (*func)(sigar_t *, pid_t, sigar_proc_fd_t *); int (*func)(sigar_t *, sigar_pid_t, sigar_proc_fd_t *);
} proc_fd; } proc_fd;
struct { struct {
const char *name; const char *name;
@ -82,6 +100,7 @@ static int sigar_main(char *argv0)
{ {
int status; int status;
sigar_t *sigar; sigar_t *sigar;
char *errmsg;
char *ptr; char *ptr;
char sigarlib[8096], archlib[512]; char sigarlib[8096], archlib[512];
void *handle; void *handle;
@ -89,35 +108,67 @@ static int sigar_main(char *argv0)
(sigar_callback_t *)&sigar_callbacks; (sigar_callback_t *)&sigar_callbacks;
strcpy(sigarlib, argv0); strcpy(sigarlib, argv0);
ptr = rindex(sigarlib, '/'); #ifdef WIN32
if ((ptr = strstr(sigarlib, ".exe"))) {
*ptr = '\0';
}
#endif
ptr = strrchr(sigarlib, FILESEP);
if (ptr) { if (ptr) {
++ptr; ++ptr;
sprintf(archlib, "lib%s.so", ptr);
strcpy(ptr, archlib);
} }
else {
ptr = sigarlib;
}
sprintf(archlib, LIBPRE "%s." LIBEXT, ptr);
strcpy(ptr, archlib);
#if defined(__sun) #if defined(__sun)
dlopen("/usr/lib/libnsl.so", RTLD_NOW|RTLD_GLOBAL); dlopen("/usr/lib/libnsl.so", RTLD_NOW|RTLD_GLOBAL);
#endif #endif
#if defined(WIN32)
if (!(handle = LoadLibrary(sigarlib))) {
errmsg = "XXX FormatMessage";
}
#else
if (!(handle = dlopen(sigarlib, RTLD_LAZY))) { if (!(handle = dlopen(sigarlib, RTLD_LAZY))) {
errmsg = dlerror();
}
#endif
if (!handle) {
fprintf(stderr, "Error opening '%s': %s\n", fprintf(stderr, "Error opening '%s': %s\n",
sigarlib, dlerror()); sigarlib, errmsg);
exit(1); exit(1);
} }
while (callbacks->name) { while (callbacks->name) {
#if defined(WIN32)
callbacks->func = GetProcAddress(handle, callbacks->name);
#else
callbacks->func = dlsym(handle, callbacks->name); callbacks->func = dlsym(handle, callbacks->name);
#endif
callbacks++; callbacks++;
} }
if (isatty(fileno(stdin))) { if (isatty(fileno(stdin))) {
sigar_version_t *version = sigar_version_t *version;
sigar_callbacks.version.func();
if (!sigar_callbacks.version.func) {
exit(1);
}
version = sigar_callbacks.version.func();
printf("version=%s, build date=%s\n", printf("version=%s, build date=%s\n",
version->version, version->build_date); version->version, version->build_date);
#if defined(WIN32)
FreeLibrary(handle);
#else
dlclose(handle); dlclose(handle);
#endif
exit(0); exit(0);
} }
@ -131,8 +182,11 @@ static int sigar_main(char *argv0)
/* XXX pipe loop */ /* XXX pipe loop */
sigar_callbacks.close.func(sigar); sigar_callbacks.close.func(sigar);
#if defined(WIN32)
FreeLibrary(handle);
#else
dlclose(handle); dlclose(handle);
#endif
return 0; return 0;
} }