File: details.C

package info (click to toggle)
qps 1.4.4-1
  • links: PTS
  • area: contrib
  • in suites: slink
  • size: 580 kB
  • ctags: 1,049
  • sloc: cpp: 6,545; makefile: 129; sh: 1
file content (604 lines) | stat: -rw-r--r-- 12,882 bytes parent folder | download
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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
// details.C
//
// This program is free software. See the file COPYING for details.
// Author: Mattias Engdegrd, 1997, 1998

// TODO:
// 1. geometry mgmt: find reasonable size from the start, make changes in
//    pane size reflect back on window size
// 2. try tabs under the panes instead?

// This is ripped from <linux/tcp.h>. It was just too much of a hassle to
// make it compile with both libc5 and libc6 otherwise.

enum {
  TCP_ESTABLISHED = 1,
  TCP_SYN_SENT,
  TCP_SYN_RECV,
  TCP_FIN_WAIT1,
  TCP_FIN_WAIT2,
  TCP_TIME_WAIT,
  TCP_CLOSE,
  TCP_CLOSE_WAIT,
  TCP_LAST_ACK,
  TCP_LISTEN,
  TCP_CLOSING	/* now a valid state */
};

#include <netdb.h>
#include <netinet/in.h>

#include <qaccel.h>
#include "details.h"
#include "qps.h"
#include "svec.C"

#define TABSHAPE RoundedAbove
//#define TABSBELOW

// default implementation does nothing
void Pane::config_change()
{}

Details::Details(Procinfo *p, Qps *qps)
    : QWidget(0),
      panes(4),
      pi(p)
{
    pi->details = this;
    QString cap(80);
    cap.sprintf("Process %d (%s) - details",
		pi->pid, (const char*)pi->comm);
    setCaption(cap);

    tbar = new QTabBar(this);
    tbar->setShape(QTabBar::TABSHAPE);

    Procinfo::read_sockets();
    if(pi->read_fds()) {
	Sockets *sockets = new Sockets(this);
	QTab *socktab = new QTab;
	socktab->label = "&Sockets";
	socktab->enabled = TRUE;
	tbar->addTab(socktab);
	panes.add(sockets);
    }
    Maps *maps = new Maps(this);
    QTab *mapstab = new QTab;
    mapstab->label = "&Memory Maps";
    mapstab->enabled = TRUE;
    tbar->addTab(mapstab);
    panes.add(maps);

    if(pi->fd_files) {
	Files *files = new Files(this);
	QTab *filetab = new QTab;
	filetab->label = "&Files";
	filetab->enabled = TRUE;
	tbar->addTab(filetab);
	panes.add(files);
    }

    connect(tbar, SIGNAL(selected(int)), SLOT(tab_change(int)));

    // The usual accelerators should work here as well
    QAccel *acc = new QAccel(this);
    acc->connectItem(acc->insertItem(ALT + Key_W),
		     this, SLOT(dismiss()));
    acc->connectItem(acc->insertItem(ALT + Key_Q),
		     qps, SLOT(save_quit()));
    acc->connectItem(acc->insertItem(Key_Space),
		     qps, SLOT(forced_update()));
    acc->connectItem(acc->insertItem(Key_Return),
		     qps, SLOT(forced_update()));

    tbar->adjustSize();
    int minw = tbar->width(), minh = 0;
    for(int i = 0; i < panes.size(); i++) {
	QWidget *w = panes[i]->asQWidget();
#ifdef TABSBELOW
	w->move(0, 0);
#else
	w->move(0, tbar->height() + top_spc + mid_spc);
#endif
	QSize s = w->minimumSize();
	minw = QMAX(minw, s.width());
	minh = QMAX(minh, s.height());
    }
    setMinimumSize(minw, minh + tbar->height() + top_spc + mid_spc);
#ifdef TABSBELOW
    tbar->move(left_spc, height() - tbar->height() - top_spc);
#else
    tbar->move(left_spc, top_spc);
#endif
}

Details::~Details()
{
    if(pi)
	pi->details = 0;
}

void Details::refresh()
{
    int cur = tbar->currentTab();
    if(cur >= 0 && cur < panes.size())
	panes[cur]->refresh();
}

