xref: /titanic_51/usr/src/cmd/hal/tools/hal_set_property.c (revision de7d23d85e06f547e8cd4ed4bce494209d63612a)
118c2aff7Sartem /***************************************************************************
218c2aff7Sartem  * CVSID: $Id$
318c2aff7Sartem  *
418c2aff7Sartem  * hal_set_property.c : Set property for a device
518c2aff7Sartem  *
618c2aff7Sartem  * Copyright (C) 2003 David Zeuthen, <david@fubar.dk>
718c2aff7Sartem  *
818c2aff7Sartem  * Licensed under the Academic Free License version 2.1
918c2aff7Sartem  *
1018c2aff7Sartem  * This program is free software; you can redistribute it and/or modify
1118c2aff7Sartem  * it under the terms of the GNU General Public License as published by
1218c2aff7Sartem  * the Free Software Foundation; either version 2 of the License, or
1318c2aff7Sartem  * (at your option) any later version.
1418c2aff7Sartem  *
1518c2aff7Sartem  * This program is distributed in the hope that it will be useful,
1618c2aff7Sartem  * but WITHOUT ANY WARRANTY; without even the implied warranty of
1718c2aff7Sartem  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1818c2aff7Sartem  * GNU General Public License for more details.
1918c2aff7Sartem  *
2018c2aff7Sartem  * You should have received a copy of the GNU General Public License
2118c2aff7Sartem  * along with this program; if not, write to the Free Software
2218c2aff7Sartem  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
2318c2aff7Sartem  *
2418c2aff7Sartem  **************************************************************************/
2518c2aff7Sartem 
2618c2aff7Sartem 
2718c2aff7Sartem #ifdef HAVE_CONFIG_H
2818c2aff7Sartem #  include <config.h>
2918c2aff7Sartem #endif
3018c2aff7Sartem 
3118c2aff7Sartem #include <stdio.h>
3218c2aff7Sartem #include <stdlib.h>
3318c2aff7Sartem #include <string.h>
3418c2aff7Sartem #include <unistd.h>
3518c2aff7Sartem #include <getopt.h>
3618c2aff7Sartem 
3718c2aff7Sartem #include <libhal.h>
3818c2aff7Sartem 
3918c2aff7Sartem static LibHalContext *hal_ctx;
4018c2aff7Sartem 
4118c2aff7Sartem enum property_op {
4218c2aff7Sartem 	PROP_INT,
4318c2aff7Sartem 	PROP_UINT64,
4418c2aff7Sartem 	PROP_STRING,
4518c2aff7Sartem 	PROP_DOUBLE,
4618c2aff7Sartem 	PROP_BOOL,
4718c2aff7Sartem 	PROP_STRLIST_PRE,
4818c2aff7Sartem 	PROP_STRLIST_POST,
4918c2aff7Sartem 	PROP_STRLIST_REM,
5018c2aff7Sartem 	PROP_INVALID
5118c2aff7Sartem };
5218c2aff7Sartem 
5318c2aff7Sartem /**
5418c2aff7Sartem  * @defgroup HalSetProperty  Set HAL device property
5518c2aff7Sartem  * @ingroup HalMisc
5618c2aff7Sartem  *
5718c2aff7Sartem  * @brief A commandline tool setting a property of a device. Uses libhal
5818c2aff7Sartem  *
5918c2aff7Sartem  * @{
6018c2aff7Sartem  */
6118c2aff7Sartem 
6218c2aff7Sartem /** Print out program usage.
6318c2aff7Sartem  *
6418c2aff7Sartem  *  @param  argc                Number of arguments given to program
6518c2aff7Sartem  *  @param  argv                Arguments given to program
6618c2aff7Sartem  */
6718c2aff7Sartem static void
6818c2aff7Sartem usage (int argc, char *argv[])
6918c2aff7Sartem {
7018c2aff7Sartem 	fprintf (stderr,
7118c2aff7Sartem  "\n"
7218c2aff7Sartem  "usage : hal-set-property --udi <udi> --key <key>\n"
7318c2aff7Sartem  "           (--int <value> | --string <value> | --bool <value> |\n"
7418c2aff7Sartem  "            --strlist-pre <value> | --strlist-post <value> |\n"
7518c2aff7Sartem  "            --strlist-rem <value> | --double <value> | --remove)\n"
7618c2aff7Sartem  "           [--help] [--version]\n");
7718c2aff7Sartem 	fprintf (stderr,
7818c2aff7Sartem  "\n" "        --udi            Unique Device Id\n"
7918c2aff7Sartem  "        --key            Key of the property to set\n"
8018c2aff7Sartem  "        --int            Set value to an integer. Accepts decimal and "
8118c2aff7Sartem  "                         hexadecimal prefixed with 0x or x\n"
8218c2aff7Sartem  "        --uint64         Set value to an integer. Accepts decimal and "
8318c2aff7Sartem  "                         hexadecimal prefixed with 0x or x\n"
8418c2aff7Sartem  "        --string         Set value to a string\n"
8518c2aff7Sartem  "        --double         Set value to a floating point number\n"
8618c2aff7Sartem  "        --bool           Set value to a boolean, ie. true or false\n"
8718c2aff7Sartem  "        --strlist-pre    Prepend a string to a list\n"
8818c2aff7Sartem  "        --strlist-post   Append a string to a list\n"
8918c2aff7Sartem  "        --strlist-rem    Remove a string from a list\n"
9018c2aff7Sartem  "        --remove         Indicates that the property should be removed\n"
9118c2aff7Sartem  "        --version        Show version and exit\n"
9218c2aff7Sartem  "        --help           Show this information and exit\n"
9318c2aff7Sartem  "\n"
9418c2aff7Sartem  "This program attempts to set property for a device. Note that, due to\n"
9518c2aff7Sartem  "security considerations, it may not be possible to set a property; on\n"
9618c2aff7Sartem  "success this program exits with exit code 0. On error, the program exits\n"
9718c2aff7Sartem  "with an exit code different from 0\n" "\n");
9818c2aff7Sartem }
9918c2aff7Sartem 
10018c2aff7Sartem /** Entry point
10118c2aff7Sartem  *
10218c2aff7Sartem  *  @param  argc                Number of arguments given to program
10318c2aff7Sartem  *  @param  argv                Arguments given to program
10418c2aff7Sartem  *  @return                     Return code
10518c2aff7Sartem  */
10618c2aff7Sartem int
10718c2aff7Sartem main (int argc, char *argv[])
10818c2aff7Sartem {
10918c2aff7Sartem 	dbus_bool_t rc = 0;
11018c2aff7Sartem 	char *udi = NULL;
11118c2aff7Sartem 	char *key = NULL;
11218c2aff7Sartem 	char *str_value = NULL;
11318c2aff7Sartem 	dbus_int32_t int_value = 0;
11418c2aff7Sartem 	dbus_uint64_t uint64_value = 0;
11518c2aff7Sartem 	double double_value = 0.0f;
11618c2aff7Sartem 	dbus_bool_t bool_value = TRUE;
11718c2aff7Sartem 	dbus_bool_t remove = FALSE;
11818c2aff7Sartem 	dbus_bool_t is_version = FALSE;
119*de7d23d8SLin Guo - Sun Microsystems 	dbus_bool_t udi_exists = FALSE;
12018c2aff7Sartem 	int type = PROP_INVALID;
12118c2aff7Sartem 	DBusError error;
12218c2aff7Sartem 
12318c2aff7Sartem 	if (argc <= 1) {
12418c2aff7Sartem 		usage (argc, argv);
12518c2aff7Sartem 		return 1;
12618c2aff7Sartem 	}
12718c2aff7Sartem 
12818c2aff7Sartem 	while (1) {
12918c2aff7Sartem 		int c;
13018c2aff7Sartem 		int option_index = 0;
13118c2aff7Sartem 		const char *opt;
13218c2aff7Sartem 		static struct option long_options[] = {
13318c2aff7Sartem 			{"udi", 1, NULL, 0},
13418c2aff7Sartem 			{"key", 1, NULL, 0},
13518c2aff7Sartem 			{"int", 1, NULL, 0},
13618c2aff7Sartem 			{"uint64", 1, NULL, 0},
13718c2aff7Sartem 			{"string", 1, NULL, 0},
13818c2aff7Sartem 			{"double", 1, NULL, 0},
13918c2aff7Sartem 			{"bool", 1, NULL, 0},
14018c2aff7Sartem 			{"strlist-pre", 1, NULL, 0},
14118c2aff7Sartem 			{"strlist-post", 1, NULL, 0},
14218c2aff7Sartem 			{"strlist-rem", 1, NULL, 0},
14318c2aff7Sartem 			{"remove", 0, NULL, 0},
14418c2aff7Sartem 			{"version", 0, NULL, 0},
14518c2aff7Sartem 			{"help", 0, NULL, 0},
14618c2aff7Sartem 			{NULL, 0, NULL, 0}
14718c2aff7Sartem 		};
14818c2aff7Sartem 
14918c2aff7Sartem 		c = getopt_long (argc, argv, "",
15018c2aff7Sartem 				 long_options, &option_index);
15118c2aff7Sartem 		if (c == -1)
15218c2aff7Sartem 			break;
15318c2aff7Sartem 
15418c2aff7Sartem 		switch (c) {
15518c2aff7Sartem 		case 0:
15618c2aff7Sartem 			opt = long_options[option_index].name;
15718c2aff7Sartem 
15818c2aff7Sartem 			if (strcmp (opt, "help") == 0) {
15918c2aff7Sartem 				usage (argc, argv);
16018c2aff7Sartem 				return 0;
16118c2aff7Sartem 			} else if (strcmp (opt, "key") == 0) {
16218c2aff7Sartem 				key = strdup (optarg);
16318c2aff7Sartem 			} else if (strcmp (opt, "string") == 0) {
16418c2aff7Sartem 				str_value = strdup (optarg);
16518c2aff7Sartem 				type = PROP_STRING;
16618c2aff7Sartem 			} else if (strcmp (opt, "int") == 0) {
16718c2aff7Sartem 				int_value = strtol (optarg, NULL, 0);
16818c2aff7Sartem 				type = PROP_INT;
16918c2aff7Sartem 			} else if (strcmp (opt, "uint64") == 0) {
17018c2aff7Sartem 				uint64_value = strtoull (optarg, NULL, 0);
17118c2aff7Sartem 				type = PROP_UINT64;
17218c2aff7Sartem 			} else if (strcmp (opt, "double") == 0) {
17318c2aff7Sartem 				double_value = (double) atof (optarg);
17418c2aff7Sartem 				type = PROP_DOUBLE;
17518c2aff7Sartem 			} else if (strcmp (opt, "bool") == 0) {
17618c2aff7Sartem 				if (strcmp (optarg, "true") == 0)
17718c2aff7Sartem 					bool_value = TRUE;
17818c2aff7Sartem 				else if (strcmp (optarg, "false") == 0)
17918c2aff7Sartem 					bool_value = FALSE;
18018c2aff7Sartem 				else {
18118c2aff7Sartem 					usage (argc, argv);
18218c2aff7Sartem 					return 1;
18318c2aff7Sartem 				}
18418c2aff7Sartem 				type = PROP_BOOL;
18518c2aff7Sartem 			} else if (strcmp (opt, "strlist-pre") == 0) {
18618c2aff7Sartem 				str_value = strdup (optarg);
18718c2aff7Sartem 				type = PROP_STRLIST_PRE;
18818c2aff7Sartem 			} else if (strcmp (opt, "strlist-post") == 0) {
18918c2aff7Sartem 				str_value = strdup (optarg);
19018c2aff7Sartem 				type = PROP_STRLIST_POST;
19118c2aff7Sartem 			} else if (strcmp (opt, "strlist-rem") == 0) {
19218c2aff7Sartem 				str_value = strdup (optarg);
19318c2aff7Sartem 				type = PROP_STRLIST_REM;
19418c2aff7Sartem 			} else if (strcmp (opt, "remove") == 0) {
19518c2aff7Sartem 				remove = TRUE;
19618c2aff7Sartem 			} else if (strcmp (opt, "udi") == 0) {
19718c2aff7Sartem 				udi = strdup (optarg);
19818c2aff7Sartem 			} else if (strcmp (opt, "version") == 0) {
19918c2aff7Sartem 				is_version = TRUE;
20018c2aff7Sartem 			}
20118c2aff7Sartem 			break;
20218c2aff7Sartem 
20318c2aff7Sartem 		default:
20418c2aff7Sartem 			usage (argc, argv);
20518c2aff7Sartem 			return 1;
20618c2aff7Sartem 			break;
20718c2aff7Sartem 		}
20818c2aff7Sartem 	}
20918c2aff7Sartem 
21018c2aff7Sartem 	if (is_version) {
21118c2aff7Sartem 		printf ("hal-set-property " PACKAGE_VERSION "\n");
21218c2aff7Sartem 		return 0;
21318c2aff7Sartem 	}
21418c2aff7Sartem 
21518c2aff7Sartem 	/* must have at least one, but not neither or both */
21618c2aff7Sartem 	if ((remove && type != PROP_INVALID) || ((!remove) && type == PROP_INVALID)) {
21718c2aff7Sartem 		usage (argc, argv);
21818c2aff7Sartem 		return 1;
21918c2aff7Sartem 	}
22018c2aff7Sartem 
22118c2aff7Sartem 	fprintf (stderr, "\n");
22218c2aff7Sartem 
22318c2aff7Sartem 	dbus_error_init (&error);
22418c2aff7Sartem 	if ((hal_ctx = libhal_ctx_new ()) == NULL) {
22518c2aff7Sartem 		fprintf (stderr, "error: libhal_ctx_new\n");
22618c2aff7Sartem 		return 1;
22718c2aff7Sartem 	}
22818c2aff7Sartem 	if (!libhal_ctx_set_dbus_connection (hal_ctx, dbus_bus_get (DBUS_BUS_SYSTEM, &error))) {
22918c2aff7Sartem 		fprintf (stderr, "error: libhal_ctx_set_dbus_connection: %s: %s\n", error.name, error.message);
23018c2aff7Sartem 		LIBHAL_FREE_DBUS_ERROR (&error);
23118c2aff7Sartem 		return 1;
23218c2aff7Sartem 	}
23318c2aff7Sartem 	if (!libhal_ctx_init (hal_ctx, &error)) {
23418c2aff7Sartem 		if (dbus_error_is_set(&error)) {
23518c2aff7Sartem 			fprintf (stderr, "error: libhal_ctx_init: %s: %s\n", error.name, error.message);
23618c2aff7Sartem 			LIBHAL_FREE_DBUS_ERROR (&error);
23718c2aff7Sartem 		}
23818c2aff7Sartem 		fprintf (stderr, "Could not initialise connection to hald.\n"
23918c2aff7Sartem 				 "Normally this means the HAL daemon (hald) is not running or not ready.\n");
24018c2aff7Sartem 		return 1;
24118c2aff7Sartem 	}
24218c2aff7Sartem 
243*de7d23d8SLin Guo - Sun Microsystems 	 /* check UDI exists */
244*de7d23d8SLin Guo - Sun Microsystems 	udi_exists = libhal_device_exists (hal_ctx, udi, &error);
245*de7d23d8SLin Guo - Sun Microsystems 	if (!udi_exists) {
246*de7d23d8SLin Guo - Sun Microsystems 		fprintf (stderr, "error: UDI %s does not exist\n", udi);
247*de7d23d8SLin Guo - Sun Microsystems 		return 1;
248*de7d23d8SLin Guo - Sun Microsystems 	}
249*de7d23d8SLin Guo - Sun Microsystems 	if (dbus_error_is_set(&error)) {
250*de7d23d8SLin Guo - Sun Microsystems 		fprintf (stderr, "error: libhal_device_exists: %s: %s\n", error.name, error.message);
251*de7d23d8SLin Guo - Sun Microsystems 		dbus_error_free (&error);
252*de7d23d8SLin Guo - Sun Microsystems 		return 1;
253*de7d23d8SLin Guo - Sun Microsystems 	}
254*de7d23d8SLin Guo - Sun Microsystems 
25518c2aff7Sartem 	if (remove) {
25618c2aff7Sartem 		rc = libhal_device_remove_property (hal_ctx, udi, key, &error);
25718c2aff7Sartem 		if (!rc) {
258*de7d23d8SLin Guo - Sun Microsystems 			if (dbus_error_is_set(&error)) {
25918c2aff7Sartem 			        fprintf (stderr, "error: libhal_device_remove_property: %s: %s\n", error.name, error.message);
260*de7d23d8SLin Guo - Sun Microsystems 				dbus_error_free (&error);
261*de7d23d8SLin Guo - Sun Microsystems 			} else {
262*de7d23d8SLin Guo - Sun Microsystems 				fprintf (stderr, "error: libhal_device_remove_property: invalid params.\n");
263*de7d23d8SLin Guo - Sun Microsystems 			}
26418c2aff7Sartem 			return 1;
26518c2aff7Sartem 		}
26618c2aff7Sartem 	} else {
26718c2aff7Sartem 		switch (type) {
26818c2aff7Sartem 		case PROP_STRING:
26918c2aff7Sartem 			rc = libhal_device_set_property_string (hal_ctx, udi, key, str_value, &error);
27018c2aff7Sartem 			break;
27118c2aff7Sartem 		case PROP_INT:
27218c2aff7Sartem 			rc = libhal_device_set_property_int (hal_ctx, udi, key, int_value, &error);
27318c2aff7Sartem 			break;
27418c2aff7Sartem 		case PROP_UINT64:
27518c2aff7Sartem 			rc = libhal_device_set_property_uint64 (hal_ctx, udi, key, uint64_value, &error);
27618c2aff7Sartem 			break;
27718c2aff7Sartem 		case PROP_DOUBLE:
27818c2aff7Sartem 			rc = libhal_device_set_property_double (hal_ctx, udi, key, double_value, &error);
27918c2aff7Sartem 			break;
28018c2aff7Sartem 		case PROP_BOOL:
28118c2aff7Sartem 			rc = libhal_device_set_property_bool (hal_ctx, udi, key, bool_value, &error);
28218c2aff7Sartem 			break;
28318c2aff7Sartem 		case PROP_STRLIST_PRE:
28418c2aff7Sartem 			rc = libhal_device_property_strlist_prepend (hal_ctx, udi, key, str_value, &error);
28518c2aff7Sartem 			break;
28618c2aff7Sartem 		case PROP_STRLIST_POST:
28718c2aff7Sartem 			rc = libhal_device_property_strlist_append (hal_ctx, udi, key, str_value, &error);
28818c2aff7Sartem 			break;
28918c2aff7Sartem 		case PROP_STRLIST_REM:
29018c2aff7Sartem 			rc = libhal_device_property_strlist_remove (hal_ctx, udi, key, str_value, &error);
29118c2aff7Sartem 			break;
29218c2aff7Sartem 		}
29318c2aff7Sartem 		if (!rc) {
294*de7d23d8SLin Guo - Sun Microsystems 			if (dbus_error_is_set(&error)) {
29518c2aff7Sartem 			        fprintf (stderr, "error: libhal_device_set_property: %s: %s\n", error.name, error.message);
29618c2aff7Sartem 				dbus_error_free (&error);
297*de7d23d8SLin Guo - Sun Microsystems 			} else {
298*de7d23d8SLin Guo - Sun Microsystems 				fprintf (stderr, "error: libhal_device_set_property: invalid params.\n");
299*de7d23d8SLin Guo - Sun Microsystems 			}
30018c2aff7Sartem 			return 1;
30118c2aff7Sartem 		}
30218c2aff7Sartem 	}
30318c2aff7Sartem 
30418c2aff7Sartem 	return rc ? 0 : 1;
30518c2aff7Sartem }
30618c2aff7Sartem 
30718c2aff7Sartem /**
30818c2aff7Sartem  * @}
30918c2aff7Sartem  */
310