Skip to content

Commit e6f6681

Browse files
committed
Merge branch 'ton31337-fix/cgroup2_handling'
2 parents db78190 + e379890 commit e6f6681

File tree

1 file changed

+42
-35
lines changed

1 file changed

+42
-35
lines changed

photoproc.c

Lines changed: 42 additions & 35 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,13 @@ 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
{
123123
if (memcmp(line, "0::", 3) == 0) // equal?
124+
{
124125
supportflags |= CGROUPV2;
126+
break;
127+
}
125128
}
126129

127130
fclose(fp);
@@ -144,20 +147,20 @@ photoproc(struct tstat *tasklist, int maxtask)
144147
*/
145148
regainrootprivs();
146149

147-
/*
148-
** if kernel module is not active on this system,
150+
/*
151+
** if kernel module is not active on this system,
149152
** netatop-bpf will try tp run;
150153
*/
151154
if (!(supportflags & NETATOPD)) {
152155
netatop_bpf_probe();
153156
}
154-
/*
155-
** if netatop-bpf is not active on this system,
157+
/*
158+
** if netatop-bpf is not active on this system,
156159
** kernel module will try to run;
157160
*/
158161
if (!(supportflags & NETATOPBPF)) {
159162
netatop_probe();
160-
}
163+
}
161164

162165
/*
163166
** if netatop-bpf is active on this system, skip call
@@ -245,7 +248,7 @@ photoproc(struct tstat *tasklist, int maxtask)
245248
*/
246249
if (getwchan)
247250
procwchan(curtask);
248-
251+
249252
if (supportflags & NETATOPBPF) {
250253
struct taskcount *tc = g_hash_table_lookup(ghash_net, &(curtask->gen.tgid));
251254
if (tc) {
@@ -288,7 +291,7 @@ photoproc(struct tstat *tasklist, int maxtask)
288291
*/
289292
curtask->cpu.rundelay = 0;
290293
curtask->cpu.blkdelay = 0;
291-
294+
292295
/*
293296
** nvcsw and nivcsw on process level only
294297
** concerns the delays of the main thread;
@@ -305,15 +308,15 @@ photoproc(struct tstat *tasklist, int maxtask)
305308
unsigned long cur_nth = 0;
306309

307310
dirtask = opendir(".");
308-
311+
309312
/*
310313
** due to race condition, opendir() might
311314
** have failed (leave task and process-level
312315
** directories)
313316
*/
314-
if( dirtask == NULL )
317+
if( dirtask == NULL )
315318
{
316-
if(chdir("../..") == -1);
319+
if(chdir("../..") == -1);
317320
continue;
318321
}
319322

@@ -333,7 +336,7 @@ photoproc(struct tstat *tasklist, int maxtask)
333336
if ( chdir("..") == -1);
334337
continue;
335338
}
336-
339+
337340
if ( !procstatus(curthr) )
338341
{
339342
if ( chdir("..") == -1);
@@ -442,7 +445,7 @@ counttasks(void)
442445
char origdir[1024];
443446

444447
/*
445-
** determine total number of threads
448+
** determine total number of threads
446449
*/
447450
if ( (fp = fopen("/proc/loadavg", "r")) != NULL)
448451
{
@@ -461,7 +464,7 @@ counttasks(void)
461464

462465

463466
/*
464-
** add total number of processes
467+
** add total number of processes
465468
*/
466469
if ( getcwd(origdir, sizeof origdir) == NULL)
467470
mcleanstop(53, "cannot determine cwd\n");
@@ -983,7 +986,7 @@ procschedstat(struct tstat *curtask)
983986
static char *schedstatfile = "schedstat";
984987

985988
/*
986-
** open the schedstat file
989+
** open the schedstat file
987990
*/
988991
if ( (fp = fopen(schedstatfile, "r")) )
989992
{
@@ -1057,38 +1060,42 @@ proccgroupv2(struct tstat *curtask)
10571060

10581061
/*
10591062
** open the cgroup file of the current process and
1060-
** read one line that should start with '0::' for cgroup v2
1063+
** read the line that starts with '0::' for cgroup v2
10611064
*/
1065+
curtask->gen.cgpath[0] = '\0';
1066+
10621067
if ( (fp = fopen("cgroup", "r")) )
10631068
{
1064-
if (fgets(line, sizeof line, fp))
1069+
while (fgets(line, sizeof line, fp))
10651070
{
1066-
if ( memcmp(line, "0::", 3) ) // unequal?
1071+
if ( memcmp(line, "0::", 3) == 0) // equal?
10671072
{
1068-
fclose(fp);
1069-
curtask->gen.cgpath[0] = '\0';
1070-
return 0; // no cgroupv2 support
1073+
line[ strlen(line)-1 ] = '\0'; // remove newline
1074+
1075+
relpath = line+3;
1076+
1077+
strncpy(curtask->gen.cgpath, relpath,
1078+
sizeof curtask->gen.cgpath);
1079+
1080+
curtask->gen.cgpath[sizeof curtask->gen.cgpath -1] = '\0';
1081+
1082+
break;
10711083
}
10721084
}
1073-
fclose(fp);
10741085

1075-
line[ strlen(line)-1 ] = '\0'; // remove newline
1076-
1077-
relpath = line+3;
1086+
fclose(fp);
10781087

1079-
strncpy(curtask->gen.cgpath, relpath,
1080-
sizeof curtask->gen.cgpath);
1081-
curtask->gen.cgpath[sizeof curtask->gen.cgpath -1] = '\0';
1088+
if (curtask->gen.cgpath[0] == '\0')
1089+
return 0; // no cgroupv2 support
10821090
}
10831091
else // open failed; no permission
10841092
{
1085-
curtask->gen.cgpath[0] = '\0';
10861093
return 2;
10871094
}
10881095

10891096
/*
10901097
** cgroup v2 pathname of this process is known;
1091-
** prepare absolute pathname of cgroup
1098+
** prepare absolute pathname of cgroup
10921099
*/
10931100
pathlen = snprintf(abspath, sizeof abspath, "%s%s/",
10941101
CGROUPROOT, relpath);
@@ -1293,7 +1300,7 @@ fillcgroupv2(struct cgroupv2vals *pvals, char *abspath, char *extpath,
12931300
**
12941301
** return value: number of entries in retvals filled
12951302
*/
1296-
int
1303+
int
12971304
readcgroupv2(char *abspath, char *extpath, char *fname, int restlen,
12981305
long retvals[])
12991306
{
@@ -1343,7 +1350,7 @@ readcgroupv2(char *abspath, char *extpath, char *fname, int restlen,
13431350
*/
13441351
static struct cgroupv2vals *
13451352
findhashcgroupv2(char *relpath, int *phash)
1346-
{
1353+
{
13471354
struct cgroupv2vals *p;
13481355
char *s;
13491356
int hash = 0;

0 commit comments

Comments
 (0)