~ubuntu-branches/ubuntu/warty/kdebase/warty

« back to all changes in this revision

Viewing changes to kcontrol/info/info_fbsd.cpp

  • Committer: Bazaar Package Importer
  • Author(s): LaMont Jones
  • Date: 2004-09-16 04:51:45 UTC
  • Revision ID: james.westby@ubuntu.com-20040916045145-9vr63kith3k1cpza
Tags: upstream-3.2.2
ImportĀ upstreamĀ versionĀ 3.2.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * $Id: info_fbsd.cpp,v 1.27 2003/01/25 14:31:11 augustin Exp $
 
3
 *
 
4
 * info_fbsd.cpp is part of the KDE program kcminfo.  This displays
 
5
 * various information about the system (hopefully a FreeBSD system)
 
6
 * it's running on.
 
7
 *
 
8
 * All of the devinfo bits were blatantly stolen from the devinfo utility
 
9
 * provided with FreeBSD 5.0 (and later).  No gross hacks were harmed 
 
10
 * during the creation of info_fbsd.cpp.  Thanks Mike.
 
11
 */
 
12
 
 
13
#define INFO_CPU_AVAILABLE
 
14
#define INFO_IRQ_AVAILABLE
 
15
#define INFO_DMA_AVAILABLE
 
16
#define INFO_PCI_AVAILABLE
 
17
#define INFO_IOPORTS_AVAILABLE
 
18
#define INFO_SOUND_AVAILABLE
 
19
#define INFO_DEVICES_AVAILABLE
 
20
#define INFO_SCSI_AVAILABLE
 
21
#define INFO_PARTITIONS_AVAILABLE
 
22
#define INFO_XSERVER_AVAILABLE
 
23
 
 
24
 
 
25
/*
 
26
 * all following functions should return TRUE, when the Information
 
27
 * was filled into the lBox-Widget. Returning FALSE indicates that
 
28
 * information was not available.
 
29
 */
 
30
 
 
31
#ifdef HAVE_CONFIG_H
 
32
        #include <config.h>
 
33
#endif
 
34
 
 
35
#include <sys/types.h>
 
36
#include <sys/sysctl.h>
 
37
 
 
38
//#if __FreeBSD_version >= 500042
 
39
//      #define we should have devinfo.h
 
40
//#else
 
41
//      #define we probably don't have devinfo.h
 
42
//#endif
 
43
 
 
44
#ifdef HAVE_DEVINFO_H
 
45
        extern "C" {
 
46
                #include <devinfo.h>
 
47
        }
 
48
#endif
 
49
 
 
50
#include <errno.h>
 
51
#include <fstab.h>
 
52
#include <string.h>
 
53
 
 
54
#include <qdict.h>
 
55
#include <qfile.h>
 
56
#include <qptrlist.h>
 
57
#include <qstring.h>
 
58
#include <qtextstream.h>
 
59
 
 
60
class Device {
 
61
public:
 
62
        Device (QString n=QString::null, QString d=QString::null)
 
63
                {name=n; description=d;}
 
64
        QString name, description;
 
65
};
 
66
 
 
67
void ProcessChildren(QString name);
 
68
QString GetController(const QString &line);
 
69
Device *GetDevice(const QString &line);
 
70
 
 
71
#ifdef HAVE_DEVINFO_H
 
72
extern "C" {
 
73
        int print_irq(struct devinfo_rman *rman, void *arg);
 
74
        int print_dma(struct devinfo_rman *rman, void *arg);
 
75
        int print_ioports(struct devinfo_rman *rman, void *arg);
 
76
        int print_resource(struct devinfo_res *res, void *arg);
 
77
}
 
78
#endif
 
79
 
 
80
bool GetInfo_CPU (QListView *lBox)
 
