cleanup some debug output and move to info level + unify select() loops

Signed-off-by: Kay Sievers <kay.sievers@suse.de>
This commit is contained in:
Kay Sievers
2005-08-15 11:57:04 +02:00
parent 8aafc06d94
commit 40caaeec51
13 changed files with 132 additions and 113 deletions

View File

@@ -38,13 +38,13 @@
#undef err
#define err(format, arg...) \
do { \
log_message(LOG_ERR ,"%s: " format ,__FILE__ ,## arg); \
log_message(LOG_ERR ,"%s: " format ,__FUNCTION__ ,## arg); \
} while (0)
#undef info
#define info(format, arg...) \
do { \
log_message(LOG_INFO ,"%s: " format ,__FILE__ ,## arg); \
log_message(LOG_INFO ,"%s: " format ,__FUNCTION__ ,## arg); \
} while (0)
#ifdef DEBUG

3
udev.c
View File

@@ -125,11 +125,10 @@ int main(int argc, char *argv[], char *envp[])
dbg("executing run list");
list_for_each_entry(name_loop, &udev.run_list, node) {
info("RUN key executes '%s'", name_loop->name);
if (strncmp(name_loop->name, "socket:", strlen("socket:")) == 0)
pass_env_to_socket(&name_loop->name[strlen("socket:")], devpath, action);
else
run_program(name_loop->name, udev.subsystem, NULL, 0, NULL, (udev_log_priority >= LOG_DEBUG));
run_program(name_loop->name, udev.subsystem, NULL, 0, NULL, (udev_log_priority >= LOG_INFO));
}
}

View File

@@ -215,7 +215,7 @@ static int create_node(struct udevice *udev, struct sysfs_class_device *class_de
strlcat(linktarget, &udev->name[tail], sizeof(linktarget));
dbg("symlink(%s, %s)", linktarget, filename);
info("creating symlink '%s' to '%s'", filename, linktarget);
if (!udev->test_run) {
unlink(filename);
selinux_setfscreatecon(filename, NULL, S_IFLNK);

View File

@@ -113,7 +113,7 @@ static int parse_db_file(struct udevice *udev, const char *filename)
size_t count;
if (file_map(filename, &buf, &bufsize) != 0) {
err("unable to read db file '%s'", filename);
dbg("no db file to read '%s'", filename);
return -1;
}

View File

@@ -22,6 +22,11 @@
#ifndef _UDEV_LIBC_WRAPPER_H_
#define _UDEV_LIBC_WRAPPER_H_
#include <string.h>
#include <unistd.h>
#include <linux/types.h>
/* needed for our signal handlers to work */
#undef asmlinkage
#ifdef __i386__
#define asmlinkage __attribute__((regparm(0)))
@@ -29,6 +34,7 @@
#define asmlinkage
#endif
/* headers are broken on some lazy platforms */
#ifndef __FD_SET
#define __FD_SET(d, set) ((set)->fds_bits[__FDELT(d)] |= __FDMASK(d))
#endif
@@ -42,12 +48,11 @@
#define __FD_ZERO(set) ((void) memset ((void*) (set), 0, sizeof (fd_set)))
#endif
/* missing in some lazy distros */
#ifndef NETLINK_KOBJECT_UEVENT
#define NETLINK_KOBJECT_UEVENT 15
#endif
#include <string.h>
#ifdef __KLIBC__
static inline int clearenv(void)
{

View File

@@ -88,7 +88,7 @@ static int delete_node(struct udevice *udev)
continue;;
}
dbg("removing symlink '%s'", filename);
info("removing symlink '%s'", filename);
unlink(filename);
if (strchr(filename, '/'))

View File

@@ -222,7 +222,7 @@ static int import_program_into_env(struct udevice *udev, const char *program)
char result[1024];
size_t reslen;
if (run_program(program, udev->subsystem, result, sizeof(result), &reslen, (udev_log_priority >= LOG_DEBUG)) != 0)
if (run_program(program, udev->subsystem, result, sizeof(result), &reslen, (udev_log_priority >= LOG_INFO)) != 0)
return -1;
return import_keys_into_env(udev, result, reslen);
}
@@ -808,7 +808,6 @@ try_parent:
apply_format(udev, import, sizeof(import), class_dev, sysfs_device);
dbg("check for IMPORT import='%s'", import);
if (rule->import_type == IMPORT_PROGRAM) {
info("IMPORT executes '%s'", import);
rc = import_program_into_env(udev, import);
} else if (rule->import_type == IMPORT_FILE) {
dbg("import file import='%s'", import);
@@ -833,8 +832,7 @@ try_parent:
strlcpy(program, key_val(rule, &rule->program), sizeof(program));
apply_format(udev, program, sizeof(program), class_dev, sysfs_device);
info("PROGRAM key executes '%s", program);
if (run_program(program, udev->subsystem, result, sizeof(result), NULL, (udev_log_priority >= LOG_DEBUG)) != 0) {
if (run_program(program, udev->subsystem, result, sizeof(result), NULL, (udev_log_priority >= LOG_INFO)) != 0) {
dbg("PROGRAM is false");
udev->program_result[0] = '\0';
if (rule->program.operation != KEY_OP_NOMATCH)
@@ -964,7 +962,7 @@ int udev_rules_get_name(struct udev_rules *rules, struct udevice *udev, struct s
}
strlcpy(temp, key_val(rule, &rule->symlink), sizeof(temp));
apply_format(udev, temp, sizeof(temp), class_dev, sysfs_device);
info("rule applied, added symlink '%s'", temp);
dbg("rule applied, added symlink '%s'", temp);
/* add multiple symlinks separated by spaces */
pos = temp;

View File

@@ -642,7 +642,13 @@ int udev_rules_init(struct udev_rules *rules, int read_compiled, int resolve_nam
retval = add_matching_files(&name_list, udev_rules_filename, RULEFILE_SUFFIX);
list_for_each_entry_safe(name_loop, name_tmp, &name_list, node) {
parse_file(rules, name_loop->name);
if (stat(name_loop->name, &stats) == 0) {
if (stats.st_size)
parse_file(rules, name_loop->name);
else
dbg("empty rules file '%s'", name_loop->name);
} else
dbg("could not read '%s'", name_loop->name);
list_del(&name_loop->node);
free(name_loop);
}

View File

@@ -46,7 +46,8 @@ int pass_env_to_socket(const char *sockname, const char *devpath, const char *ac
char buf[2048];
size_t bufpos = 0;
int i;
int retval;
ssize_t count;
int retval = 0;
dbg("pass environment to socket '%s'", sockname);
sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
@@ -63,23 +64,23 @@ int pass_env_to_socket(const char *sockname, const char *devpath, const char *ac
bufpos++;
}
retval = sendto(sock, &buf, bufpos, 0, (struct sockaddr *)&saddr, addrlen);
if (retval != -1)
retval = 0;
count = sendto(sock, &buf, bufpos, 0, (struct sockaddr *)&saddr, addrlen);
if (count < 0)
retval = -1;
info("passed %zi bytes to socket '%s', ", count, sockname);
close(sock);
return retval;
}
int run_program(const char *command, const char *subsystem,
char *result, size_t ressize, size_t *reslen, int dbg)
char *result, size_t ressize, size_t *reslen, int log)
{
int retval = 0;
int status;
int outpipe[2] = {-1, -1};
int errpipe[2] = {-1, -1};
pid_t pid;
char *pos;
char arg[PATH_SIZE];
char *argv[(sizeof(arg) / 2) + 1];
int devnull;
@@ -88,7 +89,7 @@ int run_program(const char *command, const char *subsystem,
strlcpy(arg, command, sizeof(arg));
i = 0;
if (strchr(arg, ' ')) {
pos = arg;
char *pos = arg;
while (pos != NULL) {
if (pos[0] == '\'') {
/* don't separate if in apostrophes */
@@ -103,22 +104,22 @@ int run_program(const char *command, const char *subsystem,
i++;
}
argv[i] = NULL;
dbg("execute '%s' with parsed arguments", arg);
info("'%s'", command);
} else {
argv[0] = arg;
argv[1] = (char *) subsystem;
argv[2] = NULL;
dbg("execute '%s' with subsystem '%s' argument", arg, argv[1]);
info("'%s' '%s'", arg, argv[1]);
}
/* prepare pipes from child to parent */
if (result || dbg) {
if (result || log) {
if (pipe(outpipe) != 0) {
err("pipe failed");
return -1;
}
}
if (dbg) {
if (log) {
if (pipe(errpipe) != 0) {
err("pipe failed");
return -1;
@@ -149,13 +150,13 @@ int run_program(const char *command, const char *subsystem,
dup2(outpipe[1], STDOUT_FILENO);
if (errpipe[1] > 0)
dup2(errpipe[1], STDERR_FILENO);
execv(arg, argv);
execv(argv[0], argv);
/* we should never reach this */
err("exec of program failed");
_exit(1);
case -1:
err("fork of '%s' failed", arg);
err("fork of '%s' failed", argv[0]);
return -1;
default:
/* read from child if requested */
@@ -190,6 +191,8 @@ int run_program(const char *command, const char *subsystem,
/* get stdout */
if (outpipe[0] > 0 && FD_ISSET(outpipe[0], &readfds)) {
char inbuf[1024];
char *pos;
char *line;
count = read(outpipe[0], inbuf, sizeof(inbuf)-1);
if (count <= 0) {
@@ -202,22 +205,28 @@ int run_program(const char *command, const char *subsystem,
continue;
}
inbuf[count] = '\0';
dbg("stdout: '%s'", inbuf);
/* store result for rule processing */
if (result) {
if (respos + count >= ressize) {
if (respos + count < ressize) {
memcpy(&result[respos], inbuf, count);
respos += count;
} else {
err("ressize %ld too short", (long)ressize);
retval = -1;
continue;
}
memcpy(&result[respos], inbuf, count);
respos += count;
}
pos = inbuf;
while ((line = strsep(&pos, "\n")))
if (pos || line[0] != '\0')
info("'%s' (stdout) '%s'", argv[0], line);
}
/* get stderr */
if (errpipe[0] > 0 && FD_ISSET(errpipe[0], &readfds)) {
char errbuf[1024];
char *pos;
char *line;
count = read(errpipe[0], errbuf, sizeof(errbuf)-1);
if (count <= 0) {
@@ -228,7 +237,10 @@ int run_program(const char *command, const char *subsystem,
continue;
}
errbuf[count] = '\0';
dbg("stderr: '%s'", errbuf);
pos = errbuf;
while ((line = strsep(&pos, "\n")))
if (pos || line[0] != '\0')
info("'%s' (stderr) '%s'", argv[0], line);
}
}
if (outpipe[0] > 0)
@@ -245,8 +257,12 @@ int run_program(const char *command, const char *subsystem,
}
}
waitpid(pid, &status, 0);
if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0)) {
dbg("exec program status 0x%x", status);
if (WIFEXITED(status)) {
info("'%s' returned with status %i", argv[0], WEXITSTATUS(status));
if (WEXITSTATUS(status) != 0)
retval = -1;
} else {
err("'%s' abnormal exit", argv[0]);
retval = -1;
}
}

113
udevd.c
View File

@@ -30,6 +30,7 @@
#include <ctype.h>
#include <dirent.h>
#include <fcntl.h>
#include <syslog.h>
#include <sys/select.h>
#include <sys/wait.h>
#include <sys/time.h>
@@ -62,22 +63,17 @@ static volatile int udev_exit;
static int init_phase = 1;
static int run_exec_q;
static int stop_exec_q;
static char *udev_bin;
static int event_timeout;
static int max_childs;
static int max_childs_running;
static unsigned long long expected_seqnum;
static char log[32];
static LIST_HEAD(msg_list);
static LIST_HEAD(exec_list);
static LIST_HEAD(running_list);
static void exec_queue_manager(void);
static void msg_queue_manager(void);
static void user_sighandler(void);
static void reap_sigchilds(void);
static char *udev_bin;
static unsigned long long expected_seqnum;
static int event_timeout;
static int max_childs;
static int max_childs_running;
#ifdef USE_LOG
void log_message (int priority, const char *format, ...)
@@ -138,7 +134,8 @@ static void msg_queue_insert(struct uevent_msg *msg)
/* don't delay messages with timeout set */
if (msg->timeout) {
dbg("move seq %llu with timeout %u to exec queue", msg->seqnum, msg->timeout);
info("seq %llu with timeout %u seconds will be execute without queuing, '%s' '%s'",
msg->seqnum, msg->timeout, msg->action, msg->devpath);
list_add(&msg->node, &exec_list);
run_exec_q = 1;
return;
@@ -156,7 +153,7 @@ static void msg_queue_insert(struct uevent_msg *msg)
}
}
list_add(&msg->node, &loop_msg->node);
info("seq %llu queued, devpath '%s'", msg->seqnum, msg->devpath);
info("seq %llu queued, '%s' '%s'", msg->seqnum, msg->action, msg->devpath);
/* run msg queue manager */
run_msg_q = 1;
@@ -165,7 +162,7 @@ static void msg_queue_insert(struct uevent_msg *msg)
}
/* forks event and removes event from run queue when finished */
static void udev_event_fork(struct uevent_msg *msg)
static void udev_event_run(struct uevent_msg *msg)
{
char *const argv[] = { "udev", msg->subsystem, NULL };
pid_t pid;
@@ -190,8 +187,8 @@ static void udev_event_fork(struct uevent_msg *msg)
default:
/* get SIGCHLD in main loop */
sysinfo(&info);
info("seq %llu forked, pid %d, %ld seconds old",
msg->seqnum, pid, info.uptime - msg->queue_time);
info("seq %llu forked, pid [%d], '%s' '%s', %ld seconds old",
msg->seqnum, pid, msg->action, msg->subsystem, info.uptime - msg->queue_time);
msg->pid = pid;
}
}
@@ -385,7 +382,7 @@ static void exec_queue_manager(void)
if (running_with_devpath(loop_msg, max_childs) == 0) {
/* move event to run list */
list_move_tail(&loop_msg->node, &running_list);
udev_event_fork(loop_msg);
udev_event_run(loop_msg);
running++;
dbg("moved seq %llu to running list", loop_msg->seqnum);
} else
@@ -441,7 +438,7 @@ recheck:
msg_dump_queue();
/* set timeout for remaining queued events */
if (list_empty(&msg_list) == 0) {
if (!list_empty(&msg_list)) {
struct itimerval itv = {{0, 0}, {timeout - msg_age, 0}};
dbg("next event expires in %li seconds", timeout - msg_age);
setitimer(ITIMER_REAL, &itv, NULL);
@@ -550,7 +547,7 @@ static struct uevent_msg *get_udevd_msg(void)
switch (usend_msg.type) {
case UDEVD_UEVENT_UDEVSEND:
case UDEVD_UEVENT_INITSEND:
info("udevd event message received");
dbg("udevd event message received");
envbuf_size = size - offsetof(struct udevd_msg, envbuf);
dbg("envbuf_size=%i", envbuf_size);
msg = get_msg_from_envbuf(usend_msg.envbuf, envbuf_size);
@@ -571,6 +568,8 @@ static struct uevent_msg *get_udevd_msg(void)
intval = (int *) usend_msg.envbuf;
info("udevd message (SET_LOG_PRIORITY) received, udev_log_priority=%i", *intval);
udev_log_priority = *intval;
sprintf(log, "UDEV_LOG=%i", udev_log_priority);
putenv(log);
break;
case UDEVD_SET_MAX_CHILDS:
intval = (int *) usend_msg.envbuf;
@@ -658,7 +657,7 @@ static void asmlinkage sig_handler(int signum)
* which will wakeup our mainloop
*/
if (!sig_flag) {
rc = write(pipefds[1],&signum,sizeof(signum));
rc = write(pipefds[1], &signum, sizeof(signum));
if (rc >= 0)
sig_flag = 1;
}
@@ -688,30 +687,16 @@ static void udev_done(int pid)
static void reap_sigchilds(void)
{
while(1) {
int pid = waitpid(-1, NULL, WNOHANG);
if ((pid == -1) || (pid == 0))
pid_t pid;
while (1) {
pid = waitpid(-1, NULL, WNOHANG);
if (pid <= 0)
break;
udev_done(pid);
}
}
/* just read everything from the pipe and clear the flag,
* the flags was set in the signal handler
*/
static void user_sighandler(void)
{
int sig;
while(1) {
int rc = read(pipefds[0], &sig, sizeof(sig));
if (rc < 0)
break;
sig_flag = 0;
}
}
static int init_udevd_socket(void)
{
struct sockaddr_un saddr;
@@ -783,7 +768,6 @@ static int init_uevent_netlink_sock(void)
int main(int argc, char *argv[], char *envp[])
{
int maxsockplus;
int retval;
int devnull;
struct sigaction act;
@@ -813,6 +797,7 @@ int main(int argc, char *argv[], char *envp[])
stop_exec_q = 1;
}
}
if (daemonize) {
pid_t pid;
@@ -856,7 +841,6 @@ int main(int argc, char *argv[], char *envp[])
err("error getting pipes: %s", strerror(errno));
goto exit;
}
retval = fcntl(pipefds[0], F_SETFL, O_NONBLOCK);
if (retval < 0) {
err("error fcntl on read pipe: %s", strerror(errno));
@@ -865,7 +849,6 @@ int main(int argc, char *argv[], char *envp[])
retval = fcntl(pipefds[0], F_SETFD, FD_CLOEXEC);
if (retval < 0)
err("error fcntl on read pipe: %s", strerror(errno));
retval = fcntl(pipefds[1], F_SETFL, O_NONBLOCK);
if (retval < 0) {
err("error fcntl on write pipe: %s", strerror(errno));
@@ -886,10 +869,6 @@ int main(int argc, char *argv[], char *envp[])
sigaction(SIGCHLD, &act, NULL);
sigaction(SIGHUP, &act, NULL);
if (init_uevent_netlink_sock() < 0) {
dbg("uevent socket not available");
}
if (init_udevd_socket() < 0) {
if (errno == EADDRINUSE)
dbg("another udevd running, exit");
@@ -899,6 +878,9 @@ int main(int argc, char *argv[], char *envp[])
goto exit;
}
if (init_uevent_netlink_sock() < 0)
info("uevent socket not available");
/* override of forked udev binary, used for testing */
udev_bin = getenv("UDEV_BIN");
if (udev_bin != NULL)
@@ -937,26 +919,29 @@ int main(int argc, char *argv[], char *envp[])
max_childs_running = UDEVD_MAX_CHILDS_RUNNING;
info("initialize max_childs_running to %u", max_childs_running);
FD_ZERO(&readfds);
FD_SET(udevd_sock, &readfds);
if (uevent_netlink_sock > 0)
FD_SET(uevent_netlink_sock, &readfds);
FD_SET(pipefds[0], &readfds);
maxsockplus = udevd_sock+1;
/* export log_priority , as called programs may want to follow that setting */
sprintf(log, "UDEV_LOG=%i", udev_log_priority);
putenv(log);
while (!udev_exit) {
struct uevent_msg *msg;
int fdcount;
fd_set workreadfds = readfds;
retval = select(maxsockplus, &workreadfds, NULL, NULL, NULL);
FD_ZERO(&readfds);
FD_SET(pipefds[0], &readfds);
FD_SET(udevd_sock, &readfds);
if (uevent_netlink_sock > 0)
FD_SET(uevent_netlink_sock, &readfds);
if (retval < 0) {
fdcount = select(UDEV_MAX(udevd_sock, uevent_netlink_sock)+1, &readfds, NULL, NULL, NULL);
if (fdcount < 0) {
if (errno != EINTR)
dbg("error in select: %s", strerror(errno));
continue;
}
/* get user socket message */
if (FD_ISSET(udevd_sock, &workreadfds)) {
if (FD_ISSET(udevd_sock, &readfds)) {
msg = get_udevd_msg();
if (msg) {
/* discard kernel messages if netlink is active */
@@ -970,7 +955,7 @@ int main(int argc, char *argv[], char *envp[])
}
/* get kernel netlink message */
if (FD_ISSET(uevent_netlink_sock, &workreadfds)) {
if ((uevent_netlink_sock > 0) && FD_ISSET(uevent_netlink_sock, &readfds)) {
msg = get_netlink_msg();
if (msg) {
msg_queue_insert(msg);
@@ -983,8 +968,17 @@ int main(int argc, char *argv[], char *envp[])
}
/* received a signal, clear our notification pipe */
if (FD_ISSET(pipefds[0], &workreadfds))
user_sighandler();
if (FD_ISSET(pipefds[0], &readfds)) {
int sig;
ssize_t rlen;
while(1) {
rlen = read(pipefds[0], &sig, sizeof(sig));
if (rlen <= 0)
break;
}
sig_flag = 0;
}
/* forked child have returned */
if (sigchilds_waiting) {
@@ -1018,7 +1012,6 @@ exit:
if (udevd_sock > 0)
close(udevd_sock);
if (uevent_netlink_sock > 0)
close(uevent_netlink_sock);

View File

@@ -63,7 +63,7 @@ int main(int argc, char **argv, char **envp)
const char *env;
if (stat("/events", &sb) || !(S_ISDIR(sb.st_mode)))
return 1;
return 0;
env = getenv("UDEV_LOG");
if (env)

View File

@@ -105,7 +105,7 @@ int main(int argc, char *argv[])
int env = 0;
fd_set readfds;
int i;
int retval;
int retval = 0;
for (i = 1 ; i < argc; i++) {
char *arg = argv[i];
@@ -128,34 +128,34 @@ int main(int argc, char *argv[])
exit(1);
}
retval = init_udev_monitor_socket();
if (retval)
goto out;
init_uevent_netlink_sock();
init_udev_monitor_socket();
printf("udevmonitor prints received from the kernel [UEVENT] and after\n"
"the udev processing, the event which udev [UDEV] has generated\n\n");
FD_ZERO(&readfds);
if (uevent_netlink_sock > 0)
FD_SET(uevent_netlink_sock, &readfds);
if (udev_monitor_sock > 0)
FD_SET(udev_monitor_sock, &readfds);
while (1) {
static char buf[UEVENT_BUFFER_SIZE*2];
ssize_t buflen;
fd_set workreadfds;
int fdcount;
buflen = 0;
workreadfds = readfds;
FD_ZERO(&readfds);
if (uevent_netlink_sock > 0)
FD_SET(uevent_netlink_sock, &readfds);
if (udev_monitor_sock > 0)
FD_SET(udev_monitor_sock, &readfds);
retval = select(UDEV_MAX(uevent_netlink_sock, udev_monitor_sock)+1, &workreadfds, NULL, NULL, NULL);
if (retval < 0) {
fdcount = select(UDEV_MAX(uevent_netlink_sock, udev_monitor_sock)+1, &readfds, NULL, NULL, NULL);
if (fdcount < 0) {
if (errno != EINTR)
fprintf(stderr, "error receiving uevent message\n");
continue;
}
if ((uevent_netlink_sock > 0) && FD_ISSET(uevent_netlink_sock, &workreadfds)) {
if ((uevent_netlink_sock > 0) && FD_ISSET(uevent_netlink_sock, &readfds)) {
buflen = recv(uevent_netlink_sock, &buf, sizeof(buf), 0);
if (buflen <= 0) {
fprintf(stderr, "error receiving uevent message\n");
@@ -164,7 +164,7 @@ int main(int argc, char *argv[])
printf("UEVENT[%i] %s\n", time(NULL), buf);
}
if ((udev_monitor_sock > 0) && FD_ISSET(udev_monitor_sock, &workreadfds)) {
if ((udev_monitor_sock > 0) && FD_ISSET(udev_monitor_sock, &readfds)) {
buflen = recv(udev_monitor_sock, &buf, sizeof(buf), 0);
if (buflen <= 0) {
fprintf(stderr, "error receiving udev message\n");
@@ -198,9 +198,11 @@ int main(int argc, char *argv[])
}
}
out:
if (uevent_netlink_sock > 0)
close(uevent_netlink_sock);
if (udev_monitor_sock > 0)
close(udev_monitor_sock);
exit(1);
return retval;
}

View File

@@ -167,7 +167,7 @@ run:
if (strncmp(name_loop->name, "socket:", strlen("socket:")) == 0)
pass_env_to_socket(&name_loop->name[strlen("socket:")], devpath, "add");
else
run_program(name_loop->name, udev.subsystem, NULL, 0, NULL, (udev_log_priority >= LOG_DEBUG));
run_program(name_loop->name, udev.subsystem, NULL, 0, NULL, (udev_log_priority >= LOG_INFO));
}
}
exit: