Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge branch 'dbuslog' into 'master'
Logging over D-Bus

See merge request mer-core/mms-engine!23
  • Loading branch information
monich committed Jul 8, 2020
2 parents d6e043f + 599d6f1 commit ada5b3a
Show file tree
Hide file tree
Showing 13 changed files with 404 additions and 52 deletions.
4 changes: 2 additions & 2 deletions mms-engine/Makefile
Expand Up @@ -17,7 +17,7 @@ include ../mms-lib/Config.mak
# Required packages
#

PKGS = gio-unix-2.0 gio-2.0 libdbusaccess libglibutil
PKGS = gio-unix-2.0 gio-2.0 libdbusaccess libglibutil libdbuslogserver-gio
LIB_PKGS = libwspcodec gmime-2.6 libgofono libsoup-2.4 dconf

ifdef SAILFISH
Expand All @@ -36,7 +36,7 @@ all: debug release
# Sources
#

SRC = main.c mms_engine.c
SRC = main.c mms_engine.c mms_log.c
GEN_SRC = org.nemomobile.MmsEngine.c

#
Expand Down
28 changes: 25 additions & 3 deletions mms-engine/main.c
@@ -1,6 +1,6 @@
/*
* Copyright (C) 2013-2019 Jolla Ltd.
* Copyright (C) 2013-2019 Slava Monich <slava.monich@jolla.com>
* Copyright (C) 2013-2020 Jolla Ltd.
* Copyright (C) 2013-2020 Slava Monich <slava.monich@jolla.com>
* Copyright (C) 2019 Open Mobile Platform LLC.
*
* This program is free software; you can redistribute it and/or modify
Expand Down Expand Up @@ -57,6 +57,7 @@ typedef struct mms_app_dbus_policy {
#define MMS_ENGINE_DBUS_METHOD_SET_LOG_TYPE "setLogType"
#define MMS_ENGINE_DBUS_METHOD_GET_VERSION "getVersion"
#define MMS_ENGINE_DBUS_METHOD_MIGRATE_SETTINGS "migrateSettings"
#define MMS_ENGINE_DBUS_METHOD_EXIT "exit"

static const DA_ACTION mms_engine_dbus_actions[] = {
#define INIT_DA_ACTION(id) \
Expand All @@ -74,7 +75,8 @@ static const MMSAppDBusPolicy mms_engine_default_dbus_policy = {
MMS_ENGINE_DBUS_METHOD_SEND_MESSAGE"()|"
MMS_ENGINE_DBUS_METHOD_SET_LOG_LEVEL"()|"
MMS_ENGINE_DBUS_METHOD_SET_LOG_TYPE"()|"
MMS_ENGINE_DBUS_METHOD_MIGRATE_SETTINGS"()))|"
MMS_ENGINE_DBUS_METHOD_MIGRATE_SETTINGS"()|"
MMS_ENGINE_DBUS_METHOD_EXIT"()))|"
"((!(user("RADIO_USER")&group("RADIO_GROUP")))&("
MMS_ENGINE_DBUS_METHOD_PUSH"()|"
MMS_ENGINE_DBUS_METHOD_PUSH_NOTIFY "()))=deny",
Expand Down Expand Up @@ -302,6 +304,7 @@ void
mms_app_dbus_config_init(
MMSEngineDbusConfig* dbus)
{
dbus->type = G_BUS_TYPE_SYSTEM;
dbus->engine_access =
mms_app_dbus_policy_new(&mms_engine_default_dbus_policy);
dbus->tx_list_access =
Expand All @@ -327,7 +330,21 @@ mms_app_dbus_config_parse(
MMSEngineDbusConfig* dbus)
{
const char* group = SETTINGS_DBUS_GROUP;
char* type = g_key_file_get_string(file, group, SETTINGS_DBUS_TYPE, NULL);

if (type) {
static const char SYSTEM_BUS[] = "system";
static const char SESSION_BUS[] = "session";

if (!g_strcmp0(type, SYSTEM_BUS)) {
dbus->type = G_BUS_TYPE_SYSTEM;
} else if (!g_strcmp0(type, SESSION_BUS)) {
dbus->type = G_BUS_TYPE_SESSION;
} else {
GWARN("Invalid D-Bys type \"%s\"", type);
}
g_free(type);
}
dbus->engine_access = mms_app_dbus_config_update(dbus->engine_access,
file, group, SETTINGS_DBUS_ENGINE_ACCESS,
&mms_engine_default_dbus_policy);
Expand Down Expand Up @@ -383,6 +400,7 @@ mms_app_parse_options(
char* root_dir = NULL;
gboolean log_modules = FALSE;
gboolean keep_running = FALSE;
gboolean disable_dbus_log = FALSE;
gint size_limit_kb = -1;
gdouble megapixels = -1;
char* root_dir_help = g_strdup_printf(
Expand Down Expand Up @@ -440,6 +458,8 @@ mms_app_parse_options(
"Log output (stdout|syslog|glib) [stdout]", "TYPE" },
{ "log-level", 'l', 0, G_OPTION_ARG_CALLBACK, mms_app_option_loglevel,
"Set log level (repeatable)", "[MODULE:]LEVEL" },
{ "disable-dbus-log", 'D', 0, G_OPTION_ARG_NONE, &disable_dbus_log,
"Disable logging over D-Bus", NULL },
{ "log-modules", 0, 0, G_OPTION_ARG_NONE, &log_modules,
"List available log modules", NULL },
#ifdef MMS_VERSION_STRING
Expand Down Expand Up @@ -493,6 +513,7 @@ mms_app_parse_options(
}
if (ok) {
/* Parse the rest of the command line */
session_bus = (opt->dbus.type == G_BUS_TYPE_SESSION);
ok = g_option_context_parse(options, &argc, &argv, &error);
} else if (error) {
/* Improve error message by prepending the file name */
Expand Down Expand Up @@ -564,6 +585,7 @@ mms_app_parse_options(
root_dir = NULL;
}
if (keep_running) opt->flags |= MMS_ENGINE_FLAG_KEEP_RUNNING;
if (disable_dbus_log) opt->flags |= MMS_ENGINE_FLAG_DISABLE_DBUS_LOG;
if (session_bus) {
GDEBUG("Attaching to session bus");
opt->dbus.type = G_BUS_TYPE_SESSION;
Expand Down
6 changes: 4 additions & 2 deletions mms-engine/mms-engine.pro
@@ -1,7 +1,7 @@
TEMPLATE = app
CONFIG += link_pkgconfig
PKGCONFIG += gmime-2.6 gio-unix-2.0 gio-2.0 glib-2.0 libsoup-2.4 dconf
PKGCONFIG += libwspcodec libgofono libdbusaccess libglibutil
PKGCONFIG += libwspcodec libgofono libdbusaccess libglibutil libdbuslogserver-gio
QMAKE_CFLAGS += -Wno-unused-parameter

include(../mms-lib/mms-lib-config.pri)
Expand Down Expand Up @@ -34,9 +34,11 @@ INCLUDEPATH += $$MMS_TRANSFER_LIST_DIR/include

SOURCES += \
main.c \
mms_engine.c
mms_engine.c \
mms_log.c
HEADERS += \
mms_engine.h \
mms_log.h \
mms_version.h
OTHER_FILES += \
org.nemomobile.MmsEngine.push.conf \
Expand Down
76 changes: 57 additions & 19 deletions mms-engine/mms_engine.c
@@ -1,6 +1,6 @@
/*
* Copyright (C) 2013-2019 Jolla Ltd.
* Copyright (C) 2013-2019 Slava Monich <slava.monich@jolla.com>
* Copyright (C) 2013-2020 Jolla Ltd.
* Copyright (C) 2013-2020 Slava Monich <slava.monich@jolla.com>
* Copyright (C) 2019 Open Mobile Platform LLC.
*
* This program is free software; you can redistribute it and/or modify
Expand All @@ -14,6 +14,8 @@
*/

#include "mms_engine.h"
#include "mms_log.h"

#include "mms_dispatcher.h"
#include "mms_settings.h"
#include "mms_lib_util.h"
Expand All @@ -32,6 +34,8 @@
/* Generated code */
#include "org.nemomobile.MmsEngine.h"

#include <dbuslog_util.h>

#include <dbusaccess_peer.h>
#include <dbusaccess_policy.h>

Expand All @@ -54,7 +58,7 @@ struct mms_engine {
MMSSettings* settings;
MMSDispatcher* dispatcher;
MMSDispatcherDelegate dispatcher_delegate;
MMSLogModule** log_modules;
MMSLog* log;
DAPolicy* dbus_access;
DA_BUS da_bus;
GDBusConnection* engine_bus;
Expand All @@ -63,6 +67,7 @@ struct mms_engine {
gboolean stopped;
gboolean stop_requested;
gboolean keep_running;
gboolean disable_dbus_log;
guint idle_timer_id;
gulong proxy_signal_id[MMS_ENGINE_METHOD_COUNT];
};
Expand Down Expand Up @@ -453,20 +458,9 @@ mms_engine_handle_set_log_level(
if (mms_engine_dbus_access_allowed(engine, call,
MMS_ENGINE_ACTION_SET_LOG_LEVEL)) {
GDEBUG_("%s:%d", module, level);
if (module && module[0]) {
MMSLogModule** ptr = engine->log_modules;

while (*ptr) {
MMSLogModule* log = *ptr++;

if (log->name && log->name[0] && !strcmp(log->name, module)) {
log->level = level;
break;
}
}
} else {
gutil_log_default.level = level;
}
dbus_log_server_set_category_level(engine->log->server,
module[0] ? module : gutil_log_default.name,
dbus_log_level_from_gutil(level));
org_nemomobile_mms_engine_complete_set_log_level(proxy, call);
}
mms_engine_idle_timer_check(engine);
Expand Down Expand Up @@ -553,6 +547,23 @@ mms_engine_handle_migrate_settings(
return TRUE;
}

/* org.nemomobile.MmsEngine.exit */
static
gboolean
mms_engine_handle_exit(
OrgNemomobileMmsEngine* proxy,
GDBusMethodInvocation* call,
MMSEngine* engine)
{
/* mms_engine_dbus_access_allowed completes the call if access is denied */
if (mms_engine_dbus_access_allowed(engine, call, MMS_ENGINE_ACTION_EXIT)) {
GDEBUG("Exit requested over D-Bus");
mms_engine_stop(engine);
org_nemomobile_mms_engine_complete_exit(proxy, call);
}
return TRUE;
}

MMSEngine*
mms_engine_new(
const MMSConfig* config,
Expand Down Expand Up @@ -599,10 +610,14 @@ mms_engine_new(
mms->keep_running = TRUE;
}

if (flags & MMS_ENGINE_FLAG_DISABLE_DBUS_LOG) {
mms->disable_dbus_log = TRUE;
}

mms->cm = cm;
mms->config = config;
mms->settings = settings;
mms->log_modules = log_modules;
mms->log = mms_log_new(dbus->type, log_modules);
mms->dbus_access = da_policy_ref(dbus->engine_access);
mms->da_bus = (dbus->type == G_BUS_TYPE_SESSION) ?
DA_BUS_SESSION : DA_BUS_SYSTEM;
Expand Down Expand Up @@ -638,6 +653,9 @@ mms_engine_new(
mms->proxy_signal_id[MMS_ENGINE_METHOD_MIGRATE_SETTINGS] =
g_signal_connect(mms->proxy, "handle-migrate-settings",
G_CALLBACK(mms_engine_handle_migrate_settings), mms);
mms->proxy_signal_id[MMS_ENGINE_METHOD_EXIT] =
g_signal_connect(mms->proxy, "handle-exit",
G_CALLBACK(mms_engine_handle_exit), mms);

return mms;
}
Expand All @@ -664,14 +682,19 @@ mms_engine_run(
MMSEngine* engine,
GMainLoop* loop)
{
DBusLogServer* logger = engine->disable_dbus_log ? NULL :
engine->log->server;

GASSERT(!engine->loop);
engine->loop = loop;
engine->stopped = FALSE;
engine->stop_requested = FALSE;
mms_dispatcher_start(engine->dispatcher);
dbus_log_server_start(logger);
mms_engine_idle_timer_check(engine);
g_main_loop_run(loop);
mms_engine_idle_timer_stop(engine);
dbus_log_server_stop(logger);
engine->loop = NULL;
}

Expand Down Expand Up @@ -777,10 +800,24 @@ mms_engine_dispose(
mms_connman_unref(mms->cm);
mms->cm = NULL;
}
da_policy_unref(mms->dbus_access);
G_OBJECT_CLASS(mms_engine_parent_class)->dispose(object);
}

/**
* Final stage of deinitialization
*/
static
void
mms_engine_finalize(
GObject* object)
{
MMSEngine* engine = MMS_ENGINE(object);
GVERBOSE_("%p", engine);
da_policy_unref(engine->dbus_access);
mms_log_free(engine->log);
G_OBJECT_CLASS(mms_engine_parent_class)->finalize(object);
}

/**
* Per class initializer
*/
Expand All @@ -792,6 +829,7 @@ mms_engine_class_init(
GObjectClass* object_class = G_OBJECT_CLASS(klass);
GASSERT(object_class);
object_class->dispose = mms_engine_dispose;
object_class->finalize = mms_engine_finalize;
GVERBOSE_("done");
}

Expand Down
8 changes: 5 additions & 3 deletions mms-engine/mms_engine.h
@@ -1,6 +1,6 @@
/*
* Copyright (C) 2013-2019 Jolla Ltd.
* Copyright (C) 2013-2019 Slava Monich <slava.monich@jolla.com>
* Copyright (C) 2013-2020 Jolla Ltd.
* Copyright (C) 2013-2020 Slava Monich <slava.monich@jolla.com>
* Copyright (C) 2019 Open Mobile Platform LLC.
*
* This program is free software; you can redistribute it and/or modify
Expand Down Expand Up @@ -32,6 +32,7 @@
#define MMS_ENGINE_FLAG_OVERRIDE_SIZE_LIMIT (0x04)
#define MMS_ENGINE_FLAG_OVERRIDE_MAX_PIXELS (0x08)
#define MMS_ENGINE_FLAG_OVERRIDE_UAPROF (0x10)
#define MMS_ENGINE_FLAG_DISABLE_DBUS_LOG (0x20)

#ifndef MMS_ENGINE_CONFIG_FILE
/* Default config file */
Expand All @@ -48,7 +49,8 @@
m(SET_LOG_LEVEL) \
m(SET_LOG_TYPE) \
m(GET_VERSION) \
m(MIGRATE_SETTINGS)
m(MIGRATE_SETTINGS) \
m(EXIT)

typedef enum mms_engine_action {
/* Action ids must be non-zero, shift those by one */
Expand Down

0 comments on commit ada5b3a

Please sign in to comment.