/** * pk_offline_update_reboot: **/ static void pk_offline_update_reboot (void) { GDBusConnection *connection; GError *error = NULL; GVariant *val = NULL; /* allow testing without rebooting */ if (g_getenv ("PK_OFFLINE_UPDATE_TEST") != NULL) { g_print ("TESTING, so not rebooting\n"); return; } /* reboot using systemd */ pk_offline_update_set_plymouth_mode ("shutdown"); pk_offline_update_set_plymouth_msg ("Rebooting after installing updates..."); connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error); if (connection == NULL) { g_warning ("Failed to get system bus connection: %s", error->message); g_error_free (error); goto out; } val = g_dbus_connection_call_sync (connection, "org.freedesktop.systemd1", "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "Reboot", NULL, NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error); if (val == NULL) { g_warning ("Failed to reboot: %s", error->message); g_error_free (error); goto out; } out: if (connection != NULL) g_object_unref (connection); if (val != NULL) g_variant_unref (val); }
/** * pk_offline_update_power_off: **/ static void pk_offline_update_power_off (void) { _cleanup_error_free_ GError *error = NULL; _cleanup_object_unref_ GDBusConnection *connection = NULL; _cleanup_variant_unref_ GVariant *val = NULL; /* reboot using systemd */ sd_journal_print (LOG_INFO, "shutting down"); pk_offline_update_set_plymouth_mode ("shutdown"); /* TRANSLATORS: we've finished doing offline updates */ pk_offline_update_set_plymouth_msg (_("Shutting down after installing updates…")); connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error); if (connection == NULL) { sd_journal_print (LOG_WARNING, "Failed to get system bus connection: %s", error->message); return; } val = g_dbus_connection_call_sync (connection, "org.freedesktop.systemd1", "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "PowerOff", NULL, NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error); if (val == NULL) { sd_journal_print (LOG_WARNING, "Failed to power off: %s", error->message); return; } }
/** * main: **/ int main (int argc, char *argv[]) { PkOfflineAction action = PK_OFFLINE_ACTION_UNKNOWN; gint retval; _cleanup_error_free_ GError *error = NULL; _cleanup_main_loop_unref_ GMainLoop *loop = NULL; _cleanup_object_unref_ GFile *file = NULL; _cleanup_object_unref_ PkProgressBar *progressbar = NULL; _cleanup_object_unref_ PkResults *results = NULL; _cleanup_object_unref_ PkTask *task = NULL; _cleanup_strv_free_ gchar **package_ids = NULL; #if (GLIB_MAJOR_VERSION == 2 && GLIB_MINOR_VERSION < 35) g_type_init (); #endif /* ensure root user */ if (getuid () != 0 || geteuid () != 0) { retval = EXIT_FAILURE; g_print ("This program can only be used using root\n"); sd_journal_print (LOG_WARNING, "not called with the root user"); goto out; } /* get the action, and then delete the file */ action = pk_offline_update_get_action (); g_unlink (PK_OFFLINE_ACTION_FILENAME); /* always do this first to avoid a loop if this tool segfaults */ g_unlink (PK_OFFLINE_TRIGGER_FILENAME); /* do stuff on ctrl-c */ g_unix_signal_add_full (G_PRIORITY_DEFAULT, SIGINT, pk_offline_update_sigint_cb, NULL, NULL); /* get the list of packages to update */ package_ids = pk_offline_get_prepared_ids (&error); if (package_ids == NULL) { retval = EXIT_FAILURE; sd_journal_print (LOG_WARNING, "failed to read %s: %s", PK_OFFLINE_PREPARED_FILENAME, error->message); goto out; } /* use a progress bar when the user presses <esc> in plymouth */ progressbar = pk_progress_bar_new (); pk_progress_bar_set_size (progressbar, 25); pk_progress_bar_set_padding (progressbar, 30); /* just update the system */ task = pk_task_new (); pk_client_set_interactive (PK_CLIENT (task), FALSE); pk_offline_update_set_plymouth_mode ("updates"); /* TRANSLATORS: we've started doing offline updates */ pk_offline_update_set_plymouth_msg (_("Installing updates, this could take a while...")); pk_offline_update_write_dummy_results (package_ids); results = pk_client_update_packages (PK_CLIENT (task), 0, package_ids, NULL, /* GCancellable */ pk_offline_update_progress_cb, progressbar, /* user_data */ &error); if (results == NULL) { retval = EXIT_FAILURE; pk_offline_update_write_error (error); sd_journal_print (LOG_WARNING, "failed to update system: %s", error->message); goto out; } pk_progress_bar_end (progressbar); pk_offline_update_write_results (results); /* delete prepared-update file if it's not already been done by the * pk-plugin-systemd-update daemon plugin */ if (!pk_offline_auth_invalidate (&error)) { retval = EXIT_FAILURE; sd_journal_print (LOG_WARNING, "failed to delete %s: %s", PK_OFFLINE_PREPARED_FILENAME, error->message); goto out; } retval = EXIT_SUCCESS; out: /* if we failed, we pause to show any error on the screen */ if (retval != EXIT_SUCCESS) { loop = g_main_loop_new (NULL, FALSE); g_timeout_add_seconds (10, pk_offline_update_loop_quit_cb, loop); g_main_loop_run (loop); } /* we have to manually either restart or shutdown */ if (action == PK_OFFLINE_ACTION_REBOOT) pk_offline_update_reboot (); else if (action == PK_OFFLINE_ACTION_POWER_OFF) pk_offline_update_power_off (); return retval; }
/** * pk_offline_update_progress_cb: **/ static void pk_offline_update_progress_cb (PkProgress *progress, PkProgressType type, gpointer user_data) { PkInfoEnum info; PkProgressBar *progressbar = PK_PROGRESS_BAR (user_data); PkStatusEnum status; gint percentage; _cleanup_free_ gchar *msg = NULL; _cleanup_object_unref_ PkPackage *pkg = NULL; switch (type) { case PK_PROGRESS_TYPE_ROLE: sd_journal_print (LOG_INFO, "assigned role"); pk_progress_bar_start (progressbar, "Updating system"); break; case PK_PROGRESS_TYPE_PACKAGE: g_object_get (progress, "package", &pkg, NULL); info = pk_package_get_info (pkg); if (info == PK_INFO_ENUM_UPDATING) { msg = g_strdup_printf ("Updating %s", pk_package_get_name (pkg)); pk_progress_bar_start (progressbar, msg); } else if (info == PK_INFO_ENUM_INSTALLING) { msg = g_strdup_printf ("Installing %s", pk_package_get_name (pkg)); pk_progress_bar_start (progressbar, msg); } else if (info == PK_INFO_ENUM_REMOVING) { msg = g_strdup_printf ("Removing %s", pk_package_get_name (pkg)); pk_progress_bar_start (progressbar, msg); } sd_journal_print (LOG_INFO, "package %s\t%s-%s.%s (%s)", pk_info_enum_to_string (info), pk_package_get_name (pkg), pk_package_get_version (pkg), pk_package_get_arch (pkg), pk_package_get_data (pkg)); break; case PK_PROGRESS_TYPE_PERCENTAGE: g_object_get (progress, "percentage", &percentage, NULL); if (percentage < 0) return; sd_journal_print (LOG_INFO, "percentage %i%%", percentage); /* TRANSLATORS: this is the message we send plymouth to * advise of the new percentage completion */ msg = g_strdup_printf ("%s - %i%%", _("Installing Updates"), percentage); if (percentage > 10) pk_offline_update_set_plymouth_msg (msg); /* print on terminal */ pk_progress_bar_set_percentage (progressbar, percentage); /* update plymouth */ pk_offline_update_set_plymouth_percentage (percentage); break; case PK_PROGRESS_TYPE_STATUS: g_object_get (progress, "status", &status, NULL); sd_journal_print (LOG_INFO, "status %s", pk_status_enum_to_string (status)); default: break; } }