| 1 |
/* |
| 2 |
* fprint D-Bus daemon |
| 3 |
* Copyright (C) 2008 Daniel Drake <[email protected]> |
| 4 |
* Copyright (C) 2020 Marco Trevisan <[email protected]> |
| 5 |
* |
| 6 |
* This program is free software; you can redistribute it and/or modify |
| 7 |
* it under the terms of the GNU General Public License as published by |
| 8 |
* the Free Software Foundation; either version 2 of the License, or |
| 9 |
* (at your option) any later version. |
| 10 |
* |
| 11 |
* This program is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
* GNU General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU General Public License along |
| 17 |
* with this program; if not, write to the Free Software Foundation, Inc., |
| 18 |
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 |
*/ |
| 20 |
|
| 21 |
#include "config.h" |
| 22 |
|
| 23 |
#include <locale.h> |
| 24 |
#include <poll.h> |
| 25 |
#include <stdlib.h> |
| 26 |
|
| 27 |
#include <gio/gio.h> |
| 28 |
#include <glib.h> |
| 29 |
#include <glib/gi18n.h> |
| 30 |
#include <fprint.h> |
| 31 |
#include <glib-object.h> |
| 32 |
#include <glib-unix.h> |
| 33 |
#include <gmodule.h> |
| 34 |
|
| 35 |
#include "fprintd.h" |
| 36 |
#include "storage.h" |
| 37 |
#include "file_storage.h" |
| 38 |
|
| 39 |
fp_storage store; |
| 40 |
|
| 41 |
static gboolean no_timeout = FALSE; |
| 42 |
static gboolean g_fatal_warnings = FALSE; |
| 43 |
|
| 44 |
static void |
| 45 |
set_storage_file (void) |
| 46 |
{ |
| 47 |
store.init = &file_storage_init; |
| 48 |
store.deinit = &file_storage_deinit; |
| 49 |
store.print_data_save = &file_storage_print_data_save; |
| 50 |
store.print_data_load = &file_storage_print_data_load; |
| 51 |
store.print_data_delete = &file_storage_print_data_delete; |
| 52 |
store.discover_prints = &file_storage_discover_prints; |
| 53 |
store.discover_users = &file_storage_discover_users; |
| 54 |
} |
| 55 |
|
| 56 |
static gboolean |
| 57 |
load_storage_module (const char *module_name) |
| 58 |
{ |
| 59 |
GModule *module; |
| 60 |
g_autofree char *filename = NULL; |
| 61 |
|
| 62 |
#if !GLIB_CHECK_VERSION (2, 76, 0) |
| 63 |
filename = g_module_build_path (PLUGINDIR, module_name); |
| 64 |
#else |
| 65 |
filename = g_build_filename (PLUGINDIR, module_name, NULL); |
| 66 |
#endif |
| 67 |
module = g_module_open (filename, 0); |
| 68 |
if (module == NULL) |
| 69 |
return FALSE; |
| 70 |
|
| 71 |
g_debug ("About to load module '%s'", filename); |
| 72 |
if (!g_module_symbol (module, "init", (gpointer *) &store.init) || |
| 73 |
!g_module_symbol (module, "deinit", (gpointer *) &store.deinit) || |
| 74 |
!g_module_symbol (module, "print_data_save", (gpointer *) &store.print_data_save) || |
| 75 |
!g_module_symbol (module, "print_data_load", (gpointer *) &store.print_data_load) || |
| 76 |
!g_module_symbol (module, "print_data_delete", (gpointer *) &store.print_data_delete) || |
| 77 |
!g_module_symbol (module, "discover_prints", (gpointer *) &store.discover_prints) || |
| 78 |
!g_module_symbol (module, "discover_users", (gpointer *) &store.discover_users)) |
| 79 |
{ |
| 80 |
g_module_close (module); |
| 81 |
g_debug ("Failed to load module. Please update your code."); |
| 82 |
return FALSE; |
| 83 |
} |
| 84 |
|
| 85 |
g_module_make_resident (module); |
| 86 |
|
| 87 |
return TRUE; |
| 88 |
} |
| 89 |
|
| 90 |
static gboolean |
| 91 |
load_conf (void) |
| 92 |
{ |
| 93 |
g_autofree char *filename = NULL; |
| 94 |
g_autofree char *module_name = NULL; |
| 95 |
|
| 96 |
g_autoptr(GKeyFile) file = NULL; |
| 97 |
g_autoptr(GError) error = NULL; |
| 98 |
|
| 99 |
filename = g_build_filename (SYSCONFDIR, "fprintd.conf", NULL); |
| 100 |
file = g_key_file_new (); |
| 101 |
g_debug ("About to load configuration file '%s'", filename); |
| 102 |
if (!g_key_file_load_from_file (file, filename, G_KEY_FILE_NONE, &error)) |
| 103 |
{ |
| 104 |
g_warning ("Could not open \"%s\": %s\n", filename, error->message); |
| 105 |
return FALSE; |
| 106 |
} |
| 107 |
|
| 108 |
module_name = g_key_file_get_string (file, "storage", "type", &error); |
| 109 |
if (module_name == NULL) |
| 110 |
return FALSE; |
| 111 |
|
| 112 |
if (g_str_equal (module_name, "file")) |
| 113 |
{ |
| 114 |
set_storage_file (); |
| 115 |
return TRUE; |
| 116 |
} |
| 117 |
|
| 118 |
return load_storage_module (module_name); |
| 119 |
} |
| 120 |
|
| 121 |
static const GOptionEntry entries[] = { |
| 122 |
{"g-fatal-warnings", 0, 0, G_OPTION_ARG_NONE, &g_fatal_warnings, "Make all warnings fatal", NULL}, |
| 123 |
{"no-timeout", 't', 0, G_OPTION_ARG_NONE, &no_timeout, "Do not exit after unused for a while", NULL}, |
| 124 |
{ NULL } |
| 125 |
}; |
| 126 |
|
| 127 |
static gboolean |
| 128 |
sigterm_callback (gpointer data) |
| 129 |
{ |
| 130 |
GMainLoop *loop = data; |
| 131 |
|
| 132 |
g_main_loop_quit (loop); |
| 133 |
return FALSE; |
| 134 |
} |
| 135 |
|
| 136 |
static void |
| 137 |
on_name_acquired (GDBusConnection *connection, |
| 138 |
const char *name, |
| 139 |
gpointer user_data) |
| 140 |
{ |
| 141 |
g_debug ("D-Bus service launched with name: %s", name); |
| 142 |
} |
| 143 |
|
| 144 |
static void |
| 145 |
on_name_lost (GDBusConnection *connection, |
| 146 |
const char *name, |
| 147 |
gpointer user_data) |
| 148 |
{ |
| 149 |
GMainLoop *loop = user_data; |
| 150 |
|
| 151 |
g_warning ("Failed to get name: %s", name); |
| 152 |
|
| 153 |
g_main_loop_quit (loop); |
| 154 |
} |
| 155 |
|
| 156 |
int |
| 157 |
main (int argc, char **argv) |
| 158 |
{ |
| 159 |
g_autoptr(GOptionContext) context = NULL; |
| 160 |
g_autoptr(GMainLoop) loop = NULL; |
| 161 |
g_autoptr(GError) error = NULL; |
| 162 |
g_autoptr(FprintManager) manager = NULL; |
| 163 |
g_autoptr(GDBusConnection) connection = NULL; |
| 164 |
guint32 request_name_ret; |
| 165 |
|
| 166 |
setlocale (LC_ALL, ""); |
| 167 |
|
| 168 |
bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR); |
| 169 |
bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8"); |
| 170 |
textdomain (GETTEXT_PACKAGE); |
| 171 |
|
| 172 |
context = g_option_context_new ("Fingerprint handler daemon"); |
| 173 |
g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE); |
| 174 |
|
| 175 |
if (g_option_context_parse (context, &argc, &argv, &error) == FALSE) |
| 176 |
{ |
| 177 |
g_warning ("couldn't parse command-line options: %s\n", error->message); |
| 178 |
return 1; |
| 179 |
} |
| 180 |
|
| 181 |
if (g_fatal_warnings) |
| 182 |
{ |
| 183 |
GLogLevelFlags fatal_mask; |
| 184 |
|
| 185 |
fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK); |
| 186 |
fatal_mask |= G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL; |
| 187 |
g_log_set_always_fatal (fatal_mask); |
| 188 |
} |
| 189 |
|
| 190 |
/* Obtain a connection to the system bus */ |
| 191 |
connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error); |
| 192 |
if (!G_IS_DBUS_CONNECTION (connection)) |
| 193 |
{ |
| 194 |
g_warning ("Failed to open connection to bus: %s", error->message); |
| 195 |
return 1; |
| 196 |
} |
| 197 |
|
| 198 |
/* Load the configuration file, |
| 199 |
* and the default storage plugin */ |
| 200 |
if (!load_conf ()) |
| 201 |
set_storage_file (); |
| 202 |
store.init (); |
| 203 |
|
| 204 |
loop = g_main_loop_new (NULL, FALSE); |
| 205 |
g_unix_signal_add (SIGTERM, sigterm_callback, loop); |
| 206 |
|
| 207 |
g_debug ("Launching FprintObject"); |
| 208 |
|
| 209 |
/* create the one instance of the Manager object to be shared between |
| 210 |
* all fprintd users. This blocks until all the devices are enumerated */ |
| 211 |
manager = fprint_manager_new (connection, no_timeout); |
| 212 |
|
| 213 |
/* Obtain the well-known name after the manager has been initialized. |
| 214 |
* Otherwise a client immediately enumerating the devices will not see |
| 215 |
* any. */ |
| 216 |
request_name_ret = g_bus_own_name_on_connection (connection, |
| 217 |
FPRINT_SERVICE_NAME, |
| 218 |
G_BUS_NAME_OWNER_FLAGS_NONE, |
| 219 |
on_name_acquired, |
| 220 |
on_name_lost, |
| 221 |
loop, NULL); |
| 222 |
|
| 223 |
g_debug ("entering main loop"); |
| 224 |
g_main_loop_run (loop); |
| 225 |
g_bus_unown_name (request_name_ret); |
| 226 |
g_debug ("main loop completed"); |
| 227 |
|
| 228 |
store.deinit (); |
| 229 |
|
| 230 |
return 0; |
| 231 |
} |