Add main faultd file
authorŁukasz Stelmach <l.stelmach@samsung.com>
Mon, 8 May 2017 21:00:53 +0000 (23:00 +0200)
committerKrzysztof Opasiak <k.opasiak@samsung.com>
Tue, 9 May 2017 11:49:16 +0000 (13:49 +0200)
src/faultd.c [new file with mode: 0644]

diff --git a/src/faultd.c b/src/faultd.c
new file mode 100644 (file)
index 0000000..be828ec
--- /dev/null
@@ -0,0 +1,125 @@
+/*
+ * faultd
+ *
+ * Copyright © 2017 Samsung Electronics
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <errno.h>
+#include <getopt.h>
+#include <stdio.h>
+#include <poll.h>
+#include <systemd/sd-bus.h>
+#include <systemd/sd-event.h>
+#include <signal.h>
+#include <syslog.h>
+
+#include "log.h"
+#include "module.h"
+
+static int terminate = 0;
+
+int sigint_handler(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata);
+
+int sigint_handler(sd_event_source *s,
+                   const struct signalfd_siginfo *si,
+                   void *userdata)
+{
+        sd_event* loop = userdata;
+        sd_event_exit(loop, 0);
+        terminate = 1;
+        return 0;
+}
+
+static int parse_argv(int ac, char* av[]) {
+        int c, r;
+        enum {
+                ARG_LOG_LEVEL = 0x100,
+        };
+        static const struct option options[] = {
+                {"log-level", required_argument, NULL, ARG_LOG_LEVEL},
+                {}
+        };
+
+        while ((c = getopt_long(ac, av, "D", options, NULL)) >= 0) {
+                switch (c) {
+
+                case ARG_LOG_LEVEL:
+                        r = log_parse_level_name(optarg);
+                        if (r < 0) {
+                                fprintf(stderr, "Unknown log level: \"%s\"\n", optarg);
+                                return r;
+                        }
+                        log_set_max_level(r);
+                        break;
+
+                case 'D':
+                        log_set_max_level(LOG_DEBUG);
+                        break;
+
+                default:
+                        return -EINVAL;
+                }
+        }
+        return 0;
+}
+
+int main(int ac, char* av[])
+{
+        int rc;
+        sd_bus *bus = NULL;
+        sd_event* loop;
+        sigset_t ss;
+
+        rc = parse_argv(ac, av);
+        if (rc < 0) {
+                fprintf(stderr, "Failed to parse command line options: %s\n", strerror(-rc));
+                return -1;
+        }
+
+        rc = sd_bus_default_system(&bus);
+        if (rc < 0) {
+                log_error_errno(rc, "Failed to acquire the default system bus connection: %m");
+                return -1;
+        }
+
+        rc = sd_event_new(&loop);
+        if (rc < 0) {
+                log_error_errno(rc, "Failed to allocate the event loop.");
+                return -1;
+        }
+
+        /* If there is way to register a catch-all handler */
+        /* sd_bus_attach_event(bus, loop, SD_EVENT_PRIORITY_NORMAL); */
+        sigemptyset(&ss);
+        sigaddset(&ss, SIGINT);
+        rc = sigprocmask(SIG_BLOCK, &ss, NULL);
+        rc = sd_event_add_signal(loop, NULL, SIGINT, sigint_handler, loop);
+
+
+        rc = faultd_modules_init(loop);
+        if (rc < 0) {
+                log_error("Failed to initialize modules %d.", rc);
+                return -1;
+        }
+
+        printf("Hello world!\n");
+
+        sd_event_loop(loop);
+
+        faultd_modules_cleanup();
+
+        sd_bus_close(bus);
+        return 0;
+}