81
{
 
82
        // Modified 13 July 2000 for SMP by Brad Hughes - bhughes@trolltech.com
 
83
 
 
84
        int ncpu;
 
85
        size_t len;
 
86
 
 
87
        len = sizeof(ncpu);
 
88
        sysctlbyname("hw.ncpu", &ncpu, &len, NULL, 0);
 
89
 
 
90
        QString cpustring;
 
91
        for (int i = ncpu; i > 0; i--) {
 
92
                /* Stuff for sysctl */
 
93
                char *buf;
 
94
                int i_buf;
 
95
 
 
96
                // get the processor model
 
97
                sysctlbyname("hw.model", NULL, &len, NULL, 0);
 
98
                buf = new char[len];
 
99
                sysctlbyname("hw.model", buf, &len, NULL, 0);
 
100
 
 
101
                // get the TSC speed if we can
 
102
                len = sizeof(i_buf);
 
103
                if (sysctlbyname("machdep.tsc_freq", &i_buf, &len, NULL, 0) != -1) {
 
104
                        cpustring = i18n("CPU %1: %2, %3 MHz").arg(i).arg(buf).arg(i_buf/1000000);
 
105
                } else {
 
106
                        cpustring = i18n("CPU %1: %2, unknown speed").arg(i).arg(buf);
 
107
                }
 
108
 
 
109
                /* Put everything in the listbox */
 
110
                new QListViewItem(lBox, cpustring);
 
111
 
 
112
                /* Clean up after ourselves, this time I mean it ;-) */
 
113
                delete buf;
 
114
        }
 
115
 
 
116
        return true;
 
117
}
 
118
 
 
119
bool GetInfo_IRQ (QListView *lbox)
 
120
{
 
121
#ifdef HAVE_DEVINFO_H
 
122
        /* systat lists the interrupts assigned to devices as well as how many were
 
123
           generated.  Parsing its output however is about as fun as a sandpaper
 
124
           enema.  The best idea would probably be to rip out the guts of systat.
 
125
           Too bad it's not very well commented */
 
126
        /* Oh neat, current now has a neat little utility called devinfo */
 
127
        if (devinfo_init())
 
128
                return false;
 
129
        devinfo_foreach_rman(print_irq, lbox);
 
130
        return true;
 
131
#else
 
132
        return false;
 
133
#endif
 
134
}
 
135
 
 
136
bool GetInfo_DMA (QListView *lbox)
 
137
{
 
138
#ifdef HAVE_DEVINFO_H
 
139
        /* Oh neat, current now has a neat little utility called devinfo */
 
140
        if (devinfo_init())
 
141
                return false;
 
142
        devinfo_foreach_rman(print_dma, lbox);
 
143
        return true;
 
144
#else
 
145
        return false;
 
146
#endif
 
147
}
 
148
 
 
149
bool GetInfo_IO_Ports (QListView *lbox)
 
150
{
 
151
#ifdef HAVE_DEVINFO_H
 
152
        /* Oh neat, current now has a neat little utility called devinfo */
 
153
        if (devinfo_init())
 
154
                return false;
 
155
        devinfo_foreach_rman(print_ioports, lbox);
 
156
        return true;
 
157
#else
 
158
        return false;
 
159
#endif
 
160
}
 
161
 
 
162
bool GetInfo_Sound (QListView *lbox)
 
163
{
 
164
        QFile *sndstat = new QFile("/dev/sndstat");
 
165
        QTextStream *t;
 
166
        QString s;
 
167
        QListViewItem *olditem = 0;
 
168
 
 
169
        if (!sndstat->exists() || !sndstat->open(IO_ReadOnly)) {
 
170
 
 
171
                s = i18n("Your sound system could not be queried.  /dev/sndstat does not exist or is not readable.");
 
172
                olditem = new QListViewItem(lbox, olditem, s);
 
173
        } else {
 
174
                t = new QTextStream(sndstat);
 
175
                while (!(s=t->readLine()).isNull()) {
 
176
                        olditem = new QListViewItem(lbox, olditem, s);
 
177
                }
 
178
 
 
179
                delete t;
 
180
                sndstat->close();
 
181
        }
 
182
 
 
183
        delete sndstat;
 
184
        return true;
 
185
}
 
186
 
 
187
bool GetInfo_SCSI (QListView *lbox)
 
