summaryrefslogtreecommitdiff
path: root/cmd/lsbus.c
blob: bd4676ffdbb3d42c3d1be46e3376aa822de14937 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
/*
 * lsbus.c
 *
 * Utility to list bus devices
 *
 * Copyright (C) 2003 International Business Machines, Inc.
 *
 *	This program is free software; you can redistribute it and/or modify it
 *	under the terms of the GNU General Public License as published by the
 *	Free Software Foundation version 2 of the License.
 * 
 *	This program is distributed in the hope that it will be useful, but
 *	WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *	General Public License for more details.
 * 
 *	You should have received a copy of the GNU General Public License along
 *	with this program; if not, write to the Free Software Foundation, Inc.,
 *	675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <mntent.h>
#include <dirent.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <ctype.h>

#include "libsysfs.h"

/* Command Options */
static int show_options = 0;		/* bitmask of show options */
static char *attr_to_show = NULL;	/* print value for this attribute */
static char *bus_device = NULL;		/* print only this bus device */

#define SHOW_ATTRIBUTES		0x01	/* show attributes command option */
#define SHOW_ATTRIBUTE_VALUE	0x02	/* show an attribute value option */
#define SHOW_DEVICES		0x04	/* show only devices option */
#define SHOW_DRIVERS		0x08	/* show only drivers option */
#define SHOW_ALL_ATTRIB_VALUES	0x10	/* show all attributes with values */
                                                                                
#define SHOW_ALL		0xff

static char cmd_options[] = "aA:dDhv";

/*
 * binary_files - defines existing sysfs binary files that should be printed
 * in hex.
 */
static char *binary_files[] = {
	"config",
	"data"
};

static int binfiles = 2;

/**
 * usage: prints utility usage.
 */
void usage(void)
{
	fprintf(stdout, "Usage: lsbus [<options> bus [device]]\n");
	fprintf(stdout, "\t-a\t\t\tShow attributes\n");
	fprintf(stdout, "\t-d\t\t\tShow only devices\n");
	fprintf(stdout, "\t-h\t\t\tShow usage\n");
	fprintf(stdout, "\t-v\t\t\tShow all attributes with values\n");
	fprintf(stdout, "\t-A <attribute_name>\tShow attribute value\n");
	fprintf(stdout, "\t-D\t\t\tShow only drivers\n");
}

/**
 * remove_end_newline: removes newline on the end of an attribute value
 * @value: string to remove newline from
 */
void remove_end_newline(char *value)
{
	char *p = value + (strlen(value) - 1);

	if (p != NULL && *p == '\n')
		*p = '\0';
}

/**
 * print_device_children: prints out device subdirs.
 * @sdir: print this directory's subdirectories/children.
 */
void print_device_children(struct sysfs_directory *sdir)
{
	if (sdir != NULL) {
		fprintf(stdout, "\tChildren:\n");
		if (sdir->subdirs != NULL) {
			char dirname[SYSFS_NAME_LEN];
			struct sysfs_directory *cur = sdir->subdirs;

			while (cur != NULL) {
				if ((sysfs_get_name_from_path(cur->path, 
				     dirname, SYSFS_NAME_LEN)) == 0)
					fprintf(stdout, "\t\t%s\n", dirname);
				else
					fprintf(stdout, "\t\t%s\n", cur->path);
				cur = cur->next;
			}
		}	
		if (sdir->links != NULL) {
			struct sysfs_link *curl = sdir->links;
			
			while(curl != NULL) {
				fprintf(stdout, "\t\t%s\n", curl->name);
				curl = curl->next;
			}
		}
	}
}

/**
 * isbinaryvalue: checks to see if attribute is binary or not.
 * @attr: attribute to check.
 * returns 1 if binary or 0 if not.
 */
int isbinaryvalue(struct sysfs_attribute *attr)
{
	char attrname[SYSFS_NAME_LEN];
	int i;

	if (attr == NULL || attr->value == NULL)
		return 0;
	if ((sysfs_get_name_from_path(attr->path, attrname, SYSFS_NAME_LEN))
	    != 0)
		return 0;

	for (i = 0; i < binfiles; i++) 
		if ((strcmp(attrname, binary_files[i])) == 0)
			return 1;

	return 0;
}

/**
 * print_attribute_value: prints out single attribute value.
 * @attr: attricute to print.
 */
void print_attribute_value(struct sysfs_attribute *attr)
{
	if (attr == NULL)
		return;
                                                                                
	if (attr->method & SYSFS_METHOD_SHOW) {
		if (isbinaryvalue(attr) != 0) {
			int i;
			fprintf(stdout, "\t: ");
			for (i = 0; i < attr->len; i++) {
				if (!(i %16)) {
					fprintf(stdout, "\n\t\t\t");
				} else if (!(i %8))
					fprintf(stdout, " ");
				fprintf(stdout, "%02x ",
					(unsigned char)attr->value[i]);
			}
			fprintf(stdout, "\n");
                                                                                
		} else if (attr->value != NULL && strlen(attr->value) > 0) {
			remove_end_newline(attr->value);
			fprintf(stdout, "\t: %s\n", attr->value);
		} else
			fprintf(stdout, "\n");
	} else {
		fprintf(stdout, "\t: store method only\n");
	}
}

