Skip to content

Commit 49298dd

Browse files
yosrym93kelsey-steele
authored andcommitted
selftests: cgroup: return -errno from cg_read()/cg_write() on failure
Currently, cg_read()/cg_write() returns 0 on success and -1 on failure. Modify them to return the -errno on failure. Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: Yosry Ahmed <[email protected]> Acked-by: Shakeel Butt <[email protected]> Acked-by: David Rientjes <[email protected]> Acked-by: Roman Gushchin <[email protected]> Cc: Chen Wandun <[email protected]> Cc: Dave Hansen <[email protected]> Cc: Greg Thelen <[email protected]> Cc: Johannes Weiner <[email protected]> Cc: Jonathan Corbet <[email protected]> Cc: Michal Hocko <[email protected]> Cc: "Michal Koutn" <[email protected]> Cc: Shuah Khan <[email protected]> Cc: Tejun Heo <[email protected]> Cc: Tim Chen <[email protected]> Cc: Vaibhav Jain <[email protected]> Cc: Wei Xu <[email protected]> Cc: Yu Zhao <[email protected]> Cc: Zefan Li <[email protected]> Signed-off-by: Andrew Morton <[email protected]> [kms: backport to 5.15] Signed-off-by: Kelsey Steele <[email protected]>
1 parent 0c85d55 commit 49298dd

File tree

1 file changed

+19
-25
lines changed

1 file changed

+19
-25
lines changed

tools/testing/selftests/cgroup/cgroup_util.c

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,42 +19,37 @@
1919
#include "cgroup_util.h"
2020
#include "../clone3/clone3_selftests.h"
2121

22+
/* Returns read len on success, or -errno on failure. */
2223
static ssize_t read_text(const char *path, char *buf, size_t max_len)
2324
{
2425
ssize_t len;
2526
int fd;
2627

2728
fd = open(path, O_RDONLY);
2829
if (fd < 0)
29-
return fd;
30+
return -errno;
3031

3132
len = read(fd, buf, max_len - 1);
32-
if (len < 0)
33-
goto out;
3433

35-
buf[len] = 0;
36-
out:
34+
if (len >= 0)
35+
buf[len] = 0;
36+
3737
close(fd);
38-
return len;
38+
return len < 0 ? -errno : len;
3939
}
4040

41+
/* Returns written len on success, or -errno on failure. */
4142
static ssize_t write_text(const char *path, char *buf, ssize_t len)
4243
{
4344
int fd;
4445

4546
fd = open(path, O_WRONLY | O_APPEND);
4647
if (fd < 0)
47-
return fd;
48+
return -errno;
4849

4950
len = write(fd, buf, len);
50-
if (len < 0) {
51-
close(fd);
52-
return len;
53-
}
54-
5551
close(fd);
56-
57-
return len;
52+
return len < 0 ? -errno : len;
5853
}
5954

6055
char *cg_name(const char *root, const char *name)
@@ -87,16 +82,16 @@ char *cg_control(const char *cgroup, const char *control)
8782
return ret;
8883
}
8984

85+
/* Returns 0 on success, or -errno on failure. */
9086
int cg_read(const char *cgroup, const char *control, char *buf, size_t len)
9187
{
9288
char path[PATH_MAX];
89+
ssize_t ret;
9390

9491
snprintf(path, sizeof(path), "%s/%s", cgroup, control);
9592

96-
if (read_text(path, buf, len) >= 0)
97-
return 0;
98-
99-
return -1;
93+
ret = read_text(path, buf, len);
94+
return ret >= 0 ? 0 : ret;
10095
}
10196

10297
int cg_read_strcmp(const char *cgroup, const char *control,
@@ -177,17 +172,15 @@ long cg_read_lc(const char *cgroup, const char *control)
177172
return cnt;
178173
}
179174

175+
/* Returns 0 on success, or -errno on failure. */
180176
int cg_write(const char *cgroup, const char *control, char *buf)
181177
{
182178
char path[PATH_MAX];
183-
ssize_t len = strlen(buf);
179+
ssize_t len = strlen(buf), ret;
184180

185181
snprintf(path, sizeof(path), "%s/%s", cgroup, control);
186-
187-
if (write_text(path, buf, len) == len)
188-
return 0;
189-
190-
return -1;
182+
ret = write_text(path, buf, len);
183+
return ret == len ? 0 : ret;
191184
}
192185

193186
int cg_find_unified_root(char *root, size_t len)
@@ -545,7 +538,8 @@ ssize_t proc_read_text(int pid, bool thread, const char *item, char *buf, size_t
545538
else
546539
snprintf(path, sizeof(path), "/proc/%d/%s", pid, item);
547540

548-
return read_text(path, buf, size);
541+
size = read_text(path, buf, size);
542+
return size < 0 ? -1 : size;
549543
}
550544

551545
int proc_read_strstr(int pid, bool thread, const char *item, const char *needle)

0 commit comments

Comments
 (0)