18c2aff776a775d34a4c9893a4c72e0434d68e36artem/***************************************************************************
18c2aff776a775d34a4c9893a4c72e0434d68e36artem * hal-storage-eject.c : Eject method handler
18c2aff776a775d34a4c9893a4c72e0434d68e36artem * Copyright (C) 2006 David Zeuthen, <david@fubar.dk>
18c2aff776a775d34a4c9893a4c72e0434d68e36artem * This program is free software; you can redistribute it and/or modify
18c2aff776a775d34a4c9893a4c72e0434d68e36artem * it under the terms of the GNU General Public License as published by
18c2aff776a775d34a4c9893a4c72e0434d68e36artem * the Free Software Foundation; either version 2 of the License, or
18c2aff776a775d34a4c9893a4c72e0434d68e36artem * (at your option) any later version.
18c2aff776a775d34a4c9893a4c72e0434d68e36artem * This program is distributed in the hope that it will be useful,
18c2aff776a775d34a4c9893a4c72e0434d68e36artem * but WITHOUT ANY WARRANTY; without even the implied warranty of
18c2aff776a775d34a4c9893a4c72e0434d68e36artem * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18c2aff776a775d34a4c9893a4c72e0434d68e36artem * GNU General Public License for more details.
18c2aff776a775d34a4c9893a4c72e0434d68e36artem * You should have received a copy of the GNU General Public License
18c2aff776a775d34a4c9893a4c72e0434d68e36artem * along with this program; if not, write to the Free Software
18c2aff776a775d34a4c9893a4c72e0434d68e36artem * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18c2aff776a775d34a4c9893a4c72e0434d68e36artem **************************************************************************/
18c2aff776a775d34a4c9893a4c72e0434d68e36artem/* possible values: "Volume", "Storage" */
18c2aff776a775d34a4c9893a4c72e0434d68e36artem fprintf (stderr, "This program should only be started by hald.\n");
18c2aff776a775d34a4c9893a4c72e0434d68e36artem fprintf (stderr, "org.freedesktop.Hal.Device.%s.UnknownFailure\n", devtype);
18c2aff776a775d34a4c9893a4c72e0434d68e36arteminvalid_eject_option (const char *option, const char *uid)
18c2aff776a775d34a4c9893a4c72e0434d68e36artem fprintf (stderr, "org.freedesktop.Hal.Device.Volume.InvalidEjectOption\n");
18c2aff776a775d34a4c9893a4c72e0434d68e36artem fprintf (stderr, "The option '%s' is not allowed for uid=%s\n", option, uid);
18c2aff776a775d34a4c9893a4c72e0434d68e36artem invoked_by_uid = getenv ("HAL_METHOD_INVOKED_BY_UID");
18c2aff776a775d34a4c9893a4c72e0434d68e36artem invoked_by_syscon_name = getenv ("HAL_METHOD_INVOKED_BY_SYSTEMBUS_CONNECTION_NAME");
18c2aff776a775d34a4c9893a4c72e0434d68e36artem if ((hal_ctx = libhal_ctx_init_direct (&error)) == NULL) {
18c2aff776a775d34a4c9893a4c72e0434d68e36artem /* read from stdin */
18c2aff776a775d34a4c9893a4c72e0434d68e36artem if (strlen (fgets (eject_options, sizeof (eject_options), stdin)) > 0)
18c2aff776a775d34a4c9893a4c72e0434d68e36artem /* validate that input from stdin is UTF-8 */
18c2aff776a775d34a4c9893a4c72e0434d68e36artem unknown_eject_error ("Error validating eject_options as UTF-8");
18c2aff776a775d34a4c9893a4c72e0434d68e36artem /* delete any trailing whitespace options from splitting the string */
18c2aff776a775d34a4c9893a4c72e0434d68e36artem for (i = g_strv_length (given_options) - 1; i >= 0; --i) {
18c2aff776a775d34a4c9893a4c72e0434d68e36artem /* check eject options */
18c2aff776a775d34a4c9893a4c72e0434d68e36artem /* none supported right now */
18c2aff776a775d34a4c9893a4c72e0434d68e36artem /* should be either volume or storage */
18c2aff776a775d34a4c9893a4c72e0434d68e36artem if ((volume = libhal_volume_from_udi (hal_ctx, udi)) != NULL) {
18c2aff776a775d34a4c9893a4c72e0434d68e36artem drive_udi = libhal_volume_get_storage_device_udi (volume);
18c2aff776a775d34a4c9893a4c72e0434d68e36artem if ((drive = libhal_drive_from_udi (hal_ctx, drive_udi)) == NULL) {
18c2aff776a775d34a4c9893a4c72e0434d68e36artem /* first, unmount all volumes */
18c2aff776a775d34a4c9893a4c72e0434d68e36artem volume_udis = libhal_drive_find_all_volumes (hal_ctx, drive, &num_volumes);
18c2aff776a775d34a4c9893a4c72e0434d68e36artem unknown_eject_error ("Cannot get all enclosed volumes");
18c2aff776a775d34a4c9893a4c72e0434d68e36artem for (i = 0; i < num_volumes; i++) {
18c2aff776a775d34a4c9893a4c72e0434d68e36artem printf ("processing drive's volume %s (%d of %d)\n", volume_udi, i + 1, num_volumes);
18c2aff776a775d34a4c9893a4c72e0434d68e36artem volume_to_unmount = libhal_volume_from_udi (hal_ctx, volume_udi);
18c2aff776a775d34a4c9893a4c72e0434d68e36artem /* only lock around unmount call because hald's /proc/mounts handler
18c2aff776a775d34a4c9893a4c72e0434d68e36artem * will also want to lock the /media/.hal-mtab-lock file for peeking
18c2aff776a775d34a4c9893a4c72e0434d68e36artem unknown_eject_error ("Cannot obtain lock on /media/.hal-mtab");
18c2aff776a775d34a4c9893a4c72e0434d68e36artem FALSE, FALSE, system_bus); /* use neither lazy nor force */
18c2aff776a775d34a4c9893a4c72e0434d68e36artem /* now attempt the eject */