syscalls/swapon: Further cleanup.
* Sort out included files
* Use safe macros
* etc
Signed-off-by: Cyril Hrubis <chrubis@suse.cz>
diff --git a/testcases/kernel/syscalls/swapon/swapon02.c b/testcases/kernel/syscalls/swapon/swapon02.c
index 009e95d..7ee28e8 100644
--- a/testcases/kernel/syscalls/swapon/swapon02.c
+++ b/testcases/kernel/syscalls/swapon/swapon02.c
@@ -14,11 +14,11 @@
*
*/
/*
- * This test case checks whether swapon(2) system call returns
- * 1. ENOENT when the path does not exist
- * 2. EINVAL when the path exists but is invalid
- * 3. EPERM when user is not a superuser
- * 4. EBUSY when the specified path is already being used as a swap area
+ * This test case checks whether swapon(2) system call returns
+ * 1. ENOENT when the path does not exist
+ * 2. EINVAL when the path exists but is invalid
+ * 3. EPERM when user is not a superuser
+ * 4. EBUSY when the specified path is already being used as a swap area
*/
#include <unistd.h>
@@ -34,45 +34,60 @@
#include <signal.h>
#include "test.h"
#include "usctest.h"
-#include "config.h"
#include "linux_syscall_numbers.h"
+#include "safe_macros.h"
#include "tst_fs_type.h"
-#include "swaponoff.h"
#include "libswapon.h"
static void setup(void);
static void cleanup(void);
-static int setup01(void);
-static int cleanup01(void);
-static int setup02(void);
-static int setup03(void);
-static int cleanup03(void);
+static void setup01(void);
+static void cleanup01(void);
char *TCID = "swapon02";
int TST_TOTAL = 4;
-static char nobody_uid[] = "nobody";
-static struct passwd *ltpuser;
+static uid_t nobody_uid;
+static int do_swapoff;
static int exp_enos[] = { EPERM, EINVAL, ENOENT, EBUSY, 0 };
static struct test_case_t {
- char *err_desc; /* error description */
- int exp_errno; /* expected error number */
- char *exp_errval; /* Expected errorvalue string */
- char *path; /* path to swapon */
- int (*setupfunc) (); /* Test setup function */
- int (*cleanfunc) (); /* Test cleanup function */
-} testcase[] = {
- {
- "Path does not exist", ENOENT, "ENOENT", "./abcd", NULL, NULL}, {
- "Invalid path", EINVAL, "EINVAL", "./nofile", setup02, NULL}, {
- "Permission denied", EPERM, "EPERM", "./swapfile01",
- setup01, cleanup01}, {
- "The specified path is already being used as a swap area",
- EBUSY, "EBUSY", "./alreadyused", setup03, cleanup03}
+ char *err_desc;
+ int exp_errno;
+ char *exp_errval;
+ char *path;
+ void (*setup)(void);
+ void (*cleanup)(void);
+} testcases[] = {
+ {"Path does not exist", ENOENT, "ENOENT", "./doesnotexist", NULL, NULL},
+ {"Invalid path", EINVAL, "EINVAL", "./notswap", NULL, NULL},
+ {"Permission denied", EPERM, "EPERM", "./swapfile01",
+ setup01, cleanup01},
+ {"File already used", EBUSY, "EBUSY", "./alreadyused", NULL, NULL},
};
+static void verify_swapon(struct test_case_t *test)
+{
+ if (test->setup)
+ test->setup();
+
+ TEST(ltp_syscall(__NR_swapon, test->path, 0));
+
+ if (test->cleanup)
+ test->cleanup();
+
+ if (TEST_RETURN == -1 && TEST_ERRNO == test->exp_errno) {
+ tst_resm(TPASS, "swapon(2) expected failure;"
+ " Got errno - %s : %s",
+ test->exp_errval, test->err_desc);
+ } else {
+ tst_resm(TFAIL, "swapon(2) failed to produce expected error:"
+ " %d, errno: %s and got %d.", test->exp_errno,
+ test->exp_errval, TEST_ERRNO);
+ }
+}
+
int main(int ac, char **av)
{
int lc, i;
@@ -85,153 +100,28 @@
for (lc = 0; TEST_LOOPING(lc); lc++) {
tst_count = 0;
- for (i = 0; i < TST_TOTAL; i++) {
-
- /* do the setup if the test have one */
- if (testcase[i].setupfunc
- && testcase[i].setupfunc() == -1) {
- tst_resm(TWARN,
- "Failed to setup test %d."
- " Skipping test", i);
- continue;
- } else {
- TEST(ltp_syscall(__NR_swapon,
- testcase[i].path, 0));
- }
- /* do the clean if the test have one */
- if (testcase[i].cleanfunc
- && testcase[i].cleanfunc() == -1) {
- tst_brkm(TBROK, cleanup,
- "Cleanup failed, quitting the test");
- }
- /* check return code */
- if ((TEST_RETURN == -1)
- && (TEST_ERRNO == testcase[i].exp_errno)) {
- tst_resm(TPASS,
- "swapon(2) expected failure;"
- " Got errno - %s : %s",
- testcase[i].exp_errval,
- testcase[i].err_desc);
- } else {
- tst_resm(TFAIL, "swapon(2) failed to produce"
- " expected error: %d, errno"
- ": %s and got %d. "
- " System reboot after"
- " execution of LTP"
- " test suite is"
- " recommended.",
- testcase[i].exp_errno,
- testcase[i].exp_errval, TEST_ERRNO);
- /*If swapfile is turned on, turn it off */
- if (TEST_RETURN == 0) {
- if (ltp_syscall
- (__NR_swapoff,
- testcase[i].path) != 0) {
- tst_resm(TWARN,
- "Failed to"
- " turn off swapfile"
- " swapfile. System"
- " reboot after"
- " execution of LTP"
- " test suite is"
- " recommended.");
- }
- }
- }
- TEST_ERROR_LOG(TEST_ERRNO);
- }
+ for (i = 0; i < TST_TOTAL; i++)
+ verify_swapon(testcases + i);
}
cleanup();
tst_exit();
}
-/*
- * setup01() - This function creates the file and sets the user as nobody
- */
-static int setup01(void)
+static void setup01(void)
{
- make_swapfile(cleanup, "swapfile01");
-
- if ((ltpuser = getpwnam(nobody_uid)) == NULL) {
- tst_resm(TWARN, "\"nobody\" user not present. skipping test");
- return -1;
- }
-
- if (seteuid(ltpuser->pw_uid) == -1) {
- tst_resm(TWARN, "seteuid failed to "
- "to set the effective uid to %d", ltpuser->pw_uid);
- perror("seteuid");
- return -1;
- }
- return 0;
+ SAFE_SETEUID(cleanup, nobody_uid);
}
-/*
- * cleanup01() - switch back to user root and gives swapoff to the swap file
- */
-static int cleanup01(void)
+static void cleanup01(void)
{
- if (seteuid(0) == -1) {
- tst_brkm(TBROK | TERRNO, cleanup,
- "seteuid failed to set uid to root");
- }
-
- return 0;
-}
-
-/*
- * setup02() - create a normal file, to be used with swapon
- */
-static int setup02(void)
-{
- int fd;
- fd = creat("nofile", S_IRWXU);
- if (fd == -1)
- tst_resm(TWARN, "Failed to create temporary file");
- close(fd);
- return 0;
-}
-
-/*
- * setup03() - This function creates the swap file and turn it on
- */
-static int setup03(void)
-{
- int res = 0;
-
- make_swapfile(cleanup, "alreadyused");
-
- /* turn on the swap file */
- res = ltp_syscall(__NR_swapon, "alreadyused", 0);
- if (res != 0) {
- tst_resm(TWARN, "Failed swapon for file alreadyused"
- " returned %d", res);
- return -1;
- }
-
- return 0;
-}
-
-/*
- * cleanup03() - clearing the turned on swap file
- */
-static int cleanup03(void)
-{
- /* give swapoff to the test swap file */
- if (ltp_syscall(__NR_swapoff, "alreadyused") != 0) {
- tst_resm(TWARN, "Failed to turn off swap files. system"
- " reboot after execution of LTP test"
- " suite is recommended");
- return -1;
- }
-
- return 0;
+ SAFE_SETEUID(cleanup, 0);
}
static void setup(void)
{
long type;
+ struct passwd *nobody;
tst_sig(FORK, DEF_HANDLER, cleanup);
@@ -239,6 +129,9 @@
tst_require_root(NULL);
+ nobody = SAFE_GETPWNAM(cleanup, "nobody");
+ nobody_uid = nobody->pw_uid;
+
tst_tmpdir();
switch ((type = tst_fs_type(cleanup, "."))) {
@@ -250,6 +143,15 @@
break;
}
+ SAFE_TOUCH(cleanup, "notswap", 0777, NULL);
+ make_swapfile(cleanup, "swapfile01");
+ make_swapfile(cleanup, "alreadyused");
+
+ if (ltp_syscall(__NR_swapon, "alreadyused", 0))
+ tst_resm(TWARN | TERRNO, "swapon(alreadyused) failed");
+ else
+ do_swapoff = 1;
+
TEST_PAUSE;
}
@@ -257,5 +159,8 @@
{
TEST_CLEANUP;
+ if (do_swapoff && ltp_syscall(__NR_swapoff, "alreadyused"))
+ tst_resm(TWARN | TERRNO, "swapoff(alreadyused) failed");
+
tst_rmdir();
}