]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd/sd-device/device-util.c
Merge pull request #30513 from rpigott/resolved-ede
[thirdparty/systemd.git] / src / libsystemd / sd-device / device-util.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "device-private.h"
4 #include "device-util.h"
5 #include "devnum-util.h"
6 #include "fd-util.h"
7 #include "string-util.h"
8 #include "strv.h"
9
10 int devname_from_devnum(mode_t mode, dev_t devnum, char **ret) {
11 _cleanup_(sd_device_unrefp) sd_device *dev = NULL;
12 _cleanup_free_ char *s = NULL;
13 const char *devname;
14 int r;
15
16 assert(ret);
17
18 if (devnum_is_zero(devnum))
19 return device_path_make_inaccessible(mode, ret);
20
21 r = device_new_from_mode_and_devnum(&dev, mode, devnum);
22 if (r < 0)
23 return r;
24
25 r = sd_device_get_devname(dev, &devname);
26 if (r < 0)
27 return r;
28
29 s = strdup(devname);
30 if (!s)
31 return -ENOMEM;
32
33 *ret = TAKE_PTR(s);
34 return 0;
35 }
36
37 int device_open_from_devnum(mode_t mode, dev_t devnum, int flags, char **ret) {
38 _cleanup_(sd_device_unrefp) sd_device *dev = NULL;
39 _cleanup_close_ int fd = -EBADF;
40 int r;
41
42 r = device_new_from_mode_and_devnum(&dev, mode, devnum);
43 if (r < 0)
44 return r;
45
46 fd = sd_device_open(dev, flags);
47 if (fd < 0)
48 return fd;
49
50 if (ret) {
51 const char *devname;
52 char *s;
53
54 r = sd_device_get_devname(dev, &devname);
55 if (r < 0)
56 return r;
57
58 s = strdup(devname);
59 if (!s)
60 return -ENOMEM;
61
62 *ret = s;
63 }
64
65 return TAKE_FD(fd);
66 }
67
68 static int add_string_field(
69 sd_device *device,
70 const char *field,
71 int (*func)(sd_device *dev, const char **s),
72 char ***strv) {
73
74 const char *s;
75 int r;
76
77 assert(device);
78 assert(field);
79 assert(func);
80 assert(strv);
81
82 r = func(device, &s);
83 if (r < 0 && r != -ENOENT)
84 log_device_debug_errno(device, r, "Failed to get device \"%s\" property, ignoring: %m", field);
85 if (r >= 0)
86 (void) strv_extend_assignment(strv, field, s);
87
88 return 0;
89 }
90
91 char** device_make_log_fields(sd_device *device) {
92 _cleanup_strv_free_ char **strv = NULL;
93 dev_t devnum;
94 int ifindex;
95 sd_device_action_t action;
96 uint64_t seqnum, diskseq;
97 int r;
98
99 assert(device);
100
101 (void) add_string_field(device, "SYSPATH", sd_device_get_syspath, &strv);
102 (void) add_string_field(device, "SUBSYSTEM", sd_device_get_subsystem, &strv);
103 (void) add_string_field(device, "DEVTYPE", sd_device_get_devtype, &strv);
104 (void) add_string_field(device, "DRIVER", sd_device_get_driver, &strv);
105 (void) add_string_field(device, "DEVPATH", sd_device_get_devpath, &strv);
106 (void) add_string_field(device, "DEVNAME", sd_device_get_devname, &strv);
107 (void) add_string_field(device, "SYSNAME", sd_device_get_sysname, &strv);
108 (void) add_string_field(device, "SYSNUM", sd_device_get_sysnum, &strv);
109
110 r = sd_device_get_devnum(device, &devnum);
111 if (r < 0 && r != -ENOENT)
112 log_device_debug_errno(device, r, "Failed to get device \"DEVNUM\" property, ignoring: %m");
113 if (r >= 0)
114 (void) strv_extendf(&strv, "DEVNUM="DEVNUM_FORMAT_STR, DEVNUM_FORMAT_VAL(devnum));
115
116 r = sd_device_get_ifindex(device, &ifindex);
117 if (r < 0 && r != -ENOENT)
118 log_device_debug_errno(device, r, "Failed to get device \"IFINDEX\" property, ignoring: %m");
119 if (r >= 0)
120 (void) strv_extendf(&strv, "IFINDEX=%i", ifindex);
121
122 r = sd_device_get_action(device, &action);
123 if (r < 0 && r != -ENOENT)
124 log_device_debug_errno(device, r, "Failed to get device \"ACTION\" property, ignoring: %m");
125 if (r >= 0)
126 (void) strv_extendf(&strv, "ACTION=%s", device_action_to_string(action));
127
128 r = sd_device_get_seqnum(device, &seqnum);
129 if (r < 0 && r != -ENOENT)
130 log_device_debug_errno(device, r, "Failed to get device \"SEQNUM\" property, ignoring: %m");
131 if (r >= 0)
132 (void) strv_extendf(&strv, "SEQNUM=%"PRIu64, seqnum);
133
134 r = sd_device_get_diskseq(device, &diskseq);
135 if (r < 0 && r != -ENOENT)
136 log_device_debug_errno(device, r, "Failed to get device \"DISKSEQ\" property, ignoring: %m");
137 if (r >= 0)
138 (void) strv_extendf(&strv, "DISKSEQ=%"PRIu64, diskseq);
139
140 return TAKE_PTR(strv);
141 }
142
143 bool device_in_subsystem(sd_device *device, const char *subsystem) {
144 const char *s = NULL;
145
146 assert(device);
147
148 (void) sd_device_get_subsystem(device, &s);
149 return streq_ptr(s, subsystem);
150 }
151
152 bool device_is_devtype(sd_device *device, const char *devtype) {
153 const char *s = NULL;
154
155 assert(device);
156
157 (void) sd_device_get_devtype(device, &s);
158 return streq_ptr(s, devtype);
159 }