Skip to content

Commit e379890

Browse files
committed
Code improvements for cgroupv2 handling
- removed uninitalized variable 'ret' - added break to while (fgets) loop to avoid unnecessary reading
1 parent 9f68140 commit e379890

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

photoproc.c

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,8 @@ photoproc(struct tstat *tasklist, int maxtask)
120120

121121
while (fgets(line, sizeof line, fp))
122122
{
123-
if (memcmp(line, "0::", 3) == 0) { // equal?
123+
if (memcmp(line, "0::", 3) == 0) // equal?
124+
{
124125
supportflags |= CGROUPV2;
125126
break;
126127
}
@@ -1056,37 +1057,39 @@ proccgroupv2(struct tstat *curtask)
10561057
int hash, pathlen, restlen, nslash;
10571058
struct cgroupv2vals *pvals = NULL, *ptarget;
10581059
char *p, *slashes[MAXSLASH];
1059-
int ret;
10601060

10611061
/*
10621062
** open the cgroup file of the current process and
1063-
** read one line that should start with '0::' for cgroup v2
1063+
** read the line that starts with '0::' for cgroup v2
10641064
*/
1065+
curtask->gen.cgpath[0] = '\0';
1066+
10651067
if ( (fp = fopen("cgroup", "r")) )
10661068
{
10671069
while (fgets(line, sizeof line, fp))
10681070
{
10691071
if ( memcmp(line, "0::", 3) == 0) // equal?
10701072
{
1071-
ret = 1;
10721073
line[ strlen(line)-1 ] = '\0'; // remove newline
10731074

10741075
relpath = line+3;
10751076

10761077
strncpy(curtask->gen.cgpath, relpath,
10771078
sizeof curtask->gen.cgpath);
1079+
10781080
curtask->gen.cgpath[sizeof curtask->gen.cgpath -1] = '\0';
1081+
1082+
break;
10791083
}
10801084
}
10811085

1082-
if (ret != 1)
1083-
curtask->gen.cgpath[0] = '\0';
1084-
10851086
fclose(fp);
1087+
1088+
if (curtask->gen.cgpath[0] == '\0')
1089+
return 0; // no cgroupv2 support
10861090
}
10871091
else // open failed; no permission
10881092
{
1089-
curtask->gen.cgpath[0] = '\0';
10901093
return 2;
10911094
}
10921095

0 commit comments

Comments
 (0)