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 **************************************************************************/
33 #include <glib/gstdio.h>
34 #include <sys/types.h>
38 #include <libhal-storage.h>
40 #include <libpolkit.h>
43 #include "hal-storage-shared.h"
49 fprintf (stderr
, "This program should only be started by hald.\n");
55 unknown_closetray_error (const char *detail
)
57 fprintf (stderr
, "org.freedesktop.Hal.Device.Storage.UnknownFailure\n");
58 fprintf (stderr
, "%s\n", detail
);
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
);
72 #error Need FreeBSD specific changes here
77 main (int argc
, char *argv
[])
83 LibHalContext
*hal_ctx
= NULL
;
84 DBusConnection
*system_bus
= NULL
;
86 LibPolKitContext
*pol_ctx
= NULL
;
89 char *invoked_by_syscon_name
;
91 char closetray_options
[1024];
95 device
= getenv ("HAL_PROP_BLOCK_DEVICE");
99 udi
= getenv ("HAL_PROP_INFO_UDI");
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
);
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
);
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");
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");
136 printf ("closetray_options = '%s'\n", closetray_options
);
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)
144 given_options
[i
] = NULL
;
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
,
167 libhal_drive_get_udi (drive
),
169 libhal_drive_get_device_file (drive
),
171 invoked_by_syscon_name
,
172 TRUE
/* closetray option */,