1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
3 #include "device-private.h"
4 #include "device-util.h"
5 #include "devnum-util.h"
7 #include "string-util.h"
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
;
18 if (devnum_is_zero(devnum
))
19 return device_path_make_inaccessible(mode
, ret
);
21 r
= device_new_from_mode_and_devnum(&dev
, mode
, devnum
);
25 r
= sd_device_get_devname(dev
, &devname
);
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
;
42 r
= device_new_from_mode_and_devnum(&dev
, mode
, devnum
);
46 fd
= sd_device_open(dev
, flags
);
54 r
= sd_device_get_devname(dev
, &devname
);
68 static int add_string_field(
71 int (*func
)(sd_device
*dev
, const char **s
),
83 if (r
< 0 && r
!= -ENOENT
)
84 log_device_debug_errno(device
, r
, "Failed to get device \"%s\" property, ignoring: %m", field
);
86 (void) strv_extend_assignment(strv
, field
, s
);
91 char** device_make_log_fields(sd_device
*device
) {
92 _cleanup_strv_free_
char **strv
= NULL
;
95 sd_device_action_t action
;
96 uint64_t seqnum
, diskseq
;
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
);
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");
114 (void) strv_extendf(&strv
, "DEVNUM="DEVNUM_FORMAT_STR
, DEVNUM_FORMAT_VAL(devnum
));
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");
120 (void) strv_extendf(&strv
, "IFINDEX=%i", ifindex
);
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");
126 (void) strv_extendf(&strv
, "ACTION=%s", device_action_to_string(action
));
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");
132 (void) strv_extendf(&strv
, "SEQNUM=%"PRIu64
, seqnum
);
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");
138 (void) strv_extendf(&strv
, "DISKSEQ=%"PRIu64
, diskseq
);
140 return TAKE_PTR(strv
);
143 bool device_in_subsystem(sd_device
*device
, const char *subsystem
) {
144 const char *s
= NULL
;
148 (void) sd_device_get_subsystem(device
, &s
);
149 return streq_ptr(s
, subsystem
);
152 bool device_is_devtype(sd_device
*device
, const char *devtype
) {
153 const char *s
= NULL
;
157 (void) sd_device_get_devtype(device
, &s
);
158 return streq_ptr(s
, devtype
);