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
277
278
279
280
|
/***************************************************************************
* CVSID: $Id$
*
* hal_set_property.c : Set property for a device
*
* Copyright (C) 2003 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 <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <getopt.h>
#include <libhal.h>
static LibHalContext *hal_ctx;
enum property_op {
PROP_INT,
PROP_UINT64,
PROP_STRING,
PROP_DOUBLE,
PROP_BOOL,
PROP_STRLIST_PRE,
PROP_STRLIST_POST,
PROP_STRLIST_REM,
PROP_INVALID
};
/**
* 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-set-property --udi <udi> --key <key>\n"
" (--int <value> | --string <value> | --bool <value> |\n"
" --strlist-pre <value> | --strlist-post <value> |\n"
" --strlist-rem <value> | --double <value> | --remove)\n"
" [--help] [--version]\n");
fprintf (stderr,
"\n" " --udi Unique Device Id\n"
" --key Key of the property to set\n"
" --int Set value to an integer. Accepts decimal and "
" hexadecimal prefixed with 0x or x\n"
" --uint64 Set value to an integer. Accepts decimal and "
" hexadecimal prefixed with 0x or x\n"
" --string Set value to a string\n"
" --double Set value to a floating point number\n"
" --bool Set value to a boolean, ie. true or false\n"
" --strlist-pre Prepend a string to a list\n"
" --strlist-post Append a string to a list\n"
" --strlist-rem Remove a string from a list\n"
" --remove Indicates that the property should be removed\n"
" --version Show version and exit\n"
" --help Show this information and exit\n"
"\n"
"This program attempts to set property for a device. Note that, due to\n"
"security considerations, it may not be possible to set a property; on\n"
"success this program exits with exit code 0. On error, the program exits\n"
"with an exit code different from 0\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[])
{
dbus_bool_t rc = 0;
char *udi = NULL;
char *key = NULL;
char *str_value = NULL;
dbus_int32_t int_value = 0;
dbus_uint64_t uint64_value = 0;
double double_value = 0.0f;
dbus_bool_t bool_value = TRUE;
dbus_bool_t remove = FALSE;
dbus_bool_t is_version = FALSE;
int type = PROP_INVALID;
DBusError error;
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},
{"key", 1, NULL, 0},
{"int", 1, NULL, 0},
{"uint64", 1, NULL, 0},
{"string", 1, NULL, 0},
{"double", 1, NULL, 0},
{"bool", 1, NULL, 0},
{"strlist-pre", 1, NULL, 0},
{"strlist-post", 1, NULL, 0},
{"strlist-rem", 1, NULL, 0},
{"remove", 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, "key") == 0) {
key = strdup (optarg);
} else if (strcmp (opt, "string") == 0) {
str_value = strdup (optarg);
type = PROP_STRING;
} else if (strcmp (opt, "int") == 0) {
int_value = strtol (optarg, NULL, 0);
type = PROP_INT;
} else if (strcmp (opt, "uint64") == 0) {
uint64_value = strtoull (optarg, NULL, 0);
type = PROP_UINT64;
} else if (strcmp (opt, "double") == 0) {
double_value = (double) atof (optarg);
type = PROP_DOUBLE;
} else if (strcmp (opt, "bool") == 0) {
if (strcmp (optarg, "true") == 0)
bool_value = TRUE;
else if (strcmp (optarg, "false") == 0)
bool_value = FALSE;
else {
usage (argc, argv);
return 1;
}
type = PROP_BOOL;
} else if (strcmp (opt, "strlist-pre") == 0) {
str_value = strdup (optarg);
type = PROP_STRLIST_PRE;
} else if (strcmp (opt, "strlist-post") == 0) {
str_value = strdup (optarg);
type = PROP_STRLIST_POST;
} else if (strcmp (opt, "strlist-rem") == 0) {
str_value = strdup (optarg);
type = PROP_STRLIST_REM;
} else if (strcmp (opt, "remove") == 0) {
remove = TRUE;
} else if (strcmp (opt, "udi") == 0) {
udi = strdup (optarg);
} else if (strcmp (opt, "version") == 0) {
is_version = TRUE;
}
break;
default:
usage (argc, argv);
return 1;
break;
}
}
if (is_version) {
printf ("hal-set-property " PACKAGE_VERSION "\n");
return 0;
}
/* must have at least one, but not neither or both */
if ((remove && type != PROP_INVALID) || ((!remove) && type == PROP_INVALID)) {
usage (argc, argv);
return 1;
}
fprintf (stderr, "\n");
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 (remove) {
rc = libhal_device_remove_property (hal_ctx, udi, key, &error);
if (!rc) {
fprintf (stderr, "error: libhal_device_remove_property: %s: %s\n", error.name, error.message);
LIBHAL_FREE_DBUS_ERROR (&error);
return 1;
}
} else {
switch (type) {
case PROP_STRING:
rc = libhal_device_set_property_string (hal_ctx, udi, key, str_value, &error);
break;
case PROP_INT:
rc = libhal_device_set_property_int (hal_ctx, udi, key, int_value, &error);
break;
case PROP_UINT64:
rc = libhal_device_set_property_uint64 (hal_ctx, udi, key, uint64_value, &error);
break;
case PROP_DOUBLE:
rc = libhal_device_set_property_double (hal_ctx, udi, key, double_value, &error);
break;
case PROP_BOOL:
rc = libhal_device_set_property_bool (hal_ctx, udi, key, bool_value, &error);
break;
case PROP_STRLIST_PRE:
rc = libhal_device_property_strlist_prepend (hal_ctx, udi, key, str_value, &error);
break;
case PROP_STRLIST_POST:
rc = libhal_device_property_strlist_append (hal_ctx, udi, key, str_value, &error);
break;
case PROP_STRLIST_REM:
rc = libhal_device_property_strlist_remove (hal_ctx, udi, key, str_value, &error);
break;
}
if (!rc) {
fprintf (stderr, "error: libhal_device_set_property: %s: %s\n", error.name, error.message);
dbus_error_free (&error);
return 1;
}
}
return rc ? 0 : 1;
}
|