udevd.c revision 4ec9c3e79771aa95586390cecff4218cc8938160
/*
* Copyright (C) 2004-2009 Kay Sievers <kay.sievers@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>
*
* 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 <stddef.h>
#include <signal.h>
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <ctype.h>
#include <fcntl.h>
#include <time.h>
#include <getopt.h>
#include <dirent.h>
#include <sys/signalfd.h>
#include "udev.h"
#include "sd-daemon.h"
#define UDEVD_PRIORITY -4
#define UDEV_PRIORITY -2
static bool debug;
{
if (debug) {
char buf[1024];
} else {
}
}
static struct udev_rules *rules;
static struct udev_queue_export *udev_queue_export;
static struct udev_monitor *monitor;
static pid_t settle_pid;
static bool stop_exec_queue;
static bool reload_config;
static int children;
static int children_max;
static int exec_delay;
static sigset_t orig_sigmask;
static struct udev_list_node event_list;
static struct udev_list_node worker_list;
static bool udev_exit;
static volatile sig_atomic_t worker_exit;
enum poll_fd {
};
};
enum event_state {
};
struct event {
struct udev_list_node node;
struct udev_device *dev;
enum event_state state;
int exitcode;
unsigned long long int delaying_seqnum;
unsigned long long int seqnum;
const char *devpath;
const char *devpath_old;
bool is_block;
int ifindex;
};
{
char *event;
}
enum worker_state {
};
struct worker {
struct udev_list_node node;
int refcount;
struct udev_monitor *monitor;
enum worker_state state;
};
/* passed from worker to main process */
struct worker_message {
int exitcode;
};
{
char *worker;
}
{
/* mark as failed, if "add" event returns non-zero */
else
}
static void event_sig_handler(int signum)
{
switch (signum) {
case SIGALRM:
_exit(1);
break;
case SIGTERM:
worker_exit = true;
break;
}
}
{
return worker;
}
{
return;
children--;
}
{
struct udev_monitor *worker_monitor;
/* listen for new events */
if (worker_monitor == NULL)
return;
/* allow the main daemon netlink address to send devices to the worker */
return;
}
/* worker + event reference */
switch (pid) {
case 0: {
struct udev_device *dev;
};
udev_log_init("udevd-work");
/* set signal handlers */
/* unblock SIGALRM */
/* SIGTERM is unblocked in ppoll() */
/* request TERM signal if parent exits */
/* initial device */
do {
struct udev_event *udev_event;
struct worker_message msg = {};
int err;
int failed = 0;
if (udev_event == NULL)
_exit(3);
/* set timeout to prevent hanging processes */
if (exec_delay > 0)
/* apply rules, create node, symlinks */
if (udev_device_get_event_timeout(dev) >= 0)
if (err == 0)
alarm(0);
}
/* send processed event back to libudev listeners */
/* send udevd the result of the event execution */
if (err != 0)
else if (failed != 0)
/* wait for more device messages or signal from udevd */
while (!worker_exit) {
int fdcount;
if (fdcount < 0)
continue;
break;
}
}
exit(0);
}
case -1:
break;
default:
/* close monitor, but keep address around */
children++;
break;
}
}
{
struct udev_list_node *loop;
continue;
if (count < 0) {
continue;
}
return;
}
if (children_max > 1)
return;
}
/* start new worker and pass initial device */
}
{
return -1;
/* run all events with a timeout set immediately */
if (udev_device_get_timeout(dev) > 0) {
return 0;
}
return 0;
}
{
struct udev_list_node *loop;
int max;
return;
if (max-- <= 0)
break;
continue;
}
}
/* lookup event for identical, parent, child device */
{
struct udev_list_node *loop;
/* check if queue contains events we depend on */
/* we already found a later event, earlier can not block us, no need to check again */
continue;
/* event we checked earlier still exists, no need to check again */
return true;
/* found ourself, no later event can block us */
break;
if (major(event->devnum) != 0 && event->devnum == loop_event->devnum && event->is_block == loop_event->is_block)
return true;
/* check network device ifindex */
return true;
/* check our old name */
return true;
}
/* compare devpath */
/* one devpath is contained in the other? */
continue;
/* identical device event found */
if (major(event->devnum) != 0 && (event->devnum != loop_event->devnum || event->is_block != loop_event->is_block))
continue;
continue;
return true;
}
/* parent device event found */
return true;
}
/* child device event found */
return true;
}
/* no matching device */
continue;
}
return false;
}
{
struct udev_list_node *loop;
continue;
/* do not start event if parent or child event is still running */
if (is_devpath_busy(event)) {
continue;
}
}
}
static void worker_returned(void)
{
for (;;) {
struct worker_message msg;
struct udev_list_node *loop;
if (size != sizeof(struct worker_message))
break;
/* lookup worker who sent the signal */
continue;
/* worker returned */
break;
}
}
}
/* receive the udevd message from userspace */
{
struct udev_ctrl_msg *ctrl_msg;
const char *str;
int i;
return;
if (i >= 0) {
worker_kill(udev, 0);
}
if (udev_ctrl_get_stop_exec_queue(ctrl_msg) > 0) {
stop_exec_queue = true;
}
if (udev_ctrl_get_start_exec_queue(ctrl_msg) > 0) {
stop_exec_queue = false;
}
if (udev_ctrl_get_reload_rules(ctrl_msg) > 0) {
reload_config = true;
}
char *key;
char *val;
val[0] = '\0';
if (val[0] == '\0') {
} else {
}
} else {
}
}
worker_kill(udev, 0);
}
if (i >= 0) {
children_max = i;
}
if (settle_pid > 0) {
settle_pid = 0;
}
}
/* read inotify messages */
{
char *buf;
struct inotify_event *ev;
return 0;
return -1;
}
struct udev_device *dev;
const char *s;
if (s == NULL)
continue;
continue;
reload_config = true;
continue;
}
char filename[UTIL_PATH_SIZE];
int fd;
}
}
}
return 0;
}
{
switch (signo) {
case SIGINT:
case SIGTERM:
udev_exit = true;
break;
case SIGCHLD:
for (;;) {
int status;
if (pid <= 0)
break;
continue;
/* drop reference from running event */
}
}
break;
}
}
break;
case SIGHUP:
reload_config = true;
break;
}
}
{
char modules[UTIL_PATH_SIZE];
char buf[4096];
FILE *f;
if (f == NULL)
return;
char *s;
const char *modname;
const char *devname;
const char *devno;
char type;
char filename[UTIL_PATH_SIZE];
if (buf[0] == '#')
continue;
if (s == NULL)
continue;
s[0] = '\0';
devname = &s[1];
if (s == NULL)
continue;
s[0] = '\0';
devno = &s[1];
if (s == NULL)
if (s != NULL)
s[0] = '\0';
continue;
if (type == 'c')
else if (type == 'b')
else
continue;
}
fclose(f);
}
{
continue;
continue;
} else {
}
char target[UTIL_PATH_SIZE];
continue;
if (maxdepth == 0)
continue;
dir2_to = fdopendir(openat(dirfd(dir_to), dent->d_name, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC));
continue;
dir2_from = fdopendir(openat(dirfd(dir_from), dent->d_name, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC));
continue;
}
}
}
return 0;
}
{
struct stdlinks {
const char *link;
const char *target;
};
};
unsigned int i;
for (i = 0; i < ARRAY_SIZE(stdlinks); i++) {
}
}
}
{
return;
}
{
return;
}
static int mem_size_mb(void)
{
FILE *f;
char buf[4096];
long int memsize = -1;
if (f == NULL)
return -1;
long int value;
break;
}
}
fclose(f);
return memsize;
}
{
char filename[UTIL_PATH_SIZE];
FILE *f;
struct udev_enumerate *udev_enumerate;
struct udev_list_entry *list_entry;
/* current database */
return 0;
/* make sure we do not get here again */
/* old database */
return 0;
if (f != NULL) {
fclose(f);
}
if (udev_enumerate == NULL)
return -1;
struct udev_device *device;
continue;
/* try to find the old database for devices without a current one */
bool have_db;
const char *id;
char devpath[UTIL_PATH_SIZE];
char from[UTIL_PATH_SIZE];
have_db = false;
/* find database in old location */
if (!have_db) {
have_db = true;
}
}
/* find old database with $subsys:$sysname name */
if (!have_db) {
have_db = true;
}
}
/* find old database with the encoded devpath name */
if (!have_db) {
have_db = true;
}
}
/* write out new database */
if (have_db)
}
}
return 0;
}
{
int fd;
FILE *f;
int daemonize = false;
int resolve_names = 1;
{}
};
int rc = 1;
goto exit;
udev_log_init("udevd");
/* make sure, that our runtime dir exists and is writable */
/* try to create our own subdirectory, do not create parent directories */
/* directory seems writable now */
} else {
/* fall back to /dev/.udev */
char filename[UTIL_PATH_SIZE];
goto exit;
}
}
for (;;) {
int option;
if (option == -1)
break;
switch (option) {
case 'd':
daemonize = true;
break;
case 'c':
break;
case 'e':
break;
case 'D':
debug = true;
break;
case 'N':
resolve_names = 1;
resolve_names = 0;
resolve_names = -1;
} else {
goto exit;
}
break;
case 'h':
printf("Usage: udevd OPTIONS\n"
" --daemon\n"
" --debug\n"
" --children-max=<maximum number of workers>\n"
" --exec-delay=<seconds to wait before executing RUN=>\n"
" --resolve-names=early|late|never\n"
" --version\n"
" --help\n"
"\n");
goto exit;
case 'V':
goto exit;
default:
goto exit;
}
}
/*
* read the kernel commandline, in case we need to get into debug mode
* udev.log-priority=<level> syslog priority
* udev.children-max=<number of workers> events are fully serialized if set to 1
*
*/
if (f != NULL) {
char cmdline[4096];
char *pos;
}
}
}
}
fclose(f);
}
if (getuid() != 0) {
goto exit;
}
chdir("/");
umask(022);
/* create standard links, copy static nodes, create nodes from modules */
/* before opening new files, make sure std{in,out,err} fds are in a sane state */
if (fd < 0) {
}
if (write(STDOUT_FILENO, 0, 0) < 0)
if (write(STDERR_FILENO, 0, 0) < 0)
rc = 1;
goto exit;
}
if (udev_ctrl_enable_receiving(udev_ctrl) < 0) {
rc = 1;
goto exit;
}
rc = 3;
goto exit;
}
rc = 4;
goto exit;
}
} else {
char filename[UTIL_PATH_SIZE];
/* watch dynamic rules directory */
}
}
/* block and listen to all signals on signalfd */
sigfillset(&mask);
rc = 5;
goto exit;
}
/* unnamed socket from workers to the main daemon */
rc = 6;
goto exit;
}
goto exit;
}
if (udev_queue_export == NULL) {
goto exit;
}
/* if needed, convert old database from earlier udev version */
if (!debug) {
}
if (fd > STDERR_FILENO)
if (daemonize) {
switch (pid) {
case 0:
break;
case -1:
rc = 4;
goto exit;
default:
rc = 0;
goto exit;
}
} else {
}
/* set scheduling priority for the main daemon process */
setsid();
if (f != NULL) {
fclose(f);
}
if (fd < 0) {
/* Fallback to old interface */
if (fd < 0) {
} else {
/* OOM_DISABLE == -17 */
}
} else {
}
if (children_max <= 0) {
int memsize = mem_size_mb();
/* set value depending on the amount of RAM */
if (memsize > 0)
else
children_max = 128;
}
while (!udev_exit) {
int fdcount;
int timeout;
/* set timeout to kill idle workers */
else
timeout = -1;
/* wait for events */
if (fdcount < 0)
continue;
/* timeout - kill idle workers */
if (fdcount == 0)
/* event has finished */
/* get kernel uevent */
struct udev_device *dev;
if (event_queue_insert(dev) < 0)
}
/* start new events */
/* get signal */
struct signalfd_siginfo fdsi;
if (size == sizeof(struct signalfd_siginfo))
}
/* device node and rules directory inotify watch */
/*
* get control message
*
* This needs to be after the inotify handling, to make sure,
* that the settle signal is send back after the possibly generated
* "change" events by the inotify device node watch.
*/
/* rules changed, set by inotify or a HUP signal */
if (reload_config) {
struct udev_rules *rules_new;
worker_kill(udev, 0);
}
reload_config = 0;
}
}
rc = 0;
exit:
if (worker_watch[READ_END] >= 0)
if (worker_watch[WRITE_END] >= 0)
return rc;
}