Module Name: src Committed By: jmmv Date: Sun Mar 2 20:13:12 UTC 2014
Modified Files: src/tests/kernel: t_sysv.c Log Message: Make cleanup routines actually work. The only way to pass global state from the body to the cleanup is via the file system. Fixes leaks of global system resources (in all cases, given that the body does not by itself clean things up). To generate a diff of this commit: cvs rdiff -u -r1.3 -r1.4 src/tests/kernel/t_sysv.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/tests/kernel/t_sysv.c diff -u src/tests/kernel/t_sysv.c:1.3 src/tests/kernel/t_sysv.c:1.4 --- src/tests/kernel/t_sysv.c:1.3 Wed Jul 24 11:44:10 2013 +++ src/tests/kernel/t_sysv.c Sun Mar 2 20:13:12 2014 @@ -1,4 +1,4 @@ -/* $NetBSD: t_sysv.c,v 1.3 2013/07/24 11:44:10 skrll Exp $ */ +/* $NetBSD: t_sysv.c,v 1.4 2014/03/02 20:13:12 jmmv Exp $ */ /*- * Copyright (c) 1999, 2007 The NetBSD Foundation, Inc. @@ -88,9 +88,6 @@ size_t pgsize; #define MTYPE_2 3 #define MTYPE_2_ACK 4 -int sender_msqid = -1; -int sender_semid = -1; -int sender_shmid = -1; pid_t child_pid; key_t msgkey, semkey, shmkey; @@ -104,6 +101,38 @@ union semun { }; +/* Writes an integer to a file. To be used from the body of the test + * cases below to pass any global identifiers to the cleanup routine. */ +static void +write_int(const char *path, const int value) +{ + int output; + + output = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0600); + ATF_REQUIRE_MSG(output != -1, "Failed to create %s", path); + write(output, &value, sizeof(value)); + close(output); +} + + +/* Reads an integer from a file. To be used from the cleanup routines + * of the test cases below. */ +static int +read_int(const char *path) +{ + int input; + + input = open(path, O_RDONLY); + if (input == -1) + return -1; + else { + int value; + read(input, &value, sizeof(value)); + return value; + } +} + + void sigsys_handler(int signo) { @@ -175,6 +204,7 @@ ATF_TC_BODY(msg, tc) struct msqid_ds m_ds; struct mymsg m; sigset_t sigmask; + int sender_msqid; int loop; int c_status; @@ -206,6 +236,7 @@ ATF_TC_BODY(msg, tc) sender_msqid = msgget(msgkey, IPC_CREAT | 0640); ATF_REQUIRE_MSG(sender_msqid != -1, "msgget: %d", errno); + write_int("sender_msqid", sender_msqid); if (did_sigsys) { atf_tc_skip("SYSV Message Queue not supported"); @@ -304,14 +335,15 @@ ATF_TC_BODY(msg, tc) ATF_TC_CLEANUP(msg, tc) { + int sender_msqid; /* * Remove the message queue if it exists. */ + sender_msqid = read_int("sender_msqid"); if (sender_msqid != -1) - ATF_REQUIRE_MSG(msgctl(sender_msqid, IPC_RMID, NULL) != -1, - "msgctl IPC_RMID: %d", errno); - sender_msqid = -1; + if (msgctl(sender_msqid, IPC_RMID, NULL) == -1) + err(1, "msgctl IPC_RMID"); } void @@ -411,6 +443,7 @@ ATF_TC_BODY(sem, tc) union semun sun; struct semid_ds s_ds; sigset_t sigmask; + int sender_semid; int i; int c_status; @@ -442,6 +475,7 @@ ATF_TC_BODY(sem, tc) sender_semid = semget(semkey, 1, IPC_CREAT | 0640); ATF_REQUIRE_MSG(sender_semid != -1, "semget: %d", errno); + write_int("sender_semid", sender_semid); if (did_sigsys) { atf_tc_skip("SYSV Semaphore not supported"); @@ -542,14 +576,15 @@ ATF_TC_BODY(sem, tc) ATF_TC_CLEANUP(sem, tc) { + int sender_semid; /* * Remove the semaphore if it exists */ + sender_semid = read_int("sender_semid"); if (sender_semid != -1) - ATF_REQUIRE_MSG(semctl(sender_semid, 0, IPC_RMID) != -1, - "semctl IPC_RMID: %d", errno); - sender_semid = -1; + if (semctl(sender_semid, 0, IPC_RMID) == -1) + err(1, "semctl IPC_RMID"); } void @@ -637,6 +672,7 @@ ATF_TC_BODY(shm, tc) struct shmid_ds s_ds; sigset_t sigmask; char *shm_buf; + int sender_shmid; int c_status; /* @@ -670,6 +706,7 @@ ATF_TC_BODY(shm, tc) ATF_REQUIRE_MSG((sender_shmid = shmget(shmkey, pgsize, IPC_CREAT | 0640)) != -1, "shmget: %d", errno); + write_int("sender_shmid", sender_shmid); ATF_REQUIRE_MSG(shmctl(sender_shmid, IPC_STAT, &s_ds) != -1, "shmctl IPC_STAT: %d", errno); @@ -740,14 +777,15 @@ ATF_TC_BODY(shm, tc) ATF_TC_CLEANUP(shm, tc) { + int sender_shmid; /* * Remove the shared memory area if it exists. */ + sender_shmid = read_int("sender_shmid"); if (sender_shmid != -1) - ATF_REQUIRE_MSG(shmctl(sender_shmid, IPC_RMID, NULL) != -1, - "shmctl IPC_RMID: %d", errno); - sender_shmid = -1; + if (shmctl(sender_shmid, IPC_RMID, NULL) == -1) + err(1, "shmctl IPC_RMID"); } void