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