~ubuntu-branches/ubuntu/gutsy/kdebase-workspace/gutsy-backports

« back to all changes in this revision

Viewing changes to kcontrol/infocenter/info/info_netbsd.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2007-09-05 20:45:14 UTC
  • Revision ID: james.westby@ubuntu.com-20070905204514-632hhspl0nvrc84i
Tags: upstream-3.93.0
ImportĀ upstreamĀ versionĀ 3.93.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * info_netbsd.cpp is part of the KDE program kcminfo.  This displays
 
3
 * various information about the NetBSD system it's running on.
 
4
 *
 
5
 * Originally written by Jaromir Dolecek <dolecek@ics.muni.cz>. CPU info
 
6
 * code has been imported from implementation of processor.cpp for KDE 1.0
 
7
 * by David Brownlee <abs@NetBSD.org> as found in NetBSD packages collection.
 
8
 * Hubert Feyer <hubertf@NetBSD.org> enhanced the sound information printing
 
9
 * quite a lot, too.
 
10
 *
 
11
 * The code is placed into public domain. Do whatever you want with it.
 
12
 */
 
13
 
 
14
#define INFO_CPU_AVAILABLE
 
15
#define INFO_IRQ_AVAILABLE
 
16
#define INFO_DMA_AVAILABLE
 
17
#define INFO_PCI_AVAILABLE
 
18
#define INFO_IOPORTS_AVAILABLE
 
19
#define INFO_SOUND_AVAILABLE
 
20
#define INFO_DEVICES_AVAILABLE
 
21
#define INFO_SCSI_AVAILABLE
 
22
#define INFO_PARTITIONS_AVAILABLE
 
23
#define INFO_XSERVER_AVAILABLE
 
24
 
 
25
 
 
26
/*
 
27
 * all following functions should return true, when the Information
 
28
 * was filled into the lBox-Widget. Returning false indicates that
 
29
 * information was not available.
 
30
 */
 
31
 
 
32
#include <sys/types.h>
 
33
#include <sys/param.h>
 
34
#include <sys/sysctl.h>
 
35
#include <sys/mount.h>
 
36
#include <stdio.h>      /* for NULL */
 
37
#include <stdlib.h>     /* for malloc(3) */
 
38
#include <fstab.h>
 
39
 
 
40
#include <QFile>
 
41
#include <QFontMetrics>
 
42
#include <qstrlist.h>
 
43
#include <QTextStream>
 
44
 
 
45
#include <kdebug.h>
 
46
#include <kio/global.h> /* for KIO::convertSize() */
 
47
 
 
48
typedef struct
 
49
  {
 
50
  int   string;
 
51
  int   name;
 
52
  const char    *title;
 
53
  } hw_info_mib_list_t;
 
54
 
 
55
bool GetInfo_CPU(QListView *lBox)
 
56
{
 
57
  static hw_info_mib_list_t hw_info_mib_list[]= {
 
58
        { 1, HW_MODEL,          "Model" },
 
59
        { 1, HW_MACHINE,        "Machine" },
 
60
        { 1, HW_MACHINE_ARCH,   "Architecture" },
 
61
        { 0, HW_NCPU,           "Number of CPUs" },
 
62
        { 0, HW_PAGESIZE,       "Pagesize" },
 
63
        { 0,0,0 }
 
64
        };
 
65
  hw_info_mib_list_t *hw_info_mib;
 
66
 
 
67
  int mib[2], num;
 
68
  char *buf;
 
69
  size_t len;
 
70
  QString value;
 
71
 
 
72
  lBox->addColumn(i18n("Information"));
 
73
  lBox->addColumn(i18n("Value"));
 
74
 
 
75
  for ( hw_info_mib = hw_info_mib_list ;  hw_info_mib->title ; ++hw_info_mib )
 
76
  {
 
77
        mib[0] = CTL_HW;
 
78
        mib[1] = hw_info_mib->name;
 
79
        if ( hw_info_mib->string ) {
 
80
                sysctl(mib,2,NULL,&len,NULL,0);
 
81
                if ( (buf = (char*)malloc(len)) ) {
 
82
                        sysctl(mib,2,buf,&len,NULL,0);
 
83
                        value = QString::fromLocal8Bit(buf);
 
84
                        free(buf);
 
85
                }
 
86
                else {
 
87
                        value = QString("Unknown");
 
88
                }
 
89
        }
 
90
        else {
 
91
                len = sizeof(num);
 
92
                sysctl(mib,2,&num,&len,NULL,0);
 
93
                value = QString::number(num);
 
94
        }
 
95
        new QListViewItem(lBox, hw_info_mib->title, value);
 
96
   }
 
97
 
 
98
   return true;
 
99
}
 
100
 
 
101
// this is used to find out which devices are currently
 
102
// on system
 
103
static bool GetDmesgInfo(QListView *lBox, const char *filter,
 
104
        void func(QListView *, QString s))
 
