1
0
mirror of https://github.com/systemd/systemd.git synced 2024-12-23 21:35:11 +03:00

move udev_ctrl to libudev-private

This commit is contained in:
Kay Sievers 2008-09-08 17:59:00 +02:00
parent e5931bb7d8
commit d59f11e140
16 changed files with 557 additions and 408 deletions

View File

@ -36,7 +36,8 @@ common_files = \
lib/libudev.h \
lib/libudev-private.h \
lib/libudev.c \
lib/libudev-utils.c
lib/libudev-utils.c \
lib/libudev-ctrl.c
if USE_SELINUX
@ -49,7 +50,6 @@ endif
udevd_SOURCES = \
$(common_files) \
udevd.h \
udevd.c
udevd_LDADD = \
@ -58,7 +58,6 @@ udevd_LDADD = \
udevadm_SOURCES = \
$(common_files) \
udev-control.c \
udevadm.c \
udevadm-info.c \
udevadm-control.c \

View File

@ -20,11 +20,13 @@ include_HEADERS =\
libudev.h
libudev_la_SOURCES =\
exported_symbools \
libudev-private.h \
libudev.c \
libudev-utils.c \
libudev-device.c \
libudev-enumerate.c \
libudev-ctrl.c \
libudev-monitor.c \
../list.h \
../udev.h \

View File

@ -19,8 +19,9 @@ udev_device_get_devlinks
udev_device_get_properties
udev_devices_enumerate
udev_monitor_new_from_socket
udev_monitor_enable_receiving
udev_monitor_ref
udev_monitor_unref
udev_monitor_get_udev
udev_monitor_get_fd
udev_monitor_get_device
udev_monitor_receive_device

332
udev/lib/libudev-ctrl.c Normal file
View File

