forked from Atoptool/atop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
netatopif.c
534 lines (451 loc) · 11.4 KB
/
netatopif.c
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
/*
** ATOP - System & Process Monitor
**
** The program 'atop' offers the possibility to view the activity of
** the system on system-level as well as process-level.
**
** This source-file contains functions to interface with the netatop
** module in the kernel. That module keeps track of network activity
** per process and thread.
** ================================================================
** Author: Gerlof Langeveld
** E-mail: gerlof.langeveld@atoptool.nl
** Date: August/September 2012
**
** 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; either version 2, or (at your option) any
** later version.
**
** 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.
*/
#include <sys/types.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <signal.h>
#include <zlib.h>
#include <sys/mman.h>
#include "atop.h"
#include "photoproc.h"
#include "netatop.h"
#include "netatopd.h"
static int netsock = -1;
static int netexitfd = -1;
static struct naheader *nahp;
static int semid = -1;
static unsigned long lastseq;
/*
** storage of last exited tasks read from exitfile
** every exitstore struct is registered in hash buckets,
** by its pid or by its begin time
*/
struct exitstore {
struct exitstore *next;
unsigned char isused;
struct netpertask npt;
};
#define NHASH 1024 // must be power of two!
#define HASHCALC(x) ((x)&(NHASH-1))
static struct exitstore *esbucket[NHASH];
static struct exitstore *exitall;
static int exitnum;
static char exithash;
static void fill_networkcnt(struct tstat *, struct tstat *,
struct exitstore *);
/*
** open a raw socket to the IP layer (root privs required)
*/
void
netatop_ipopen(void)
{
netsock = socket(PF_INET, SOCK_RAW, IPPROTO_RAW);
}
/*
** check if at this moment the netatop kernel module is loaded and
** the netatopd daemon is active
*/
void
netatop_probe(void)
{
struct sembuf semdecr = {1, -1, SEM_UNDO};
socklen_t sl = 0;
struct stat exstat;
/*
** check if IP socket is open
*/
if (netsock == -1)
return;
/*
** probe if the netatop module is active
*/
if ( getsockopt(netsock, SOL_IP, NETATOP_PROBE, NULL, &sl) != 0)
{
supportflags &= ~NETATOP;
supportflags &= ~NETATOPD;
return;
}
// set appropriate support flag
supportflags |= NETATOP;
/*
** check if the netatopd daemon is active to register exited tasks
** and decrement semaphore to indicate that we want to subscribe
*/
if (semid == -1)
{
if ( (semid = semget(SEMAKEY, 0, 0)) == -1 ||
semop(semid, &semdecr, 1) == -1 )
{
supportflags &= ~NETATOPD;
return;
}
}
if (semctl(semid, 0, GETVAL, 0) != 1)
{
supportflags &= ~NETATOPD;
return;
}
/*
** check if exitfile still open and not removed by netatopd
*/
if (netexitfd != -1)
{
if ( fstat(netexitfd, &exstat) == 0 &&
exstat.st_nlink > 0 ) // not removed
{
supportflags |= NETATOPD;
return;
}
else
{
(void) close(netexitfd);
if (nahp)
munmap(nahp, sizeof *nahp);
netexitfd = -1;
nahp = NULL;
}
}
/*
** open file with compressed stats of exited tasks
** and (re)mmap the start record, mainly to obtain current sequence
*/
if (netexitfd == -1)
{
if ( (netexitfd = open(NETEXITFILE, O_RDONLY, 0)) == -1)
{
supportflags &= ~NETATOPD;
return;
}
}
if ( (nahp = mmap((void *)0, sizeof *nahp, PROT_READ, MAP_SHARED,
netexitfd, 0)) == (void *) -1)
{
(void) close(netexitfd);
netexitfd = -1;
nahp = NULL;
supportflags &= ~NETATOPD;
return;
}
/*
** if this is a new incarnation of the netatopd daemon,
** position seek pointer on first task that is relevant to us
** and remember last sequence number to know where to start
*/
(void) lseek(netexitfd, 0, SEEK_END);
lastseq = nahp->curseq;
// set appropriate support flag
supportflags |= NETATOPD;
}
void
netatop_signoff(void)
{
struct sembuf semincr = {1, +1, SEM_UNDO};
if (netsock == -1 || nahp == NULL)
return;
if (supportflags & NETATOPD)
{
regainrootprivs();
(void) semop(semid, &semincr, 1);
kill(nahp->mypid, SIGHUP);
if (! droprootprivs())
mcleanstop(42, "failed to drop root privs\n");
(void) munmap(nahp, sizeof *nahp);
(void) close(netexitfd);
}
}
/*
** read network counters for one existing task
** (type 'g' for thread group or type 't' for thread)
*/
void
netatop_gettask(pid_t id, char type, struct tstat *tp)
{
struct netpertask npt;
socklen_t socklen = sizeof npt;
int cmd = (type == 'g' ?
NETATOP_GETCNT_TGID : NETATOP_GETCNT_PID);
/*
** if kernel module netatop not active on this system, skip call
*/
if (!(supportflags & NETATOP) ) {
memset(&tp->net, 0, sizeof tp->net);
return;
}
/*
** get statistics of this process/thread
*/
npt.id = id;
regainrootprivs();
if (getsockopt(netsock, SOL_IP, cmd, &npt, &socklen) != 0) {
memset(&tp->net, 0, sizeof tp->net);
if (! droprootprivs())
mcleanstop(42, "failed to drop root privs\n");
if (errno == ENOPROTOOPT || errno == EPERM)
{
supportflags &= ~NETATOP;
supportflags &= ~NETATOPD;
close(netsock);
netsock = -1;
}
return;
}
if (! droprootprivs())
mcleanstop(42, "failed to drop root privs\n");
/*
** statistics available: fill counters
*/
tp->net.tcpsnd = npt.tc.tcpsndpacks;
tp->net.tcprcv = npt.tc.tcprcvpacks;
tp->net.tcpssz = npt.tc.tcpsndbytes;
tp->net.tcprsz = npt.tc.tcprcvbytes;
tp->net.udpsnd = npt.tc.udpsndpacks;
tp->net.udprcv = npt.tc.udprcvpacks;
tp->net.udpssz = npt.tc.udpsndbytes;
tp->net.udprsz = npt.tc.udprcvbytes;
}
/*
** read all exited processes that have been added to the exitfile
** and store them into memory
*/
unsigned int
netatop_exitstore(void)
{
socklen_t socklen = 0, nexitnet, sz, nr=0;
unsigned long uncomplen;
unsigned char nextsize;
unsigned char readbuf[nahp->ntplen+100];
unsigned char databuf[nahp->ntplen];
struct netpertask *tmp = (struct netpertask *)databuf;
struct exitstore *esp;
regainrootprivs();
/*
** force garbage collection:
** netatop module builds new list of exited processes that
** can be read by netatopd and written to exitfile
*/
if (getsockopt(netsock, SOL_IP, NETATOP_FORCE_GC, NULL, &socklen)!=0) {
if (! droprootprivs())
mcleanstop(42, "failed to drop root privs\n");
if (errno == ENOPROTOOPT || errno == EPERM)
{
supportflags &= ~NETATOP;
supportflags &= ~NETATOPD;
close(netsock);
netsock = -1;
}
return 0;
}
/*
** wait until list of exited processes is read by netatopd
** and available to be read by atop
*/
if (getsockopt(netsock, SOL_IP, NETATOP_EMPTY_EXIT, 0, &socklen) !=0) {
if (! droprootprivs())
mcleanstop(42, "failed to drop root privs\n");
if (errno == ENOPROTOOPT || errno == EPERM)
{
supportflags &= ~NETATOP;
supportflags &= ~NETATOPD;
close(netsock);
netsock = -1;
}
return 0;
}
if (! droprootprivs())
mcleanstop(42, "failed to drop root privs\n");
/*
** verify how many exited processes are available to be read
** from the exitfile
*/
nexitnet = nahp->curseq - lastseq;
lastseq = nahp->curseq;
if (nexitnet == 0)
return 0;
/*
** allocate storage for all exited processes
*/
exitall = malloc(nexitnet * sizeof(struct exitstore));
ptrverify(exitall, "Malloc failed for %d exited netprocs\n", nexitnet);
memset(exitall, 0, nexitnet * sizeof(struct exitstore));
esp = exitall;
/*
** read next byte from exitfile that specifies the length
** of the next record
*/
if ( read(netexitfd, &nextsize, 1) != 1)
return 0;
/*
** read the next record and (if possible) the byte specifying
** the size of the next record
*/
while ( (sz = read(netexitfd, readbuf, nextsize+1)) >= nextsize)
{
/*
** decompress record and store it
*/
uncomplen = nahp->ntplen;
if (nahp->ntplen <= sizeof(struct netpertask))
{
(void) uncompress((Byte *)&(esp->npt), &uncomplen,
readbuf, nextsize);
}
else
{
(void) uncompress((Byte *)databuf, &uncomplen,
readbuf, nextsize);
esp->npt = *tmp;
}
esp++;
nr++;
/*
** check if we have read all records
*/
if (nr == nexitnet)
{
/*
** if we have read one byte too many:
** reposition seek pointer
*/
if (sz > nextsize)
(void) lseek(netexitfd, -1, SEEK_CUR);
break;
}
/*
** prepare reading next record
*/
if (sz > nextsize)
nextsize = readbuf[nextsize];
else
break; // unexpected: more requested than available
}
exitnum = nr;
return nr;
}
/*
** remove all stored exited processes from the hash bucket list
*/
void
netatop_exiterase(void)
{
free(exitall);
memset(esbucket, 0, sizeof esbucket);
exitnum = 0;
}
/*
** add all stored tasks to a hash bucket, either
** by pid (argument 'p') or by begin time (argument 'b')
*/
void
netatop_exithash(char hashtype)
{
int i, h;
struct exitstore *esp;
for (i=0, esp=exitall; i < exitnum; i++, esp++)
{
if (hashtype == 'p')
h = HASHCALC(esp->npt.id);
else
h = HASHCALC(esp->npt.btime);
esp->next = esbucket[h];
esbucket[h] = esp;
}
exithash = hashtype;
}
/*
** search for relevant exited network task and
** update counters in tstat struct
*/
void
netatop_exitfind(unsigned long key, struct tstat *dev, struct tstat *pre)
{
int h = HASHCALC(key);
struct exitstore *esp;
/*
** if bucket empty, forget about it
*/
if ( (esp = esbucket[h]) == NULL)
return;
/*
** search thru hash bucket list
*/
for (; esp; esp=esp->next)
{
switch (exithash)
{
case 'p': // search by PID
if (key != esp->npt.id)
continue;
/*
** correct PID found
*/
fill_networkcnt(dev, pre, esp);
break;
case 'b': // search by begin time
if (esp->isused)
continue;
if (key != esp->npt.btime)
continue;
/*
** btime is okay; additional checks required
*/
if ( strcmp(esp->npt.command, pre->gen.name) != 0)
continue;
if (esp->npt.tc.tcpsndpacks < pre->net.tcpsnd ||
esp->npt.tc.tcpsndbytes < pre->net.tcpssz ||
esp->npt.tc.tcprcvpacks < pre->net.tcprcv ||
esp->npt.tc.tcprcvbytes < pre->net.tcprsz ||
esp->npt.tc.udpsndpacks < pre->net.udpsnd ||
esp->npt.tc.udpsndbytes < pre->net.udpssz ||
esp->npt.tc.udprcvpacks < pre->net.udprcv ||
esp->npt.tc.udprcvbytes < pre->net.udprsz )
continue;
esp->isused = 1;
fill_networkcnt(dev, pre, esp);
break;
}
}
}
static void
fill_networkcnt(struct tstat *dev, struct tstat *pre, struct exitstore *esp)
{
dev->net.tcpsnd = esp->npt.tc.tcpsndpacks - pre->net.tcpsnd;
dev->net.tcpssz = esp->npt.tc.tcpsndbytes - pre->net.tcpssz;
dev->net.tcprcv = esp->npt.tc.tcprcvpacks - pre->net.tcprcv;
dev->net.tcprsz = esp->npt.tc.tcprcvbytes - pre->net.tcprsz;
dev->net.udpsnd = esp->npt.tc.udpsndpacks - pre->net.udpsnd;
dev->net.udpssz = esp->npt.tc.udpsndbytes - pre->net.udpssz;
dev->net.udprcv = esp->npt.tc.udprcvpacks - pre->net.udprcv;
dev->net.udprsz = esp->npt.tc.udprcvbytes - pre->net.udprsz;
}