#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
-#include <time.h>
#include <errno.h>
+#include <signal.h>
+#include <sys/time.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/select.h>
-#include <linux/types.h>
#include <linux/netlink.h>
#include "udev.h"
static int uevent_netlink_sock;
static int udev_monitor_sock;
+static volatile int udev_exit;
static int init_udev_monitor_socket(void)
{
return 0;
}
+static void asmlinkage sig_handler(int signum)
+{
+ if (signum == SIGINT || signum == SIGTERM)
+ udev_exit = 1;
+}
+
int main(int argc, char *argv[])
{
+ struct sigaction act;
int env = 0;
fd_set readfds;
int i;
exit(2);
}
+ /* set signal handlers */
+ memset(&act, 0x00, sizeof(struct sigaction));
+ act.sa_handler = (void (*)(int)) sig_handler;
+ sigemptyset(&act.sa_mask);
+ act.sa_flags = SA_RESTART;
+ sigaction(SIGINT, &act, NULL);
+ sigaction(SIGTERM, &act, NULL);
+
retval = init_udev_monitor_socket();
if (retval)
goto out;
printf("udevmonitor prints the received event from the kernel [UEVENT]\n"
"and the event which udev sends out after rule processing [UDEV]\n\n");
- while (1) {
+ while (!udev_exit) {
static char buf[UEVENT_BUFFER_SIZE*2];
ssize_t buflen;
int fdcount;
+ struct timeval tv;
+ struct timezone tz;
+ char timestr[64];
buflen = 0;
FD_ZERO(&readfds);
continue;
}
+ if (gettimeofday(&tv, &tz) == 0) {
+ snprintf(timestr, sizeof(timestr), "%llu.%06u",
+ (unsigned long long) tv.tv_sec, (unsigned int) tv.tv_usec);
+ } else
+ timestr[0] = '\0';
+
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");
continue;
}
- printf("UEVENT[%i] %s\n", time(NULL), buf);
+ printf("UEVENT[%s] %s\n", timestr, buf);
}
if ((udev_monitor_sock > 0) && FD_ISSET(udev_monitor_sock, &readfds)) {
fprintf(stderr, "error receiving udev message\n");
continue;
}
- printf("UDEV [%i] %s\n", time(NULL), buf);
+ printf("UDEV [%s] %s\n", timestr, buf);
}
if (buflen == 0)