188
{
 
189
        FILE *pipe;
 
190
        QFile *camcontrol = new QFile("/sbin/camcontrol");
 
191
        QTextStream *t;
 
192
        QString s;
 
193
        QListViewItem *olditem = 0;
 
194
 
 
195
        if (!camcontrol->exists()) {
 
196
                s = i18n ("SCSI subsystem could not be queried: /sbin/camcontrol could not be found");
 
197
                olditem = new QListViewItem(lbox, olditem, s);
 
198
        } else if ((pipe = popen("/sbin/camcontrol devlist 2>&1", "r")) == NULL) {
 
199
                s = i18n ("SCSI subsystem could not be queried: /sbin/camcontrol could not be executed");
 
200
                olditem = new QListViewItem(lbox, olditem, s);
 
201
        } else {
 
202
 
 
203
                /* This prints out a list of all the scsi devies, perhaps eventually we could
 
204
                   parse it as opposed to schlepping it into a listbox */
 
205
 
 
206
                t = new QTextStream(pipe, IO_ReadOnly);
 
207
 
 
208
                while (true) {
 
209
                        s = t->readLine();
 
210
                        if ( s.isEmpty() )
 
211
                                break;
 
212
                        olditem = new QListViewItem(lbox, olditem, s);
 
213
                }
 
214
 
 
215
                delete t;
 
216
                pclose(pipe);
 
217
        }
 
218
 
 
219
        delete camcontrol;
 
220
 
 
221
        if (!lbox->childCount())
 
222
                return false;
 
223
 
 
224
        return true;
 
225
}
 
226
 
 
227
bool GetInfo_PCI (QListView *lbox)
 
228
{
 
229
        FILE *pipe;
 
230
        QFile *pcicontrol;
 
231
        QTextStream *t;
 
232
        QString s, cmd;
 
233
        QListViewItem *olditem = 0;
 
234
 
 
235
        pcicontrol = new QFile("/usr/X11R6/bin/scanpci");
 
236
 
 
237
        if (!pcicontrol->exists()) {
 
238
                delete pcicontrol;
 
239
                pcicontrol = new QFile("/usr/X11R6/bin/pcitweak");
 
240
                if (!pcicontrol->exists()) {
 
241
                        delete pcicontrol;
 
242
                        pcicontrol = new QFile("/usr/sbin/pciconf");
 
243
                        if (!pcicontrol->exists()) {
 
244
                                QString s;
 
245
                                s = i18n("Could not find any programs with which to query your system's PCI information");
 
246
                                (void) new QListViewItem(lbox, 0, s);
 
247
                                delete pcicontrol;
 
248
                                return true;
 
249
                        } else {
 
250
                                cmd = "/usr/sbin/pciconf -l";
 
251
                        }
 
252
                } else {
 
253
                        cmd = "/usr/X11R6/bin/pcitweak -l 2>&1";
 
254
                }
 
255
        } else {
 
256
                cmd = "/usr/X11R6/bin/scanpci";
 
257
        }
 
258
        delete pcicontrol;
 
259
 
 
260
        if ((pipe = popen(cmd.latin1(), "r")) == NULL) {
 
261
                s = i18n ("PCI subsystem could not be queried: %1 could not be executed").arg(cmd);
 
262
                olditem = new QListViewItem(lbox, olditem, s);
 
263
        } else {
 
264
 
 
265
                /* This prints out a list of all the pci devies, perhaps eventually we could
 
266
                   parse it as opposed to schlepping it into a listbox */
 
267
 
 
268
                t = new QTextStream(pipe, IO_ReadOnly);
 
269
 
 
270
                while (true) {
 
271
                        s = t->readLine();
 
272
                        if ( s.isEmpty() )
 
273
                                break;
 
274
                        olditem = new QListViewItem(lbox, olditem, s);
 
275
                }
 
276
 
 
277
                delete t;
 
278
                pclose(pipe);
 
279
        }
 
280
 
 
281
        if (!lbox->childCount()) {
 
282
                s = i18n("The PCI subsystem could not be queried, this may need root privileges.");
 
283
                olditem = new QListViewItem(lbox, olditem, s);
 
284
                return true;
 
285
        }
 
286
 
 
287
        return true;
 
288
}
 
