~ubuntu-branches/ubuntu/lucid/hal/lucid-proposed

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/***************************************************************************
 *
 * hal-storage-closetray.c : CloseTray method handler
 *
 * Copyright (C) 2006 David Zeuthen, <david@fubar.dk>
 * Copyright (C) 2006 Sun Microsystems, Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 **************************************************************************/


#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include <glib/gstdio.h>
#include <sys/types.h>
#include <unistd.h>

#include <libhal.h>
#include <libhal-storage.h>

#include "hal-storage-shared.h"


static void
usage (void)
{
	fprintf (stderr, "This program should only be started by hald.\n");
	exit (1);
}


static void
unknown_closetray_error (const char *detail)
{
	fprintf (stderr, "org.freedesktop.Hal.Device.Storage.UnknownFailure\n");
	fprintf (stderr, "%s\n", detail);
	exit (1);
}


static void
invalid_closetray_option (const char *option, const char *uid)
{
	fprintf (stderr, "org.freedesktop.Hal.Device.Storage.InvalidCloseTrayOption\n");
	fprintf (stderr, "The option '%s' is not allowed for uid=%s\n", option, uid);
	exit (1);
}

int
main (int argc, char *argv[])
{
	char *udi;
	char *device;
	LibHalDrive *drive;
	DBusError error;
	LibHalContext *hal_ctx = NULL;
	DBusConnection *system_bus = NULL;
	char *invoked_by_uid;
	char *invoked_by_syscon_name;
	int i;
	char closetray_options[1024];
	char **given_options;
	const char *end;

	device = getenv ("HAL_PROP_BLOCK_DEVICE");
	if (device == NULL)
		usage ();

	udi = getenv ("HAL_PROP_INFO_UDI");
	if (udi == NULL)
		usage ();

	invoked_by_uid = getenv ("HAL_METHOD_INVOKED_BY_UID");

	invoked_by_syscon_name = getenv ("HAL_METHOD_INVOKED_BY_SYSTEMBUS_CONNECTION_NAME");

	dbus_error_init (&error);
	if ((hal_ctx = libhal_ctx_init_direct (&error)) == NULL) {
		printf ("Cannot connect to hald\n");
		if (dbus_error_is_set (&error)) {
			dbus_error_free (&error);
		}
		usage ();
	}

	dbus_error_init (&error);
	system_bus = dbus_bus_get (DBUS_BUS_SYSTEM, &error);
	if (system_bus == NULL) {
		printf ("Cannot connect to the system bus\n");
		if (dbus_error_is_set (&error)) {
			dbus_error_free (&error);
		}
		usage ();
	}

	/* read from stdin */
	if (strlen (fgets (closetray_options, sizeof (closetray_options), stdin)) > 0)
		closetray_options [strlen (closetray_options) - 1] = '\0';
	/* validate that input from stdin is UTF-8 */
	if (!g_utf8_validate (closetray_options, -1, &end))
		unknown_closetray_error ("Error validating closetray_options as UTF-8");
#ifdef DEBUG
	printf ("closetray_options  = '%s'\n", closetray_options);
#endif

	/* delete any trailing whitespace options from splitting the string */
	given_options = g_strsplit (closetray_options, "\t", 0);
	for (i = g_strv_length (given_options) - 1; i >= 0; --i) {
		if (strlen (given_options[i]) > 0)
			break;
		given_options[i] = NULL;
	}

	/* check options */
	for (i = 0; given_options[i] != NULL; i++) {
		char *given = given_options[i];

		/* none supported right now */

		invalid_closetray_option (given, invoked_by_uid);
	}
	g_strfreev (given_options);

	/* should be storage */
	if ((drive = libhal_drive_from_udi (hal_ctx, udi)) == NULL) {
		unknown_closetray_error ("Cannot get drive");
	}

	/* use handle_eject() with the closetray option */
	handle_eject (system_bus,
                      hal_ctx, 
		      libhal_drive_get_udi (drive),
		      drive,
		      libhal_drive_get_device_file (drive),
		      invoked_by_uid, 
		      invoked_by_syscon_name,
		      TRUE /* closetray option */);

	return 0;
}