/*
- * Copyright (C) 2004-2006 Kay Sievers <kay.sievers@vrfy.org>
+ * Copyright (C) 2004-2008 Kay Sievers <kay.sievers@vrfy.org>
* Copyright (C) 2004 Chris Friesen <chris_friesen@sympatico.ca>
*
- * 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 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, either version 2 of the License, or
+ * (at your option) any later version.
*
- * 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.
+ * 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, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#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
static int debug;
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;
static int signal_pipe[2] = {-1, -1};
static volatile int sigchilds_waiting;
static int run_exec_q;
static int stop_exec_q;
static int max_childs;
-static int max_childs_running;
static char udev_log_env[32];
static LIST_HEAD(exec_list);
unlink(filename_failed);
delete_path(msg->udev, filename_failed);
create_path(msg->udev, filename);
- selinux_setfscreatecon(msg->udev, filename, NULL, S_IFLNK);
+ udev_selinux_setfscreatecon(msg->udev, filename, S_IFLNK);
symlink(msg->devpath, filename);
- selinux_resetfscreatecon(msg->udev);
+ udev_selinux_resetfscreatecon(msg->udev);
break;
case EVENT_FINISHED:
if (msg->devpath_old != NULL) {
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]);
return memsize;
}
-static int cpu_count(void)
-{
- FILE* f;
- char buf[4096];
- int count = 0;
-
- f = fopen("/proc/stat", "r");
- if (f == NULL)
- return -1;
-
- while (fgets(buf, sizeof(buf), f) != NULL) {
- if (strncmp(buf, "cpu", 3) == 0 && isdigit(buf[3]))
- count++;
- }
-
- fclose(f);
- if (count == 0)
- return -1;
- return count;
-}
-
-static int running_processes(void)
-{
- FILE* f;
- char buf[4096];
- int running = -1;
-
- f = fopen("/proc/stat", "r");
- if (f == NULL)
- return -1;
-
- while (fgets(buf, sizeof(buf), f) != NULL) {
- int value;
-
- if (sscanf(buf, "procs_running %u", &value) == 1) {
- running = value;
- break;
- }
- }
-
- fclose(f);
- return running;
-}
-
-/* return the number of process es in our session, count only until limit */
-static int running_processes_in_session(pid_t session, int limit)
-{
- DIR *dir;
- struct dirent *dent;
- int running = 0;
-
- dir = opendir("/proc");
- if (!dir)
- return -1;
-
- /* read process info from /proc */
- for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
- int f;
- char procdir[64];
- char line[256];
- const char *pos;
- char state;
- pid_t ppid, pgrp, sess;
- int len;
-
- if (!isdigit(dent->d_name[0]))
- continue;
-
- snprintf(procdir, sizeof(procdir), "/proc/%s/stat", dent->d_name);
- procdir[sizeof(procdir)-1] = '\0';
-
- f = open(procdir, O_RDONLY);
- if (f == -1)
- continue;
-
- len = read(f, line, sizeof(line)-1);
- close(f);
-
- if (len <= 0)
- continue;
- else
- line[len] = '\0';
-
- /* skip ugly program name */
- pos = strrchr(line, ')') + 2;
- if (pos == NULL)
- continue;
-
- if (sscanf(pos, "%c %d %d %d ", &state, &ppid, &pgrp, &sess) != 4)
- continue;
-
- /* count only processes in our session */
- if (sess != session)
- continue;
-
- /* count only running, no sleeping processes */
- if (state != 'R')
- continue;
-
- running++;
- if (limit > 0 && running >= limit)
- break;
- }
- closedir(dir);
-
- return running;
-}
-
static int compare_devpath(const char *running, const char *waiting)
{
int i;
/* check run queue for still running events */
list_for_each_entry(loop_msg, &running_list, node) {
- if (limit && childs_count++ > limit) {
- dbg(msg->udev, "%llu, maximum number (%i) of childs reached\n", msg->seqnum, childs_count);
+ childs_count++;
+
+ if (childs_count++ >= limit) {
+ info(msg->udev, "%llu, maximum number (%i) of childs reached\n", msg->seqnum, childs_count);
return 1;
}
{
struct udevd_uevent_msg *loop_msg;
struct udevd_uevent_msg *tmp_msg;
- int running;
if (list_empty(&exec_list))
return;
- running = running_processes();
- dbg(udev, "%d processes runnning on system\n", running);
- if (running < 0)
- running = max_childs_running;
-
list_for_each_entry_safe(loop_msg, tmp_msg, &exec_list, node) {
- /* check running processes in our session and possibly throttle */
- if (running >= max_childs_running) {
- running = running_processes_in_session(sid, max_childs_running+10);
- dbg(udev, "at least %d processes running in session\n", running);
- if (running >= max_childs_running) {
- dbg(udev, "delay seq %llu, too many processes already running\n", loop_msg->seqnum);
- return;
- }
- }
-
/* serialize and wait for parent or child events */
if (devpath_busy(loop_msg, max_childs) != 0) {
dbg(udev, "delay seq %llu (%s)\n", loop_msg->seqnum, loop_msg->devpath);
/* move event to run list */
list_move_tail(&loop_msg->node, &running_list);
udev_event_run(loop_msg);
- running++;
dbg(udev, "moved seq %llu to running list\n", loop_msg->seqnum);
}
}
}
/* 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;
-
- 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));
- 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;
- }
+ struct udev *udev = udev_ctrl_get_udev(uctrl);
+ struct udev_ctrl_msg *ctrl_msg;
+ const char *str;
+ int i;
- if (cred->uid != 0) {
- err(udev, "sender uid=%i, message ignored\n", cred->uid);
+ ctrl_msg = udev_ctrl_receive_msg(uctrl);
+ if (ctrl_msg == NULL)
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;
+ 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);
}
- 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;
+ }
+
+ udev_ctrl_msg_unref(ctrl_msg);
}
/* receive the kernel user event message and do some sanity checks */
}
}
-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;
logging_init("udevd");
udev_set_log_fn(udev, log_fn);
-
- selinux_init(udev);
dbg(udev, "version %s\n", VERSION);
while (1) {
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");
- rc = 1;
- } else {
- fprintf(stderr, "error initializing udevd socket\n");
- err(udev, "error initializing udevd socket\n");
- rc = 2;
- }
+ /* 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;
+ 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;
}
goto exit;
}
- /* setup signal handler pipe */
retval = pipe(signal_pipe);
if (retval < 0) {
err(udev, "error getting pipes: %s\n", strerror(errno));
}
}
- /* redirect std{out,err} fd's */
- if (!debug)
+ /* redirect std{out,err} */
+ if (!debug) {
dup2(fd, STDOUT_FILENO);
- dup2(fd, STDERR_FILENO);
+ dup2(fd, STDERR_FILENO);
+ }
if (fd > STDERR_FILENO)
close(fd);
chdir("/");
umask(022);
-
- /* become session leader */
- sid = setsid();
- dbg(udev, "our session is %d\n", sid);
+ setsid();
/* OOM_DISABLE == -17 */
fd = open("/proc/self/oom_adj", O_RDWR);
}
info(udev, "initialize max_childs to %u\n", max_childs);
- /* start to throttle forking if maximum number of _running_ childs is reached */
- value = getenv("UDEVD_MAX_CHILDS_RUNNING");
- if (value)
- max_childs_running = strtoull(value, NULL, 10);
- else {
- int cpus = cpu_count();
- if (cpus > 0)
- max_childs_running = 8 + (8 * cpus);
- else
- max_childs_running = UDEVD_MAX_CHILDS_RUNNING;
- }
- info(udev, "initialize max_childs_running to %u\n", max_childs_running);
-
/* clear environment for forked event processes */
clearenv();
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);
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);
}
/* 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)) {
exit:
udev_rules_cleanup(&rules);
sysfs_cleanup();
- selinux_exit(udev);
if (signal_pipe[READ_END] >= 0)
close(signal_pipe[READ_END]);
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)