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