xref: /titanic_51/usr/src/cmd/hal/tools/hal-storage-closetray.c (revision 18c2aff776a775d34a4c9893a4c72e0434d68e36)
1*18c2aff7Sartem /***************************************************************************
2*18c2aff7Sartem  *
3*18c2aff7Sartem  * hal-storage-closetray.c : CloseTray method handler
4*18c2aff7Sartem  *
5*18c2aff7Sartem  * Copyright (C) 2006 David Zeuthen, <david@fubar.dk>
6*18c2aff7Sartem  * Copyright (C) 2006 Sun Microsystems, Inc.
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 #include <sys/types.h>
35*18c2aff7Sartem #include <unistd.h>
36*18c2aff7Sartem 
37*18c2aff7Sartem #include <libhal.h>
38*18c2aff7Sartem #include <libhal-storage.h>
39*18c2aff7Sartem #ifdef HAVE_POLKIT
40*18c2aff7Sartem #include <libpolkit.h>
41*18c2aff7Sartem #endif
42*18c2aff7Sartem 
43*18c2aff7Sartem #include "hal-storage-shared.h"
44*18c2aff7Sartem 
45*18c2aff7Sartem 
46*18c2aff7Sartem static void
47*18c2aff7Sartem usage (void)
48*18c2aff7Sartem {
49*18c2aff7Sartem 	fprintf (stderr, "This program should only be started by hald.\n");
50*18c2aff7Sartem 	exit (1);
51*18c2aff7Sartem }
52*18c2aff7Sartem 
53*18c2aff7Sartem 
54*18c2aff7Sartem void static
55*18c2aff7Sartem unknown_closetray_error (const char *detail)
56*18c2aff7Sartem {
57*18c2aff7Sartem 	fprintf (stderr, "org.freedesktop.Hal.Device.Storage.UnknownFailure\n");
58*18c2aff7Sartem 	fprintf (stderr, "%s\n", detail);
59*18c2aff7Sartem 	exit (1);
60*18c2aff7Sartem }
61*18c2aff7Sartem 
62*18c2aff7Sartem 
63*18c2aff7Sartem static void
64*18c2aff7Sartem invalid_closetray_option (const char *option, const char *uid)
65*18c2aff7Sartem {
66*18c2aff7Sartem 	fprintf (stderr, "org.freedesktop.Hal.Device.Storage.InvalidCloseTrayOption\n");
67*18c2aff7Sartem 	fprintf (stderr, "The option '%s' is not allowed for uid=%s\n", option, uid);
68*18c2aff7Sartem 	exit (1);
69*18c2aff7Sartem }
70*18c2aff7Sartem 
71*18c2aff7Sartem #ifdef __FreeBSD__
72*18c2aff7Sartem #error Need FreeBSD specific changes here
73*18c2aff7Sartem #endif
74*18c2aff7Sartem 
75*18c2aff7Sartem 
76*18c2aff7Sartem int
77*18c2aff7Sartem main (int argc, char *argv[])
78*18c2aff7Sartem {
79*18c2aff7Sartem 	char *udi;
80*18c2aff7Sartem 	char *device;
81*18c2aff7Sartem 	LibHalDrive *drive;
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 	int i;
91*18c2aff7Sartem 	char closetray_options[1024];
92*18c2aff7Sartem 	char **given_options;
93*18c2aff7Sartem 	const char *end;
94*18c2aff7Sartem 
95*18c2aff7Sartem 	device = getenv ("HAL_PROP_BLOCK_DEVICE");
96*18c2aff7Sartem 	if (device == NULL)
97*18c2aff7Sartem 		usage ();
98*18c2aff7Sartem 
99*18c2aff7Sartem 	udi = getenv ("HAL_PROP_INFO_UDI");
100*18c2aff7Sartem 	if (udi == NULL)
101*18c2aff7Sartem 		usage ();
102*18c2aff7Sartem 
103*18c2aff7Sartem 	invoked_by_uid = getenv ("HAL_METHOD_INVOKED_BY_UID");
104*18c2aff7Sartem 
105*18c2aff7Sartem 	invoked_by_syscon_name = getenv ("HAL_METHOD_INVOKED_BY_SYSTEMBUS_CONNECTION_NAME");
106*18c2aff7Sartem 
107*18c2aff7Sartem 	dbus_error_init (&error);
108*18c2aff7Sartem 	if ((hal_ctx = libhal_ctx_init_direct (&error)) == NULL) {
109*18c2aff7Sartem 		printf ("Cannot connect to hald\n");
110*18c2aff7Sartem 		LIBHAL_FREE_DBUS_ERROR (&error);
111*18c2aff7Sartem 		usage ();
112*18c2aff7Sartem 	}
113*18c2aff7Sartem 
114*18c2aff7Sartem 	dbus_error_init (&error);
115*18c2aff7Sartem 	system_bus = dbus_bus_get (DBUS_BUS_SYSTEM, &error);
116*18c2aff7Sartem 	if (system_bus == NULL) {
117*18c2aff7Sartem 		printf ("Cannot connect to the system bus\n");
118*18c2aff7Sartem 		LIBHAL_FREE_DBUS_ERROR (&error);
119*18c2aff7Sartem 		usage ();
120*18c2aff7Sartem 	}
121*18c2aff7Sartem #ifdef HAVE_POLKIT
122*18c2aff7Sartem 	pol_ctx = libpolkit_new_context (system_bus);
123*18c2aff7Sartem 	if (pol_ctx == NULL) {
124*18c2aff7Sartem 		printf ("Cannot get libpolkit context\n");
125*18c2aff7Sartem 		unknown_closetray_error ("Cannot get libpolkit context");
126*18c2aff7Sartem 	}
127*18c2aff7Sartem #endif
128*18c2aff7Sartem 
129*18c2aff7Sartem 	/* read from stdin */
130*18c2aff7Sartem 	if (strlen (fgets (closetray_options, sizeof (closetray_options), stdin)) > 0)
131*18c2aff7Sartem 		closetray_options [strlen (closetray_options) - 1] = '\0';
132*18c2aff7Sartem 	/* validate that input from stdin is UTF-8 */
133*18c2aff7Sartem 	if (!g_utf8_validate (closetray_options, -1, &end))
134*18c2aff7Sartem 		unknown_closetray_error ("Error validating closetray_options as UTF-8");
135*18c2aff7Sartem #ifdef DEBUG
136*18c2aff7Sartem 	printf ("closetray_options  = '%s'\n", closetray_options);
137*18c2aff7Sartem #endif
138*18c2aff7Sartem 
139*18c2aff7Sartem 	/* delete any trailing whitespace options from splitting the string */
140*18c2aff7Sartem 	given_options = g_strsplit (closetray_options, "\t", 0);
141*18c2aff7Sartem 	for (i = g_strv_length (given_options) - 1; i >= 0; --i) {
142*18c2aff7Sartem 		if (strlen (given_options[i]) > 0)
143*18c2aff7Sartem 			break;
144*18c2aff7Sartem 		given_options[i] = NULL;
145*18c2aff7Sartem 	}
146*18c2aff7Sartem 
147*18c2aff7Sartem 	/* check options */
148*18c2aff7Sartem 	for (i = 0; given_options[i] != NULL; i++) {
149*18c2aff7Sartem 		char *given = given_options[i];
150*18c2aff7Sartem 
151*18c2aff7Sartem 		/* none supported right now */
152*18c2aff7Sartem 
153*18c2aff7Sartem 		invalid_closetray_option (given, invoked_by_uid);
154*18c2aff7Sartem 	}
155*18c2aff7Sartem 	g_strfreev (given_options);
156*18c2aff7Sartem 
157*18c2aff7Sartem 	/* should be storage */
158*18c2aff7Sartem 	if ((drive = libhal_drive_from_udi (hal_ctx, udi)) == NULL) {
159*18c2aff7Sartem 		unknown_closetray_error ("Cannot get drive");
160*18c2aff7Sartem 	}
161*18c2aff7Sartem 
162*18c2aff7Sartem 	/* use handle_eject() with the closetray option */
163*18c2aff7Sartem 	handle_eject (hal_ctx,
164*18c2aff7Sartem #ifdef HAVE_POLKIT
165*18c2aff7Sartem 		      pol_ctx,
166*18c2aff7Sartem #endif
167*18c2aff7Sartem 		      libhal_drive_get_udi (drive),
168*18c2aff7Sartem 		      drive,
169*18c2aff7Sartem 		      libhal_drive_get_device_file (drive),
170*18c2aff7Sartem 		      invoked_by_uid,
171*18c2aff7Sartem 		      invoked_by_syscon_name,
172*18c2aff7Sartem 		      TRUE /* closetray option */,
173*18c2aff7Sartem 		      system_bus);
174*18c2aff7Sartem 
175*18c2aff7Sartem 	return 0;
176*18c2aff7Sartem }
177*18c2aff7Sartem 
178*18c2aff7Sartem 
179