]> www.infradead.org Git - users/mchehab/rasdaemon.git/commitdiff
rasdaemon: logging via syslog enabled
authorPetr Holasek <pholasek@redhat.com>
Mon, 8 Apr 2013 17:59:14 +0000 (19:59 +0200)
committerMauro Carvalho Chehab <mchehab@redhat.com>
Mon, 8 Apr 2013 22:45:51 +0000 (19:45 -0300)
Patch adds simple (so far) code for customizing of logging levels
and possible outputs. Macro log(..) were introduced and should be
called in all places where rasdaemon could send some output either
to console or syslog.

Systemd unit file was added, so systemd will guarantee that daemon
will be restarted immediately after stop or killing.

Signed-off-by: Petr Holasek <pholasek@redhat.com>
Signed-off-by: Mauro Carvalho Chehab <mchehab@redhat.com>
Makefile.am
misc/rasdaemon.service [new file with mode: 0644]
ras-logger.h [new file with mode: 0644]
ras-mc-event.c
ras-record.c
rasdaemon.c

index 4cc1b1bf3b9788acaabf2b96f2457edeec78c814..a20dd487d3ce333bc0cb24b2e591e0d5633325ed 100644 (file)
@@ -1,6 +1,7 @@
 ACLOCAL_AMFLAGS=-I m4
 SUBDIRS = libtrace
+EXTRA_DIST = misc/rasdaemon.service
 
-bin_PROGRAMS = rasdaemon
+sbin_PROGRAMS = rasdaemon
 rasdaemon_SOURCES = rasdaemon.c ras-record.c ras-mc-event.c
 rasdaemon_LDADD = -lpthread -lsqlite3 libtrace/libtrace.a
