udev-watchdog.c 4.33 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * Copyright (C) 2004-2009 Kay Sievers <kay.sievers@vrfy.org>
 *
 * + Trimmed from udev-149. Fixed passing of devtype parameter.
 *
 * This program is free software: you can redistribute it and/or modify
 * 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/>.
 */

20 21 22 23 24 25 26
#ifndef _POSIX_C_SOURCE
#define _POSIX_C_SOURCE 1
#ifndef _BSD_SOURCE
#define _BSD_SOURCE
#endif
#endif

27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <signal.h>
#include <getopt.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 <libudev.h>

static volatile sig_atomic_t udev_exit;

static void sig_handler(int signum)
{
	if (signum == SIGINT || signum == SIGTERM)
		udev_exit = 2;
}

static int print_device(struct udev_device *device, const char *f_action, const char *f_devsuffix)
{
	struct timeval  tv;
55

56 57 58 59
    const  char *action  = udev_device_get_action(device);
    const  char *devpath = udev_device_get_devpath(device);
    size_t f_len, d_len;

60 61 62
	gettimeofday(&tv, NULL);
	printf("%lu.%06u %s %s\n",
	       (unsigned long) tv.tv_sec, (unsigned int) tv.tv_usec,
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
	       action, devpath);

    if (! strcmp(f_action, action)) {
        f_len = strlen(f_devsuffix);
        d_len = strlen(devpath);

        if (d_len >= f_len && !strcmp(devpath+(d_len-f_len), f_devsuffix))
            return 1;
    }

    return 0;
}

int main(int argc, char *argv[])
{
	int rc = 0;

80
	struct udev *udev;
81 82 83 84 85 86 87

	struct sigaction act;
	sigset_t mask;
	struct udev_monitor *kernel_monitor = NULL;
	fd_set readfds;

	const char *filter_subsys    = "block";
88
	/* const char *filter_devtype   = "partition"; */
89 90 91
	const char *filter_devsuffix;
	const char *filter_devtype;
	const char *filter_action;
92 93 94 95 96

	udev = udev_new();
	if (udev == NULL)
		goto out2;

97
	if (argc != 3)
98
		goto out2;
99

100
	filter_devsuffix = argv[1];
101 102 103 104 105 106 107
	filter_devtype = argv[2];

	if (strcmp(filter_devtype, "cd") == 0) {
		filter_action = "change";
	} else if (strcmp(filter_devtype, "disk") == 0) {
		filter_action = "remove";
	}
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127

	/* set signal handlers */
	memset(&act, 0x00, sizeof(struct sigaction));
	act.sa_handler = sig_handler;
	sigemptyset(&act.sa_mask);
	act.sa_flags = SA_RESTART;
	sigaction(SIGINT, &act, NULL);
	sigaction(SIGTERM, &act, NULL);
	sigemptyset(&mask);
	sigaddset(&mask, SIGINT);
	sigaddset(&mask, SIGTERM);
	sigprocmask(SIG_UNBLOCK, &mask, NULL);

	kernel_monitor = udev_monitor_new_from_netlink(udev, "kernel");
	if (kernel_monitor == NULL) {
		fprintf(stderr, "error: unable to create netlink socket\n");
		rc = 3;
		goto out;
	}

128 129
	if (udev_monitor_filter_add_match_subsystem_devtype(kernel_monitor, filter_subsys, NULL /* filter_devtype */) < 0)
		fprintf(stderr, "error: unable to apply subsystem filter '%s:%s'\n", filter_subsys, "NULL" /* filter_devtype */);
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147

	if (udev_monitor_enable_receiving(kernel_monitor) < 0) {
		fprintf(stderr, "error: unable to subscribe to kernel events\n");
		rc = 4;
		goto out;
	}

	while (!udev_exit) {
		int fdcount;

		FD_ZERO(&readfds);
		if (kernel_monitor != NULL)
			FD_SET(udev_monitor_get_fd(kernel_monitor), &readfds);

		fdcount = select(udev_monitor_get_fd(kernel_monitor)+1,
				 &readfds, NULL, NULL, NULL);
		if (fdcount < 0) {
			if (errno != EINTR)
148
				fprintf(stderr, "error receiving uevent message: %s\n", strerror(errno));
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
			continue;
		}

		if ((kernel_monitor != NULL) && FD_ISSET(udev_monitor_get_fd(kernel_monitor), &readfds)) {
			struct udev_device *device;

			device = udev_monitor_receive_device(kernel_monitor);
			if (device == NULL)
				continue;
			if (print_device(device, filter_action, filter_devsuffix))
                udev_exit = 1;

			udev_device_unref(device);
		}
	}

out:
	udev_monitor_unref(kernel_monitor);

out2:
	udev_unref(udev);

    if (udev_exit == 2)
        rc = 1;

	return rc;
}