289
 
 
290
bool GetInfo_Partitions (QListView *lbox)
 
291
{
 
292
        struct fstab *fstab_ent;
 
293
 
 
294
        if (setfsent() != 1) /* Try to open fstab */ {
 
295
                int s_err = errno;
 
296
                QString s;
 
297
                s = i18n("Could not check filesystem info: ");
 
298
                s += strerror(s_err);
 
299
                (void)new QListViewItem(lbox, 0, s);
 
300
        } else {
 
301
                lbox->addColumn(i18n("Device"));
 
302
                lbox->addColumn(i18n("Mount Point"));
 
303
                lbox->addColumn(i18n("FS Type"));
 
304
                lbox->addColumn(i18n("Mount Options"));
 
305
 
 
306
                while ((fstab_ent=getfsent())!=NULL) {
 
307
                        new QListViewItem(lbox, fstab_ent->fs_spec,
 
308
                                          fstab_ent->fs_file, fstab_ent->fs_vfstype,
 
309
                                          fstab_ent->fs_mntops);
 
310
                }
 
311
 
 
312
                lbox->setSorting(0);
 
313
                lbox->header()->setClickEnabled(true);
 
314
 
 
315
                endfsent(); /* Close fstab */
 
316
        }
 
317
        return true;
 
318
}
 
319
 
 
320
bool GetInfo_XServer_and_Video (QListView *lBox)
 
321
{
 
322
        return GetInfo_XServer_Generic( lBox );
 
323
}
 
324
 
 
325
bool GetInfo_Devices (QListView *lbox)
 
326
{
 
327
        QFile *f = new QFile("/var/run/dmesg.boot");
 
328
        if (f->open(IO_ReadOnly)) {
 
329
                QTextStream qts(f);
 
330
                QDict<QListViewItem> lv_items;
 
331
                Device *dev;
 
332
                QString line, controller;
 
333
                lbox->setRootIsDecorated(true);
 
334
                lbox->addColumn("Device");
 
335
                lbox->addColumn("Description");
 
336
                while ( !(line=qts.readLine()).isNull() ) {
 
337
                        controller = GetController(line);
 
338
                        if (controller.isNull())
 
339
                                continue;
 
340
                        dev=GetDevice(line);
 
341
                        if (!dev)
 
342
                                continue;
 
343
                        // Ewww assuing motherboard is the only toplevel controller is rather gross
 
344
                        if (controller == "motherboard") {
 
345
                                if (!lv_items[dev->name]) {
 
346
                                        lv_items.insert(dev->name, new QListViewItem(lbox, dev->name, dev->description) );
 
347
                                }
 
348
                        } else {
 
349
                                QListViewItem *parent=lv_items[controller];
 
350
                                if (parent && !lv_items[dev->name]) {
 
351
                                        lv_items.insert(dev->name, new QListViewItem(parent, dev->name, dev->description) );
 
352
                                }
 
353
                        }
 
354
                }
 
355
                return true;
 
356
        }
 
357
        return false;
 
358
}
 
359
 
 
360
QString GetController(const QString &line)
 
361
{
 
362
                if ( ( (line.startsWith("ad")) || (line.startsWith("afd")) || (line.startsWith("acd")) ) && (line.find(":") < 6) ) {
 
363
                        QString controller = line;
 
364
                        controller.remove(0, controller.find(" at ")+4);
 
365
                        if (controller.find("-slave") != -1) {
 
366
                                controller.remove(controller.find("-slave"), controller.length());
 
367
                        } else if (controller.find("-master") != -1) {
 
368
                                controller.remove(controller.find("-master"), controller.length());
 
369
                        } else
 
370
                                controller=QString::null;
 
371
                        if (!controller.isNull())
 
372
                                return controller;
 
373
                }
 
374
                if (line.find(" on ") != -1) {
 
375
                        QString controller;
 
376
                        controller = line;
 
377
                        controller.remove(0, controller.find(" on ")+4);
 
378
                        if (controller.find(" ") != -1)
 
379
                                controller.remove(controller.find(" "), controller.length());
 
380
                        return controller;
 
381
                }
 
382
                        return QString::null;
 
383
}
 
