1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 2f08c3bdfSopenharmony_ci 3f08c3bdfSopenharmony_ci/* 4f08c3bdfSopenharmony_ci * Copyright (c) Wipro Technologies Ltd, 2002. All Rights Reserved. 5f08c3bdfSopenharmony_ci */ 6f08c3bdfSopenharmony_ci 7f08c3bdfSopenharmony_ci/*\ 8f08c3bdfSopenharmony_ci * [Description] 9f08c3bdfSopenharmony_ci * 10f08c3bdfSopenharmony_ci * This test case checks whether swapon(2) system call returns 11f08c3bdfSopenharmony_ci * 1. ENOENT when the path does not exist 12f08c3bdfSopenharmony_ci * 2. EINVAL when the path exists but is invalid 13f08c3bdfSopenharmony_ci * 3. EPERM when user is not a superuser 14f08c3bdfSopenharmony_ci * 4. EBUSY when the specified path is already being used as a swap area 15f08c3bdfSopenharmony_ci */ 16f08c3bdfSopenharmony_ci 17f08c3bdfSopenharmony_ci#include <errno.h> 18f08c3bdfSopenharmony_ci#include <pwd.h> 19f08c3bdfSopenharmony_ci 20f08c3bdfSopenharmony_ci#include "tst_test.h" 21f08c3bdfSopenharmony_ci#include "lapi/syscalls.h" 22f08c3bdfSopenharmony_ci#include "libswap.h" 23f08c3bdfSopenharmony_ci 24f08c3bdfSopenharmony_cistatic void setup01(void); 25f08c3bdfSopenharmony_cistatic void cleanup01(void); 26f08c3bdfSopenharmony_ci 27f08c3bdfSopenharmony_cistatic uid_t nobody_uid; 28f08c3bdfSopenharmony_cistatic int do_swapoff; 29f08c3bdfSopenharmony_ci 30f08c3bdfSopenharmony_cistatic struct tcase { 31f08c3bdfSopenharmony_ci char *err_desc; 32f08c3bdfSopenharmony_ci int exp_errno; 33f08c3bdfSopenharmony_ci char *exp_errval; 34f08c3bdfSopenharmony_ci char *path; 35f08c3bdfSopenharmony_ci void (*setup)(void); 36f08c3bdfSopenharmony_ci void (*cleanup)(void); 37f08c3bdfSopenharmony_ci} tcases[] = { 38f08c3bdfSopenharmony_ci {"Path does not exist", ENOENT, "ENOENT", "./doesnotexist", NULL, NULL}, 39f08c3bdfSopenharmony_ci {"Invalid path", EINVAL, "EINVAL", "./notswap", NULL, NULL}, 40f08c3bdfSopenharmony_ci {"Permission denied", EPERM, "EPERM", "./swapfile01", setup01, cleanup01}, 41f08c3bdfSopenharmony_ci {"File already used", EBUSY, "EBUSY", "./alreadyused", NULL, NULL}, 42f08c3bdfSopenharmony_ci}; 43f08c3bdfSopenharmony_ci 44f08c3bdfSopenharmony_cistatic void setup01(void) 45f08c3bdfSopenharmony_ci{ 46f08c3bdfSopenharmony_ci SAFE_SETEUID(nobody_uid); 47f08c3bdfSopenharmony_ci} 48f08c3bdfSopenharmony_ci 49f08c3bdfSopenharmony_cistatic void cleanup01(void) 50f08c3bdfSopenharmony_ci{ 51f08c3bdfSopenharmony_ci SAFE_SETEUID(0); 52f08c3bdfSopenharmony_ci} 53f08c3bdfSopenharmony_ci 54f08c3bdfSopenharmony_cistatic void setup(void) 55f08c3bdfSopenharmony_ci{ 56f08c3bdfSopenharmony_ci struct passwd *nobody; 57f08c3bdfSopenharmony_ci 58f08c3bdfSopenharmony_ci nobody = SAFE_GETPWNAM("nobody"); 59f08c3bdfSopenharmony_ci nobody_uid = nobody->pw_uid; 60f08c3bdfSopenharmony_ci 61f08c3bdfSopenharmony_ci is_swap_supported("./tstswap"); 62f08c3bdfSopenharmony_ci 63f08c3bdfSopenharmony_ci SAFE_TOUCH("notswap", 0777, NULL); 64f08c3bdfSopenharmony_ci make_swapfile("swapfile01", 0); 65f08c3bdfSopenharmony_ci make_swapfile("alreadyused", 0); 66f08c3bdfSopenharmony_ci 67f08c3bdfSopenharmony_ci if (tst_syscall(__NR_swapon, "alreadyused", 0)) 68f08c3bdfSopenharmony_ci tst_res(TWARN | TERRNO, "swapon(alreadyused) failed"); 69f08c3bdfSopenharmony_ci else 70f08c3bdfSopenharmony_ci do_swapoff = 1; 71f08c3bdfSopenharmony_ci} 72f08c3bdfSopenharmony_ci 73f08c3bdfSopenharmony_civoid cleanup(void) 74f08c3bdfSopenharmony_ci{ 75f08c3bdfSopenharmony_ci if (do_swapoff && tst_syscall(__NR_swapoff, "alreadyused")) 76f08c3bdfSopenharmony_ci tst_res(TWARN | TERRNO, "swapoff(alreadyused) failed"); 77f08c3bdfSopenharmony_ci} 78f08c3bdfSopenharmony_ci 79f08c3bdfSopenharmony_cistatic void verify_swapon(unsigned int i) 80f08c3bdfSopenharmony_ci{ 81f08c3bdfSopenharmony_ci struct tcase *tc = tcases + i; 82f08c3bdfSopenharmony_ci if (tc->setup) 83f08c3bdfSopenharmony_ci tc->setup(); 84f08c3bdfSopenharmony_ci 85f08c3bdfSopenharmony_ci TEST(tst_syscall(__NR_swapon, tc->path, 0)); 86f08c3bdfSopenharmony_ci 87f08c3bdfSopenharmony_ci if (tc->cleanup) 88f08c3bdfSopenharmony_ci tc->cleanup(); 89f08c3bdfSopenharmony_ci 90f08c3bdfSopenharmony_ci if (TST_RET == -1 && TST_ERR == tc->exp_errno) { 91f08c3bdfSopenharmony_ci tst_res(TPASS, "swapon(2) expected failure;" 92f08c3bdfSopenharmony_ci " Got errno - %s : %s", 93f08c3bdfSopenharmony_ci tc->exp_errval, tc->err_desc); 94f08c3bdfSopenharmony_ci return; 95f08c3bdfSopenharmony_ci } 96f08c3bdfSopenharmony_ci 97f08c3bdfSopenharmony_ci tst_res(TFAIL, "swapon(2) failed to produce expected error:" 98f08c3bdfSopenharmony_ci " %d, errno: %s and got %d.", tc->exp_errno, 99f08c3bdfSopenharmony_ci tc->exp_errval, TST_ERR); 100f08c3bdfSopenharmony_ci} 101f08c3bdfSopenharmony_ci 102f08c3bdfSopenharmony_cistatic struct tst_test test = { 103f08c3bdfSopenharmony_ci .needs_root = 1, 104f08c3bdfSopenharmony_ci .needs_tmpdir = 1, 105f08c3bdfSopenharmony_ci .test = verify_swapon, 106f08c3bdfSopenharmony_ci .tcnt = ARRAY_SIZE(tcases), 107f08c3bdfSopenharmony_ci .setup = setup, 108f08c3bdfSopenharmony_ci .cleanup = cleanup 109f08c3bdfSopenharmony_ci}; 110