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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
|
/***************************************************************************
* CVSID: $Id$
*
* hal-disable-polling.c : Disable polling on a drive
*
* Copyright (C) 2007 David Zeuthen, <david@fubar.dk>
*
* Licensed under the Academic Free License version 2.1
*
* 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 <string.h>
#include <unistd.h>
#include <getopt.h>
#include <glib.h>
#include <libhal.h>
/**
* usage:
* @argc: Number of arguments given to program
* @argv: Arguments given to program
*
* Print out program usage.
*/
static void
usage (int argc, char *argv[])
{
fprintf (stderr,
"\n"
"usage : hal-disable-polling [--udi <udi> | --device <device-file>]\n"
" [--enable-polling]\n"
" [--help] [--version]\n");
fprintf (stderr,
"\n"
" --udi Unique Device Id\n"
" --device Device file\n"
" --enable-polling Enable polling instead of disabling it\n"
" --version Show version and exit\n"
" --help Show this information and exit\n"
"\n"
"This program is provided to make HAL stop polling a drive. Please read.\n"
"the entire manual page before using this program.\n"
"\n");
}
/**
* main:
* @argc: Number of arguments given to program
* @argv: Arguments given to program
*
* Returns: Return code
*
* Main entry point
*/
int
main (int argc, char *argv[])
{
char *udi = NULL;
char *device = NULL;
dbus_bool_t is_version = FALSE;
dbus_bool_t enable_polling = FALSE;
DBusError error;
LibHalContext *hal_ctx;
FILE *f;
char *filename;
if (argc <= 1) {
usage (argc, argv);
return 1;
}
while (1) {
int c;
int option_index = 0;
const char *opt;
static struct option long_options[] = {
{"udi", 1, NULL, 0},
{"device", 1, NULL, 0},
{"enable-polling", 0, NULL, 0},
{"version", 0, NULL, 0},
{"help", 0, NULL, 0},
{NULL, 0, NULL, 0}
};
c = getopt_long (argc, argv, "",
long_options, &option_index);
if (c == -1)
break;
switch (c) {
case 0:
opt = long_options[option_index].name;
if (strcmp (opt, "help") == 0) {
usage (argc, argv);
return 0;
} else if (strcmp (opt, "version") == 0) {
is_version = TRUE;
} else if (strcmp (opt, "udi") == 0) {
udi = strdup (optarg);
} else if (strcmp (opt, "device") == 0) {
device = strdup (optarg);
} else if (strcmp (opt, "enable-polling") == 0) {
enable_polling = TRUE;
}
break;
default:
usage (argc, argv);
return 1;
break;
}
}
if (is_version) {
printf ("hal-disable-polling " PACKAGE_VERSION "\n");
return 0;
}
if (udi == NULL && device == NULL) {
usage (argc, argv);
return 1;
}
if (udi != NULL && device != NULL) {
usage (argc, argv);
return 1;
}
dbus_error_init (&error);
if ((hal_ctx = libhal_ctx_new ()) == NULL) {
fprintf (stderr, "error: libhal_ctx_new\n");
return 1;
}
if (!libhal_ctx_set_dbus_connection (hal_ctx, dbus_bus_get (DBUS_BUS_SYSTEM, &error))) {
fprintf (stderr, "error: libhal_ctx_set_dbus_connection: %s: %s\n", error.name, error.message);
LIBHAL_FREE_DBUS_ERROR (&error);
return 1;
}
if (!libhal_ctx_init (hal_ctx, &error)) {
if (dbus_error_is_set(&error)) {
fprintf (stderr, "error: libhal_ctx_init: %s: %s\n", error.name, error.message);
dbus_error_free (&error);
}
fprintf (stderr, "Could not initialise connection to hald.\n"
"Normally this means the HAL daemon (hald) is not running or not ready.\n");
return 1;
}
if (getuid () != 0) {
fprintf (stderr, "This program requires super user (root) privileges.\n");
return 1;
}
if (device != NULL) {
char **devices;
int num_devices;
int n;
devices = libhal_manager_find_device_string_match (hal_ctx, "block.device", device, &num_devices, NULL);
if (devices == NULL) {
fprintf (stderr, "Cannot find device %s.\n", device);
return 1;
}
for (n = 0; devices[n] != NULL; n++) {
if (libhal_device_query_capability (hal_ctx, devices[n], "storage", NULL)) {
udi = devices[n];
break;
}
}
if (udi == NULL) {
fprintf (stderr, "Cannot find storage device %s.\n", device);
return 1;
}
/* mmmkay, we don't care about leaking the variable devices... mmkay? mmkay! */
} else {
if (!libhal_device_exists (hal_ctx, udi, &error)) {
fprintf (stderr, "Cannot find device with udi %s.\n", udi);
return 1;
}
if (!libhal_device_query_capability (hal_ctx, udi, "storage", NULL)) {
fprintf (stderr, "Device with udi %s is not a storage device.\n", udi);
return 1;
}
device = libhal_device_get_property_string (hal_ctx, udi, "block.device", NULL);
if (device == NULL) {
fprintf (stderr, "Device with udi %s does not have block.device set.\n", udi);
return 1;
}
}
if (!libhal_device_get_property_bool (hal_ctx, udi, "storage.removable", NULL)) {
fprintf (stderr, "The given drive don't use removable media so it's not polled anyway.\n");
return 1;
}
filename = g_strdup_printf (PACKAGE_SYSCONF_DIR "/hal/fdi/information/media-check-disable-%s.fdi",
g_basename (udi));
if (enable_polling) {
if (libhal_device_get_property_bool (hal_ctx, udi, "storage.media_check_enabled", NULL)) {
fprintf (stderr, "Polling is already enabled on the given drive.\n");
return 1;
}
if (!g_file_test (filename, G_FILE_TEST_EXISTS)) {
fprintf (stderr, "Cannot find fdi file %s. Perhaps polling wasn't disabled using this tool?\n", filename);
return 1;
}
if (unlink (filename) != 0) {
fprintf (stderr, "Cannot delete fdi file %s.\n", filename);
return 1;
}
} else {
if (!libhal_device_get_property_bool (hal_ctx, udi, "storage.media_check_enabled", NULL)) {
fprintf (stderr, "Polling is already disabled on the given drive.\n");
return 1;
}
if (g_file_test (filename, G_FILE_TEST_EXISTS)) {
fprintf (stderr, "The fdi file %s already exist. Cowardly refusing to overwrite it.\n", filename);
return 1;
}
f = fopen (filename, "w");
if (f == NULL) {
fprintf (stderr, "Cannot open %s for writing.\n", filename);
return 1;
}
fprintf (f, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
"\n"
"<deviceinfo version=\"0.2\">\n"
" <device>\n"
" <match key=\"info.udi\" string=\"%s\">\n"
" <merge key=\"storage.media_check_enabled\" type=\"bool\">false</merge>\n"
" </match>\n"
" </device>\n"
"</deviceinfo>\n"
"\n", udi);
fclose (f);
}
libhal_device_reprobe (hal_ctx, udi, &error);
if (enable_polling)
printf ("Polling for drive %s have been enabled. The fdi file deleted was\n"
" %s\n", device, filename);
else
printf ("Polling for drive %s have been disabled. The fdi file written was\n"
" %s\n", device, filename);
return 0;
}
|