+static void msg_queue_insert(struct udevd_uevent_msg *msg)
+{
+ char filename[PATH_SIZE];
+ int fd;
+
+ msg->queue_time = time(NULL);
+
+ strlcpy(filename, udev_root, sizeof(filename));
+ strlcat(filename, "/" EVENT_SEQNUM, sizeof(filename));
+ fd = open(filename, O_WRONLY|O_TRUNC|O_CREAT, 0644);
+ if (fd >= 0) {
+ char str[32];
+ int len;
+
+ len = sprintf(str, "%llu\n", msg->seqnum);
+ write(fd, str, len);
+ close(fd);
+ }
+
+ export_event_state(msg, EVENT_QUEUED);
+
+ /* run one event after the other in debug mode */
+ if (debug_trace) {
+ list_add_tail(&msg->node, &running_list);
+ udev_event_run(msg);
+ waitpid(msg->pid, NULL, 0);
+ msg_queue_delete(msg);
+ return;
+ }
+
+ /* run all events with a timeout set immediately */
+ if (msg->timeout != 0) {
+ list_add_tail(&msg->node, &running_list);
+ udev_event_run(msg);
+ return;
+ }
+
+ list_add_tail(&msg->node, &exec_list);
+ run_exec_q = 1;
+}
+
+static int mem_size_mb(void)
+{
+ int f;
+ char buf[8192];
+ long int len;
+ const char *pos;
+ long int memsize;
+
+ f = open("/proc/meminfo", O_RDONLY);
+ if (f == -1)
+ return -1;
+
+ len = read(f, buf, sizeof(buf)-1);
+ close(f);
+
+ if (len <= 0)
+ return -1;
+ buf[len] = '\0';
+
+ pos = strstr(buf, "MemTotal: ");
+ if (pos == NULL)
+ return -1;
+
+ if (sscanf(pos, "MemTotal: %ld kB", &memsize) != 1)
+ return -1;
+
+ return memsize / 1024;
+}
+
+static int cpu_count(void)
+{
+ int f;
+ char buf[32768];
+ int len;
+ const char *pos;
+ int count = 0;
+
+ f = open("/proc/stat", O_RDONLY);
+ if (f == -1)
+ return -1;
+
+ len = read(f, buf, sizeof(buf)-1);
+ close(f);
+ if (len <= 0)
+ return -1;
+ buf[len] = '\0';
+
+ pos = strstr(buf, "cpu");
+ if (pos == NULL)
+ return -1;
+
+ while (pos != NULL) {
+ if (strncmp(pos, "cpu", 3) == 0 &&isdigit(pos[3]))
+ count++;
+ pos = strstr(&pos[3], "cpu");
+ }
+
+ if (count == 0)
+ return -1;
+ return count;
+}
+