Skip to content

Commit 922a1b5

Browse files
sargunkees
authored andcommitted
selftests/seccomp: Refactor get_proc_stat to split out file reading code
This splits up the get_proc_stat function to make it so we can use it as a generic helper to read the nth field from multiple different files, versus replicating the logic in multiple places. Signed-off-by: Sargun Dhillon <sargun@sargun.me> Cc: linux-kselftest@vger.kernel.org Signed-off-by: Kees Cook <keescook@chromium.org> Link: https://lore.kernel.org/r/20220503080958.20220-3-sargun@sargun.me
1 parent c2aa2df commit 922a1b5

1 file changed

Lines changed: 38 additions & 16 deletions

File tree

tools/testing/selftests/seccomp/seccomp_bpf.c

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4297,32 +4297,54 @@ TEST_F(O_SUSPEND_SECCOMP, seize)
42974297
ASSERT_EQ(EPERM, errno);
42984298
}
42994299

4300-
static char get_proc_stat(int pid)
4300+
/*
4301+
* get_nth - Get the nth, space separated entry in a file.
4302+
*
4303+
* Returns the length of the read field.
4304+
* Throws error if field is zero-lengthed.
4305+
*/
4306+
static ssize_t get_nth(struct __test_metadata *_metadata, const char *path,
4307+
const unsigned int position, char **entry)
43014308
{
4302-
char proc_path[100] = {0};
43034309
char *line = NULL;
4304-
size_t len = 0;
4310+
unsigned int i;
43054311
ssize_t nread;
4306-
char status;
4312+
size_t len = 0;
43074313
FILE *f;
4308-
int i;
43094314

4310-
snprintf(proc_path, sizeof(proc_path), "/proc/%d/stat", pid);
4311-
f = fopen(proc_path, "r");
4312-
if (f == NULL)
4313-
ksft_exit_fail_msg("%s - Could not open %s\n",
4314-
strerror(errno), proc_path);
4315+
f = fopen(path, "r");
4316+
ASSERT_NE(f, NULL) {
4317+
TH_LOG("Coud not open %s: %s", path, strerror(errno));
4318+
}
43154319

4316-
for (i = 0; i < 3; i++) {
4320+
for (i = 0; i < position; i++) {
43174321
nread = getdelim(&line, &len, ' ', f);
4318-
if (nread <= 0)
4319-
ksft_exit_fail_msg("Failed to read status: %s\n",
4320-
strerror(errno));
4322+
ASSERT_GE(nread, 0) {
4323+
TH_LOG("Failed to read %d entry in file %s", i, path);
4324+
}
43214325
}
4326+
fclose(f);
4327+
4328+
ASSERT_GT(nread, 0) {
4329+
TH_LOG("Entry in file %s had zero length", path);
4330+
}
4331+
4332+
*entry = line;
4333+
return nread - 1;
4334+
}
4335+
4336+
/* For a given PID, get the task state (D, R, etc...) */
4337+
static char get_proc_stat(struct __test_metadata *_metadata, pid_t pid)
4338+
{
4339+
char proc_path[100] = {0};
4340+
char status;
4341+
char *line;
4342+
4343+
snprintf(proc_path, sizeof(proc_path), "/proc/%d/stat", pid);
4344+
ASSERT_EQ(get_nth(_metadata, proc_path, 3, &line), 1);
43224345

43234346
status = *line;
43244347
free(line);
4325-
fclose(f);
43264348

43274349
return status;
43284350
}
@@ -4383,7 +4405,7 @@ TEST(user_notification_fifo)
43834405
/* This spins until all of the children are sleeping */
43844406
restart_wait:
43854407
for (i = 0; i < ARRAY_SIZE(pids); i++) {
4386-
if (get_proc_stat(pids[i]) != 'S') {
4408+
if (get_proc_stat(_metadata, pids[i]) != 'S') {
43874409
nanosleep(&delay, NULL);
43884410
goto restart_wait;
43894411
}

0 commit comments

Comments
 (0)