udev-ctrl: make udev_ctrl_new() return negative errno on failure
This commit is contained in:
+18
-18
@@ -73,27 +73,29 @@ struct udev_ctrl_connection {
|
||||
int sock;
|
||||
};
|
||||
|
||||
struct udev_ctrl *udev_ctrl_new_from_fd(int fd) {
|
||||
int udev_ctrl_new_from_fd(struct udev_ctrl **ret, int fd) {
|
||||
_cleanup_close_ int sock = -1;
|
||||
struct udev_ctrl *uctrl;
|
||||
int r;
|
||||
|
||||
uctrl = new0(struct udev_ctrl, 1);
|
||||
if (!uctrl)
|
||||
return NULL;
|
||||
uctrl->n_ref = 1;
|
||||
assert(ret);
|
||||
|
||||
if (fd < 0) {
|
||||
uctrl->sock = socket(AF_LOCAL, SOCK_SEQPACKET|SOCK_NONBLOCK|SOCK_CLOEXEC, 0);
|
||||
if (uctrl->sock < 0) {
|
||||
log_error_errno(errno, "Failed to create socket: %m");
|
||||
udev_ctrl_unref(uctrl);
|
||||
return NULL;
|
||||
}
|
||||
} else {
|
||||
uctrl->bound = true;
|
||||
uctrl->sock = fd;
|
||||
sock = socket(AF_LOCAL, SOCK_SEQPACKET|SOCK_NONBLOCK|SOCK_CLOEXEC, 0);
|
||||
if (sock < 0)
|
||||
return log_error_errno(errno, "Failed to create socket: %m");
|
||||
}
|
||||
|
||||
uctrl = new(struct udev_ctrl, 1);
|
||||
if (!uctrl)
|
||||
return -ENOMEM;
|
||||
|
||||
*uctrl = (struct udev_ctrl) {
|
||||
.n_ref = 1,
|
||||
.sock = fd >= 0 ? fd : TAKE_FD(sock),
|
||||
.bound = fd >= 0,
|
||||
};
|
||||
|
||||
/*
|
||||
* FIXME: remove it as soon as we can depend on this:
|
||||
* http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/?id=90c6bd34f884cd9cee21f1d152baf6c18bcac949
|
||||
@@ -108,11 +110,9 @@ struct udev_ctrl *udev_ctrl_new_from_fd(int fd) {
|
||||
};
|
||||
|
||||
uctrl->addrlen = SOCKADDR_UN_LEN(uctrl->saddr.un);
|
||||
return uctrl;
|
||||
}
|
||||
|
||||
struct udev_ctrl *udev_ctrl_new(void) {
|
||||
return udev_ctrl_new_from_fd(-1);
|
||||
*ret = TAKE_PTR(uctrl);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int udev_ctrl_enable_receiving(struct udev_ctrl *uctrl) {
|
||||
|
||||
@@ -5,8 +5,12 @@
|
||||
#include "time-util.h"
|
||||
|
||||
struct udev_ctrl;
|
||||
struct udev_ctrl *udev_ctrl_new(void);
|
||||
struct udev_ctrl *udev_ctrl_new_from_fd(int fd);
|
||||
|
||||
int udev_ctrl_new_from_fd(struct udev_ctrl **ret, int fd);
|
||||
static inline int udev_ctrl_new(struct udev_ctrl **ret) {
|
||||
return udev_ctrl_new_from_fd(ret, -1);
|
||||
}
|
||||
|
||||
int udev_ctrl_enable_receiving(struct udev_ctrl *uctrl);
|
||||
struct udev_ctrl *udev_ctrl_unref(struct udev_ctrl *uctrl);
|
||||
int udev_ctrl_cleanup(struct udev_ctrl *uctrl);
|
||||
|
||||
@@ -86,9 +86,9 @@ int control_main(int argc, char *argv[], void *userdata) {
|
||||
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
|
||||
"This command expects one or more options.");
|
||||
|
||||
uctrl = udev_ctrl_new();
|
||||
if (!uctrl)
|
||||
return log_oom();
|
||||
r = udev_ctrl_new(&uctrl);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to initialize udev control: %m");
|
||||
|
||||
while ((c = getopt_long(argc, argv, "el:sSRp:m:t:Vh", options, NULL)) >= 0)
|
||||
switch (c) {
|
||||
|
||||
@@ -100,8 +100,7 @@ int settle_main(int argc, char *argv[], void *userdata) {
|
||||
if (getuid() == 0) {
|
||||
_cleanup_(udev_ctrl_unrefp) struct udev_ctrl *uctrl = NULL;
|
||||
|
||||
uctrl = udev_ctrl_new();
|
||||
if (uctrl) {
|
||||
if (udev_ctrl_new(&uctrl) >= 0) {
|
||||
r = udev_ctrl_send_ping(uctrl, MAX(5 * USEC_PER_SEC, arg_timeout));
|
||||
if (r < 0) {
|
||||
log_debug_errno(r, "Failed to connect to udev daemon: %m");
|
||||
|
||||
@@ -308,9 +308,9 @@ int trigger_main(int argc, char *argv[], void *userdata) {
|
||||
if (ping) {
|
||||
_cleanup_(udev_ctrl_unrefp) struct udev_ctrl *uctrl = NULL;
|
||||
|
||||
uctrl = udev_ctrl_new();
|
||||
if (!uctrl)
|
||||
return log_oom();
|
||||
r = udev_ctrl_new(&uctrl);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to initialize udev control: %m");
|
||||
|
||||
r = udev_ctrl_send_ping(uctrl, ping_timeout_usec);
|
||||
if (r < 0)
|
||||
|
||||
+3
-3
@@ -1596,9 +1596,9 @@ static int manager_new(Manager **ret, int fd_ctrl, int fd_uevent, const char *cg
|
||||
.cgroup = cgroup,
|
||||
};
|
||||
|
||||
manager->ctrl = udev_ctrl_new_from_fd(fd_ctrl);
|
||||
if (!manager->ctrl)
|
||||
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to initialize udev control socket");
|
||||
r = udev_ctrl_new_from_fd(&manager->ctrl, fd_ctrl);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to initialize udev control socket: %m");
|
||||
|
||||
r = udev_ctrl_enable_receiving(manager->ctrl);
|
||||
if (r < 0)
|
||||
|
||||
Reference in New Issue
Block a user