dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / hal / tools / hal-storage-closetray.c
blob9dbfd05e4f9dc675bf023dc3da7003345081db6e
1 /***************************************************************************
3 * hal-storage-closetray.c : CloseTray method handler
5 * Copyright (C) 2006 David Zeuthen, <david@fubar.dk>
6 * Copyright (C) 2006 Sun Microsystems, Inc.
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.
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.
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
22 **************************************************************************/
25 #ifdef HAVE_CONFIG_H
26 # include <config.h>
27 #endif
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>
37 #include <libhal.h>
38 #include <libhal-storage.h>
39 #ifdef HAVE_POLKIT
40 #include <libpolkit.h>
41 #endif
43 #include "hal-storage-shared.h"
46 static void
47 usage (void)
49 fprintf (stderr, "This program should only be started by hald.\n");
50 exit (1);
54 void static
55 unknown_closetray_error (const char *detail)
57 fprintf (stderr, "org.freedesktop.Hal.Device.Storage.UnknownFailure\n");
58 fprintf (stderr, "%s\n", detail);
59 exit (1);
63 static void
64 invalid_closetray_option (const char *option, const char *uid)
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);
71 #ifdef __FreeBSD__
72 #error Need FreeBSD specific changes here
73 #endif
76 int
77 main (int argc, char *argv[])
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;
95 device = getenv ("HAL_PROP_BLOCK_DEVICE");
96 if (device == NULL)
97 usage ();
99 udi = getenv ("HAL_PROP_INFO_UDI");
100 if (udi == NULL)
101 usage ();
103 invoked_by_uid = getenv ("HAL_METHOD_INVOKED_BY_UID");
105 invoked_by_syscon_name = getenv ("HAL_METHOD_INVOKED_BY_SYSTEMBUS_CONNECTION_NAME");
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 ();
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 ();
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");
127 #endif
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
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;
147 /* check options */
148 for (i = 0; given_options[i] != NULL; i++) {
149 char *given = given_options[i];
151 /* none supported right now */
153 invalid_closetray_option (given, invoked_by_uid);
155 g_strfreev (given_options);
157 /* should be storage */
158 if ((drive = libhal_drive_from_udi (hal_ctx, udi)) == NULL) {
159 unknown_closetray_error ("Cannot get drive");
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);
175 return 0;