]> git.ipfire.org Git - thirdparty/util-linux.git/blame - misc-utils/lsblk-properties.c
Merge branch 'hardlink' of https://github.com/rudimeier/util-linux into hardlink
[thirdparty/util-linux.git] / misc-utils / lsblk-properties.c
CommitLineData
ccafadb7
KZ
1
2#include <blkid.h>
3
4#ifdef HAVE_LIBUDEV
5# include <libudev.h>
6#endif
7
8#include "c.h"
9#include "xalloc.h"
10#include "mangle.h"
11
12#include "lsblk.h"
13
14#ifdef HAVE_LIBUDEV
15static struct udev *udev;
16#endif
17
18void lsblk_device_free_properties(struct lsblk_devprop *p)
19{
20 if (!p)
21 return;
22
23 free(p->fstype);
24 free(p->uuid);
25 free(p->ptuuid);
26 free(p->pttype);
27 free(p->label);
28 free(p->parttype);
29 free(p->partuuid);
30 free(p->partlabel);
31 free(p->wwn);
32 free(p->serial);
33 free(p->model);
34
35 free(p);
36}
37
38#ifndef HAVE_LIBUDEV
fed34a1e 39static struct lsblk_devprop *get_properties_by_udev(struct lsblk_device *dev
ccafadb7
KZ
40 __attribute__((__unused__)))
41{
42 return NULL;
43}
44#else
fed34a1e 45static struct lsblk_devprop *get_properties_by_udev(struct lsblk_device *ld)
ccafadb7
KZ
46{
47 struct udev_device *dev;
48
fed34a1e
KZ
49 if (ld->udev_requested)
50 return ld->properties;
ccafadb7
KZ
51
52 if (lsblk->sysroot)
53 goto done;
54 if (!udev)
55 udev = udev_new(); /* global handler */
56 if (!udev)
57 goto done;
58
fed34a1e 59 dev = udev_device_new_from_subsystem_sysname(udev, "block", ld->name);
ccafadb7
KZ
60 if (dev) {
61 const char *data;
62 struct lsblk_devprop *prop;
63
fed34a1e
KZ
64 if (ld->properties)
65 lsblk_device_free_properties(ld->properties);
66 prop = ld->properties = xcalloc(1, sizeof(*ld->properties));
ccafadb7
KZ
67
68 if ((data = udev_device_get_property_value(dev, "ID_FS_LABEL_ENC"))) {
69 prop->label = xstrdup(data);
70 unhexmangle_string(prop->label);
71 }
72 if ((data = udev_device_get_property_value(dev, "ID_FS_UUID_ENC"))) {
73 prop->uuid = xstrdup(data);
74 unhexmangle_string(prop->uuid);
75 }
76 if ((data = udev_device_get_property_value(dev, "ID_PART_TABLE_UUID")))
77 prop->ptuuid = xstrdup(data);
78 if ((data = udev_device_get_property_value(dev, "ID_PART_TABLE_TYPE")))
79 prop->pttype = xstrdup(data);
80 if ((data = udev_device_get_property_value(dev, "ID_PART_ENTRY_NAME"))) {
81 prop->partlabel = xstrdup(data);
82 unhexmangle_string(prop->partlabel);
83 }
84 if ((data = udev_device_get_property_value(dev, "ID_FS_TYPE")))
85 prop->fstype = xstrdup(data);
86 if ((data = udev_device_get_property_value(dev, "ID_PART_ENTRY_TYPE")))
87 prop->parttype = xstrdup(data);
88 if ((data = udev_device_get_property_value(dev, "ID_PART_ENTRY_UUID")))
89 prop->partuuid = xstrdup(data);
90 if ((data = udev_device_get_property_value(dev, "ID_PART_ENTRY_FLAGS")))
91 prop->partflags = xstrdup(data);
92
93 data = udev_device_get_property_value(dev, "ID_WWN_WITH_EXTENSION");
94 if (!data)
95 data = udev_device_get_property_value(dev, "ID_WWN");
96 if (data)
97 prop->wwn = xstrdup(data);
98
99 if ((data = udev_device_get_property_value(dev, "ID_SERIAL_SHORT")))
100 prop->serial = xstrdup(data);
101 if ((data = udev_device_get_property_value(dev, "ID_MODEL")))
102 prop->model = xstrdup(data);
103
104 udev_device_unref(dev);
fed34a1e 105 DBG(DEV, ul_debugobj(ld, "%s: found udev properties", ld->name));
ccafadb7
KZ
106 }
107
108done:
fed34a1e
KZ
109 ld->udev_requested = 1;
110 return ld->properties;
ccafadb7
KZ
111}
112#endif /* HAVE_LIBUDEV */
113
fed34a1e 114static struct lsblk_devprop *get_properties_by_blkid(struct lsblk_device *dev)
ccafadb7
KZ
115{
116 blkid_probe pr = NULL;
117
fed34a1e
KZ
118 if (dev->blkid_requested)
119 return dev->properties;
ccafadb7 120
fed34a1e 121 if (!dev->size)
ccafadb7
KZ
122 goto done;
123 if (getuid() != 0)
124 goto done;; /* no permissions to read from the device */
125
fed34a1e 126 pr = blkid_new_probe_from_filename(dev->filename);
ccafadb7
KZ
127 if (!pr)
128 goto done;
129
130 blkid_probe_enable_superblocks(pr, 1);
131 blkid_probe_set_superblocks_flags(pr, BLKID_SUBLKS_LABEL |
132 BLKID_SUBLKS_UUID |
133 BLKID_SUBLKS_TYPE);
134 blkid_probe_enable_partitions(pr, 1);
135 blkid_probe_set_partitions_flags(pr, BLKID_PARTS_ENTRY_DETAILS);
136
137 if (!blkid_do_safeprobe(pr)) {
138 const char *data = NULL;
139 struct lsblk_devprop *prop;
140
fed34a1e
KZ
141 if (dev->properties)
142 lsblk_device_free_properties(dev->properties);
143 prop = dev->properties = xcalloc(1, sizeof(*dev->properties));
ccafadb7
KZ
144
145 if (!blkid_probe_lookup_value(pr, "TYPE", &data, NULL))
146 prop->fstype = xstrdup(data);
147 if (!blkid_probe_lookup_value(pr, "UUID", &data, NULL))
148 prop->uuid = xstrdup(data);
149 if (!blkid_probe_lookup_value(pr, "PTUUID", &data, NULL))
150 prop->ptuuid = xstrdup(data);
151 if (!blkid_probe_lookup_value(pr, "PTTYPE", &data, NULL))
152 prop->pttype = xstrdup(data);
153 if (!blkid_probe_lookup_value(pr, "LABEL", &data, NULL))
154 prop->label = xstrdup(data);
155 if (!blkid_probe_lookup_value(pr, "PART_ENTRY_TYPE", &data, NULL))
156 prop->parttype = xstrdup(data);
157 if (!blkid_probe_lookup_value(pr, "PART_ENTRY_UUID", &data, NULL))
158 prop->partuuid = xstrdup(data);
159 if (!blkid_probe_lookup_value(pr, "PART_ENTRY_NAME", &data, NULL))
160 prop->partlabel = xstrdup(data);
161 if (!blkid_probe_lookup_value(pr, "PART_ENTRY_FLAGS", &data, NULL))
162 prop->partflags = xstrdup(data);
163
fed34a1e 164 DBG(DEV, ul_debugobj(dev, "%s: found blkid properties", dev->name));
ccafadb7
KZ
165 }
166
167done:
168 blkid_free_probe(pr);
169
fed34a1e
KZ
170 dev->blkid_requested = 1;
171 return dev->properties;
ccafadb7
KZ
172}
173
fed34a1e 174struct lsblk_devprop *lsblk_device_get_properties(struct lsblk_device *dev)
ccafadb7 175{
fed34a1e 176 struct lsblk_devprop *p = get_properties_by_udev(dev);
ccafadb7
KZ
177
178 if (!p)
fed34a1e 179 p = get_properties_by_blkid(dev);
ccafadb7
KZ
180 return p;
181}
182
183void lsblk_properties_deinit(void)
184{
185#ifdef HAVE_LIBUDEV
186 udev_unref(udev);
187#endif
188}