@ -0,0 +1,332 @@
/*
* Copyright (C) 2005-2008 Kay Sievers <kay.sievers@vrfy.org>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation version 2 of the License.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include "config.h"
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include "../udev.h"
#include "libudev.h"
#include "libudev-private.h"
#define UDEV_CTRL_MAGIC "udevd-128"
enum udev_ctrl_msg_type {
UDEV_CTRL_UNKNOWN,
UDEV_CTRL_SET_LOG_LEVEL,
UDEV_CTRL_STOP_EXEC_QUEUE,
UDEV_CTRL_START_EXEC_QUEUE,
UDEV_CTRL_RELOAD_RULES,
UDEV_CTRL_SET_ENV,
UDEV_CTRL_SET_MAX_CHILDS,
UDEV_CTRL_SET_MAX_CHILDS_RUNNING,
};
struct ctrl_msg {
char magic[32];
enum udev_ctrl_msg_type type;
union {
int intval;
char buf[256];
};
};
struct udev_ctrl_msg {
int refcount;
struct udev_ctrl *uctrl;
struct ctrl_msg ctrl_msg;
};
struct udev_ctrl {
int refcount;
struct udev *udev;
int sock;
struct sockaddr_un saddr;
socklen_t addrlen;
};
struct udev_ctrl *udev_ctrl_new_from_socket(struct udev *udev, const char *socket_path)
{
struct udev_ctrl *uctrl;
uctrl = malloc(sizeof(struct udev_ctrl));
if (uctrl == NULL)
return NULL;
memset(uctrl, 0x00, sizeof(struct udev_ctrl));
uctrl->refcount = 1;
uctrl->udev = udev;
uctrl->sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
if (uctrl->sock < 0) {
err(udev, "error getting socket: %s\n", strerror(errno));
udev_ctrl_unref(uctrl);
return NULL;
}
uctrl->saddr.sun_family = AF_LOCAL;
strcpy(uctrl->saddr.sun_path, socket_path);
uctrl->addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(uctrl->saddr.sun_path);
/* translate leading '@' to abstract namespace */
if (uctrl->saddr.sun_path[0] == '@')
uctrl->saddr.sun_path[0] = '\0';
return uctrl;
}
int udev_ctrl_enable_receiving(struct udev_ctrl *uctrl)
{
int err;
const int feature_on = 1;
err= bind(uctrl->sock, (struct sockaddr *)&uctrl->saddr, uctrl->addrlen);
if (err < 0) {
err(uctrl->udev, "bind failed: %s\n", strerror(errno));
return err;
}
/* enable receiving of the sender credentials */
setsockopt(uctrl->sock, SOL_SOCKET, SO_PASSCRED, &feature_on, sizeof(feature_on));
return 0;
}
struct udev *udev_ctrl_get_udev(struct udev_ctrl *uctrl)
{
return uctrl->udev;
}
struct udev_ctrl *udev_ctrl_ref(struct udev_ctrl *uctrl)
{
if (uctrl == NULL)
return NULL;
uctrl->refcount++;
return uctrl;
}
void udev_ctrl_unref(struct udev_ctrl *uctrl)
{
if (uctrl == NULL)
return;
uctrl->refcount--;
if (uctrl->refcount > 0)
return;
if (uctrl->sock >= 0)
close(uctrl->sock);
free(uctrl);
}
int udev_ctrl_get_fd(struct udev_ctrl *uctrl)
{
if (uctrl == NULL)
return -1;
return uctrl->sock;
}
static int ctrl_send(struct udev_ctrl *uctrl, enum udev_ctrl_msg_type type, int intval, const char *buf)
{
struct ctrl_msg ctrl_msg;
int err;
memset(&ctrl_msg, 0x00, sizeof(struct ctrl_msg));
strcpy(ctrl_msg.magic, UDEV_CTRL_MAGIC);
ctrl_msg.type = type;
if (buf != NULL)
strlcpy(ctrl_msg.buf, buf, sizeof(ctrl_msg.buf));
else
ctrl_msg.intval = intval;
err = sendto(uctrl->sock, &ctrl_msg, sizeof(ctrl_msg), 0, (struct sockaddr *)&uctrl->saddr, uctrl->addrlen);
if (err == -1) {
err(uctrl->udev, "error sending message: %s\n", strerror(errno));
}
return err;
}
int udev_ctrl_send_set_log_level(struct udev_ctrl *uctrl, int priority)
{
ctrl_send(uctrl, UDEV_CTRL_SET_LOG_LEVEL, priority, NULL);
return 0;
}
int udev_ctrl_send_stop_exec_queue(struct udev_ctrl *uctrl)
{
ctrl_send(uctrl, UDEV_CTRL_STOP_EXEC_QUEUE, 0, NULL);
return 0;
}
int udev_ctrl_send_start_exec_queue(struct udev_ctrl *uctrl)
{
ctrl_send(uctrl, UDEV_CTRL_START_EXEC_QUEUE, 0, NULL);
return 0;
}
int udev_ctrl_send_reload_rules(struct udev_ctrl *uctrl)
{
ctrl_send(uctrl, UDEV_CTRL_RELOAD_RULES, 0, NULL);
return 0;
}
int udev_ctrl_send_set_env(struct udev_ctrl *uctrl, const char *key)
{
ctrl_send(uctrl, UDEV_CTRL_SET_ENV, 0, optarg);
return 0;
}
int udev_ctrl_send_set_max_childs(struct udev_ctrl *uctrl, int count)
{
ctrl_send(uctrl, UDEV_CTRL_SET_MAX_CHILDS, count, NULL);
return 0;
}
int udev_ctrl_send_set_max_childs_running(struct udev_ctrl *uctrl, int count)
{
ctrl_send(uctrl, UDEV_CTRL_SET_MAX_CHILDS_RUNNING, count, NULL);
return 0;
}
struct udev_ctrl_msg *udev_ctrl_receive_msg(struct udev_ctrl *uctrl)
{
struct udev_ctrl_msg *uctrl_msg;
ssize_t size;
struct msghdr smsg;
struct cmsghdr *cmsg;
struct iovec iov;
struct ucred *cred;
char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
uctrl_msg = malloc(sizeof(struct udev_ctrl_msg));
if (uctrl_msg == NULL)
return NULL;
memset(uctrl_msg, 0x00, sizeof(struct udev_ctrl_msg));
uctrl_msg->refcount = 1;
uctrl_msg->uctrl = uctrl;
iov.iov_base = &uctrl_msg->ctrl_msg;
iov.iov_len = sizeof(struct udev_ctrl_msg);
memset(&smsg, 0x00, sizeof(struct msghdr));
smsg.msg_iov = &iov;
smsg.msg_iovlen = 1;
smsg.msg_control = cred_msg;
smsg.msg_controllen = sizeof(cred_msg);
size = recvmsg(uctrl->sock, &smsg, 0);
if (size < 0) {
err(uctrl->udev, "unable to receive user udevd message: %s\n", strerror(errno));
goto err;
}
cmsg = CMSG_FIRSTHDR(&smsg);
cred = (struct ucred *) CMSG_DATA(cmsg);
if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) {
err(uctrl->udev, "no sender credentials received, message ignored\n");
goto err;
}
if (cred->uid != 0) {
err(uctrl->udev, "sender uid=%i, message ignored\n", cred->uid);
goto err;
}
if (strncmp(uctrl_msg->ctrl_msg.magic, UDEV_CTRL_MAGIC, sizeof(UDEV_CTRL_MAGIC)) != 0 ) {
err(uctrl->udev, "message magic '%s' doesn't match, ignore it\n", uctrl_msg->ctrl_msg.magic);
goto err;
}
info(uctrl->udev, "created ctrl_msg %p (%i)\n", uctrl_msg, uctrl_msg->ctrl_msg.type);
return uctrl_msg;
err:
udev_ctrl_msg_unref(uctrl_msg);
return NULL;
}
struct udev_ctrl_msg *udev_ctrl_msg_ref(struct udev_ctrl_msg *ctrl_msg)
{
if (ctrl_msg == NULL)
return NULL;
ctrl_msg->refcount++;
return ctrl_msg;
}
void udev_ctrl_msg_unref(struct udev_ctrl_msg *ctrl_msg)
{
if (ctrl_msg == NULL)
return;
ctrl_msg->refcount--;
if (ctrl_msg->refcount > 0)
return;
info(ctrl_msg->uctrl->udev, "release ctrl_msg %p\n", ctrl_msg);
free(ctrl_msg);
}
int udev_ctrl_get_set_log_level(struct udev_ctrl_msg *ctrl_msg)
{
if (ctrl_msg->ctrl_msg.type == UDEV_CTRL_SET_LOG_LEVEL)
return ctrl_msg->ctrl_msg.intval;
return -1;
}
int udev_ctrl_get_stop_exec_queue(struct udev_ctrl_msg *ctrl_msg)
{
if (ctrl_msg->ctrl_msg.type == UDEV_CTRL_STOP_EXEC_QUEUE)
return 1;
return -1;
}
int udev_ctrl_get_start_exec_queue(struct udev_ctrl_msg *ctrl_msg)
{
if (ctrl_msg->ctrl_msg.type == UDEV_CTRL_START_EXEC_QUEUE)
return 1;
return -1;
}
int udev_ctrl_get_reload_rules(struct udev_ctrl_msg *ctrl_msg)
{
if (ctrl_msg->ctrl_msg.type == UDEV_CTRL_RELOAD_RULES)
return 1;
return -1;
}
const char *udev_ctrl_get_set_env(struct udev_ctrl_msg *ctrl_msg)
{
if (ctrl_msg->ctrl_msg.type == UDEV_CTRL_SET_ENV)
return ctrl_msg->ctrl_msg.buf;
return NULL;
}
int udev_ctrl_get_set_max_childs(struct udev_ctrl_msg *ctrl_msg)
{
if (ctrl_msg->ctrl_msg.type == UDEV_CTRL_SET_MAX_CHILDS)
return ctrl_msg->ctrl_msg.intval;
return -1;
}
int udev_ctrl_get_set_max_childs_running(struct udev_ctrl_msg *ctrl_msg)
{
if (ctrl_msg->ctrl_msg.type == UDEV_CTRL_SET_MAX_CHILDS_RUNNING)
return ctrl_msg->ctrl_msg.intval;
return -1;
}

