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