2006-07-16 01:46:36 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) [2004, 2005, 2006], Hyperic, Inc.
|
|
|
|
* This file is part of SIGAR.
|
|
|
|
*
|
|
|
|
* SIGAR is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms version 2 of the GNU General Public License as
|
|
|
|
* published by the Free Software Foundation. This program is distributed
|
|
|
|
* in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
|
|
|
|
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A
|
|
|
|
* PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
|
|
* details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
|
|
|
* USA.
|
|
|
|
*/
|
|
|
|
|
2006-01-01 02:29:55 +08:00
|
|
|
#define WIN32
|
|
|
|
#if defined(WIN32)
|
|
|
|
#define WINDOWS_LEAN_AND_MEAN
|
|
|
|
#define FILESEP '\\'
|
|
|
|
#define LIBPRE ""
|
|
|
|
#include "windows.h"
|
|
|
|
#else
|
|
|
|
#define FILESEP '/'
|
|
|
|
#define LIBPRE "lib"
|
2005-12-15 13:36:34 +08:00
|
|
|
#include <dlfcn.h>
|
2006-01-01 02:29:55 +08:00
|
|
|
#include <strings.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
2005-12-15 13:36:34 +08:00
|
|
|
#include <stdio.h>
|
2005-12-16 04:22:01 +08:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2005-12-15 13:36:34 +08:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include "sigar.h"
|
|
|
|
#include "sigar_fileinfo.h"
|
|
|
|
|
2006-01-01 02:29:55 +08:00
|
|
|
#if defined(WIN32)
|
|
|
|
# define LIBEXT "dll"
|
|
|
|
#elif defined(DARWIN)
|
|
|
|
# define LIBEXT "dylib"
|
|
|
|
#else
|
|
|
|
# define LIBEXT "so"
|
|
|
|
#endif
|
|
|
|
|
2005-12-15 13:36:34 +08:00
|
|
|
typedef struct {
|
|
|
|
const char *name;
|
|
|
|
void *func;
|
|
|
|
} sigar_callback_t;
|
|
|
|
|
|
|
|
struct {
|
|
|
|
struct {
|
|
|
|
const char *name;
|
|
|
|
sigar_version_t * (*func)(void);
|
|
|
|
} version;
|
|
|
|
|
2005-12-16 10:10:16 +08:00
|
|
|
struct {
|
|
|
|
const char *name;
|
|
|
|
int (*func)(sigar_t **);
|
|
|
|
} open;
|
|
|
|
|
|
|
|
struct {
|
|
|
|
const char *name;
|
|
|
|
int (*func)(sigar_t *);
|
|
|
|
} close;
|
|
|
|
|
2005-12-15 13:36:34 +08:00
|
|
|
struct {
|
|
|
|
const char *name;
|
2006-01-01 02:29:55 +08:00
|
|
|
int (*func)(sigar_t *, sigar_pid_t, sigar_proc_exe_t *);
|
2005-12-15 13:36:34 +08:00
|
|
|
} proc_exe;
|
|
|
|
struct {
|
|
|
|
const char *name;
|
2006-01-01 02:29:55 +08:00
|
|
|
int (*func)(sigar_t *, sigar_pid_t, sigar_proc_args_t *);
|
2005-12-15 13:36:34 +08:00
|
|
|
} proc_args;
|
|
|
|
struct {
|
|
|
|
const char *name;
|
2006-01-01 02:29:55 +08:00
|
|
|
int (*func)(sigar_t *, sigar_pid_t, sigar_proc_env_t *);
|
2005-12-15 13:36:34 +08:00
|
|
|
} proc_env;
|
|
|
|
struct {
|
|
|
|
const char *name;
|
2006-01-01 02:29:55 +08:00
|
|
|
int (*func)(sigar_t *, sigar_pid_t, sigar_proc_modules_t *);
|
2005-12-15 13:36:34 +08:00
|
|
|
} proc_modules;
|
|
|
|
struct {
|
|
|
|
const char *name;
|
2006-01-01 02:29:55 +08:00
|
|
|
int (*func)(sigar_t *, sigar_pid_t, sigar_proc_fd_t *);
|
2005-12-15 13:36:34 +08:00
|
|
|
} proc_fd;
|
|
|
|
struct {
|
|
|
|
const char *name;
|
|
|
|
int (*func)(sigar_t *, int, unsigned long, sigar_pid_t *);
|
|
|
|
} proc_port;
|
|
|
|
|
|
|
|
struct {
|
|
|
|
const char *name;
|
|
|
|
int (*func)(sigar_t *, const char *dir, sigar_dir_stat_t *);
|
|
|
|
} dir_stat;
|
|
|
|
struct {
|
|
|
|
const char *name;
|
|
|
|
int (*func)(sigar_t *, const char *dir, sigar_dir_usage_t *);
|
|
|
|
} dir_usage;
|
|
|
|
sigar_callback_t end;
|
|
|
|
} sigar_callbacks = {
|
|
|
|
{ "sigar_version_get", NULL },
|
2005-12-16 10:10:16 +08:00
|
|
|
{ "sigar_open", NULL },
|
|
|
|
{ "sigar_close", NULL },
|
2005-12-15 13:36:34 +08:00
|
|
|
{ "sigar_proc_exe_get", NULL },
|
|
|
|
{ "sigar_proc_args_get", NULL },
|
|
|
|
{ "sigar_proc_env_get", NULL },
|
|
|
|
{ "sigar_proc_modules_get", NULL },
|
|
|
|
{ "sigar_proc_fd_get", NULL },
|
|
|
|
{ "sigar_proc_port_get", NULL },
|
|
|
|
{ "sigar_dir_stat_get", NULL },
|
|
|
|
{ "sigar_dir_usage_get", NULL },
|
|
|
|
{ NULL, NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
static int sigar_main(char *argv0)
|
|
|
|
{
|
2005-12-16 10:10:16 +08:00
|
|
|
int status;
|
|
|
|
sigar_t *sigar;
|
2006-01-01 02:29:55 +08:00
|
|
|
char *errmsg;
|
2005-12-15 13:36:34 +08:00
|
|
|
char *ptr;
|
|
|
|
char sigarlib[8096], archlib[512];
|
|
|
|
void *handle;
|
|
|
|
sigar_callback_t *callbacks =
|
|
|
|
(sigar_callback_t *)&sigar_callbacks;
|
|
|
|
|
|
|
|
strcpy(sigarlib, argv0);
|
2006-01-01 02:29:55 +08:00
|
|
|
#ifdef WIN32
|
|
|
|
if ((ptr = strstr(sigarlib, ".exe"))) {
|
|
|
|
*ptr = '\0';
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
ptr = strrchr(sigarlib, FILESEP);
|
2005-12-15 13:36:34 +08:00
|
|
|
if (ptr) {
|
|
|
|
++ptr;
|
2006-01-01 02:29:55 +08:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
ptr = sigarlib;
|
2005-12-15 13:36:34 +08:00
|
|
|
}
|
|
|
|
|
2006-01-01 02:29:55 +08:00
|
|
|
sprintf(archlib, LIBPRE "%s." LIBEXT, ptr);
|
|
|
|
strcpy(ptr, archlib);
|
|
|
|
|
2005-12-15 13:36:34 +08:00
|
|
|
#if defined(__sun)
|
|
|
|
dlopen("/usr/lib/libnsl.so", RTLD_NOW|RTLD_GLOBAL);
|
|
|
|
#endif
|
|
|
|
|
2006-01-01 02:29:55 +08:00
|
|
|
#if defined(WIN32)
|
|
|
|
if (!(handle = LoadLibrary(sigarlib))) {
|
|
|
|
errmsg = "XXX FormatMessage";
|
|
|
|
}
|
|
|
|
#else
|
2005-12-15 13:36:34 +08:00
|
|
|
if (!(handle = dlopen(sigarlib, RTLD_LAZY))) {
|
2006-01-01 02:29:55 +08:00
|
|
|
errmsg = dlerror();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (!handle) {
|
2005-12-15 13:36:34 +08:00
|
|
|
fprintf(stderr, "Error opening '%s': %s\n",
|
2006-01-01 02:29:55 +08:00
|
|
|
sigarlib, errmsg);
|
2005-12-15 13:36:34 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
while (callbacks->name) {
|
2006-01-01 02:29:55 +08:00
|
|
|
#if defined(WIN32)
|
|
|
|
callbacks->func = GetProcAddress(handle, callbacks->name);
|
|
|
|
#else
|
2005-12-15 13:36:34 +08:00
|
|
|
callbacks->func = dlsym(handle, callbacks->name);
|
2006-01-01 02:29:55 +08:00
|
|
|
#endif
|
2005-12-15 13:36:34 +08:00
|
|
|
callbacks++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isatty(fileno(stdin))) {
|
2006-01-01 02:29:55 +08:00
|
|
|
sigar_version_t *version;
|
|
|
|
|
|
|
|
if (!sigar_callbacks.version.func) {
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
version = sigar_callbacks.version.func();
|
2005-12-15 13:36:34 +08:00
|
|
|
|
|
|
|
printf("version=%s, build date=%s\n",
|
|
|
|
version->version, version->build_date);
|
2006-01-01 02:29:55 +08:00
|
|
|
#if defined(WIN32)
|
|
|
|
FreeLibrary(handle);
|
|
|
|
#else
|
2005-12-15 13:36:34 +08:00
|
|
|
dlclose(handle);
|
2006-01-01 02:29:55 +08:00
|
|
|
#endif
|
2005-12-15 13:36:34 +08:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2005-12-16 10:10:16 +08:00
|
|
|
status = sigar_callbacks.open.func(&sigar);
|
|
|
|
|
|
|
|
if (status != SIGAR_OK) {
|
|
|
|
perror("sigar_open");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* XXX pipe loop */
|
|
|
|
|
|
|
|
sigar_callbacks.close.func(sigar);
|
2006-01-01 02:29:55 +08:00
|
|
|
#if defined(WIN32)
|
|
|
|
FreeLibrary(handle);
|
|
|
|
#else
|
2005-12-15 13:36:34 +08:00
|
|
|
dlclose(handle);
|
2006-01-01 02:29:55 +08:00
|
|
|
#endif
|
2005-12-15 13:36:34 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
if (argc == 1) {
|
|
|
|
return sigar_main(argv[0]);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return 1; /*XXX*/
|
|
|
|
}
|
|
|
|
}
|