1N/A/***************************************************************************
1N/A *
1N/A * hal-storage-eject.c : Eject method handler
1N/A *
1N/A * Copyright (C) 2006 David Zeuthen, <david@fubar.dk>
1N/A *
1N/A * This program is free software; you can redistribute it and/or modify
1N/A * it under the terms of the GNU General Public License as published by
1N/A * the Free Software Foundation; either version 2 of the License, or
1N/A * (at your option) any later version.
1N/A *
1N/A * This program is distributed in the hope that it will be useful,
1N/A * but WITHOUT ANY WARRANTY; without even the implied warranty of
1N/A * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1N/A * GNU General Public License for more details.
1N/A *
1N/A * You should have received a copy of the GNU General Public License
1N/A * along with this program; if not, write to the Free Software
1N/A * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
1N/A *
1N/A **************************************************************************/
1N/A
1N/A
1N/A#ifdef HAVE_CONFIG_H
1N/A# include <config.h>
1N/A#endif
1N/A
1N/A#include <stdio.h>
1N/A#include <stdlib.h>
1N/A#include <string.h>
1N/A#include <glib.h>
1N/A#include <glib/gstdio.h>
1N/A#include <sys/types.h>
1N/A#include <unistd.h>
1N/A
1N/A#include <libhal.h>
1N/A#include <libhal-storage.h>
1N/A#ifdef HAVE_POLKIT
1N/A#include <libpolkit.h>
1N/A#endif
1N/A
1N/A#include "hal-storage-shared.h"
1N/A
1N/A/* possible values: "Volume", "Storage" */
1N/Astatic char *devtype = "Volume";
1N/A
1N/A
1N/Astatic void
1N/Ausage (void)
1N/A{
1N/A fprintf (stderr, "This program should only be started by hald.\n");
1N/A exit (1);
1N/A}
1N/A
1N/A
1N/Avoid static
1N/Aunknown_eject_error (const char *detail)
1N/A{
1N/A fprintf (stderr, "org.freedesktop.Hal.Device.%s.UnknownFailure\n", devtype);
1N/A fprintf (stderr, "%s\n", detail);
1N/A exit (1);
1N/A}
1N/A
1N/A
1N/Astatic void
1N/Ainvalid_eject_option (const char *option, const char *uid)
1N/A{
1N/A fprintf (stderr, "org.freedesktop.Hal.Device.Volume.InvalidEjectOption\n");
1N/A fprintf (stderr, "The option '%s' is not allowed for uid=%s\n", option, uid);
1N/A exit (1);
1N/A}
1N/A
1N/A
1N/Aint
1N/Amain (int argc, char *argv[])
1N/A{
1N/A char *udi;
1N/A char *device;
1N/A const char *drive_udi;
1N/A LibHalDrive *drive;
1N/A LibHalVolume *volume;
1N/A DBusError error;
1N/A LibHalContext *hal_ctx = NULL;
1N/A DBusConnection *system_bus = NULL;
1N/A#ifdef HAVE_POLKIT
1N/A LibPolKitContext *pol_ctx = NULL;
1N/A#endif
1N/A char *invoked_by_uid;
1N/A char *invoked_by_syscon_name;
1N/A char **volume_udis;
1N/A int num_volumes;
1N/A int i;
1N/A char eject_options[1024];
1N/A char **given_options;
1N/A const char *end;
1N/A
1N/A device = getenv ("HAL_PROP_BLOCK_DEVICE");
1N/A if (device == NULL)
1N/A usage ();
1N/A
1N/A udi = getenv ("HAL_PROP_INFO_UDI");
1N/A if (udi == NULL)
1N/A usage ();
1N/A
1N/A invoked_by_uid = getenv ("HAL_METHOD_INVOKED_BY_UID");
1N/A
1N/A invoked_by_syscon_name = getenv ("HAL_METHOD_INVOKED_BY_SYSTEMBUS_CONNECTION_NAME");
1N/A
1N/A dbus_error_init (&error);
1N/A if ((hal_ctx = libhal_ctx_init_direct (&error)) == NULL) {
1N/A printf ("Cannot connect to hald\n");
1N/A LIBHAL_FREE_DBUS_ERROR (&error);
1N/A usage ();
1N/A }
1N/A
1N/A dbus_error_init (&error);
1N/A system_bus = dbus_bus_get (DBUS_BUS_SYSTEM, &error);
1N/A if (system_bus == NULL) {
1N/A printf ("Cannot connect to the system bus\n");
1N/A LIBHAL_FREE_DBUS_ERROR (&error);
1N/A usage ();
1N/A }
1N/A#ifdef HAVE_POLKIT
1N/A pol_ctx = libpolkit_new_context (system_bus);
1N/A if (pol_ctx == NULL) {
1N/A printf ("Cannot get libpolkit context\n");
1N/A unknown_eject_error ("Cannot get libpolkit context");
1N/A }
1N/A#endif
1N/A
1N/A /* read from stdin */
1N/A if (strlen (fgets (eject_options, sizeof (eject_options), stdin)) > 0)
1N/A eject_options [strlen (eject_options) - 1] = '\0';
1N/A /* validate that input from stdin is UTF-8 */
1N/A if (!g_utf8_validate (eject_options, -1, &end))
1N/A unknown_eject_error ("Error validating eject_options as UTF-8");
1N/A#ifdef DEBUG
1N/A printf ("eject_options = '%s'\n", eject_options);
1N/A#endif
1N/A
1N/A /* delete any trailing whitespace options from splitting the string */
1N/A given_options = g_strsplit (eject_options, "\t", 0);
1N/A for (i = g_strv_length (given_options) - 1; i >= 0; --i) {
1N/A if (strlen (given_options[i]) > 0)
1N/A break;
1N/A given_options[i] = NULL;
1N/A }
1N/A
1N/A /* check eject options */
1N/A for (i = 0; given_options[i] != NULL; i++) {
1N/A char *given = given_options[i];
1N/A
1N/A /* none supported right now */
1N/A
1N/A invalid_eject_option (given, invoked_by_uid);
1N/A }
1N/A g_strfreev (given_options);
1N/A
1N/A /* should be either volume or storage */
1N/A if ((volume = libhal_volume_from_udi (hal_ctx, udi)) != NULL) {
1N/A drive_udi = libhal_volume_get_storage_device_udi (volume);
1N/A } else {
1N/A drive_udi = g_strdup (udi);
1N/A devtype = "Storage";
1N/A }
1N/A if (drive_udi == NULL) {
1N/A unknown_eject_error ("Cannot get drive udi");
1N/A }
1N/A if ((drive = libhal_drive_from_udi (hal_ctx, drive_udi)) == NULL) {
1N/A unknown_eject_error ("Cannot get drive from udi");
1N/A }
1N/A
1N/A /* first, unmount all volumes */
1N/A volume_udis = libhal_drive_find_all_volumes (hal_ctx, drive, &num_volumes);
1N/A if (volume_udis == NULL)
1N/A unknown_eject_error ("Cannot get all enclosed volumes");
1N/A for (i = 0; i < num_volumes; i++) {
1N/A char *volume_udi;
1N/A LibHalVolume *volume_to_unmount;
1N/A
1N/A volume_udi = volume_udis[i];
1N/A
1N/A#ifdef DEBUG
1N/A printf ("processing drive's volume %s (%d of %d)\n", volume_udi, i + 1, num_volumes);
1N/A#endif
1N/A volume_to_unmount = libhal_volume_from_udi (hal_ctx, volume_udi);
1N/A if (volume_to_unmount == NULL) {
1N/A unknown_eject_error ("Cannot get volume object");
1N/A }
1N/A
1N/A if (libhal_volume_is_mounted (volume_to_unmount)) {
1N/A#ifdef DEBUG
1N/A printf (" unmounting\n");
1N/A#endif
1N/A /* only lock around unmount call because hald's /proc/mounts handler
1N/A * will also want to lock the /media/.hal-mtab-lock file for peeking
1N/A */
1N/A if (!lock_hal_mtab ()) {
1N/A unknown_eject_error ("Cannot obtain lock on /media/.hal-mtab");
1N/A }
1N/A handle_unmount (hal_ctx,
1N/A#ifdef HAVE_POLKIT
1N/A pol_ctx,
1N/A#endif
1N/A volume_udi, volume_to_unmount, drive,
1N/A libhal_volume_get_device_file (volume_to_unmount),
1N/A invoked_by_uid, invoked_by_syscon_name,
1N/A FALSE, FALSE, system_bus); /* use neither lazy nor force */
1N/A unlock_hal_mtab ();
1N/A } else {
1N/A#ifdef DEBUG
1N/A printf (" not mounted\n");
1N/A#endif
1N/A }
1N/A
1N/A libhal_volume_free (volume_to_unmount);
1N/A
1N/A }
1N/A libhal_free_string_array (volume_udis);
1N/A
1N/A /* now attempt the eject */
1N/A handle_eject (hal_ctx,
1N/A#ifdef HAVE_POLKIT
1N/A pol_ctx,
1N/A#endif
1N/A libhal_drive_get_udi (drive),
1N/A drive,
1N/A libhal_drive_get_device_file (drive),
1N/A invoked_by_uid,
1N/A invoked_by_syscon_name,
1N/A FALSE, system_bus);
1N/A
1N/A return 0;
1N/A}
1N/A
1N/A