xref: /titanic_44/usr/src/cmd/hal/tools/hal_set_property.c (revision b0fc0e77220f1fa4c933fd58a4e1dedcd650b0f1)
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 	int type = PROP_INVALID;
120 	DBusError error;
121 
122 	if (argc <= 1) {
123 		usage (argc, argv);
124 		return 1;
125 	}
126 
127 	while (1) {
128 		int c;
129 		int option_index = 0;
130 		const char *opt;
131 		static struct option long_options[] = {
132 			{"udi", 1, NULL, 0},
133 			{"key", 1, NULL, 0},
134 			{"int", 1, NULL, 0},
135 			{"uint64", 1, NULL, 0},
136 			{"string", 1, NULL, 0},
137 			{"double", 1, NULL, 0},
138 			{"bool", 1, NULL, 0},
139 			{"strlist-pre", 1, NULL, 0},
140 			{"strlist-post", 1, NULL, 0},
141 			{"strlist-rem", 1, NULL, 0},
142 			{"remove", 0, NULL, 0},
143 			{"version", 0, NULL, 0},
144 			{"help", 0, NULL, 0},
145 			{NULL, 0, NULL, 0}
146 		};
147 
148 		c = getopt_long (argc, argv, "",
149 				 long_options, &option_index);
150 		if (c == -1)
151 			break;
152 
153 		switch (c) {
154 		case 0:
155 			opt = long_options[option_index].name;
156 
157 			if (strcmp (opt, "help") == 0) {
158 				usage (argc, argv);
159 				return 0;
160 			} else if (strcmp (opt, "key") == 0) {
161 				key = strdup (optarg);
162 			} else if (strcmp (opt, "string") == 0) {
163 				str_value = strdup (optarg);
164 				type = PROP_STRING;
165 			} else if (strcmp (opt, "int") == 0) {
166 				int_value = strtol (optarg, NULL, 0);
167 				type = PROP_INT;
168 			} else if (strcmp (opt, "uint64") == 0) {
169 				uint64_value = strtoull (optarg, NULL, 0);
170 				type = PROP_UINT64;
171 			} else if (strcmp (opt, "double") == 0) {
172 				double_value = (double) atof (optarg);
173 				type = PROP_DOUBLE;
174 			} else if (strcmp (opt, "bool") == 0) {
175 				if (strcmp (optarg, "true") == 0)
176 					bool_value = TRUE;
177 				else if (strcmp (optarg, "false") == 0)
178 					bool_value = FALSE;
179 				else {
180 					usage (argc, argv);
181 					return 1;
182 				}
183 				type = PROP_BOOL;
184 			} else if (strcmp (opt, "strlist-pre") == 0) {
185 				str_value = strdup (optarg);
186 				type = PROP_STRLIST_PRE;
187 			} else if (strcmp (opt, "strlist-post") == 0) {
188 				str_value = strdup (optarg);
189 				type = PROP_STRLIST_POST;
190 			} else if (strcmp (opt, "strlist-rem") == 0) {
191 				str_value = strdup (optarg);
192 				type = PROP_STRLIST_REM;
193 			} else if (strcmp (opt, "remove") == 0) {
194 				remove = TRUE;
195 			} else if (strcmp (opt, "udi") == 0) {
196 				udi = strdup (optarg);
197 			} else if (strcmp (opt, "version") == 0) {
198 				is_version = TRUE;
199 			}
200 			break;
201 
202 		default:
203 			usage (argc, argv);
204 			return 1;
205 			break;
206 		}
207 	}
208 
209 	if (is_version) {
210 		printf ("hal-set-property " PACKAGE_VERSION "\n");
211 		return 0;
212 	}
213 
214 	/* must have at least one, but not neither or both */
215 	if ((remove && type != PROP_INVALID) || ((!remove) && type == PROP_INVALID)) {
216 		usage (argc, argv);
217 		return 1;
218 	}
219 
220 	fprintf (stderr, "\n");
221 
222 	dbus_error_init (&error);
223 	if ((hal_ctx = libhal_ctx_new ()) == NULL) {
224 		fprintf (stderr, "error: libhal_ctx_new\n");
225 		return 1;
226 	}
227 	if (!libhal_ctx_set_dbus_connection (hal_ctx, dbus_bus_get (DBUS_BUS_SYSTEM, &error))) {
228 		fprintf (stderr, "error: libhal_ctx_set_dbus_connection: %s: %s\n", error.name, error.message);
229 		LIBHAL_FREE_DBUS_ERROR (&error);
230 		return 1;
231 	}
232 	if (!libhal_ctx_init (hal_ctx, &error)) {
233 		if (dbus_error_is_set(&error)) {
234 			fprintf (stderr, "error: libhal_ctx_init: %s: %s\n", error.name, error.message);
235 			LIBHAL_FREE_DBUS_ERROR (&error);
236 		}
237 		fprintf (stderr, "Could not initialise connection to hald.\n"
238 				 "Normally this means the HAL daemon (hald) is not running or not ready.\n");
239 		return 1;
240 	}
241 
242 	if (remove) {
243 		rc = libhal_device_remove_property (hal_ctx, udi, key, &error);
244 		if (!rc) {
245 			fprintf (stderr, "error: libhal_device_remove_property: %s: %s\n", error.name, error.message);
246 			LIBHAL_FREE_DBUS_ERROR (&error);
247 			return 1;
248 		}
249 	} else {
250 		switch (type) {
251 		case PROP_STRING:
252 			rc = libhal_device_set_property_string (hal_ctx, udi, key, str_value, &error);
253 			break;
254 		case PROP_INT:
255 			rc = libhal_device_set_property_int (hal_ctx, udi, key, int_value, &error);
256 			break;
257 		case PROP_UINT64:
258 			rc = libhal_device_set_property_uint64 (hal_ctx, udi, key, uint64_value, &error);
259 			break;
260 		case PROP_DOUBLE:
261 			rc = libhal_device_set_property_double (hal_ctx, udi, key, double_value, &error);
262 			break;
263 		case PROP_BOOL:
264 			rc = libhal_device_set_property_bool (hal_ctx, udi, key, bool_value, &error);
265 			break;
266 		case PROP_STRLIST_PRE:
267 			rc = libhal_device_property_strlist_prepend (hal_ctx, udi, key, str_value, &error);
268 			break;
269 		case PROP_STRLIST_POST:
270 			rc = libhal_device_property_strlist_append (hal_ctx, udi, key, str_value, &error);
271 			break;
272 		case PROP_STRLIST_REM:
273 			rc = libhal_device_property_strlist_remove (hal_ctx, udi, key, str_value, &error);
274 			break;
275 		}
276 		if (!rc) {
277 			fprintf (stderr, "error: libhal_device_set_property: %s: %s\n", error.name, error.message);
278 			dbus_error_free (&error);
279 			return 1;
280 		}
281 	}
282 
283 	return rc ? 0 : 1;
284 }
285 
286 /**
287  * @}
288  */
289