Blame src/daemon/main.c

Packit 79f644
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
Packit 79f644
/*
Packit 79f644
 * Copyright © 2011 – 2017 Red Hat, Inc.
Packit 79f644
 *
Packit 79f644
 * This library is free software; you can redistribute it and/or
Packit 79f644
 * modify it under the terms of the GNU Lesser General Public
Packit 79f644
 * License as published by the Free Software Foundation; either
Packit 79f644
 * version 2 of the License, or (at your option) any later version.
Packit 79f644
 *
Packit 79f644
 * This library is distributed in the hope that it will be useful,
Packit 79f644
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 79f644
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 79f644
 * Lesser General Public License for more details.
Packit 79f644
 *
Packit 79f644
 * You should have received a copy of the GNU Lesser General
Packit 79f644
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit 79f644
 */
Packit 79f644
Packit 79f644
#include "config.h"
Packit 79f644
Packit 79f644
#include <glib-unix.h>
Packit 79f644
Packit 79f644
#include <locale.h>
Packit 79f644
#include <signal.h>
Packit 79f644
#include <gio/gio.h>
Packit 79f644
#include <libintl.h>
Packit 79f644
Packit 79f644
#include "goadaemon.h"
Packit 79f644
Packit 79f644
/* ---------------------------------------------------------------------------------------------------- */
Packit 79f644
Packit 79f644
static GMainLoop *loop = NULL;
Packit 79f644
static gboolean opt_replace = FALSE;
Packit 79f644
static gboolean opt_no_sigint = FALSE;
Packit 79f644
static GOptionEntry opt_entries[] =
Packit 79f644
{
Packit 79f644
  {"replace", 0, 0, G_OPTION_ARG_NONE, &opt_replace, "Replace existing daemon", NULL},
Packit 79f644
  {"no-sigint", 0, 0, G_OPTION_ARG_NONE, &opt_no_sigint, "Do not handle SIGINT for controlled shutdown", NULL},
Packit 79f644
  {NULL }
Packit 79f644
};
Packit 79f644
static GoaDaemon *the_daemon = NULL;
Packit 79f644
Packit 79f644
static void
Packit 79f644
on_bus_acquired (GDBusConnection *connection,
Packit 79f644
                 const gchar     *name,
Packit 79f644
                 gpointer         user_data)
Packit 79f644
{
Packit 79f644
  g_return_if_fail (G_IS_DBUS_CONNECTION (connection));
Packit 79f644
  g_return_if_fail (name != NULL && name[0] != '\0');
Packit 79f644
Packit 79f644
  the_daemon = goa_daemon_new (connection);
Packit 79f644
  g_debug ("Connected to the session bus");
Packit 79f644
}
Packit 79f644
Packit 79f644
static void
Packit 79f644
on_name_lost (GDBusConnection *connection,
Packit 79f644
              const gchar     *name,
Packit 79f644
              gpointer         user_data)
Packit 79f644
{
Packit 79f644
  g_return_if_fail (connection == NULL || G_IS_DBUS_CONNECTION (connection));
Packit 79f644
  g_return_if_fail (name != NULL && name[0] != '\0');
Packit 79f644
Packit 79f644
  g_info ("Lost (or failed to acquire) the name %s on the session message bus", name);
Packit 79f644
  g_main_loop_quit (loop);
Packit 79f644
}
Packit 79f644
Packit 79f644
static void
Packit 79f644
on_name_acquired (GDBusConnection *connection,
Packit 79f644
                  const gchar     *name,
Packit 79f644
                  gpointer         user_data)
Packit 79f644
{
Packit 79f644
  g_return_if_fail (G_IS_DBUS_CONNECTION (connection));
Packit 79f644
  g_return_if_fail (name != NULL && name[0] != '\0');
Packit 79f644
Packit 79f644
  g_debug ("Acquired the name %s on the session message bus", name);
Packit 79f644
}
Packit 79f644
Packit 79f644
static gboolean
Packit 79f644
on_sigint (gpointer user_data)
Packit 79f644
{
Packit 79f644
  g_info ("Caught SIGINT. Initiating shutdown.");
Packit 79f644
  g_main_loop_quit (loop);
Packit 79f644
  return FALSE;
Packit 79f644
}
Packit 79f644
Packit 79f644
int
Packit 79f644
main (int    argc,
Packit 79f644
      char **argv)
Packit 79f644
{
Packit 79f644
  GError *error;
Packit 79f644
  GOptionContext *opt_context = NULL;
Packit 79f644
  gint ret = 1;
Packit 79f644
  guint name_owner_id = 0;
Packit 79f644
Packit 79f644
  setlocale (LC_ALL, "");
Packit 79f644
  bindtextdomain (GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR);
Packit 79f644
  bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
Packit 79f644
  textdomain (GETTEXT_PACKAGE);
Packit 79f644
Packit 79f644
  opt_context = g_option_context_new ("GNOME Online Accounts daemon");
Packit 79f644
  g_option_context_add_main_entries (opt_context, opt_entries, NULL);
Packit 79f644
  error = NULL;
Packit 79f644
  if (!g_option_context_parse (opt_context, &argc, &argv, &error))
Packit 79f644
    {
Packit 79f644
      g_critical ("Error parsing options: %s", error->message);
Packit 79f644
      g_error_free (error);
Packit 79f644
      goto out;
Packit 79f644
    }
Packit 79f644
Packit 79f644
  g_message ("goa-daemon version %s starting", PACKAGE_VERSION);
Packit 79f644
Packit 79f644
  loop = g_main_loop_new (NULL, FALSE);
Packit 79f644
Packit 79f644
  if (!opt_no_sigint)
Packit 79f644
    {
Packit 79f644
      g_unix_signal_add (SIGINT, on_sigint, NULL);
Packit 79f644
    }
Packit 79f644
Packit 79f644
  name_owner_id = g_bus_own_name (G_BUS_TYPE_SESSION,
Packit 79f644
                                  "org.gnome.OnlineAccounts",
Packit 79f644
                                  G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT |
Packit 79f644
                                    (opt_replace ? G_BUS_NAME_OWNER_FLAGS_REPLACE : 0),
Packit 79f644
                                  on_bus_acquired,
Packit 79f644
                                  on_name_acquired,
Packit 79f644
                                  on_name_lost,
Packit 79f644
                                  NULL,
Packit 79f644
                                  NULL);
Packit 79f644
Packit 79f644
  g_debug ("Entering main event loop");
Packit 79f644
Packit 79f644
  g_main_loop_run (loop);
Packit 79f644
Packit 79f644
  ret = 0;
Packit 79f644
Packit 79f644
 out:
Packit 79f644
  g_clear_object (&the_daemon);
Packit 79f644
  if (name_owner_id != 0)
Packit 79f644
    g_bus_unown_name (name_owner_id);
Packit 79f644
  g_clear_pointer (&loop, (GDestroyNotify) g_main_loop_unref);
Packit 79f644
  g_clear_pointer (&opt_context, (GDestroyNotify) g_option_context_free);
Packit 79f644
Packit 79f644
  g_message ("goa-daemon version %s exiting", PACKAGE_VERSION);
Packit 79f644
Packit 79f644
  return ret;
Packit 79f644
}