Debian Codegraph @

modules/iscsi/udiskslinuxmanageriscsiinitiator.c

raw
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
2 *
3 * Copyright (C) 2015 Peter Hatina <[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 #include "config.h"
21
22 #include <fcntl.h>
23 #include <errno.h>
24 #include <string.h>
25
26 #include <glib/gi18n-lib.h>
27
28 #include <libiscsi.h>
29
30 #include <src/udisksdaemon.h>
31 #include <src/udisksdaemonutil.h>
32 #include <src/udiskslogging.h>
33 #include <src/udisksmodulemanager.h>
34
35 #include "udisksiscsiutil.h"
36 #include "udiskslinuxmanageriscsiinitiator.h"
37
38 /**
39 * SECTION:udiskslinuxmanageriscsiinitiator
40 * @title: UDisksLinuxManagerISCSIInitiator
41 * @short_description: Linux implementation of
42 * #UDisksLinuxManagerISCSIInitiator
43 *
44 * This type provides an implementation of the
45 * #UDisksLinuxManagerISCSIInitiator interface on Linux.
46 */
47
48 /**
49 * UDisksLinuxManagerISCSIInitiator:
50 *
51 * The #UDisksLinuxManagerISCSIInitiator structure contains only private data
52 * and should only be accessed using the provided API.
53 */
54 struct _UDisksLinuxManagerISCSIInitiator {
55 UDisksManagerISCSIInitiatorSkeleton parent_instance;
56
57 UDisksLinuxModuleISCSI *module;
58 GMutex initiator_config_mutex; /* We use separate mutex for configuration
59 file because libiscsi doesn't provide us
60 any API for this. */
61 };
62
63 struct _UDisksLinuxManagerISCSIInitiatorClass {
64 UDisksManagerISCSIInitiatorSkeletonClass parent_class;
65 };
66
67 enum
68 {
69 PROP_0,
70 PROP_MODULE
71 };
72
73 static void udisks_linux_manager_iscsi_initiator_iface_init (UDisksManagerISCSIInitiatorIface *iface);
74
75 G_DEFINE_TYPE_WITH_CODE (UDisksLinuxManagerISCSIInitiator, udisks_linux_manager_iscsi_initiator,
76 UDISKS_TYPE_MANAGER_ISCSI_INITIATOR_SKELETON,
77 G_IMPLEMENT_INTERFACE (UDISKS_TYPE_MANAGER_ISCSI_INITIATOR,
78 udisks_linux_manager_iscsi_initiator_iface_init));
79
80 #define INITIATOR_FILENAME "/etc/iscsi/initiatorname.iscsi"
81 #define INITIATOR_NAME_KEY "InitiatorName"
82
83 /* ---------------------------------------------------------------------------------------------------- */
84
85 static void
86 udisks_linux_manager_iscsi_initiator_get_property (GObject *object, guint property_id,
87 GValue *value, GParamSpec *pspec)
88 {
89 UDisksLinuxManagerISCSIInitiator *manager = UDISKS_LINUX_MANAGER_ISCSI_INITIATOR (object);
90
91 switch (property_id)
92 {
93 case PROP_MODULE:
94 g_value_set_object (value, udisks_linux_manager_iscsi_initiator_get_module (manager));
95 break;
96
97 default:
98 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
99 break;
100 }
101 }
102
103 static void
104 udisks_linux_manager_iscsi_initiator_set_property (GObject *object, guint property_id,
105 const GValue *value, GParamSpec *pspec)
106 {
107 UDisksLinuxManagerISCSIInitiator *manager = UDISKS_LINUX_MANAGER_ISCSI_INITIATOR (object);
108
109 switch (property_id)
110 {
111 case PROP_MODULE:
112 g_assert (manager->module == NULL);
113 manager->module = g_value_dup_object (value);
114 break;
115
116 default:
117 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
118 break;
119 }
120 }
121
122 static void
123 udisks_linux_manager_iscsi_initiator_finalize (GObject *object)
124 {
125 if (G_OBJECT_CLASS (udisks_linux_manager_iscsi_initiator_parent_class))
126 G_OBJECT_CLASS (udisks_linux_manager_iscsi_initiator_parent_class)->finalize (object);
127 }
128
129 static void
130 udisks_linux_manager_iscsi_initiator_class_init (UDisksLinuxManagerISCSIInitiatorClass *klass)
131 {
132 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
133
134 gobject_class->get_property = udisks_linux_manager_iscsi_initiator_get_property;
135 gobject_class->set_property = udisks_linux_manager_iscsi_initiator_set_property;
136 gobject_class->finalize = udisks_linux_manager_iscsi_initiator_finalize;
137
138 /**
139 * UDisksLinuxManager:module:
140 *
141 * The #UDisksLinuxModuleISCSI for the object.
142 */
143 g_object_class_install_property (gobject_class,
144 PROP_MODULE,
145 g_param_spec_object ("module",
146 "Module",
147 "The module for the object",
148 UDISKS_TYPE_LINUX_MODULE_ISCSI,
149 G_PARAM_READABLE |
150 G_PARAM_WRITABLE |
151 G_PARAM_CONSTRUCT_ONLY |
152 G_PARAM_STATIC_STRINGS));
153 }
154
155 static void
156 udisks_linux_manager_iscsi_initiator_init (UDisksLinuxManagerISCSIInitiator *manager)
157 {
158 g_dbus_interface_skeleton_set_flags (G_DBUS_INTERFACE_SKELETON (manager),
159 G_DBUS_INTERFACE_SKELETON_FLAGS_HANDLE_METHOD_INVOCATIONS_IN_THREAD);
160 #ifdef HAVE_LIBISCSI_GET_SESSION_INFOS
161 udisks_manager_iscsi_initiator_set_sessions_supported (UDISKS_MANAGER_ISCSI_INITIATOR (manager),
162 TRUE);
163 #endif
164 }
165
166 /**
167 * udisks_linux_manager_iscsi_initiator_new:
168 * @module: A #UDisksLinuxModuleISCSI.
169 *
170 * Creates a new #UDisksLinuxManagerISCSIInitiator instance.
171 *
172 * Returns: A new #UDisksLinuxManagerISCSIInitiator. Free with g_object_unref().
173 */
174 UDisksLinuxManagerISCSIInitiator *
175 udisks_linux_manager_iscsi_initiator_new (UDisksLinuxModuleISCSI *module)
176 {
177 g_return_val_if_fail (UDISKS_IS_LINUX_MODULE_ISCSI (module), NULL);
178 return UDISKS_LINUX_MANAGER_ISCSI_INITIATOR (g_object_new (UDISKS_TYPE_LINUX_MANAGER_ISCSI_INITIATOR,
179 "module", module,
180 NULL));
181 }
182
183 /**
184 * udisks_linux_manager_iscsi_initiator_get_module:
185 * @manager: A #UDisksLinuxManagerISCSIInitiator.
186 *
187 * Gets the module used by @manager.
188 *
189 * Returns: A #UDisksLinuxModuleISCSI. Do not free, the object is owned by @manager.
190 */
191 UDisksLinuxModuleISCSI *
192 udisks_linux_manager_iscsi_initiator_get_module (UDisksLinuxManagerISCSIInitiator *manager)
193 {
194 g_return_val_if_fail (UDISKS_IS_LINUX_MANAGER_ISCSI_INITIATOR (manager), NULL);
195 return manager->module;
196 }
197
198 /* ---------------------------------------------------------------------------------------------------- */
199
200 static gboolean
201 handle_get_firmware_initiator_name (UDisksManagerISCSIInitiator *object,
202 GDBusMethodInvocation *invocation)
203 {
204 gchar initiator_name[LIBISCSI_VALUE_MAXLEN];
205 gint rval;
206
207 rval = libiscsi_get_firmware_initiator_name (initiator_name);
208 if (rval == 0)
209 {
210 udisks_manager_iscsi_initiator_complete_get_firmware_initiator_name (object,
211 invocation,
212 initiator_name);
213 }
214 else
215 {
216 g_dbus_method_invocation_return_error (invocation,
217 UDISKS_ERROR,
218 UDISKS_ERROR_ISCSI_NO_FIRMWARE,
219 "No firmware found");
220 }
221
222 /* Indicate that we handled the method invocation */
223 return TRUE;
224 }
225
226
227 #define FAKE_GROUP_NAME "general" /* NOTE: shows up in some error messages */
228
229 static gchar *
230 _get_initiator_name (GError **error)
231 {
232 gchar *initiator_name;
233 gchar *contents = NULL;
234 gchar *contents_group;
235 GKeyFile *key_file;
236
237 if (! g_file_get_contents (INITIATOR_FILENAME, &contents, NULL, error))
238 {
239 g_prefix_error (error, "Error reading iSCSI initiator name from '%s': ", INITIATOR_FILENAME);
240 return NULL;
241 }
242
243 /* key-value pairs have to belong to some group, let's create a fake one just to satisfy the parser needs */
244 contents_group = g_strconcat ("[" FAKE_GROUP_NAME "]\n", contents, NULL);
245 g_free (contents);
246
247 key_file = g_key_file_new ();
248 if (! g_key_file_load_from_data (key_file, contents_group, -1, G_KEY_FILE_NONE, error))
249 {
250 g_prefix_error (error, "Error reading iSCSI initiator name from '%s': ", INITIATOR_FILENAME);
251 g_key_file_free (key_file);
252 g_free (contents_group);
253 return NULL;
254 }
255
256 initiator_name = g_key_file_get_string (key_file, FAKE_GROUP_NAME, INITIATOR_NAME_KEY, error);
257 if (initiator_name == NULL)
258 g_prefix_error (error, "Error reading iSCSI initiator name from '%s': ", INITIATOR_FILENAME);
259
260 g_key_file_free (key_file);
261 g_free (contents_group);
262
263 /* trim the whitespace at the end of the string */
264 if (initiator_name != NULL)
265 initiator_name = g_strchomp (initiator_name);
266
267 return initiator_name;
268 }
269
270 static gboolean
271 handle_get_initiator_name (UDisksManagerISCSIInitiator *object,
272 GDBusMethodInvocation *invocation)
273 {
274 UDisksLinuxManagerISCSIInitiator *manager = UDISKS_LINUX_MANAGER_ISCSI_INITIATOR (object);
275 gchar *initiator_name = NULL;
276 GError *error = NULL;
277
278 /* Enter a critical section. */
279 g_mutex_lock (&manager->initiator_config_mutex);
280
281 initiator_name = _get_initiator_name(&error);
282 if (!initiator_name)
283 {
284 g_dbus_method_invocation_take_error (invocation, error);
285 goto out;
286 }
287
288 /* Return the initiator name */
289 udisks_manager_iscsi_initiator_complete_get_initiator_name (object,
290 invocation,
291 initiator_name);
292
293 out:
294 /* Leave the critical section. */
295 g_mutex_unlock (&manager->initiator_config_mutex);
296
297 /* Release the resources */
298 g_free (initiator_name);
299
300 /* Indicate that we handled the method invocation */
301 return TRUE;
302 }
303
304 static gboolean
305 handle_get_initiator_name_raw (UDisksManagerISCSIInitiator *object,
306 GDBusMethodInvocation *invocation)
307 {
308 UDisksLinuxManagerISCSIInitiator *manager = UDISKS_LINUX_MANAGER_ISCSI_INITIATOR (object);
309 gchar *initiator_name = NULL;
310 GError *error = NULL;
311
312 /* Enter a critical section. */
313 g_mutex_lock (&manager->initiator_config_mutex);
314
315 initiator_name = _get_initiator_name(&error);
316 if (!initiator_name)
317 {
318 g_dbus_method_invocation_take_error (invocation, error);
319 goto out;
320 }
321
322 /* Return the initiator name */
323 udisks_manager_iscsi_initiator_complete_get_initiator_name_raw (object,
324 invocation,
325 initiator_name);
326
327 out:
328 /* Leave the critical section. */
329 g_mutex_unlock (&manager->initiator_config_mutex);
330
331 /* Release the resources */
332 g_free (initiator_name);
333
334 /* Indicate that we handled the method invocation */
335 return TRUE;
336 }
337
338 static gboolean
339 handle_set_initiator_name (UDisksManagerISCSIInitiator *object,
340 GDBusMethodInvocation *invocation,
341 const gchar *arg_name,
342 GVariant *arg_options)
343 {
344 UDisksLinuxManagerISCSIInitiator *manager = UDISKS_LINUX_MANAGER_ISCSI_INITIATOR (object);
345 UDisksDaemon *daemon;
346 gchar *contents = NULL;
347 gchar *contents_group;
348 gchar *initiator_name;
349 GKeyFile *key_file;
350 GError *error = NULL;
351
352 daemon = udisks_module_get_daemon (UDISKS_MODULE (manager->module));
353
354 /* Policy check. */
355 if (! udisks_daemon_util_check_authorization_sync (daemon,
356 NULL,
357 ISCSI_MODULE_POLICY_ACTION_ID,
358 arg_options,
359 N_("Authentication is required change iSCSI initiator name"),
360 invocation))
361 return TRUE;
362
363 if (!arg_name || strlen (arg_name) == 0)
364 {
365 g_dbus_method_invocation_return_error_literal (invocation,
366 UDISKS_ERROR,
367 UDISKS_ERROR_FAILED,
368 N_("Empty initiator name"));
369 return TRUE;
370 }
371
372 /* enter critical section */
373 g_mutex_lock (&manager->initiator_config_mutex);
374
375 /* first try to read existing file */
376 g_file_get_contents (INITIATOR_FILENAME, &contents, NULL, NULL /* ignore errors */);
377
378 /* key-value pairs have to belong to some group, let's create a fake one just to satisfy the parser needs */
379 contents_group = g_strconcat ("[" FAKE_GROUP_NAME "]\n", contents, NULL);
380 g_free (contents);
381
382 key_file = g_key_file_new ();
383 if (! g_key_file_load_from_data (key_file, contents_group, -1,
384 G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS,
385 NULL /* ignore errors */))
386 {
387 /* ignoring errors, leaving the keyfile instance empty (e.g. empty or non-existing config file) */
388 }
389 g_free (contents_group);
390
391 /* ensure trailing space in the initiator name */
392 if (arg_name[strlen (arg_name) - 1] != ' ')
393 initiator_name = g_strconcat (arg_name, " ", NULL);
394 else
395 initiator_name = g_strdup (arg_name);
396 g_key_file_set_string (key_file, FAKE_GROUP_NAME, INITIATOR_NAME_KEY, initiator_name);
397 g_free (initiator_name);
398
399 contents_group = g_key_file_to_data (key_file, NULL, NULL);
400 /* strip the fake group name */
401 contents = contents_group ? g_strrstr (contents_group, "[" FAKE_GROUP_NAME "]") : NULL;
402 if (contents != NULL)
403 contents += strlen ("[" FAKE_GROUP_NAME "]\n");
404
405 if (contents == NULL)
406 {
407 g_dbus_method_invocation_return_error_literal (invocation,
408 UDISKS_ERROR,
409 UDISKS_ERROR_FAILED,
410 N_("Error parsing the iSCSI initiator name"));
411 }
412 else if (! g_file_set_contents (INITIATOR_FILENAME, contents, -1, &error))
413 {
414 g_prefix_error (&error, N_("Error writing to %s while setting iSCSI initiator name: "), INITIATOR_FILENAME);
415 g_dbus_method_invocation_take_error (invocation, error);
416 }
417 else
418 {
419 /* finish with no error */
420 udisks_manager_iscsi_initiator_complete_set_initiator_name (object, invocation);
421 }
422 g_free (contents_group);
423 g_key_file_free (key_file);
424
425 /* leave critical section */
426 g_mutex_unlock (&manager->initiator_config_mutex);
427
428 /* indicate that we handled the method invocation */
429 return TRUE;
430 }
431
432 /**
433 * discover_firmware:
434 * @object: A #UDisksManagerISCSIInitiator
435 * @nodes: A #GVariant containing an array with discovery results
436 * @nodes_cnt: The count of discovered nodes
437 * @errorstr: An error string pointer; may be NULL. Free with g_free().
438 *
439 * Performs firmware discovery (ppc or ibft).
440 */
441 static gint
442 discover_firmware (UDisksManagerISCSIInitiator *object,
443 GVariant **nodes,
444 gint *nodes_cnt,
445 gchar **errorstr)
446 {
447 UDisksLinuxManagerISCSIInitiator *manager = UDISKS_LINUX_MANAGER_ISCSI_INITIATOR (object);
448 struct libiscsi_context *ctx;
449 struct libiscsi_node *found_nodes;
450 gint rval;
451
452 /* Enter a critical section. */
453 udisks_linux_module_iscsi_lock_libiscsi_context (manager->module);
454
455 /* Discovery */
456 ctx = udisks_linux_module_iscsi_get_libiscsi_context (manager->module);
457 rval = libiscsi_discover_firmware (ctx, nodes_cnt, &found_nodes);
458
459 if (rval == 0)
460 *nodes = iscsi_libiscsi_nodes_to_gvariant (found_nodes, *nodes_cnt);
461 else if (errorstr)
462 *errorstr = g_strdup (libiscsi_get_error_string (ctx));
463
464 /* Leave the critical section. */
465 udisks_linux_module_iscsi_unlock_libiscsi_context (manager->module);
466
467 /* Release the resources */
468 iscsi_libiscsi_nodes_free (found_nodes);
469
470 return rval;
471 }
472
473 static gboolean
474 handle_discover_send_targets (UDisksManagerISCSIInitiator *object,
475 GDBusMethodInvocation *invocation,
476 const gchar *arg_address,
477 const guint16 arg_port,
478 GVariant *arg_options)
479 {
480 UDisksLinuxManagerISCSIInitiator *manager = UDISKS_LINUX_MANAGER_ISCSI_INITIATOR (object);
481 UDisksDaemon *daemon;
482 GVariant *nodes = NULL;
483 gchar *errorstr = NULL;
484 gint err = 0;
485 gint nodes_cnt = 0;
486
487 daemon = udisks_module_get_daemon (UDISKS_MODULE (manager->module));
488
489 /* Policy check. */
490 UDISKS_DAEMON_CHECK_AUTHORIZATION (daemon,
491 NULL,
492 ISCSI_MODULE_POLICY_ACTION_ID,
493 arg_options,
494 N_("Authentication is required to discover targets"),
495 invocation);
496
497 /* Enter a critical section. */
498 udisks_linux_module_iscsi_lock_libiscsi_context (manager->module);
499
500 /* Perform the discovery. */
501 err = iscsi_discover_send_targets (manager->module, arg_address, arg_port, arg_options, &nodes, &nodes_cnt, &errorstr);
502
503 /* Leave the critical section. */
504 udisks_linux_module_iscsi_unlock_libiscsi_context (manager->module);
505
506 if (err != 0)
507 {
508 /* Discovery failed. */
509 g_dbus_method_invocation_return_error (invocation,
510 UDISKS_ERROR,
511 iscsi_error_to_udisks_error (err),
512 N_("Discovery failed: %s"),
513 errorstr);
514 goto out;
515 }
516
517 /* Return discovered portals. */
518 udisks_manager_iscsi_initiator_complete_discover_send_targets (object, invocation, nodes, nodes_cnt);
519
520 out:
521 g_free (errorstr);
522
523 /* Indicate that we handled the method invocation. */
524 return TRUE;
525 }
526
527 static gboolean
528 handle_discover_firmware (UDisksManagerISCSIInitiator *object,
529 GDBusMethodInvocation *invocation,
530 GVariant *arg_options)
531 {
532 UDisksLinuxManagerISCSIInitiator *manager = UDISKS_LINUX_MANAGER_ISCSI_INITIATOR (object);
533 UDisksDaemon *daemon;
534 GVariant *nodes = NULL;
535 gint err = 0;
536 gint nodes_cnt = 0;
537 gchar *errorstr = NULL;
538
539 daemon = udisks_module_get_daemon (UDISKS_MODULE (manager->module));
540
541 /* Policy check. */
542 UDISKS_DAEMON_CHECK_AUTHORIZATION (daemon,
543 NULL,
544 ISCSI_MODULE_POLICY_ACTION_ID,
545 arg_options,
546 N_("Authentication is required to discover firmware targets"),
547 invocation);
548
549 /* Perform the discovery. */
550 err = discover_firmware (object, &nodes, &nodes_cnt, &errorstr);
551
552 if (err != 0)
553 {
554 /* Discovery failed. */
555 g_dbus_method_invocation_return_error (invocation,
556 UDISKS_ERROR,
557 iscsi_error_to_udisks_error (err),
558 N_("Discovery failed: %s"),
559 errorstr);
560 g_free (errorstr);
561 goto out;
562 }
563
564 /* Return discovered portals. */
565 udisks_manager_iscsi_initiator_complete_discover_firmware (object, invocation, nodes, nodes_cnt);
566
567 out:
568 /* Indicate that we handled the method invocation. */
569 return TRUE;
570 }
571
572 static gboolean
573 handle_login (UDisksManagerISCSIInitiator *object,
574 GDBusMethodInvocation *invocation,
575 const gchar *arg_name,
576 gint arg_tpgt,
577 const gchar *arg_address,
578 gint arg_port,
579 const gchar *arg_iface,
580 GVariant *arg_options)
581 {
582 UDisksLinuxManagerISCSIInitiator *manager = UDISKS_LINUX_MANAGER_ISCSI_INITIATOR (object);
583 UDisksDaemon *daemon;
584 gint err = 0;
585 gchar *errorstr = NULL;
586 GError *error = NULL;
587 UDisksObject *iscsi_object = NULL;
588 UDisksObject *iscsi_session_object = NULL;
589
590 daemon = udisks_module_get_daemon (UDISKS_MODULE (manager->module));
591
592 /* Policy check. */
593 UDISKS_DAEMON_CHECK_AUTHORIZATION (daemon,
594 NULL,
595 ISCSI_MODULE_POLICY_ACTION_ID,
596 arg_options,
597 N_("Authentication is required to perform iSCSI login"),
598 invocation);
599
600 /* Enter a critical section. */
601 udisks_linux_module_iscsi_lock_libiscsi_context (manager->module);
602
603 /* Login */
604 err = iscsi_login (manager->module, arg_name, arg_tpgt, arg_address, arg_port, arg_iface, arg_options, &errorstr);
605
606 /* Leave the critical section. */
607 udisks_linux_module_iscsi_unlock_libiscsi_context (manager->module);
608
609 if (err != 0)
610 {
611 /* Login failed. */
612 g_dbus_method_invocation_return_error (invocation,
613 UDISKS_ERROR,
614 iscsi_error_to_udisks_error (err),
615 N_("Login failed: %s"),
616 errorstr);
617 goto out;
618 }
619
620 /* sit and wait until the device appears on dbus */
621 iscsi_object = udisks_daemon_wait_for_object_sync (daemon,
622 wait_for_iscsi_object,
623 g_strdup (arg_name),
624 g_free,
625 UDISKS_DEFAULT_WAIT_TIMEOUT,
626 &error);
627 if (iscsi_object == NULL)
628 {
629 g_prefix_error (&error, "Error waiting for iSCSI device to appear: ");
630 g_dbus_method_invocation_take_error (invocation, error);
631 goto out;
632 }
633
634 if (udisks_manager_iscsi_initiator_get_sessions_supported (UDISKS_MANAGER_ISCSI_INITIATOR (manager)))
635 {
636 iscsi_session_object = udisks_daemon_wait_for_object_sync (daemon,
637 wait_for_iscsi_session_object,
638 g_strdup (arg_name),
639 g_free,
640 UDISKS_DEFAULT_WAIT_TIMEOUT,
641 &error);
642 if (iscsi_session_object == NULL)
643 {
644 g_prefix_error (&error, "Error waiting for iSCSI session object to appear: ");
645 g_dbus_method_invocation_take_error (invocation, error);
646 goto out;
647 }
648 }
649
650 /* Complete DBus call. */
651 udisks_manager_iscsi_initiator_complete_login (object, invocation);
652
653 out:
654 g_clear_object (&iscsi_object);
655 g_clear_object (&iscsi_session_object);
656 g_free (errorstr);
657
658 /* Indicate that we handled the method invocation. */
659 return TRUE;
660 }
661
662 static gboolean
663 handle_logout(UDisksManagerISCSIInitiator *object,
664 GDBusMethodInvocation *invocation,
665 const gchar *arg_name,
666 gint arg_tpgt,
667 const gchar *arg_address,
668 gint arg_port,
669 const gchar *arg_iface,
670 GVariant *arg_options)
671 {
672 UDisksLinuxManagerISCSIInitiator *manager = UDISKS_LINUX_MANAGER_ISCSI_INITIATOR (object);
673 UDisksDaemon *daemon;
674 gint err = 0;
675 gchar *errorstr = NULL;
676 GError *error = NULL;
677
678 daemon = udisks_module_get_daemon (UDISKS_MODULE (manager->module));
679
680 /* Policy check. */
681 UDISKS_DAEMON_CHECK_AUTHORIZATION (daemon,
682 NULL,
683 ISCSI_MODULE_POLICY_ACTION_ID,
684 arg_options,
685 N_("Authentication is required to perform iSCSI logout"),
686 invocation);
687
688 /* Enter a critical section. */
689 udisks_linux_module_iscsi_lock_libiscsi_context (manager->module);
690
691 /* Logout */
692 err = iscsi_logout (manager->module, arg_name, arg_tpgt, arg_address, arg_port, arg_iface, arg_options, &errorstr);
693
694 /* Leave the critical section. */
695 udisks_linux_module_iscsi_unlock_libiscsi_context (manager->module);
696
697 if (err != 0)
698 {
699 /* Logout failed. */
700 g_dbus_method_invocation_return_error (invocation,
701 UDISKS_ERROR,
702 iscsi_error_to_udisks_error (err),
703 N_("Logout failed: %s"),
704 errorstr);
705 goto out;
706 }
707
708 /* now sit and wait until the device and session disappear on dbus */
709 if (!udisks_daemon_wait_for_object_to_disappear_sync (daemon,
710 wait_for_iscsi_object,
711 g_strdup (arg_name),
712 g_free,
713 UDISKS_DEFAULT_WAIT_TIMEOUT,
714 &error))
715 {
716 g_prefix_error (&error, "Error waiting for iSCSI device to disappear: ");
717 g_dbus_method_invocation_take_error (invocation, error);
718 goto out;
719 }
720
721 if (udisks_manager_iscsi_initiator_get_sessions_supported (UDISKS_MANAGER_ISCSI_INITIATOR (manager)))
722 {
723 if (!udisks_daemon_wait_for_object_to_disappear_sync (daemon,
724 wait_for_iscsi_session_object,
725 g_strdup (arg_name),
726 g_free,
727 UDISKS_DEFAULT_WAIT_TIMEOUT,
728 &error))
729 {
730 g_prefix_error (&error, "Error waiting for iSCSI session object to disappear: ");
731 g_dbus_method_invocation_take_error (invocation, error);
732 goto out;
733 }
734 }
735
736 /* Complete DBus call. */
737 udisks_manager_iscsi_initiator_complete_logout (object, invocation);
738
739 out:
740 g_free (errorstr);
741
742 /* Indicate that we handled the method invocation. */
743 return TRUE;
744 }
745
746 /* ---------------------------------------------------------------------------------------------------- */
747
748 static void
749 udisks_linux_manager_iscsi_initiator_iface_init (UDisksManagerISCSIInitiatorIface *iface)
750 {
751 iface->handle_get_firmware_initiator_name = handle_get_firmware_initiator_name;
752 iface->handle_get_initiator_name = handle_get_initiator_name;
753 iface->handle_get_initiator_name_raw = handle_get_initiator_name_raw;
754 iface->handle_set_initiator_name = handle_set_initiator_name;
755 iface->handle_discover_send_targets = handle_discover_send_targets;
756 iface->handle_discover_firmware = handle_discover_firmware;
757 iface->handle_login = handle_login;
758 iface->handle_logout = handle_logout;
759 }