xref: /illumos-gate/usr/src/cmd/hal/tools/hal_set_property.c (revision 5422785d352a2bb398daceab3d1898a8aa64d006)
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  "           [--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  "        --version        Show version and exit\n"
92  "        --help           Show this information and exit\n"
93  "\n"
94  "This program attempts to set property for a device. Note that, due to\n"
95  "security considerations, it may not be possible to set a property; on\n"
96  "success this program exits with exit code 0. On error, the program exits\n"
97  "with an exit code different from 0\n" "\n");
98 }
99 
100 /** Entry point
101  *
102  *  @param  argc                Number of arguments given to program
103  *  @param  argv                Arguments given to program
104  *  @return                     Return code
105  */
106 int
107 main (int argc, char *argv[])
108 {
109 	dbus_bool_t rc = 0;
110 	char *udi = NULL;
111 	char *key = NULL;
112 	char *str_value = NULL;
113 	dbus_int32_t int_value = 0;
114 	dbus_uint64_t uint64_value = 0;
115 	double double_value = 0.0f;
116 	dbus_bool_t bool_value = TRUE;
117 	dbus_bool_t remove = FALSE;
118 	dbus_bool_t is_version = FALSE;
119 	dbus_bool_t udi_exists = FALSE;
120 	int type = PROP_INVALID;
121 	DBusError error;
122 
123 	if (argc <= 1) {
124 		usage (argc, argv);
125 		return 1;
126 	}
127 
128 	while (1) {
129 		int c;
130 		int option_index = 0;
131 		const char *opt;
132 		static struct option long_options[] = {
133 			{"udi", 1, NULL, 0},
134 			{"key", 1, NULL, 0},
135 			{"int", 1, NULL, 0},
136 			{"uint64", 1, NULL, 0},
137 			{"string", 1, NULL, 0},
138 			{"double", 1, NULL, 0},
139 			{"bool", 1, NULL, 0},
140 			{"strlist-pre", 1, NULL, 0},
141 			{"strlist-post", 1, NULL, 0},
142 			{"strlist-rem", 1, NULL, 0},
143 			{"remove", 0, NULL, 0},
144 			{"version", 0, NULL, 0},
145 			{"help", 0, NULL, 0},
146 			{NULL, 0, NULL, 0}
147 		};
148 
149 		c = getopt_long (argc, argv, "",
150 				 long_options, &option_index);
151 		if (c == -1)
152 			break;
153 
154 		switch (c) {
155 		case 0:
156 			opt = long_options[option_index].name;
157 
158 			if (strcmp (opt, "help") == 0) {
159 				usage (argc, argv);
160 				return 0;
161 			} else if (strcmp (opt, "key") == 0) {
162 				key = strdup (optarg);
163 			} else if (strcmp (opt, "string") == 0) {
164 				str_value = strdup (optarg);
165 				type = PROP_STRING;
166 			} else if (strcmp (opt, "int") == 0) {
167 				int_value = strtol (optarg, NULL, 0);
168 				type = PROP_INT;
169 			} else if (strcmp (opt, "uint64") == 0) {
170 				uint64_value = strtoull (optarg, NULL, 0);
171 				type = PROP_UINT64;
172 			} else if (strcmp (opt, "double") == 0) {
173 				double_value = (double) atof (optarg);
174 				type = PROP_DOUBLE;
175 			} else if (strcmp (opt, "bool") == 0) {
176 				if (strcmp (optarg, "true") == 0)
177 					bool_value = TRUE;
178 				else if (strcmp (optarg, "false") == 0)
179 					bool_value = FALSE;
180 				else {
181 					usage (argc, argv);
182 					return 1;
183 				}
184 				type = PROP_BOOL;
185 			} else if (strcmp (opt, "strlist-pre") == 0) {
186 				str_value = strdup (optarg);
187 				type = PROP_STRLIST_PRE;
188 			} else if (strcmp (opt, "strlist-post") == 0) {
189 				str_value = strdup (optarg);
190 				type = PROP_STRLIST_POST;
191 			} else if (strcmp (opt, "strlist-rem") == 0) {
192 				str_value = strdup (optarg);
193 				type = PROP_STRLIST_REM;
194 			} else if (strcmp (opt, "remove") == 0) {
195 				remove = TRUE;
196 			} else if (strcmp (opt, "udi") == 0) {
197 				udi = strdup (optarg);
198 			} else if (strcmp (opt, "version") == 0) {
199 				is_version = TRUE;
200 			}
201 			break;
202 
203 		default:
204 			usage (argc, argv);
205 			return 1;
206 			break;
207 		}
208 	}
209 
210 	if (is_version) {
211 		printf ("hal-set-property " PACKAGE_VERSION "\n");
212 		return 0;
213 	}
214 
215 	/* must have at least one, but not neither or both */
216 	if ((remove && type != PROP_INVALID) || ((!remove) && type == PROP_INVALID)) {
217 		usage (argc, argv);
218 		return 1;
219 	}
220 
221 	fprintf (stderr, "\n");
222 
223 	dbus_error_init (&error);
224 	if ((hal_ctx = libhal_ctx_new ()) == NULL) {
225 		fprintf (stderr, "error: libhal_ctx_new\n");
226 		return 1;
227 	}
228 	if (!libhal_ctx_set_dbus_connection (hal_ctx, dbus_bus_get (DBUS_BUS_SYSTEM, &error))) {
229 		fprintf (stderr, "error: libhal_ctx_set_dbus_connection: %s: %s\n", error.name, error.message);
230 		LIBHAL_FREE_DBUS_ERROR (&error);
231 		return 1;
232 	}
233 	if (!libhal_ctx_init (hal_ctx, &error)) {
234 		if (dbus_error_is_set(&error)) {
235 			fprintf (stderr, "error: libhal_ctx_init: %s: %s\n", error.name, error.message);
236 			LIBHAL_FREE_DBUS_ERROR (&error);
237 		}
238 		fprintf (stderr, "Could not initialise connection to hald.\n"
239 				 "Normally this means the HAL daemon (hald) is not running or not ready.\n");
240 		return 1;
241 	}
242 
243 	 /* check UDI exists */
244 	udi_exists = libhal_device_exists (hal_ctx, udi, &error);
245 	if (!udi_exists) {
246 		fprintf (stderr, "error: UDI %s does not exist\n", udi);
247 		return 1;
248 	}
249 	if (dbus_error_is_set(&error)) {
250 		fprintf (stderr, "error: libhal_device_exists: %s: %s\n", error.name, error.message);
251 		dbus_error_free (&error);
252 		return 1;
253 	}
254 
255 	if (remove) {
256 		rc = libhal_device_remove_property (hal_ctx, udi, key, &error);
257 		if (!rc) {
258 			if (dbus_error_is_set(&error)) {
259 			        fprintf (stderr, "error: libhal_device_remove_property: %s: %s\n", error.name, error.message);
260 				dbus_error_free (&error);
261 			} else {
262 				fprintf (stderr, "error: libhal_device_remove_property: invalid params.\n");
263 			}
264 			return 1;
265 		}
266 	} else {
267 		switch (type) {
268 		case PROP_STRING:
269 			rc = libhal_device_set_property_string (hal_ctx, udi, key, str_value, &error);
270 			break;
271 		case PROP_INT:
272 			rc = libhal_device_set_property_int (hal_ctx, udi, key, int_value, &error);
273 			break;
274 		case PROP_UINT64:
275 			rc = libhal_device_set_property_uint64 (hal_ctx, udi, key, uint64_value, &error);
276 			break;
277 		case PROP_DOUBLE:
278 			rc = libhal_device_set_property_double (hal_ctx, udi, key, double_value, &error);
279 			break;
280 		case PROP_BOOL:
281 			rc = libhal_device_set_property_bool (hal_ctx, udi, key, bool_value, &error);
282 			break;
283 		case PROP_STRLIST_PRE:
284 			rc = libhal_device_property_strlist_prepend (hal_ctx, udi, key, str_value, &error);
285 			break;
286 		case PROP_STRLIST_POST:
287 			rc = libhal_device_property_strlist_append (hal_ctx, udi, key, str_value, &error);
288 			break;
289 		case PROP_STRLIST_REM:
290 			rc = libhal_device_property_strlist_remove (hal_ctx, udi, key, str_value, &error);
291 			break;
292 		}
293 		if (!rc) {
294 			if (dbus_error_is_set(&error)) {
295 			        fprintf (stderr, "error: libhal_device_set_property: %s: %s\n", error.name, error.message);
296 				dbus_error_free (&error);
297 			} else {
298 				fprintf (stderr, "error: libhal_device_set_property: invalid params.\n");
299 			}
300 			return 1;
301 		}
302 	}
303 
304 	return rc ? 0 : 1;
305 }
306 
307 /**
308  * @}
309  */
310