" --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");
}
{ "env", 1, NULL, 'e' },
{ "max-childs", 1, NULL, 'm' },
{ "max_childs", 1, NULL, 'm' + 256},
- { "max-childs-running", 1, NULL, 'M' },
- { "max_childs_running", 1, NULL, 'M' + 256},
{ "help", 0, NULL, 'h' },
{}
};
udev_ctrl_send_set_max_childs(uctrl, i);
rc = 0;
break;
- case 'M':
- case 'M' + 256:
- i = strtoul(optarg, &endp, 0);
- if (endp[0] != '\0' || i < 1) {
- fprintf(stderr, "invalid number '%s'\n", optarg);
- goto exit;
- }
- udev_ctrl_send_set_max_childs_running(uctrl, i);
- rc = 0;
- break;
case 'h':
print_help();
rc = 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_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_send_set_env(uctrl, &arg[strlen("env=")]);
rc = 0;
/* 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 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);
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);
}
}
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);
}
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();