xref: /illumos-gate/usr/src/cmd/hal/tools/hal_find_by_property.c (revision 55fea89dcaa64928bed4327112404dcb3e07b79f)
1*18c2aff7Sartem /***************************************************************************
2*18c2aff7Sartem  * CVSID: $Id$
3*18c2aff7Sartem  *
4*18c2aff7Sartem  * hal_find_by_property.c : Find hal devices
5*18c2aff7Sartem  *
6*18c2aff7Sartem  * Copyright (C) 2005 David Zeuthen, <david@fubar.dk>
7*18c2aff7Sartem  *
8*18c2aff7Sartem  * Licensed under the Academic Free License version 2.1
9*18c2aff7Sartem  *
10*18c2aff7Sartem  * This program is free software; you can redistribute it and/or modify
11*18c2aff7Sartem  * it under the terms of the GNU General Public License as published by
12*18c2aff7Sartem  * the Free Software Foundation; either version 2 of the License, or
13*18c2aff7Sartem  * (at your option) any later version.
14*18c2aff7Sartem  *
15*18c2aff7Sartem  * This program is distributed in the hope that it will be useful,
16*18c2aff7Sartem  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*18c2aff7Sartem  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*18c2aff7Sartem  * GNU General Public License for more details.
19*18c2aff7Sartem  *
20*18c2aff7Sartem  * You should have received a copy of the GNU General Public License
21*18c2aff7Sartem  * along with this program; if not, write to the Free Software
22*18c2aff7Sartem  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
23*18c2aff7Sartem  *
24*18c2aff7Sartem  **************************************************************************/
25*18c2aff7Sartem 
26*18c2aff7Sartem 
27*18c2aff7Sartem #ifdef HAVE_CONFIG_H
28*18c2aff7Sartem #  include <config.h>
29*18c2aff7Sartem #endif
30*18c2aff7Sartem 
31*18c2aff7Sartem #include <stdio.h>
32*18c2aff7Sartem #include <string.h>
33*18c2aff7Sartem #include <unistd.h>
34*18c2aff7Sartem #include <getopt.h>
35*18c2aff7Sartem 
36*18c2aff7Sartem #include <libhal.h>
37*18c2aff7Sartem 
38*18c2aff7Sartem 
39*18c2aff7Sartem /** Print out program usage.
40*18c2aff7Sartem  *
41*18c2aff7Sartem  *  @param  argc                Number of arguments given to program
42*18c2aff7Sartem  *  @param  argv                Arguments given to program
43*18c2aff7Sartem  */
44*18c2aff7Sartem static void
usage(int argc,char * argv[])45*18c2aff7Sartem usage (int argc, char *argv[])
46*18c2aff7Sartem {
47*18c2aff7Sartem 	fprintf (stderr,
48*18c2aff7Sartem  "\n"
49*18c2aff7Sartem  "usage : hal-find-by-property --key <key> --string <value>\n"
50*18c2aff7Sartem  "                              [--help] [--verbose] [--version]\n");
51*18c2aff7Sartem 
52*18c2aff7Sartem /** @todo support other property types a'la hal-[get|set]-property */
53*18c2aff7Sartem 
54*18c2aff7Sartem 	fprintf (stderr,
55*18c2aff7Sartem  "\n"
56*18c2aff7Sartem  "        --key            Key of the property to check\n"
57*18c2aff7Sartem  "        --string         String value of property\n"
58*18c2aff7Sartem  "        --verbose        Be verbose\n"
59*18c2aff7Sartem  "        --version        Show version and exit\n"
60*18c2aff7Sartem  "        --help           Show this information and exit\n"
61*18c2aff7Sartem  "\n"
62*18c2aff7Sartem  "This program prints the Unique Device Identifiers for HAL device\n"
63*18c2aff7Sartem  "objects where a given property assumes a given value. On success\n"
64*18c2aff7Sartem  "the program exists with exit code 0. If there is an error, the\n"
65*18c2aff7Sartem  "program exits with an exit code different from 0.\n"
66*18c2aff7Sartem  "\n");
67*18c2aff7Sartem }
68*18c2aff7Sartem 
69*18c2aff7Sartem /** Entry point
70*18c2aff7Sartem  *
71*18c2aff7Sartem  *  @param  argc                Number of arguments given to program
72*18c2aff7Sartem  *  @param  argv                Arguments given to program
73*18c2aff7Sartem  *  @return                     Return code
74*18c2aff7Sartem  */
75*18c2aff7Sartem int
main(int argc,char * argv[])76*18c2aff7Sartem main (int argc, char *argv[])
77*18c2aff7Sartem {
78*18c2aff7Sartem 	int i;
79*18c2aff7Sartem 	int num_udis;
80*18c2aff7Sartem 	char **udis;
81*18c2aff7Sartem 	char *key = NULL;
82*18c2aff7Sartem 	char *value = NULL;
83*18c2aff7Sartem 	dbus_bool_t is_verbose = FALSE;
84*18c2aff7Sartem 	dbus_bool_t is_version = FALSE;
85*18c2aff7Sartem 	DBusError error;
86*18c2aff7Sartem 	LibHalContext *hal_ctx;
87*18c2aff7Sartem 
88*18c2aff7Sartem 	if (argc <= 1) {
89*18c2aff7Sartem 		usage (argc, argv);
90*18c2aff7Sartem 		return 1;
91*18c2aff7Sartem 	}
92*18c2aff7Sartem 
93*18c2aff7Sartem 	while (1) {
94*18c2aff7Sartem 		int c;
95*18c2aff7Sartem 		int option_index = 0;
96*18c2aff7Sartem 		const char *opt;
97*18c2aff7Sartem 		static struct option long_options[] = {
98*18c2aff7Sartem 			{"key", 1, NULL, 0},
99*18c2aff7Sartem 			{"string", 1, NULL, 0},
100*18c2aff7Sartem 			{"verbose", 0, NULL, 0},
101*18c2aff7Sartem 			{"version", 0, NULL, 0},
102*18c2aff7Sartem 			{"help", 0, NULL, 0},
103*18c2aff7Sartem 			{NULL, 0, NULL, 0}
104*18c2aff7Sartem 		};
105*18c2aff7Sartem 
106*18c2aff7Sartem 		c = getopt_long (argc, argv, "",
107*18c2aff7Sartem 				 long_options, &option_index);
108*18c2aff7Sartem 		if (c == -1)
109*18c2aff7Sartem 			break;
110*18c2aff7Sartem 
111*18c2aff7Sartem 		switch (c) {
112*18c2aff7Sartem 		case 0:
113*18c2aff7Sartem 			opt = long_options[option_index].name;
114*18c2aff7Sartem 
115*18c2aff7Sartem 			if (strcmp (opt, "help") == 0) {
116*18c2aff7Sartem 				usage (argc, argv);
117*18c2aff7Sartem 				return 0;
118*18c2aff7Sartem 			} else if (strcmp (opt, "verbose") == 0) {
119*18c2aff7Sartem 				is_verbose = TRUE;
120*18c2aff7Sartem 			} else if (strcmp (opt, "version") == 0) {
121*18c2aff7Sartem 				is_version = TRUE;
122*18c2aff7Sartem 			} else if (strcmp (opt, "key") == 0) {
123*18c2aff7Sartem 				key = strdup (optarg);
124*18c2aff7Sartem 			} else if (strcmp (opt, "string") == 0) {
125*18c2aff7Sartem 				value = strdup (optarg);
126*18c2aff7Sartem 			}
127*18c2aff7Sartem 			break;
128*18c2aff7Sartem 
129*18c2aff7Sartem 		default:
130*18c2aff7Sartem 			usage (argc, argv);
131*18c2aff7Sartem 			return 1;
132*18c2aff7Sartem 			break;
133*18c2aff7Sartem 		}
134*18c2aff7Sartem 	}
135*18c2aff7Sartem 
136*18c2aff7Sartem 	if (is_version) {
137*18c2aff7Sartem 		printf ("hal-find-by-property " PACKAGE_VERSION "\n");
138*18c2aff7Sartem 		return 0;
139*18c2aff7Sartem 	}
140*18c2aff7Sartem 
141*18c2aff7Sartem 	if (key == NULL || value == NULL) {
142*18c2aff7Sartem 		usage (argc, argv);
143*18c2aff7Sartem 		return 1;
144*18c2aff7Sartem 	}
145*18c2aff7Sartem 
146*18c2aff7Sartem 	dbus_error_init (&error);
147*18c2aff7Sartem 	if ((hal_ctx = libhal_ctx_new ()) == NULL) {
148*18c2aff7Sartem 		fprintf (stderr, "error: libhal_ctx_new\n");
149*18c2aff7Sartem 		return 1;
150*18c2aff7Sartem 	}
151*18c2aff7Sartem 	if (!libhal_ctx_set_dbus_connection (hal_ctx, dbus_bus_get (DBUS_BUS_SYSTEM, &error))) {
152*18c2aff7Sartem 		fprintf (stderr, "error: libhal_ctx_set_dbus_connection: %s: %s\n", error.name, error.message);
153*18c2aff7Sartem 		LIBHAL_FREE_DBUS_ERROR (&error);
154*18c2aff7Sartem 		return 1;
155*18c2aff7Sartem 	}
156*18c2aff7Sartem 	if (!libhal_ctx_init (hal_ctx, &error)) {
157*18c2aff7Sartem 		if (dbus_error_is_set(&error)) {
158*18c2aff7Sartem 			fprintf (stderr, "error: libhal_ctx_init: %s: %s\n", error.name, error.message);
159*18c2aff7Sartem 			LIBHAL_FREE_DBUS_ERROR (&error);
160*18c2aff7Sartem 		}
161*18c2aff7Sartem 		fprintf (stderr, "Could not initialise connection to hald.\n"
162*18c2aff7Sartem 				 "Normally this means the HAL daemon (hald) is not running or not ready.\n");
163*18c2aff7Sartem 		return 1;
164*18c2aff7Sartem 	}
165*18c2aff7Sartem 
166*18c2aff7Sartem 
167*18c2aff7Sartem 	udis = libhal_manager_find_device_string_match (hal_ctx, key, value, &num_udis, &error);
168*18c2aff7Sartem 
169*18c2aff7Sartem 	if (dbus_error_is_set (&error)) {
170*18c2aff7Sartem 		fprintf (stderr, "error: %s: %s\n", error.name, error.message);
171*18c2aff7Sartem 		LIBHAL_FREE_DBUS_ERROR (&error);
172*18c2aff7Sartem 		return 1;
173*18c2aff7Sartem 	}
174*18c2aff7Sartem 
175*18c2aff7Sartem 	if (is_verbose)
176*18c2aff7Sartem 		printf ("Found %d device objects with string property %s = '%s'\n", num_udis, key, value);
177*18c2aff7Sartem 
178*18c2aff7Sartem 	if (num_udis == 0) {
179*18c2aff7Sartem 		return 1;
180*18c2aff7Sartem 	}
181*18c2aff7Sartem 
182*18c2aff7Sartem 	for (i = 0; i < num_udis; i++) {
183*18c2aff7Sartem 		printf ("%s\n", udis[i]);
184*18c2aff7Sartem 	}
185*18c2aff7Sartem 
186*18c2aff7Sartem 	libhal_free_string_array (udis);
187*18c2aff7Sartem 
188*18c2aff7Sartem 	return 0;
189*18c2aff7Sartem }
190*18c2aff7Sartem 
191*18c2aff7Sartem /**
192*18c2aff7Sartem  * @}
193*18c2aff7Sartem  */
194