(SIGAR-139) support scripted SIGAR_PROC_NET for testing

This commit is contained in:
Doug MacEachern 2009-03-12 21:30:50 -07:00
parent 1ce9a3209d
commit 3025c4554f
1 changed files with 32 additions and 3 deletions

View File

@ -1848,6 +1848,34 @@ static int proc_net_walker(sigar_net_connection_walker_t *walker,
while(*p && (*p != c)) p++; \ while(*p && (*p != c)) p++; \
SKIP_WHILE(p, c) SKIP_WHILE(p, c)
typedef struct {
FILE *fp;
int (*close)(FILE *);
} xproc_t;
static FILE *xproc_open(const char *command, xproc_t *xproc)
{
struct stat sb;
if (stat(command, &sb) == 0) {
if (sb.st_mode & S_IXUSR) {
/* executable script for testing large
* conn table where we can sleep() to better
* simulate /proc/net/tcp behavior
*/
xproc->fp = popen(command, "r");
xproc->close = pclose;
}
else {
xproc->fp = fopen(command, "r");
xproc->close = fclose;
}
return xproc->fp;
}
else {
return NULL;
}
}
static int proc_net_read(sigar_net_connection_walker_t *walker, static int proc_net_read(sigar_net_connection_walker_t *walker,
const char *fname, const char *fname,
int type) int type)
@ -1857,13 +1885,14 @@ static int proc_net_read(sigar_net_connection_walker_t *walker,
sigar_t *sigar = walker->sigar; sigar_t *sigar = walker->sigar;
char *ptr = sigar->proc_net; char *ptr = sigar->proc_net;
int flags = walker->flags; int flags = walker->flags;
xproc_t xproc = { NULL, fclose };
if (ptr) { if (ptr) {
snprintf(buffer, sizeof(buffer), snprintf(buffer, sizeof(buffer),
"%s/%s", ptr, "%s/%s", ptr,
fname + sizeof(PROC_FS_ROOT)-1); fname + sizeof(PROC_FS_ROOT)-1);
if ((fp = fopen(buffer, "r"))) { if ((fp = xproc_open(buffer, &xproc))) {
if (SIGAR_LOG_IS_DEBUG(sigar)) { if (SIGAR_LOG_IS_DEBUG(sigar)) {
sigar_log_printf(sigar, SIGAR_LOG_DEBUG, sigar_log_printf(sigar, SIGAR_LOG_DEBUG,
"[proc_net] using %s", "[proc_net] using %s",
@ -1955,12 +1984,12 @@ static int proc_net_read(sigar_net_connection_walker_t *walker,
more = walker->add_connection(walker, &conn); more = walker->add_connection(walker, &conn);
if (more != SIGAR_OK) { if (more != SIGAR_OK) {
fclose(fp); xproc.close(fp);
return SIGAR_OK; return SIGAR_OK;
} }
} }
fclose(fp); xproc.close(fp);
return SIGAR_OK; return SIGAR_OK;
} }