xref: /illumos-gate/usr/src/cmd/hal/tools/hal_set_property.c (revision 0250c53ad267726f2438e3c6556199a0bbf588a2)
1 /***************************************************************************
2  * CVSID: $Id$
3  *
4  * hal_set_property.c : Set property for a device
5  *
6  * Copyright (C) 2003 David Zeuthen, <david@fubar.dk>
7  *
8  * Licensed under the Academic Free License version 2.1
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
23  *
24  **************************************************************************/
25 
26 
27 #ifdef HAVE_CONFIG_H
28 #  include <config.h>
29 #endif
30 
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <unistd.h>
35 #include <getopt.h>
36 
37 #include <libhal.h>
38 
39 static LibHalContext *hal_ctx;
40 
41 enum property_op {
42 	PROP_INT,
43 	PROP_UINT64,
44 	PROP_STRING,
45 	PROP_DOUBLE,
46 	PROP_BOOL,
47 	PROP_STRLIST_PRE,
48 	PROP_STRLIST_POST,
49 	PROP_STRLIST_REM,
50 	PROP_INVALID
51 };
52 
53 /**
54  * @defgroup HalSetProperty  Set HAL device property
55  * @ingroup HalMisc
56  *
57  * @brief A commandline tool setting a property of a device. Uses libhal
58  *
59  * @{
60  */
61 
62 /** Print out program usage.
63  *
64  *  @param  argc                Number of arguments given to program
65  *  @param  argv                Arguments given to program
66  */
67 static void
68 usage (int argc, char *argv[])
69 {
70 	fprintf (stderr,
71  "\n"
72  "usage : hal-set-property --udi <udi> --key <key>\n"
73  "           (--int <value> | --string <value> | --bool <value> |\n"
74  "            --strlist-pre <value> | --strlist-post <value> |\n"
75  "            --strlist-rem <value> | --double <value> | --remove)\n"
76  "           [--direct] [--help] [--version]\n");
77 	fprintf (stderr,
78  "\n" "        --udi            Unique Device Id\n"
79  "        --key            Key of the property to set\n"
80  "        --int            Set value to an integer. Accepts decimal and "
81  "                         hexadecimal prefixed with 0x or x\n"
82  "        --uint64         Set value to an integer. Accepts decimal and "
83  "                         hexadecimal prefixed with 0x or x\n"
84  "        --string         Set value to a string\n"
85  "        --double         Set value to a floating point number\n"
86  "        --bool           Set value to a boolean, ie. true or false\n"
87  "        --strlist-pre    Prepend a string to a list\n"
88  "        --strlist-post   Append a string to a list\n"
89  "        --strlist-rem    Remove a string from a list\n"
90  "        --remove         Indicates that the property should be removed\n"
91  "        --direct         Use direct HAL connection\n"
92  "        --version        Show version and exit\n"
93  "        --help           Show this information and exit\n"
94  "\n"
95  "This program attempts to set property for a device. Note that, due to\n"
96  "security considerations, it may not be possible to set a property; on\n"
97  "success this program exits with exit code 0. On error, the program exits\n"
98  "with an exit code different from 0\n" "\n");
99 }
100 
101 /** Entry point
102  *
103  *  @param  argc                Number of arguments given to program
104  *  @param  argv                Arguments given to program
105  *  @return                     Return code
106  */
107 int
108 main (int argc, char *argv[])
109 {
110 	dbus_bool_t rc = 0;
111 	char *udi = NULL;
112 	char *key = NULL;
113 	char *str_value = NULL;
114 	dbus_int32_t int_value = 0;
115 	dbus_uint64_t uint64_value = 0;
116 	double double_value = 0.0f;
117 	dbus_bool_t bool_value = TRUE;
118 	dbus_bool_t remove = FALSE;
119 	dbus_bool_t is_version = FALSE;
120 	dbus_bool_t udi_exists = FALSE;
121 	int type = PROP_INVALID;
122 	DBusError error;
123 	dbus_bool_t direct = FALSE;
124 
125 	if (argc <= 1) {
126 		usage (argc, argv);
127 		return 1;
128 	}
129 
130 	while (1) {
131 		int c;
132 		int option_index = 0;
133 		const char *opt;
134 		static struct option long_options[] = {
135 			{"udi", 1, NULL, 0},
136 			{"key", 1, NULL, 0},
137 			{"int", 1, NULL, 0},
138 			{"uint64", 1, NULL, 0},
139 			{"string", 1, NULL, 0},
140 			{"double", 1, NULL, 0},
141 			{"bool", 1, NULL, 0},
142 			{"strlist-pre", 1, NULL, 0},
143 			{"strlist-post", 1, NULL, 0},
144 			{"strlist-rem", 1, NULL, 0},
145 			{"direct", 0, NULL, 0},
146 			{"remove", 0, NULL, 0},
147 			{"version", 0, NULL, 0},
148 			{"help", 0, NULL, 0},
149 			{NULL, 0, NULL, 0}
150 		};
151 
152 		c = getopt_long (argc, argv, "",
153 				 long_options, &option_index);
154 		if (c == -1)
155 			break;
156 
157 		switch (c) {
158 		case 0:
159 			opt = long_options[option_index].name;
160 
161 			if (strcmp (opt, "help") == 0) {
162 				usage (argc, argv);
163 				return 0;
164 			} else if (strcmp (opt, "key") == 0) {
165 				key = strdup (optarg);
166 			} else if (strcmp (opt, "string") == 0) {
167 				str_value = strdup (optarg);
168 				type = PROP_STRING;
169 			} else if (strcmp (opt, "int") == 0) {
170 				int_value = strtol (optarg, NULL, 0);
171 				type = PROP_INT;
172 			} else if (strcmp (opt, "uint64") == 0) {
173 				uint64_value = strtoull (optarg, NULL, 0);
174 				type = PROP_UINT64;
175 			} else if (strcmp (opt, "double") == 0) {
176 				double_value = (double) atof (optarg);
177 				type = PROP_DOUBLE;
178 			} else if (strcmp (opt, "bool") == 0) {
179 				if (strcmp (optarg, "true") == 0)
180 					bool_value = TRUE;
181 				else if (strcmp (optarg, "false") == 0)
182 					bool_value = FALSE;
183 				else {
184 					usage (argc, argv);
185 					return 1;
186 				}
187 				type = PROP_BOOL;
188 			} else if (strcmp (opt, "strlist-pre") == 0) {
189 				str_value = strdup (optarg);
190 				type = PROP_STRLIST_PRE;
191 			} else if (strcmp (opt, "strlist-post") == 0) {
192 				str_value = strdup (optarg);
193 				type = PROP_STRLIST_POST;
194 			} else if (strcmp (opt, "strlist-rem") == 0) {
195 				str_value = strdup (optarg);
196 				type = PROP_STRLIST_REM;
197 			} else if (strcmp (opt, "remove") == 0) {
198 				remove = TRUE;
199 			} else if (strcmp (opt, "direct") == 0) {
200 				direct = TRUE;
201 			} else if (strcmp (opt, "udi") == 0) {
202 				udi = strdup (optarg);
203 			} else if (strcmp (opt, "version") == 0) {
204 				is_version = TRUE;
205 			}
206 			break;
207 
208 		default:
209 			usage (argc, argv);
210 			return 1;
211 			break;
212 		}
213 	}
214 
215 	if (is_version) {
216 		printf ("hal-set-property " PACKAGE_VERSION "\n");
217 		return 0;
218 	}
219 
220 	/* must have at least one, but not neither or both */
221 	if ((remove && type != PROP_INVALID) || ((!remove) && type == PROP_INVALID)) {
222 		usage (argc, argv);
223 		return 1;
224 	}
225 
226 	fprintf (stderr, "\n");
227 
228 	dbus_error_init (&error);
229 	if (direct) {
230 		if ((hal_ctx = libhal_ctx_init_direct (&error)) == NULL) {
231 			fprintf (stderr, "error: libhal_ctx_init_direct\n");
232 			LIBHAL_FREE_DBUS_ERROR (&error);
233 			return 1;
234 		}
235 	} else {
236 		if ((hal_ctx = libhal_ctx_new ()) == NULL) {
237 			fprintf (stderr, "error: libhal_ctx_new\n");
238 			return 1;
239 		}
240 		if (!libhal_ctx_set_dbus_connection (hal_ctx, dbus_bus_get (DBUS_BUS_SYSTEM, &error))) {
241 			fprintf (stderr, "error: libhal_ctx_set_dbus_connection: %s: %s\n", error.name, error.message);
242 			LIBHAL_FREE_DBUS_ERROR (&error);
243 			return 1;
244 		}
245 		if (!libhal_ctx_init (hal_ctx, &error)) {
246 			if (dbus_error_is_set(&error)) {
247 				fprintf (stderr, "error: libhal_ctx_init: %s: %s\n", error.name, error.message);
248 				LIBHAL_FREE_DBUS_ERROR (&error);
249 			}
250 			fprintf (stderr, "Could not initialise connection to hald.\n"
251 					 "Normally this means the HAL daemon (hald) is not running or not ready.\n");
252 			return 1;
253 		}
254 	}
255 
256 	 /* check UDI exists */
257 	udi_exists = libhal_device_exists (hal_ctx, udi, &error);
258 	if (!udi_exists) {
259 		fprintf (stderr, "error: UDI %s does not exist\n", udi);
260 		return 1;
261 	}
262 	if (dbus_error_is_set(&error)) {
263 		fprintf (stderr, "error: libhal_device_exists: %s: %s\n", error.name, error.message);
264 		dbus_error_free (&error);
265 		return 1;
266 	}
267 
268 	if (remove) {
269 		rc = libhal_device_remove_property (hal_ctx, udi, key, &error);
270 		if (!rc) {
271 			if (dbus_error_is_set(&error)) {
272 			        fprintf (stderr, "error: libhal_device_remove_property: %s: %s\n", error.name, error.message);
273 				dbus_error_free (&error);
274 			} else {
275 				fprintf (stderr, "error: libhal_device_remove_property: invalid params.\n");
276 			}
277 			return 1;
278 		}
279 	} else {
280 		switch (type) {
281 		case PROP_STRING:
282 			rc = libhal_device_set_property_string (hal_ctx, udi, key, str_value, &error);
283 			break;
284 		case PROP_INT:
285 			rc = libhal_device_set_property_int (hal_ctx, udi, key, int_value, &error);
286 			break;
287 		case PROP_UINT64:
288 			rc = libhal_device_set_property_uint64 (hal_ctx, udi, key, uint64_value, &error);
289 			break;
290 		case PROP_DOUBLE:
291 			rc = libhal_device_set_property_double (hal_ctx, udi, key, double_value, &error);
292 			break;
293 		case PROP_BOOL:
294 			rc = libhal_device_set_property_bool (hal_ctx, udi, key, bool_value, &error);
295 			break;
296 		case PROP_STRLIST_PRE:
297 			rc = libhal_device_property_strlist_prepend (hal_ctx, udi, key, str_value, &error);
298 			break;
299 		case PROP_STRLIST_POST:
300 			rc = libhal_device_property_strlist_append (hal_ctx, udi, key, str_value, &error);
301 			break;
302 		case PROP_STRLIST_REM:
303 			rc = libhal_device_property_strlist_remove (hal_ctx, udi, key, str_value, &error);
304 			break;
305 		}
306 		if (!rc) {
307 			if (dbus_error_is_set(&error)) {
308 			        fprintf (stderr, "error: libhal_device_set_property: %s: %s\n", error.name, error.message);
309 				dbus_error_free (&error);
310 			} else {
311 				fprintf (stderr, "error: libhal_device_set_property: invalid params.\n");
312 			}
313 			return 1;
314 		}
315 	}
316 
317 	return rc ? 0 : 1;
318 }
319 
320 /**
321  * @}
322  */
323