2017-11-18 18:18:16 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1+
|
|
|
|
*
|
2008-09-10 02:40:42 +02:00
|
|
|
* libudev - interface to udev device information
|
2008-09-08 17:59:00 +02:00
|
|
|
*
|
2009-03-26 19:29:36 +01:00
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2008-09-08 17:59:00 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <errno.h>
|
2015-11-16 22:09:36 +01:00
|
|
|
#include <poll.h>
|
2008-09-08 17:59:00 +02:00
|
|
|
#include <stddef.h>
|
2015-11-16 22:09:36 +01:00
|
|
|
#include <stdlib.h>
|
2008-09-08 17:59:00 +02:00
|
|
|
#include <string.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/un.h>
|
2015-11-16 22:09:36 +01:00
|
|
|
#include <unistd.h>
|
2008-09-08 17:59:00 +02:00
|
|
|
|
2015-10-27 03:01:06 +01:00
|
|
|
#include "alloc-util.h"
|
2015-10-25 13:14:12 +01:00
|
|
|
#include "fd-util.h"
|
2016-11-07 16:14:59 +01:00
|
|
|
#include "format-util.h"
|
2015-10-25 13:14:12 +01:00
|
|
|
#include "socket-util.h"
|
2018-08-29 09:03:45 +02:00
|
|
|
#include "strxcpyx.h"
|
|
|
|
#include "udev-ctrl.h"
|
2008-09-08 17:59:00 +02:00
|
|
|
|
2009-04-19 03:25:04 +02:00
|
|
|
/* wire protocol magic must match */
|
2012-01-10 01:34:15 +01:00
|
|
|
#define UDEV_CTRL_MAGIC 0xdead1dea
|
2008-09-08 17:59:00 +02:00
|
|
|
|
|
|
|
enum udev_ctrl_msg_type {
|
2012-01-10 01:34:15 +01:00
|
|
|
UDEV_CTRL_UNKNOWN,
|
|
|
|
UDEV_CTRL_SET_LOG_LEVEL,
|
|
|
|
UDEV_CTRL_STOP_EXEC_QUEUE,
|
|
|
|
UDEV_CTRL_START_EXEC_QUEUE,
|
|
|
|
UDEV_CTRL_RELOAD,
|
|
|
|
UDEV_CTRL_SET_ENV,
|
|
|
|
UDEV_CTRL_SET_CHILDREN_MAX,
|
|
|
|
UDEV_CTRL_PING,
|
|
|
|
UDEV_CTRL_EXIT,
|
2008-09-08 17:59:00 +02:00
|
|
|
};
|
|
|
|
|
2008-10-17 17:32:17 +02:00
|
|
|
struct udev_ctrl_msg_wire {
|
2012-01-10 01:34:15 +01:00
|
|
|
char version[16];
|
2018-10-19 20:00:46 +02:00
|
|
|
unsigned magic;
|
2012-01-10 01:34:15 +01:00
|
|
|
enum udev_ctrl_msg_type type;
|
|
|
|
union {
|
|
|
|
int intval;
|
|
|
|
char buf[256];
|
|
|
|
};
|
2008-09-08 17:59:00 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct udev_ctrl_msg {
|
2018-08-28 07:01:35 +02:00
|
|
|
unsigned n_ref;
|
2012-01-10 01:34:15 +01:00
|
|
|
struct udev_ctrl_connection *conn;
|
|
|
|
struct udev_ctrl_msg_wire ctrl_msg_wire;
|
2008-09-08 17:59:00 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct udev_ctrl {
|
2018-08-28 07:01:35 +02:00
|
|
|
unsigned n_ref;
|
2012-01-10 01:34:15 +01:00
|
|
|
int sock;
|
2015-03-11 05:01:47 +01:00
|
|
|
union sockaddr_union saddr;
|
2012-01-10 01:34:15 +01:00
|
|
|
socklen_t addrlen;
|
|
|
|
bool bound;
|
|
|
|
bool cleanup_socket;
|
|
|
|
bool connected;
|
2011-04-13 01:17:09 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct udev_ctrl_connection {
|
2018-08-28 07:01:35 +02:00
|
|
|
unsigned n_ref;
|
2012-01-10 01:34:15 +01:00
|
|
|
struct udev_ctrl *uctrl;
|
|
|
|
int sock;
|
2008-09-08 17:59:00 +02:00
|
|
|
};
|
|
|
|
|
2018-08-22 12:57:32 +02:00
|
|
|
struct udev_ctrl *udev_ctrl_new_from_fd(int fd) {
|
2012-01-10 01:34:15 +01:00
|
|
|
struct udev_ctrl *uctrl;
|
2014-09-18 19:26:11 +02:00
|
|
|
int r;
|
2012-01-10 01:34:15 +01:00
|
|
|
|
2014-02-13 14:45:51 +01:00
|
|
|
uctrl = new0(struct udev_ctrl, 1);
|
2018-11-23 20:46:27 +01:00
|
|
|
if (!uctrl)
|
2012-01-10 01:34:15 +01:00
|
|
|
return NULL;
|
2018-08-28 07:01:35 +02:00
|
|
|
uctrl->n_ref = 1;
|
2012-01-10 01:34:15 +01:00
|
|
|
|
|
|
|
if (fd < 0) {
|
|
|
|
uctrl->sock = socket(AF_LOCAL, SOCK_SEQPACKET|SOCK_NONBLOCK|SOCK_CLOEXEC, 0);
|
|
|
|
if (uctrl->sock < 0) {
|
2018-11-26 03:52:40 +01:00
|
|
|
log_error_errno(errno, "Failed to create socket: %m");
|
2012-01-10 01:34:15 +01:00
|
|
|
udev_ctrl_unref(uctrl);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
uctrl->bound = true;
|
|
|
|
uctrl->sock = fd;
|
|
|
|
}
|
2015-08-27 12:57:17 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*/
|
2018-10-18 19:48:18 +02:00
|
|
|
r = setsockopt_int(uctrl->sock, SOL_SOCKET, SO_PASSCRED, true);
|
2014-09-18 19:26:11 +02:00
|
|
|
if (r < 0)
|
2018-11-26 03:52:40 +01:00
|
|
|
log_warning_errno(r, "Failed to set SO_PASSCRED: %m");
|
2012-01-10 01:34:15 +01:00
|
|
|
|
2018-10-15 13:59:07 +02:00
|
|
|
uctrl->saddr.un = (struct sockaddr_un) {
|
|
|
|
.sun_family = AF_UNIX,
|
|
|
|
.sun_path = "/run/udev/control",
|
|
|
|
};
|
|
|
|
|
2016-05-05 22:24:36 +02:00
|
|
|
uctrl->addrlen = SOCKADDR_UN_LEN(uctrl->saddr.un);
|
2012-01-10 01:34:15 +01:00
|
|
|
return uctrl;
|
2011-03-23 16:40:23 +01:00
|
|
|
}
|
|
|
|
|
2018-08-22 12:57:32 +02:00
|
|
|
struct udev_ctrl *udev_ctrl_new(void) {
|
|
|
|
return udev_ctrl_new_from_fd(-1);
|
2008-09-08 17:59:00 +02:00
|
|
|
}
|
|
|
|
|
2014-07-29 15:47:41 +02:00
|
|
|
int udev_ctrl_enable_receiving(struct udev_ctrl *uctrl) {
|
2012-01-10 01:34:15 +01:00
|
|
|
int err;
|
|
|
|
|
|
|
|
if (!uctrl->bound) {
|
2015-03-11 05:01:47 +01:00
|
|
|
err = bind(uctrl->sock, &uctrl->saddr.sa, uctrl->addrlen);
|
2012-01-10 01:34:15 +01:00
|
|
|
if (err < 0 && errno == EADDRINUSE) {
|
2018-10-15 12:08:30 +02:00
|
|
|
(void) sockaddr_un_unlink(&uctrl->saddr.un);
|
2015-03-11 05:01:47 +01:00
|
|
|
err = bind(uctrl->sock, &uctrl->saddr.sa, uctrl->addrlen);
|
2012-01-10 01:34:15 +01:00
|
|
|
}
|
|
|
|
|
2015-09-08 23:03:38 +02:00
|
|
|
if (err < 0)
|
2018-11-26 03:52:40 +01:00
|
|
|
return log_error_errno(errno, "Failed to bind socket: %m");
|
2012-01-10 01:34:15 +01:00
|
|
|
|
|
|
|
err = listen(uctrl->sock, 0);
|
2015-09-08 23:03:38 +02:00
|
|
|
if (err < 0)
|
2018-11-26 03:52:40 +01:00
|
|
|
return log_error_errno(errno, "Failed to listen: %m");
|
2012-01-10 01:34:15 +01:00
|
|
|
|
|
|
|
uctrl->bound = true;
|
|
|
|
uctrl->cleanup_socket = true;
|
|
|
|
}
|
|
|
|
return 0;
|
2008-09-08 17:59:00 +02:00
|
|
|
}
|
|
|
|
|
2018-08-28 07:01:35 +02:00
|
|
|
static struct udev_ctrl *udev_ctrl_free(struct udev_ctrl *uctrl) {
|
|
|
|
assert(uctrl);
|
2015-05-14 15:30:52 +02:00
|
|
|
|
2018-08-28 07:01:35 +02:00
|
|
|
safe_close(uctrl->sock);
|
|
|
|
return mfree(uctrl);
|
2008-09-08 17:59:00 +02:00
|
|
|
}
|
|
|
|
|
2018-08-28 07:01:35 +02:00
|
|
|
DEFINE_PRIVATE_TRIVIAL_REF_FUNC(struct udev_ctrl, udev_ctrl);
|
|
|
|
DEFINE_TRIVIAL_UNREF_FUNC(struct udev_ctrl, udev_ctrl, udev_ctrl_free);
|
2008-09-08 17:59:00 +02:00
|
|
|
|
2014-07-29 15:47:41 +02:00
|
|
|
int udev_ctrl_cleanup(struct udev_ctrl *uctrl) {
|
2018-11-23 20:46:27 +01:00
|
|
|
if (!uctrl)
|
2012-01-10 01:34:15 +01:00
|
|
|
return 0;
|
|
|
|
if (uctrl->cleanup_socket)
|
2018-10-15 12:08:30 +02:00
|
|
|
sockaddr_un_unlink(&uctrl->saddr.un);
|
2012-01-10 01:34:15 +01:00
|
|
|
return 0;
|
2011-10-10 00:38:56 +02:00
|
|
|
}
|
|
|
|
|
2014-07-29 15:47:41 +02:00
|
|
|
int udev_ctrl_get_fd(struct udev_ctrl *uctrl) {
|
2018-11-23 20:46:27 +01:00
|
|
|
if (!uctrl)
|
2012-01-10 01:34:15 +01:00
|
|
|
return -EINVAL;
|
|
|
|
return uctrl->sock;
|
2008-09-08 17:59:00 +02:00
|
|
|
}
|
|
|
|
|
2014-07-29 15:47:41 +02:00
|
|
|
struct udev_ctrl_connection *udev_ctrl_get_connection(struct udev_ctrl *uctrl) {
|
2012-01-10 01:34:15 +01:00
|
|
|
struct udev_ctrl_connection *conn;
|
2014-02-19 17:47:11 +01:00
|
|
|
struct ucred ucred = {};
|
2013-12-24 15:53:04 +01:00
|
|
|
int r;
|
2012-01-10 01:34:15 +01:00
|
|
|
|
2013-12-24 15:53:04 +01:00
|
|
|
conn = new(struct udev_ctrl_connection, 1);
|
2018-11-23 20:46:27 +01:00
|
|
|
if (!conn)
|
2012-01-10 01:34:15 +01:00
|
|
|
return NULL;
|
2018-08-28 07:01:35 +02:00
|
|
|
conn->n_ref = 1;
|
2012-01-10 01:34:15 +01:00
|
|
|
conn->uctrl = uctrl;
|
|
|
|
|
|
|
|
conn->sock = accept4(uctrl->sock, NULL, NULL, SOCK_CLOEXEC|SOCK_NONBLOCK);
|
|
|
|
if (conn->sock < 0) {
|
|
|
|
if (errno != EINTR)
|
2018-11-26 03:52:40 +01:00
|
|
|
log_error_errno(errno, "Failed to receive ctrl connection: %m");
|
2012-01-10 01:34:15 +01:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check peer credential of connection */
|
2013-12-24 15:53:04 +01:00
|
|
|
r = getpeercred(conn->sock, &ucred);
|
|
|
|
if (r < 0) {
|
2018-11-26 03:52:40 +01:00
|
|
|
log_error_errno(r, "Failed to receive credentials of ctrl connection: %m");
|
2012-01-10 01:34:15 +01:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if (ucred.uid > 0) {
|
2018-11-26 03:52:40 +01:00
|
|
|
log_error("Sender uid="UID_FMT", message ignored", ucred.uid);
|
2012-01-10 01:34:15 +01:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* enable receiving of the sender credentials in the messages */
|
2018-10-18 19:48:18 +02:00
|
|
|
r = setsockopt_int(conn->sock, SOL_SOCKET, SO_PASSCRED, true);
|
2014-09-18 19:26:11 +02:00
|
|
|
if (r < 0)
|
2018-11-26 03:52:40 +01:00
|
|
|
log_warning_errno(r, "Failed to set SO_PASSCRED: %m");
|
2014-09-18 19:26:11 +02:00
|
|
|
|
2012-01-10 01:34:15 +01:00
|
|
|
udev_ctrl_ref(uctrl);
|
|
|
|
return conn;
|
2011-05-19 22:57:46 +02:00
|
|
|
err:
|
2018-10-15 19:23:09 +02:00
|
|
|
safe_close(conn->sock);
|
2016-10-17 00:28:30 +02:00
|
|
|
return mfree(conn);
|
2011-04-13 01:17:09 +02:00
|
|
|
}
|
|
|
|
|
2018-08-28 07:01:35 +02:00
|
|
|
static struct udev_ctrl_connection *udev_ctrl_connection_free(struct udev_ctrl_connection *conn) {
|
|
|
|
assert(conn);
|
2015-05-14 15:30:52 +02:00
|
|
|
|
2018-08-28 07:01:35 +02:00
|
|
|
safe_close(conn->sock);
|
|
|
|
udev_ctrl_unref(conn->uctrl);
|
|
|
|
return mfree(conn);
|
2011-04-13 01:17:09 +02:00
|
|
|
}
|
|
|
|
|
2018-08-28 07:01:35 +02:00
|
|
|
DEFINE_TRIVIAL_REF_UNREF_FUNC(struct udev_ctrl_connection, udev_ctrl_connection, udev_ctrl_connection_free);
|
|
|
|
|
2014-07-29 15:47:41 +02:00
|
|
|
static int ctrl_send(struct udev_ctrl *uctrl, enum udev_ctrl_msg_type type, int intval, const char *buf, int timeout) {
|
2012-01-10 01:34:15 +01:00
|
|
|
struct udev_ctrl_msg_wire ctrl_msg_wire;
|
|
|
|
int err = 0;
|
|
|
|
|
2014-01-31 06:51:32 +01:00
|
|
|
memzero(&ctrl_msg_wire, sizeof(struct udev_ctrl_msg_wire));
|
2017-04-03 18:15:33 +02:00
|
|
|
strcpy(ctrl_msg_wire.version, "udev-" PACKAGE_VERSION);
|
2012-01-10 01:34:15 +01:00
|
|
|
ctrl_msg_wire.magic = UDEV_CTRL_MAGIC;
|
|
|
|
ctrl_msg_wire.type = type;
|
|
|
|
|
2018-11-23 20:46:27 +01:00
|
|
|
if (buf)
|
2013-01-09 19:06:46 +01:00
|
|
|
strscpy(ctrl_msg_wire.buf, sizeof(ctrl_msg_wire.buf), buf);
|
2012-01-10 01:34:15 +01:00
|
|
|
else
|
|
|
|
ctrl_msg_wire.intval = intval;
|
|
|
|
|
|
|
|
if (!uctrl->connected) {
|
2015-03-11 05:01:47 +01:00
|
|
|
if (connect(uctrl->sock, &uctrl->saddr.sa, uctrl->addrlen) < 0) {
|
2012-01-10 01:34:15 +01:00
|
|
|
err = -errno;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
uctrl->connected = true;
|
|
|
|
}
|
|
|
|
if (send(uctrl->sock, &ctrl_msg_wire, sizeof(ctrl_msg_wire), 0) < 0) {
|
|
|
|
err = -errno;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* wait for peer message handling or disconnect */
|
|
|
|
for (;;) {
|
|
|
|
struct pollfd pfd[1];
|
|
|
|
int r;
|
|
|
|
|
|
|
|
pfd[0].fd = uctrl->sock;
|
|
|
|
pfd[0].events = POLLIN;
|
2014-04-13 07:35:50 +02:00
|
|
|
r = poll(pfd, 1, timeout * MSEC_PER_SEC);
|
2018-11-26 03:53:21 +01:00
|
|
|
if (r < 0) {
|
2012-01-10 01:34:15 +01:00
|
|
|
if (errno == EINTR)
|
|
|
|
continue;
|
|
|
|
err = -errno;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (r > 0 && pfd[0].revents & POLLERR) {
|
|
|
|
err = -EIO;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (r == 0)
|
|
|
|
err = -ETIMEDOUT;
|
|
|
|
break;
|
|
|
|
}
|
2011-04-13 01:17:09 +02:00
|
|
|
out:
|
2012-01-10 01:34:15 +01:00
|
|
|
return err;
|
2008-09-08 17:59:00 +02:00
|
|
|
}
|
|
|
|
|
2014-07-29 15:47:41 +02:00
|
|
|
int udev_ctrl_send_set_log_level(struct udev_ctrl *uctrl, int priority, int timeout) {
|
2012-01-10 01:34:15 +01:00
|
|
|
return ctrl_send(uctrl, UDEV_CTRL_SET_LOG_LEVEL, priority, NULL, timeout);
|
2011-04-13 01:17:09 +02:00
|
|
|
}
|
|
|
|
|
2014-07-29 15:47:41 +02:00
|
|
|
int udev_ctrl_send_stop_exec_queue(struct udev_ctrl *uctrl, int timeout) {
|
2012-01-10 01:34:15 +01:00
|
|
|
return ctrl_send(uctrl, UDEV_CTRL_STOP_EXEC_QUEUE, 0, NULL, timeout);
|
2008-09-08 17:59:00 +02:00
|
|
|
}
|
|
|
|
|
2014-07-29 15:47:41 +02:00
|
|
|
int udev_ctrl_send_start_exec_queue(struct udev_ctrl *uctrl, int timeout) {
|
2012-01-10 01:34:15 +01:00
|
|
|
return ctrl_send(uctrl, UDEV_CTRL_START_EXEC_QUEUE, 0, NULL, timeout);
|
2008-09-08 17:59:00 +02:00
|
|
|
}
|
|
|
|
|
2014-07-29 15:47:41 +02:00
|
|
|
int udev_ctrl_send_reload(struct udev_ctrl *uctrl, int timeout) {
|
2012-01-10 01:34:15 +01:00
|
|
|
return ctrl_send(uctrl, UDEV_CTRL_RELOAD, 0, NULL, timeout);
|
2008-09-08 17:59:00 +02:00
|
|
|
}
|
|
|
|
|
2014-07-29 15:47:41 +02:00
|
|
|
int udev_ctrl_send_set_env(struct udev_ctrl *uctrl, const char *key, int timeout) {
|
2012-01-10 01:34:15 +01:00
|
|
|
return ctrl_send(uctrl, UDEV_CTRL_SET_ENV, 0, key, timeout);
|
2008-09-08 17:59:00 +02:00
|
|
|
}
|
|
|
|
|
2014-07-29 15:47:41 +02:00
|
|
|
int udev_ctrl_send_set_children_max(struct udev_ctrl *uctrl, int count, int timeout) {
|
2012-01-10 01:34:15 +01:00
|
|
|
return ctrl_send(uctrl, UDEV_CTRL_SET_CHILDREN_MAX, count, NULL, timeout);
|
2008-09-08 17:59:00 +02:00
|
|
|
}
|
|
|
|
|
2014-07-29 15:47:41 +02:00
|
|
|
int udev_ctrl_send_ping(struct udev_ctrl *uctrl, int timeout) {
|
2012-01-10 01:34:15 +01:00
|
|
|
return ctrl_send(uctrl, UDEV_CTRL_PING, 0, NULL, timeout);
|
2008-09-08 17:59:00 +02:00
|
|
|
}
|
|
|
|
|
2014-07-29 15:47:41 +02:00
|
|
|
int udev_ctrl_send_exit(struct udev_ctrl *uctrl, int timeout) {
|
2012-01-10 01:34:15 +01:00
|
|
|
return ctrl_send(uctrl, UDEV_CTRL_EXIT, 0, NULL, timeout);
|
2009-03-10 14:00:16 +01:00
|
|
|
}
|
|
|
|
|
2014-07-29 15:47:41 +02:00
|
|
|
struct udev_ctrl_msg *udev_ctrl_receive_msg(struct udev_ctrl_connection *conn) {
|
2012-01-10 01:34:15 +01:00
|
|
|
struct udev_ctrl_msg *uctrl_msg;
|
|
|
|
ssize_t size;
|
|
|
|
struct cmsghdr *cmsg;
|
|
|
|
struct iovec iov;
|
|
|
|
char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
|
2013-10-12 01:34:21 +02:00
|
|
|
struct msghdr smsg = {
|
|
|
|
.msg_iov = &iov,
|
|
|
|
.msg_iovlen = 1,
|
|
|
|
.msg_control = cred_msg,
|
|
|
|
.msg_controllen = sizeof(cred_msg),
|
|
|
|
};
|
|
|
|
struct ucred *cred;
|
2012-01-10 01:34:15 +01:00
|
|
|
|
2013-10-12 01:34:21 +02:00
|
|
|
uctrl_msg = new0(struct udev_ctrl_msg, 1);
|
2018-11-23 20:46:27 +01:00
|
|
|
if (!uctrl_msg)
|
2012-01-10 01:34:15 +01:00
|
|
|
return NULL;
|
2018-08-28 07:01:35 +02:00
|
|
|
uctrl_msg->n_ref = 1;
|
2012-01-10 01:34:15 +01:00
|
|
|
uctrl_msg->conn = conn;
|
|
|
|
udev_ctrl_connection_ref(conn);
|
|
|
|
|
|
|
|
/* wait for the incoming message */
|
2013-12-03 22:27:45 +01:00
|
|
|
for (;;) {
|
2012-01-10 01:34:15 +01:00
|
|
|
struct pollfd pfd[1];
|
|
|
|
int r;
|
|
|
|
|
|
|
|
pfd[0].fd = conn->sock;
|
|
|
|
pfd[0].events = POLLIN;
|
|
|
|
|
|
|
|
r = poll(pfd, 1, 10000);
|
2018-11-26 03:53:21 +01:00
|
|
|
if (r < 0) {
|
2012-01-10 01:34:15 +01:00
|
|
|
if (errno == EINTR)
|
|
|
|
continue;
|
|
|
|
goto err;
|
|
|
|
} else if (r == 0) {
|
2018-11-26 03:52:40 +01:00
|
|
|
log_error("Timeout waiting for ctrl message");
|
2012-01-10 01:34:15 +01:00
|
|
|
goto err;
|
|
|
|
} else {
|
|
|
|
if (!(pfd[0].revents & POLLIN)) {
|
2018-11-26 03:52:40 +01:00
|
|
|
log_error("Invalid ctrl connection: %m");
|
2012-01-10 01:34:15 +01:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
iov.iov_base = &uctrl_msg->ctrl_msg_wire;
|
|
|
|
iov.iov_len = sizeof(struct udev_ctrl_msg_wire);
|
2013-10-12 01:34:21 +02:00
|
|
|
|
2015-02-12 11:44:48 +01:00
|
|
|
size = recvmsg(conn->sock, &smsg, 0);
|
2018-11-26 03:52:40 +01:00
|
|
|
if (size < 0) {
|
|
|
|
log_error_errno(errno, "Failed to receive ctrl message: %m");
|
2012-01-10 01:34:15 +01:00
|
|
|
goto err;
|
|
|
|
}
|
2015-02-18 19:20:47 +01:00
|
|
|
|
|
|
|
cmsg_close_all(&smsg);
|
|
|
|
|
2012-01-10 01:34:15 +01:00
|
|
|
cmsg = CMSG_FIRSTHDR(&smsg);
|
|
|
|
|
2018-11-23 20:46:27 +01:00
|
|
|
if (!cmsg || cmsg->cmsg_type != SCM_CREDENTIALS) {
|
2018-11-26 03:52:40 +01:00
|
|
|
log_error("No sender credentials received, ignoring message");
|
2012-01-10 01:34:15 +01:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2015-08-06 13:46:49 +02:00
|
|
|
cred = (struct ucred *) CMSG_DATA(cmsg);
|
|
|
|
|
2012-01-10 01:34:15 +01:00
|
|
|
if (cred->uid != 0) {
|
2018-11-26 03:52:40 +01:00
|
|
|
log_error("Sender uid="UID_FMT", ignoring message", cred->uid);
|
2012-01-10 01:34:15 +01:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (uctrl_msg->ctrl_msg_wire.magic != UDEV_CTRL_MAGIC) {
|
2018-11-26 03:52:40 +01:00
|
|
|
log_error("Message magic 0x%08x doesn't match, ignoring", uctrl_msg->ctrl_msg_wire.magic);
|
2012-01-10 01:34:15 +01:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
return uctrl_msg;
|
2008-09-08 17:59:00 +02:00
|
|
|
err:
|
2012-01-10 01:34:15 +01:00
|
|
|
udev_ctrl_msg_unref(uctrl_msg);
|
|
|
|
return NULL;
|
2008-09-08 17:59:00 +02:00
|
|
|
}
|
|
|
|
|
2018-08-28 07:01:35 +02:00
|
|
|
static struct udev_ctrl_msg *udev_ctrl_msg_free(struct udev_ctrl_msg *ctrl_msg) {
|
|
|
|
assert(ctrl_msg);
|
2015-05-14 15:30:52 +02:00
|
|
|
|
2018-08-28 07:01:35 +02:00
|
|
|
udev_ctrl_connection_unref(ctrl_msg->conn);
|
|
|
|
return mfree(ctrl_msg);
|
2008-09-08 17:59:00 +02:00
|
|
|
}
|
|
|
|
|
2018-08-28 07:01:35 +02:00
|
|
|
DEFINE_TRIVIAL_UNREF_FUNC(struct udev_ctrl_msg, udev_ctrl_msg, udev_ctrl_msg_free);
|
|
|
|
|
2014-07-29 15:47:41 +02:00
|
|
|
int udev_ctrl_get_set_log_level(struct udev_ctrl_msg *ctrl_msg) {
|
2012-01-10 01:34:15 +01:00
|
|
|
if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_SET_LOG_LEVEL)
|
|
|
|
return ctrl_msg->ctrl_msg_wire.intval;
|
|
|
|
return -1;
|
2008-09-08 17:59:00 +02:00
|
|
|
}
|
|
|
|
|
2014-07-29 15:47:41 +02:00
|
|
|
int udev_ctrl_get_stop_exec_queue(struct udev_ctrl_msg *ctrl_msg) {
|
2012-01-10 01:34:15 +01:00
|
|
|
if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_STOP_EXEC_QUEUE)
|
|
|
|
return 1;
|
|
|
|
return -1;
|
2008-09-08 17:59:00 +02:00
|
|
|
}
|
|
|
|
|
2014-07-29 15:47:41 +02:00
|
|
|
int udev_ctrl_get_start_exec_queue(struct udev_ctrl_msg *ctrl_msg) {
|
2012-01-10 01:34:15 +01:00
|
|
|
if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_START_EXEC_QUEUE)
|
|
|
|
return 1;
|
|
|
|
return -1;
|
2008-09-08 17:59:00 +02:00
|
|
|
}
|
|
|
|
|
2014-07-29 15:47:41 +02:00
|
|
|
int udev_ctrl_get_reload(struct udev_ctrl_msg *ctrl_msg) {
|
2012-01-10 01:34:15 +01:00
|
|
|
if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_RELOAD)
|
|
|
|
return 1;
|
|
|
|
return -1;
|
2008-09-08 17:59:00 +02:00
|
|
|
}
|
|
|
|
|
2014-07-29 15:47:41 +02:00
|
|
|
const char *udev_ctrl_get_set_env(struct udev_ctrl_msg *ctrl_msg) {
|
2012-01-10 01:34:15 +01:00
|
|
|
if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_SET_ENV)
|
|
|
|
return ctrl_msg->ctrl_msg_wire.buf;
|
|
|
|
return NULL;
|
2008-09-08 17:59:00 +02:00
|
|
|
}
|
|
|
|
|
2014-07-29 15:47:41 +02:00
|
|
|
int udev_ctrl_get_set_children_max(struct udev_ctrl_msg *ctrl_msg) {
|
2012-01-10 01:34:15 +01:00
|
|
|
if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_SET_CHILDREN_MAX)
|
|
|
|
return ctrl_msg->ctrl_msg_wire.intval;
|
|
|
|
return -1;
|
2008-09-08 17:59:00 +02:00
|
|
|
}
|
2009-03-10 14:00:16 +01:00
|
|
|
|
2014-07-29 15:47:41 +02:00
|
|
|
int udev_ctrl_get_ping(struct udev_ctrl_msg *ctrl_msg) {
|
2012-01-10 01:34:15 +01:00
|
|
|
if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_PING)
|
|
|
|
return 1;
|
|
|
|
return -1;
|
2011-04-13 01:17:09 +02:00
|
|
|
}
|
|
|
|
|
2014-07-29 15:47:41 +02:00
|
|
|
int udev_ctrl_get_exit(struct udev_ctrl_msg *ctrl_msg) {
|
2012-01-10 01:34:15 +01:00
|
|
|
if (ctrl_msg->ctrl_msg_wire.type == UDEV_CTRL_EXIT)
|
|
|
|
return 1;
|
|
|
|
return -1;
|
2009-03-10 14:00:16 +01:00
|
|
|
}
|