105
{
 
106
        QFile *dmesg = new QFile("/var/run/dmesg.boot");
 
107
        bool usepipe = false;
 
108
        FILE *pipe = NULL;
 
109
        QTextStream *t;
 
110
        bool seencpu = false;
 
111
        QString s;
 
112
        bool found = false;
 
113
 
 
114
        if (dmesg->exists() && dmesg->open(QIODevice::ReadOnly)) {
 
115
                t = new QTextStream(dmesg);
 
116
        }
 
117
        else {
 
118
                delete dmesg;
 
119
                pipe = popen("/sbin/dmesg", "r");
 
120
                if (!pipe) return false;
 
121
                usepipe = true;
 
122
                t = new QTextStream(pipe, QIODevice::ReadOnly);
 
123
        }
 
124
 
 
125
        QListViewItem *olditem = NULL;
 
126
        while(!(s = t->readLine().toLocal8Bit()).isEmpty()) {
 
127
                if (!seencpu) {
 
128
                        if (s.contains("cpu"))
 
129
                                seencpu = true;
 
130
                        else
 
131
                                continue;
 
132
                }
 
133
                if (s.contains("boot device") ||
 
134
                        s.contains("WARNING: old BSD partition ID!"))
 
135
                        break;
 
136
 
 
137
                if (!filter
 
138
                    || (filter[0] == '^' && s.find(&filter[1]) == 0)
 
139
                    || (filter[0] != '^' && s.contains(filter))) {
 
140
                        if (func)
 
141
                                func(lBox, s);
 
142
                        else
 
143
                                olditem = new QListViewItem(lBox, olditem, s);
 
144
                        found = true;
 
145
                }
 
146
        }
 
147
 
 
148
        delete t;
 
149
        if (pipe)
 
150
                pclose(pipe);
 
151
        else {
 
152
                dmesg->close();
 
153
                delete dmesg;
 
154
        }
 
155
 
 
156
        return found;
 
157
}
 
158
 
 
159
 
 
160
void
 
161
AddIRQLine(QListView *lBox, QString s)
 
162
{
 
163
        int pos, irqnum;
 
164
        char numstr[3];
 
165
 
 
166
        pos = s.find(" irq ");
 
167
        irqnum = (pos < 0) ? 0 : atoi(&s.toAscii()[pos+5]);
 
168
        if (irqnum)
 
169
                snprintf(numstr, 3, "%02d", irqnum);
 
170
        else {
 
171
                // this should never happen
 
172
                strcpy(numstr, "??");
 
173
        }
 
174
 
 
175
        new QListViewItem(lBox, numstr, s);
 
176
}
 
177
 
 
178
bool GetInfo_IRQ (QListView *lBox)
 
179
{
 
180
        lBox->addColumn(i18n("IRQ"));
 
181
        lBox->addColumn(i18n("Device"));
 
182
        lBox->setSorting(0);
 
183
        lBox->setShowSortIndicator(false);
 
184
        (void) GetDmesgInfo(lBox, " irq ", AddIRQLine);
 
185
        return true;
 
186
}
 
187
 
 
188
bool GetInfo_DMA (QListView *)
 
189
{
 
190
        return false;
 
191
}
 
192
 
 
193
bool GetInfo_PCI (QListView *lbox)
 
194
{
 
195
        if (!GetDmesgInfo(lbox, "at pci", NULL))
 
196
                new QListViewItem(lbox, i18n("No PCI devices found."));
 
197
        return true;
 
198
}
 
199
 
 
200
bool GetInfo_IO_Ports (QListView *lbox)
 
201
{
 
202
        if (!GetDmesgInfo(lbox, "port 0x", NULL))
 
203
                new QListViewItem(lbox, i18n("No I/O port devices found."));
 
204
        return true;
 
205
}
 
206
 
 
207
bool GetInfo_Sound (QListView *lbox)
 
208
{
 
209
        lbox->setSorting(false);
 
210
 
 
211
        if (!GetDmesgInfo(lbox, "audio", NULL))
 
212
                new QListViewItem(lbox, i18n("No audio devices found."));
 
213
 
 
214
        // append information for each audio devices found
 
215
        QListViewItem *lvitem = lbox->firstChild();
 
216
        for(; lvitem; lvitem = lvitem->nextSibling()) {
 
217
                QString s;
 
218
                int pos, len;
 
219
                const char *start;
 
220
                char *dev;
 
221
 
 
222
                s = lvitem->text(0);
 
223
                // The autoconf message is in form 'audio0 at auvia0: ...'
 
224
                if (s.find("audio") == 0 && (pos = s.find(" at ")) > 0) {
 
225
                        pos += 4;       // skip " at "
 
226
                        start = s.toAscii() + pos;
 
227
                        len = (int) strcspn(start, ":\n\t ");
 
228
                        dev = (char *) malloc(1 + len + 1);
 
229
                        sprintf(dev, "^%.*s", len, start);      /* safe */
 
230
 
 
231
                        GetDmesgInfo(lbox, dev, NULL);
 
232
 
 
233
                        free(dev);
 
234
                }
 
235
        }
 
236
 
 
237
        return true;
 
238
}
 
239
 
 
240
bool GetInfo_Devices (QListView *lBox)
 
241
{
 
242
        (void) GetDmesgInfo(lBox, NULL, NULL);
 
243
        return true;
 
244
}
 
