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