View File

@ -37,7 +37,9 @@
struct udev_monitor {
struct udev *udev;
int refcount;
int socket;
int sock;
struct sockaddr_un saddr;
socklen_t addrlen;
};
/**
@ -60,9 +62,6 @@ struct udev_monitor {
struct udev_monitor *udev_monitor_new_from_socket(struct udev *udev, const char *socket_path)
{
struct udev_monitor *udev_monitor;
struct sockaddr_un saddr;
socklen_t addrlen;
const int on = 1;
if (udev == NULL)
return NULL;
@ -75,34 +74,38 @@ struct udev_monitor *udev_monitor_new_from_socket(struct udev *udev, const char
udev_monitor->refcount = 1;
udev_monitor->udev = udev;
memset(&saddr, 0x00, sizeof(saddr));
saddr.sun_family = AF_LOCAL;
strcpy(saddr.sun_path, socket_path);
addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(saddr.sun_path);
udev_monitor->saddr.sun_family = AF_LOCAL;
strcpy(udev_monitor->saddr.sun_path, socket_path);
udev_monitor->addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(udev_monitor->saddr.sun_path);
/* translate leading '@' to abstract namespace */
if (saddr.sun_path[0] == '@')
saddr.sun_path[0] = '\0';
if (udev_monitor->saddr.sun_path[0] == '@')
udev_monitor->saddr.sun_path[0] = '\0';
udev_monitor->socket = socket(AF_LOCAL, SOCK_DGRAM, 0);
if (udev_monitor->socket == -1) {
udev_monitor->sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
if (udev_monitor->sock == -1) {
err(udev, "error getting socket: %s\n", strerror(errno));
free(udev_monitor);
return NULL;
}
return udev_monitor;
}
if (bind(udev_monitor->socket, (struct sockaddr *) &saddr, addrlen) < 0) {
err(udev, "bind failed: %s\n", strerror(errno));
close(udev_monitor->socket);
free(udev_monitor);
return NULL;
int udev_monitor_enable_receiving(struct udev_monitor *udev_monitor)
{
int err;
const int on = 1;
err = bind(udev_monitor->sock, (struct sockaddr *) &udev_monitor->saddr, udev_monitor->addrlen);
if (err < 0) {
err(udev_monitor->udev, "bind failed: %s\n", strerror(errno));
return err;
}
/* enable receiving of the sender credentials */
setsockopt(udev_monitor->socket, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on));
setsockopt(udev_monitor->sock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on));
info(udev_monitor->udev, "udev_monitor: %p created\n", udev_monitor);
return udev_monitor;
return 0;
}
/**
@ -137,7 +140,8 @@ void udev_monitor_unref(struct udev_monitor *udev_monitor)
udev_monitor->refcount--;
if (udev_monitor->refcount > 0)
return;
close(udev_monitor->socket);
if (udev_monitor->sock >= 0)
close(udev_monitor->sock);
info(udev_monitor->udev, "udev_monitor: %p released\n", udev_monitor);
free(udev_monitor);
}
@ -169,14 +173,14 @@ int udev_monitor_get_fd(struct udev_monitor *udev_monitor)
{
if (udev_monitor == NULL)
return -1;
return udev_monitor->socket;
return udev_monitor->sock;
}
/**
* udev_monitor_get_device:
* udev_monitor_receive_device:
* @udev_monitor: udev monitor
*
* Retrieve data from the udev monitor socket, allocate a new udev
* Receive data from the udev monitor socket, allocate a new udev
* device, fill in the received data, and return the device.
*
* Only socket connections with uid=0 are accepted. The caller
@ -188,7 +192,7 @@ int udev_monitor_get_fd(struct udev_monitor *udev_monitor)
*
* Returns: a new udev device, or #NULL, in case of an error
**/
struct udev_device *udev_monitor_get_device(struct udev_monitor *udev_monitor)
struct udev_device *udev_monitor_receive_device(struct udev_monitor *udev_monitor)
{
struct udev_device *udev_device;
struct msghdr smsg;
@ -210,7 +214,7 @@ struct udev_device *udev_monitor_get_device(struct udev_monitor *udev_monitor)
smsg.msg_control = cred_msg;
smsg.msg_controllen = sizeof(cred_msg);
if (recvmsg(udev_monitor->socket, &smsg, 0) < 0) {
if (recvmsg(udev_monitor->sock, &smsg, 0) < 0) {
if (errno != EINTR)
info(udev_monitor->udev, "unable to receive message");
return NULL;

View File

@ -61,6 +61,34 @@ extern int device_set_devname(struct udev_device *udev_device, const char *devna
extern int device_add_devlink(struct udev_device *udev_device, const char *devlink);
extern int device_add_property(struct udev_device *udev_device, const char *property);
/* udev_ctrl - daemon runtime setup */
struct udev_ctrl;
extern struct udev_ctrl *udev_ctrl_new_from_socket(struct udev *udev, const char *socket_path);
extern int udev_ctrl_enable_receiving(struct udev_ctrl *uctrl);
extern struct udev_ctrl *udev_ctrl_ref(struct udev_ctrl *uctrl);
extern void udev_ctrl_unref(struct udev_ctrl *uctrl);
extern struct udev *udev_ctrl_get_udev(struct udev_ctrl *uctrl);
extern int udev_ctrl_get_fd(struct udev_ctrl *uctrl);
extern int udev_ctrl_send_set_log_level(struct udev_ctrl *uctrl, int priority);
extern int udev_ctrl_send_stop_exec_queue(struct udev_ctrl *uctrl);
extern int udev_ctrl_send_start_exec_queue(struct udev_ctrl *uctrl);
extern int udev_ctrl_send_reload_rules(struct udev_ctrl *uctrl);
extern int udev_ctrl_send_set_env(struct udev_ctrl *uctrl, const char *key);
extern int udev_ctrl_send_set_max_childs(struct udev_ctrl *uctrl, int count);
extern int udev_ctrl_send_set_max_childs_running(struct udev_ctrl *uctrl, int count);
struct udev_ctrl_msg;
extern struct udev_ctrl_msg *udev_ctrl_msg(struct udev_ctrl *uctrl);
extern struct udev_ctrl_msg *udev_ctrl_receive_msg(struct udev_ctrl *uctrl);
extern struct udev_ctrl_msg *udev_ctrl_msg_ref(struct udev_ctrl_msg *ctrl_msg);
extern void udev_ctrl_msg_unref(struct udev_ctrl_msg *ctrl_msg);
extern int udev_ctrl_get_set_log_level(struct udev_ctrl_msg *ctrl_msg);
extern int udev_ctrl_get_stop_exec_queue(struct udev_ctrl_msg *ctrl_msg);
extern int udev_ctrl_get_start_exec_queue(struct udev_ctrl_msg *ctrl_msg);
extern int udev_ctrl_get_reload_rules(struct udev_ctrl_msg *ctrl_msg);
extern const char *udev_ctrl_get_set_env(struct udev_ctrl_msg *ctrl_msg);
extern int udev_ctrl_get_set_max_childs(struct udev_ctrl_msg *ctrl_msg);
extern int udev_ctrl_get_set_max_childs_running(struct udev_ctrl_msg *ctrl_msg);
/* libudev-utils */
extern ssize_t util_get_sys_subsystem(struct udev *udev, const char *devpath, char *subsystem, size_t size);
#endif

View File

@ -28,9 +28,6 @@
#endif
struct udev;
struct udev_device;
struct udev_monitor;
extern struct udev *udev_new(void);
extern struct udev *udev_ref(struct udev *udev);
extern void udev_unref(struct udev *udev);
@ -43,6 +40,7 @@ extern void udev_set_log_priority(struct udev *udev, int priority);
extern const char *udev_get_sys_path(struct udev *udev);
extern const char *udev_get_dev_path(struct udev *udev);
struct udev_device;
extern struct udev_device *udev_device_new_from_devpath(struct udev *udev, const char *devpath);
extern struct udev_device *udev_device_ref(struct udev_device *udev_device);
extern void udev_device_unref(struct udev_device *udev_device);
@ -65,11 +63,13 @@ extern int udev_devices_enumerate(struct udev *udev, const char *subsystem,
const char *devpath, const char *subsystem, const char *name, void *data),
void *data);
struct udev_monitor;
extern struct udev_monitor *udev_monitor_new_from_socket(struct udev *udev, const char *socket_path);
extern int udev_monitor_enable_receiving(struct udev_monitor *udev_monitor);
extern struct udev_monitor *udev_monitor_ref(struct udev_monitor *udev_monitor);
extern void udev_monitor_unref(struct udev_monitor *udev_monitor);
extern struct udev *udev_monitor_get_udev(struct udev_monitor *udev_monitor);
extern int udev_monitor_get_fd(struct udev_monitor *udev_monitor);
extern struct udev_device *udev_monitor_get_device(struct udev_monitor *udev_monitor);
extern struct udev_device *udev_monitor_receive_device(struct udev_monitor *udev_monitor);
#endif

View File

@ -112,6 +112,10 @@ static int test_monitor(struct udev *udev, const char *socket_path)
printf("no socket\n");
return -1;
}
if (udev_monitor_enable_receiving(udev_monitor) < 0) {
printf("bind failed\n");
return -1;
}
fd = udev_monitor_get_fd(udev_monitor);
FD_ZERO(&readfds);
@ -128,7 +132,7 @@ static int test_monitor(struct udev *udev, const char *socket_path)
printf("select fd count: %i\n", fdcount);
if (FD_ISSET(fd, &readfds)) {
device = udev_monitor_get_device(udev_monitor);
device = udev_monitor_receive_device(udev_monitor);
if (device == NULL) {
printf("no device from socket\n");
continue;

View File

@ -1,135 +0,0 @@
/*
* Copyright (C) 2005-2008 Kay Sievers <kay.sievers@vrfy.org>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation version 2 of the License.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include "config.h"
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include "udev.h"
#include "udevd.h"
struct udev_ctrl {
struct udev *udev;
int sock;
struct sockaddr_un saddr;
socklen_t addrlen;
};
struct udev_ctrl *udev_ctrl_new_from_socket(struct udev *udev, const char *socket_path)
{
struct udev_ctrl *uctrl;
uctrl = malloc(sizeof(struct udev_ctrl));
if (uctrl == NULL)
return NULL;
memset(uctrl, 0x00, sizeof(struct udev_ctrl));
uctrl->udev = udev;
uctrl->sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
if (uctrl->sock < 0) {
err(udev, "error getting socket: %s\n", strerror(errno));
free(uctrl);
return NULL;
}
uctrl->saddr.sun_family = AF_LOCAL;
strcpy(uctrl->saddr.sun_path, socket_path);
uctrl->addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(uctrl->saddr.sun_path);
/* translate leading '@' to abstract namespace */
if (uctrl->saddr.sun_path[0] == '@')
uctrl->saddr.sun_path[0] = '\0';
return uctrl;
}
void udev_ctrl_unref(struct udev_ctrl *uctrl)
{
if (uctrl == NULL)
return;
close(uctrl->sock);
}
static int ctrl_send(struct udev_ctrl *uctrl, enum udevd_ctrl_msg_type type, int intval, const char *buf)
{
struct udevd_ctrl_msg ctrl_msg;
int err;
memset(&ctrl_msg, 0x00, sizeof(struct udevd_ctrl_msg));
strcpy(ctrl_msg.magic, UDEVD_CTRL_MAGIC);
ctrl_msg.type = type;
if (buf != NULL)
strlcpy(ctrl_msg.buf, buf, sizeof(ctrl_msg.buf));
else
ctrl_msg.intval = intval;
err = sendto(uctrl->sock, &ctrl_msg, sizeof(ctrl_msg), 0, (struct sockaddr *)&uctrl->saddr, uctrl->addrlen);
if (err == -1) {
err(uctrl->udev, "error sending message: %s\n", strerror(errno));
}
return err;
}
int udev_ctrl_set_log_level(struct udev_ctrl *uctrl, int priority)
{
ctrl_send(uctrl, UDEVD_CTRL_SET_LOG_LEVEL, priority, NULL);
return 0;
}
int udev_ctrl_stop_exec_queue(struct udev_ctrl *uctrl)
{
ctrl_send(uctrl, UDEVD_CTRL_STOP_EXEC_QUEUE, 0, NULL);
return 0;
}
int udev_ctrl_start_exec_queue(struct udev_ctrl *uctrl)
{
ctrl_send(uctrl, UDEVD_CTRL_START_EXEC_QUEUE, 0, NULL);
return 0;
}
int udev_ctrl_reload_rules(struct udev_ctrl *uctrl)
{
ctrl_send(uctrl, UDEVD_CTRL_RELOAD_RULES, 0, NULL);
return 0;
}
int udev_ctrl_set_env(struct udev_ctrl *uctrl, const char *key)
{
ctrl_send(uctrl, UDEVD_CTRL_ENV, 0, optarg);
return 0;
}
int udev_ctrl_set_max_childs(struct udev_ctrl *uctrl, int count)
{
ctrl_send(uctrl, UDEVD_CTRL_SET_MAX_CHILDS, count, NULL);
return 0;
}
int udev_ctrl_set_max_childs_running(struct udev_ctrl *uctrl, int count)
{
ctrl_send(uctrl, UDEVD_CTRL_SET_MAX_CHILDS_RUNNING, count, NULL);
return 0;
}

View File

@ -44,6 +44,12 @@
#define DEFAULT_PARTITIONS_COUNT 15
#define UDEV_EVENT_TIMEOUT 180
/* linux/include/linux/kobject.h */
#define UEVENT_BUFFER_SIZE 2048
#define UEVENT_NUM_ENVP 32
#define UDEV_CTRL_SOCK_PATH "@" UDEV_PREFIX "/org/kernel/udev/udevd"
#define UDEV_MAX(a,b) ((a) > (b) ? (a) : (b))
/* pipes */
@ -190,16 +196,4 @@ extern int udevadm_trigger(struct udev *udev, int argc, char *argv[]);
extern int udevadm_settle(struct udev *udev, int argc, char *argv[]);
extern int udevadm_test(struct udev *udev, int argc, char *argv[]);
/* udev_ctrl - daemon runtime setup */
struct udev_ctrl;
extern struct udev_ctrl *udev_ctrl_new_from_socket(struct udev *udev, const char *socket_path);
extern void udev_ctrl_unref(struct udev_ctrl *uctrl);
extern int udev_ctrl_set_log_level(struct udev_ctrl *uctrl, int priority);
extern int udev_ctrl_stop_exec_queue(struct udev_ctrl *uctrl);
extern int udev_ctrl_start_exec_queue(struct udev_ctrl *uctrl);
extern int udev_ctrl_reload_rules(struct udev_ctrl *uctrl);
extern int udev_ctrl_set_env(struct udev_ctrl *uctrl, const char *key);
extern int udev_ctrl_set_max_childs(struct udev_ctrl *uctrl, int count);
extern int udev_ctrl_set_max_childs_running(struct udev_ctrl *uctrl, int count);
#endif

View File

@ -32,11 +32,23 @@
#include <sys/un.h>
#include "udev.h"
#include "udevd.h"
static void print_help(void)
{
printf("Usage: udevadm control COMMAND\n"
" --log-priority=<level> set the udev log level for the daemon\n"
" --stop-exec-queue keep udevd from executing events, queue only\n"
" --start-exec-queue execute events, flush queue\n"
" --reload-rules reloads the rules files\n"
" --env=<KEY>=<value> set a global environment variable\n"
" --max-childs=<N> maximum number of childs\n"
" --max-childs-running=<N> maximum number of childs running at the same time\n"
" --help print this help text\n\n");
}
int udevadm_control(struct udev *udev, int argc, char *argv[])
{
struct udev_ctrl *uctrl;
struct udev_ctrl *uctrl = NULL;
int rc = 1;
/* compat values with '_' will be removed in a future release */
@ -63,7 +75,7 @@ int udevadm_control(struct udev *udev, int argc, char *argv[])
goto exit;
}
uctrl = udev_ctrl_new_from_socket(udev, UDEVD_CTRL_SOCK_PATH);
uctrl = udev_ctrl_new_from_socket(udev, UDEV_CTRL_SOCK_PATH);
if (uctrl == NULL)
goto exit;
@ -91,26 +103,31 @@ int udevadm_control(struct udev *udev, int argc, char *argv[])
fprintf(stderr, "invalid number '%s'\n", optarg);
goto exit;
}
udev_ctrl_set_log_level(uctrl, log_priority(optarg));
udev_ctrl_send_set_log_level(uctrl, log_priority(optarg));
rc = 0;
break;
case 's':
case 's' + 256:
udev_ctrl_stop_exec_queue(uctrl);
udev_ctrl_send_stop_exec_queue(uctrl);
rc = 0;
break;
case 'S':
case 'S' + 256:
udev_ctrl_start_exec_queue(uctrl);
udev_ctrl_send_start_exec_queue(uctrl);
rc = 0;
break;
case 'R':
case 'R' + 256:
udev_ctrl_reload_rules(uctrl);
udev_ctrl_send_reload_rules(uctrl);
rc = 0;
break;
case 'e':
if (strchr(optarg, '=') == NULL) {
fprintf(stderr, "expect <KEY>=<valaue> instead of '%s'\n", optarg);
goto exit;
}
udev_ctrl_set_env(uctrl, optarg);
udev_ctrl_send_set_env(uctrl, optarg);
rc = 0;
break;
case 'm':
case 'm' + 256:
@ -119,7 +136,8 @@ int udevadm_control(struct udev *udev, int argc, char *argv[])
fprintf(stderr, "invalid number '%s'\n", optarg);
goto exit;
}
udev_ctrl_set_max_childs(uctrl, i);
udev_ctrl_send_set_max_childs(uctrl, i);
rc = 0;
break;
case 'M':
case 'M' + 256:
@ -128,18 +146,12 @@ int udevadm_control(struct udev *udev, int argc, char *argv[])
fprintf(stderr, "invalid number '%s'\n", optarg);
goto exit;
}
udev_ctrl_set_max_childs_running(uctrl, i);
udev_ctrl_send_set_max_childs_running(uctrl, i);
rc = 0;
break;
case 'h':
printf("Usage: udevadm control COMMAND\n"
" --log-priority=<level> set the udev log level for the daemon\n"
" --stop-exec-queue keep udevd from executing events, queue only\n"
" --start-exec-queue execute events, flush queue\n"
" --reload-rules reloads the rules files\n"
" --env=<KEY>=<value> set a global environment variable\n"
" --max-childs=<N> maximum number of childs\n"
" --max-childs-running=<N> maximum number of childs running at the same time\n"
" --help print this help text\n\n");
print_help();
rc = 0;
goto exit;
default:
goto exit;
@ -156,24 +168,40 @@ int udevadm_control(struct udev *udev, int argc, char *argv[])
"this will stop working in a future release\n");
if (!strncmp(arg, "log_priority=", strlen("log_priority="))) {
udev_ctrl_set_log_level(uctrl, log_priority(&arg[strlen("log_priority=")]));
udev_ctrl_send_set_log_level(uctrl, log_priority(&arg[strlen("log_priority=")]));
rc = 0;
goto exit;
} else if (!strcmp(arg, "stop_exec_queue")) {
udev_ctrl_stop_exec_queue(uctrl);
udev_ctrl_send_stop_exec_queue(uctrl);
rc = 0;
goto exit;
} else if (!strcmp(arg, "start_exec_queue")) {
udev_ctrl_start_exec_queue(uctrl);
udev_ctrl_send_start_exec_queue(uctrl);
rc = 0;
goto exit;
} else if (!strcmp(arg, "reload_rules")) {
udev_ctrl_reload_rules(uctrl);
udev_ctrl_send_reload_rules(uctrl);
rc = 0;
goto exit;
} else if (!strncmp(arg, "max_childs=", strlen("max_childs="))) {
udev_ctrl_set_max_childs(uctrl, strtoul(&arg[strlen("max_childs=")], NULL, 0));
udev_ctrl_send_set_max_childs(uctrl, strtoul(&arg[strlen("max_childs=")], NULL, 0));
rc = 0;
goto exit;
} else if (!strncmp(arg, "max_childs_running=", strlen("max_childs_running="))) {
udev_ctrl_set_max_childs_running(uctrl, strtoul(&arg[strlen("max_childs_running=")], NULL, 0));
udev_ctrl_send_set_max_childs_running(uctrl, strtoul(&arg[strlen("max_childs_running=")], NULL, 0));
rc = 0;
goto exit;
} else if (!strncmp(arg, "env", strlen("env"))) {
udev_ctrl_set_env(uctrl, &arg[strlen("env=")]);
} else {
fprintf(stderr, "unrecognized command '%s'\n", arg);
err(udev, "unrecognized command '%s'\n", arg);
udev_ctrl_send_set_env(uctrl, &arg[strlen("env=")]);
rc = 0;
goto exit;
}
}
if (rc != 0) {
fprintf(stderr, "unrecognized command\n");
err(udev, "unrecognized command\n");
}
exit:
udev_ctrl_unref(uctrl);
return rc;

