From: Ɓukasz Stelmach Date: Wed, 5 Apr 2017 11:57:03 +0000 (+0200) Subject: sd_event_loop X-Git-Tag: end-of-sprint-1~39 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=66b87a26163b44b5db2db153c786325cb0df2bcb;p=platform%2Fcore%2Fsystem%2Ffaultd.git sd_event_loop --- diff --git a/src/faultd.c b/src/faultd.c index 521847f..1b767d8 100644 --- a/src/faultd.c +++ b/src/faultd.c @@ -19,10 +19,59 @@ #include #include +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;