const char *env;
const char *val;
int *intval;
+ int i;
int retval = 1;
env = getenv("UDEV_LOG");
logging_init("udevcontrol");
dbg("version %s", UDEV_VERSION);
- if (argc != 2) {
- err("error finding comand");
+ if (argc < 2) {
+ fprintf(stderr, "missing command\n\n");
goto exit;
}
memset(&usend_msg, 0x00, sizeof(struct udevd_msg));
strcpy(usend_msg.magic, UDEV_MAGIC);
- if (!strcmp(argv[1], "stop_exec_queue"))
- usend_msg.type = UDEVD_STOP_EXEC_QUEUE;
- else if (!strcmp(argv[1], "start_exec_queue"))
- usend_msg.type = UDEVD_START_EXEC_QUEUE;
- else if (!strncmp(argv[1], "log_priority=", strlen("log_priority="))) {
- intval = (int *) usend_msg.envbuf;
- val = &argv[1][strlen("log_priority=")];
- usend_msg.type = UDEVD_SET_LOG_LEVEL;
- *intval = log_priority(val);
- info("send log_priority=%i", *intval);
- } else if (!strncmp(argv[1], "max_childs=", strlen("max_childs="))) {
- intval = (int *) usend_msg.envbuf;
- val = &argv[1][strlen("max_childs=")];
- usend_msg.type = UDEVD_SET_MAX_CHILDS;
- *intval = atoi(val);
- info("send max_childs=%i", *intval);
- } else {
- err("error parsing command\n");
- goto exit;
+ for (i = 1 ; i < argc; i++) {
+ char *arg = argv[i];
+
+ if (!strcmp(arg, "stop_exec_queue"))
+ usend_msg.type = UDEVD_STOP_EXEC_QUEUE;
+ else if (!strcmp(arg, "start_exec_queue"))
+ usend_msg.type = UDEVD_START_EXEC_QUEUE;
+ else if (!strncmp(arg, "log_priority=", strlen("log_priority="))) {
+ intval = (int *) usend_msg.envbuf;
+ val = &arg[strlen("log_priority=")];
+ usend_msg.type = UDEVD_SET_LOG_LEVEL;
+ *intval = log_priority(val);
+ info("send log_priority=%i", *intval);
+ } else if (!strncmp(arg, "max_childs=", strlen("max_childs="))) {
+ intval = (int *) usend_msg.envbuf;
+ val = &arg[strlen("max_childs=")];
+ usend_msg.type = UDEVD_SET_MAX_CHILDS;
+ *intval = atoi(val);
+ info("send max_childs=%i", *intval);
+ } else if (strcmp(arg, "help") == 0 || strcmp(arg, "--help") == 0 || strcmp(arg, "-h") == 0) {
+ printf("Usage: udevcontrol COMMAND\n"
+ " log_priority=<level> set the udev log level for the daemon\n"
+ " stop_exec_queue keep udevd from executing events, queue only\n"
+ " start_exec_queue execute events, flush queue\n"
+ " max_childs=<N> maximum number of childs running at the same time\n"
+ " --help print this help text\n\n");
+ exit(0);
+ } else {
+ fprintf(stderr, "unknown option\n\n");
+ exit(1);
+ }
+ }
+
+ if (getuid() != 0) {
+ fprintf(stderr, "need to be root, exit\n\n");
+ exit(1);
}
sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
static int uevent_netlink_sock;
static pid_t sid;
-static int pipefds[2];
+static int pipefds[2] = {-1, -1};
static volatile int sigchilds_waiting;
static volatile int run_msg_q;
static volatile int sig_flag;
/* Set fds to dev/null */
fd = open( "/dev/null", O_RDWR );
if (fd >= 0) {
- dup2(fd, 0);
- dup2(fd, 1);
- dup2(fd, 2);
- if (fd > 2)
- close(fd);
+ dup2(fd, STDIN_FILENO);
+ dup2(fd, STDOUT_FILENO);
+ dup2(fd, STDERR_FILENO);
+ close(fd);
} else
err("error opening /dev/null %s", strerror(errno));
}
exit:
+ if (pipefds[0] > 0)
+ close(pipefds[0]);
+ if (pipefds[1] > 0)
+ close(pipefds[1]);
+
if (udevd_sock > 0)
close(udevd_sock);
#include "udev.h"
#include "udevd.h"
+#include "udev_utils.h"
#include "udev_libc_wrapper.h"
static int uevent_netlink_sock;
int main(int argc, char *argv[])
{
int env = 0;
- int maxsockplus;
fd_set readfds;
+ int i;
int retval;
+ for (i = 1 ; i < argc; i++) {
+ char *arg = argv[i];
+ if (strcmp(arg, "--env") == 0 || strcmp(arg, "-e") == 0) {
+ env = 1;
+ }
+ else if (strcmp(arg, "--help") == 0 || strcmp(arg, "-h") == 0){
+ printf("Usage: udevmonitor [--env]\n"
+ " --env print the whole event environment\n"
+ " --help print this help text\n\n");
+ exit(0);
+ } else {
+ fprintf(stderr, "unknown option\n\n");
+ exit(1);
+ }
+ }
+
if (getuid() != 0) {
- printf("need to be root, exit\n");
+ fprintf(stderr, "need to be root, exit\n\n");
exit(1);
}
- if (argc == 2 && strstr(argv[1], "--env"))
- env = 1;
-
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);
- FD_SET(uevent_netlink_sock, &readfds);
- FD_SET(udev_monitor_sock, &readfds);
- maxsockplus = udev_monitor_sock+1;
+ 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];
buflen = 0;
workreadfds = readfds;
- retval = select(maxsockplus, &workreadfds, NULL, NULL, NULL);
+ retval = select(UDEV_MAX(uevent_netlink_sock, udev_monitor_sock)+1, &workreadfds, NULL, NULL, NULL);
if (retval < 0) {
if (errno != EINTR)
fprintf(stderr, "error receiving uevent message\n");
continue;
}
- if (FD_ISSET(uevent_netlink_sock, &workreadfds)) {
+ if ((uevent_netlink_sock > 0) && FD_ISSET(uevent_netlink_sock, &workreadfds)) {
buflen = recv(uevent_netlink_sock, &buf, sizeof(buf), 0);
if (buflen <= 0) {
fprintf(stderr, "error receiving uevent message\n");
printf("UEVENT[%i] %s\n", time(NULL), buf);
}
- if (FD_ISSET(udev_monitor_sock, &workreadfds)) {
+ if ((udev_monitor_sock > 0) && FD_ISSET(udev_monitor_sock, &workreadfds)) {
buflen = recv(udev_monitor_sock, &buf, sizeof(buf), 0);
if (buflen <= 0) {
fprintf(stderr, "error receiving udev message\n");
}
}
- close(uevent_netlink_sock);
- close(udev_monitor_sock);
+ if (uevent_netlink_sock > 0)
+ close(uevent_netlink_sock);
+ if (udev_monitor_sock > 0)
+ close(udev_monitor_sock);
exit(1);
}