summaryrefslogtreecommitdiff
path: root/src/udev/udevd.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/udev/udevd.c')
-rw-r--r--src/udev/udevd.c70
1 files changed, 33 insertions, 37 deletions
diff --git a/src/udev/udevd.c b/src/udev/udevd.c
index 615c4ed3e2..34f6a95503 100644
--- a/src/udev/udevd.c
+++ b/src/udev/udevd.c
@@ -1,22 +1,9 @@
/* SPDX-License-Identifier: GPL-2.0+ */
/*
- * Copyright (C) 2004-2012 Kay Sievers <kay@vrfy.org>
- * Copyright (C) 2004 Chris Friesen <chris_friesen@sympatico.ca>
- * Copyright (C) 2009 Canonical Ltd.
- * Copyright (C) 2009 Scott James Remnant <scott@netsplit.com>
+ * Copyright © 2004 Chris Friesen <chris_friesen@sympatico.ca>
+ * Copyright © 2009 Canonical Ltd.
+ * Copyright © 2009 Scott James Remnant <scott@netsplit.com>
*
- * 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, see <http://www.gnu.org/licenses/>.
*/
#include <errno.h>
@@ -233,8 +220,7 @@ static int worker_new(struct worker **ret, Manager *manager, struct udev_monitor
if (r < 0)
return r;
- *ret = worker;
- worker = NULL;
+ *ret = TAKE_PTR(worker);
return 0;
}
@@ -325,9 +311,21 @@ static int worker_send_message(int fd) {
return loop_write(fd, &message, sizeof(message), false);
}
+static bool shall_lock_device(struct udev_device *dev) {
+ const char *sysname;
+
+ if (!streq_ptr("block", udev_device_get_subsystem(dev)))
+ return false;
+
+ sysname = udev_device_get_sysname(dev);
+ return !startswith(sysname, "dm-") &&
+ !startswith(sysname, "md") &&
+ !startswith(sysname, "drbd");
+}
+
static void worker_spawn(Manager *manager, struct event *event) {
struct udev *udev = event->udev;
- _cleanup_udev_monitor_unref_ struct udev_monitor *worker_monitor = NULL;
+ _cleanup_(udev_monitor_unrefp) struct udev_monitor *worker_monitor = NULL;
pid_t pid;
int r = 0;
@@ -353,8 +351,7 @@ static void worker_spawn(Manager *manager, struct event *event) {
sigset_t mask;
/* take initial device from queue */
- dev = event->dev;
- event->dev = NULL;
+ dev = TAKE_PTR(event->dev);
unsetenv("NOTIFY_SOCKET");
@@ -427,9 +424,7 @@ static void worker_spawn(Manager *manager, struct event *event) {
* udev has finished its event handling.
*/
if (!streq_ptr(udev_device_get_action(dev), "remove") &&
- streq_ptr("block", udev_device_get_subsystem(dev)) &&
- !startswith(udev_device_get_sysname(dev), "dm-") &&
- !startswith(udev_device_get_sysname(dev), "md")) {
+ shall_lock_device(dev)) {
struct udev_device *d = dev;
if (streq_ptr("partition", udev_device_get_devtype(d)))
@@ -923,8 +918,8 @@ static int on_uevent(sd_event_source *s, int fd, uint32_t revents, void *userdat
/* receive the udevd message from userspace */
static int on_ctrl_msg(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
Manager *manager = userdata;
- _cleanup_udev_ctrl_connection_unref_ struct udev_ctrl_connection *ctrl_conn = NULL;
- _cleanup_udev_ctrl_msg_unref_ struct udev_ctrl_msg *ctrl_msg = NULL;
+ _cleanup_(udev_ctrl_connection_unrefp) struct udev_ctrl_connection *ctrl_conn = NULL;
+ _cleanup_(udev_ctrl_msg_unrefp) struct udev_ctrl_msg *ctrl_msg = NULL;
const char *str;
int i;
@@ -1021,7 +1016,7 @@ static int synthesize_change(struct udev_device *dev) {
bool has_partitions = false;
int fd;
struct udev *udev = udev_device_get_udev(dev);
- _cleanup_udev_enumerate_unref_ struct udev_enumerate *e = NULL;
+ _cleanup_(udev_enumerate_unrefp) struct udev_enumerate *e = NULL;
struct udev_list_entry *item;
/*
@@ -1059,7 +1054,7 @@ static int synthesize_change(struct udev_device *dev) {
return r;
udev_list_entry_foreach(item, udev_enumerate_get_list_entry(e)) {
- _cleanup_udev_device_unref_ struct udev_device *d = NULL;
+ _cleanup_(udev_device_unrefp) struct udev_device *d = NULL;
d = udev_device_new_from_syspath(udev, udev_list_entry_get_name(item));
if (!d)
@@ -1089,7 +1084,7 @@ static int synthesize_change(struct udev_device *dev) {
write_string_file(filename, "change", WRITE_STRING_FILE_CREATE);
udev_list_entry_foreach(item, udev_enumerate_get_list_entry(e)) {
- _cleanup_udev_device_unref_ struct udev_device *d = NULL;
+ _cleanup_(udev_device_unrefp) struct udev_device *d = NULL;
d = udev_device_new_from_syspath(udev, udev_list_entry_get_name(item));
if (!d)
@@ -1131,7 +1126,7 @@ static int on_inotify(sd_event_source *s, int fd, uint32_t revents, void *userda
}
FOREACH_INOTIFY_EVENT(e, buffer, l) {
- _cleanup_udev_device_unref_ struct udev_device *dev = NULL;
+ _cleanup_(udev_device_unrefp) struct udev_device *dev = NULL;
dev = udev_watch_lookup(manager->udev, e->wd);
if (!dev)
@@ -1258,7 +1253,7 @@ static int on_post(sd_event_source *s, void *userdata) {
}
static int listen_fds(int *rctrl, int *rnetlink) {
- _cleanup_udev_unref_ struct udev *udev = NULL;
+ _cleanup_(udev_unrefp) struct udev *udev = NULL;
int ctrl_fd = -1, netlink_fd = -1;
int fd, n, r;
@@ -1288,7 +1283,7 @@ static int listen_fds(int *rctrl, int *rnetlink) {
}
if (ctrl_fd < 0) {
- _cleanup_udev_ctrl_unref_ struct udev_ctrl *ctrl = NULL;
+ _cleanup_(udev_ctrl_unrefp) struct udev_ctrl *ctrl = NULL;
udev = udev_new();
if (!udev)
@@ -1312,7 +1307,7 @@ static int listen_fds(int *rctrl, int *rnetlink) {
}
if (netlink_fd < 0) {
- _cleanup_udev_monitor_unref_ struct udev_monitor *monitor = NULL;
+ _cleanup_(udev_monitor_unrefp) struct udev_monitor *monitor = NULL;
if (!udev) {
udev = udev_new();
@@ -1606,8 +1601,7 @@ static int manager_new(Manager **ret, int fd_ctrl, int fd_uevent, const char *cg
if (r < 0)
return log_error_errno(r, "error creating post event source: %m");
- *ret = manager;
- manager = NULL;
+ *ret = TAKE_PTR(manager);
return 0;
}
@@ -1676,12 +1670,16 @@ int main(int argc, char *argv[]) {
if (arg_children_max == 0) {
cpu_set_t cpu_set;
+ unsigned long mem_limit;
arg_children_max = 8;
if (sched_getaffinity(0, sizeof(cpu_set), &cpu_set) == 0)
arg_children_max += CPU_COUNT(&cpu_set) * 2;
+ mem_limit = physical_memory() / (128LU*1024*1024);
+ arg_children_max = MAX(10U, MIN(arg_children_max, mem_limit));
+
log_debug("set children_max to %u", arg_children_max);
}
@@ -1739,8 +1737,6 @@ int main(int argc, char *argv[]) {
log_warning_errno(r, "Failed to redirect standard streams to /dev/null: %m");
}
-
-
pid = fork();
switch (pid) {
case 0: