...
authoralex <[email protected]>
Mon, 16 May 2022 01:29:23 +0000 (18:29 -0700)
committeralex <[email protected]>
Mon, 16 May 2022 01:29:23 +0000 (18:29 -0700)
inc/main.h
src/main.c

index 1be31819312160016fa9d457245a27357a365fb0..4300af81a0420f80a852219ea09cbdf7e29995e3 100644 (file)
@@ -3,6 +3,7 @@
 
 #include<stdio.h>
 #include<stdlib.h>
+#include<string.h>
 #include<unistd.h>
 
 #include<systemd/sd-device.h>
index 89d7fb2b6ea311aea3cc173c35ac903113fd6573..ae5aa95c8a1670026ec74dcd184833c0ef990f6d 100644 (file)
@@ -1,31 +1,40 @@
 #include<main.h>
 
-static int detect_usbs();
+static int detect_usb();
 static int on_event();
 
 int main(int argc, char **argv) {
-       if(detect_usbs()<0) { return EXIT_FAILURE; }
+       if(detect_usb()<0) { return EXIT_FAILURE; }
 
        return EXIT_SUCCESS;
 }
 
-static int detect_usbs() {
+static int detect_usb() {
        sd_device_monitor *monitor;
 
        if(sd_device_monitor_new(&monitor)<0) { return -1; }
 
        if(sd_device_monitor_filter_add_match_subsystem_devtype(monitor,"block","disk")!=0) { return -1; }
 
-       if(sd_device_monitor_filter_update(monitor)!=0) { return -1; }
 
        if(sd_device_monitor_start(monitor,&on_event,NULL)!=0) { return -1; }
 
-       while(1) { }
+       if(sd_event_run(sd_device_monitor_get_event(monitor),UINT64_MAX)<0) { return -1; }
 
        return 1;
 }
 
 static int on_event(sd_device_monitor *monitor, sd_device *dev, void *userdata) {
-       printf("event?\n");
-       return -1;
+       const char *name, *bus;
+
+       if(sd_device_get_devname(dev,&name)!=0) { return -1; }
+       if(sd_device_get_property_value(dev,"ID_BUS",&bus)!=0) { return -1; }
+
+       if(strcmp(bus,"usb")!=0) { return -1; }
+
+       printf("dev name: %s\n",name);
+
+       if(sd_device_monitor_stop(monitor)!=0) { return -1; }
+
+       return 0;
 }