chiark / gitweb /
scsi_id: compiler warning on 32-bit
[elogind.git] / udev / udevadm-monitor.c
index 0b9b8118097c17c546fa26df19b50f2fb4e8dde8..a798a81626734c5ee4db41518c6ad018819168d6 100644 (file)
@@ -1,19 +1,18 @@
 /*
- * Copyright (C) 2004-2006 Kay Sievers <kay.sievers@vrfy.org>
+ * Copyright (C) 2004-2008 Kay Sievers <kay.sievers@vrfy.org>
  *
- *     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 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.
+ * 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/>.
  */
 
 #include <unistd.h>
 #include <linux/netlink.h>
 
 #include "udev.h"
-#include "udevd.h"
-
-static int uevent_netlink_sock = -1;
-static int udev_monitor_sock = -1;
-static volatile int udev_exit;
-
-static int init_udev_monitor_socket(void)
-{
-       struct sockaddr_un saddr;
-       socklen_t addrlen;
-       int retval;
-
-       memset(&saddr, 0x00, sizeof(saddr));
-       saddr.sun_family = AF_LOCAL;
-       /* use abstract namespace for socket path */
-       strcpy(&saddr.sun_path[1], "/org/kernel/udev/monitor");
-       addrlen = offsetof(struct sockaddr_un, sun_path) + 1 + strlen(&saddr.sun_path[1]);
-
-       udev_monitor_sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
-       if (udev_monitor_sock == -1) {
-               fprintf(stderr, "error getting socket: %s\n", strerror(errno));
-               return -1;
-       }
-
-       /* the bind takes care of ensuring only one copy running */
-       retval = bind(udev_monitor_sock, (struct sockaddr *) &saddr, addrlen);
-       if (retval < 0) {
-               fprintf(stderr, "bind failed: %s\n", strerror(errno));
-               close(udev_monitor_sock);
-               udev_monitor_sock = -1;
-               return -1;
-       }
-
-       return 0;
-}
-
-static int init_uevent_netlink_sock(void)
-{
-       struct sockaddr_nl snl;
-       int retval;
 
-       memset(&snl, 0x00, sizeof(struct sockaddr_nl));
-       snl.nl_family = AF_NETLINK;
-       snl.nl_pid = getpid();
-       snl.nl_groups = 1;
-
-       uevent_netlink_sock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
-       if (uevent_netlink_sock == -1) {
-               fprintf(stderr, "error getting socket: %s\n", strerror(errno));
-               return -1;
-       }
-
-       retval = bind(uevent_netlink_sock, (struct sockaddr *) &snl,
-                     sizeof(struct sockaddr_nl));
-       if (retval < 0) {
-               fprintf(stderr, "bind failed: %s\n", strerror(errno));
-               close(uevent_netlink_sock);
-               uevent_netlink_sock = -1;
-               return -1;
-       }
-
-       return 0;
-}
+static int udev_exit;
 
 static void asmlinkage sig_handler(int signum)
 {
@@ -103,24 +41,16 @@ static void asmlinkage sig_handler(int signum)
                udev_exit = 1;
 }
 
-static const char *search_key(const char *searchkey, const char *buf, size_t buflen)
+static int print_properties_cb(struct udev_device *udev_device, const char *key, const char *value, void *data)
 {
-       size_t bufpos = 0;
-       size_t searchkeylen = strlen(searchkey);
-
-       while (bufpos < buflen) {
-               const char *key;
-               int keylen;
+       printf("%s=%s\n", key, value);
+       return 0;
+}
 
-               key = &buf[bufpos];
-               keylen = strlen(key);
-               if (keylen == 0)
-                       break;
-                if ((strncmp(searchkey, key, searchkeylen) == 0) && key[searchkeylen] == '=')
-                       return &key[searchkeylen + 1];
-               bufpos += keylen + 1;
-       }
-       return NULL;
+static void print_properties(struct udev_device *device)
+{
+       udev_device_get_properties(device, print_properties_cb, NULL);
+       printf("\n");
 }
 
 int udevadm_monitor(struct udev *udev, int argc, char *argv[])
@@ -130,8 +60,10 @@ int udevadm_monitor(struct udev *udev, int argc, char *argv[])
        int env = 0;
        int print_kernel = 0;
        int print_udev = 0;
+       struct udev_monitor *udev_monitor = NULL;
+       struct udev_monitor *kernel_monitor = NULL;
        fd_set readfds;