245
 
 
246
bool GetInfo_SCSI (QListView *lbox)
 
247
{
 
248
        if (!GetDmesgInfo(lbox, "scsibus", NULL))
 
249
                new QListViewItem(lbox, i18n("No SCSI devices found."));
 
250
 
 
251
        // remove the 'waiting %d seconds for devices to settle' message
 
252
        QListViewItem *lvitem = lbox->firstChild();
 
253
        for(; lvitem; lvitem = lvitem->nextSibling()) {
 
254
                QString s = lvitem->text(0);
 
255
 
 
256
                if (s.contains("seconds for devices to settle")) {
 
257
                        lbox->removeItem(lvitem);
 
258
                        break;
 
259
                }
 
260
        }
 
261
        
 
262
        return true;
 
263
}
 
264
 
 
265
bool GetInfo_Partitions (QListView *lbox)
 
266
{
 
267
        int num; // number of mounts
 
268
        // FIXME: older pkgsrc patches checked ST_RDONLY for this declaration
 
269
        // what is ST_RDONLY and how does it affect getmntinfo?
 
270
        struct statfs *mnt; // mount data pointer
 
271
 
 
272
        // get mount info
 
273
        if (!(num=getmntinfo(&mnt, MNT_WAIT))) {
 
274
                kError() << "getmntinfo failed" << endl;
 
275
                return false;
 
276
        }
 
277
 
 
278
        // table headers
 
279
        lbox->addColumn(i18n("Device"));
 
280
        lbox->addColumn(i18n("Mount Point"));
 
281
        lbox->addColumn(i18n("FS Type"));
 
282
        lbox->addColumn(i18n("Total Size"));
 
283
        lbox->addColumn(i18n("Free Size"));
 
284
        lbox->addColumn(i18n("Total Nodes"));
 
285
        lbox->addColumn(i18n("Free Nodes"));
 
286
        lbox->addColumn(i18n("Flags"));
 
287
 
 
288
        // mnt points into a static array (no need to free it)
 
289
        for(; num--; ++mnt) {
 
290
                unsigned long long big[2];
 
291
                QString vv[5];
 
292
 
 
293
                big[0] = big[1] = mnt->f_bsize; // coerce the product
 
294
                big[0] *= mnt->f_blocks;
 
295
                big[1] *= mnt->f_bavail; // FIXME: use f_bfree if root?
 
296
 
 
297
                // convert to strings
 
298
                vv[0] = KIO::convertSize(big[0]);
 
299
                vv[1] = QLatin1String("%1 (%2%%)")
 
300
                                .arg(KIO::convertSize(big[1]))
 
301
                                .arg(mnt->f_blocks ? mnt->f_bavail*100/mnt->f_blocks : 0);
 
302
 
 
303
                // FIXME: these two are large enough to punctuate
 
304
                vv[2] = QString::number(mnt->f_files);
 
305
                vv[3] = QLatin1String("%1 (%2%%) ")
 
306
                                .arg(mnt->f_ffree)
 
307
                                .arg(mnt->f_files ? mnt->f_ffree*100/mnt->f_files : 0);
 
308
 
 
309
                vv[4].clear();
 
310
#define MNTF(x) if (mnt->f_flags & MNT_##x) vv[4] += QLatin1String(#x " ");
 
311
                MNTF(ASYNC)
 
312
                MNTF(DEFEXPORTED)
 
313
                MNTF(EXKERB)
 
314
                MNTF(EXNORESPORT)
 
315
                MNTF(EXPORTANON)
 
316
                MNTF(EXPORTED)
 
317
                MNTF(EXPUBLIC)
 
318
                MNTF(EXRDONLY)
 
319
                MNTF(IGNORE)
 
320
                MNTF(LOCAL)
 
321
                MNTF(NOATIME)
 
322
                MNTF(NOCOREDUMP)
 
323
                MNTF(NODEV)
 
324
                MNTF(NODEVMTIME)
 
325
                MNTF(NOEXEC)
 
326
                MNTF(NOSUID)
 
327
                MNTF(QUOTA)
 
328
                MNTF(RDONLY)
 
329
                MNTF(ROOTFS)
 
330
                MNTF(SOFTDEP)
 
331
                MNTF(SYMPERM)
 
332
                MNTF(SYNCHRONOUS)
 
333
                MNTF(UNION)
 
334
#undef MNTF
 
335
 
 
336
                // put it in the table
 
337
                // FIXME: there're more data but we have limited args (this is wrong! just add!)
 
338
                new QListViewItem(lbox,
 
339
                        // FIXME: names need pad space
 
340
                        mnt->f_mntfromname,
 
341
                        mnt->f_mntonname,
 
342
                        mnt->f_fstypename,
 
343
                        vv[0], vv[1], vv[2], vv[3], vv[4]);
 
344
        }
 
345
 
 
346
        // job well done
 
347
        return true;
 
348
}
 
349
 
 
350
bool GetInfo_XServer_and_Video (QListView *lBox)
 
351
{
 
352
        return GetInfo_XServer_Generic( lBox );
 
353
}