1*18c2aff7Sartem /*************************************************************************** 2*18c2aff7Sartem * 3*18c2aff7Sartem * hal-storage-eject.c : Eject method handler 4*18c2aff7Sartem * 5*18c2aff7Sartem * Copyright (C) 2006 David Zeuthen, <david@fubar.dk> 6*18c2aff7Sartem * 7*18c2aff7Sartem * This program is free software; you can redistribute it and/or modify 8*18c2aff7Sartem * it under the terms of the GNU General Public License as published by 9*18c2aff7Sartem * the Free Software Foundation; either version 2 of the License, or 10*18c2aff7Sartem * (at your option) any later version. 11*18c2aff7Sartem * 12*18c2aff7Sartem * This program is distributed in the hope that it will be useful, 13*18c2aff7Sartem * but WITHOUT ANY WARRANTY; without even the implied warranty of 14*18c2aff7Sartem * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15*18c2aff7Sartem * GNU General Public License for more details. 16*18c2aff7Sartem * 17*18c2aff7Sartem * You should have received a copy of the GNU General Public License 18*18c2aff7Sartem * along with this program; if not, write to the Free Software 19*18c2aff7Sartem * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 20*18c2aff7Sartem * 21*18c2aff7Sartem **************************************************************************/ 22*18c2aff7Sartem 23*18c2aff7Sartem 24*18c2aff7Sartem #ifdef HAVE_CONFIG_H 25*18c2aff7Sartem # include <config.h> 26*18c2aff7Sartem #endif 27*18c2aff7Sartem 28*18c2aff7Sartem #include <stdio.h> 29*18c2aff7Sartem #include <stdlib.h> 30*18c2aff7Sartem #include <string.h> 31*18c2aff7Sartem #include <glib.h> 32*18c2aff7Sartem #include <glib/gstdio.h> 33*18c2aff7Sartem #include <sys/types.h> 34*18c2aff7Sartem #include <unistd.h> 35*18c2aff7Sartem 36*18c2aff7Sartem #include <libhal.h> 37*18c2aff7Sartem #include <libhal-storage.h> 38*18c2aff7Sartem #ifdef HAVE_POLKIT 39*18c2aff7Sartem #include <libpolkit.h> 40*18c2aff7Sartem #endif 41*18c2aff7Sartem 42*18c2aff7Sartem #include "hal-storage-shared.h" 43*18c2aff7Sartem 44*18c2aff7Sartem /* possible values: "Volume", "Storage" */ 45*18c2aff7Sartem static char *devtype = "Volume"; 46*18c2aff7Sartem 47*18c2aff7Sartem 48*18c2aff7Sartem static void 49*18c2aff7Sartem usage (void) 50*18c2aff7Sartem { 51*18c2aff7Sartem fprintf (stderr, "This program should only be started by hald.\n"); 52*18c2aff7Sartem exit (1); 53*18c2aff7Sartem } 54*18c2aff7Sartem 55*18c2aff7Sartem 56*18c2aff7Sartem void static 57*18c2aff7Sartem unknown_eject_error (const char *detail) 58*18c2aff7Sartem { 59*18c2aff7Sartem fprintf (stderr, "org.freedesktop.Hal.Device.%s.UnknownFailure\n", devtype); 60*18c2aff7Sartem fprintf (stderr, "%s\n", detail); 61*18c2aff7Sartem exit (1); 62*18c2aff7Sartem } 63*18c2aff7Sartem 64*18c2aff7Sartem 65*18c2aff7Sartem static void 66*18c2aff7Sartem invalid_eject_option (const char *option, const char *uid) 67*18c2aff7Sartem { 68*18c2aff7Sartem fprintf (stderr, "org.freedesktop.Hal.Device.Volume.InvalidEjectOption\n"); 69*18c2aff7Sartem fprintf (stderr, "The option '%s' is not allowed for uid=%s\n", option, uid); 70*18c2aff7Sartem exit (1); 71*18c2aff7Sartem } 72*18c2aff7Sartem 73*18c2aff7Sartem 74*18c2aff7Sartem int 75*18c2aff7Sartem main (int argc, char *argv[]) 76*18c2aff7Sartem { 77*18c2aff7Sartem char *udi; 78*18c2aff7Sartem char *device; 79*18c2aff7Sartem const char *drive_udi; 80*18c2aff7Sartem LibHalDrive *drive; 81*18c2aff7Sartem LibHalVolume *volume; 82*18c2aff7Sartem DBusError error; 83*18c2aff7Sartem LibHalContext *hal_ctx = NULL; 84*18c2aff7Sartem DBusConnection *system_bus = NULL; 85*18c2aff7Sartem #ifdef HAVE_POLKIT 86*18c2aff7Sartem LibPolKitContext *pol_ctx = NULL; 87*18c2aff7Sartem #endif 88*18c2aff7Sartem char *invoked_by_uid; 89*18c2aff7Sartem char *invoked_by_syscon_name; 90*18c2aff7Sartem char **volume_udis; 91*18c2aff7Sartem int num_volumes; 92*18c2aff7Sartem int i; 93*18c2aff7Sartem char eject_options[1024]; 94*18c2aff7Sartem char **given_options; 95*18c2aff7Sartem const char *end; 96*18c2aff7Sartem 97*18c2aff7Sartem device = getenv ("HAL_PROP_BLOCK_DEVICE"); 98*18c2aff7Sartem if (device == NULL) 99*18c2aff7Sartem usage (); 100*18c2aff7Sartem 101*18c2aff7Sartem udi = getenv ("HAL_PROP_INFO_UDI"); 102*18c2aff7Sartem if (udi == NULL) 103*18c2aff7Sartem usage (); 104*18c2aff7Sartem 105*18c2aff7Sartem invoked_by_uid = getenv ("HAL_METHOD_INVOKED_BY_UID"); 106*18c2aff7Sartem 107*18c2aff7Sartem invoked_by_syscon_name = getenv ("HAL_METHOD_INVOKED_BY_SYSTEMBUS_CONNECTION_NAME"); 108*18c2aff7Sartem 109*18c2aff7Sartem dbus_error_init (&error); 110*18c2aff7Sartem if ((hal_ctx = libhal_ctx_init_direct (&error)) == NULL) { 111*18c2aff7Sartem printf ("Cannot connect to hald\n"); 112*18c2aff7Sartem LIBHAL_FREE_DBUS_ERROR (&error); 113*18c2aff7Sartem usage (); 114*18c2aff7Sartem } 115*18c2aff7Sartem 116*18c2aff7Sartem dbus_error_init (&error); 117*18c2aff7Sartem system_bus = dbus_bus_get (DBUS_BUS_SYSTEM, &error); 118*18c2aff7Sartem if (system_bus == NULL) { 119*18c2aff7Sartem printf ("Cannot connect to the system bus\n"); 120*18c2aff7Sartem LIBHAL_FREE_DBUS_ERROR (&error); 121*18c2aff7Sartem usage (); 122*18c2aff7Sartem } 123*18c2aff7Sartem #ifdef HAVE_POLKIT 124*18c2aff7Sartem pol_ctx = libpolkit_new_context (system_bus); 125*18c2aff7Sartem if (pol_ctx == NULL) { 126*18c2aff7Sartem printf ("Cannot get libpolkit context\n"); 127*18c2aff7Sartem unknown_eject_error ("Cannot get libpolkit context"); 128*18c2aff7Sartem } 129*18c2aff7Sartem #endif 130*18c2aff7Sartem 131*18c2aff7Sartem /* read from stdin */ 132*18c2aff7Sartem if (strlen (fgets (eject_options, sizeof (eject_options), stdin)) > 0) 133*18c2aff7Sartem eject_options [strlen (eject_options) - 1] = '\0'; 134*18c2aff7Sartem /* validate that input from stdin is UTF-8 */ 135*18c2aff7Sartem if (!g_utf8_validate (eject_options, -1, &end)) 136*18c2aff7Sartem unknown_eject_error ("Error validating eject_options as UTF-8"); 137*18c2aff7Sartem #ifdef DEBUG 138*18c2aff7Sartem printf ("eject_options = '%s'\n", eject_options); 139*18c2aff7Sartem #endif 140*18c2aff7Sartem 141*18c2aff7Sartem /* delete any trailing whitespace options from splitting the string */ 142*18c2aff7Sartem given_options = g_strsplit (eject_options, "\t", 0); 143*18c2aff7Sartem for (i = g_strv_length (given_options) - 1; i >= 0; --i) { 144*18c2aff7Sartem if (strlen (given_options[i]) > 0) 145*18c2aff7Sartem break; 146*18c2aff7Sartem given_options[i] = NULL; 147*18c2aff7Sartem } 148*18c2aff7Sartem 149*18c2aff7Sartem /* check eject options */ 150*18c2aff7Sartem for (i = 0; given_options[i] != NULL; i++) { 151*18c2aff7Sartem char *given = given_options[i]; 152*18c2aff7Sartem 153*18c2aff7Sartem /* none supported right now */ 154*18c2aff7Sartem 155*18c2aff7Sartem invalid_eject_option (given, invoked_by_uid); 156*18c2aff7Sartem } 157*18c2aff7Sartem g_strfreev (given_options); 158*18c2aff7Sartem 159*18c2aff7Sartem /* should be either volume or storage */ 160*18c2aff7Sartem if ((volume = libhal_volume_from_udi (hal_ctx, udi)) != NULL) { 161*18c2aff7Sartem drive_udi = libhal_volume_get_storage_device_udi (volume); 162*18c2aff7Sartem } else { 163*18c2aff7Sartem drive_udi = g_strdup (udi); 164*18c2aff7Sartem devtype = "Storage"; 165*18c2aff7Sartem } 166*18c2aff7Sartem if (drive_udi == NULL) { 167*18c2aff7Sartem unknown_eject_error ("Cannot get drive udi"); 168*18c2aff7Sartem } 169*18c2aff7Sartem if ((drive = libhal_drive_from_udi (hal_ctx, drive_udi)) == NULL) { 170*18c2aff7Sartem unknown_eject_error ("Cannot get drive from udi"); 171*18c2aff7Sartem } 172*18c2aff7Sartem 173*18c2aff7Sartem /* first, unmount all volumes */ 174*18c2aff7Sartem volume_udis = libhal_drive_find_all_volumes (hal_ctx, drive, &num_volumes); 175*18c2aff7Sartem if (volume_udis == NULL) 176*18c2aff7Sartem unknown_eject_error ("Cannot get all enclosed volumes"); 177*18c2aff7Sartem for (i = 0; i < num_volumes; i++) { 178*18c2aff7Sartem char *volume_udi; 179*18c2aff7Sartem LibHalVolume *volume_to_unmount; 180*18c2aff7Sartem 181*18c2aff7Sartem volume_udi = volume_udis[i]; 182*18c2aff7Sartem 183*18c2aff7Sartem #ifdef DEBUG 184*18c2aff7Sartem printf ("processing drive's volume %s (%d of %d)\n", volume_udi, i + 1, num_volumes); 185*18c2aff7Sartem #endif 186*18c2aff7Sartem volume_to_unmount = libhal_volume_from_udi (hal_ctx, volume_udi); 187*18c2aff7Sartem if (volume_to_unmount == NULL) { 188*18c2aff7Sartem unknown_eject_error ("Cannot get volume object"); 189*18c2aff7Sartem } 190*18c2aff7Sartem 191*18c2aff7Sartem if (libhal_volume_is_mounted (volume_to_unmount)) { 192*18c2aff7Sartem #ifdef DEBUG 193*18c2aff7Sartem printf (" unmounting\n"); 194*18c2aff7Sartem #endif 195*18c2aff7Sartem /* only lock around unmount call because hald's /proc/mounts handler 196*18c2aff7Sartem * will also want to lock the /media/.hal-mtab-lock file for peeking 197*18c2aff7Sartem */ 198*18c2aff7Sartem if (!lock_hal_mtab ()) { 199*18c2aff7Sartem unknown_eject_error ("Cannot obtain lock on /media/.hal-mtab"); 200*18c2aff7Sartem } 201*18c2aff7Sartem handle_unmount (hal_ctx, 202*18c2aff7Sartem #ifdef HAVE_POLKIT 203*18c2aff7Sartem pol_ctx, 204*18c2aff7Sartem #endif 205*18c2aff7Sartem volume_udi, volume_to_unmount, drive, 206*18c2aff7Sartem libhal_volume_get_device_file (volume_to_unmount), 207*18c2aff7Sartem invoked_by_uid, invoked_by_syscon_name, 208*18c2aff7Sartem FALSE, FALSE, system_bus); /* use neither lazy nor force */ 209*18c2aff7Sartem unlock_hal_mtab (); 210*18c2aff7Sartem } else { 211*18c2aff7Sartem #ifdef DEBUG 212*18c2aff7Sartem printf (" not mounted\n"); 213*18c2aff7Sartem #endif 214*18c2aff7Sartem } 215*18c2aff7Sartem 216*18c2aff7Sartem libhal_volume_free (volume_to_unmount); 217*18c2aff7Sartem 218*18c2aff7Sartem } 219*18c2aff7Sartem libhal_free_string_array (volume_udis); 220*18c2aff7Sartem 221*18c2aff7Sartem /* now attempt the eject */ 222*18c2aff7Sartem handle_eject (hal_ctx, 223*18c2aff7Sartem #ifdef HAVE_POLKIT 224*18c2aff7Sartem pol_ctx, 225*18c2aff7Sartem #endif 226*18c2aff7Sartem libhal_drive_get_udi (drive), 227*18c2aff7Sartem drive, 228*18c2aff7Sartem libhal_drive_get_device_file (drive), 229*18c2aff7Sartem invoked_by_uid, 230*18c2aff7Sartem invoked_by_syscon_name, 231*18c2aff7Sartem FALSE, system_bus); 232*18c2aff7Sartem 233*18c2aff7Sartem return 0; 234*18c2aff7Sartem } 235*18c2aff7Sartem 236*18c2aff7Sartem 237