/**
 * print_attribute: prints out a single attribute
 * @attr: attribute to print.
 */
void print_attribute(struct sysfs_attribute *attr)
{
	char attrname[SYSFS_NAME_LEN];

	if (attr == NULL) 
		return;

	if ((sysfs_get_name_from_path(attr->path, attrname, SYSFS_NAME_LEN)) 
	    != 0)
		return; 

	if (show_options & SHOW_ALL_ATTRIB_VALUES) {
		fprintf(stdout, "\t\t%s", attrname);
		print_attribute_value(attr);

	} else if ((show_options & SHOW_ATTRIBUTES) || ((show_options 
	    & SHOW_ATTRIBUTE_VALUE) && (strcmp(attrname, attr_to_show) 
	    == 0))) {
		fprintf (stdout, "\t\t%s", attrname);
		if (show_options & SHOW_ATTRIBUTE_VALUE && attr->value != NULL
		    && (strcmp(attrname, attr_to_show)) == 0) {
			print_attribute_value(attr);
		} else {
			fprintf(stdout, "\n");
		}
	}
}

/**
 * print_device_attributes: prints out device attributes.
 * @sdir: print this directory's attributes/files.
 */
void print_device_attributes(struct sysfs_directory *sdir)
{
	if (sdir != NULL && sdir->attributes != NULL) {
		struct sysfs_attribute *cur = sdir->attributes;

		fprintf (stdout, "\tAttributes:\n");
		while (cur != NULL) {
			print_attribute(cur);
			cur = cur->next;
		}
	}
}

/**
 * print_device: prints out device information.
 * @device: device to print.
 */
void print_device(struct sysfs_device *device)
{
	if (device != NULL && device->directory != NULL) {
		fprintf (stdout, "  %s %s\n", device->bus_id, device->name);
		if (device->directory->subdirs != NULL
		    || device->directory->links != NULL)
			print_device_children(device->directory);
		if (device->directory->attributes != NULL && (show_options 
		    & (SHOW_ATTRIBUTES | SHOW_ATTRIBUTE_VALUE
		    | SHOW_ALL_ATTRIB_VALUES))) 
			print_device_attributes(device->directory);
		if (device->driver != NULL) 
			fprintf (stdout, "\tDriver: %s\n", 
						device->driver->name);
	}
}

/**
 * print_driver_attributes: prints out driver attributes .
 * @driver: print this driver's attributes.
 */
void print_driver_attributes(struct sysfs_driver *driver)
{
	if (driver != NULL && driver->directory != NULL) {
		struct sysfs_directory *sdir = driver->directory;

		if (sdir->attributes != NULL) {
			struct sysfs_attribute *cur = sdir->attributes;

			fprintf (stdout, "\t%s Attributes:\n", driver->name);
			while (cur != NULL) {
				print_attribute(cur);
				cur = cur->next;
			}
		}
	}
}

/**
 * print_driver_devices: prints out devices under driver.
 * @driver: print devices bound to this driver.
 */
void print_driver_devices(struct sysfs_driver *driver)
{
	if (driver != NULL && driver->directory != NULL) {
		struct sysfs_directory *sdir = driver->directory;

		if (sdir->links != NULL) {
			struct sysfs_link *cur = sdir->links;

			fprintf (stdout, "\tDevices:\n");
			while (cur != NULL) {
				fprintf(stdout, "\t\t%s\n", cur->name);
				cur = cur->next;
			}
		}
	}
}

/**
 * print_driver: prints out driver information.
 * @driver: driver to print.
 */
void print_driver(struct sysfs_driver *driver)
{
	struct sysfs_directory *sdir = NULL;

	if (driver != NULL) {
		fprintf (stdout, "  %s\n", driver->name);
		sdir = driver->directory;
		if (sdir != NULL) {
			if (sdir->links != NULL)
				print_driver_devices(driver);
			if(sdir->attributes != NULL && (show_options
			    & (SHOW_ATTRIBUTES | SHOW_ATTRIBUTE_VALUE
			    | SHOW_ALL_ATTRIB_VALUES)))
				print_driver_attributes(driver);
		}
	}
}

/**
 * print_default_bus: prints out everything on a bus.
 * @busname: bus to print.
 * returns 0 with success or 1 with error.
 */