-       int retval = 0;
+       int rc = 0;
 
        static const struct option options[] = {
                { "environment", 0, NULL, 'e' },
@@ -187,39 +119,45 @@ int udevadm_monitor(struct udev *udev, int argc, char *argv[])
 
        printf("monitor will print the received events for:\n");
        if (print_udev) {
-               retval = init_udev_monitor_socket();
-               if (retval)
+               udev_monitor = udev_monitor_new_from_socket(udev, "@/org/kernel/udev/monitor");
+               if (udev_monitor == NULL) {
+                       rc = 1;
                        goto out;
+               }
+               if (udev_monitor_enable_receiving(udev_monitor) < 0) {
+                       rc = 2;
+                       goto out;
+               }
                printf("UDEV the event which udev sends out after rule processing\n");
        }
        if (print_kernel) {
-               retval = init_uevent_netlink_sock();
-               if (retval)
+               kernel_monitor = udev_monitor_new_from_netlink(udev);
+               if (kernel_monitor == NULL) {
+                       rc = 3;
+                       goto out;
+               }
+               if (udev_monitor_enable_receiving(kernel_monitor) < 0) {
+                       rc = 4;
                        goto out;
+               }
                printf("UEVENT the kernel uevent\n");
        }
        printf("\n");
 
        while (!udev_exit) {
-               char buf[UEVENT_BUFFER_SIZE*2];
-               ssize_t buflen;
-               ssize_t bufpos;
-               ssize_t keys;
                int fdcount;
                struct timeval tv;
                struct timezone tz;
                char timestr[64];
-               const char *source = NULL;
-               const char *devpath, *action, *subsys;
 
-               buflen = 0;
                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);
+               if (kernel_monitor != NULL)
+                       FD_SET(udev_monitor_get_fd(kernel_monitor), &readfds);
+               if (udev_monitor != NULL)
+                       FD_SET(udev_monitor_get_fd(udev_monitor), &readfds);
 
-               fdcount = select(UDEV_MAX(uevent_netlink_sock, udev_monitor_sock)+1, &readfds, NULL, NULL, NULL);
+               fdcount = select(UDEV_MAX(udev_monitor_get_fd(kernel_monitor), udev_monitor_get_fd(udev_monitor))+1,
+                                &readfds, NULL, NULL, NULL);
                if (fdcount < 0) {
                        if (errno != EINTR)
                                fprintf(stderr, "error receiving uevent message: %s\n", strerror(errno));
@@ -232,58 +170,35 @@ int udevadm_monitor(struct udev *udev, int argc, char *argv[])
                } 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: %s\n", strerror(errno));
+               if ((kernel_monitor != NULL) && FD_ISSET(udev_monitor_get_fd(kernel_monitor), &readfds)) {
+                       struct udev_device *device = udev_monitor_receive_device(kernel_monitor);
+                       if (device == NULL)
                                continue;
-                       }
-                       source = "UEVENT";
+                       printf("UEVENT[%s] %-8s %s (%s)\n", timestr,
+                              udev_device_get_action(device),
+                              udev_device_get_devpath(device),
+                              udev_device_get_subsystem(device));
+                       if (env)
+                               print_properties(device);
+                       udev_device_unref(device);
                }
 
-               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: %s\n", strerror(errno));
+               if ((udev_monitor != NULL) && FD_ISSET(udev_monitor_get_fd(udev_monitor), &readfds)) {
+                       struct udev_device *device = udev_monitor_receive_device(udev_monitor);
+                       if (device == NULL)
                                continue;
-                       }
-                       source = "UDEV  ";
-               }
-
-               if (buflen == 0)
-                       continue;
-
-               keys = strlen(buf) + 1; /* start of payload */
-               devpath = search_key("DEVPATH", &buf[keys], buflen);
-               action = search_key("ACTION", &buf[keys], buflen);
-               subsys = search_key("SUBSYSTEM", &buf[keys], buflen);
-               printf("%s[%s] %-8s %s (%s)\n", source, timestr, action, devpath, subsys);
-
-               /* print environment */
-               bufpos = keys;
-               if (env) {
-                       while (bufpos < buflen) {
-                               int keylen;
-                               char *key;
-
-                               key = &buf[bufpos];
-                               keylen = strlen(key);
-                               if (keylen == 0)
-                                       break;
-                               printf("%s\n", key);
-                               bufpos += keylen + 1;
-                       }
-                       printf("\n");
+                       printf("UDEV  [%s] %-8s %s (%s)\n", timestr,
+                              udev_device_get_action(device),
+                              udev_device_get_devpath(device),
+                              udev_device_get_subsystem(device));
+                       if (env)
+                               print_properties(device);
+                       udev_device_unref(device);
                }
        }
 
 out:
-       if (uevent_netlink_sock >= 0)
-               close(uevent_netlink_sock);
-       if (udev_monitor_sock >= 0)
-               close(udev_monitor_sock);
-
-       if (retval)
-               return 1;
-       return 0;
+       udev_monitor_unref(udev_monitor);
+       udev_monitor_unref(kernel_monitor);
+       return rc;
 }