#include "config.h"
#include <errno.h>
#include <string.h>
#include <signal.h>
#include <syslog.h>
#include <unistd.h>
#include <stdlib.h>
#include <locale.h>
#include <glib.h>
#include <glib-unix.h>
#include <glib/gi18n.h>
#include <packagekit-glib2/pk-debug.h>
#ifdef HAVE_SYSTEMD_SD_DAEMON_H
#include <systemd/sd-daemon.h>
#endif
#include "pk-engine.h"
#include "pk-shared.h"
#include "pk-transaction.h"
typedef struct {
GMainLoop *loop;
PkEngine *engine;
guint exit_idle_time;
guint timer_id;
} PkMainHelper;
static gboolean
timed_exit_cb (GMainLoop *mainloop)
{
g_main_loop_quit (mainloop);
return FALSE;
}
static gboolean
pk_main_timeout_check_cb (PkMainHelper *helper)
{
guint idle;
idle = pk_engine_get_seconds_idle (helper->engine);
g_debug ("idle is %i", idle);
if (idle > helper->exit_idle_time) {
g_main_loop_quit (helper->loop);
helper->timer_id = 0;
return FALSE;
}
return TRUE;
}
static void
pk_main_quit_cb (PkEngine *engine, GMainLoop *mainloop)
{
g_debug ("engine quit");
g_main_loop_quit (mainloop);
}
static gboolean
pk_main_sigint_cb (gpointer user_data)
{
GMainLoop *mainloop = (GMainLoop *) user_data;
g_debug ("Handling SIGINT");
g_main_loop_quit (mainloop);
return FALSE;
}
int
main (int argc, char *argv[])
{
GMainLoop *loop = NULL;
GOptionContext *context;
PkMainHelper helper;
gboolean ret = TRUE;
gboolean disable_timer = FALSE;
gboolean version = FALSE;
gboolean timed_exit = FALSE;
gboolean immediate_exit = FALSE;
gboolean keep_environment = FALSE;
gint exit_idle_time;
g_autoptr(GError) error = NULL;
g_autofree gchar *backend_name = NULL;
g_autofree gchar *conf_filename = NULL;
g_autoptr(GKeyFile) conf = NULL;
g_autoptr(PkEngine) engine = NULL;
const GOptionEntry options[] = {
{ "backend", '\0', 0, G_OPTION_ARG_STRING, &backend_name,
_("Packaging backend to use, e.g. dummy"), NULL },
{ "disable-timer", '\0', 0, G_OPTION_ARG_NONE, &disable_timer,
_("Disable the idle timer"), NULL },
{ "version", '\0', 0, G_OPTION_ARG_NONE, &version,
_("Show version and exit"), NULL },
{ "timed-exit", '\0', 0, G_OPTION_ARG_NONE, &timed_exit,
_("Exit after a small delay"), NULL },
{ "immediate-exit", '\0', 0, G_OPTION_ARG_NONE, &immediate_exit,
_("Exit after the engine has loaded"), NULL },
{ "keep-environment", '\0', 0, G_OPTION_ARG_NONE, &keep_environment,
_("Don't clear environment on startup"), NULL },
{ NULL }
};
setlocale (LC_ALL, "");
bindtextdomain (GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR);
bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
textdomain (GETTEXT_PACKAGE);
openlog ("PackageKit", LOG_NDELAY, LOG_USER);
pk_is_thread_default ();
context = g_option_context_new (_("PackageKit service"));
g_option_context_add_main_entries (context, options, NULL);
g_option_context_add_group (context, pk_debug_get_option_group ());
g_option_context_parse (context, &argc, &argv, NULL);
g_option_context_free (context);
if (version) {
g_print ("Version %s\n", VERSION);
goto exit_program;
}
#ifdef HAVE_CLEARENV
if (!keep_environment)
clearenv ();
#endif
conf = g_key_file_new ();
conf_filename = pk_util_get_config_filename ();
if (conf_filename == NULL) {
g_printerr ("%s\n", _("Config file was not found."));
goto out;
}
ret = g_key_file_load_from_file (conf, conf_filename,
G_KEY_FILE_NONE, &error);
if (!ret) {
g_autofree gchar *message = g_strdup_printf (_("Failed to load config file: %s"), error->message);
g_printerr ("%s\n", message);
goto out;
}
g_key_file_set_boolean (conf, "Daemon", "KeepEnvironment", keep_environment);
syslog (LOG_DAEMON | LOG_DEBUG, "daemon start");
exit_idle_time = g_key_file_get_integer (conf, "Daemon", "ShutdownTimeout", &error);
if (error != NULL) {
exit_idle_time = 300;
g_clear_error (&error);
}
if (exit_idle_time > 0)
g_debug ("daemon shutdown set to %i seconds", exit_idle_time);
else
g_debug ("daemon will not shut down on idle");
if (backend_name != NULL) {
g_key_file_set_string (conf,
"Daemon",
"DefaultBackend",
backend_name);
}
backend_name = g_key_file_get_string (conf, "Daemon", "DefaultBackend", NULL);
if (backend_name == NULL || g_strcmp0 (backend_name, "auto") == 0) {
ret = pk_util_set_auto_backend (conf, &error);
if (!ret) {
g_autofree gchar *message = g_strdup_printf (_("Failed to resolve auto: %s"), error->message);
g_printerr ("%s\n", message);
goto out;
}
}
loop = g_main_loop_new (NULL, FALSE);
engine = pk_engine_new (conf);
g_signal_connect (engine, "quit",
G_CALLBACK (pk_main_quit_cb), loop);
g_unix_signal_add_full (G_PRIORITY_DEFAULT,
SIGINT,
pk_main_sigint_cb,
loop,
NULL);
ret = pk_engine_load_backend (engine, &error);
if (!ret) {
g_autofree gchar *message = g_strdup_printf (_("Failed to load the backend: %s"), error->message);
g_printerr ("%s\n", message);
goto out;
}
if (timed_exit)
g_timeout_add_seconds (20, (GSourceFunc) timed_exit_cb, loop);
if (exit_idle_time > 0 && !disable_timer) {
helper.engine = engine;
helper.exit_idle_time = exit_idle_time;
helper.loop = loop;
helper.timer_id = g_timeout_add_seconds (5, (GSourceFunc) pk_main_timeout_check_cb, &helper);
g_source_set_name_by_id (helper.timer_id, "[PkMain] main poll");
} else {
helper.timer_id = 0;
}
if (immediate_exit)
g_timeout_add (50, (GSourceFunc) timed_exit_cb, loop);
g_main_loop_run (loop);
out:
syslog (LOG_DAEMON | LOG_DEBUG, "daemon quit");
closelog ();
#ifdef HAVE_SYSTEMD_SD_DAEMON_H
sd_notify (0, "STOPPING=1");
#endif
if (helper.timer_id > 0)
g_source_remove (helper.timer_id);
if (loop != NULL)
g_main_loop_unref (loop);
exit_program:
return 0;
}