int print_sysfs_bus(char *busname)
{
	struct sysfs_bus *bus = NULL;
	struct sysfs_device *curdev = NULL;
	struct sysfs_driver *curdrv = NULL;

	if (busname == NULL) {
		errno = EINVAL;
		return 1;
	}
	bus = sysfs_open_bus(busname);
	if (bus == NULL) {
		fprintf(stderr, "Error opening bus %s\n", busname);
		return 1;
	}

	fprintf(stdout, "Bus: %s\n", busname);
	if (bus->devices != NULL && (show_options & SHOW_DEVICES)) {
		curdev = bus->devices;
		if (bus_device == NULL)
			fprintf(stdout, "Devices:\n");
		while (curdev != NULL) {
			if (bus_device == NULL || (strcmp(bus_device,
			    curdev->bus_id) == 0))
				print_device(curdev);
			curdev = curdev->next;
		}
	}
	if (bus->drivers != NULL && (show_options & SHOW_DRIVERS)) {
		curdrv = bus->drivers;
		fprintf(stdout, "Drivers:\n");
		while (curdrv != NULL) {
			print_driver(curdrv);
			curdrv = curdrv->next;
		}
	}
	sysfs_close_bus(bus);
	return 0;
}

/**
 * print_sysfs_buses: prints out supported buses.
 * @buses: list of supported system buses.
 * returns 0 with success or 1 with error.
 */
int print_sysfs_buses(void)
{
	struct sysfs_directory *busdir = NULL, *current = NULL;
	char busname[SYSFS_NAME_LEN];
	char buspath[SYSFS_PATH_MAX];
	int ret = 0;

	/* get sysfs mount point */
	if ((sysfs_get_mnt_path(buspath, SYSFS_PATH_MAX) != 0)) {
		perror("sysfs_get_mnt_path");
		fprintf(stderr, "Error getting sysfs mount path - exiting\n");
		exit(1);
	}
	strcat(buspath, SYSFS_BUS_DIR);
	busdir = sysfs_open_directory(buspath);
	if (busdir == NULL) {
		fprintf(stderr, "Error opening sysfs bus directory at %s\n",
			buspath);
		return 1;
	}
	if ((sysfs_read_directory(busdir)) != 0) {
		fprintf(stderr, "Error reading sysfs bus directory at %s\n",
			buspath);
		sysfs_close_directory(busdir);
		return 1;
	}

	fprintf(stdout, "Supported sysfs buses:\n");
	current = busdir->subdirs;
	while (current != NULL) {
		if ((sysfs_get_name_from_path(current->path, busname,
		    SYSFS_NAME_LEN)) != 0) {
			sysfs_close_directory(busdir);
			return 1;
		}
		fprintf(stdout, "\t%s\n", busname);
		current = current->next;
	}
	sysfs_close_directory(busdir);
	return ret;
}

/* MAIN */
int main(int argc, char *argv[])
{
	char *bus_to_print = NULL;
	int retval = 0;
	int opt;
	extern int optind;
	extern char *optarg;

	while((opt = getopt(argc, argv, cmd_options)) != EOF) {
		switch(opt) {
		case 'a':
			show_options |= SHOW_ATTRIBUTES;
			break;
		case 'A':
			if ((strlen(optarg) + 1) > SYSFS_NAME_LEN) {
				fprintf(stderr, 
					"Attribute name %s is too long\n",
					optarg);
				exit(1);
			}
			attr_to_show = optarg;
			show_options |= SHOW_ATTRIBUTE_VALUE;
			break;	
		case 'd':
			show_options |= SHOW_DEVICES;
			break;
		case 'D':
			show_options |= SHOW_DRIVERS;
			break;
		case 'h':
			usage();
			exit(0);
			break;
		case 'v':
			show_options |= SHOW_ALL_ATTRIB_VALUES;
			break;
		default:
			usage();
			exit(1);
		}
	}
	argc -= optind;
	argv += optind;

	switch(argc) {
	case 0:
		break;
	case 1:
		/* get bus to view */
		if ((strlen(*argv)+1) < SYSFS_NAME_LEN) 
			bus_to_print = *argv;
		else
			fprintf(stderr, 
				"Invalid argument: busname too long\n");
		break;
	case 2:
		/* get bus and device to view */
		if ((strlen(argv[0])) < SYSFS_NAME_LEN && (strlen(argv[1]))
	 	    < SYSFS_NAME_LEN) {
			bus_to_print = argv[0];
			bus_device = argv[1];
			show_options |= SHOW_DEVICES;
		} else {
			fprintf(stderr, 
				"Invalid argument: bus or device name too long\n");
			exit(1);
		}
		break;
	default:
		usage();
		exit(1);
	}

	if (bus_to_print == NULL && (show_options & (SHOW_ATTRIBUTES 
	    | SHOW_ATTRIBUTE_VALUE | SHOW_DEVICES | SHOW_DRIVERS
	    | SHOW_ALL_ATTRIB_VALUES))) {
		fprintf(stderr, "Please specify a bus\n");
		usage();
		exit(1);
	}
	/* default is to print devices */
	if (!(show_options & (SHOW_DEVICES | SHOW_DRIVERS)))
		show_options |= SHOW_DEVICES;

	if (bus_to_print != NULL)  
		retval = print_sysfs_bus(bus_to_print);
	else
		retval = print_sysfs_buses();

	exit(retval);
}