udevd.c revision 282988c4f8a85c28468e6442e86efe51dc71cc93
/*
* Copyright (C) 2004-2006 Kay Sievers <kay.sievers@vrfy.org>
* Copyright (C) 2004 Chris Friesen <chris_friesen@sympatico.ca>
*
* under the terms of the GNU General Public License as published by the
* Free Software Foundation version 2 of the License.
*
* 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.
*
*/
#include <stddef.h>
#include <signal.h>
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <dirent.h>
#include <fcntl.h>
#include <syslog.h>
#include <time.h>
#include <getopt.h>
#include "udev.h"
#include "udev_rules.h"
#include "udevd.h"
#include "udev_selinux.h"
static int debug_trace;
static int debug;
static struct udev_rules rules;
static int udevd_sock = -1;
static int uevent_netlink_sock = -1;
static int inotify_fd = -1;
static volatile int sigchilds_waiting;
static volatile int udev_exit;
static volatile int reload_config;
static int run_exec_q;
static int stop_exec_q;
static int max_childs;
static int max_childs_running;
static char udev_log[32];
static LIST_HEAD(running_list);
#ifdef USE_LOG
{
if (priority > udev_log_priority)
return;
if (debug) {
} else
}
#endif
{
exit(1);
}
{
int i;
int retval;
/* set signal handlers */
/* reset to default */
/* trigger timeout to prevent hanging processes */
/* reconstruct event environment from message */
return -1;
if (udev->event_timeout >= 0)
/* run programs collected by RUN-key*/
return retval;
}
enum event_state {
};
{
char filename_failed[PATH_SIZE];
/* location of queue file */
/* location of failed file */
switch (state) {
case EVENT_QUEUED:
break;
case EVENT_FINISHED:
/* "move" event - rename failed file to current name, do not delete failed */
char filename_failed_old[PATH_SIZE];
info("renamed devpath, moved failed state of '%s' to %s'\n",
} else {
}
break;
case EVENT_FAILED:
/* move failed event to the failed directory */
/* clean up possibly empty queue directory */
break;
}
return;
}
{
/* mark as failed, if "add" event returns non-zero */
else
}
{
int retval;
switch (pid) {
case 0:
/* child */
if (inotify_fd >= 0)
logging_init("udevd-event");
if (retval)
exit(1);
exit(0);
case -1:
break;
default:
/* get SIGCHLD in main loop */
info("seq %llu forked, pid [%d], '%s' '%s', %ld seconds old\n",
}
}
{
int fd;
if (fd >= 0) {
char str[32];
int len;
}
/* run one event after the other in debug mode */
if (debug_trace) {
return;
}
/* run all events with a timeout set immediately */
return;
}
run_exec_q = 1;
}
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;
}
static int cpu_count(void)
{
FILE* f;
char buf[4096];
int count = 0;
if (f == NULL)
return -1;
count++;
}
fclose(f);
if (count == 0)
return -1;
return count;
}
static int running_processes(void)
{
FILE* f;
char buf[4096];
int running = -1;
if (f == NULL)
return -1;
int value;
break;
}
}
fclose(f);
return running;
}
/* return the number of process es in our session, count only until limit */
{
int running = 0;
if (!dir)
return -1;
/* read process info from /proc */
int f;
char procdir[64];
char line[256];
const char *pos;
char state;
int len;
continue;
if (f == -1)
continue;
close(f);
if (len <= 0)
continue;
else
/* skip ugly program name */
continue;
continue;
/* count only processes in our session */
continue;
/* count only running, no sleeping processes */
if (state != 'R')
continue;
running++;
break;
}
return running;
}
{
int i;
for (i = 0; i < PATH_SIZE; i++) {
/* identical device event found */
return 1;
/* parent device event found */
return 2;
/* child device event found */
return 3;
/* no matching event */
break;
}
return 0;
}
/* lookup event for identical, parent, child, or physical device */
{
struct udevd_uevent_msg *loop_msg;
int childs_count = 0;
/* check exec-queue which may still contain delayed events we depend on */
/* skip ourself and all later events */
break;
/* check our old name */
return 2;
/* check identical, parent, or child device event */
dbg("%llu, device event still pending %llu (%s)\n",
return 3;
}
/* check for our major:minor number */
return 4;
}
/* check physical device event (special case of parent) */
dbg("%llu, physical device event still pending %llu (%s)\n",
return 5;
}
}
/* check run queue for still running events */
return 1;
}
/* check our old name */
return 2;
/* check identical, parent, or child device event */
dbg("%llu, device event still running %llu (%s)\n",
return 3;
}
/* check for our major:minor number */
return 4;
}
/* check physical device event (special case of parent) */
dbg("%llu, physical device event still running %llu (%s)\n",
return 5;
}
}
return 0;
}
/* serializes events for the identical and parent and child devices */
static void msg_queue_manager(void)
{
struct udevd_uevent_msg *loop_msg;
struct udevd_uevent_msg *tmp_msg;
int running;
if (list_empty(&exec_list))
return;
running = running_processes();
if (running < 0)
/* check running processes in our session and possibly throttle */
if (running >= max_childs_running) {
if (running >= max_childs_running) {
return;
}
}
/* serialize and wait for parent or child events */
continue;
}
/* move event to run list */
running++;
}
}
{
int bufpos;
int i;
struct udevd_uevent_msg *msg;
char *physdevdriver_key = NULL;
int maj = 0;
int min = 0;
return NULL;
/* copy environment buffer and reconstruct envp */
bufpos = 0;
int keylen;
char *key;
/* remember some keys for further processing */
}
/* for older kernels DRIVER is empty for a bus device, export PHYSDEVDRIVER as DRIVER */
}
info("DEVPATH or ACTION missing, ignore message\n");
return NULL;
}
return msg;
}
/* receive the udevd message from userspace */
static void get_ctrl_msg(void)
{
struct udevd_ctrl_msg ctrl_msg;
int *intval;
char *pos;
if (size < 0) {
return;
}
err("no sender credentials received, message ignored\n");
return;
}
return;
}
return;
}
case UDEVD_CTRL_ENV:
break;
}
pos[0] = '\0';
} else {
}
break;
info("udevd message (STOP_EXEC_QUEUE) received\n");
stop_exec_q = 1;
break;
info("udevd message (START_EXEC_QUEUE) received\n");
stop_exec_q = 0;
break;
case UDEVD_CTRL_SET_LOG_LEVEL:
break;
max_childs = *intval;
break;
break;
case UDEVD_CTRL_RELOAD_RULES:
info("udevd message (RELOAD_RULES) received\n");
reload_config = 1;
break;
default:
err("unknown control message type\n");
}
}
/* receive the kernel user event message and do some sanity checks */
static struct udevd_uevent_msg *get_netlink_msg(void)
{
struct udevd_uevent_msg *msg;
int bufpos;
char *pos;
if (size < 0) {
return NULL;
}
/* start of event payload */
return NULL;
/* validate message */
return NULL;
}
pos[0] = '\0';
return NULL;
}
return NULL;
}
return msg;
}
{
switch (signum) {
case SIGINT:
case SIGTERM:
udev_exit = 1;
break;
case SIGCHLD:
/* set flag, then write to pipe if needed */
sigchilds_waiting = 1;
break;
case SIGHUP:
reload_config = 1;
break;
}
/* write to pipe, which will wakeup select() in our mainloop */
}
{
/* find msg associated with pid and delete it */
struct udevd_uevent_msg *msg;
/* there may be events waiting with the same devpath */
run_exec_q = 1;
return;
}
}
}
static void reap_sigchilds(void)
{
int status;
while (1) {
if (pid <= 0)
break;
else if (WIFSIGNALED(status))
else
status = 0;
}
}
static int init_udevd_socket(void)
{
struct sockaddr_un saddr;
const int feature_on = 1;
int retval;
/* use abstract namespace for socket path */
if (udevd_sock == -1) {
return -1;
}
/* the bind takes care of ensuring only one copy running */
if (retval < 0) {
udevd_sock = -1;
return -1;
}
/* enable receiving of the sender credentials */
return 0;
}
static int init_uevent_netlink_sock(void)
{
struct sockaddr_nl snl;
int retval;
if (uevent_netlink_sock == -1) {
return -1;
}
/* set receive buffersize */
if (retval < 0) {
uevent_netlink_sock = -1;
return -1;
}
return 0;
}
static void export_initial_seqnum(void)
{
int fd;
char seqnum[32];
if (fd >= 0) {
}
if (len <= 0) {
len = 3;
}
if (fd >= 0) {
}
}
{
int retval;
int fd;
const char *value;
int daemonize = 0;
int option;
{}
};
int rc = 1;
int maxfd;
logging_init("udevd");
selinux_init();
while (1) {
if (option == -1)
break;
switch (option) {
case 'd':
daemonize = 1;
break;
case 't':
debug_trace = 1;
break;
case 'D':
debug = 1;
if (udev_log_priority < LOG_INFO)
break;
case 'h':
printf("Usage: udevd [--help] [--daemon] [--debug-trace] [--debug] [--version]\n");
goto exit;
case 'V':
goto exit;
default:
goto exit;
}
}
if (getuid() != 0) {
err("root privileges required\n");
goto exit;
}
/* make sure std{in,out,err} fd's are in a sane state */
if (fd < 0) {
}
if (fd > STDIN_FILENO)
if (write(STDOUT_FILENO, 0, 0) < 0)
if (write(STDERR_FILENO, 0, 0) < 0)
/* init sockets to receive events */
if (init_udevd_socket() < 0) {
if (errno == EADDRINUSE) {
err("another udev daemon already running\n");
rc = 1;
} else {
err("error initializing udevd socket\n");
rc = 2;
}
goto exit;
}
if (init_uevent_netlink_sock() < 0) {
err("error initializing netlink socket\n");
rc = 3;
goto exit;
}
/* setup signal handler pipe */
if (retval < 0) {
goto exit;
}
if (retval < 0) {
goto exit;
}
if (retval < 0) {
goto exit;
}
if (retval < 0) {
goto exit;
}
if (retval < 0) {
goto exit;
}
/* parse the rules and keep them in memory */
sysfs_init();
if (daemonize) {
switch (pid) {
case 0:
dbg("daemonized fork running\n");
break;
case -1:
rc = 4;
goto exit;
default:
rc = 0;
goto exit;
}
}
/* redirect std{out,err} fd's */
if (!debug)
if (fd > STDERR_FILENO)
/* set scheduling priority for the daemon */
chdir("/");
umask(022);
/* become session leader */
/* OOM_DISABLE == -17 */
if (fd < 0)
else {
}
if (fd > 0) {
}
/* set signal handlers */
/* watch rules directory */
inotify_fd = inotify_init();
if (inotify_fd >= 0) {
if (udev_rules_dir[0] != '\0') {
} else {
/* watch dynamic rules directory */
}
err("the kernel does not support inotify, udevd can't monitor rules file changes\n");
else
/* maximum limit of forked childs */
if (value)
else {
int memsize = mem_size_mb();
if (memsize > 0)
else
}
/* start to throttle forking if maximum number of _running_ childs is reached */
if (value)
else {
if (cpus > 0)
else
}
/* clear environment for forked event processes */
clearenv();
/* export log_priority , as called programs may want to follow that setting */
if (debug_trace)
putenv("DEBUG=1");
maxfd = udevd_sock;
while (!udev_exit) {
struct udevd_uevent_msg *msg;
int fdcount;
if (inotify_fd >= 0)
if (fdcount < 0) {
continue;
}
/* get control message */
get_ctrl_msg();
/* get netlink message */
msg = get_netlink_msg();
if (msg)
}
/* received a signal, clear our notification pipe */
char buf[256];
}
/* rules directory inotify watch */
int nbytes;
/* discard all possible events, we can just reload the config */
char *buf;
reload_config = 1;
err("error getting buffer for inotify, disable watching\n");
inotify_fd = -1;
}
}
}
/* rules changed, set by inotify or a HUP signal */
if (reload_config) {
reload_config = 0;
}
/* forked child has returned */
if (sigchilds_waiting) {
sigchilds_waiting = 0;
}
if (run_exec_q) {
run_exec_q = 0;
if (!stop_exec_q)
}
}
rc = 0;
exit:
selinux_exit();
if (signal_pipe[READ_END] >= 0)
if (signal_pipe[WRITE_END] >= 0)
if (udevd_sock >= 0)
if (inotify_fd >= 0)
if (uevent_netlink_sock >= 0)
return rc;
}