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