void Details::process_gone()
{
    pi = 0;
    // for now, we just close the window. Another possibility would be
    // to leave it with a "process terminated" note.
    dismiss();
}

// slot: close the window
void Details::dismiss()
{
    emit closed(this);
}

// slot: changed displayed pane
void Details::tab_change(int id)
{
    for(int i = 0; i < panes.size(); i++)
	if(id == i)
	    panes[i]->asQWidget()->show();
	else
	    panes[i]->asQWidget()->hide();
}

// slot: react to changes in preferences
void Details::config_change()
{
    for(int i = 0; i < panes.size(); i++)
	panes[i]->config_change();
}

void Details::paintEvent(QPaintEvent *)
{
    QPainter p(this);
#ifdef TABSBELOW
    p.setPen(colorGroup().dark());
    int y = tbar->geometry().top();
#else
    p.setPen(white);
    int y = tbar->geometry().bottom();
#endif
    p.drawLine(0, y, width() - 1, y); 	// will be partially overwritten
    p.end();
}

void Details::resizeEvent(QResizeEvent *)
{
    int w = width();
    int h = height() - tbar->height() - top_spc;
#ifdef TABSBELOW
    tbar->move(left_spc, h);
#endif
    for(int i = 0; i < panes.size(); i++)
	panes[i]->asQWidget()->resize(w, h - mid_spc);
}

// user closed the window (via window manager)
void Details::closeEvent(QCloseEvent *)
{
    dismiss();
}

SimpleTable::SimpleTable(QWidget *parent, TableField *f)
    : HeadedTable(parent, HTBL_HEADING_TOOLTIPS),
      fields(f)
{}

QString SimpleTable::title(int col)
{
    return fields[col].name;
}

int SimpleTable::colWidth(int col)
{
    return fields[col].width;
}

int SimpleTable::alignment(int col)
{
    return fields[col].align;
}

int SimpleTable::leftGap(int col)
{
    return fields[col].gap;
}

QString SimpleTable::tipText(int col)
{
    return fields[col].tooltip;
}

// declaration of static members
bool Sockets::have_services = FALSE;
QIntDict<char> Sockets::servdict(101);
Lookup *Sockets::lookup = 0;

TableField Sockets::fields[] = {
    {"Proto", 44, 8, AlignLeft, "Protocol (TCP or UDP)"},
    {"Recv-Q", 60, 8, AlignRight, "Bytes in receive queue"},
    {"Send-Q", 60, 8, AlignRight, "Bytes in send queue"},
    {"Local Addr", -1, 8, AlignLeft, "Local IP address"},
    {"Port", 48, 8, AlignLeft, "Local port"},
    {"Remote Addr", -1, 8, AlignLeft, "Remote IP address"},
    {"Port", 48, 8, AlignLeft, "Remote port"},
    {"State", 128, 8, AlignLeft, "Connection state"}
};

Sockets::Sockets(QWidget *parent)
         : SimpleTable(parent, fields), Pane(this)
{
    setNumCols(SOCKFIELDS);

    if(!lookup)
	lookup = new Lookup();
    connect(lookup, SIGNAL(resolved(unsigned)),
	    SLOT(update_hostname(unsigned)));
    doing_lookup = Qps::hostname_lookup;
    refresh();
    // compute total width = window width
    int totw = 0;
    for(int i = 0; i < SOCKFIELDS; i++)
	totw += actualColWidth(i);
    resize(totw, 200);
}

Sockets::~Sockets()
{}

