Skip to content

Commit 9f68140

Browse files
committed
Merge branch 'fix/cgroup2_handling' of github.com:ton31337/atop into ton31337-fix/cgroup2_handling
2 parents db78190 + fc4dc01 commit 9f68140

File tree

1 file changed

+38
-34
lines changed

1 file changed

+38
-34
lines changed

photoproc.c

Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/*
2-
** ATOP - System & Process Monitor
3-
**
2+
** ATOP - System & Process Monitor
3+
**
44
** The program 'atop' offers the possibility to view the activity of
55
** the system on system-level as well as process-/thread-level.
6-
**
6+
**
77
** This source-file contains functions to read the process-administration
88
** of every running process from kernel-space and extract the required
99
** activity-counters.
@@ -118,10 +118,12 @@ photoproc(struct tstat *tasklist, int maxtask)
118118
{
119119
char line[128];
120120

121-
if (fgets(line, sizeof line, fp))
121+
while (fgets(line, sizeof line, fp))
122122
{
123-
if (memcmp(line, "0::", 3) == 0) // equal?
123+
if (memcmp(line, "0::", 3) == 0) { // equal?
124124
supportflags |= CGROUPV2;
125+
break;
126+
}
125127
}
126128

127129
fclose(fp);
@@ -144,20 +146,20 @@ photoproc(struct tstat *tasklist, int maxtask)
144146
*/
145147
regainrootprivs();
146148

147-
/*
148-
** if kernel module is not active on this system,
149+
/*
150+
** if kernel module is not active on this system,
149151
** netatop-bpf will try tp run;
150152
*/
151153
if (!(supportflags & NETATOPD)) {
152154
netatop_bpf_probe();
153155
}
154-
/*
155-
** if netatop-bpf is not active on this system,
156+
/*
157+
** if netatop-bpf is not active on this system,
156158
** kernel module will try to run;
157159
*/
158160
if (!(supportflags & NETATOPBPF)) {
159161
netatop_probe();
160-
}
162+
}
161163

162164
/*
163165
** if netatop-bpf is active on this system, skip call
@@ -245,7 +247,7 @@ photoproc(struct tstat *tasklist, int maxtask)
245247
*/
246248
if (getwchan)
247249
procwchan(curtask);
248-
250+
249251
if (supportflags & NETATOPBPF) {
250252
struct taskcount *tc = g_hash_table_lookup(ghash_net, &(curtask->gen.tgid));
251253
if (tc) {
@@ -288,7 +290,7 @@ photoproc(struct tstat *tasklist, int maxtask)
288290
*/
289291
curtask->cpu.rundelay = 0;
290292
curtask->cpu.blkdelay = 0;
291-
293+
292294
/*
293295
** nvcsw and nivcsw on process level only
294296
** concerns the delays of the main thread;
@@ -305,15 +307,15 @@ photoproc(struct tstat *tasklist, int maxtask)
305307
unsigned long cur_nth = 0;
306308

307309
dirtask = opendir(".");
308-
310+
309311
/*
310312
** due to race condition, opendir() might
311313
** have failed (leave task and process-level
312314
** directories)
313315
*/
314-
if( dirtask == NULL )
316+
if( dirtask == NULL )
315317
{
316-
if(chdir("../..") == -1);
318+
if(chdir("../..") == -1);
317319
continue;
318320
}
319321

@@ -333,7 +335,7 @@ photoproc(struct tstat *tasklist, int maxtask)
333335
if ( chdir("..") == -1);
334336
continue;
335337
}
336-
338+
337339
if ( !procstatus(curthr) )
338340
{
339341
if ( chdir("..") == -1);
@@ -442,7 +444,7 @@ counttasks(void)
442444
char origdir[1024];
443445

444446
/*
445-
** determine total number of threads
447+
** determine total number of threads
446448
*/
447449
if ( (fp = fopen("/proc/loadavg", "r")) != NULL)
448450
{
@@ -461,7 +463,7 @@ counttasks(void)
461463

462464

463465
/*
464-
** add total number of processes
466+
** add total number of processes
465467
*/
466468
if ( getcwd(origdir, sizeof origdir) == NULL)
467469
mcleanstop(53, "cannot determine cwd\n");
@@ -983,7 +985,7 @@ procschedstat(struct tstat *curtask)
983985
static char *schedstatfile = "schedstat";
984986

985987
/*
986-
** open the schedstat file
988+
** open the schedstat file
987989
*/
988990
if ( (fp = fopen(schedstatfile, "r")) )
989991
{
@@ -1054,31 +1056,33 @@ proccgroupv2(struct tstat *curtask)
10541056
int hash, pathlen, restlen, nslash;
10551057
struct cgroupv2vals *pvals = NULL, *ptarget;
10561058
char *p, *slashes[MAXSLASH];
1059+
int ret;
10571060

10581061
/*
10591062
** open the cgroup file of the current process and
10601063
** read one line that should start with '0::' for cgroup v2
10611064
*/
10621065
if ( (fp = fopen("cgroup", "r")) )
10631066
{
1064-
if (fgets(line, sizeof line, fp))
1067+
while (fgets(line, sizeof line, fp))
10651068
{
1066-
if ( memcmp(line, "0::", 3) ) // unequal?
1069+
if ( memcmp(line, "0::", 3) == 0) // equal?
10671070
{
1068-
fclose(fp);
1069-
curtask->gen.cgpath[0] = '\0';
1070-
return 0; // no cgroupv2 support
1071+
ret = 1;
1072+
line[ strlen(line)-1 ] = '\0'; // remove newline
1073+
1074+
relpath = line+3;
1075+
1076+
strncpy(curtask->gen.cgpath, relpath,
1077+
sizeof curtask->gen.cgpath);
1078+
curtask->gen.cgpath[sizeof curtask->gen.cgpath -1] = '\0';
10711079
}
10721080
}
1073-
fclose(fp);
1074-
1075-
line[ strlen(line)-1 ] = '\0'; // remove newline
10761081

1077-
relpath = line+3;
1082+
if (ret != 1)
1083+
curtask->gen.cgpath[0] = '\0';
10781084

1079-
strncpy(curtask->gen.cgpath, relpath,
1080-
sizeof curtask->gen.cgpath);
1081-
curtask->gen.cgpath[sizeof curtask->gen.cgpath -1] = '\0';
1085+
fclose(fp);
10821086
}
10831087
else // open failed; no permission
10841088
{
@@ -1088,7 +1092,7 @@ proccgroupv2(struct tstat *curtask)
10881092

10891093
/*
10901094
** cgroup v2 pathname of this process is known;
1091-
** prepare absolute pathname of cgroup
1095+
** prepare absolute pathname of cgroup
10921096
*/
10931097
pathlen = snprintf(abspath, sizeof abspath, "%s%s/",
10941098
CGROUPROOT, relpath);
@@ -1293,7 +1297,7 @@ fillcgroupv2(struct cgroupv2vals *pvals, char *abspath, char *extpath,
12931297
**
12941298
** return value: number of entries in retvals filled
12951299
*/
1296-
int
1300+
int
12971301
readcgroupv2(char *abspath, char *extpath, char *fname, int restlen,
12981302
long retvals[])
12991303
{
@@ -1343,7 +1347,7 @@ readcgroupv2(char *abspath, char *extpath, char *fname, int restlen,
13431347
*/
13441348
static struct cgroupv2vals *
13451349
findhashcgroupv2(char *relpath, int *phash)
1346-
{
1350+
{
13471351
struct cgroupv2vals *p;
13481352
char *s;
13491353
int hash = 0;

0 commit comments

Comments
 (0)