Reworked. Should works with IDE, SCSI. DM support is ready, but i don't

* fsusage.c: (get_partition), (get_sys_path), (linux_2_6_0): Reworked.
	Should works with IDE, SCSI. DM support is ready, but i don't know why
	their /stat is empty ...
This commit is contained in:
Benoît Dejean
2005-01-13 08:56:54 +00:00
parent 50e4e07013
commit fcb83e637d
2 changed files with 92 additions and 56 deletions

View File

@@ -1,3 +1,9 @@
2005-01-13 Benoît Dejean <TazForEver@dlfp.org>
* fsusage.c: (get_partition), (get_sys_path), (linux_2_6_0): Reworked.
Should works with IDE, SCSI. DM support is ready, but i don't know why
their /stat is empty ...
2005-01-12 Benoît Dejean <TazForEver@dlfp.org>
* procopenfiles.c: (get_all_sockets),

View File

@@ -5,9 +5,10 @@
#include <glib.h>
#include <mntent.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <linux/kdev_t.h>
#include <stdio.h>
#include <string.h>
@@ -24,54 +25,89 @@ _glibtop_linux_get_fsusage_read_write(glibtop *server,
* linux/Documentation/iostats.txt
*/
static void linux_2_6_0(glibtop *server, glibtop_fsusage *buf, const char *path)
static char *
get_partition(const char *mountpoint)
{
FILE *mtab = setmntent("/etc/mtab", "r");
FILE *partitions;
char *name = NULL;
char line[1024];
struct stat statb;
struct mntent *emnt;
if(stat(mountpoint, &statb) == -1)
return NULL;
while((emnt = getmntent(mtab)) != NULL)
g_return_val_if_fail((partitions = fopen("/proc/partitions", "r")), NULL);
while(fgets(line, sizeof line, partitions))
{
if(strcmp(emnt->mnt_dir, path) == 0)
unsigned major, minor;
char dev[32];
if(sscanf(line, "%u %u %*u %31s", &major, &minor, dev) != 3)
continue;
if(MKDEV(major, minor) != statb.st_dev)
continue;
name = g_strdup(dev);
break;
}
fclose(partitions);
return name;
}
static char *
get_sys_path(const char *device)
{
if(g_str_has_prefix(device, "hd") || g_str_has_prefix(device, "sd"))
{
char filename[64]; /* magic */
char buffer[1024]; /* magic */
char *p;
char device[32]; /* magic */
unsigned partition;
const char *devname = emnt->mnt_fsname;
char *prefix;
char *path;
size_t offset;
/*
we only deal with /dev block devices "/dev/<DEVICE><PARTITION>"
i don't know if other block devices such as network block
devices provide this kind of information.
*/
offset = strcspn(device, "0123456789");
if(!g_str_has_prefix(devname, "/dev/"))
break;
prefix = g_strdup(device);
prefix [offset] = '\0';
/* skip the "/dev/" */
devname += sizeof "/dev/" - 1;
path = g_strdup_printf("/sys/block/%s/%s/stat",
prefix, device);
g_strlcpy(device, devname, sizeof device);
g_free(prefix);
return path;
offset = strcspn(devname, "0123456789");
partition = strtoul(devname + offset, NULL, 0);
device[offset] = '\0';
if((size_t) g_snprintf(filename, sizeof filename,
"/sys/block/%s/%s%u/stat",
device, device, partition) >= sizeof filename)
break;
}
else
{
return g_strdup_printf("/sys/block/%s/stat", device);
}
}
if(try_file_to_buffer(buffer, filename) < 0)
break;
static void linux_2_6_0(glibtop *server, glibtop_fsusage *buf, const char *path)
{
char *device;
char *filename;
int ret;
char buffer[BUFSIZ];
char *p;
device = get_partition(path);
if(!device) return;
filename = get_sys_path(device);
g_free(device);
ret = try_file_to_buffer(buffer, filename);
g_free(filename);
if(ret < 0) return;
p = buffer;
p = skip_token(p);
@@ -80,12 +116,6 @@ static void linux_2_6_0(glibtop *server, glibtop_fsusage *buf, const char *path)
buf->write = strtoull(p, &p, 0);
buf->flags |= (1 << GLIBTOP_FSUSAGE_READ) | (1 << GLIBTOP_FSUSAGE_WRITE);
break;
}
}
endmntent(mtab);
}