sd-device: introduce device_open_from_devnum()
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
#include "device-private.h"
|
||||
#include "device-util.h"
|
||||
#include "devnum-util.h"
|
||||
#include "fd-util.h"
|
||||
#include "string-util.h"
|
||||
|
||||
int devpath_from_devnum(mode_t mode, dev_t devnum, char **ret) {
|
||||
@@ -31,3 +32,34 @@ int devpath_from_devnum(mode_t mode, dev_t devnum, char **ret) {
|
||||
*ret = TAKE_PTR(s);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int device_open_from_devnum(mode_t mode, dev_t devnum, int flags, char **ret) {
|
||||
_cleanup_(sd_device_unrefp) sd_device *dev = NULL;
|
||||
_cleanup_close_ int fd = -1;
|
||||
int r;
|
||||
|
||||
r = device_new_from_mode_and_devnum(&dev, mode, devnum);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
fd = sd_device_open(dev, flags);
|
||||
if (fd < 0)
|
||||
return fd;
|
||||
|
||||
if (ret) {
|
||||
const char *devname;
|
||||
char *s;
|
||||
|
||||
r = sd_device_get_devname(dev, &devname);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
s = strdup(devname);
|
||||
if (!s)
|
||||
return -ENOMEM;
|
||||
|
||||
*ret = s;
|
||||
}
|
||||
|
||||
return TAKE_FD(fd);
|
||||
}
|
||||
|
||||
@@ -82,3 +82,4 @@
|
||||
#define log_device_error_errno(device, error, ...) log_device_full_errno(device, LOG_ERR, error, __VA_ARGS__)
|
||||
|
||||
int devpath_from_devnum(mode_t mode, dev_t devnum, char **ret);
|
||||
int device_open_from_devnum(mode_t mode, dev_t devnum, int flags, char **ret);
|
||||
|
||||
Reference in New Issue
Block a user