Browse Source

lsfd: move the code for reading /proc/devices to lsfd.c

The original code is only for reading the names of character device
drivers. For making the code reusable in reading that of block device
drivers, rearrange the code an move it to the common area, lsfd.c.

Signed-off-by: Masatake YAMATO <yamato@redhat.com>
pull/1530/head
Masatake YAMATO 8 months ago
parent
commit
3c0074005b
  1. 86
      misc-utils/lsfd-cdev.c
  2. 79
      misc-utils/lsfd.c

86
misc-utils/lsfd-cdev.c

@ -25,15 +25,8 @@
#include "lsfd.h"
static struct list_head chrdrvs;
static struct list_head miscdevs;
struct chrdrv {
struct list_head chrdrvs;
unsigned long major;
char *name;
};
struct miscdev {
struct list_head miscdevs;
unsigned long minor;
@ -47,7 +40,7 @@ static bool cdev_fill_column(struct proc *proc __attribute__((__unused__)),
size_t column_index)
{
char *str = NULL;
const char *chrdrv;
const char *devdrv;
const char *miscdev;
switch(column_id) {
@ -56,8 +49,8 @@ static bool cdev_fill_column(struct proc *proc __attribute__((__unused__)),
err(EXIT_FAILURE, _("failed to add output data"));
return true;
case COL_MISCDEV:
chrdrv = get_chrdrv(major(file->stat.st_rdev));
if (chrdrv && strcmp(chrdrv, "misc") == 0) {
devdrv = get_chrdrv(major(file->stat.st_rdev));
if (devdrv && strcmp(devdrv, "misc") == 0) {
miscdev = get_miscdev(minor(file->stat.st_rdev));
if (miscdev)
str = strdup(miscdev);
@ -73,23 +66,23 @@ static bool cdev_fill_column(struct proc *proc __attribute__((__unused__)),
err(EXIT_FAILURE, _("failed to add output data"));
return true;
case COL_CHRDRV:
chrdrv = get_chrdrv(major(file->stat.st_rdev));
if (chrdrv)
str = strdup(chrdrv);
devdrv = get_chrdrv(major(file->stat.st_rdev));
if (devdrv)
str = strdup(devdrv);
else
xasprintf(&str, "%u",
major(file->stat.st_rdev));
break;
case COL_SOURCE:
chrdrv = get_chrdrv(major(file->stat.st_rdev));
devdrv = get_chrdrv(major(file->stat.st_rdev));
miscdev = NULL;
if (chrdrv && strcmp(chrdrv, "misc") == 0)
if (devdrv && strcmp(devdrv, "misc") == 0)
miscdev = get_miscdev(minor(file->stat.st_rdev));
if (chrdrv) {
if (devdrv) {
if (miscdev) {
xasprintf(&str, "misc:%s", miscdev);
} else {
xasprintf(&str, "%s:%u", chrdrv,
xasprintf(&str, "%s:%u", devdrv,
minor(file->stat.st_rdev));
}
break;
@ -111,45 +104,6 @@ static bool cdev_fill_column(struct proc *proc __attribute__((__unused__)),
return true;
}
static struct chrdrv *new_chrdrv(unsigned long major, const char *name)
{
struct chrdrv *chrdrv = xcalloc(1, sizeof(*chrdrv));
INIT_LIST_HEAD(&chrdrv->chrdrvs);
chrdrv->major = major;
chrdrv->name = xstrdup(name);
return chrdrv;
}
static void free_chrdrv(struct chrdrv *chrdrv)
{
free(chrdrv->name);
free(chrdrv);
}
static void read_devices(struct list_head *chrdrvs_list, FILE *devices_fp)
{
unsigned long major;
char line[256];
char name[sizeof(line)];
while (fgets(line, sizeof(line), devices_fp)) {
struct chrdrv *chrdrv;
if (line[0] == 'C')
continue; /* "Character devices:" */
else if (line[0] == '\n')
break;
if (sscanf(line, "%lu %s", &major, name) != 2)
continue;
chrdrv = new_chrdrv(major, name);
list_add_tail(&chrdrv->chrdrvs, chrdrvs_list);
}
}
static struct miscdev *new_miscdev(unsigned long minor, const char *name)
{
struct miscdev *miscdev = xcalloc(1, sizeof(*miscdev));
@ -187,18 +141,10 @@ static void read_misc(struct list_head *miscdevs_list, FILE *misc_fp)
static void cdev_class_initialize(void)
{
FILE *devices_fp;
FILE *misc_fp;
INIT_LIST_HEAD(&chrdrvs);
INIT_LIST_HEAD(&miscdevs);
devices_fp = fopen("/proc/devices", "r");
if (devices_fp) {
read_devices(&chrdrvs, devices_fp);
fclose(devices_fp);
}
misc_fp = fopen("/proc/misc", "r");
if (misc_fp) {
read_misc(&miscdevs, misc_fp);
@ -208,21 +154,9 @@ static void cdev_class_initialize(void)
static void cdev_class_finalize(void)
{
list_free(&chrdrvs, struct chrdrv, chrdrvs, free_chrdrv);
list_free(&miscdevs, struct miscdev, miscdevs, free_miscdev);
}
const char *get_chrdrv(unsigned long major)
{
struct list_head *c;
list_for_each(c, &chrdrvs) {
struct chrdrv *chrdrv = list_entry(c, struct chrdrv, chrdrvs);
if (chrdrv->major == major)
return chrdrv->name;
}
return NULL;
}
const char *get_miscdev(unsigned long minor)
{
struct list_head *c;

79
misc-utils/lsfd.c

@ -80,6 +80,17 @@ struct name_manager {
unsigned long next_id;
};
/*
* /proc/devices entries
*/
struct devdrv {
struct list_head devdrvs;
unsigned long major;
char *name;
};
static struct list_head chrdrvs;
/*
* Column related stuffs
*/
@ -907,7 +918,73 @@ static void finalize_classes(void)
finalize_class(&unkn_class);
}
static struct devdrv *new_devdrv(unsigned long major, const char *name)
{
struct devdrv *devdrv = xcalloc(1, sizeof(*devdrv));
INIT_LIST_HEAD(&devdrv->devdrvs);
devdrv->major = major;
devdrv->name = xstrdup(name);
return devdrv;
}
static void free_devdrv(struct devdrv *devdrv)
{
free(devdrv->name);
free(devdrv);
}
static void read_devices(struct list_head *devdrvs_list, FILE *devices_fp)
{
unsigned long major;
char line[256];
char name[sizeof(line)];
while (fgets(line, sizeof(line), devices_fp)) {
struct devdrv *devdrv;
if (line[0] == 'C')
continue; /* "Character devices:" */
else if (line[0] == '\n')
break;
if (sscanf(line, "%lu %s", &major, name) != 2)
continue;
devdrv = new_devdrv(major, name);
list_add_tail(&devdrv->devdrvs, devdrvs_list);
}
}
static void initialize_devdrvs(void)
{
FILE *devices_fp;
INIT_LIST_HEAD(&chrdrvs);
devices_fp = fopen("/proc/devices", "r");
if (devices_fp) {
read_devices(&chrdrvs, devices_fp);
fclose(devices_fp);
}
}
static void finalize_devdrvs(void)
{
list_free(&chrdrvs, struct devdrv, devdrvs, free_devdrv);
}
const char *get_chrdrv(unsigned long major)
{
struct list_head *c;
list_for_each(c, &chrdrvs) {
struct devdrv *devdrv = list_entry(c, struct devdrv, devdrvs);
if (devdrv->major == major)
return devdrv->name;
}
return NULL;
}
struct name_manager *new_name_manager(void)
{
@ -1564,6 +1641,7 @@ int main(int argc, char *argv[])
/* collect data */
initialize_nodevs();
initialize_classes();
initialize_devdrvs();
collect_processes(&ctl, pids, n_pids);
free(pids);
@ -1580,6 +1658,7 @@ int main(int argc, char *argv[])
/* cleanup */
delete(&ctl.procs, &ctl);
finalize_devdrvs();
finalize_classes();
finalize_nodevs();

Loading…
Cancel
Save