sd_event_loop
authorŁukasz Stelmach <l.stelmach@samsung.com>
Wed, 5 Apr 2017 11:57:03 +0000 (13:57 +0200)
committerŁukasz Stelmach <l.stelmach@samsung.com>
Wed, 5 Apr 2017 11:57:03 +0000 (13:57 +0200)
src/faultd.c

index 521847f983334d0469e1038a021ea8a143142ced..1b767d8588a66601e00b5bc27ead568d3817015c 100644 (file)
 #include <stdio.h>
 #include <systemd/sd-bus.h>
 
+int dbus_handler(sd_event_source *s, int fd, uint32_t revents, void *userdata);
+
+int dbus_handler(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
+  sd_bus* bus = userdata;
+  sd_bus_message *m = NULL;
+  int rc;
+
+  for (;;) {
+
+    rc = sd_bus_process(bus, &m);
+    if (rc < 0) {
+      fprintf(stderr, "Failed to process the bus.\n");
+      return rc;
+    }
+
+    if (m == NULL) {
+      fprintf(stderr, "No message.\n");
+      return 0;
+    } else {
+      uint8_t type;
+      int _rc;
+      _rc = sd_bus_message_get_type(m, &type);
+      if (_rc < 0) {
+       fprintf(stderr, "Oops!\n");
+       return _rc;
+      }
+      if (type != SD_BUS_MESSAGE_SIGNAL ||
+         strcmp("org.freedesktop.DBus.Properties", sd_bus_message_get_interface(m)) != 0 ||
+         strcmp("PropertiesChanged", sd_bus_message_get_member(m)) != 0 ||
+         strncmp("/org/freedesktop/systemd1/unit/", sd_bus_message_get_path(m), 31) != 0) {
+       continue;
+      }
+      fprintf(stdout, "Received a message!\n");
+      fprintf(stdout, "    Type: %s\n",
+             type ==  SD_BUS_MESSAGE_METHOD_CALL ? "method call" :
+             (type ==  SD_BUS_MESSAGE_METHOD_RETURN ? "method return" :
+              (type ==  SD_BUS_MESSAGE_METHOD_ERROR ? "method error" :
+               (type ==  SD_BUS_MESSAGE_SIGNAL ? "signal" : "INVALID"))));
+      fprintf(stdout, "    Interface: %s\n", sd_bus_message_get_interface(m));
+      fprintf(stdout, "    Member:    %s\n", sd_bus_message_get_member(m));
+      fprintf(stdout, "    Path:      %s\n", sd_bus_message_get_path(m));
+    }
+
+    sd_bus_message_unref(m);
+    if (rc > 0)
+      continue;
+  }
+}
+
 int main(int ac, char* av[])
 {
+  sd_event* loop;
   sd_bus_error error = SD_BUS_ERROR_NULL;
-  sd_bus_message *m = NULL;
   sd_bus *bus = NULL;
   /* const char *path; */
   int rc;
@@ -58,48 +107,17 @@ int main(int ac, char* av[])
     return -1;
   }
 
-  printf("Hello world!\n");
-  for (;;) {
-
-    rc = sd_bus_process(bus, &m);
-    if (rc < 0) {
-      fprintf(stderr, "Failed to process the bus.\n");
-      break;
-    }
-
-    if (m == NULL) {
-      fprintf(stderr, "No message.\n");
-    } else {
-      uint8_t type;
-      int _rc;
-      _rc = sd_bus_message_get_type(m, &type);
-      if (_rc < 0) {
-       fprintf(stderr, "Oops!\n");
-       break;
-      }
-      if (type != SD_BUS_MESSAGE_SIGNAL ||
-         strcmp("org.freedesktop.DBus.Properties", sd_bus_message_get_interface(m)) != 0 ||
-         strcmp("PropertiesChanged", sd_bus_message_get_member(m)) != 0 ||
-         strncmp("/org/freedesktop/systemd1/unit/", sd_bus_message_get_path(m), 31) != 0) {
-       continue;
-      }
-      fprintf(stdout, "Received a message!\n");
-      fprintf(stdout, "    Type: %s\n",
-             type ==  SD_BUS_MESSAGE_METHOD_CALL ? "method call" :
-             (type ==  SD_BUS_MESSAGE_METHOD_RETURN ? "method return" :
-             (type ==  SD_BUS_MESSAGE_METHOD_ERROR ? "method error" :
-             (type ==  SD_BUS_MESSAGE_SIGNAL ? "signal" : "INVALID"))));
-      fprintf(stdout, "    Interface: %s\n", sd_bus_message_get_interface(m));
-      fprintf(stdout, "    Member:    %s\n", sd_bus_message_get_member(m));
-      fprintf(stdout, "    Path:      %s\n", sd_bus_message_get_path(m));
-
-    }
+  rc = sd_event_new(&loop);
+  if (rc < 0) {
+    fprintf(stderr, "Failed to allocate the event loop.\n");
+    return -1;
+  }
 
-    sd_bus_message_unref(m);
-    if (rc > 0)
-      continue;
+  sd_event_add_io(loop, NULL,   sd_bus_get_fd(bus), EPOLLIN | EPOLLOUT, dbus_handler, (void*)bus);
 
-    sd_bus_wait(bus, (uint64_t) -1);
+  printf("Hello world!\n");
+  for (;;) {
+    rc = sd_event_run(loop, (uint64_t) -1);
     if (rc < 0) {
       fprintf(stderr, "Failed to wait on the bus.\n");
       break;