diff --git a/misc/rasdaemon.service b/misc/rasdaemon.service
new file mode 100644 (file)
index 0000000..36cdef5
--- /dev/null
@@ -0,0 +1,10 @@
+[Unit]
+Description=RAS daemon to log the RAS events
+After=syslog.target
+
+[Service]
+ExecStart=/usr/local/sbin/rasdaemon -f
+Restart=on-abort
+
+[Install]
+WantedBy=multi-user.target
diff --git a/ras-logger.h b/ras-logger.h
new file mode 100644 (file)
index 0000000..551b882
--- /dev/null
@@ -0,0 +1,48 @@
+/*
+ * Copyright (C) 2013 Petr Holasek <pholasek@redhat.com>
+ *
+ * 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, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+*/
+
+#ifndef __RAS_LOGGER_H
+
+#include <syslog.h>
+
+/*
+ * Logging macros
+ */
+
+#ifndef TOOL_NAME
+       #define TOOL_NAME "rasdaemon"
+#endif
+
+#define SYSLOG (1 << 0)
+#define TERM   (1 << 1)
+#define ALL    (SYSLOG | TERM)
+/* TODO: global logging limit mask */
+
+#define log(where, level, fmt, args...) \
+{\
+       if (where & SYSLOG)\
+               syslog(level, fmt, ##args);\
+       if (where & TERM) {\
+               fprintf(stderr, "%s: ", TOOL_NAME);\
+               fprintf(stderr, fmt, ##args);\
+               fflush(stderr);\
+       }\
+}
+
+#define __RAS_LOGGER_H
+#endif
index f243deca2ee7d7c192f564ca0b499f0b4931ed90..8e28d25d99e5bd1d2ea7a7cc957bf847c4b7b1df 100644 (file)
@@ -29,6 +29,7 @@
 #include "libtrace/event-parse.h"
 #include "ras-mc-event.h"
 #include "ras-record.h"
+#include "ras-logger.h"
 
 /*
  * Polling time, if read() doesn't block. Currently, trace_pipe_raw never
@@ -59,7 +60,7 @@ int toggle_ras_mc_event(int enable)
        /* Enable RAS events */
        fd = open(DEBUGFS "tracing/set_event", O_RDWR | O_APPEND);
        if (fd < 0) {
-               perror("Open set_event");
+               log(ALL, LOG_WARNING, "Can't open set_event")
                return errno;
        }
        if (enable)
@@ -69,20 +70,20 @@ int toggle_ras_mc_event(int enable)
                rc = write(fd, DISABLE_RAS_MC_EVENT,
                           sizeof(DISABLE_RAS_MC_EVENT));
        if (rc < 0) {
-               perror("can't write to set_event");
+               log(ALL, LOG_WARNING, "Can't write to set_event")
                close(fd);
                return rc;
        }
        close(fd);
        if (!rc) {
-               fprintf(stderr, "nothing was written on set_event\n");
+               log(ALL, LOG_WARNING, "Nothing was written on set_event\n")
                return EIO;
        }
 
        if (enable)
-               printf("RAS events enabled\n");
+               log(ALL, LOG_INFO, "RAS events enabled\n")
        else
-               printf("RAS events disabled\n");
+               log(ALL, LOG_INFO, "RAS events disabled\n")
 
        return 0;
 }
@@ -282,6 +283,7 @@ static void parse_ras_data(struct pthread_data *pdata, struct kbuffer *kbuf,
        record.missed_events = kbuffer_missed_events(kbuf);
        record.record_size = kbuffer_curr_size(kbuf);
 
+       /* TODO - logging */
        trace_seq_init(&s);
        printf("cpu %02d:", pdata->cpu);
        fflush(stdout);
@@ -308,11 +310,11 @@ static int read_ras_event(int fd,
        do {
                ready = poll(&fds, 1, -1);
                if (ready < 0) {
-                       perror("poll");
+                       log(TERM, LOG_WARNING, "poll")
                }
                size = read(fd, page, pdata->ras->page_size);
                if (size < 0) {
-                       perror ("read");
+                       log(TERM, LOG_WARNING, "read")
                        return -1;
                } else if (size > 0) {
                        kbuffer_load_subbuffer(kbuf, page);
@@ -329,7 +331,7 @@ static int read_ras_event(int fd,
                         * need to sleep for a while
                         */
                        if (!warn_sleep) {
-                               printf("Old kernel: need to sleep\n");
+                               log(ALL, LOG_INFO, "Old kernel: need to sleep\n")
                                warn_sleep = 1;
                        }
                        sleep(POLLING_TIME);
@@ -367,13 +369,13 @@ static void *handle_ras_events_cpu(void *priv)
 
        page = malloc(pdata->ras->page_size);
        if (!page) {
-               perror("Can't allocate page");
+               log(TERM, LOG_ERR, "Can't allocate page")
                return NULL;
        }
 
        kbuf = kbuffer_alloc(KBUFFER_LSIZE_8, ENDIAN);
        if (!kbuf) {
-               perror("Can't allocate kbuf");
+               log(TERM, LOG_ERR, "Can't allocate kbuf")
                free(page);
                return NULL;
        }
@@ -385,7 +387,7 @@ static void *handle_ras_events_cpu(void *priv)
 
        fd = open(pipe_raw, O_RDONLY);
        if (fd < 0) {
-               perror("Can't open trace_pipe_raw");
+               log(TERM, LOG_ERR, "Can't open trace_pipe_raw")
                kbuffer_free(kbuf);
                free(page);
                return NULL;
@@ -415,14 +417,14 @@ int handle_ras_events(int record_events)
 
        pevent = pevent_alloc();
        if (!pevent) {
-               perror("Can't allocate pevent");
+               log(TERM, LOG_ERR, "Can't allocate pevent")
                return errno;
        }
 
        fd = open(DEBUGFS "tracing/events/ras/mc_event/format",
                  O_RDONLY);
        if (fd < 0) {
-               perror("Open ras format");
+               log(TERM, LOG_ERR, "Open ras format")
                rc = errno;
                goto free_pevent;
        }
@@ -431,7 +433,7 @@ int handle_ras_events(int record_events)
 
        page = malloc(page_size);
        if (!page) {
-               perror("Can't allocate page to read event format");
+               log(TERM, LOG_ERR, "Can't allocate page to read event format")
                rc = errno;
                close(fd);
                goto free_pevent;
@@ -467,7 +469,7 @@ int handle_ras_events(int record_events)
        if (!data)
                goto free_ras;
 
-       printf("Opening one thread per cpu (%d threads)\n", cpus);
+       log(SYSLOG, LOG_INFO, "Opening one thread per cpu (%d threads)\n", cpus)
        for (i = 0; i < cpus; i++) {
                data[i].ras = ras;
                data[i].cpu = i;
index 1f7556abcb38ee8da5d13273facd1f9977f2941a..ea732ceb25595c6bc479b7f7251f8c844da7c0df 100644 (file)
@@ -21,6 +21,7 @@
  */
 
 #include "ras-mc-event.h"
+#include "ras-logger.h"
 #include <string.h>
 #include <stdio.h>
 
@@ -87,8 +88,8 @@ static int ras_mc_prepare_stmt(struct ras_events *ras)
 
        rc = sqlite3_prepare_v2(ras->db, sql, -1, &ras->stmt, NULL);
        if (rc != SQLITE_OK)
-               printf("Failed to prepare insert db on %s: error = %s\n",
-                      SQLITE_RAS_DB, sqlite3_errmsg(ras->db));
+               log(TERM, LOG_ERR, "Failed to prepare insert db on %s: error = %s\n",
+                      SQLITE_RAS_DB, sqlite3_errmsg(ras->db))
 
        return rc;
 }
@@ -103,15 +104,15 @@ sqlite3 *ras_mc_event_opendb(struct ras_events *ras)
 
        rc = sqlite3_initialize();
        if (rc != SQLITE_OK) {
-               printf("Failed to initialize sqlite: error = %d\n", rc);
+               log(TERM, LOG_ERR, "Failed to initialize sqlite: error = %d\n", rc)
                return NULL;
        }
 
        rc = sqlite3_open_v2(SQLITE_RAS_DB, &db,
                             SQLITE_OPEN_FULLMUTEX | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
        if (rc != SQLITE_OK) {
-               printf("Failed to connect to %s: error = %d\n",
-                      SQLITE_RAS_DB, rc);
+               log(TERM, LOG_ERR, "Failed to connect to %s: error = %d\n",
+                      SQLITE_RAS_DB, rc)
                return NULL;
        }
 
@@ -120,8 +121,8 @@ sqlite3 *ras_mc_event_opendb(struct ras_events *ras)
        strcat(sql, mc_event_db_create_fields);
        rc = sqlite3_exec(db, sql, NULL, NULL, NULL);
        if (rc != SQLITE_OK) {
-               printf("Failed to create db on %s: error = %d\n",
-                      SQLITE_RAS_DB, rc);
+               log(TERM, LOG_ERR, "Failed to create db on %s: error = %d\n",
+                      SQLITE_RAS_DB, rc)
 
                return NULL;
        }
@@ -130,7 +131,7 @@ sqlite3 *ras_mc_event_opendb(struct ras_events *ras)
 
        rc = ras_mc_prepare_stmt(ras);
        if (rc == SQLITE_OK)
-               printf("Recording events at %s\n", SQLITE_RAS_DB, rc);
+               log(TERM, LOG_INFO, "Recording events at %s\n", SQLITE_RAS_DB, rc)
 
        return db;
 }
@@ -139,7 +140,7 @@ int ras_store_mc_event(struct ras_events *ras, struct ras_mc_event *ev)
 {
        int rc;
 
-       printf("store_event: %p\n", ras->stmt);
+       log(TERM, LOG_INFO, "store_event: %p\n", ras->stmt)
        if (!ras->stmt)
                return 0;
 
@@ -158,12 +159,12 @@ int ras_store_mc_event(struct ras_events *ras, struct ras_mc_event *ev)
        sqlite3_bind_text(ras->stmt, 13, ev->driver_detail, -1, NULL);
        rc = sqlite3_step(ras->stmt);
        if (rc != SQLITE_OK && rc != SQLITE_DONE)
-               printf("Failed to do step on sqlite: error = %d\n", rc);
+               log(TERM, LOG_ERR, "Failed to do step on sqlite: error = %d\n", rc)
        rc = sqlite3_finalize(ras->stmt);
        if (rc != SQLITE_OK && rc != SQLITE_DONE)
-               printf("Failed to do finalize insert on sqlite: error = %d\n",
-                      rc);
-       printf("register interted at db\n");
+               log(TERM, LOG_ERR, "Failed to do finalize insert on sqlite: error = %d\n",
+                      rc)
+       log(TERM, LOG_INFO, "register interted at db\n")
 
        return rc;
 }
index 16004a8ca5955a6d63ed4e59f97153b7f201e777..479bc6c235fa686650382fe09d88f9844b6bd23b 100644 (file)
 
 #include <argp.h>
 #include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
 
 #include "ras-record.h"
+#include "ras-logger.h"
 
 /*
  * Arguments(argp) handling logic and main
@@ -36,6 +38,7 @@ const char *argp_program_bug_address = "Mauro Carvalho Chehab <mchehab@redhat.co
 struct arguments {
        int record_events;
        int enable_ras;
+       int foreground;
 };
 
 static error_t parse_opt(int k, char *arg, struct argp_state *state)
@@ -52,6 +55,9 @@ static error_t parse_opt(int k, char *arg, struct argp_state *state)
        case 'r':
                args->record_events++;
                break;
+       case 'f':
+               args->foreground++;
+               break;
        default:
                return ARGP_ERR_UNKNOWN;
        }
@@ -66,6 +72,7 @@ int main(int argc, char *argv[])
                {"enable",  'e', 0, 0, "enable RAS events and exit", 0},
                {"disable", 'd', 0, 0, "disable RAS events and exit", 0},
                {"record",  'r', 0, 0, "record events via sqlite3", 0},
+               {"foreground", 'f', 0, 0, "run foreground, not daemonize"},
 
                { 0, 0, 0, 0, 0, 0 }
        };
@@ -90,8 +97,15 @@ int main(int argc, char *argv[])
        else if (args.enable_ras < 0)
                toggle_ras_mc_event(0);
 
-       if (!args.enable_ras)
-               handle_ras_events(args.record_events);
+       if (args.enable_ras)
+               return 0;
+
+       openlog(TOOL_NAME, 0, LOG_DAEMON);
+       if (!args.foreground)
+               if (daemon(0,0))
+                       exit(EXIT_FAILURE);
+
+       handle_ras_events(args.record_events);
 
        return 0;
 }