summaryrefslogtreecommitdiffstats
path: root/misc-utils/lsfd-bdev.c
blob: b2fecdc8dc9687fb0fb5fab2bb71f0d89a9ab4f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
 * lsfd-bdev.c - handle associations opening block devices
 *
 * Copyright (C) 2021 Red Hat, Inc. All rights reserved.
 * Written by Masatake YAMATO <yamato@redhat.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it would 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
 */

#include "xalloc.h"
#include "nls.h"
#include "libsmartcols.h"

#include "lsfd.h"

static struct list_head partitions;

struct partition {
	struct list_head partitions;
	dev_t dev;
	char *name;
};

static bool bdev_fill_column(struct proc *proc __attribute__((__unused__)),
			     struct file *file __attribute__((__unused__)),
			     struct libscols_line *ln,
			     int column_id,
			     size_t column_index)
{
	char *str = NULL;
	const char *partition, *devdrv;

	switch(column_id) {
	case COL_TYPE:
		if (scols_line_set_data(ln, column_index, "BLK"))
			err(EXIT_FAILURE, _("failed to add output data"));
		return true;
	case COL_BLKDRV:
		devdrv = get_blkdrv(major(file->stat.st_rdev));
		if (devdrv)
			str = strdup(devdrv);
		else
			xasprintf(&str, "%u",
				  major(file->stat.st_rdev));
		break;
	case COL_DEVTYPE:
		if (scols_line_set_data(ln, column_index,
					"blk"))
			err(EXIT_FAILURE, _("failed to add output data"));
		return true;
	case COL_SOURCE:
	case COL_PARTITION:
		partition = get_partition(file->stat.st_rdev);
		if (partition) {
			str = strdup(partition);
			break;
		}
		devdrv = get_blkdrv(major(file->stat.st_rdev));
		if (devdrv) {
			xasprintf(&str, "%s:%u", devdrv,
				  minor(file->stat.st_rdev));
			break;
		}
		/* FALL THROUGH */
	case COL_MAJMIN:
		xasprintf(&str, "%u:%u",
			  major(file->stat.st_rdev),
			  minor(file->stat.st_rdev));
		break;
	default:
		return false;
	}

	if (!str)
		err(EXIT_FAILURE, _("failed to add output data"));
	if (scols_line_refer_data(ln, column_index, str))
		err(EXIT_FAILURE, _("failed to add output data"));
	return true;
}

static struct partition *new_partition(dev_t dev, const char *name)
{
	struct partition *partition = xcalloc(1, sizeof(*partition));

	INIT_LIST_HEAD(&partition->partitions);

	partition->dev = dev;
	partition->name = xstrdup(name);

	return partition;
}

static void free_partition(struct partition *partition)
{
	free(partition->name);
	free(partition);
}

static void read_partitions(struct list_head *partitions_list, FILE *part_fp)
{
	unsigned int major, minor;
	char line[256];
	char name[sizeof(line)];

	while (fgets(line, sizeof(line), part_fp)) {
		struct partition *partition;

		if (sscanf(line, "%u %u %*u %s", &major, &minor, name) != 3)
			continue;
		partition = new_partition(makedev(major, minor), name);
		list_add_tail(&partition->partitions, partitions_list);
	}
}

static void bdev_class_initialize(void)
{
	FILE *part_fp;

	INIT_LIST_HEAD(&partitions);

	part_fp = fopen("/proc/partitions", "r");
	if (part_fp) {
		read_partitions(&partitions, part_fp);
		fclose(part_fp);
	}
}

static void bdev_class_finalize(void)
{
	list_free(&partitions, struct partition,  partitions, free_partition);
}

const char *get_partition(dev_t dev)
{
	struct list_head *p;
	list_for_each(p, &partitions) {
		struct partition *partition = list_entry(p, struct partition, partitions);
		if (partition->dev == dev)
			return partition->name;
	}
	return NULL;
}

const struct file_class bdev_class = {
	.super = &file_class,
	.size = sizeof(struct file),
	.initialize_class = bdev_class_initialize,
	.finalize_class = bdev_class_finalize,
	.fill_column = bdev_fill_column,
	.free_content = NULL,
};