View File

@ -33,7 +33,6 @@
#include <linux/netlink.h>
#include "udev.h"
#include "udevd.h"
static int uevent_netlink_sock = -1;
static int udev_monitor_sock = -1;

View File

@ -30,7 +30,6 @@
#include <sys/types.h>
#include "udev.h"
#include "udevd.h"
#define DEFAULT_TIMEOUT 180
#define LOOP_PER_SECOND 20

View File

@ -34,7 +34,6 @@
#include <sys/un.h>
#include "udev.h"
#include "udevd.h"
#include "udev_rules.h"
static int verbose;

View File

@ -47,9 +47,16 @@
#include "udev.h"
#include "udev_rules.h"
#include "udevd.h"
#include "udev_selinux.h"
#define UDEVD_PRIORITY -4
#define UDEV_PRIORITY -2
/* maximum limit of forked childs */
#define UDEVD_MAX_CHILDS 256
/* start to throttle forking if maximum number of running childs in our session is reached */
#define UDEVD_MAX_CHILDS_RUNNING 16
static int debug;
static void log_fn(struct udev *udev, int priority,
@ -85,7 +92,7 @@ struct udevd_uevent_msg {
static int debug_trace;
static struct udev_rules rules;
static int udevd_sock = -1;
static struct udev_ctrl *udev_ctrl;
static int uevent_netlink_sock = -1;
static int inotify_fd = -1;
static pid_t sid;
@ -248,7 +255,7 @@ static void udev_event_run(struct udevd_uevent_msg *msg)
case 0:
/* child */
close(uevent_netlink_sock);
close(udevd_sock);
udev_ctrl_unref(udev_ctrl);
if (inotify_fd >= 0)
close(inotify_fd);
close(signal_pipe[READ_END]);
@ -665,97 +672,76 @@ static struct udevd_uevent_msg *get_msg_from_envbuf(struct udev *udev, const cha
}
/* receive the udevd message from userspace */
static void get_ctrl_msg(struct udev *udev)
static void handle_ctrl_msg(struct udev_ctrl *uctrl)
{
struct udevd_ctrl_msg ctrl_msg;
ssize_t size;
struct msghdr smsg;
struct cmsghdr *cmsg;
struct iovec iov;
struct ucred *cred;
char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
char *pos;
struct udev *udev = udev_ctrl_get_udev(uctrl);
struct udev_ctrl_msg *ctrl_msg;
const char *str;
int i;
memset(&ctrl_msg, 0x00, sizeof(struct udevd_ctrl_msg));
iov.iov_base = &ctrl_msg;
iov.iov_len = sizeof(struct udevd_ctrl_msg);
memset(&smsg, 0x00, sizeof(struct msghdr));
smsg.msg_iov = &iov;
smsg.msg_iovlen = 1;
smsg.msg_control = cred_msg;
smsg.msg_controllen = sizeof(cred_msg);
size = recvmsg(udevd_sock, &smsg, 0);
if (size < 0) {
if (errno != EINTR)
err(udev, "unable to receive user udevd message: %s\n", strerror(errno));
ctrl_msg = udev_ctrl_receive_msg(uctrl);
if (ctrl_msg == NULL)
return;
}
cmsg = CMSG_FIRSTHDR(&smsg);
cred = (struct ucred *) CMSG_DATA(cmsg);
if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) {
err(udev, "no sender credentials received, message ignored\n");
return;
i = udev_ctrl_get_set_log_level(ctrl_msg);
if (i >= 0) {
info(udev, "udevd message (SET_LOG_PRIORITY) received, log_priority=%i\n", i);
udev_set_log_priority(udev, i);
sprintf(udev_log_env, "UDEV_LOG=%i", i);
putenv(udev_log_env);
}
if (cred->uid != 0) {
err(udev, "sender uid=%i, message ignored\n", cred->uid);
return;
}
if (strncmp(ctrl_msg.magic, UDEVD_CTRL_MAGIC, sizeof(UDEVD_CTRL_MAGIC)) != 0 ) {
err(udev, "message magic '%s' doesn't match, ignore it\n", ctrl_msg.magic);
return;
}
switch (ctrl_msg.type) {
case UDEVD_CTRL_ENV:
pos = strchr(ctrl_msg.buf, '=');
if (pos == NULL) {
err(udev, "wrong key format '%s'\n", ctrl_msg.buf);
break;
}
pos[0] = '\0';
if (pos[1] == '\0') {
info(udev, "udevd message (ENV) received, unset '%s'\n", ctrl_msg.buf);
unsetenv(ctrl_msg.buf);
} else {
info(udev, "udevd message (ENV) received, set '%s=%s'\n", ctrl_msg.buf, &pos[1]);
setenv(ctrl_msg.buf, &pos[1], 1);
}
break;
case UDEVD_CTRL_STOP_EXEC_QUEUE:
if (udev_ctrl_get_stop_exec_queue(ctrl_msg) > 0) {
info(udev, "udevd message (STOP_EXEC_QUEUE) received\n");
stop_exec_q = 1;
break;
case UDEVD_CTRL_START_EXEC_QUEUE:
}
if (udev_ctrl_get_start_exec_queue(ctrl_msg) > 0) {
info(udev, "udevd message (START_EXEC_QUEUE) received\n");
stop_exec_q = 0;
msg_queue_manager(udev);
break;
case UDEVD_CTRL_SET_LOG_LEVEL:
info(udev, "udevd message (SET_LOG_PRIORITY) received, log_priority=%i\n", ctrl_msg.intval);
udev_set_log_priority(udev, ctrl_msg.intval);
sprintf(udev_log_env, "UDEV_LOG=%i", udev_get_log_priority(udev));
putenv(udev_log_env);
break;
case UDEVD_CTRL_SET_MAX_CHILDS:
info(udev, "udevd message (UDEVD_SET_MAX_CHILDS) received, max_childs=%i\n", ctrl_msg.intval);
max_childs = ctrl_msg.intval;
break;
case UDEVD_CTRL_SET_MAX_CHILDS_RUNNING:
info(udev, "udevd message (UDEVD_SET_MAX_CHILDS_RUNNING) received, max_childs_running=%i\n", ctrl_msg.intval);
max_childs_running = ctrl_msg.intval;
break;
case UDEVD_CTRL_RELOAD_RULES:
}
if (udev_ctrl_get_reload_rules(ctrl_msg) > 0) {
info(udev, "udevd message (RELOAD_RULES) received\n");
reload_config = 1;
break;
default:
err(udev, "unknown control message type\n");
}
str = udev_ctrl_get_set_env(ctrl_msg);
if (str != NULL) {
char *key = strdup(str);
char *val;
val = strchr(str, '=');
if (val != NULL) {
val[0] = '\0';
val = &val[1];
if (val[0] == '\0') {
info(udev, "udevd message (ENV) received, unset '%s'\n", key);
unsetenv(str);
} else {
info(udev, "udevd message (ENV) received, set '%s=%s'\n", key, val);
setenv(key, val, 1);
}
} else {
err(udev, "wrong key format '%s'\n", key);
}
free(key);
}
i = udev_ctrl_get_set_max_childs(ctrl_msg);
if (i >= 0) {
info(udev, "udevd message (SET_MAX_CHILDS) received, max_childs=%i\n", i);
max_childs = i;
}
i = udev_ctrl_get_set_max_childs_running(ctrl_msg);
if (i > 0) {
info(udev, "udevd message (SET_MAX_CHILDS_RUNNING) received, max_childs_running=%i\n", i);
max_childs_running = i;
}
udev_ctrl_msg_unref(ctrl_msg);
}
/* receive the kernel user event message and do some sanity checks */
@ -867,42 +853,6 @@ static void reap_sigchilds(void)
}
}
static int init_udevd_socket(struct udev *udev)
{
struct sockaddr_un saddr;
socklen_t addrlen;
const int feature_on = 1;
int retval;
memset(&saddr, 0x00, sizeof(saddr));
saddr.sun_family = AF_LOCAL;
strcpy(saddr.sun_path, UDEVD_CTRL_SOCK_PATH);
addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(saddr.sun_path);
/* translate leading '@' to abstract namespace */
if (saddr.sun_path[0] == '@')
saddr.sun_path[0] = '\0';
udevd_sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
if (udevd_sock == -1) {
err(udev, "error getting socket: %s\n", strerror(errno));
return -1;
}
/* the bind takes care of ensuring only one copy running */
retval = bind(udevd_sock, (struct sockaddr *) &saddr, addrlen);
if (retval < 0) {
err(udev, "bind failed: %s\n", strerror(errno));
close(udevd_sock);
udevd_sock = -1;
return -1;
}
/* enable receiving of the sender credentials */
setsockopt(udevd_sock, SOL_SOCKET, SO_PASSCRED, &feature_on, sizeof(feature_on));
return 0;
}
static int init_uevent_netlink_sock(struct udev *udev)
{
struct sockaddr_nl snl;
@ -1040,17 +990,19 @@ int main(int argc, char *argv[])
if (write(STDERR_FILENO, 0, 0) < 0)
dup2(fd, STDERR_FILENO);
/* init sockets to receive events */
if (init_udevd_socket(udev) < 0) {
if (errno == EADDRINUSE) {
fprintf(stderr, "another udev daemon already running\n");
err(udev, "another udev daemon already running\n");
/* init control socket, bind() ensures, that only one udevd instance is running */
udev_ctrl = udev_ctrl_new_from_socket(udev, UDEV_CTRL_SOCK_PATH);
if (udev_ctrl == NULL) {
fprintf(stderr, "error initializing control socket");
err(udev, "error initializing udevd socket");
rc = 1;
} else {
fprintf(stderr, "error initializing udevd socket\n");
err(udev, "error initializing udevd socket\n");
rc = 2;
goto exit;
}
if (udev_ctrl_enable_receiving(udev_ctrl) < 0) {
fprintf(stderr, "error binding control socket, seems udevd is already running\n");
err(udev, "error binding control socket, seems udevd is already running\n");
rc = 1;
goto exit;
}
@ -1061,7 +1013,6 @@ int main(int argc, char *argv[])
goto exit;
}
/* setup signal handler pipe */
retval = pipe(signal_pipe);
if (retval < 0) {
err(udev, "error getting pipes: %s\n", strerror(errno));
@ -1115,10 +1066,11 @@ int main(int argc, char *argv[])
}
}
/* redirect std{out,err} fd's */
if (!debug)
/* redirect std{out,err} */
if (!debug) {
dup2(fd, STDOUT_FILENO);
dup2(fd, STDERR_FILENO);
}
if (fd > STDERR_FILENO)
close(fd);
@ -1219,7 +1171,7 @@ int main(int argc, char *argv[])
if (debug_trace)
putenv("DEBUG=1");
maxfd = udevd_sock;
maxfd = udev_ctrl_get_fd(udev_ctrl);
maxfd = UDEV_MAX(maxfd, uevent_netlink_sock);
maxfd = UDEV_MAX(maxfd, signal_pipe[READ_END]);
maxfd = UDEV_MAX(maxfd, inotify_fd);
@ -1230,7 +1182,7 @@ int main(int argc, char *argv[])
FD_ZERO(&readfds);
FD_SET(signal_pipe[READ_END], &readfds);
FD_SET(udevd_sock, &readfds);
FD_SET(udev_ctrl_get_fd(udev_ctrl), &readfds);
FD_SET(uevent_netlink_sock, &readfds);
if (inotify_fd >= 0)
FD_SET(inotify_fd, &readfds);
@ -1243,8 +1195,8 @@ int main(int argc, char *argv[])
}
/* get control message */
if (FD_ISSET(udevd_sock, &readfds))
get_ctrl_msg(udev);
if (FD_ISSET(udev_ctrl_get_fd(udev_ctrl), &readfds))
handle_ctrl_msg(udev_ctrl);
/* get netlink message */
if (FD_ISSET(uevent_netlink_sock, &readfds)) {
@ -1311,8 +1263,7 @@ exit:
if (signal_pipe[WRITE_END] >= 0)
close(signal_pipe[WRITE_END]);
if (udevd_sock >= 0)
close(udevd_sock);
udev_ctrl_unref(udev_ctrl);
if (inotify_fd >= 0)
close(inotify_fd);
if (uevent_netlink_sock >= 0)

View File

@ -1,56 +0,0 @@
/*
* Copyright (C) 2004 Ling, Xiaofeng <xiaofeng.ling@intel.com>
* Copyright (C) 2004-2006 Kay Sievers <kay.sievers@vrfy.org>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation version 2 of the License.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include "list.h"
#define UDEVD_PRIORITY -4
#define UDEV_PRIORITY -2
/* maximum limit of forked childs */
#define UDEVD_MAX_CHILDS 256
/* start to throttle forking if maximum number of running childs in our session is reached */
#define UDEVD_MAX_CHILDS_RUNNING 16
/* linux/include/linux/kobject.h */
#define UEVENT_BUFFER_SIZE 2048
#define UEVENT_NUM_ENVP 32
#define UDEVD_CTRL_SOCK_PATH "@" UDEV_PREFIX "/org/kernel/udev/udevd"
#define UDEVD_CTRL_MAGIC "udevd-128"
enum udevd_ctrl_msg_type {
UDEVD_CTRL_UNKNOWN,
UDEVD_CTRL_SET_LOG_LEVEL,
UDEVD_CTRL_STOP_EXEC_QUEUE,
UDEVD_CTRL_START_EXEC_QUEUE,
UDEVD_CTRL_RELOAD_RULES,
UDEVD_CTRL_ENV,
UDEVD_CTRL_SET_MAX_CHILDS,
UDEVD_CTRL_SET_MAX_CHILDS_RUNNING,
};
struct udevd_ctrl_msg {
char magic[32];
enum udevd_ctrl_msg_type type;
union {
int intval;
char buf[256];
};
};