diff options
author | Kay Sievers <kay.sievers@vrfy.org> | 2008-09-08 17:59:00 +0200 |
---|---|---|
committer | Kay Sievers <kay.sievers@vrfy.org> | 2008-09-08 17:59:00 +0200 |
commit | d59f11e1407ec6fa26e3a6f20b2d404af6978199 (patch) | |
tree | daf47b7982035323fd4323b260275e6fc9a5669a /udev/lib | |
parent | e5931bb7d86226a3823bdf5f4ec01c857827d386 (diff) |
move udev_ctrl to libudev-private
Diffstat (limited to 'udev/lib')
-rw-r--r-- | udev/lib/Makefile.am | 2 | ||||
-rw-r--r-- | udev/lib/exported_symbols | 3 | ||||
-rw-r--r-- | udev/lib/libudev-ctrl.c | 332 | ||||
-rw-r--r-- | udev/lib/libudev-monitor.c | 58 | ||||
-rw-r--r-- | udev/lib/libudev-private.h | 28 | ||||
-rw-r--r-- | udev/lib/libudev.h | 8 | ||||
-rw-r--r-- | udev/lib/test-libudev.c | 6 |
7 files changed, 404 insertions, 33 deletions
diff --git a/udev/lib/Makefile.am b/udev/lib/Makefile.am index b7dd373d5e..630888d3d9 100644 --- a/udev/lib/Makefile.am +++ b/udev/lib/Makefile.am @@ -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 \ diff --git a/udev/lib/exported_symbols b/udev/lib/exported_symbols index 66275f4f9c..fa12633377 100644 --- a/udev/lib/exported_symbols +++ b/udev/lib/exported_symbols @@ -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 diff --git a/udev/lib/libudev-ctrl.c b/udev/lib/libudev-ctrl.c new file mode 100644 index 0000000000..b2af0c73ed --- /dev/null +++ b/udev/lib/libudev-ctrl.c @@ -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; +} diff --git a/udev/lib/libudev-monitor.c b/udev/lib/libudev-monitor.c index 09845576d9..2cba7caba0 100644 --- a/udev/lib/libudev-monitor.c +++ b/udev/lib/libudev-monitor.c @@ -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; } /** @@ -125,7 +128,7 @@ struct udev_monitor *udev_monitor_ref(struct udev_monitor *udev_monitor) * udev_monitor_unref: * @udev_monitor: udev monitor * - * Drop a reference of a udev monitor. If the refcount reaches zero, + * Drop a reference ofa udev monitor. If the refcount reaches zero, * the bound socket will be closed, and the ressources of the monitor * will be released. * @@ -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; diff --git a/udev/lib/libudev-private.h b/udev/lib/libudev-private.h index ef26ec7e11..b03c813c2f 100644 --- a/udev/lib/libudev-private.h +++ b/udev/lib/libudev-private.h @@ -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 diff --git a/udev/lib/libudev.h b/udev/lib/libudev.h index 98652c5904..a2a06b58e9 100644 --- a/udev/lib/libudev.h +++ b/udev/lib/libudev.h @@ -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 diff --git a/udev/lib/test-libudev.c b/udev/lib/test-libudev.c index d0095f15fc..835536af84 100644 --- a/udev/lib/test-libudev.c +++ b/udev/lib/test-libudev.c @@ -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; |