QString Sockets::text(int row, int col)
{
    Procinfo *p = procinfo();
    if(!p->sock_inodes)
	refresh_sockets();
    int inode = (*p->sock_inodes)[row];
    Sockinfo *si = Procinfo::socks[inode];
    if(!si)
	return "";		// process gone, return empty string

    QString s(20);
    switch(col) {
    case PROTO:
        s = (si->proto == Sockinfo::TCP) ? "tcp" : "udp";
	break;

    case RECVQ:
	s.setNum(si->rx_queue);
	break;

    case SENDQ:
	s.setNum(si->tx_queue);
	break;

    case LOCALADDR:
	s = ipAddr(si->local_addr);
	break;

    case LOCALPORT:
	{
	    const char *serv = servname(si->local_port);
	    if(serv)
		s = serv;
	    else
		s.setNum(si->local_port);
	}
	break;

    case REMOTEADDR:
	s = ipAddr(si->rem_addr);
	break;

    case REMOTEPORT:
	{
	    const char *serv = servname(si->rem_port);
	    if(serv)
		s = serv;
	    else
		s.setNum(si->rem_port);
	}
	break;

    case STATE:
	switch(si->st) {
	case TCP_ESTABLISHED:
	    s = "ESTABLISHED"; break;
	case TCP_SYN_SENT:
	    s = "SYN_SENT"; break;
	case TCP_SYN_RECV:
	    s = "SYN_RECV"; break;
	case TCP_FIN_WAIT1:
	    s = "FIN_WAIT1"; break;
	case TCP_FIN_WAIT2:
	    s = "FIN_WAIT2"; break;
	case TCP_TIME_WAIT:
	    s = "TIME_WAIT"; break;
	case TCP_CLOSE:
	    s = "CLOSE"; break;
	case TCP_CLOSE_WAIT:
	    s = "CLOSE_WAIT"; break;
	case TCP_LAST_ACK:
	    s = "LAST_ACK"; break;
	case TCP_LISTEN:
	    s = "LISTEN"; break;
	case TCP_CLOSING:
	    s = "CLOSING"; break;
	default:
	    s = "UNKNOWN"; break;
	}
	break;
    }
    return s;
}

QString Sockets::ipAddr(unsigned addr)
{
    unsigned a = htonl(addr);
    QString s;
    if(doing_lookup) {
	s = lookup->hostname(addr);
	if(s.isNull()) {
	    s.sprintf("(%d.%d.%d.%d)",
		      (a >> 24) & 0xff,
		      (a >> 16) & 0xff,
		      (a >> 8) & 0xff,
		      a & 0xff);
	}
    } else {
	if(a == 0)
	    s = "*";
	else
	    s.sprintf("%d.%d.%d.%d",
		      (a >> 24) & 0xff,
		      (a >> 16) & 0xff,
		      (a >> 8) & 0xff,
		      a & 0xff);
    }
    return s;
}

void Sockets::refresh_window()
{
    Procinfo *p = procinfo();
    if(!p) return;
    int rows = p->sock_inodes->size();
    resetWidths();
    setNumRows(rows);
    setNumCols(SOCKFIELDS);
    repaintAll();
}

void Sockets::refresh()
{
    if(refresh_sockets())
	refresh_window();
}

// return true if sockets could be read successfully, false otherwise
bool Sockets::refresh_sockets()
{
    Procinfo::read_sockets();
    return procinfo()->read_fds();
}

// react to changes in preferences
void Sockets::config_change()
{
    if(doing_lookup != Qps::hostname_lookup) {
	doing_lookup = Qps::hostname_lookup;
	setNumCols(SOCKFIELDS);
	for(int col = 0; col < SOCKFIELDS; col++)
	    widthChanged(col);
	updateTableSize();
	repaintAll();
    }
}

// slot: called when a host name has been looked up
void Sockets::update_hostname(unsigned addr)
{
    if(widthChanged(REMOTEADDR) || widthChanged(LOCALADDR)) {
	updateTableSize();
	repaintAll();
    } else {
	// just repaint some rows
	Procinfo *p = procinfo();
	if(!p->sock_inodes) {
	    Procinfo::read_sockets();
	    p->read_fds();
	}
	int rows = p->sock_inodes->size();
	for(int i = 0; i < rows; i++) {
	    int inode = (*p->sock_inodes)[i];
	    Sockinfo *si = Procinfo::socks[inode];
	    if(si->local_addr == addr)
		updateCell(i, LOCALADDR);
	    if(si->rem_addr == addr)
		updateCell(i, REMOTEADDR);
	}
    }
}

const char *Sockets::servname(unsigned port)
{
    if(!have_services) {
	have_services = TRUE;
	// fill servdict from /etc/services (just try once)
	setservent(1);
	struct servent *s;
	while((s = getservent()) != 0) {
	    unsigned short hport = ntohs((unsigned short)s->s_port);
	    if(!servdict[hport])
		servdict.insert(hport, strdup(s->s_name));
	}
	endservent();
    }
    return servdict[port];
}