384
 
 
385
Device *GetDevice(const QString &line)
 
386
{
 
387
        Device *dev;
 
388
        int colon = line.find(":");
 
389
        if (colon == -1)
 
390
                return 0;
 
391
        dev = new Device;
 
392
        dev->name = line.mid(0, colon);
 
393
        dev->description = line.mid(line.find("<")+1, line.length());
 
394
        dev->description.remove(dev->description.find(">"), dev->description.length());
 
395
        return dev;
 
396
}
 
397
 
 
398
#ifdef HAVE_DEVINFO_H
 
399
 
 
400
int print_irq(struct devinfo_rman *rman, void *arg)
 
401
{
 
402
        QListView *lbox = (QListView *)arg;
 
403
        if (strcmp(rman->dm_desc, "Interrupt request lines")==0) {
 
404
                (void)new QListViewItem(lbox, 0, rman->dm_desc);
 
405
                devinfo_foreach_rman_resource(rman, print_resource, arg);
 
406
        }
 
407
        return(0);
 
408
}
 
409
 
 
410
int print_dma(struct devinfo_rman *rman, void *arg)
 
411
{
 
412
        QListView *lbox = (QListView *)arg;
 
413
        if (strcmp(rman->dm_desc, "DMA request lines")==0) {
 
414
                (void)new QListViewItem(lbox, lbox->lastItem(), rman->dm_desc);
 
415
                devinfo_foreach_rman_resource(rman, print_resource, arg);
 
416
        }
 
417
        return(0);
 
418
}
 
419
 
 
420
int print_ioports(struct devinfo_rman *rman, void *arg)
 
421
{
 
422
        QListView *lbox = (QListView *)arg;
 
423
 
 
424
        if (strcmp(rman->dm_desc, "I/O ports")==0) {
 
425
                (void)new QListViewItem(lbox, lbox->lastItem(), rman->dm_desc);
 
426
                devinfo_foreach_rman_resource(rman, print_resource, arg);
 
427
        }
 
428
        else if (strcmp(rman->dm_desc, "I/O memory addresses")==0) {
 
429
                (void)new QListViewItem(lbox, lbox->lastItem(), rman->dm_desc);
 
430
                devinfo_foreach_rman_resource(rman, print_resource, arg);
 
431
        }
 
432
        return(0);
 
433
}
 
434
 
 
435
int print_resource(struct devinfo_res *res, void *arg)
 
436
{
 
437
        struct devinfo_dev      *dev;
 
438
        struct devinfo_rman     *rman;
 
439
        int                     hexmode;
 
440
 
 
441
        QListView *lbox;
 
442
 
 
443
        lbox = (QListView *)arg;
 
444
 
 
445
        QString s, tmp;
 
446
 
 
447
        rman = devinfo_handle_to_rman(res->dr_rman);
 
448
        hexmode =  (rman->dm_size > 100) || (rman->dm_size == 0);
 
449
        tmp.sprintf(hexmode ? "0x%lx" : "%lu", res->dr_start);
 
450
        s += tmp;
 
451
        if (res->dr_size > 1) {
 
452
                tmp.sprintf(hexmode ? "-0x%lx" : "-%lu",
 
453
                    res->dr_start + res->dr_size - 1);
 
454
                s += tmp;
 
455
        }
 
456
 
 
457
        dev = devinfo_handle_to_device(res->dr_device);
 
458
        if ((dev != NULL) && (dev->dd_name[0] != 0)) {
 
459
                tmp.sprintf(" (%s)", dev->dd_name);
 
460
        } else {
 
461
                tmp.sprintf(" ----");
 
462
        }
 
463
        s += tmp;
 
464
 
 
465
        (void)new QListViewItem(lbox, lbox->lastItem(), s);
 
466
        return(0);
 
467
}
 
468
 
 
469
#endif