summaryrefslogtreecommitdiffstats
path: root/kcontrol/info/info_fbsd.cpp
blob: 3f67b4b7a512bef236c64cfda3ebd7e5aea097dc (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
/*
 * info_fbsd.cpp is part of the KDE program kcminfo.  This displays
 * various information about the system (hopefully a FreeBSD system)
 * it's running on.
 *
 * All of the devinfo bits were blatantly stolen from the devinfo utility
 * provided with FreeBSD 5.0 (and later).  No gross hacks were harmed 
 * during the creation of info_fbsd.cpp.  Thanks Mike.
 */

#define INFO_CPU_AVAILABLE
#define INFO_IRQ_AVAILABLE
#define INFO_DMA_AVAILABLE
#define INFO_PCI_AVAILABLE
#define INFO_IOPORTS_AVAILABLE
#define INFO_SOUND_AVAILABLE
#define INFO_DEVICES_AVAILABLE
#define INFO_SCSI_AVAILABLE
#define INFO_PARTITIONS_AVAILABLE
#define INFO_XSERVER_AVAILABLE


/*
 * all following functions should return TRUE, when the Information
 * was filled into the lBox-Widget. Returning FALSE indicates that
 * information was not available.
 */

#ifdef HAVE_CONFIG_H
	#include <config.h>
#endif

#include <sys/types.h>
#include <sys/sysctl.h>

//#if __FreeBSD_version >= 500042
//	#define we should have devinfo.h
//#else
//	#define we probably don't have devinfo.h
//#endif

#ifdef HAVE_DEVINFO_H
	extern "C" {
		#include <devinfo.h>
	}
#endif

#include <errno.h>
#include <fstab.h>
#include <string.h>

#include <tqdict.h>
#include <tqfile.h>
#include <tqptrlist.h>
#include <tqstring.h>
#include <tqtextstream.h>

class Device {
public:
	Device (TQString n=TQString::null, TQString d=TQString::null)
		{name=n; description=d;}
	TQString name, description;
};

void ProcessChildren(TQString name);
TQString GetController(const TQString &line);
Device *GetDevice(const TQString &line);

#ifdef HAVE_DEVINFO_H
extern "C" {
	int print_irq(struct devinfo_rman *rman, void *arg);
	int print_dma(struct devinfo_rman *rman, void *arg);
	int print_ioports(struct devinfo_rman *rman, void *arg);
	int print_resource(struct devinfo_res *res, void *arg);
}
#endif

bool GetInfo_CPU (TQListView *lBox)
{
	// Modified 13 July 2000 for SMP by Brad Hughes - bhughes@trolltech.com

	int ncpu;
	size_t len;

	len = sizeof(ncpu);
	sysctlbyname("hw.ncpu", &ncpu, &len, NULL, 0);

	TQString cpustring;
	for (int i = ncpu; i > 0; i--) {
		/* Stuff for sysctl */
		char *buf;
		int i_buf;

		// get the processor model
		sysctlbyname("hw.model", NULL, &len, NULL, 0);
		buf = new char[len];
		sysctlbyname("hw.model", buf, &len, NULL, 0);

		// get the TSC speed if we can
		len = sizeof(i_buf);
		if (sysctlbyname("machdep.tsc_freq", &i_buf, &len, NULL, 0) != -1) {
			cpustring = i18n("CPU %1: %2, %3 MHz").arg(i).arg(buf).arg(i_buf/1000000);
		} else {
			cpustring = i18n("CPU %1: %2, unknown speed").arg(i).arg(buf);
		}

		/* Put everything in the listbox */
		new TQListViewItem(lBox, cpustring);

		/* Clean up after ourselves, this time I mean it ;-) */
		delete buf;
	}

	return true;
}

bool GetInfo_IRQ (TQListView *lbox)
{
#ifdef HAVE_DEVINFO_H
	/* systat lists the interrupts assigned to devices as well as how many were
	   generated.  Parsing its output however is about as fun as a sandpaper
	   enema.  The best idea would probably be to rip out the guts of systat.
	   Too bad it's not very well commented */
	/* Oh neat, current now has a neat little utility called devinfo */
	if (devinfo_init())
		return false;
	devinfo_foreach_rman(print_irq, lbox);
	return true;
#else
	return false;
#endif
}

bool GetInfo_DMA (TQListView *lbox)
{
#ifdef HAVE_DEVINFO_H
	/* Oh neat, current now has a neat little utility called devinfo */
	if (devinfo_init())
		return false;
	devinfo_foreach_rman(print_dma, lbox);
	return true;
#else
	return false;
#endif
}

bool GetInfo_IO_Ports (TQListView *lbox)
{
#ifdef HAVE_DEVINFO_H
	/* Oh neat, current now has a neat little utility called devinfo */
	if (devinfo_init())
		return false;
	devinfo_foreach_rman(print_ioports, lbox);
	return true;
#else
	return false;
#endif
}

bool GetInfo_Sound (TQListView *lbox)
{
	TQFile *sndstat = new TQFile("/dev/sndstat");
	TQTextStream *t;
	TQString s;
	TQListViewItem *olditem = 0;

	if (!sndstat->exists() || !sndstat->open(IO_ReadOnly)) {

		s = i18n("Your sound system could not be queried.  /dev/sndstat does not exist or is not readable.");
		olditem = new TQListViewItem(lbox, olditem, s);
	} else {
		t = new TQTextStream(sndstat);
		while (!(s=t->readLine()).isNull()) {
			olditem = new TQListViewItem(lbox, olditem, s);
		}

		delete t;
		sndstat->close();
	}

	delete sndstat;
	return true;
}

bool GetInfo_SCSI (TQListView *lbox)
{
	FILE *pipe;
	TQFile *camcontrol = new TQFile("/sbin/camcontrol");
	TQTextStream *t;
	TQString s;
	TQListViewItem *olditem = 0;

	if (!camcontrol->exists()) {
		s = i18n ("SCSI subsystem could not be queried: /sbin/camcontrol could not be found");
		olditem = new TQListViewItem(lbox, olditem, s);
	} else if ((pipe = popen("/sbin/camcontrol devlist 2>&1", "r")) == NULL) {
		s = i18n ("SCSI subsystem could not be queried: /sbin/camcontrol could not be executed");
		olditem = new TQListViewItem(lbox, olditem, s);
	} else {

		/* This prints out a list of all the scsi devies, perhaps eventually we could
		   parse it as opposed to schlepping it into a listbox */

		t = new TQTextStream(pipe, IO_ReadOnly);

		while (true) {
			s = t->readLine();
			if ( s.isEmpty() )
				break;
			olditem = new TQListViewItem(lbox, olditem, s);
		}

		delete t;
		pclose(pipe);
	}

	delete camcontrol;

	if (!lbox->childCount())
		return false;

	return true;
}

bool GetInfo_PCI (TQListView *lbox)
{
	FILE *pipe;
	TQFile *pcicontrol;
	TQString s, cmd;
	TQListViewItem *olditem = 0;

	pcicontrol = new TQFile("/usr/sbin/pciconf");

	if (!pcicontrol->exists()) {
		delete pcicontrol;
		pcicontrol = new TQFile("/usr/X11R6/bin/scanpci");
		if (!pcicontrol->exists()) {
			delete pcicontrol;
			pcicontrol = new TQFile("/usr/X11R6/bin/pcitweak");
			if (!pcicontrol->exists()) {
				TQString s;
				s = i18n("Could not find any programs with which to query your system's PCI information");
				(void) new TQListViewItem(lbox, 0, s);
				delete pcicontrol;
				return true;
			} else {
				cmd = "/usr/X11R6/bin/pcitweak -l 2>&1";
			}
		} else {
			cmd = "/usr/X11R6/bin/scanpci";
		}
	} else {
		cmd = "/usr/sbin/pciconf -l -v 2>&1";
	}
	delete pcicontrol;

	if ((pipe = popen(cmd.latin1(), "r")) == NULL) {
		s = i18n ("PCI subsystem could not be queried: %1 could not be executed").arg(cmd);
		olditem = new TQListViewItem(lbox, olditem, s);
	} else {

		/* This prints out a list of all the pci devies, perhaps eventually we could
		   parse it as opposed to schlepping it into a listbox */

		pclose(pipe);
		GetInfo_ReadfromPipe(lbox, cmd.latin1(), true);
	}

	if (!lbox->childCount()) {
		s = i18n("The PCI subsystem could not be queried, this may need root privileges.");
		olditem = new TQListViewItem(lbox, olditem, s);
		return true;
	}

	return true;
}

bool GetInfo_Partitions (TQListView *lbox)
{
	struct fstab *fstab_ent;

	if (setfsent() != 1) /* Try to open fstab */ {
		int s_err = errno;
		TQString s;
		s = i18n("Could not check filesystem info: ");
		s += strerror(s_err);
		(void)new TQListViewItem(lbox, 0, s);
	} else {
		lbox->addColumn(i18n("Device"));
		lbox->addColumn(i18n("Mount Point"));
		lbox->addColumn(i18n("FS Type"));
		lbox->addColumn(i18n("Mount Options"));

		while ((fstab_ent=getfsent())!=NULL) {
			new TQListViewItem(lbox, fstab_ent->fs_spec,
					  fstab_ent->fs_file, fstab_ent->fs_vfstype,
					  fstab_ent->fs_mntops);
		}

		lbox->setSorting(0);
		lbox->header()->setClickEnabled(true);

		endfsent(); /* Close fstab */
	}
	return true;
}

bool GetInfo_XServer_and_Video (TQListView *lBox)
{
	return GetInfo_XServer_Generic( lBox );
}

bool GetInfo_Devices (TQListView *lbox)
{
	TQFile *f = new TQFile("/var/run/dmesg.boot");
	if (f->open(IO_ReadOnly)) {
		TQTextStream qts(f);
		TQDict<TQListViewItem> lv_items;
		Device *dev;
		TQString line, controller;
		lbox->setRootIsDecorated(true);
		lbox->addColumn("Device");
		lbox->addColumn("Description");
		while ( !(line=qts.readLine()).isNull() ) {
			controller = GetController(line);
			if (controller.isNull())
				continue;
			dev=GetDevice(line);
			if (!dev)
				continue;
			// Ewww assuing motherboard is the only toplevel controller is rather gross
			if (controller == "motherboard") {
				if (!lv_items[dev->name]) {
					lv_items.insert(dev->name, new TQListViewItem(lbox, dev->name, dev->description) );
				}
			} else {
				TQListViewItem *parent=lv_items[controller];
				if (parent && !lv_items[dev->name]) {
					lv_items.insert(dev->name, new TQListViewItem(parent, dev->name, dev->description) );
				}
			}
		}
		return true;
	}
	return false;
}

TQString GetController(const TQString &line)
{
		if ( ( (line.startsWith("ad")) || (line.startsWith("afd")) || (line.startsWith("acd")) ) && (line.tqfind(":") < 6) ) {
			TQString controller = line;
			controller.remove(0, controller.tqfind(" at ")+4);
			if (controller.tqfind("-slave") != -1) {
				controller.remove(controller.tqfind("-slave"), controller.length());
			} else if (controller.tqfind("-master") != -1) {
				controller.remove(controller.tqfind("-master"), controller.length());
			} else
				controller=TQString::null;
			if (!controller.isNull())
				return controller;
		}
		if (line.tqfind(" on ") != -1) {
			TQString controller;
			controller = line;
			controller.remove(0, controller.tqfind(" on ")+4);
			if (controller.tqfind(" ") != -1)
				controller.remove(controller.tqfind(" "), controller.length());
			return controller;
		}
			return TQString::null;
}

Device *GetDevice(const TQString &line)
{
	Device *dev;
	int colon = line.tqfind(":");
	if (colon == -1)
		return 0;
	dev = new Device;
	dev->name = line.mid(0, colon);
	dev->description = line.mid(line.tqfind("<")+1, line.length());
	dev->description.remove(dev->description.tqfind(">"), dev->description.length());
	return dev;
}

#ifdef HAVE_DEVINFO_H

int print_irq(struct devinfo_rman *rman, void *arg)
{
	TQListView *lbox = (TQListView *)arg;
        if (strcmp(rman->dm_desc, "Interrupt request lines")==0) {
		(void)new TQListViewItem(lbox, 0, rman->dm_desc);
		devinfo_foreach_rman_resource(rman, print_resource, arg);
        }
        return(0);
}

int print_dma(struct devinfo_rman *rman, void *arg)
{
	TQListView *lbox = (TQListView *)arg;
        if (strcmp(rman->dm_desc, "DMA request lines")==0) {
		(void)new TQListViewItem(lbox, lbox->lastItem(), rman->dm_desc);
		devinfo_foreach_rman_resource(rman, print_resource, arg);
        }
        return(0);
}

int print_ioports(struct devinfo_rman *rman, void *arg)
{
	TQListView *lbox = (TQListView *)arg;

	if (strcmp(rman->dm_desc, "I/O ports")==0) {
		(void)new TQListViewItem(lbox, lbox->lastItem(), rman->dm_desc);
		devinfo_foreach_rman_resource(rman, print_resource, arg);
        }
	else if (strcmp(rman->dm_desc, "I/O memory addresses")==0) {
		(void)new TQListViewItem(lbox, lbox->lastItem(), rman->dm_desc);
		devinfo_foreach_rman_resource(rman, print_resource, arg);
	}
        return(0);
}

int print_resource(struct devinfo_res *res, void *arg)
{
        struct devinfo_dev      *dev;
        struct devinfo_rman     *rman;
        int                     hexmode;

	TQListView *lbox;

	lbox = (TQListView *)arg;

	TQString s, tmp;

        rman = devinfo_handle_to_rman(res->dr_rman);
        hexmode =  (rman->dm_size > 100) || (rman->dm_size == 0);
        tmp.sprintf(hexmode ? "0x%lx" : "%lu", res->dr_start);
	s += tmp;
        if (res->dr_size > 1) {
                tmp.sprintf(hexmode ? "-0x%lx" : "-%lu",
                    res->dr_start + res->dr_size - 1);
		s += tmp;
	}

        dev = devinfo_handle_to_device(res->dr_device);
        if ((dev != NULL) && (dev->dd_name[0] != 0)) {
                tmp.sprintf(" (%s)", dev->dd_name);
        } else {
                tmp.sprintf(" ----");
        }
	s += tmp;

	(void)new TQListViewItem(lbox, lbox->lastItem(), s);
        return(0);
}

#endif