From: Łukasz Stelmach Date: Wed, 19 Apr 2017 15:02:10 +0000 (+0200) Subject: wip: use log API instead of fprintf(3) X-Git-Tag: end-of-sprint-1~27 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=832b426e7b7b26b444bfd9361ef7dc2b17b3b63f;p=platform%2Fcore%2Fsystem%2Ffaultd.git wip: use log API instead of fprintf(3) --- diff --git a/src/audit.c b/src/audit.c index ceadfc2..23acfa4 100644 --- a/src/audit.c +++ b/src/audit.c @@ -1,11 +1,33 @@ -#include -#include +/* + * This file is a part of 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. + */ + +#define _GNU_SOURCE 1 + #include +#include #include -#include -#include #include +#include +#include +#include + #include "audit.h" +#include "log.h" static struct audit_rule_data rule_data = { .flags = AUDIT_FILTER_EXIT, /* trigger on exit from syscall */ @@ -48,8 +70,9 @@ static int parse_event(char *message, int len, struct event *event) ++p; event->timestamp = strtol(p, &p, 10); + event->pid = 0; - while (p = strchr(p, ' ')) { + while ((p = strchr(p, ' ')) != 0) { ++p; e = strchr(p, '='); key = str2key(p, e - p); @@ -77,7 +100,7 @@ static int audit_handler(sd_event_source *s, int fd, uint32_t revents, void *use ret = audit_get_reply(fd, &reply, GET_REPLY_NONBLOCKING, 0); if (ret < 0) { - fprintf(stderr, "Could not get reply\n"); + log_error("Could not get reply.\n"); return ret; } @@ -87,11 +110,11 @@ static int audit_handler(sd_event_source *s, int fd, uint32_t revents, void *use reply.message[reply.len] = '\0'; ret = parse_event(reply.message, reply.len, &ev); if (ret < 0) { - fprintf(stderr, "Could not parse event\n"); + log_error("Could not parse event\n"); return ret; } - printf("timestamp = %d, pid = %d\n", ev.timestamp, ev.pid); + log_debug("timestamp = %ld, pid = %d\n", ev.timestamp, ev.pid); return 0; } @@ -103,19 +126,19 @@ int faultd_audit_init(sd_event *event) fd = audit_open(); if (fd < 0) { - fprintf(stderr, "Could not open audit socket (%d)\n", fd); + log_error("Could not open audit socket: %m\n"); return fd; } ret = audit_set_pid(fd, getpid(), WAIT_YES); if (ret < 0) { - fprintf(stderr, "Could not set pid (%d)\n", ret); + log_error("Could not set pid (%d)\n", ret); return ret; } ret = sd_event_add_io(event, NULL, fd, EPOLLIN, audit_handler, NULL); if (ret < 0) { - fprintf(stderr, "Could not add event io (%d)\n", ret); + log_error_errno(ret, "Could not add io event: %m"); return ret; } @@ -123,8 +146,8 @@ int faultd_audit_init(sd_event *event) audit_rule_syscallbyname_data(&rule_data, "all"); ret = audit_add_rule_data(fd, &rule_data, AUDIT_FILTER_EXIT, AUDIT_ALWAYS); - if (ret < 0 && ret != -EEXIST) { - fprintf(stderr, "Could not add rule: %d\n", ret); + if (ret <= 0 && ret != -EEXIST) { + log_error("Could not add rule (%d).", ret); return ret; } @@ -137,13 +160,13 @@ int faultd_audit_close(int fd) ret = audit_delete_rule_data(fd, &rule_data, AUDIT_FILTER_EXIT, AUDIT_ALWAYS); if (ret < 0 && ret != -EEXIST) { - fprintf(stderr, "Could not add rule: %d\n", ret); + log_error("Could not add rule (%d)", ret); return ret; } ret = audit_set_pid(fd, 0, WAIT_YES); if (ret < 0) { - fprintf(stderr, "Could not set pid (%d)\n", ret); + log_error("Could not set pid (%d)", ret); return ret; } diff --git a/src/faultd.c b/src/faultd.c index d063b7e..f4928f0 100644 --- a/src/faultd.c +++ b/src/faultd.c @@ -16,12 +16,17 @@ * limitations under the License. */ +#include +#include #include #include #include #include #include +#include + #include "audit.h" +#include "log.h" #include "systemd.h" static int terminate = 0; @@ -38,6 +43,39 @@ int sigint_handler(sd_event_source *s, 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 aufd; @@ -46,15 +84,21 @@ int main(int ac, char* av[]) 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) { - fprintf(stderr, "Failed to acquire the defult system bus connection.\n"); + log_error_errno(rc, "Failed to acquire the defult system bus connection: %m"); return -1; } rc = sd_event_new(&loop); if (rc < 0) { - fprintf(stderr, "Failed to allocate the event loop.\n"); + log_error_errno(rc, "Failed to allocate the event loop."); return -1; } diff --git a/src/log.c b/src/log.c index d28fe7c..fba373e 100644 --- a/src/log.c +++ b/src/log.c @@ -23,6 +23,7 @@ #include #include #include +#include #include #include "log.h" @@ -44,6 +45,25 @@ void log_set_max_level(int level) { log_max_level = level; } +int log_parse_level_name(const char *name) { + int i; + static const char *levels[] = { + [LOG_EMERG] = "emerg", + [LOG_ALERT] = "alert", + [LOG_CRIT] = "crit", + [LOG_ERR] = "err", + [LOG_WARNING] = "warning", + [LOG_NOTICE] = "notice", + [LOG_INFO] = "info", + [LOG_DEBUG] = "debug", + }; + + for (i = 0; i < sizeof(levels)/sizeof(levels[0]); i++) + if (strcmp(levels[i], name) == 0) + return i; + return -EINVAL; +} + int log_internal(int level, int error, const char *file, diff --git a/src/systemd.c b/src/systemd.c index ef624ef..f53ab4c 100644 --- a/src/systemd.c +++ b/src/systemd.c @@ -20,7 +20,9 @@ #include #include #include + #include "systemd.h" +#include "log.h" static int on_unit_properties_changed(sd_bus_message *m, void *userdata, sd_bus_error *ret_error); @@ -31,13 +33,13 @@ int faultd_systemd_init(sd_event* loop) { rc = sd_bus_default_system(&bus); if (rc < 0) { - fprintf(stderr, "Failed to acquire the defult system bus connection.\n"); + log_error_errno(rc, "Failed to acquire the defult system bus connection."); return -1; } rc = sd_bus_attach_event(bus, loop, SD_EVENT_PRIORITY_NORMAL); if (rc < 0) { - fprintf(stderr, "Failed to attach the bus to the event loop.\n"); + log_error_errno(rc, "Failed to attach the bus to the event loop."); return -1; } @@ -50,7 +52,7 @@ int faultd_systemd_init(sd_event* loop) { on_unit_properties_changed, NULL); if (rc < 0) { - fprintf(stderr, "Failed to add match"); + log_error_errno(rc, "Failed to add match."); return -1; } @@ -62,7 +64,7 @@ int faultd_systemd_init(sd_event* loop) { &error, NULL, NULL); if (rc < 0) { - fprintf(stderr, "Failed to subscribe.\n"); + log_error_errno(rc, "Failed to subscribe."); return -1; } @@ -76,7 +78,7 @@ int faultd_systemd_close() { rc = sd_bus_default_system(&bus); if (rc < 0) { - fprintf(stderr, "Failed to acquire the defult system bus connection.\n"); + log_error_errno(rc, "Failed to acquire the defult system bus connection."); return -1; } @@ -88,7 +90,7 @@ int faultd_systemd_close() { &error, NULL, NULL); if (rc < 0) { - fprintf(stderr, "Failed to unsubscribe.\n"); + log_error_errno(rc, "Failed to unsubscribe."); return -1; } return 0; @@ -103,7 +105,7 @@ static int on_unit_properties_changed(sd_bus_message *m, void *userdata, sd_bus_ rc = sd_bus_message_read(m, "s", &interface); if (rc < 0) { - fprintf(stderr, "Invalid message format.\n"); + log_error_errno(rc, "Invalid message format."); goto finish; } if (strcmp("org.freedesktop.systemd1.Unit", interface) != 0) { @@ -115,37 +117,36 @@ static int on_unit_properties_changed(sd_bus_message *m, void *userdata, sd_bus_ fflush(stdout); rc = sd_bus_message_get_type(m, &type); if (rc < 0) { - fprintf(stderr, "Oops!\n"); + log_error_errno(rc, "Failed to get message type."); goto finish; } - 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)); - fprintf(stdout, " Message If: %s\n", interface); + log_debug("Received a message!"); + log_debug(" Type: %s", + 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")))); + log_debug(" Interface: %s", sd_bus_message_get_interface(m)); + log_debug(" Member: %s", sd_bus_message_get_member(m)); + log_debug(" Path: %s", sd_bus_message_get_path(m)); + log_debug(" Message If: %s", interface); rc = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "{sv}"); if (rc < 0) { rc = 0; } - fprintf(stdout, " Message dictionary:\n"); + log_debug(" Message dictionary:"); while((rc = sd_bus_message_enter_container(m, SD_BUS_TYPE_DICT_ENTRY, "sv")) > 0) { const char *key; rc = sd_bus_message_read(m, "s", &key); if (rc < 0) { - fprintf(stderr, "Failed to read a DICT_ENTRY: %s.\n", strerror(-rc)); + log_error_errno(rc, "Failed to read a DICT_ENTRY: %m."); rc = 0; goto finish; } - fprintf(stdout, " %s", key); if (strcmp("ActiveState", key) == 0) { const char *value; @@ -157,15 +158,15 @@ static int on_unit_properties_changed(sd_bus_message *m, void *userdata, sd_bus_ } rc = sd_bus_message_read(m, "s", &value); if (rc < 0) { - log_error_errno(rc, "Failed to read the AciveSate value: %m.\n"); + log_error_errno(rc, "Failed to read the AciveSate value: %m."); rc = 0; goto finish; } rc = sd_bus_message_exit_container(m); - fprintf(stdout, ":%s", value); + log_debug(" %s:%s", key, value); } else { - + log_debug(" %s", key); rc = sd_bus_message_skip(m, "v"); if (rc < 0) { fprintf(stderr, "Failed to skip a value."); @@ -173,11 +174,10 @@ static int on_unit_properties_changed(sd_bus_message *m, void *userdata, sd_bus_ goto finish; } } - fprintf(stdout,"\n"); rc = sd_bus_message_exit_container(m); if (rc < 0) { - fprintf(stderr, "Failed to exit a container: %s.\n", strerror(-rc)); + log_error_errno(rc, "Failed to exit a container: %s.", strerror(-rc)); rc = 0; goto finish; }