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