1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
* save-session.c - Small program to talk to session manager.
Copyright (C) 1998 Tom Tromey
Copyright (C) 2008 Red Hat, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#include <config.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <glib.h>
#include <glib/gi18n.h>
#include <dbus/dbus-glib.h>
#include <dbus/dbus-glib-lowlevel.h>
#define GSM_SERVICE_DBUS "org.gnome.SessionManager"
#define GSM_PATH_DBUS "/org/gnome/SessionManager"
#define GSM_INTERFACE_DBUS "org.gnome.SessionManager"
enum {
GSM_LOGOUT_MODE_NORMAL = 0,
GSM_LOGOUT_MODE_NO_CONFIRMATION,
GSM_LOGOUT_MODE_FORCE
};
static gboolean opt_logout = FALSE;
static gboolean opt_power_off = FALSE;
static gboolean opt_reboot = FALSE;
static gboolean opt_no_prompt = FALSE;
static gboolean opt_force = FALSE;
static GOptionEntry options[] = {
{"logout", '\0', 0, G_OPTION_ARG_NONE, &opt_logout, N_("Log out"), NULL},
{"power-off", '\0', 0, G_OPTION_ARG_NONE, &opt_power_off, N_("Power off"), NULL},
{"reboot", '\0', 0, G_OPTION_ARG_NONE, &opt_reboot, N_("Reboot"), NULL},
{"force", '\0', 0, G_OPTION_ARG_NONE, &opt_force, N_("Ignoring any existing inhibitors"), NULL},
{"no-prompt", '\0', 0, G_OPTION_ARG_NONE, &opt_no_prompt, N_("Don't prompt for user confirmation"), NULL},
{NULL}
};
static void
display_error (const char *message)
{
g_printerr ("%s\n", message);
}
static DBusGConnection *
get_session_bus (void)
{
DBusGConnection *bus;
GError *error = NULL;
bus = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
if (bus == NULL) {
g_warning ("Couldn't connect to session bus: %s", error->message);
g_error_free (error);
}
return bus;
}
static DBusGProxy *
get_sm_proxy (void)
{
DBusGConnection *connection;
DBusGProxy *sm_proxy;
connection = get_session_bus ();
if (connection == NULL) {
display_error (_("Could not connect to the session manager"));
return NULL;
}
sm_proxy = dbus_g_proxy_new_for_name (connection,
GSM_SERVICE_DBUS,
GSM_PATH_DBUS,
GSM_INTERFACE_DBUS);
if (sm_proxy == NULL) {
display_error (_("Could not connect to the session manager"));
return NULL;
}
return sm_proxy;
}
static void
do_logout (unsigned int mode)
{
DBusGProxy *sm_proxy;
GError *error;
gboolean res;
sm_proxy = get_sm_proxy ();
if (sm_proxy == NULL) {
return;
}
error = NULL;
res = dbus_g_proxy_call (sm_proxy,
"Logout",
&error,
G_TYPE_UINT, mode,
G_TYPE_INVALID,
G_TYPE_INVALID);
if (!res) {
if (error != NULL) {
g_warning ("Failed to call logout: %s",
error->message);
g_error_free (error);
} else {
g_warning ("Failed to call logout");
}
}
g_clear_object (&sm_proxy);
}
static void
do_power_off (const char *action)
{
DBusGProxy *sm_proxy;
GError *error;
gboolean res;
sm_proxy = get_sm_proxy ();
if (sm_proxy == NULL) {
return;
}
error = NULL;
res = dbus_g_proxy_call (sm_proxy,
action,
&error,
G_TYPE_INVALID,
G_TYPE_INVALID);
if (!res) {
if (error != NULL) {
g_warning ("Failed to call %s: %s",
action, error->message);
g_error_free (error);
} else {
g_warning ("Failed to call %s", action);
}
}
g_clear_object (&sm_proxy);
}
int
main (int argc, char *argv[])
{
GError *error;
int conflicting_options;
GOptionContext *ctx;
/* Initialize the i18n stuff */
bindtextdomain (GETTEXT_PACKAGE, LOCALE_DIR);
bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
textdomain (GETTEXT_PACKAGE);
error = NULL;
ctx = g_option_context_new ("");
g_option_context_add_main_entries (ctx, options, GETTEXT_PACKAGE);
if (! g_option_context_parse (ctx, &argc, &argv, &error)) {
g_warning ("Unable to start: %s", error->message);
g_error_free (error);
exit (1);
}
g_option_context_free (ctx);
conflicting_options = 0;
if (opt_logout)
conflicting_options++;
if (opt_power_off)
conflicting_options++;
if (opt_reboot)
conflicting_options++;
if (conflicting_options > 1)
display_error (_("Program called with conflicting options"));
if (opt_power_off) {
do_power_off ("Shutdown");
} else if (opt_reboot) {
do_power_off ("Reboot");
} else {
/* default to logout */
if (opt_force)
do_logout (GSM_LOGOUT_MODE_FORCE);
else if (opt_no_prompt)
do_logout (GSM_LOGOUT_MODE_NO_CONFIRMATION);
else
do_logout (GSM_LOGOUT_MODE_NORMAL);
}
return 0;
}
|