| 1 |
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- |
| 2 |
* |
| 3 |
* Copyright (C) 2007-2010 David Zeuthen <[email protected]> |
| 4 |
* |
| 5 |
* This program is free software; you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation; either version 2 of the License, or |
| 8 |
* (at your option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License |
| 16 |
* along with this program; if not, write to the Free Software |
| 17 |
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 |
* |
| 19 |
*/ |
| 20 |
|
| 21 |
#include "config.h" |
| 22 |
#include <glib/gi18n.h> |
| 23 |
#include <gio/gunixfdlist.h> |
| 24 |
|
| 25 |
#include <stdio.h> |
| 26 |
#include <stdlib.h> |
| 27 |
#include <udisks/udisks.h> |
| 28 |
#include <string.h> |
| 29 |
|
| 30 |
#include <sys/types.h> |
| 31 |
#include <sys/stat.h> |
| 32 |
#include <fcntl.h> |
| 33 |
#include <termios.h> |
| 34 |
#include <unistd.h> |
| 35 |
|
| 36 |
#include <locale.h> |
| 37 |
|
| 38 |
#include <polkit/polkit.h> |
| 39 |
#define POLKIT_AGENT_I_KNOW_API_IS_SUBJECT_TO_CHANGE |
| 40 |
#include <polkitagent/polkitagent.h> |
| 41 |
|
| 42 |
static UDisksClient *client = NULL; |
| 43 |
static GMainLoop *loop = NULL; |
| 44 |
|
| 45 |
/* Uncomment to get debug traces in /tmp/udisksctl-completion-debug.txt - use tail(1) to |
| 46 |
* inspect this file |
| 47 |
*/ |
| 48 |
/* #define COMPLETION_DEBUG */ |
| 49 |
|
| 50 |
/* ---------------------------------------------------------------------------------------------------- */ |
| 51 |
|
| 52 |
G_GNUC_UNUSED static void completion_debug (const gchar *format, ...); |
| 53 |
|
| 54 |
static void remove_arg (gint num, gint *argc, gchar **argv[]); |
| 55 |
static void modify_argv0_for_command (gint *argc, gchar **argv[], const gchar *command); |
| 56 |
|
| 57 |
|
| 58 |
/* ---------------------------------------------------------------------------------------------------- */ |
| 59 |
|
| 60 |
static PolkitAgentListener *local_polkit_agent = NULL; |
| 61 |
static gpointer local_agent_handle = NULL; |
| 62 |
|
| 63 |
static gboolean |
| 64 |
setup_local_polkit_agent (void) |
| 65 |
{ |
| 66 |
gboolean ret; |
| 67 |
GError *error; |
| 68 |
PolkitSubject *subject; |
| 69 |
|
| 70 |
ret = FALSE; |
| 71 |
subject = NULL; |
| 72 |
|
| 73 |
if (local_polkit_agent != NULL) |
| 74 |
goto out; |
| 75 |
|
| 76 |
subject = polkit_unix_process_new_for_owner (getpid (), 0, getuid ()); |
| 77 |
|
| 78 |
error = NULL; |
| 79 |
/* this will fail if we can't find a controlling terminal */ |
| 80 |
local_polkit_agent = polkit_agent_text_listener_new (NULL, &error); |
| 81 |
if (local_polkit_agent == NULL) |
| 82 |
{ |
| 83 |
g_printerr ("Error creating textual authentication agent: %s (%s, %d)\n", |
| 84 |
error->message, |
| 85 |
g_quark_to_string (error->domain), |
| 86 |
error->code); |
| 87 |
g_clear_error (&error); |
| 88 |
goto out; |
| 89 |
} |
| 90 |
local_agent_handle = polkit_agent_listener_register (local_polkit_agent, |
| 91 |
POLKIT_AGENT_REGISTER_FLAGS_RUN_IN_THREAD, |
| 92 |
subject, |
| 93 |
NULL, /* object_path */ |
| 94 |
NULL, /* GCancellable */ |
| 95 |
&error); |
| 96 |
if (local_agent_handle == NULL) |
| 97 |
{ |
| 98 |
g_printerr ("Error registering local authentication agent: %s (%s, %d)\n", |
| 99 |
error->message, |
| 100 |
g_quark_to_string (error->domain), |
| 101 |
error->code); |
| 102 |
g_clear_error (&error); |
| 103 |
goto out; |
| 104 |
} |
| 105 |
|
| 106 |
ret = TRUE; |
| 107 |
|
| 108 |
out: |
| 109 |
if (subject != NULL) |
| 110 |
g_object_unref (subject); |
| 111 |
return ret; |
| 112 |
} |
| 113 |
|
| 114 |
static void |
| 115 |
shutdown_local_polkit_agent (void) |
| 116 |
{ |
| 117 |
if (local_agent_handle != NULL) |
| 118 |
polkit_agent_listener_unregister (local_agent_handle); |
| 119 |
if (local_polkit_agent != NULL) |
| 120 |
g_object_unref (local_polkit_agent); |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
/* ---------------------------------------------------------------------------------------------------- */ |
| 125 |
|
| 126 |
typedef enum |
| 127 |
{ |
| 128 |
_COLOR_RESET, |
| 129 |
_COLOR_BOLD_ON, |
| 130 |
_COLOR_INVERSE_ON, |
| 131 |
_COLOR_BOLD_OFF, |
| 132 |
_COLOR_FG_BLACK, |
| 133 |
_COLOR_FG_RED, |
| 134 |
_COLOR_FG_GREEN, |
| 135 |
_COLOR_FG_YELLOW, |
| 136 |
_COLOR_FG_BLUE, |
| 137 |
_COLOR_FG_MAGENTA, |
| 138 |
_COLOR_FG_CYAN, |
| 139 |
_COLOR_FG_WHITE, |
| 140 |
_COLOR_BG_RED, |
| 141 |
_COLOR_BG_GREEN, |
| 142 |
_COLOR_BG_YELLOW, |
| 143 |
_COLOR_BG_BLUE, |
| 144 |
_COLOR_BG_MAGENTA, |
| 145 |
_COLOR_BG_CYAN, |
| 146 |
_COLOR_BG_WHITE |
| 147 |
} _Color; |
| 148 |
|
| 149 |
static gboolean _color_stdin_is_tty = FALSE; |
| 150 |
static gboolean _color_initialized = FALSE; |
| 151 |
static FILE *_color_pager_out = NULL; |
| 152 |
|
| 153 |
static void |
| 154 |
_color_init (void) |
| 155 |
{ |
| 156 |
if (_color_initialized) |
| 157 |
return; |
| 158 |
_color_initialized = TRUE; |
| 159 |
_color_stdin_is_tty = (isatty (STDIN_FILENO) != 0 && isatty (STDOUT_FILENO) != 0); |
| 160 |
} |
| 161 |
|
| 162 |
static void |
| 163 |
_color_shutdown (void) |
| 164 |
{ |
| 165 |
if (!_color_initialized) |
| 166 |
return; |
| 167 |
_color_initialized = FALSE; |
| 168 |
if (_color_pager_out != NULL) |
| 169 |
pclose (_color_pager_out); |
| 170 |
} |
| 171 |
|
| 172 |
static const gchar * |
| 173 |
_color_get (_Color color) |
| 174 |
{ |
| 175 |
const gchar *str; |
| 176 |
|
| 177 |
_color_init (); |
| 178 |
|
| 179 |
if (!_color_stdin_is_tty) |
| 180 |
return ""; |
| 181 |
|
| 182 |
str = NULL; |
| 183 |
switch (color) |
| 184 |
{ |
| 185 |
case _COLOR_RESET: str="\x1b[0m"; break; |
| 186 |
case _COLOR_BOLD_ON: str="\x1b[1m"; break; |
| 187 |
case _COLOR_INVERSE_ON: str="\x1b[7m"; break; |
| 188 |
case _COLOR_BOLD_OFF: str="\x1b[22m"; break; |
| 189 |
case _COLOR_FG_BLACK: str="\x1b[30m"; break; |
| 190 |
case _COLOR_FG_RED: str="\x1b[31m"; break; |
| 191 |
case _COLOR_FG_GREEN: str="\x1b[32m"; break; |
| 192 |
case _COLOR_FG_YELLOW: str="\x1b[33m"; break; |
| 193 |
case _COLOR_FG_BLUE: str="\x1b[34m"; break; |
| 194 |
case _COLOR_FG_MAGENTA: str="\x1b[35m"; break; |
| 195 |
case _COLOR_FG_CYAN: str="\x1b[36m"; break; |
| 196 |
case _COLOR_FG_WHITE: str="\x1b[37m"; break; |
| 197 |
case _COLOR_BG_RED: str="\x1b[41m"; break; |
| 198 |
case _COLOR_BG_GREEN: str="\x1b[42m"; break; |
| 199 |
case _COLOR_BG_YELLOW: str="\x1b[43m"; break; |
| 200 |
case _COLOR_BG_BLUE: str="\x1b[44m"; break; |
| 201 |
case _COLOR_BG_MAGENTA: str="\x1b[45m"; break; |
| 202 |
case _COLOR_BG_CYAN: str="\x1b[46m"; break; |
| 203 |
case _COLOR_BG_WHITE: str="\x1b[47m"; break; |
| 204 |
default: |
| 205 |
g_assert_not_reached (); |
| 206 |
break; |
| 207 |
} |
| 208 |
return str; |
| 209 |
} |
| 210 |
|
| 211 |
static void |
| 212 |
_color_run_pager (void) |
| 213 |
{ |
| 214 |
const gchar *pager_program; |
| 215 |
|
| 216 |
_color_init (); |
| 217 |
if (!_color_stdin_is_tty) |
| 218 |
goto out; |
| 219 |
|
| 220 |
pager_program = g_getenv ("PAGER"); |
| 221 |
if (pager_program == NULL) |
| 222 |
pager_program = "less -R"; |
| 223 |
|
| 224 |
_color_pager_out = popen (pager_program, "w"); |
| 225 |
if (_color_pager_out == NULL) |
| 226 |
{ |
| 227 |
g_printerr ("Error spawning pager `%s': %m\n", pager_program); |
| 228 |
} |
| 229 |
else |
| 230 |
{ |
| 231 |
fflush (stdout); |
| 232 |
dup2 (fileno(_color_pager_out), fileno(stdout)); |
| 233 |
} |
| 234 |
|
| 235 |
out: |
| 236 |
; |
| 237 |
} |
| 238 |
|
| 239 |
/* ---------------------------------------------------------------------------------------------------- */ |
| 240 |
|
| 241 |
static gchar * |
| 242 |
variant_to_string_with_indent (GVariant *value, |
| 243 |
guint indent) |
| 244 |
{ |
| 245 |
gchar *value_str; |
| 246 |
|
| 247 |
if (g_variant_is_of_type (value, G_VARIANT_TYPE_STRING)) |
| 248 |
{ |
| 249 |
value_str = g_variant_dup_string (value, NULL); |
| 250 |
} |
| 251 |
else if (g_variant_is_of_type (value, G_VARIANT_TYPE_BYTESTRING)) |
| 252 |
{ |
| 253 |
value_str = g_variant_dup_bytestring (value, NULL); |
| 254 |
} |
| 255 |
else if (g_variant_is_of_type (value, G_VARIANT_TYPE_STRING_ARRAY) || |
| 256 |
g_variant_is_of_type (value, G_VARIANT_TYPE_BYTESTRING_ARRAY) || |
| 257 |
g_variant_is_of_type (value, G_VARIANT_TYPE_OBJECT_PATH_ARRAY)) |
| 258 |
{ |
| 259 |
const gchar **strv; |
| 260 |
guint m; |
| 261 |
GString *str; |
| 262 |
if (g_variant_is_of_type (value, G_VARIANT_TYPE_BYTESTRING_ARRAY)) |
| 263 |
strv = g_variant_get_bytestring_array (value, NULL); |
| 264 |
else if (g_variant_is_of_type (value, G_VARIANT_TYPE_OBJECT_PATH_ARRAY)) |
| 265 |
strv = g_variant_get_objv (value, NULL); |
| 266 |
else |
| 267 |
strv = g_variant_get_strv (value, NULL); |
| 268 |
str = g_string_new (NULL); |
| 269 |
for (m = 0; strv != NULL && strv[m] != NULL; m++) |
| 270 |
{ |
| 271 |
if (m > 0) |
| 272 |
g_string_append_printf (str, "\n%*s", |
| 273 |
indent, ""); |
| 274 |
g_string_append (str, strv[m]); |
| 275 |
} |
| 276 |
value_str = g_string_free (str, FALSE); |
| 277 |
g_free (strv); |
| 278 |
} |
| 279 |
else |
| 280 |
{ |
| 281 |
value_str = g_variant_print (value, FALSE); |
| 282 |
} |
| 283 |
return value_str; |
| 284 |
} |
| 285 |
|
| 286 |
static gint |
| 287 |
if_proxy_cmp (GDBusProxy *a, |
| 288 |
GDBusProxy *b) |
| 289 |
{ |
| 290 |
return g_strcmp0 (g_dbus_proxy_get_interface_name (a), g_dbus_proxy_get_interface_name (b)); |
| 291 |
} |
| 292 |
|
| 293 |
static void |
| 294 |
print_interface_properties (GDBusProxy *proxy, |
| 295 |
guint indent) |
| 296 |
{ |
| 297 |
gchar **cached_properties; |
| 298 |
guint n; |
| 299 |
guint value_column; |
| 300 |
guint max_property_name_len; |
| 301 |
|
| 302 |
/* note: this is guaranteed to be sorted */ |
| 303 |
cached_properties = g_dbus_proxy_get_cached_property_names (proxy); |
| 304 |
|
| 305 |
max_property_name_len = 0; |
| 306 |
for (n = 0; cached_properties != NULL && cached_properties[n] != NULL; n++) |
| 307 |
{ |
| 308 |
const gchar *property_name = cached_properties[n]; |
| 309 |
guint property_name_len; |
| 310 |
property_name_len = strlen (property_name); |
| 311 |
if (max_property_name_len < property_name_len) |
| 312 |
max_property_name_len = property_name_len; |
| 313 |
} |
| 314 |
|
| 315 |
value_column = ((max_property_name_len + 7) / 8) * 8 + 8; |
| 316 |
if (value_column < 24) |
| 317 |
value_column = 24; |
| 318 |
else if (value_column > 64) |
| 319 |
value_column = 64; |
| 320 |
|
| 321 |
for (n = 0; cached_properties != NULL && cached_properties[n] != NULL; n++) |
| 322 |
{ |
| 323 |
const gchar *property_name = cached_properties[n]; |
| 324 |
GVariant *value; |
| 325 |
gchar *value_str; |
| 326 |
guint rightmost; |
| 327 |
gint value_indent; |
| 328 |
|
| 329 |
rightmost = indent + strlen (property_name) + 2; |
| 330 |
value_indent = value_column - rightmost; |
| 331 |
if (value_indent < 0) |
| 332 |
value_indent = 0; |
| 333 |
|
| 334 |
value = g_dbus_proxy_get_cached_property (proxy, property_name); |
| 335 |
|
| 336 |
value_str = variant_to_string_with_indent (value, indent + strlen (property_name) + 2 + value_indent); |
| 337 |
|
| 338 |
g_print ("%*s%s%s:%s %*s%s\n", |
| 339 |
indent, "", |
| 340 |
_color_get (_COLOR_FG_WHITE), property_name, _color_get (_COLOR_RESET), |
| 341 |
value_indent, "", |
| 342 |
value_str); |
| 343 |
|
| 344 |
g_free (value_str); |
| 345 |
g_variant_unref (value); |
| 346 |
} |
| 347 |
g_strfreev (cached_properties); |
| 348 |
} |
| 349 |
|
| 350 |
static void |
| 351 |
print_object (UDisksObject *object, |
| 352 |
guint indent) |
| 353 |
{ |
| 354 |
GList *interface_proxies; |
| 355 |
GList *l; |
| 356 |
|
| 357 |
g_return_if_fail (G_IS_DBUS_OBJECT (object)); |
| 358 |
|
| 359 |
interface_proxies = g_dbus_object_get_interfaces (G_DBUS_OBJECT (object)); |
| 360 |
|
| 361 |
/* We want to print the interfaces in order */ |
| 362 |
interface_proxies = g_list_sort (interface_proxies, (GCompareFunc) if_proxy_cmp); |
| 363 |
|
| 364 |
for (l = interface_proxies; l != NULL; l = l->next) |
| 365 |
{ |
| 366 |
GDBusProxy *iproxy = G_DBUS_PROXY (l->data); |
| 367 |
g_print ("%*s%s%s%s:%s\n", |
| 368 |
indent, "", |
| 369 |
_color_get (_COLOR_BOLD_ON), _color_get (_COLOR_FG_MAGENTA), g_dbus_proxy_get_interface_name (iproxy), _color_get (_COLOR_RESET)); |
| 370 |
print_interface_properties (iproxy, indent + 2); |
| 371 |
} |
| 372 |
g_list_free_full (interface_proxies, g_object_unref); |
| 373 |
} |
| 374 |
|
| 375 |
/* ---------------------------------------------------------------------------------------------------- */ |
| 376 |
|
| 377 |
static UDisksObject * |
| 378 |
lookup_object_by_path (const gchar *path) |
| 379 |
{ |
| 380 |
GDBusObject *ret; |
| 381 |
gchar *s; |
| 382 |
|
| 383 |
s = g_strdup_printf ("/org/freedesktop/UDisks2/%s", path); |
| 384 |
ret = g_dbus_object_manager_get_object (udisks_client_get_object_manager (client), s); |
| 385 |
g_free (s); |
| 386 |
|
| 387 |
if (ret == NULL) |
| 388 |
return NULL; |
| 389 |
else |
| 390 |
return UDISKS_OBJECT (ret); |
| 391 |
} |
| 392 |
|
| 393 |
static UDisksObject * |
| 394 |
lookup_object_by_device (const gchar *device) |
| 395 |
{ |
| 396 |
UDisksObject *ret; |
| 397 |
GList *objects; |
| 398 |
GList *l; |
| 399 |
|
| 400 |
ret = NULL; |
| 401 |
|
| 402 |
objects = g_dbus_object_manager_get_objects (udisks_client_get_object_manager (client)); |
| 403 |
for (l = objects; l != NULL; l = l->next) |
| 404 |
{ |
| 405 |
UDisksObject *object = UDISKS_OBJECT (l->data); |
| 406 |
UDisksBlock *block; |
| 407 |
|
| 408 |
block = udisks_object_peek_block (object); |
| 409 |
if (block != NULL) |
| 410 |
{ |
| 411 |
const gchar * const *symlinks; |
| 412 |
guint n; |
| 413 |
|
| 414 |
if (g_strcmp0 (udisks_block_get_device (block), device) == 0) |
| 415 |
{ |
| 416 |
ret = g_object_ref (object); |
| 417 |
goto out; |
| 418 |
} |
| 419 |
|
| 420 |
symlinks = udisks_block_get_symlinks (block); |
| 421 |
for (n = 0; symlinks != NULL && symlinks[n] != NULL; n++) |
| 422 |
{ |
| 423 |
if (g_strcmp0 (symlinks[n], device) == 0) |
| 424 |
{ |
| 425 |
ret = g_object_ref (object); |
| 426 |
goto out; |
| 427 |
} |
| 428 |
} |
| 429 |
} |
| 430 |
} |
| 431 |
|
| 432 |
out: |
| 433 |
g_list_free_full (objects, g_object_unref); |
| 434 |
|
| 435 |
return ret; |
| 436 |
} |
| 437 |
|
| 438 |
static UDisksObject * |
| 439 |
lookup_object_by_drive (const gchar *drive) |
| 440 |
{ |
| 441 |
UDisksObject *ret; |
| 442 |
GList *objects; |
| 443 |
GList *l; |
| 444 |
gchar *full_drive_object_path; |
| 445 |
|
| 446 |
ret = NULL; |
| 447 |
|
| 448 |
full_drive_object_path = g_strdup_printf ("/org/freedesktop/UDisks2/drives/%s", drive); |
| 449 |
|
| 450 |
objects = g_dbus_object_manager_get_objects (udisks_client_get_object_manager (client)); |
| 451 |
for (l = objects; l != NULL; l = l->next) |
| 452 |
{ |
| 453 |
UDisksObject *object = UDISKS_OBJECT (l->data); |
| 454 |
UDisksDrive *drive_iface; |
| 455 |
|
| 456 |
if (g_strcmp0 (g_dbus_object_get_object_path (G_DBUS_OBJECT (object)), full_drive_object_path) != 0) |
| 457 |
continue; |
| 458 |
|
| 459 |
drive_iface = udisks_object_peek_drive (object); |
| 460 |
if (drive_iface != NULL) |
| 461 |
{ |
| 462 |
ret = g_object_ref (object); |
| 463 |
goto out; |
| 464 |
} |
| 465 |
} |
| 466 |
|
| 467 |
out: |
| 468 |
g_list_free_full (objects, g_object_unref); |
| 469 |
g_free (full_drive_object_path); |
| 470 |
|
| 471 |
return ret; |
| 472 |
} |
| 473 |
|
| 474 |
/* ---------------------------------------------------------------------------------------------------- */ |
| 475 |
|
| 476 |
static gchar *opt_mount_unmount_object_path = NULL; |
| 477 |
static gchar *opt_mount_unmount_device = NULL; |
| 478 |
static gchar *opt_mount_options = NULL; |
| 479 |
static gchar *opt_mount_filesystem_type = NULL; |
| 480 |
static gboolean opt_unmount_force = FALSE; |
| 481 |
static gboolean opt_mount_unmount_no_user_interaction = FALSE; |
| 482 |
|
| 483 |
static const GOptionEntry command_mount_entries[] = |
| 484 |
{ |
| 485 |
{ |
| 486 |
"object-path", |
| 487 |
'p', |
| 488 |
0, |
| 489 |
G_OPTION_ARG_STRING, |
| 490 |
&opt_mount_unmount_object_path, |
| 491 |
"Object to mount", |
| 492 |
NULL |
| 493 |
}, |
| 494 |
{ |
| 495 |
"block-device", |
| 496 |
'b', |
| 497 |
0, |
| 498 |
G_OPTION_ARG_STRING, |
| 499 |
&opt_mount_unmount_device, |
| 500 |
"Block device to mount", |
| 501 |
NULL |
| 502 |
}, |
| 503 |
{ |
| 504 |
"filesystem-type", |
| 505 |
't', |
| 506 |
0, |
| 507 |
G_OPTION_ARG_STRING, |
| 508 |
&opt_mount_filesystem_type, |
| 509 |
"Filesystem type to use", |
| 510 |
NULL |
| 511 |
}, |
| 512 |
{ |
| 513 |
"options", |
| 514 |
'o', |
| 515 |
0, |
| 516 |
G_OPTION_ARG_STRING, |
| 517 |
&opt_mount_options, |
| 518 |
"Mount options", |
| 519 |
NULL |
| 520 |
}, |
| 521 |
{ |
| 522 |
"no-user-interaction", |
| 523 |
0, /* no short option */ |
| 524 |
0, |
| 525 |
G_OPTION_ARG_NONE, |
| 526 |
&opt_mount_unmount_no_user_interaction, |
| 527 |
"Do not authenticate the user if needed", |
| 528 |
NULL |
| 529 |
}, |
| 530 |
{ |
| 531 |
NULL |
| 532 |
} |
| 533 |
}; |
| 534 |
|
| 535 |
static const GOptionEntry command_unmount_entries[] = |
| 536 |
{ |
| 537 |
{ |
| 538 |
"object-path", |
| 539 |
'p', |
| 540 |
0, |
| 541 |
G_OPTION_ARG_STRING, |
| 542 |
&opt_mount_unmount_object_path, |
| 543 |
"Object to unmount", |
| 544 |
NULL |
| 545 |
}, |
| 546 |
{ |
| 547 |
"block-device", |
| 548 |
'b', |
| 549 |
0, |
| 550 |
G_OPTION_ARG_STRING, |
| 551 |
&opt_mount_unmount_device, |
| 552 |
"Block device to unmount", |
| 553 |
NULL |
| 554 |
}, |
| 555 |
{ |
| 556 |
"force", |
| 557 |
'f', |
| 558 |
0, |
| 559 |
G_OPTION_ARG_NONE, |
| 560 |
&opt_unmount_force, |
| 561 |
"Force/lazy unmount", |
| 562 |
NULL |
| 563 |
}, |
| 564 |
{ |
| 565 |
"no-user-interaction", |
| 566 |
0, /* no short option */ |
| 567 |
0, |
| 568 |
G_OPTION_ARG_NONE, |
| 569 |
&opt_mount_unmount_no_user_interaction, |
| 570 |
"Do not authenticate the user if needed", |
| 571 |
NULL |
| 572 |
}, |
| 573 |
{ |
| 574 |
NULL |
| 575 |
} |
| 576 |
}; |
| 577 |
|
| 578 |
static void |
| 579 |
list_options (const GOptionEntry *entries) |
| 580 |
{ |
| 581 |
const GOptionEntry* current; |
| 582 |
for (current = entries; current->long_name; current++) |
| 583 |
{ |
| 584 |
if ((current->arg != G_OPTION_ARG_STRING && current->arg != G_OPTION_ARG_FILENAME) || |
| 585 |
(current->arg_data != NULL && !*(gchar**)current->arg_data)) |
| 586 |
{ |
| 587 |
g_print ("--%s \n", current->long_name); |
| 588 |
} |
| 589 |
} |
| 590 |
} |
| 591 |
|
| 592 |
static gint |
| 593 |
handle_command_mount_unmount (gint *argc, |
| 594 |
gchar **argv[], |
| 595 |
gboolean request_completion, |
| 596 |
const gchar *completion_cur, |
| 597 |
const gchar *completion_prev, |
| 598 |
gboolean is_mount) |
| 599 |
{ |
| 600 |
gint ret; |
| 601 |
GOptionContext *o; |
| 602 |
gchar *s; |
| 603 |
gboolean complete_objects; |
| 604 |
gboolean complete_devices; |
| 605 |
GList *l; |
| 606 |
GList *objects; |
| 607 |
UDisksObject *object; |
| 608 |
UDisksBlock *block; |
| 609 |
UDisksFilesystem *filesystem; |
| 610 |
guint n; |
| 611 |
const gchar * const *mount_points; |
| 612 |
GVariant *options; |
| 613 |
GVariantBuilder builder; |
| 614 |
|
| 615 |
ret = 1; |
| 616 |
opt_mount_unmount_object_path = NULL; |
| 617 |
opt_mount_unmount_device = NULL; |
| 618 |
opt_mount_options = NULL; |
| 619 |
opt_mount_filesystem_type = NULL; |
| 620 |
opt_unmount_force = FALSE; |
| 621 |
object = NULL; |
| 622 |
options = NULL; |
| 623 |
|
| 624 |
if (is_mount) |
| 625 |
modify_argv0_for_command (argc, argv, "mount"); |
| 626 |
else |
| 627 |
modify_argv0_for_command (argc, argv, "unmount"); |
| 628 |
|
| 629 |
o = g_option_context_new (NULL); |
| 630 |
if (request_completion) |
| 631 |
g_option_context_set_ignore_unknown_options (o, TRUE); |
| 632 |
g_option_context_set_help_enabled (o, FALSE); |
| 633 |
if (is_mount) |
| 634 |
g_option_context_set_summary (o, "Mount a filesystem."); |
| 635 |
else |
| 636 |
g_option_context_set_summary (o, "Unmount a filesystem."); |
| 637 |
g_option_context_add_main_entries (o, |
| 638 |
is_mount ? command_mount_entries : command_unmount_entries, |
| 639 |
NULL /* GETTEXT_PACKAGE*/); |
| 640 |
|
| 641 |
complete_objects = FALSE; |
| 642 |
if (request_completion && (g_strcmp0 (completion_prev, "--object-path") == 0 || g_strcmp0 (completion_prev, "-p") == 0 || |
| 643 |
g_strcmp0 (completion_cur, "--object-path") == 0 || g_strcmp0 (completion_cur, "-p") == 0)) |
| 644 |
{ |
| 645 |
complete_objects = TRUE; |
| 646 |
remove_arg ((*argc) - 1, argc, argv); |
| 647 |
} |
| 648 |
|
| 649 |
complete_devices = FALSE; |
| 650 |
if (request_completion && (g_strcmp0 (completion_prev, "--block-device") == 0 || g_strcmp0 (completion_prev, "-b") == 0 || |
| 651 |
g_strcmp0 (completion_cur, "--block-device") == 0 || g_strcmp0 (completion_cur, "-b") == 0)) |
| 652 |
{ |
| 653 |
complete_devices = TRUE; |
| 654 |
remove_arg ((*argc) - 1, argc, argv); |
| 655 |
} |
| 656 |
|
| 657 |
if (!g_option_context_parse (o, argc, argv, NULL)) |
| 658 |
{ |
| 659 |
if (!request_completion) |
| 660 |
{ |
| 661 |
s = g_option_context_get_help (o, FALSE, NULL); |
| 662 |
g_printerr ("%s", s); |
| 663 |
g_free (s); |
| 664 |
goto out; |
| 665 |
} |
| 666 |
} |
| 667 |
|
| 668 |
if (request_completion && !complete_objects && !complete_devices) |
| 669 |
{ |
| 670 |
list_options (is_mount ? command_mount_entries : command_unmount_entries); |
| 671 |
} |
| 672 |
|
| 673 |
if (complete_objects) |
| 674 |
{ |
| 675 |
const gchar *object_path; |
| 676 |
|
| 677 |
objects = g_dbus_object_manager_get_objects (udisks_client_get_object_manager (client)); |
| 678 |
for (l = objects; l != NULL; l = l->next) |
| 679 |
{ |
| 680 |
gboolean is_mounted; |
| 681 |
|
| 682 |
object = UDISKS_OBJECT (l->data); |
| 683 |
filesystem = udisks_object_peek_filesystem (object); |
| 684 |
|
| 685 |
if (filesystem == NULL) |
| 686 |
continue; |
| 687 |
|
| 688 |
is_mounted = FALSE; |
| 689 |
mount_points = udisks_filesystem_get_mount_points (filesystem); |
| 690 |
if (mount_points != NULL && g_strv_length ((gchar **) mount_points) > 0) |
| 691 |
is_mounted = TRUE; |
| 692 |
|
| 693 |
if ((is_mount && !is_mounted) || (!is_mount && is_mounted)) |
| 694 |
{ |
| 695 |
object_path = g_dbus_object_get_object_path (G_DBUS_OBJECT (object)); |
| 696 |
g_assert (g_str_has_prefix (object_path, "/org/freedesktop/UDisks2/")); |
| 697 |
g_print ("%s \n", object_path + sizeof ("/org/freedesktop/UDisks2/") - 1); |
| 698 |
} |
| 699 |
} |
| 700 |
g_list_free_full (objects, g_object_unref); |
| 701 |
goto out; |
| 702 |
} |
| 703 |
|
| 704 |
if (complete_devices) |
| 705 |
{ |
| 706 |
objects = g_dbus_object_manager_get_objects (udisks_client_get_object_manager (client)); |
| 707 |
for (l = objects; l != NULL; l = l->next) |
| 708 |
{ |
| 709 |
object = UDISKS_OBJECT (l->data); |
| 710 |
block = udisks_object_peek_block (object); |
| 711 |
filesystem = udisks_object_peek_filesystem (object); |
| 712 |
|
| 713 |
if (block != NULL) |
| 714 |
{ |
| 715 |
gboolean is_mounted; |
| 716 |
|
| 717 |
is_mounted = FALSE; |
| 718 |
if (filesystem != NULL) |
| 719 |
{ |
| 720 |
mount_points = udisks_filesystem_get_mount_points (filesystem); |
| 721 |
if (mount_points != NULL && g_strv_length ((gchar **) mount_points) > 0) |
| 722 |
is_mounted = TRUE; |
| 723 |
} |
| 724 |
|
| 725 |
if ((is_mount && !is_mounted) || (!is_mount && is_mounted)) |
| 726 |
{ |
| 727 |
const gchar * const *symlinks; |
| 728 |
g_print ("%s \n", udisks_block_get_device (block)); |
| 729 |
symlinks = udisks_block_get_symlinks (block); |
| 730 |
for (n = 0; symlinks != NULL && symlinks[n] != NULL; n++) |
| 731 |
g_print ("%s \n", symlinks[n]); |
| 732 |
} |
| 733 |
} |
| 734 |
} |
| 735 |
g_list_free_full (objects, g_object_unref); |
| 736 |
goto out; |
| 737 |
} |
| 738 |
|
| 739 |
/* done with completion */ |
| 740 |
if (request_completion) |
| 741 |
goto out; |
| 742 |
|
| 743 |
if (opt_mount_unmount_object_path != NULL) |
| 744 |
{ |
| 745 |
object = lookup_object_by_path (opt_mount_unmount_object_path); |
| 746 |
if (object == NULL) |
| 747 |
{ |
| 748 |
g_printerr ("Error looking up object with path %s\n", opt_mount_unmount_object_path); |
| 749 |
goto out; |
| 750 |
} |
| 751 |
} |
| 752 |
else if (opt_mount_unmount_device != NULL) |
| 753 |
{ |
| 754 |
object = lookup_object_by_device (opt_mount_unmount_device); |
| 755 |
if (object == NULL) |
| 756 |
{ |
| 757 |
g_printerr ("Error looking up object for device %s\n", opt_mount_unmount_device); |
| 758 |
goto out; |
| 759 |
} |
| 760 |
} |
| 761 |
else |
| 762 |
{ |
| 763 |
s = g_option_context_get_help (o, FALSE, NULL); |
| 764 |
g_printerr ("%s", s); |
| 765 |
g_free (s); |
| 766 |
goto out; |
| 767 |
} |
| 768 |
|
| 769 |
block = udisks_object_peek_block (object); |
| 770 |
filesystem = udisks_object_peek_filesystem (object); |
| 771 |
if (filesystem == NULL) |
| 772 |
{ |
| 773 |
g_printerr ("Object %s is not a mountable filesystem.\n", g_dbus_object_get_object_path (G_DBUS_OBJECT (object))); |
| 774 |
g_object_unref (object); |
| 775 |
goto out; |
| 776 |
} |
| 777 |
|
| 778 |
if (opt_mount_filesystem_type == NULL) |
| 779 |
opt_mount_filesystem_type = g_strdup (""); |
| 780 |
|
| 781 |
g_variant_builder_init (&builder, G_VARIANT_TYPE_VARDICT); |
| 782 |
if (opt_mount_unmount_no_user_interaction) |
| 783 |
{ |
| 784 |
g_variant_builder_add (&builder, |
| 785 |
"{sv}", |
| 786 |
"auth.no_user_interaction", g_variant_new_boolean (TRUE)); |
| 787 |
} |
| 788 |
if (is_mount) |
| 789 |
{ |
| 790 |
if (opt_mount_options != NULL) |
| 791 |
{ |
| 792 |
g_variant_builder_add (&builder, |
| 793 |
"{sv}", |
| 794 |
"options", g_variant_new_string (opt_mount_options)); |
| 795 |
} |
| 796 |
if (opt_mount_filesystem_type != NULL) |
| 797 |
{ |
| 798 |
g_variant_builder_add (&builder, |
| 799 |
"{sv}", |
| 800 |
"fstype", g_variant_new_string (opt_mount_filesystem_type)); |
| 801 |
} |
| 802 |
} |
| 803 |
else |
| 804 |
{ |
| 805 |
if (opt_unmount_force) |
| 806 |
{ |
| 807 |
g_variant_builder_add (&builder, |
| 808 |
"{sv}", |
| 809 |
"force", g_variant_new_boolean (TRUE)); |
| 810 |
} |
| 811 |
} |
| 812 |
options = g_variant_builder_end (&builder); |
| 813 |
g_variant_ref_sink (options); |
| 814 |
|
| 815 |
try_again: |
| 816 |
if (is_mount) |
| 817 |
{ |
| 818 |
GError *error; |
| 819 |
gchar *mount_path; |
| 820 |
|
| 821 |
error = NULL; |
| 822 |
if (!udisks_filesystem_call_mount_sync (filesystem, |
| 823 |
options, |
| 824 |
&mount_path, |
| 825 |
NULL, /* GCancellable */ |
| 826 |
&error)) |
| 827 |
{ |
| 828 |
if (error->domain == UDISKS_ERROR && |
| 829 |
error->code == UDISKS_ERROR_NOT_AUTHORIZED_CAN_OBTAIN && |
| 830 |
setup_local_polkit_agent ()) |
| 831 |
{ |
| 832 |
g_clear_error (&error); |
| 833 |
goto try_again; |
| 834 |
} |
| 835 |
g_printerr ("Error mounting %s: %s\n", |
| 836 |
udisks_block_get_device (block), |
| 837 |
error->message); |
| 838 |
g_clear_error (&error); |
| 839 |
g_object_unref (object); |
| 840 |
goto out; |
| 841 |
} |
| 842 |
g_print ("Mounted %s at %s\n", |
| 843 |
udisks_block_get_device (block), |
| 844 |
mount_path); |
| 845 |
g_free (mount_path); |
| 846 |
} |
| 847 |
else |
| 848 |
{ |
| 849 |
GError *error; |
| 850 |
|
| 851 |
error = NULL; |
| 852 |
if (!udisks_filesystem_call_unmount_sync (filesystem, |
| 853 |
options, |
| 854 |
NULL, /* GCancellable */ |
| 855 |
&error)) |
| 856 |
{ |
| 857 |
if (error->domain == UDISKS_ERROR && |
| 858 |
error->code == UDISKS_ERROR_NOT_AUTHORIZED_CAN_OBTAIN && |
| 859 |
setup_local_polkit_agent ()) |
| 860 |
{ |
| 861 |
g_clear_error (&error); |
| 862 |
goto try_again; |
| 863 |
} |
| 864 |
g_printerr ("Error unmounting %s: %s\n", |
| 865 |
udisks_block_get_device (block), |
| 866 |
error->message); |
| 867 |
g_clear_error (&error); |
| 868 |
g_object_unref (object); |
| 869 |
goto out; |
| 870 |
} |
| 871 |
g_print ("Unmounted %s.\n", |
| 872 |
udisks_block_get_device (block)); |
| 873 |
} |
| 874 |
|
| 875 |
ret = 0; |
| 876 |
|
| 877 |
g_object_unref (object); |
| 878 |
|
| 879 |
out: |
| 880 |
if (options != NULL) |
| 881 |
g_variant_unref (options); |
| 882 |
g_option_context_free (o); |
| 883 |
g_free (opt_mount_unmount_object_path); |
| 884 |
g_free (opt_mount_unmount_device); |
| 885 |
g_free (opt_mount_options); |
| 886 |
g_free (opt_mount_filesystem_type); |
| 887 |
return ret; |
| 888 |
} |
| 889 |
|
| 890 |
/* ---------------------------------------------------------------------------------------------------- */ |
| 891 |
|
| 892 |
static gchar * |
| 893 |
read_passphrase (void) |
| 894 |
{ |
| 895 |
struct termios ts, ots; |
| 896 |
GString *str = NULL; |
| 897 |
const gchar *tty_name = NULL; |
| 898 |
FILE *tty = NULL; |
| 899 |
gchar *ret = NULL; |
| 900 |
|
| 901 |
tty_name = ctermid (NULL); |
| 902 |
if (tty_name == NULL) |
| 903 |
{ |
| 904 |
g_warning ("Cannot determine pathname for current controlling terminal for the process: %m"); |
| 905 |
return NULL; |
| 906 |
} |
| 907 |
|
| 908 |
tty = fopen (tty_name, "r+"); |
| 909 |
if (tty == NULL) |
| 910 |
{ |
| 911 |
g_warning ("Error opening current controlling terminal %s: %m", tty_name); |
| 912 |
return NULL; |
| 913 |
} |
| 914 |
|
| 915 |
fprintf (tty, "Passphrase: "); |
| 916 |
fflush (tty); |
| 917 |
|
| 918 |
setbuf (tty, NULL); |
| 919 |
|
| 920 |
tcgetattr (fileno (tty), &ts); |
| 921 |
ots = ts; |
| 922 |
ts.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL); |
| 923 |
tcsetattr (fileno (tty), TCSAFLUSH, &ts); |
| 924 |
|
| 925 |
str = g_string_new (NULL); |
| 926 |
while (TRUE) |
| 927 |
{ |
| 928 |
gint c; |
| 929 |
c = getc (tty); |
| 930 |
if (c == '\n') |
| 931 |
{ |
| 932 |
/* ok, done */ |
| 933 |
break; |
| 934 |
} |
| 935 |
else if (c == EOF) |
| 936 |
{ |
| 937 |
tcsetattr (fileno (tty), TCSAFLUSH, &ots); |
| 938 |
g_error ("Unexpected EOF while reading from controlling terminal."); |
| 939 |
abort (); |
| 940 |
break; |
| 941 |
} |
| 942 |
else |
| 943 |
{ |
| 944 |
g_string_append_c (str, c); |
| 945 |
} |
| 946 |
} |
| 947 |
tcsetattr (fileno (tty), TCSAFLUSH, &ots); |
| 948 |
putc ('\n', tty); |
| 949 |
|
| 950 |
ret = g_string_free (str, FALSE); |
| 951 |
str = NULL; |
| 952 |
|
| 953 |
fclose (tty); |
| 954 |
return ret; |
| 955 |
} |
| 956 |
|
| 957 |
static gboolean |
| 958 |
encrypted_is_unlocked (UDisksObject *encrypted_object) |
| 959 |
{ |
| 960 |
GList *objects; |
| 961 |
GList *l; |
| 962 |
const gchar *encrypted_object_path; |
| 963 |
gboolean ret; |
| 964 |
|
| 965 |
ret = FALSE; |
| 966 |
|
| 967 |
encrypted_object_path = g_dbus_object_get_object_path (G_DBUS_OBJECT (encrypted_object)); |
| 968 |
|
| 969 |
objects = g_dbus_object_manager_get_objects (udisks_client_get_object_manager (client)); |
| 970 |
for (l = objects; l != NULL; l = l->next) |
| 971 |
{ |
| 972 |
UDisksObject *object = UDISKS_OBJECT (l->data); |
| 973 |
UDisksBlock *block; |
| 974 |
|
| 975 |
block = udisks_object_peek_block (object); |
| 976 |
if (block != NULL) |
| 977 |
{ |
| 978 |
if (g_strcmp0 (udisks_block_get_crypto_backing_device (block), encrypted_object_path) == 0) |
| 979 |
{ |
| 980 |
ret = TRUE; |
| 981 |
goto out; |
| 982 |
} |
| 983 |
} |
| 984 |
} |
| 985 |
|
| 986 |
out: |
| 987 |
g_list_free_full (objects, g_object_unref); |
| 988 |
return ret; |
| 989 |
} |
| 990 |
|
| 991 |
static gchar *opt_unlock_lock_object_path = NULL; |
| 992 |
static gchar *opt_unlock_lock_device = NULL; |
| 993 |
static gboolean opt_unlock_lock_no_user_interaction = FALSE; |
| 994 |
static gchar *opt_unlock_keyfile = NULL; |
| 995 |
static gboolean opt_unlock_read_only = FALSE; |
| 996 |
|
| 997 |
static const GOptionEntry command_unlock_entries[] = |
| 998 |
{ |
| 999 |
{ |
| 1000 |
"object-path", |
| 1001 |
'p', |
| 1002 |
0, |
| 1003 |
G_OPTION_ARG_STRING, |
| 1004 |
&opt_unlock_lock_object_path, |
| 1005 |
"Object to unlock", |
| 1006 |
NULL |
| 1007 |
}, |
| 1008 |
{ |
| 1009 |
"block-device", |
| 1010 |
'b', |
| 1011 |
0, |
| 1012 |
G_OPTION_ARG_STRING, |
| 1013 |
&opt_unlock_lock_device, |
| 1014 |
"Block device to unlock", |
| 1015 |
NULL |
| 1016 |
}, |
| 1017 |
{ |
| 1018 |
"no-user-interaction", |
| 1019 |
0, /* no short option */ |
| 1020 |
0, |
| 1021 |
G_OPTION_ARG_NONE, |
| 1022 |
&opt_unlock_lock_no_user_interaction, |
| 1023 |
"Do not authenticate the user if needed", |
| 1024 |
NULL |
| 1025 |
}, |
| 1026 |
{ |
| 1027 |
"key-file", |
| 1028 |
0, /* no short option */ |
| 1029 |
0, |
| 1030 |
G_OPTION_ARG_STRING, |
| 1031 |
&opt_unlock_keyfile, |
| 1032 |
"Keyfile for unlocking", |
| 1033 |
NULL |
| 1034 |
}, |
| 1035 |
{ |
| 1036 |
"read-only", |
| 1037 |
0, /* no short option */ |
| 1038 |
0, |
| 1039 |
G_OPTION_ARG_NONE, |
| 1040 |
&opt_unlock_read_only, |
| 1041 |
"Unlock the device as read-only", |
| 1042 |
NULL |
| 1043 |
}, |
| 1044 |
{ |
| 1045 |
NULL |
| 1046 |
} |
| 1047 |
}; |
| 1048 |
|
| 1049 |
static const GOptionEntry command_lock_entries[] = |
| 1050 |
{ |
| 1051 |
{ |
| 1052 |
"object-path", |
| 1053 |
'p', |
| 1054 |
0, |
| 1055 |
G_OPTION_ARG_STRING, |
| 1056 |
&opt_unlock_lock_object_path, |
| 1057 |
"Object to lock", |
| 1058 |
NULL |
| 1059 |
}, |
| 1060 |
{ |
| 1061 |
"block-device", |
| 1062 |
'b', |
| 1063 |
0, |
| 1064 |
G_OPTION_ARG_STRING, |
| 1065 |
&opt_unlock_lock_device, |
| 1066 |
"Block device to lock", |
| 1067 |
NULL |
| 1068 |
}, |
| 1069 |
{ |
| 1070 |
"no-user-interaction", |
| 1071 |
0, /* no short option */ |
| 1072 |
0, |
| 1073 |
G_OPTION_ARG_NONE, |
| 1074 |
&opt_unlock_lock_no_user_interaction, |
| 1075 |
"Do not authenticate the user if needed", |
| 1076 |
NULL |
| 1077 |
}, |
| 1078 |
{ |
| 1079 |
NULL |
| 1080 |
} |
| 1081 |
}; |
| 1082 |
|
| 1083 |
static GVariant* |
| 1084 |
pack_binary_blob (const gchar *data, |
| 1085 |
gsize size) |
| 1086 |
{ |
| 1087 |
GVariantBuilder builder; |
| 1088 |
|
| 1089 |
g_variant_builder_init (&builder, G_VARIANT_TYPE ("ay")); |
| 1090 |
|
| 1091 |
for (gsize i = 0; i < size; i++) |
| 1092 |
{ |
| 1093 |
g_variant_builder_add (&builder, "y", data[i]); |
| 1094 |
} |
| 1095 |
|
| 1096 |
return g_variant_builder_end (&builder); |
| 1097 |
} |
| 1098 |
|
| 1099 |
static gint |
| 1100 |
handle_command_unlock_lock (gint *argc, |
| 1101 |
gchar **argv[], |
| 1102 |
gboolean request_completion, |
| 1103 |
const gchar *completion_cur, |
| 1104 |
const gchar *completion_prev, |
| 1105 |
gboolean is_unlock) |
| 1106 |
{ |
| 1107 |
gint ret; |
| 1108 |
GOptionContext *o; |
| 1109 |
gchar *s; |
| 1110 |
gboolean complete_objects; |
| 1111 |
gboolean complete_devices; |
| 1112 |
GList *l; |
| 1113 |
GList *objects; |
| 1114 |
UDisksObject *object; |
| 1115 |
UDisksBlock *block; |
| 1116 |
UDisksEncrypted *encrypted; |
| 1117 |
guint n; |
| 1118 |
GVariant *options; |
| 1119 |
GVariantBuilder builder; |
| 1120 |
gchar *passphrase; |
| 1121 |
gchar *keyfile_contents = NULL; |
| 1122 |
gsize keyfile_size = 0; |
| 1123 |
GError *error = NULL; |
| 1124 |
|
| 1125 |
ret = 1; |
| 1126 |
opt_unlock_lock_object_path = NULL; |
| 1127 |
opt_unlock_lock_device = NULL; |
| 1128 |
object = NULL; |
| 1129 |
options = NULL; |
| 1130 |
passphrase = NULL; |
| 1131 |
|
| 1132 |
if (is_unlock) |
| 1133 |
modify_argv0_for_command (argc, argv, "unlock"); |
| 1134 |
else |
| 1135 |
modify_argv0_for_command (argc, argv, "lock"); |
| 1136 |
|
| 1137 |
o = g_option_context_new (NULL); |
| 1138 |
if (request_completion) |
| 1139 |
g_option_context_set_ignore_unknown_options (o, TRUE); |
| 1140 |
g_option_context_set_help_enabled (o, FALSE); |
| 1141 |
if (is_unlock) |
| 1142 |
g_option_context_set_summary (o, "Unlock an encrypted device."); |
| 1143 |
else |
| 1144 |
g_option_context_set_summary (o, "Lock an encrypted device."); |
| 1145 |
g_option_context_add_main_entries (o, |
| 1146 |
is_unlock ? command_unlock_entries : command_lock_entries, |
| 1147 |
NULL /* GETTEXT_PACKAGE*/); |
| 1148 |
|
| 1149 |
complete_objects = FALSE; |
| 1150 |
if (request_completion && (g_strcmp0 (completion_prev, "--object-path") == 0 || g_strcmp0 (completion_prev, "-p") == 0 || |
| 1151 |
g_strcmp0 (completion_cur, "--object-path") == 0 || g_strcmp0 (completion_cur, "-p") == 0)) |
| 1152 |
{ |
| 1153 |
complete_objects = TRUE; |
| 1154 |
remove_arg ((*argc) - 1, argc, argv); |
| 1155 |
} |
| 1156 |
|
| 1157 |
complete_devices = FALSE; |
| 1158 |
if (request_completion && (g_strcmp0 (completion_prev, "--block-device") == 0 || g_strcmp0 (completion_prev, "-b") == 0 || |
| 1159 |
g_strcmp0 (completion_cur, "--block-device") == 0 || g_strcmp0 (completion_cur, "-b") == 0)) |
| 1160 |
{ |
| 1161 |
complete_devices = TRUE; |
| 1162 |
remove_arg ((*argc) - 1, argc, argv); |
| 1163 |
} |
| 1164 |
|
| 1165 |
if (!g_option_context_parse (o, argc, argv, NULL)) |
| 1166 |
{ |
| 1167 |
if (!request_completion) |
| 1168 |
{ |
| 1169 |
s = g_option_context_get_help (o, FALSE, NULL); |
| 1170 |
g_printerr ("%s", s); |
| 1171 |
g_free (s); |
| 1172 |
goto out; |
| 1173 |
} |
| 1174 |
} |
| 1175 |
|
| 1176 |
if (request_completion && !complete_objects && !complete_devices) |
| 1177 |
{ |
| 1178 |
list_options (is_unlock ? command_unlock_entries : command_lock_entries); |
| 1179 |
} |
| 1180 |
|
| 1181 |
if (complete_objects) |
| 1182 |
{ |
| 1183 |
const gchar *object_path; |
| 1184 |
|
| 1185 |
objects = g_dbus_object_manager_get_objects (udisks_client_get_object_manager (client)); |
| 1186 |
for (l = objects; l != NULL; l = l->next) |
| 1187 |
{ |
| 1188 |
gboolean is_unlocked; |
| 1189 |
|
| 1190 |
object = UDISKS_OBJECT (l->data); |
| 1191 |
encrypted = udisks_object_peek_encrypted (object); |
| 1192 |
|
| 1193 |
if (encrypted == NULL) |
| 1194 |
continue; |
| 1195 |
|
| 1196 |
is_unlocked = encrypted_is_unlocked (object); |
| 1197 |
|
| 1198 |
if ((is_unlock && !is_unlocked) || (!is_unlock && is_unlocked)) |
| 1199 |
{ |
| 1200 |
object_path = g_dbus_object_get_object_path (G_DBUS_OBJECT (object)); |
| 1201 |
g_assert (g_str_has_prefix (object_path, "/org/freedesktop/UDisks2/")); |
| 1202 |
g_print ("%s \n", object_path + sizeof ("/org/freedesktop/UDisks2/") - 1); |
| 1203 |
} |
| 1204 |
} |
| 1205 |
g_list_free_full (objects, g_object_unref); |
| 1206 |
goto out; |
| 1207 |
} |
| 1208 |
|
| 1209 |
if (complete_devices) |
| 1210 |
{ |
| 1211 |
objects = g_dbus_object_manager_get_objects (udisks_client_get_object_manager (client)); |
| 1212 |
for (l = objects; l != NULL; l = l->next) |
| 1213 |
{ |
| 1214 |
object = UDISKS_OBJECT (l->data); |
| 1215 |
block = udisks_object_peek_block (object); |
| 1216 |
|
| 1217 |
if (block != NULL) |
| 1218 |
{ |
| 1219 |
gboolean is_unlocked; |
| 1220 |
|
| 1221 |
is_unlocked = encrypted_is_unlocked (object); |
| 1222 |
|
| 1223 |
if ((is_unlock && !is_unlocked) || (!is_unlock && is_unlocked)) |
| 1224 |
{ |
| 1225 |
const gchar * const *symlinks; |
| 1226 |
g_print ("%s \n", udisks_block_get_device (block)); |
| 1227 |
symlinks = udisks_block_get_symlinks (block); |
| 1228 |
for (n = 0; symlinks != NULL && symlinks[n] != NULL; n++) |
| 1229 |
g_print ("%s \n", symlinks[n]); |
| 1230 |
} |
| 1231 |
} |
| 1232 |
} |
| 1233 |
g_list_free_full (objects, g_object_unref); |
| 1234 |
goto out; |
| 1235 |
} |
| 1236 |
|
| 1237 |
/* done with completion */ |
| 1238 |
if (request_completion) |
| 1239 |
goto out; |
| 1240 |
|
| 1241 |
if (opt_unlock_lock_object_path != NULL) |
| 1242 |
{ |
| 1243 |
object = lookup_object_by_path (opt_unlock_lock_object_path); |
| 1244 |
if (object == NULL) |
| 1245 |
{ |
| 1246 |
g_printerr ("Error looking up object with path %s\n", opt_unlock_lock_object_path); |
| 1247 |
goto out; |
| 1248 |
} |
| 1249 |
} |
| 1250 |
else if (opt_unlock_lock_device != NULL) |
| 1251 |
{ |
| 1252 |
object = lookup_object_by_device (opt_unlock_lock_device); |
| 1253 |
if (object == NULL) |
| 1254 |
{ |
| 1255 |
g_printerr ("Error looking up object for device %s\n", opt_unlock_lock_device); |
| 1256 |
goto out; |
| 1257 |
} |
| 1258 |
} |
| 1259 |
else |
| 1260 |
{ |
| 1261 |
s = g_option_context_get_help (o, FALSE, NULL); |
| 1262 |
g_printerr ("%s", s); |
| 1263 |
g_free (s); |
| 1264 |
goto out; |
| 1265 |
} |
| 1266 |
|
| 1267 |
block = udisks_object_peek_block (object); |
| 1268 |
encrypted = udisks_object_peek_encrypted (object); |
| 1269 |
if (encrypted == NULL) |
| 1270 |
{ |
| 1271 |
g_printerr ("Object %s is not an encrypted device.\n", g_dbus_object_get_object_path (G_DBUS_OBJECT (object))); |
| 1272 |
g_object_unref (object); |
| 1273 |
goto out; |
| 1274 |
} |
| 1275 |
|
| 1276 |
g_variant_builder_init (&builder, G_VARIANT_TYPE_VARDICT); |
| 1277 |
if (opt_unlock_lock_no_user_interaction) |
| 1278 |
{ |
| 1279 |
g_variant_builder_add (&builder, |
| 1280 |
"{sv}", |
| 1281 |
"auth.no_user_interaction", g_variant_new_boolean (TRUE)); |
| 1282 |
} |
| 1283 |
if (opt_unlock_keyfile) |
| 1284 |
{ |
| 1285 |
if (!g_file_get_contents (opt_unlock_keyfile, |
| 1286 |
&keyfile_contents, |
| 1287 |
&keyfile_size, |
| 1288 |
&error)) |
| 1289 |
{ |
| 1290 |
g_printerr ("Error unlocking %s: %s\n", |
| 1291 |
udisks_block_get_device (block), |
| 1292 |
error->message); |
| 1293 |
g_clear_error (&error); |
| 1294 |
goto out; |
| 1295 |
} |
| 1296 |
g_variant_builder_add (&builder, |
| 1297 |
"{sv}", |
| 1298 |
"keyfile_contents", |
| 1299 |
pack_binary_blob (keyfile_contents, keyfile_size)); |
| 1300 |
} |
| 1301 |
if (opt_unlock_read_only) |
| 1302 |
g_variant_builder_add (&builder, |
| 1303 |
"{sv}", |
| 1304 |
"read-only", g_variant_new_boolean (TRUE)); |
| 1305 |
options = g_variant_builder_end (&builder); |
| 1306 |
g_variant_ref_sink (options); |
| 1307 |
|
| 1308 |
if (is_unlock && !opt_unlock_keyfile) |
| 1309 |
passphrase = read_passphrase (); |
| 1310 |
|
| 1311 |
try_again: |
| 1312 |
if (is_unlock) |
| 1313 |
{ |
| 1314 |
gchar *cleartext_object_path; |
| 1315 |
UDisksObject *cleartext_object; |
| 1316 |
|
| 1317 |
if (!udisks_encrypted_call_unlock_sync (encrypted, |
| 1318 |
passphrase ? passphrase : "", |
| 1319 |
options, |
| 1320 |
&cleartext_object_path, |
| 1321 |
NULL, /* GCancellable */ |
| 1322 |
&error)) |
| 1323 |
{ |
| 1324 |
if (error->domain == UDISKS_ERROR && |
| 1325 |
error->code == UDISKS_ERROR_NOT_AUTHORIZED_CAN_OBTAIN && |
| 1326 |
setup_local_polkit_agent ()) |
| 1327 |
{ |
| 1328 |
g_clear_error (&error); |
| 1329 |
goto try_again; |
| 1330 |
} |
| 1331 |
g_printerr ("Error unlocking %s: %s\n", |
| 1332 |
udisks_block_get_device (block), |
| 1333 |
error->message); |
| 1334 |
g_clear_error (&error); |
| 1335 |
g_object_unref (object); |
| 1336 |
goto out; |
| 1337 |
} |
| 1338 |
udisks_client_settle (client); |
| 1339 |
|
| 1340 |
cleartext_object = UDISKS_OBJECT (g_dbus_object_manager_get_object (udisks_client_get_object_manager (client), |
| 1341 |
(cleartext_object_path))); |
| 1342 |
g_print ("Unlocked %s as %s.\n", |
| 1343 |
udisks_block_get_device (block), |
| 1344 |
udisks_block_get_device (udisks_object_get_block (cleartext_object))); |
| 1345 |
g_object_unref (cleartext_object); |
| 1346 |
g_free (cleartext_object_path); |
| 1347 |
} |
| 1348 |
else |
| 1349 |
{ |
| 1350 |
if (!udisks_encrypted_call_lock_sync (encrypted, |
| 1351 |
options, |
| 1352 |
NULL, /* GCancellable */ |
| 1353 |
&error)) |
| 1354 |
{ |
| 1355 |
if (error->domain == UDISKS_ERROR && |
| 1356 |
error->code == UDISKS_ERROR_NOT_AUTHORIZED_CAN_OBTAIN && |
| 1357 |
setup_local_polkit_agent ()) |
| 1358 |
{ |
| 1359 |
g_clear_error (&error); |
| 1360 |
goto try_again; |
| 1361 |
} |
| 1362 |
g_printerr ("Error locking %s: %s\n", |
| 1363 |
udisks_block_get_device (block), |
| 1364 |
error->message); |
| 1365 |
g_clear_error (&error); |
| 1366 |
g_object_unref (object); |
| 1367 |
goto out; |
| 1368 |
} |
| 1369 |
g_print ("Locked %s.\n", |
| 1370 |
udisks_block_get_device (block)); |
| 1371 |
} |
| 1372 |
|
| 1373 |
ret = 0; |
| 1374 |
|
| 1375 |
g_object_unref (object); |
| 1376 |
|
| 1377 |
out: |
| 1378 |
if (passphrase != NULL) |
| 1379 |
{ |
| 1380 |
memset (passphrase, '\0', strlen (passphrase)); |
| 1381 |
g_free (passphrase); |
| 1382 |
} |
| 1383 |
if (keyfile_contents != NULL) |
| 1384 |
{ |
| 1385 |
memset (keyfile_contents, '\0', keyfile_size); |
| 1386 |
g_free (keyfile_contents); |
| 1387 |
} |
| 1388 |
if (options != NULL) |
| 1389 |
g_variant_unref (options); |
| 1390 |
g_option_context_free (o); |
| 1391 |
g_free (opt_unlock_lock_object_path); |
| 1392 |
g_free (opt_unlock_lock_device); |
| 1393 |
return ret; |
| 1394 |
} |
| 1395 |
|
| 1396 |
/* ---------------------------------------------------------------------------------------------------- */ |
| 1397 |
|
| 1398 |
static gchar *opt_loop_file = NULL; |
| 1399 |
static gchar *opt_loop_object_path = NULL; |
| 1400 |
static gchar *opt_loop_device = NULL; |
| 1401 |
static gboolean opt_loop_no_user_interaction = FALSE; |
| 1402 |
static gboolean opt_loop_read_only = FALSE; |
| 1403 |
static gint64 opt_loop_offset = 0; |
| 1404 |
static gint64 opt_loop_size = 0; |
| 1405 |
static gboolean opt_loop_no_partition_scan = FALSE; |
| 1406 |
|
| 1407 |
static const GOptionEntry command_loop_setup_entries[] = |
| 1408 |
{ |
| 1409 |
{ |
| 1410 |
"file", |
| 1411 |
'f', |
| 1412 |
0, |
| 1413 |
G_OPTION_ARG_FILENAME, |
| 1414 |
&opt_loop_file, |
| 1415 |
"File to set-up a loop device for", |
| 1416 |
NULL |
| 1417 |
}, |
| 1418 |
{ |
| 1419 |
"read-only", |
| 1420 |
'r', |
| 1421 |
0, |
| 1422 |
G_OPTION_ARG_NONE, |
| 1423 |
&opt_loop_read_only, |
| 1424 |
"Setup read-only device", |
| 1425 |
NULL |
| 1426 |
}, |
| 1427 |
{ |
| 1428 |
"offset", |
| 1429 |
'o', |
| 1430 |
0, |
| 1431 |
G_OPTION_ARG_INT64, |
| 1432 |
&opt_loop_offset, |
| 1433 |
"Start at <num> bytes into file", |
| 1434 |
NULL |
| 1435 |
}, |
| 1436 |
{ |
| 1437 |
"size", |
| 1438 |
's', |
| 1439 |
0, |
| 1440 |
G_OPTION_ARG_INT64, |
| 1441 |
&opt_loop_size, |
| 1442 |
"Limit size to <num> bytes", |
| 1443 |
NULL |
| 1444 |
}, |
| 1445 |
{ |
| 1446 |
"no-user-interaction", |
| 1447 |
0, /* no short option */ |
| 1448 |
0, |
| 1449 |
G_OPTION_ARG_NONE, |
| 1450 |
&opt_loop_no_user_interaction, |
| 1451 |
"Do not authenticate the user if needed", |
| 1452 |
NULL |
| 1453 |
}, |
| 1454 |
{ |
| 1455 |
"no-partition-scan", |
| 1456 |
0, /* no short option */ |
| 1457 |
0, |
| 1458 |
G_OPTION_ARG_NONE, |
| 1459 |
&opt_loop_no_partition_scan, |
| 1460 |
"Do not scan the loop device for partitions", |
| 1461 |
NULL |
| 1462 |
}, |
| 1463 |
{ |
| 1464 |
NULL |
| 1465 |
} |
| 1466 |
}; |
| 1467 |
|
| 1468 |
static const GOptionEntry command_loop_delete_entries[] = |
| 1469 |
{ |
| 1470 |
{ |
| 1471 |
"object-path", |
| 1472 |
'p', |
| 1473 |
0, |
| 1474 |
G_OPTION_ARG_STRING, |
| 1475 |
&opt_loop_object_path, |
| 1476 |
"Object for loop device to delete", |
| 1477 |
NULL |
| 1478 |
}, |
| 1479 |
{ |
| 1480 |
"block-device", |
| 1481 |
'b', |
| 1482 |
0, |
| 1483 |
G_OPTION_ARG_STRING, |
| 1484 |
&opt_loop_device, |
| 1485 |
"Loop device to delete", |
| 1486 |
NULL |
| 1487 |
}, |
| 1488 |
{ |
| 1489 |
"no-user-interaction", |
| 1490 |
0, /* no short option */ |
| 1491 |
0, |
| 1492 |
G_OPTION_ARG_NONE, |
| 1493 |
&opt_loop_no_user_interaction, |
| 1494 |
"Do not authenticate the user if needed", |
| 1495 |
NULL |
| 1496 |
}, |
| 1497 |
{ |
| 1498 |
NULL |
| 1499 |
} |
| 1500 |
}; |
| 1501 |
|
| 1502 |
static gint |
| 1503 |
handle_command_loop (gint *argc, |
| 1504 |
gchar **argv[], |
| 1505 |
gboolean request_completion, |
| 1506 |
const gchar *completion_cur, |
| 1507 |
const gchar *completion_prev, |
| 1508 |
gboolean is_setup) |
| 1509 |
{ |
| 1510 |
gint ret; |
| 1511 |
GOptionContext *o; |
| 1512 |
gchar *s; |
| 1513 |
gboolean complete_objects; |
| 1514 |
gboolean complete_devices; |
| 1515 |
gboolean complete_files; |
| 1516 |
GList *l; |
| 1517 |
GList *objects; |
| 1518 |
UDisksObject *object; |
| 1519 |
UDisksBlock *block; |
| 1520 |
guint n; |
| 1521 |
GVariant *options; |
| 1522 |
GVariantBuilder builder; |
| 1523 |
GError *error; |
| 1524 |
|
| 1525 |
ret = 1; |
| 1526 |
opt_loop_object_path = NULL; |
| 1527 |
opt_loop_device = NULL; |
| 1528 |
object = NULL; |
| 1529 |
options = NULL; |
| 1530 |
|
| 1531 |
if (is_setup) |
| 1532 |
modify_argv0_for_command (argc, argv, "loop-setup"); |
| 1533 |
else |
| 1534 |
modify_argv0_for_command (argc, argv, "loop-delete"); |
| 1535 |
|
| 1536 |
o = g_option_context_new (NULL); |
| 1537 |
if (request_completion) |
| 1538 |
g_option_context_set_ignore_unknown_options (o, TRUE); |
| 1539 |
g_option_context_set_help_enabled (o, FALSE); |
| 1540 |
if (is_setup) |
| 1541 |
g_option_context_set_summary (o, "Set up a loop device."); |
| 1542 |
else |
| 1543 |
g_option_context_set_summary (o, "Delete a loop device."); |
| 1544 |
g_option_context_add_main_entries (o, |
| 1545 |
is_setup ? command_loop_setup_entries : command_loop_delete_entries, |
| 1546 |
NULL /* GETTEXT_PACKAGE*/); |
| 1547 |
|
| 1548 |
complete_objects = FALSE; |
| 1549 |
if (request_completion && (g_strcmp0 (completion_prev, "--object-path") == 0 || g_strcmp0 (completion_prev, "-p") == 0 || |
| 1550 |
g_strcmp0 (completion_cur, "--object-path") == 0 || g_strcmp0 (completion_cur, "-p") == 0)) |
| 1551 |
{ |
| 1552 |
complete_objects = TRUE; |
| 1553 |
remove_arg ((*argc) - 1, argc, argv); |
| 1554 |
} |
| 1555 |
|
| 1556 |
complete_devices = FALSE; |
| 1557 |
if (request_completion && (g_strcmp0 (completion_prev, "--block-device") == 0 || g_strcmp0 (completion_prev, "-b") == 0 || |
| 1558 |
g_strcmp0 (completion_cur, "--block-device") == 0 || g_strcmp0 (completion_cur, "-b") == 0)) |
| 1559 |
{ |
| 1560 |
complete_devices = TRUE; |
| 1561 |
remove_arg ((*argc) - 1, argc, argv); |
| 1562 |
} |
| 1563 |
|
| 1564 |
complete_files = FALSE; |
| 1565 |
if (request_completion && (g_strcmp0 (completion_prev, "--file") == 0 || g_strcmp0 (completion_prev, "-f") == 0 || |
| 1566 |
g_strcmp0 (completion_cur, "--file") == 0 || g_strcmp0 (completion_cur, "-f") == 0)) |
| 1567 |
{ |
| 1568 |
complete_files = TRUE; |
| 1569 |
remove_arg ((*argc) - 1, argc, argv); |
| 1570 |
} |
| 1571 |
|
| 1572 |
if (!g_option_context_parse (o, argc, argv, NULL)) |
| 1573 |
{ |
| 1574 |
if (!request_completion) |
| 1575 |
{ |
| 1576 |
s = g_option_context_get_help (o, FALSE, NULL); |
| 1577 |
g_printerr ("%s", s); |
| 1578 |
g_free (s); |
| 1579 |
goto out; |
| 1580 |
} |
| 1581 |
} |
| 1582 |
|
| 1583 |
if (request_completion) |
| 1584 |
{ |
| 1585 |
if (!complete_files && !complete_devices && !complete_objects) |
| 1586 |
{ |
| 1587 |
list_options (is_setup ? command_loop_setup_entries : command_loop_delete_entries); |
| 1588 |
} |
| 1589 |
else |
| 1590 |
{ |
| 1591 |
if (complete_files) |
| 1592 |
{ |
| 1593 |
g_print ("@FILES@"); |
| 1594 |
} |
| 1595 |
|
| 1596 |
if (complete_objects) |
| 1597 |
{ |
| 1598 |
const gchar *object_path; |
| 1599 |
objects = g_dbus_object_manager_get_objects (udisks_client_get_object_manager (client)); |
| 1600 |
for (l = objects; l != NULL; l = l->next) |
| 1601 |
{ |
| 1602 |
object = UDISKS_OBJECT (l->data); |
| 1603 |
if (udisks_object_peek_loop (object) != NULL) |
| 1604 |
{ |
| 1605 |
object_path = g_dbus_object_get_object_path (G_DBUS_OBJECT (object)); |
| 1606 |
g_assert (g_str_has_prefix (object_path, "/org/freedesktop/UDisks2/")); |
| 1607 |
g_print ("%s \n", object_path + sizeof ("/org/freedesktop/UDisks2/") - 1); |
| 1608 |
} |
| 1609 |
} |
| 1610 |
g_list_free_full (objects, g_object_unref); |
| 1611 |
} |
| 1612 |
|
| 1613 |
if (complete_devices) |
| 1614 |
{ |
| 1615 |
objects = g_dbus_object_manager_get_objects (udisks_client_get_object_manager (client)); |
| 1616 |
for (l = objects; l != NULL; l = l->next) |
| 1617 |
{ |
| 1618 |
object = UDISKS_OBJECT (l->data); |
| 1619 |
block = udisks_object_peek_block (object); |
| 1620 |
if (block != NULL && udisks_object_peek_loop (object) != NULL) |
| 1621 |
{ |
| 1622 |
const gchar * const *symlinks; |
| 1623 |
g_print ("%s \n", udisks_block_get_device (block)); |
| 1624 |
symlinks = udisks_block_get_symlinks (block); |
| 1625 |
for (n = 0; symlinks != NULL && symlinks[n] != NULL; n++) |
| 1626 |
g_print ("%s \n", symlinks[n]); |
| 1627 |
} |
| 1628 |
} |
| 1629 |
g_list_free_full (objects, g_object_unref); |
| 1630 |
} |
| 1631 |
} |
| 1632 |
|
| 1633 |
/* done with completion */ |
| 1634 |
goto out; |
| 1635 |
} |
| 1636 |
|
| 1637 |
g_variant_builder_init (&builder, G_VARIANT_TYPE_VARDICT); |
| 1638 |
if (opt_loop_no_user_interaction) |
| 1639 |
{ |
| 1640 |
g_variant_builder_add (&builder, |
| 1641 |
"{sv}", |
| 1642 |
"auth.no_user_interaction", g_variant_new_boolean (TRUE)); |
| 1643 |
} |
| 1644 |
if (is_setup) |
| 1645 |
{ |
| 1646 |
if (opt_loop_read_only) |
| 1647 |
g_variant_builder_add (&builder, |
| 1648 |
"{sv}", |
| 1649 |
"read-only", g_variant_new_boolean (TRUE)); |
| 1650 |
if (opt_loop_offset > 0) |
| 1651 |
g_variant_builder_add (&builder, |
| 1652 |
"{sv}", |
| 1653 |
"offset", g_variant_new_uint64 (opt_loop_offset)); |
| 1654 |
if (opt_loop_size > 0) |
| 1655 |
g_variant_builder_add (&builder, |
| 1656 |
"{sv}", |
| 1657 |
"size", g_variant_new_uint64 (opt_loop_size)); |
| 1658 |
if (opt_loop_no_partition_scan) |
| 1659 |
g_variant_builder_add (&builder, |
| 1660 |
"{sv}", |
| 1661 |
"no-part-scan", g_variant_new_boolean (TRUE)); |
| 1662 |
} |
| 1663 |
options = g_variant_builder_end (&builder); |
| 1664 |
g_variant_ref_sink (options); |
| 1665 |
|
| 1666 |
if (is_setup) |
| 1667 |
{ |
| 1668 |
gchar *resulting_object_path; |
| 1669 |
UDisksObject *resulting_object; |
| 1670 |
GUnixFDList *fd_list; |
| 1671 |
gint fd; |
| 1672 |
gboolean rc; |
| 1673 |
|
| 1674 |
if (opt_loop_file == NULL) |
| 1675 |
{ |
| 1676 |
s = g_option_context_get_help (o, FALSE, NULL); |
| 1677 |
g_printerr ("%s", s); |
| 1678 |
g_free (s); |
| 1679 |
goto out; |
| 1680 |
} |
| 1681 |
|
| 1682 |
fd = open (opt_loop_file, opt_loop_read_only ? O_RDONLY : O_RDWR); |
| 1683 |
if (fd == -1) |
| 1684 |
{ |
| 1685 |
g_printerr ("Error opening (%s) file %s: %m\n", |
| 1686 |
opt_loop_read_only ? "ro" : "rw", |
| 1687 |
opt_loop_file); |
| 1688 |
goto out; |
| 1689 |
} |
| 1690 |
fd_list = g_unix_fd_list_new_from_array (&fd, 1); /* adopts the fd */ |
| 1691 |
|
| 1692 |
setup_try_again: |
| 1693 |
error = NULL; |
| 1694 |
rc = udisks_manager_call_loop_setup_sync (udisks_client_get_manager (client), |
| 1695 |
g_variant_new_handle (0), |
| 1696 |
options, |
| 1697 |
fd_list, |
| 1698 |
&resulting_object_path, |
| 1699 |
NULL, /* out_fd_list */ |
| 1700 |
NULL, /* GCancellable */ |
| 1701 |
&error); |
| 1702 |
if (!rc) |
| 1703 |
{ |
| 1704 |
if (error->domain == UDISKS_ERROR && |
| 1705 |
error->code == UDISKS_ERROR_NOT_AUTHORIZED_CAN_OBTAIN && |
| 1706 |
setup_local_polkit_agent ()) |
| 1707 |
{ |
| 1708 |
g_clear_error (&error); |
| 1709 |
goto setup_try_again; |
| 1710 |
} |
| 1711 |
g_object_unref (fd_list); |
| 1712 |
g_printerr ("Error setting up loop device for %s: %s\n", |
| 1713 |
opt_loop_file, |
| 1714 |
error->message); |
| 1715 |
g_clear_error (&error); |
| 1716 |
goto out; |
| 1717 |
} |
| 1718 |
g_object_unref (fd_list); |
| 1719 |
udisks_client_settle (client); |
| 1720 |
|
| 1721 |
resulting_object = UDISKS_OBJECT (g_dbus_object_manager_get_object (udisks_client_get_object_manager (client), |
| 1722 |
(resulting_object_path))); |
| 1723 |
g_print ("Mapped file %s as %s.\n", |
| 1724 |
opt_loop_file, |
| 1725 |
udisks_block_get_device (udisks_object_get_block (resulting_object))); |
| 1726 |
g_object_unref (resulting_object); |
| 1727 |
g_free (resulting_object_path); |
| 1728 |
} |
| 1729 |
else |
| 1730 |
{ |
| 1731 |
if (opt_loop_object_path != NULL) |
| 1732 |
{ |
| 1733 |
object = lookup_object_by_path (opt_loop_object_path); |
| 1734 |
if (object == NULL) |
| 1735 |
{ |
| 1736 |
g_printerr ("Error looking up object with path %s\n", opt_loop_object_path); |
| 1737 |
goto out; |
| 1738 |
} |
| 1739 |
} |
| 1740 |
else if (opt_loop_device != NULL) |
| 1741 |
{ |
| 1742 |
object = lookup_object_by_device (opt_loop_device); |
| 1743 |
if (object == NULL) |
| 1744 |
{ |
| 1745 |
g_printerr ("Error looking up object for device %s\n", opt_loop_device); |
| 1746 |
goto out; |
| 1747 |
} |
| 1748 |
} |
| 1749 |
else |
| 1750 |
{ |
| 1751 |
s = g_option_context_get_help (o, FALSE, NULL); |
| 1752 |
g_printerr ("%s", s); |
| 1753 |
g_free (s); |
| 1754 |
goto out; |
| 1755 |
} |
| 1756 |
|
| 1757 |
if (udisks_object_peek_loop (object) == NULL) |
| 1758 |
{ |
| 1759 |
g_printerr ("Error: specified object is not a loop device\n"); |
| 1760 |
goto out; |
| 1761 |
} |
| 1762 |
|
| 1763 |
delete_try_again: |
| 1764 |
error = NULL; |
| 1765 |
if (!udisks_loop_call_delete_sync (udisks_object_peek_loop (object), |
| 1766 |
options, |
| 1767 |
NULL, /* GCancellable */ |
| 1768 |
&error)) |
| 1769 |
{ |
| 1770 |
if (error->domain == UDISKS_ERROR && |
| 1771 |
error->code == UDISKS_ERROR_NOT_AUTHORIZED_CAN_OBTAIN && |
| 1772 |
setup_local_polkit_agent ()) |
| 1773 |
{ |
| 1774 |
g_clear_error (&error); |
| 1775 |
goto delete_try_again; |
| 1776 |
} |
| 1777 |
g_printerr ("Error deleting loop device %s: %s\n", |
| 1778 |
udisks_block_get_device (udisks_object_peek_block (object)), |
| 1779 |
error->message); |
| 1780 |
g_clear_error (&error); |
| 1781 |
goto out; |
| 1782 |
} |
| 1783 |
g_object_unref (object); |
| 1784 |
} |
| 1785 |
|
| 1786 |
ret = 0; |
| 1787 |
|
| 1788 |
out: |
| 1789 |
if (options != NULL) |
| 1790 |
g_variant_unref (options); |
| 1791 |
g_option_context_free (o); |
| 1792 |
g_free (opt_loop_file); |
| 1793 |
g_free (opt_loop_object_path); |
| 1794 |
g_free (opt_loop_device); |
| 1795 |
return ret; |
| 1796 |
} |
| 1797 |
|
| 1798 |
/* ---------------------------------------------------------------------------------------------------- */ |
| 1799 |
|
| 1800 |
static gchar *opt_smart_simulate_file = NULL; |
| 1801 |
static gchar *opt_smart_simulate_object_path = NULL; |
| 1802 |
static gchar *opt_smart_simulate_device = NULL; |
| 1803 |
static gboolean opt_smart_simulate_no_user_interaction = FALSE; |
| 1804 |
|
| 1805 |
static const GOptionEntry command_smart_simulate_entries[] = |
| 1806 |
{ |
| 1807 |
{ |
| 1808 |
"file", |
| 1809 |
'f', |
| 1810 |
0, |
| 1811 |
G_OPTION_ARG_FILENAME, |
| 1812 |
&opt_smart_simulate_file, |
| 1813 |
"File with libatasmart blob", |
| 1814 |
NULL |
| 1815 |
}, |
| 1816 |
{ |
| 1817 |
"object-path", |
| 1818 |
'p', |
| 1819 |
0, |
| 1820 |
G_OPTION_ARG_STRING, |
| 1821 |
&opt_smart_simulate_object_path, |
| 1822 |
"Object path for ATA device", |
| 1823 |
NULL |
| 1824 |
}, |
| 1825 |
{ |
| 1826 |
"block-device", |
| 1827 |
'b', |
| 1828 |
0, |
| 1829 |
G_OPTION_ARG_STRING, |
| 1830 |
&opt_smart_simulate_device, |
| 1831 |
"Device file for ATA device", |
| 1832 |
NULL |
| 1833 |
}, |
| 1834 |
{ |
| 1835 |
"no-user-interaction", |
| 1836 |
0, /* no short option */ |
| 1837 |
0, |
| 1838 |
G_OPTION_ARG_NONE, |
| 1839 |
&opt_smart_simulate_no_user_interaction, |
| 1840 |
"Do not authenticate the user if needed", |
| 1841 |
NULL |
| 1842 |
}, |
| 1843 |
{ |
| 1844 |
NULL |
| 1845 |
} |
| 1846 |
}; |
| 1847 |
|
| 1848 |
static gint |
| 1849 |
handle_command_smart_simulate (gint *argc, |
| 1850 |
gchar **argv[], |
| 1851 |
gboolean request_completion, |
| 1852 |
const gchar *completion_cur, |
| 1853 |
const gchar *completion_prev) |
| 1854 |
{ |
| 1855 |
gint ret; |
| 1856 |
GOptionContext *o; |
| 1857 |
gchar *s; |
| 1858 |
gboolean complete_objects; |
| 1859 |
gboolean complete_devices; |
| 1860 |
gboolean complete_files; |
| 1861 |
GList *l; |
| 1862 |
GList *objects; |
| 1863 |
UDisksObject *object; |
| 1864 |
UDisksDriveAta *ata; |
| 1865 |
guint n; |
| 1866 |
GVariant *options; |
| 1867 |
GVariantBuilder builder; |
| 1868 |
GError *error; |
| 1869 |
|
| 1870 |
ret = 1; |
| 1871 |
opt_smart_simulate_object_path = NULL; |
| 1872 |
opt_smart_simulate_device = NULL; |
| 1873 |
object = NULL; |
| 1874 |
options = NULL; |
| 1875 |
|
| 1876 |
modify_argv0_for_command (argc, argv, "smart-simulate"); |
| 1877 |
|
| 1878 |
o = g_option_context_new (NULL); |
| 1879 |
if (request_completion) |
| 1880 |
g_option_context_set_ignore_unknown_options (o, TRUE); |
| 1881 |
g_option_context_set_help_enabled (o, FALSE); |
| 1882 |
g_option_context_set_summary (o, "Set SMART data for drive."); |
| 1883 |
g_option_context_add_main_entries (o, |
| 1884 |
command_smart_simulate_entries, |
| 1885 |
NULL /* GETTEXT_PACKAGE*/); |
| 1886 |
|
| 1887 |
complete_objects = FALSE; |
| 1888 |
if (request_completion && (g_strcmp0 (completion_prev, "--object-path") == 0 || g_strcmp0 (completion_prev, "-p") == 0 || |
| 1889 |
g_strcmp0 (completion_cur, "--object-path") == 0 || g_strcmp0 (completion_cur, "-p") == 0)) |
| 1890 |
{ |
| 1891 |
complete_objects = TRUE; |
| 1892 |
remove_arg ((*argc) - 1, argc, argv); |
| 1893 |
} |
| 1894 |
|
| 1895 |
complete_devices = FALSE; |
| 1896 |
if (request_completion && (g_strcmp0 (completion_prev, "--block-device") == 0 || g_strcmp0 (completion_prev, "-b") == 0 || |
| 1897 |
g_strcmp0 (completion_cur, "--block-device") == 0 || g_strcmp0 (completion_cur, "-b") == 0)) |
| 1898 |
{ |
| 1899 |
complete_devices = TRUE; |
| 1900 |
remove_arg ((*argc) - 1, argc, argv); |
| 1901 |
} |
| 1902 |
|
| 1903 |
complete_files = FALSE; |
| 1904 |
if (request_completion && (g_strcmp0 (completion_prev, "--file") == 0 || g_strcmp0 (completion_prev, "-f") == 0 || |
| 1905 |
g_strcmp0 (completion_cur, "--file") == 0 || g_strcmp0 (completion_cur, "-f") == 0)) |
| 1906 |
{ |
| 1907 |
complete_files = TRUE; |
| 1908 |
remove_arg ((*argc) - 1, argc, argv); |
| 1909 |
} |
| 1910 |
|
| 1911 |
if (!g_option_context_parse (o, argc, argv, NULL)) |
| 1912 |
{ |
| 1913 |
if (!request_completion) |
| 1914 |
{ |
| 1915 |
s = g_option_context_get_help (o, FALSE, NULL); |
| 1916 |
g_printerr ("%s", s); |
| 1917 |
g_free (s); |
| 1918 |
goto out; |
| 1919 |
} |
| 1920 |
} |
| 1921 |
|
| 1922 |
if (request_completion) |
| 1923 |
{ |
| 1924 |
if (complete_files) |
| 1925 |
{ |
| 1926 |
g_print ("@FILES@"); |
| 1927 |
goto out; |
| 1928 |
} |
| 1929 |
|
| 1930 |
if (!complete_objects && !complete_devices) |
| 1931 |
{ |
| 1932 |
list_options (command_smart_simulate_entries); |
| 1933 |
} |
| 1934 |
|
| 1935 |
if (complete_objects) |
| 1936 |
{ |
| 1937 |
const gchar *object_path; |
| 1938 |
objects = g_dbus_object_manager_get_objects (udisks_client_get_object_manager (client)); |
| 1939 |
for (l = objects; l != NULL; l = l->next) |
| 1940 |
{ |
| 1941 |
object = UDISKS_OBJECT (l->data); |
| 1942 |
ata = udisks_object_peek_drive_ata (object); |
| 1943 |
if (ata != NULL) |
| 1944 |
{ |
| 1945 |
object_path = g_dbus_object_get_object_path (G_DBUS_OBJECT (object)); |
| 1946 |
g_assert (g_str_has_prefix (object_path, "/org/freedesktop/UDisks2/")); |
| 1947 |
g_print ("%s \n", object_path + sizeof ("/org/freedesktop/UDisks2/") - 1); |
| 1948 |
} |
| 1949 |
} |
| 1950 |
g_list_free_full (objects, g_object_unref); |
| 1951 |
} |
| 1952 |
|
| 1953 |
if (complete_devices) |
| 1954 |
{ |
| 1955 |
objects = g_dbus_object_manager_get_objects (udisks_client_get_object_manager (client)); |
| 1956 |
for (l = objects; l != NULL; l = l->next) |
| 1957 |
{ |
| 1958 |
object = UDISKS_OBJECT (l->data); |
| 1959 |
ata = udisks_object_peek_drive_ata (object); |
| 1960 |
if (ata != NULL) |
| 1961 |
{ |
| 1962 |
UDisksBlock *block; |
| 1963 |
|
| 1964 |
block = udisks_client_get_block_for_drive (client, udisks_object_peek_drive (object), TRUE); |
| 1965 |
if (block != NULL) |
| 1966 |
{ |
| 1967 |
const gchar * const *symlinks; |
| 1968 |
|
| 1969 |
g_print ("%s \n", udisks_block_get_device (block)); |
| 1970 |
symlinks = udisks_block_get_symlinks (block); |
| 1971 |
for (n = 0; symlinks != NULL && symlinks[n] != NULL; n++) |
| 1972 |
g_print ("%s \n", symlinks[n]); |
| 1973 |
g_object_unref (block); |
| 1974 |
} |
| 1975 |
} |
| 1976 |
} |
| 1977 |
g_list_free_full (objects, g_object_unref); |
| 1978 |
} |
| 1979 |
goto out; |
| 1980 |
} |
| 1981 |
|
| 1982 |
if (opt_smart_simulate_file == NULL) |
| 1983 |
{ |
| 1984 |
s = g_option_context_get_help (o, FALSE, NULL); |
| 1985 |
g_printerr ("%s", s); |
| 1986 |
g_free (s); |
| 1987 |
goto out; |
| 1988 |
} |
| 1989 |
|
| 1990 |
g_variant_builder_init (&builder, G_VARIANT_TYPE_VARDICT); |
| 1991 |
if (opt_smart_simulate_no_user_interaction) |
| 1992 |
{ |
| 1993 |
g_variant_builder_add (&builder, |
| 1994 |
"{sv}", |
| 1995 |
"auth.no_user_interaction", g_variant_new_boolean (TRUE)); |
| 1996 |
} |
| 1997 |
g_variant_builder_add (&builder, |
| 1998 |
"{sv}", |
| 1999 |
"atasmart_blob", g_variant_new_string (opt_smart_simulate_file)); |
| 2000 |
options = g_variant_builder_end (&builder); |
| 2001 |
g_variant_ref_sink (options); |
| 2002 |
|
| 2003 |
if (opt_smart_simulate_object_path != NULL) |
| 2004 |
{ |
| 2005 |
object = lookup_object_by_path (opt_smart_simulate_object_path); |
| 2006 |
if (object == NULL) |
| 2007 |
{ |
| 2008 |
g_printerr ("Error looking up object with path %s\n", opt_smart_simulate_object_path); |
| 2009 |
goto out; |
| 2010 |
} |
| 2011 |
} |
| 2012 |
else if (opt_smart_simulate_device != NULL) |
| 2013 |
{ |
| 2014 |
UDisksObject *block_object; |
| 2015 |
UDisksDrive *drive; |
| 2016 |
|
| 2017 |
block_object = lookup_object_by_device (opt_smart_simulate_device); |
| 2018 |
if (block_object == NULL) |
| 2019 |
{ |
| 2020 |
g_printerr ("Error looking up object for device %s\n", opt_smart_simulate_device); |
| 2021 |
goto out; |
| 2022 |
} |
| 2023 |
drive = udisks_client_get_drive_for_block (client, udisks_object_peek_block (block_object)); |
| 2024 |
if (drive == NULL) |
| 2025 |
{ |
| 2026 |
g_printerr ("Error looking up drive for device %s\n", opt_smart_simulate_device); |
| 2027 |
g_object_unref (block_object); |
| 2028 |
goto out; |
| 2029 |
} |
| 2030 |
object = (UDisksObject *) g_dbus_interface_dup_object (G_DBUS_INTERFACE (drive)); |
| 2031 |
if (object == NULL) |
| 2032 |
{ |
| 2033 |
g_printerr ("Error looking up object for device %s\n", opt_smart_simulate_device); |
| 2034 |
g_object_unref (block_object); |
| 2035 |
goto out; |
| 2036 |
} |
| 2037 |
g_object_unref (block_object); |
| 2038 |
} |
| 2039 |
else |
| 2040 |
{ |
| 2041 |
s = g_option_context_get_help (o, FALSE, NULL); |
| 2042 |
g_printerr ("%s", s); |
| 2043 |
g_free (s); |
| 2044 |
goto out; |
| 2045 |
} |
| 2046 |
|
| 2047 |
if (udisks_object_peek_drive_ata (object) == NULL) |
| 2048 |
{ |
| 2049 |
UDisksBlock *block; |
| 2050 |
|
| 2051 |
block = udisks_object_peek_block (object); |
| 2052 |
g_printerr ("Device %s is not an ATA device\n", |
| 2053 |
block ? udisks_block_get_device (block) : "(not a block device)"); |
| 2054 |
g_object_unref (object); |
| 2055 |
goto out; |
| 2056 |
} |
| 2057 |
|
| 2058 |
try_again: |
| 2059 |
error = NULL; |
| 2060 |
if (!udisks_drive_ata_call_smart_update_sync (udisks_object_peek_drive_ata (object), |
| 2061 |
options, |
| 2062 |
NULL, /* GCancellable */ |
| 2063 |
&error)) |
| 2064 |
{ |
| 2065 |
if (error->domain == UDISKS_ERROR && |
| 2066 |
error->code == UDISKS_ERROR_NOT_AUTHORIZED_CAN_OBTAIN && |
| 2067 |
setup_local_polkit_agent ()) |
| 2068 |
{ |
| 2069 |
g_clear_error (&error); |
| 2070 |
goto try_again; |
| 2071 |
} |
| 2072 |
g_dbus_error_strip_remote_error (error); |
| 2073 |
g_printerr ("Error updating SMART data: %s (%s, %d)\n", |
| 2074 |
error->message, g_quark_to_string (error->domain), error->code); |
| 2075 |
g_clear_error (&error); |
| 2076 |
g_object_unref (object); |
| 2077 |
goto out; |
| 2078 |
} |
| 2079 |
|
| 2080 |
g_object_unref (object); |
| 2081 |
|
| 2082 |
|
| 2083 |
ret = 0; |
| 2084 |
|
| 2085 |
out: |
| 2086 |
if (options != NULL) |
| 2087 |
g_variant_unref (options); |
| 2088 |
g_option_context_free (o); |
| 2089 |
g_free (opt_smart_simulate_file); |
| 2090 |
g_free (opt_smart_simulate_object_path); |
| 2091 |
g_free (opt_smart_simulate_device); |
| 2092 |
return ret; |
| 2093 |
} |
| 2094 |
|
| 2095 |
/* ---------------------------------------------------------------------------------------------------- */ |
| 2096 |
|
| 2097 |
static gchar *opt_power_off_object_path = NULL; |
| 2098 |
static gchar *opt_power_off_device = NULL; |
| 2099 |
static gboolean opt_power_off_no_user_interaction = FALSE; |
| 2100 |
|
| 2101 |
static const GOptionEntry command_power_off_entries[] = |
| 2102 |
{ |
| 2103 |
{ |
| 2104 |
"object-path", |
| 2105 |
'p', |
| 2106 |
0, |
| 2107 |
G_OPTION_ARG_STRING, |
| 2108 |
&opt_power_off_object_path, |
| 2109 |
"Object path for ATA device", |
| 2110 |
NULL |
| 2111 |
}, |
| 2112 |
{ |
| 2113 |
"block-device", |
| 2114 |
'b', |
| 2115 |
0, |
| 2116 |
G_OPTION_ARG_STRING, |
| 2117 |
&opt_power_off_device, |
| 2118 |
"Device file for ATA device", |
| 2119 |
NULL |
| 2120 |
}, |
| 2121 |
{ |
| 2122 |
"no-user-interaction", |
| 2123 |
0, /* no short option */ |
| 2124 |
0, |
| 2125 |
G_OPTION_ARG_NONE, |
| 2126 |
&opt_power_off_no_user_interaction, |
| 2127 |
"Do not authenticate the user if needed", |
| 2128 |
NULL |
| 2129 |
}, |
| 2130 |
{ |
| 2131 |
NULL |
| 2132 |
} |
| 2133 |
}; |
| 2134 |
|
| 2135 |
static gint |
| 2136 |
handle_command_power_off (gint *argc, |
| 2137 |
gchar **argv[], |
| 2138 |
gboolean request_completion, |
| 2139 |
const gchar *completion_cur, |
| 2140 |
const gchar *completion_prev) |
| 2141 |
{ |
| 2142 |
gint ret; |
| 2143 |
GOptionContext *o; |
| 2144 |
gchar *s; |
| 2145 |
gboolean complete_objects; |
| 2146 |
gboolean complete_devices; |
| 2147 |
GList *l; |
| 2148 |
GList *objects; |
| 2149 |
UDisksObject *object; |
| 2150 |
UDisksDrive *drv; |
| 2151 |
guint n; |
| 2152 |
GVariant *options; |
| 2153 |
GVariantBuilder builder; |
| 2154 |
GError *error; |
| 2155 |
UDisksDrive *proxy; |
| 2156 |
|
| 2157 |
ret = 1; |
| 2158 |
opt_power_off_object_path = NULL; |
| 2159 |
opt_power_off_device = NULL; |
| 2160 |
object = NULL; |
| 2161 |
options = NULL; |
| 2162 |
|
| 2163 |
modify_argv0_for_command (argc, argv, "power-off"); |
| 2164 |
|
| 2165 |
o = g_option_context_new (NULL); |
| 2166 |
if (request_completion) |
| 2167 |
g_option_context_set_ignore_unknown_options (o, TRUE); |
| 2168 |
g_option_context_set_help_enabled (o, FALSE); |
| 2169 |
g_option_context_set_summary (o, "Safely power off a drive."); |
| 2170 |
g_option_context_add_main_entries (o, |
| 2171 |
command_power_off_entries, |
| 2172 |
NULL /* GETTEXT_PACKAGE*/); |
| 2173 |
|
| 2174 |
complete_objects = FALSE; |
| 2175 |
if (request_completion && (g_strcmp0 (completion_prev, "--object-path") == 0 || g_strcmp0 (completion_prev, "-p") == 0 || |
| 2176 |
g_strcmp0 (completion_cur, "--object-path") == 0 || g_strcmp0 (completion_cur, "-p") == 0)) |
| 2177 |
{ |
| 2178 |
complete_objects = TRUE; |
| 2179 |
remove_arg ((*argc) - 1, argc, argv); |
| 2180 |
} |
| 2181 |
|
| 2182 |
complete_devices = FALSE; |
| 2183 |
if (request_completion && (g_strcmp0 (completion_prev, "--block-device") == 0 || g_strcmp0 (completion_prev, "-b") == 0 || |
| 2184 |
g_strcmp0 (completion_cur, "--block-device") == 0 || g_strcmp0 (completion_cur, "-b") == 0)) |
| 2185 |
{ |
| 2186 |
complete_devices = TRUE; |
| 2187 |
remove_arg ((*argc) - 1, argc, argv); |
| 2188 |
} |
| 2189 |
|
| 2190 |
if (!g_option_context_parse (o, argc, argv, NULL)) |
| 2191 |
{ |
| 2192 |
if (!request_completion) |
| 2193 |
{ |
| 2194 |
s = g_option_context_get_help (o, FALSE, NULL); |
| 2195 |
g_printerr ("%s", s); |
| 2196 |
g_free (s); |
| 2197 |
goto out; |
| 2198 |
} |
| 2199 |
} |
| 2200 |
|
| 2201 |
if (request_completion) |
| 2202 |
{ |
| 2203 |
if (!complete_objects && !complete_devices) |
| 2204 |
{ |
| 2205 |
list_options (command_power_off_entries); |
| 2206 |
} |
| 2207 |
|
| 2208 |
if (complete_objects) |
| 2209 |
{ |
| 2210 |
const gchar *object_path; |
| 2211 |
objects = g_dbus_object_manager_get_objects (udisks_client_get_object_manager (client)); |
| 2212 |
for (l = objects; l != NULL; l = l->next) |
| 2213 |
{ |
| 2214 |
object = UDISKS_OBJECT (l->data); |
| 2215 |
drv = udisks_object_peek_drive (object); |
| 2216 |
if (drv != NULL) |
| 2217 |
{ |
| 2218 |
if (udisks_drive_get_can_power_off (drv)) |
| 2219 |
{ |
| 2220 |
object_path = g_dbus_object_get_object_path (G_DBUS_OBJECT (object)); |
| 2221 |
g_assert (g_str_has_prefix (object_path, "/org/freedesktop/UDisks2/")); |
| 2222 |
g_print ("%s \n", object_path + sizeof ("/org/freedesktop/UDisks2/") - 1); |
| 2223 |
} |
| 2224 |
} |
| 2225 |
} |
| 2226 |
g_list_free_full (objects, g_object_unref); |
| 2227 |
} |
| 2228 |
|
| 2229 |
if (complete_devices) |
| 2230 |
{ |
| 2231 |
objects = g_dbus_object_manager_get_objects (udisks_client_get_object_manager (client)); |
| 2232 |
for (l = objects; l != NULL; l = l->next) |
| 2233 |
{ |
| 2234 |
object = UDISKS_OBJECT (l->data); |
| 2235 |
drv = udisks_object_peek_drive (object); |
| 2236 |
if (drv != NULL) |
| 2237 |
{ |
| 2238 |
if (udisks_drive_get_can_power_off (drv)) |
| 2239 |
{ |
| 2240 |
UDisksBlock *block; |
| 2241 |
|
| 2242 |
block = udisks_client_get_block_for_drive (client, drv, TRUE); |
| 2243 |
if (block != NULL) |
| 2244 |
{ |
| 2245 |
const gchar * const *symlinks; |
| 2246 |
|
| 2247 |
g_print ("%s \n", udisks_block_get_device (block)); |
| 2248 |
symlinks = udisks_block_get_symlinks (block); |
| 2249 |
for (n = 0; symlinks != NULL && symlinks[n] != NULL; n++) |
| 2250 |
g_print ("%s \n", symlinks[n]); |
| 2251 |
g_object_unref (block); |
| 2252 |
} |
| 2253 |
} |
| 2254 |
} |
| 2255 |
} |
| 2256 |
g_list_free_full (objects, g_object_unref); |
| 2257 |
} |
| 2258 |
goto out; |
| 2259 |
} |
| 2260 |
|
| 2261 |
g_variant_builder_init (&builder, G_VARIANT_TYPE_VARDICT); |
| 2262 |
if (opt_power_off_no_user_interaction) |
| 2263 |
{ |
| 2264 |
g_variant_builder_add (&builder, |
| 2265 |
"{sv}", |
| 2266 |
"auth.no_user_interaction", g_variant_new_boolean (TRUE)); |
| 2267 |
} |
| 2268 |
options = g_variant_builder_end (&builder); |
| 2269 |
g_variant_ref_sink (options); |
| 2270 |
|
| 2271 |
if (opt_power_off_object_path != NULL) |
| 2272 |
{ |
| 2273 |
object = lookup_object_by_path (opt_power_off_object_path); |
| 2274 |
if (object == NULL) |
| 2275 |
{ |
| 2276 |
g_printerr ("Error looking up object with path %s\n", opt_power_off_object_path); |
| 2277 |
goto out; |
| 2278 |
} |
| 2279 |
} |
| 2280 |
else if (opt_power_off_device != NULL) |
| 2281 |
{ |
| 2282 |
UDisksObject *block_object; |
| 2283 |
UDisksDrive *drive; |
| 2284 |
|
| 2285 |
block_object = lookup_object_by_device (opt_power_off_device); |
| 2286 |
if (block_object == NULL) |
| 2287 |
{ |
| 2288 |
g_printerr ("Error looking up object for device %s\n", opt_power_off_device); |
| 2289 |
goto out; |
| 2290 |
} |
| 2291 |
drive = udisks_client_get_drive_for_block (client, udisks_object_peek_block (block_object)); |
| 2292 |
if (drive == NULL) |
| 2293 |
{ |
| 2294 |
g_printerr ("Error looking up drive for device %s\n", opt_power_off_device); |
| 2295 |
g_object_unref (block_object); |
| 2296 |
goto out; |
| 2297 |
} |
| 2298 |
object = (UDisksObject *) g_dbus_interface_dup_object (G_DBUS_INTERFACE (drive)); |
| 2299 |
if (object == NULL) |
| 2300 |
{ |
| 2301 |
g_printerr ("Error looking up object for device %s\n", opt_power_off_device); |
| 2302 |
g_object_unref (block_object); |
| 2303 |
goto out; |
| 2304 |
} |
| 2305 |
g_object_unref (block_object); |
| 2306 |
} |
| 2307 |
else |
| 2308 |
{ |
| 2309 |
s = g_option_context_get_help (o, FALSE, NULL); |
| 2310 |
g_printerr ("%s", s); |
| 2311 |
g_free (s); |
| 2312 |
goto out; |
| 2313 |
} |
| 2314 |
|
| 2315 |
try_again: |
| 2316 |
error = NULL; |
| 2317 |
|
| 2318 |
proxy = udisks_object_peek_drive (object); |
| 2319 |
if (!proxy) |
| 2320 |
{ |
| 2321 |
g_printerr ("Error powering off drive: dbus interface not supported"); |
| 2322 |
g_object_unref (object); |
| 2323 |
goto out; |
| 2324 |
} |
| 2325 |
|
| 2326 |
if (!udisks_drive_call_power_off_sync (proxy, |
| 2327 |
options, |
| 2328 |
NULL, /* GCancellable */ |
| 2329 |
&error)) |
| 2330 |
{ |
| 2331 |
if (error->domain == UDISKS_ERROR && |
| 2332 |
error->code == UDISKS_ERROR_NOT_AUTHORIZED_CAN_OBTAIN && |
| 2333 |
setup_local_polkit_agent ()) |
| 2334 |
{ |
| 2335 |
g_clear_error (&error); |
| 2336 |
goto try_again; |
| 2337 |
} |
| 2338 |
g_dbus_error_strip_remote_error (error); |
| 2339 |
g_printerr ("Error powering off drive: %s (%s, %d)\n", |
| 2340 |
error->message, g_quark_to_string (error->domain), error->code); |
| 2341 |
g_clear_error (&error); |
| 2342 |
g_object_unref (object); |
| 2343 |
goto out; |
| 2344 |
} |
| 2345 |
|
| 2346 |
g_object_unref (object); |
| 2347 |
|
| 2348 |
|
| 2349 |
ret = 0; |
| 2350 |
|
| 2351 |
out: |
| 2352 |
if (options != NULL) |
| 2353 |
g_variant_unref (options); |
| 2354 |
g_option_context_free (o); |
| 2355 |
g_free (opt_power_off_object_path); |
| 2356 |
g_free (opt_power_off_device); |
| 2357 |
return ret; |
| 2358 |
} |
| 2359 |
|
| 2360 |
/* ---------------------------------------------------------------------------------------------------- */ |
| 2361 |
|
| 2362 |
static gchar *opt_info_object = NULL; |
| 2363 |
static gchar *opt_info_device = NULL; |
| 2364 |
static gchar *opt_info_drive = NULL; |
| 2365 |
|
| 2366 |
static const GOptionEntry command_info_entries[] = |
| 2367 |
{ |
| 2368 |
{ "object-path", 'p', 0, G_OPTION_ARG_STRING, &opt_info_object, "Object to get information about", NULL}, |
| 2369 |
{ "block-device", 'b', 0, G_OPTION_ARG_STRING, &opt_info_device, "Block device to get information about", NULL}, |
| 2370 |
{ "drive", 'd', 0, G_OPTION_ARG_STRING, &opt_info_drive, "Drive to get information about", NULL}, |
| 2371 |
{ NULL } |
| 2372 |
}; |
| 2373 |
|
| 2374 |
static gint |
| 2375 |
handle_command_info (gint *argc, |
| 2376 |
gchar **argv[], |
| 2377 |
gboolean request_completion, |
| 2378 |
const gchar *completion_cur, |
| 2379 |
const gchar *completion_prev) |
| 2380 |
{ |
| 2381 |
gint ret; |
| 2382 |
GOptionContext *o; |
| 2383 |
gchar *s; |
| 2384 |
gboolean complete_objects; |
| 2385 |
gboolean complete_devices; |
| 2386 |
gboolean complete_drives; |
| 2387 |
GList *l; |
| 2388 |
GList *objects; |
| 2389 |
UDisksObject *object; |
| 2390 |
UDisksBlock *block; |
| 2391 |
UDisksDrive *drive; |
| 2392 |
guint n; |
| 2393 |
|
| 2394 |
ret = 1; |
| 2395 |
opt_info_object = NULL; |
| 2396 |
opt_info_device = NULL; |
| 2397 |
opt_info_drive = NULL; |
| 2398 |
|
| 2399 |
modify_argv0_for_command (argc, argv, "info"); |
| 2400 |
|
| 2401 |
o = g_option_context_new (NULL); |
| 2402 |
if (request_completion) |
| 2403 |
g_option_context_set_ignore_unknown_options (o, TRUE); |
| 2404 |
g_option_context_set_help_enabled (o, FALSE); |
| 2405 |
g_option_context_set_summary (o, "Show information about an object."); |
| 2406 |
g_option_context_add_main_entries (o, command_info_entries, NULL /* GETTEXT_PACKAGE*/); |
| 2407 |
|
| 2408 |
complete_objects = FALSE; |
| 2409 |
if (request_completion && (g_strcmp0 (completion_prev, "--object-path") == 0 || g_strcmp0 (completion_prev, "-p") == 0 || |
| 2410 |
g_strcmp0 (completion_cur, "--object-path") == 0 || g_strcmp0 (completion_cur, "-p") == 0)) |
| 2411 |
{ |
| 2412 |
complete_objects = TRUE; |
| 2413 |
remove_arg ((*argc) - 1, argc, argv); |
| 2414 |
} |
| 2415 |
|
| 2416 |
complete_devices = FALSE; |
| 2417 |
if (request_completion && (g_strcmp0 (completion_prev, "--block-device") == 0 || g_strcmp0 (completion_prev, "-b") == 0 || |
| 2418 |
g_strcmp0 (completion_cur, "--block-device") == 0 || g_strcmp0 (completion_cur, "-b") == 0)) |
| 2419 |
{ |
| 2420 |
complete_devices = TRUE; |
| 2421 |
remove_arg ((*argc) - 1, argc, argv); |
| 2422 |
} |
| 2423 |
|
| 2424 |
complete_drives = FALSE; |
| 2425 |
if (request_completion && (g_strcmp0 (completion_prev, "--drive") == 0 || g_strcmp0 (completion_prev, "-d") == 0 || |
| 2426 |
g_strcmp0 (completion_cur, "--drive") == 0 || g_strcmp0 (completion_cur, "-d") == 0)) |
| 2427 |
{ |
| 2428 |
complete_drives = TRUE; |
| 2429 |
remove_arg ((*argc) - 1, argc, argv); |
| 2430 |
} |
| 2431 |
|
| 2432 |
if (!g_option_context_parse (o, argc, argv, NULL)) |
| 2433 |
{ |
| 2434 |
if (!request_completion) |
| 2435 |
{ |
| 2436 |
s = g_option_context_get_help (o, FALSE, NULL); |
| 2437 |
g_printerr ("%s", s); |
| 2438 |
g_free (s); |
| 2439 |
goto out; |
| 2440 |
} |
| 2441 |
} |
| 2442 |
|
| 2443 |
if (request_completion && |
| 2444 |
!complete_objects && |
| 2445 |
!complete_devices && |
| 2446 |
!complete_drives) |
| 2447 |
{ |
| 2448 |
list_options (command_info_entries); |
| 2449 |
} |
| 2450 |
|
| 2451 |
if (complete_objects) |
| 2452 |
{ |
| 2453 |
const gchar *object_path; |
| 2454 |
|
| 2455 |
objects = g_dbus_object_manager_get_objects (udisks_client_get_object_manager (client)); |
| 2456 |
for (l = objects; l != NULL; l = l->next) |
| 2457 |
{ |
| 2458 |
object = UDISKS_OBJECT (l->data); |
| 2459 |
|
| 2460 |
object_path = g_dbus_object_get_object_path (G_DBUS_OBJECT (object)); |
| 2461 |
g_assert (g_str_has_prefix (object_path, "/org/freedesktop/UDisks2/")); |
| 2462 |
g_print ("%s \n", object_path + sizeof ("/org/freedesktop/UDisks2/") - 1); |
| 2463 |
} |
| 2464 |
g_list_free_full (objects, g_object_unref); |
| 2465 |
goto out; |
| 2466 |
} |
| 2467 |
|
| 2468 |
if (complete_devices) |
| 2469 |
{ |
| 2470 |
objects = g_dbus_object_manager_get_objects (udisks_client_get_object_manager (client)); |
| 2471 |
for (l = objects; l != NULL; l = l->next) |
| 2472 |
{ |
| 2473 |
object = UDISKS_OBJECT (l->data); |
| 2474 |
block = udisks_object_peek_block (object); |
| 2475 |
if (block != NULL) |
| 2476 |
{ |
| 2477 |
const gchar * const *symlinks; |
| 2478 |
g_print ("%s \n", udisks_block_get_device (block)); |
| 2479 |
symlinks = udisks_block_get_symlinks (block); |
| 2480 |
for (n = 0; symlinks != NULL && symlinks[n] != NULL; n++) |
| 2481 |
g_print ("%s \n", symlinks[n]); |
| 2482 |
} |
| 2483 |
} |
| 2484 |
g_list_free_full (objects, g_object_unref); |
| 2485 |
goto out; |
| 2486 |
} |
| 2487 |
|
| 2488 |
if (complete_drives) |
| 2489 |
{ |
| 2490 |
objects = g_dbus_object_manager_get_objects (udisks_client_get_object_manager (client)); |
| 2491 |
for (l = objects; l != NULL; l = l->next) |
| 2492 |
{ |
| 2493 |
object = UDISKS_OBJECT (l->data); |
| 2494 |
drive = udisks_object_peek_drive (object); |
| 2495 |
if (drive != NULL) |
| 2496 |
{ |
| 2497 |
const gchar *base; |
| 2498 |
base = g_strrstr (g_dbus_object_get_object_path (G_DBUS_OBJECT (object)), "/") + 1; |
| 2499 |
g_print ("%s \n", base); |
| 2500 |
} |
| 2501 |
} |
| 2502 |
g_list_free_full (objects, g_object_unref); |
| 2503 |
goto out; |
| 2504 |
} |
| 2505 |
|
| 2506 |
/* done with completion */ |
| 2507 |
if (request_completion) |
| 2508 |
goto out; |
| 2509 |
|
| 2510 |
|
| 2511 |
if (opt_info_object != NULL) |
| 2512 |
{ |
| 2513 |
object = lookup_object_by_path (opt_info_object); |
| 2514 |
if (object == NULL) |
| 2515 |
{ |
| 2516 |
g_printerr ("Error looking up object with path %s\n", opt_info_object); |
| 2517 |
goto out; |
| 2518 |
} |
| 2519 |
} |
| 2520 |
else if (opt_info_device != NULL) |
| 2521 |
{ |
| 2522 |
object = lookup_object_by_device (opt_info_device); |
| 2523 |
if (object == NULL) |
| 2524 |
{ |
| 2525 |
g_printerr ("Error looking up object for device %s\n", opt_info_device); |
| 2526 |
goto out; |
| 2527 |
} |
| 2528 |
} |
| 2529 |
else if (opt_info_drive != NULL) |
| 2530 |
{ |
| 2531 |
object = lookup_object_by_drive (opt_info_drive); |
| 2532 |
if (object == NULL) |
| 2533 |
{ |
| 2534 |
g_printerr ("Error looking up object for drive %s\n", opt_info_drive); |
| 2535 |
goto out; |
| 2536 |
} |
| 2537 |
} |
| 2538 |
else |
| 2539 |
{ |
| 2540 |
s = g_option_context_get_help (o, FALSE, NULL); |
| 2541 |
g_printerr ("%s", s); |
| 2542 |
g_free (s); |
| 2543 |
goto out; |
| 2544 |
} |
| 2545 |
|
| 2546 |
g_print ("%s%s%s:%s\n", |
| 2547 |
_color_get (_COLOR_BOLD_ON), _color_get (_COLOR_FG_BLUE), g_dbus_object_get_object_path (G_DBUS_OBJECT (object)), _color_get (_COLOR_RESET)); |
| 2548 |
print_object (object, 2); |
| 2549 |
g_object_unref (object); |
| 2550 |
|
| 2551 |
ret = 0; |
| 2552 |
|
| 2553 |
out: |
| 2554 |
g_option_context_free (o); |
| 2555 |
g_free (opt_info_object); |
| 2556 |
g_free (opt_info_device); |
| 2557 |
g_free (opt_info_drive); |
| 2558 |
return ret; |
| 2559 |
} |
| 2560 |
|
| 2561 |
/* ---------------------------------------------------------------------------------------------------- */ |
| 2562 |
|
| 2563 |
|
| 2564 |
static gint |
| 2565 |
obj_proxy_cmp (GDBusObject *a, |
| 2566 |
GDBusObject *b) |
| 2567 |
{ |
| 2568 |
return g_strcmp0 (g_dbus_object_get_object_path (a), g_dbus_object_get_object_path (b)); |
| 2569 |
} |
| 2570 |
|
| 2571 |
static gint |
| 2572 |
obj_proxy_drive_sortkey_cmp (GDBusObject *a, |
| 2573 |
GDBusObject *b) |
| 2574 |
{ |
| 2575 |
UDisksDrive *da = udisks_object_peek_drive (UDISKS_OBJECT (a)); |
| 2576 |
UDisksDrive *db = udisks_object_peek_drive (UDISKS_OBJECT (b)); |
| 2577 |
if (da != NULL && db != NULL) |
| 2578 |
return g_strcmp0 (udisks_drive_get_sort_key (da), udisks_drive_get_sort_key (db)); |
| 2579 |
else |
| 2580 |
return obj_proxy_cmp (a, b); |
| 2581 |
} |
| 2582 |
|
| 2583 |
|
| 2584 |
static const GOptionEntry command_dump_entries[] = |
| 2585 |
{ |
| 2586 |
{ NULL } |
| 2587 |
}; |
| 2588 |
|
| 2589 |
static gint |
| 2590 |
handle_command_dump (gint *argc, |
| 2591 |
gchar **argv[], |
| 2592 |
gboolean request_completion, |
| 2593 |
const gchar *completion_cur, |
| 2594 |
const gchar *completion_prev) |
| 2595 |
{ |
| 2596 |
gint ret; |
| 2597 |
GOptionContext *o; |
| 2598 |
GList *l; |
| 2599 |
GList *objects; |
| 2600 |
gchar *s; |
| 2601 |
gboolean first; |
| 2602 |
|
| 2603 |
ret = 1; |
| 2604 |
|
| 2605 |
modify_argv0_for_command (argc, argv, "dump"); |
| 2606 |
|
| 2607 |
o = g_option_context_new (NULL); |
| 2608 |
if (request_completion) |
| 2609 |
g_option_context_set_ignore_unknown_options (o, TRUE); |
| 2610 |
g_option_context_set_help_enabled (o, FALSE); |
| 2611 |
g_option_context_set_summary (o, "Show information about all objects."); |
| 2612 |
g_option_context_add_main_entries (o, command_dump_entries, NULL /* GETTEXT_PACKAGE*/); |
| 2613 |
|
| 2614 |
if (!g_option_context_parse (o, argc, argv, NULL)) |
| 2615 |
{ |
| 2616 |
if (!request_completion) |
| 2617 |
{ |
| 2618 |
s = g_option_context_get_help (o, FALSE, NULL); |
| 2619 |
g_printerr ("%s", s); |
| 2620 |
g_free (s); |
| 2621 |
goto out; |
| 2622 |
} |
| 2623 |
} |
| 2624 |
|
| 2625 |
/* done with completion */ |
| 2626 |
if (request_completion) |
| 2627 |
goto out; |
| 2628 |
|
| 2629 |
_color_run_pager (); |
| 2630 |
|
| 2631 |
objects = g_dbus_object_manager_get_objects (udisks_client_get_object_manager (client)); |
| 2632 |
/* We want to print the objects in order */ |
| 2633 |
objects = g_list_sort (objects, (GCompareFunc) obj_proxy_cmp); |
| 2634 |
first = TRUE; |
| 2635 |
for (l = objects; l != NULL; l = l->next) |
| 2636 |
{ |
| 2637 |
UDisksObject *object = UDISKS_OBJECT (l->data); |
| 2638 |
if (!first) |
| 2639 |
g_print ("\n"); |
| 2640 |
first = FALSE; |
| 2641 |
g_print ("%s%s%s:%s\n", |
| 2642 |
_color_get (_COLOR_BOLD_ON), _color_get (_COLOR_FG_BLUE), g_dbus_object_get_object_path (G_DBUS_OBJECT (object)), _color_get (_COLOR_RESET)); |
| 2643 |
print_object (object, 2); |
| 2644 |
} |
| 2645 |
g_list_free_full (objects, g_object_unref); |
| 2646 |
|
| 2647 |
ret = 0; |
| 2648 |
|
| 2649 |
/* explicitly close stdout so that pagers are aware of EOF */ |
| 2650 |
fclose (stdout); |
| 2651 |
|
| 2652 |
out: |
| 2653 |
g_option_context_free (o); |
| 2654 |
return ret; |
| 2655 |
} |
| 2656 |
|
| 2657 |
/* ---------------------------------------------------------------------------------------------------- */ |
| 2658 |
|
| 2659 |
static void |
| 2660 |
monitor_print_timestamp (void) |
| 2661 |
{ |
| 2662 |
GDateTime *now = g_date_time_new_now_local (); |
| 2663 |
gchar *time_str = g_date_time_format (now, "%H:%M:%S"); |
| 2664 |
|
| 2665 |
g_print ("%s%s%s.%03d:%s ", |
| 2666 |
_color_get (_COLOR_BOLD_ON), _color_get (_COLOR_FG_YELLOW), |
| 2667 |
time_str, (gint) g_date_time_get_microsecond (now) / 1000, |
| 2668 |
_color_get (_COLOR_RESET)); |
| 2669 |
|
| 2670 |
g_free (time_str); |
| 2671 |
g_date_time_unref (now); |
| 2672 |
} |
| 2673 |
|
| 2674 |
static gboolean |
| 2675 |
monitor_has_name_owner (void) |
| 2676 |
{ |
| 2677 |
gchar *name_owner; |
| 2678 |
gboolean ret; |
| 2679 |
name_owner = g_dbus_object_manager_client_get_name_owner (G_DBUS_OBJECT_MANAGER_CLIENT (udisks_client_get_object_manager (client))); |
| 2680 |
ret = (name_owner != NULL); |
| 2681 |
g_free (name_owner); |
| 2682 |
return ret; |
| 2683 |
} |
| 2684 |
|
| 2685 |
static void |
| 2686 |
monitor_print_name_owner (void) |
| 2687 |
{ |
| 2688 |
gchar *name_owner; |
| 2689 |
name_owner = g_dbus_object_manager_client_get_name_owner (G_DBUS_OBJECT_MANAGER_CLIENT (udisks_client_get_object_manager (client))); |
| 2690 |
monitor_print_timestamp (); |
| 2691 |
if (name_owner != NULL) |
| 2692 |
g_print ("The udisks-daemon is running (name-owner %s).\n", name_owner); |
| 2693 |
else |
| 2694 |
g_print ("The udisks-daemon is not running.\n"); |
| 2695 |
g_free (name_owner); |
| 2696 |
} |
| 2697 |
|
| 2698 |
static void |
| 2699 |
monitor_on_notify_name_owner (GObject *object, |
| 2700 |
GParamSpec *pspec, |
| 2701 |
gpointer user_data) |
| 2702 |
{ |
| 2703 |
monitor_print_name_owner (); |
| 2704 |
} |
| 2705 |
|
| 2706 |
static void |
| 2707 |
monitor_on_object_added (GDBusObjectManager *manager, |
| 2708 |
GDBusObject *object, |
| 2709 |
gpointer user_data) |
| 2710 |
{ |
| 2711 |
if (!monitor_has_name_owner ()) |
| 2712 |
goto out; |
| 2713 |
monitor_print_timestamp (); |
| 2714 |
g_print ("%s%sAdded %s%s\n", |
| 2715 |
_color_get (_COLOR_BOLD_ON), _color_get (_COLOR_FG_GREEN), |
| 2716 |
g_dbus_object_get_object_path (object), |
| 2717 |
_color_get (_COLOR_RESET)); |
| 2718 |
print_object (UDISKS_OBJECT (object), 2); |
| 2719 |
out: |
| 2720 |
; |
| 2721 |
} |
| 2722 |
|
| 2723 |
static void |
| 2724 |
monitor_on_object_removed (GDBusObjectManager *manager, |
| 2725 |
GDBusObject *object, |
| 2726 |
gpointer user_data) |
| 2727 |
{ |
| 2728 |
if (!monitor_has_name_owner ()) |
| 2729 |
goto out; |
| 2730 |
monitor_print_timestamp (); |
| 2731 |
g_print ("%s%sRemoved %s%s\n", |
| 2732 |
_color_get (_COLOR_BOLD_ON), _color_get (_COLOR_FG_RED), |
| 2733 |
g_dbus_object_get_object_path (object), |
| 2734 |
_color_get (_COLOR_RESET)); |
| 2735 |
out: |
| 2736 |
; |
| 2737 |
} |
| 2738 |
|
| 2739 |
static void |
| 2740 |
monitor_on_interface_proxy_added (GDBusObjectManager *manager, |
| 2741 |
GDBusObject *object, |
| 2742 |
GDBusInterface *interface, |
| 2743 |
gpointer user_data) |
| 2744 |
{ |
| 2745 |
if (!monitor_has_name_owner ()) |
| 2746 |
goto out; |
| 2747 |
monitor_print_timestamp (); |
| 2748 |
g_print ("%s%s%s:%s %s%sAdded interface %s%s\n", |
| 2749 |
_color_get (_COLOR_BOLD_ON), _color_get (_COLOR_FG_BLUE), |
| 2750 |
g_dbus_object_get_object_path (object), |
| 2751 |
_color_get (_COLOR_RESET), |
| 2752 |
_color_get (_COLOR_BOLD_ON), _color_get (_COLOR_FG_GREEN), |
| 2753 |
g_dbus_proxy_get_interface_name (G_DBUS_PROXY (interface)), |
| 2754 |
_color_get (_COLOR_RESET)); |
| 2755 |
|
| 2756 |
print_interface_properties (G_DBUS_PROXY (interface), 2); |
| 2757 |
out: |
| 2758 |
; |
| 2759 |
} |
| 2760 |
|
| 2761 |
static void |
| 2762 |
monitor_on_interface_proxy_removed (GDBusObjectManager *manager, |
| 2763 |
GDBusObject *object, |
| 2764 |
GDBusInterface *interface, |
| 2765 |
gpointer user_data) |
| 2766 |
{ |
| 2767 |
if (!monitor_has_name_owner ()) |
| 2768 |
goto out; |
| 2769 |
monitor_print_timestamp (); |
| 2770 |
g_print ("%s%s%s:%s %s%sRemoved interface %s%s\n", |
| 2771 |
_color_get (_COLOR_BOLD_ON), _color_get (_COLOR_FG_BLUE), |
| 2772 |
g_dbus_object_get_object_path (object), |
| 2773 |
_color_get (_COLOR_RESET), |
| 2774 |
_color_get (_COLOR_BOLD_ON), _color_get (_COLOR_FG_RED), |
| 2775 |
g_dbus_proxy_get_interface_name (G_DBUS_PROXY (interface)), |
| 2776 |
_color_get (_COLOR_RESET)); |
| 2777 |
out: |
| 2778 |
; |
| 2779 |
} |
| 2780 |
|
| 2781 |
static void |
| 2782 |
monitor_on_interface_proxy_properties_changed (GDBusObjectManagerClient *manager, |
| 2783 |
GDBusObjectProxy *object_proxy, |
| 2784 |
GDBusProxy *interface_proxy, |
| 2785 |
GVariant *changed_properties, |
| 2786 |
const gchar* const *invalidated_properties, |
| 2787 |
gpointer user_data) |
| 2788 |
{ |
| 2789 |
GVariantIter *iter; |
| 2790 |
const gchar *property_name; |
| 2791 |
GVariant *value; |
| 2792 |
guint max_property_name_len; |
| 2793 |
guint value_column; |
| 2794 |
|
| 2795 |
if (!monitor_has_name_owner ()) |
| 2796 |
goto out; |
| 2797 |
|
| 2798 |
monitor_print_timestamp (); |
| 2799 |
|
| 2800 |
g_print ("%s%s%s:%s %s%s%s:%s %s%sProperties Changed%s\n", |
| 2801 |
_color_get (_COLOR_BOLD_ON), _color_get (_COLOR_FG_BLUE), |
| 2802 |
g_dbus_object_get_object_path (G_DBUS_OBJECT (object_proxy)), |
| 2803 |
_color_get (_COLOR_RESET), |
| 2804 |
_color_get (_COLOR_BOLD_ON), _color_get (_COLOR_FG_MAGENTA), |
| 2805 |
g_dbus_proxy_get_interface_name (interface_proxy), |
| 2806 |
_color_get (_COLOR_RESET), |
| 2807 |
_color_get (_COLOR_BOLD_ON), _color_get (_COLOR_FG_YELLOW), |
| 2808 |
_color_get (_COLOR_RESET)); |
| 2809 |
|
| 2810 |
/* the daemon doesn't use the invalidated properties feature */ |
| 2811 |
g_warn_if_fail (g_strv_length ((gchar **) invalidated_properties) == 0); |
| 2812 |
|
| 2813 |
g_variant_get (changed_properties, "a{sv}", &iter); |
| 2814 |
max_property_name_len = 0; |
| 2815 |
while (g_variant_iter_next (iter, "{&sv}", &property_name, NULL)) |
| 2816 |
{ |
| 2817 |
guint property_name_len; |
| 2818 |
property_name_len = strlen (property_name); |
| 2819 |
if (max_property_name_len < property_name_len) |
| 2820 |
max_property_name_len = property_name_len; |
| 2821 |
} |
| 2822 |
|
| 2823 |
value_column = ((max_property_name_len + 7) / 8) * 8 + 8; |
| 2824 |
if (value_column < 24) |
| 2825 |
value_column = 24; |
| 2826 |
else if (value_column > 64) |
| 2827 |
value_column = 64; |
| 2828 |
|
| 2829 |
g_variant_get (changed_properties, "a{sv}", &iter); |
| 2830 |
while (g_variant_iter_next (iter, "{&sv}", &property_name, &value)) |
| 2831 |
{ |
| 2832 |
gchar *value_str; |
| 2833 |
guint rightmost; |
| 2834 |
gint value_indent; |
| 2835 |
|
| 2836 |
rightmost = 2 + strlen (property_name) + 2; |
| 2837 |
value_indent = value_column - rightmost; |
| 2838 |
if (value_indent < 0) |
| 2839 |
value_indent = 0; |
| 2840 |
|
| 2841 |
value_str = variant_to_string_with_indent (value, 2 + strlen (property_name) + 2 + value_indent); |
| 2842 |
|
| 2843 |
g_print (" %s%s:%s %*s%s\n", |
| 2844 |
_color_get (_COLOR_FG_WHITE), property_name, _color_get (_COLOR_RESET), |
| 2845 |
value_indent, "", |
| 2846 |
value_str); |
| 2847 |
|
| 2848 |
g_free (value_str); |
| 2849 |
g_variant_unref (value); |
| 2850 |
} |
| 2851 |
out: |
| 2852 |
; |
| 2853 |
} |
| 2854 |
|
| 2855 |
static void |
| 2856 |
monitor_on_interface_proxy_signal (GDBusObjectManagerClient *manager, |
| 2857 |
GDBusObjectProxy *object_proxy, |
| 2858 |
GDBusProxy *interface_proxy, |
| 2859 |
const gchar *sender_name, |
| 2860 |
const gchar *signal_name, |
| 2861 |
GVariant *parameters, |
| 2862 |
gpointer user_data) |
| 2863 |
{ |
| 2864 |
gchar *param_str; |
| 2865 |
if (!monitor_has_name_owner ()) |
| 2866 |
goto out; |
| 2867 |
|
| 2868 |
param_str = g_variant_print (parameters, TRUE); |
| 2869 |
monitor_print_timestamp (); |
| 2870 |
|
| 2871 |
g_print ("%s%s%s:%s %s%s%s%s%s%s::%s%s %s%s%s%s\n", |
| 2872 |
_color_get (_COLOR_BOLD_ON), _color_get (_COLOR_FG_BLUE), |
| 2873 |
g_dbus_object_get_object_path (G_DBUS_OBJECT (object_proxy)), |
| 2874 |
_color_get (_COLOR_RESET), |
| 2875 |
_color_get (_COLOR_BOLD_ON), _color_get (_COLOR_FG_MAGENTA), |
| 2876 |
g_dbus_proxy_get_interface_name (interface_proxy), |
| 2877 |
_color_get (_COLOR_RESET), |
| 2878 |
_color_get (_COLOR_BOLD_ON), _color_get (_COLOR_FG_YELLOW), |
| 2879 |
signal_name, |
| 2880 |
_color_get (_COLOR_RESET), |
| 2881 |
_color_get (_COLOR_BOLD_ON), _color_get (_COLOR_FG_WHITE), |
| 2882 |
param_str, |
| 2883 |
_color_get (_COLOR_RESET)); |
| 2884 |
g_free (param_str); |
| 2885 |
out: |
| 2886 |
; |
| 2887 |
} |
| 2888 |
|
| 2889 |
|
| 2890 |
static const GOptionEntry command_monitor_entries[] = |
| 2891 |
{ |
| 2892 |
{ NULL } |
| 2893 |
}; |
| 2894 |
|
| 2895 |
static gint |
| 2896 |
handle_command_monitor (gint *argc, |
| 2897 |
gchar **argv[], |
| 2898 |
gboolean request_completion, |
| 2899 |
const gchar *completion_cur, |
| 2900 |
const gchar *completion_prev) |
| 2901 |
{ |
| 2902 |
gint ret; |
| 2903 |
GOptionContext *o; |
| 2904 |
gchar *s; |
| 2905 |
GDBusObjectManager *manager; |
| 2906 |
|
| 2907 |
ret = 1; |
| 2908 |
|
| 2909 |
modify_argv0_for_command (argc, argv, "monitor"); |
| 2910 |
|
| 2911 |
o = g_option_context_new (NULL); |
| 2912 |
if (request_completion) |
| 2913 |
g_option_context_set_ignore_unknown_options (o, TRUE); |
| 2914 |
g_option_context_set_help_enabled (o, FALSE); |
| 2915 |
g_option_context_set_summary (o, "Monitor changes to objects."); |
| 2916 |
g_option_context_add_main_entries (o, command_monitor_entries, NULL /* GETTEXT_PACKAGE*/); |
| 2917 |
|
| 2918 |
if (!g_option_context_parse (o, argc, argv, NULL)) |
| 2919 |
{ |
| 2920 |
if (!request_completion) |
| 2921 |
{ |
| 2922 |
s = g_option_context_get_help (o, FALSE, NULL); |
| 2923 |
g_printerr ("%s", s); |
| 2924 |
g_free (s); |
| 2925 |
goto out; |
| 2926 |
} |
| 2927 |
} |
| 2928 |
|
| 2929 |
/* done with completion */ |
| 2930 |
if (request_completion) |
| 2931 |
goto out; |
| 2932 |
|
| 2933 |
g_print ("Monitoring the udisks daemon. Press Ctrl+C to exit.\n"); |
| 2934 |
|
| 2935 |
manager = udisks_client_get_object_manager (client); |
| 2936 |
g_signal_connect (manager, |
| 2937 |
"notify::name-owner", |
| 2938 |
G_CALLBACK (monitor_on_notify_name_owner), |
| 2939 |
NULL); |
| 2940 |
|
| 2941 |
g_signal_connect (manager, |
| 2942 |
"object-added", |
| 2943 |
G_CALLBACK (monitor_on_object_added), |
| 2944 |
NULL); |
| 2945 |
g_signal_connect (manager, |
| 2946 |
"object-removed", |
| 2947 |
G_CALLBACK (monitor_on_object_removed), |
| 2948 |
NULL); |
| 2949 |
g_signal_connect (manager, |
| 2950 |
"interface-added", |
| 2951 |
G_CALLBACK (monitor_on_interface_proxy_added), |
| 2952 |
NULL); |
| 2953 |
g_signal_connect (manager, |
| 2954 |
"interface-removed", |
| 2955 |
G_CALLBACK (monitor_on_interface_proxy_removed), |
| 2956 |
NULL); |
| 2957 |
g_signal_connect (manager, |
| 2958 |
"interface-proxy-properties-changed", |
| 2959 |
G_CALLBACK (monitor_on_interface_proxy_properties_changed), |
| 2960 |
NULL); |
| 2961 |
g_signal_connect (manager, |
| 2962 |
"interface-proxy-signal", |
| 2963 |
G_CALLBACK (monitor_on_interface_proxy_signal), |
| 2964 |
NULL); |
| 2965 |
|
| 2966 |
monitor_print_name_owner (); |
| 2967 |
|
| 2968 |
g_main_loop_run (loop); |
| 2969 |
|
| 2970 |
ret = 0; |
| 2971 |
|
| 2972 |
out: |
| 2973 |
g_option_context_free (o); |
| 2974 |
return ret; |
| 2975 |
} |
| 2976 |
|
| 2977 |
/* ---------------------------------------------------------------------------------------------------- */ |
| 2978 |
|
| 2979 |
static GList * |
| 2980 |
find_blocks_for_drive (GList *objects, |
| 2981 |
const gchar *drive_object_path) |
| 2982 |
{ |
| 2983 |
GList *ret; |
| 2984 |
GList *l; |
| 2985 |
|
| 2986 |
ret = NULL; |
| 2987 |
for (l = objects; l != NULL; l = l->next) |
| 2988 |
{ |
| 2989 |
UDisksObject *object = UDISKS_OBJECT (l->data); |
| 2990 |
UDisksBlock *block; |
| 2991 |
|
| 2992 |
block = udisks_object_get_block (object); |
| 2993 |
if (block == NULL) |
| 2994 |
continue; |
| 2995 |
|
| 2996 |
if (g_strcmp0 (udisks_block_get_drive (block), drive_object_path) == 0) |
| 2997 |
{ |
| 2998 |
ret = g_list_append (ret, g_object_ref (block)); |
| 2999 |
} |
| 3000 |
g_object_unref (block); |
| 3001 |
} |
| 3002 |
return ret; |
| 3003 |
} |
| 3004 |
|
| 3005 |
static const GOptionEntry command_status_entries[] = |
| 3006 |
{ |
| 3007 |
{ NULL } |
| 3008 |
}; |
| 3009 |
|
| 3010 |
#if 0 |
| 3011 |
static void |
| 3012 |
print_with_padding_and_ellipsis (const gchar *str, |
| 3013 |
gint max_len) |
| 3014 |
{ |
| 3015 |
gint len; |
| 3016 |
len = str != NULL ? strlen (str) : 0; |
| 3017 |
if (len == 0) |
| 3018 |
{ |
| 3019 |
g_print ("%-*s", max_len, "-"); |
| 3020 |
} |
| 3021 |
else if (len <= max_len - 1) |
| 3022 |
{ |
| 3023 |
g_print ("%-*s", max_len, str); |
| 3024 |
} |
| 3025 |
else |
| 3026 |
{ |
| 3027 |
gchar *s; |
| 3028 |
s = g_strndup (str, max_len - 2); |
| 3029 |
/* … U+2026 HORIZONTAL ELLIPSIS |
| 3030 |
* UTF-8: 0xE2 0x80 0xA6 |
| 3031 |
*/ |
| 3032 |
g_print ("%s… ", s); |
| 3033 |
g_free (s); |
| 3034 |
} |
| 3035 |
} |
| 3036 |
#endif |
| 3037 |
|
| 3038 |
static gint |
| 3039 |
handle_command_status (gint *argc, |
| 3040 |
gchar **argv[], |
| 3041 |
gboolean request_completion, |
| 3042 |
const gchar *completion_cur, |
| 3043 |
const gchar *completion_prev) |
| 3044 |
{ |
| 3045 |
gint ret; |
| 3046 |
GOptionContext *o; |
| 3047 |
gchar *s; |
| 3048 |
GList *l; |
| 3049 |
GList *objects; |
| 3050 |
|
| 3051 |
ret = 1; |
| 3052 |
|
| 3053 |
modify_argv0_for_command (argc, argv, "status"); |
| 3054 |
|
| 3055 |
o = g_option_context_new (NULL); |
| 3056 |
if (request_completion) |
| 3057 |
g_option_context_set_ignore_unknown_options (o, TRUE); |
| 3058 |
g_option_context_set_help_enabled (o, FALSE); |
| 3059 |
g_option_context_set_summary (o, "Shows high-level status."); |
| 3060 |
g_option_context_add_main_entries (o, command_status_entries, NULL /* GETTEXT_PACKAGE*/); |
| 3061 |
|
| 3062 |
if (!g_option_context_parse (o, argc, argv, NULL)) |
| 3063 |
{ |
| 3064 |
if (!request_completion) |
| 3065 |
{ |
| 3066 |
s = g_option_context_get_help (o, FALSE, NULL); |
| 3067 |
g_printerr ("%s", s); |
| 3068 |
g_free (s); |
| 3069 |
goto out; |
| 3070 |
} |
| 3071 |
} |
| 3072 |
|
| 3073 |
/* done with completion */ |
| 3074 |
if (request_completion) |
| 3075 |
goto out; |
| 3076 |
|
| 3077 |
objects = g_dbus_object_manager_get_objects (udisks_client_get_object_manager (client)); |
| 3078 |
|
| 3079 |
/* print all drives |
| 3080 |
* |
| 3081 |
* We are guaranteed that, usually, |
| 3082 |
* |
| 3083 |
* - model <= 16 (SCSI: 16, ATA: 40) |
| 3084 |
* - vendor <= 8 (SCSI: 8, ATA: 0) |
| 3085 |
* - revision <= 8 (SCSI: 6, ATA: 8) |
| 3086 |
* - serial <= 20 (SCSI: 16, ATA: 20) |
| 3087 |
*/ |
| 3088 |
g_print ("MODEL REVISION SERIAL DEVICE\n" |
| 3089 |
"--------------------------------------------------------------------------\n"); |
| 3090 |
/* SEAGATE ST3300657SS 0006 3SJ1QNMQ00009052NECM sdaa sdab dm-32 */ |
| 3091 |
/* 01234567890123456789012345678901234567890123456789012345678901234567890123456789 */ |
| 3092 |
|
| 3093 |
/* sort on Drive:SortKey */ |
| 3094 |
objects = g_list_sort (objects, (GCompareFunc) obj_proxy_drive_sortkey_cmp); |
| 3095 |
for (l = objects; l != NULL; l = l->next) |
| 3096 |
{ |
| 3097 |
UDisksObject *object = UDISKS_OBJECT (l->data); |
| 3098 |
UDisksDrive *drive; |
| 3099 |
GList *blocks; |
| 3100 |
const gchar *vendor; |
| 3101 |
const gchar *model; |
| 3102 |
const gchar *revision; |
| 3103 |
const gchar *serial; |
| 3104 |
gchar *vendor_model; |
| 3105 |
GString *str; |
| 3106 |
gchar *block; |
| 3107 |
GList *j; |
| 3108 |
|
| 3109 |
drive = udisks_object_peek_drive (object); |
| 3110 |
if (drive == NULL) |
| 3111 |
continue; |
| 3112 |
|
| 3113 |
str = g_string_new (NULL); |
| 3114 |
blocks = find_blocks_for_drive (objects, g_dbus_object_get_object_path (G_DBUS_OBJECT (object))); |
| 3115 |
for (j = blocks; j != NULL; j = j->next) |
| 3116 |
{ |
| 3117 |
UDisksBlock *block_iface = UDISKS_BLOCK (j->data); |
| 3118 |
GDBusObject *block_object; |
| 3119 |
UDisksPartition *partition; |
| 3120 |
block_object = g_dbus_interface_get_object (G_DBUS_INTERFACE (block_iface)); |
| 3121 |
partition = block_object == NULL ? NULL : udisks_object_peek_partition (UDISKS_OBJECT (block_object)); |
| 3122 |
if (partition == NULL) |
| 3123 |
{ |
| 3124 |
const gchar *device_file; |
| 3125 |
if (str->len > 0) |
| 3126 |
g_string_append (str, " "); |
| 3127 |
device_file = udisks_block_get_device (block_iface); |
| 3128 |
if (g_str_has_prefix (device_file, "/dev/")) |
| 3129 |
g_string_append (str, device_file + 5); |
| 3130 |
else |
| 3131 |
g_string_append (str, device_file); |
| 3132 |
} |
| 3133 |
} |
| 3134 |
if (str->len == 0) |
| 3135 |
g_string_append (str, "-"); |
| 3136 |
block = g_string_free (str, FALSE); |
| 3137 |
g_list_free_full (blocks, g_object_unref); |
| 3138 |
|
| 3139 |
vendor = udisks_drive_get_vendor (drive); |
| 3140 |
model = udisks_drive_get_model (drive); |
| 3141 |
revision = udisks_drive_get_revision (drive); |
| 3142 |
serial = udisks_drive_get_serial (drive); |
| 3143 |
|
| 3144 |
if (strlen (vendor) == 0) |
| 3145 |
vendor = NULL; |
| 3146 |
if (strlen (model) == 0) |
| 3147 |
model = NULL; |
| 3148 |
if (vendor != NULL && model != NULL) |
| 3149 |
vendor_model = g_strdup_printf ("%s %s", vendor, model); |
| 3150 |
else if (model != NULL) |
| 3151 |
vendor_model = g_strdup (model); |
| 3152 |
else if (vendor != NULL) |
| 3153 |
vendor_model = g_strdup (vendor); |
| 3154 |
else |
| 3155 |
vendor_model = g_strdup ("-"); |
| 3156 |
|
| 3157 |
/* TODO: would be nice to show the port/slot if disk is in a SES-2 enclosure */ |
| 3158 |
g_print ("%-25s %-9s %-20s %-8s\n", |
| 3159 |
vendor_model, |
| 3160 |
revision, |
| 3161 |
serial, |
| 3162 |
block); |
| 3163 |
g_free (block); |
| 3164 |
g_free (vendor_model); |
| 3165 |
} |
| 3166 |
|
| 3167 |
|
| 3168 |
g_list_free_full (objects, g_object_unref); |
| 3169 |
|
| 3170 |
ret = 0; |
| 3171 |
|
| 3172 |
out: |
| 3173 |
g_option_context_free (o); |
| 3174 |
return ret; |
| 3175 |
} |
| 3176 |
|
| 3177 |
/* ---------------------------------------------------------------------------------------------------- */ |
| 3178 |
|
| 3179 |
static void |
| 3180 |
usage (gint *argc, gchar **argv[], gboolean use_stdout) |
| 3181 |
{ |
| 3182 |
GOptionContext *o; |
| 3183 |
static GOptionEntry entries[] = { { NULL } }; |
| 3184 |
gchar *description; |
| 3185 |
gchar *s; |
| 3186 |
gchar *program_name; |
| 3187 |
|
| 3188 |
o = g_option_context_new ("COMMAND"); |
| 3189 |
g_option_context_set_help_enabled (o, FALSE); |
| 3190 |
g_option_context_add_main_entries (o, entries, NULL); |
| 3191 |
/* Ignore parsing result */ |
| 3192 |
/* coverity[check_return] */ |
| 3193 |
g_option_context_parse (o, argc, argv, NULL); |
| 3194 |
program_name = g_path_get_basename ((*argv)[0]); |
| 3195 |
description = g_strdup_printf ("Commands:\n" |
| 3196 |
" help Shows this information\n" |
| 3197 |
" info Shows information about an object\n" |
| 3198 |
" dump Shows information about all objects\n" |
| 3199 |
" status Shows high-level status\n" |
| 3200 |
" monitor Monitor changes to objects\n" |
| 3201 |
" mount Mount a filesystem\n" |
| 3202 |
" unmount Unmount a filesystem\n" |
| 3203 |
" unlock Unlock an encrypted device\n" |
| 3204 |
" lock Lock an encrypted device\n" |
| 3205 |
" loop-setup Set-up a loop device\n" |
| 3206 |
" loop-delete Delete a loop device\n" |
| 3207 |
" power-off Safely power off a drive\n" |
| 3208 |
" smart-simulate Set SMART data for a drive\n" |
| 3209 |
"\n" |
| 3210 |
"Use \"%s COMMAND --help\" to get help on each command.\n", |
| 3211 |
program_name); |
| 3212 |
g_free (program_name); |
| 3213 |
g_option_context_set_description (o, description); |
| 3214 |
s = g_option_context_get_help (o, FALSE, NULL); |
| 3215 |
if (use_stdout) |
| 3216 |
g_print ("%s", s); |
| 3217 |
else |
| 3218 |
g_printerr ("%s", s); |
| 3219 |
g_free (s); |
| 3220 |
g_free (description); |
| 3221 |
g_option_context_free (o); |
| 3222 |
} |
| 3223 |
|
| 3224 |
static void |
| 3225 |
remove_arg (gint num, gint *argc, gchar **argv[]) |
| 3226 |
{ |
| 3227 |
gint n; |
| 3228 |
|
| 3229 |
g_assert (num <= (*argc)); |
| 3230 |
|
| 3231 |
for (n = num; (*argv)[n] != NULL; n++) |
| 3232 |
(*argv)[n] = (*argv)[n+1]; |
| 3233 |
(*argv)[n] = NULL; |
| 3234 |
(*argc) = (*argc) - 1; |
| 3235 |
} |
| 3236 |
|
| 3237 |
static void |
| 3238 |
modify_argv0_for_command (gint *argc, gchar **argv[], const gchar *command) |
| 3239 |
{ |
| 3240 |
gchar *s; |
| 3241 |
gchar *program_name; |
| 3242 |
|
| 3243 |
/* TODO: |
| 3244 |
* 1. get a g_set_prgname() ?; or |
| 3245 |
* 2. save old argv[0] and restore later |
| 3246 |
*/ |
| 3247 |
|
| 3248 |
g_assert (g_strcmp0 ((*argv)[1], command) == 0); |
| 3249 |
remove_arg (1, argc, argv); |
| 3250 |
|
| 3251 |
program_name = g_path_get_basename ((*argv)[0]); |
| 3252 |
s = g_strdup_printf ("%s %s", (*argv)[0], command); |
| 3253 |
(*argv)[0] = s; |
| 3254 |
g_free (program_name); |
| 3255 |
} |
| 3256 |
|
| 3257 |
static gchar * |
| 3258 |
pick_word_at (const gchar *s, |
| 3259 |
gint cursor, |
| 3260 |
gint *out_word_begins_at) |
| 3261 |
{ |
| 3262 |
gint begin; |
| 3263 |
gint end; |
| 3264 |
size_t len = strlen(s); |
| 3265 |
|
| 3266 |
if (cursor < 0) |
| 3267 |
cursor = 0; |
| 3268 |
else if ((size_t) cursor >= len) |
| 3269 |
cursor = len - 1; |
| 3270 |
|
| 3271 |
if (s[0] == '\0') |
| 3272 |
{ |
| 3273 |
if (out_word_begins_at != NULL) |
| 3274 |
*out_word_begins_at = -1; |
| 3275 |
return NULL; |
| 3276 |
} |
| 3277 |
|
| 3278 |
if (g_ascii_isspace (s[cursor]) && ((cursor > 0 && g_ascii_isspace(s[cursor-1])) || cursor == 0)) |
| 3279 |
{ |
| 3280 |
if (out_word_begins_at != NULL) |
| 3281 |
*out_word_begins_at = cursor; |
| 3282 |
return g_strdup (""); |
| 3283 |
} |
| 3284 |
|
| 3285 |
while (!g_ascii_isspace (s[cursor - 1]) && cursor > 0) |
| 3286 |
cursor--; |
| 3287 |
begin = cursor; |
| 3288 |
|
| 3289 |
end = begin; |
| 3290 |
while (!g_ascii_isspace (s[end]) && s[end] != '\0') |
| 3291 |
end++; |
| 3292 |
|
| 3293 |
if (out_word_begins_at != NULL) |
| 3294 |
*out_word_begins_at = begin; |
| 3295 |
|
| 3296 |
return g_strndup (s + begin, end - begin); |
| 3297 |
} |
| 3298 |
|
| 3299 |
/* TODO: would be nice with generic options that can be used before any verb such as |
| 3300 |
* |
| 3301 |
* -n, --no-color Turn colorization off always. |
| 3302 |
* -C, --color Turn colorization on always. |
| 3303 |
*/ |
| 3304 |
|
| 3305 |
int |
| 3306 |
main (int argc, |
| 3307 |
char **argv) |
| 3308 |
{ |
| 3309 |
gint ret; |
| 3310 |
const gchar *command; |
| 3311 |
gboolean request_completion; |
| 3312 |
gchar *completion_cur; |
| 3313 |
gchar *completion_prev; |
| 3314 |
GError *error; |
| 3315 |
|
| 3316 |
ret = 1; |
| 3317 |
completion_cur = NULL; |
| 3318 |
completion_prev = NULL; |
| 3319 |
loop = NULL; |
| 3320 |
|
| 3321 |
_color_init (); |
| 3322 |
|
| 3323 |
setlocale (LC_ALL, ""); |
| 3324 |
|
| 3325 |
if (argc < 2) |
| 3326 |
{ |
| 3327 |
usage (&argc, &argv, FALSE); |
| 3328 |
goto out; |
| 3329 |
} |
| 3330 |
|
| 3331 |
loop = g_main_loop_new (NULL, FALSE); |
| 3332 |
|
| 3333 |
error = NULL; |
| 3334 |
client = udisks_client_new_sync (NULL, /* GCancellable */ |
| 3335 |
&error); |
| 3336 |
if (client == NULL) |
| 3337 |
{ |
| 3338 |
g_printerr ("Error connecting to the udisks daemon: %s\n", error->message); |
| 3339 |
g_clear_error (&error); |
| 3340 |
goto out; |
| 3341 |
} |
| 3342 |
|
| 3343 |
request_completion = FALSE; |
| 3344 |
|
| 3345 |
completion_debug ("========================================================================"); |
| 3346 |
completion_debug ("---- argc=%d --------------------------------------------------------", argc); |
| 3347 |
|
| 3348 |
again: |
| 3349 |
command = argv[1]; |
| 3350 |
if (g_strcmp0 (command, "help") == 0) |
| 3351 |
{ |
| 3352 |
if (request_completion) |
| 3353 |
{ |
| 3354 |
/* do nothing */ |
| 3355 |
} |
| 3356 |
else |
| 3357 |
{ |
| 3358 |
usage (&argc, &argv, TRUE); |
| 3359 |
ret = 0; |
| 3360 |
} |
| 3361 |
goto out; |
| 3362 |
} |
| 3363 |
else if (g_strcmp0 (command, "info") == 0) |
| 3364 |
{ |
| 3365 |
ret = handle_command_info (&argc, |
| 3366 |
&argv, |
| 3367 |
request_completion, |
| 3368 |
completion_cur, |
| 3369 |
completion_prev); |
| 3370 |
goto out; |
| 3371 |
} |
| 3372 |
else if (g_strcmp0 (command, "mount") == 0 || g_strcmp0 (command, "unmount") == 0) |
| 3373 |
{ |
| 3374 |
ret = handle_command_mount_unmount (&argc, |
| 3375 |
&argv, |
| 3376 |
request_completion, |
| 3377 |
completion_cur, |
| 3378 |
completion_prev, |
| 3379 |
g_strcmp0 (command, "mount") == 0); |
| 3380 |
goto out; |
| 3381 |
} |
| 3382 |
else if (g_strcmp0 (command, "unlock") == 0 || g_strcmp0 (command, "lock") == 0) |
| 3383 |
{ |
| 3384 |
ret = handle_command_unlock_lock (&argc, |
| 3385 |
&argv, |
| 3386 |
request_completion, |
| 3387 |
completion_cur, |
| 3388 |
completion_prev, |
| 3389 |
g_strcmp0 (command, "unlock") == 0); |
| 3390 |
goto out; |
| 3391 |
} |
| 3392 |
else if (g_strcmp0 (command, "loop-setup") == 0 || g_strcmp0 (command, "loop-delete") == 0) |
| 3393 |
{ |
| 3394 |
ret = handle_command_loop (&argc, |
| 3395 |
&argv, |
| 3396 |
request_completion, |
| 3397 |
completion_cur, |
| 3398 |
completion_prev, |
| 3399 |
g_strcmp0 (command, "loop-setup") == 0); |
| 3400 |
goto out; |
| 3401 |
} |
| 3402 |
else if (g_strcmp0 (command, "smart-simulate") == 0) |
| 3403 |
{ |
| 3404 |
ret = handle_command_smart_simulate (&argc, |
| 3405 |
&argv, |
| 3406 |
request_completion, |
| 3407 |
completion_cur, |
| 3408 |
completion_prev); |
| 3409 |
goto out; |
| 3410 |
} |
| 3411 |
else if (g_strcmp0 (command, "power-off") == 0) |
| 3412 |
{ |
| 3413 |
ret = handle_command_power_off (&argc, |
| 3414 |
&argv, |
| 3415 |
request_completion, |
| 3416 |
completion_cur, |
| 3417 |
completion_prev); |
| 3418 |
goto out; |
| 3419 |
} |
| 3420 |
else if (g_strcmp0 (command, "dump") == 0) |
| 3421 |
{ |
| 3422 |
ret = handle_command_dump (&argc, |
| 3423 |
&argv, |
| 3424 |
request_completion, |
| 3425 |
completion_cur, |
| 3426 |
completion_prev); |
| 3427 |
goto out; |
| 3428 |
} |
| 3429 |
else if (g_strcmp0 (command, "monitor") == 0) |
| 3430 |
{ |
| 3431 |
ret = handle_command_monitor (&argc, |
| 3432 |
&argv, |
| 3433 |
request_completion, |
| 3434 |
completion_cur, |
| 3435 |
completion_prev); |
| 3436 |
goto out; |
| 3437 |
} |
| 3438 |
else if (g_strcmp0 (command, "status") == 0) |
| 3439 |
{ |
| 3440 |
ret = handle_command_status (&argc, |
| 3441 |
&argv, |
| 3442 |
request_completion, |
| 3443 |
completion_cur, |
| 3444 |
completion_prev); |
| 3445 |
goto out; |
| 3446 |
} |
| 3447 |
else if (g_strcmp0 (command, "complete") == 0 && argc == 4 && !request_completion) |
| 3448 |
{ |
| 3449 |
const gchar *completion_line; |
| 3450 |
gchar **completion_argv; |
| 3451 |
gint completion_argc; |
| 3452 |
gint completion_point; |
| 3453 |
gchar *endp; |
| 3454 |
gint cur_begin; |
| 3455 |
|
| 3456 |
request_completion = TRUE; |
| 3457 |
|
| 3458 |
completion_line = argv[2]; |
| 3459 |
completion_point = strtol (argv[3], &endp, 10); |
| 3460 |
if (endp == argv[3] || *endp != '\0') |
| 3461 |
goto out; |
| 3462 |
|
| 3463 |
completion_debug ("completion_point=%d", completion_point); |
| 3464 |
completion_debug ("----"); |
| 3465 |
completion_debug (" 0123456789012345678901234567890123456789012345678901234567890123456789"); |
| 3466 |
completion_debug ("`%s'", completion_line); |
| 3467 |
completion_debug (" %*s^", |
| 3468 |
completion_point, ""); |
| 3469 |
completion_debug ("----"); |
| 3470 |
|
| 3471 |
if (!g_shell_parse_argv (completion_line, |
| 3472 |
&completion_argc, |
| 3473 |
&completion_argv, |
| 3474 |
NULL)) |
| 3475 |
{ |
| 3476 |
/* it's very possible the command line can't be parsed (for |
| 3477 |
* example, missing quotes etc) - in that case, we just |
| 3478 |
* don't autocomplete at all |
| 3479 |
*/ |
| 3480 |
goto out; |
| 3481 |
} |
| 3482 |
|
| 3483 |
/* compute cur and prev */ |
| 3484 |
completion_prev = NULL; |
| 3485 |
/* coverity[tainted_data] */ |
| 3486 |
completion_cur = pick_word_at (completion_line, completion_point, &cur_begin); |
| 3487 |
if (cur_begin > 0) |
| 3488 |
{ |
| 3489 |
gint prev_end; |
| 3490 |
for (prev_end = cur_begin - 1; prev_end >= 0; prev_end--) |
| 3491 |
{ |
| 3492 |
/* coverity[tainted_data] */ |
| 3493 |
if (!g_ascii_isspace (completion_line[prev_end])) |
| 3494 |
{ |
| 3495 |
completion_prev = pick_word_at (completion_line, prev_end, NULL); |
| 3496 |
break; |
| 3497 |
} |
| 3498 |
} |
| 3499 |
} |
| 3500 |
completion_debug (" cur=`%s'", completion_cur); |
| 3501 |
completion_debug ("prev=`%s'", completion_prev); |
| 3502 |
|
| 3503 |
argc = completion_argc; |
| 3504 |
argv = completion_argv; |
| 3505 |
|
| 3506 |
ret = 0; |
| 3507 |
|
| 3508 |
goto again; |
| 3509 |
} |
| 3510 |
else |
| 3511 |
{ |
| 3512 |
if (request_completion) |
| 3513 |
{ |
| 3514 |
g_print ("help \n" |
| 3515 |
"info \n" |
| 3516 |
"dump \n" |
| 3517 |
"monitor \n" |
| 3518 |
"status \n" |
| 3519 |
"mount \n" |
| 3520 |
"unmount \n" |
| 3521 |
"lock \n" |
| 3522 |
"unlock \n" |
| 3523 |
"loop-setup \n" |
| 3524 |
"loop-delete \n" |
| 3525 |
"power-off \n" |
| 3526 |
"smart-simulate \n" |
| 3527 |
); |
| 3528 |
ret = 0; |
| 3529 |
goto out; |
| 3530 |
} |
| 3531 |
else |
| 3532 |
{ |
| 3533 |
g_printerr ("Unknown command `%s'\n", command); |
| 3534 |
usage (&argc, &argv, FALSE); |
| 3535 |
goto out; |
| 3536 |
} |
| 3537 |
} |
| 3538 |
|
| 3539 |
|
| 3540 |
out: |
| 3541 |
if (loop != NULL) |
| 3542 |
g_main_loop_unref (loop); |
| 3543 |
if (client != NULL) |
| 3544 |
g_object_unref (client); |
| 3545 |
_color_shutdown (); |
| 3546 |
shutdown_local_polkit_agent (); |
| 3547 |
return ret; |
| 3548 |
} |
| 3549 |
|
| 3550 |
#ifdef COMPLETION_DEBUG |
| 3551 |
G_GNUC_UNUSED static void |
| 3552 |
completion_debug (const gchar *format, ...) |
| 3553 |
{ |
| 3554 |
va_list var_args; |
| 3555 |
gchar *s; |
| 3556 |
static FILE *f = NULL; |
| 3557 |
|
| 3558 |
va_start (var_args, format); |
| 3559 |
s = g_strdup_vprintf (format, var_args); |
| 3560 |
if (f == NULL) |
| 3561 |
{ |
| 3562 |
f = fopen ("/tmp/udisksctl-completion-debug.txt", "a+"); |
| 3563 |
} |
| 3564 |
fprintf (f, "%s\n", s); |
| 3565 |
g_free (s); |
| 3566 |
} |
| 3567 |
#else |
| 3568 |
static void |
| 3569 |
completion_debug (const gchar *format, ...) |
| 3570 |
{ |
| 3571 |
} |
| 3572 |
#endif |