TableField Maps::fields[] = {
    {"Address Range", -1, 8, AlignLeft, "Mapped addresses (hex)"},
    {"Size", 56, 8, AlignRight, "Kbytes mapped (dec)"},
    {"Perm", 48, 8, AlignLeft, "Permission flags"},
    {"Offset", -1, 8, AlignRight, "File offset at start of mapping (hex)"},
    {"Device", 52, 8, AlignLeft, "Major:Minor device numbers (hex)"},
    {"Inode", 64, 8, AlignRight, "Inode number (dec)"},
    {"File", -1, 8, AlignLeft, "File name (if available)"}
};


Maps::Maps(QWidget *parent)
    : SimpleTable(parent, fields), Pane(this)
{
    // monospaced font looks best in the table body since it contains
    // hex numerals and flag fields. Pick Courier (why not)
    setBodyFont(QFont("Courier", font().pointSize()));
    setNumCols(MAPSFIELDS);
    
    refresh();
    // compute total width = window width
    int totw = 0;
    for(int i = 0; i < MAPSFIELDS; i++)
	totw += actualColWidth(i);
    resize(totw + 20, 200);
}

Maps::~Maps()
{}

QString Maps::text(int row, int col)
{
    Procinfo *p = procinfo();
    if(!p->maps) {
	refresh_maps();
	if(!p->maps)
	    return "";
    }
    Mapsinfo *mi = (*p->maps)[row];

    QString s(20);
    switch(col) {
    case ADDRESS:
	s.sprintf((sizeof(void*) == 4) ? "%08lx-%08lx" : "%016lx-%016lx",
		  mi->from, mi->to);
	break;
    case SIZE:
	s.setNum((mi->to - mi->from) >> 10);
	break;
    case PERM:
	for(int i = 0; i < 4; i++)
	    s[i] = mi->perm[i];
	s[4] = '\0';
	break;
    case OFFSET:
	s.sprintf((sizeof(void*) == 4) ? "%08lx" : "%016lx",
		  mi->offset);
	break;
    case DEVICE:
	s.sprintf("%02x:%02x", mi->major, mi->minor);
	break;
    case INODE:
	s.setNum(mi->inode);
	break;
    case FILENAME:
	s = mi->filename;
	break;
    }
    return s;
}

void Maps::refresh_window()
{
    if(!procinfo()) return;
    int rows = procinfo()->maps->size();
    resetWidths();
    setNumRows(rows);
    setNumCols(MAPSFIELDS);
    repaintAll();
}

void Maps::refresh()
{
    if(refresh_maps())
	refresh_window();
}

bool Maps::refresh_maps()
{
    return procinfo()->read_maps();
}


TableField Files::fields[] = {
    {"Fd", 32, 8, AlignRight, "File descriptor"},
    {"Name", -1, 8, AlignLeft, "File name (if available)"}
};

Files::Files(QWidget *parent)
    : SimpleTable(parent, fields), Pane(this)
{
    setNumCols(FILEFIELDS);

    refresh();
    // compute total width = window width
    int totw = 0;
    for(int i = 0; i < FILEFIELDS; i++)
	totw += actualColWidth(i);
    resize(totw, 200);
}

Files::~Files()
{}

void Files::refresh()
{
    if(refresh_fds())
	refresh_window();
}

// return true if fds could be read successfully, false otherwise
bool Files::refresh_fds()
{
    return procinfo()->read_fds();
}

void Files::refresh_window()
{
    Procinfo *p = procinfo();
    if(!p) return;
    int rows = p->fd_files->size();
    resetWidths();
    setNumRows(rows);
    setNumCols(FILEFIELDS);
    repaintAll();
}

QString Files::text(int row, int col)
{
    Procinfo *p = procinfo();
    if(!p->fd_files) {
	refresh_fds();
	if(!p->fd_files)
	    return "";
    }
    if(row >= p->fd_files->size())
	return "";

    Fileinfo *fi = (*p->fd_files)[row];
    if(col ==  FILEDESC) {
	QString s(10);
	s.setNum(fi->fd);
	return s;
    } else
	return fi->filename;
}