1f08c3bdfSopenharmony_ci/* 2f08c3bdfSopenharmony_ci * Copyright (c) Wipro Technologies Ltd, 2002. All Rights Reserved. 3f08c3bdfSopenharmony_ci * 4f08c3bdfSopenharmony_ci * This program is free software; you can redistribute it and/or modify it 5f08c3bdfSopenharmony_ci * under the terms of version 2 of the GNU General Public License as 6f08c3bdfSopenharmony_ci * published by the Free Software Foundation. 7f08c3bdfSopenharmony_ci * 8f08c3bdfSopenharmony_ci * This program is distributed in the hope that it would be useful, but 9f08c3bdfSopenharmony_ci * WITHOUT ANY WARRANTY; without even the implied warranty of 10f08c3bdfSopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 11f08c3bdfSopenharmony_ci * 12f08c3bdfSopenharmony_ci * You should have received a copy of the GNU General Public License along 13f08c3bdfSopenharmony_ci * with this program; if not, write the Free Software Foundation, Inc., 14f08c3bdfSopenharmony_ci * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 15f08c3bdfSopenharmony_ci * 16f08c3bdfSopenharmony_ci */ 17f08c3bdfSopenharmony_ci/************************************************************************** 18f08c3bdfSopenharmony_ci * 19f08c3bdfSopenharmony_ci * TEST IDENTIFIER : mlockall02 20f08c3bdfSopenharmony_ci * 21f08c3bdfSopenharmony_ci * EXECUTED BY : root / superuser 22f08c3bdfSopenharmony_ci * 23f08c3bdfSopenharmony_ci * TEST TITLE : Test for checking basic error conditions for 24f08c3bdfSopenharmony_ci * mlockall(2) 25f08c3bdfSopenharmony_ci * 26f08c3bdfSopenharmony_ci * TEST CASE TOTAL : 3 27f08c3bdfSopenharmony_ci * 28f08c3bdfSopenharmony_ci * AUTHOR : Nirmala Devi Dhanasekar <nirmala.devi@wipro.com> 29f08c3bdfSopenharmony_ci * 30f08c3bdfSopenharmony_ci * SIGNALS 31f08c3bdfSopenharmony_ci * Uses SIGUSR1 to pause before test if option set. 32f08c3bdfSopenharmony_ci * (See the parse_opts(3) man page). 33f08c3bdfSopenharmony_ci * 34f08c3bdfSopenharmony_ci * DESCRIPTION 35f08c3bdfSopenharmony_ci * Check for basic errors returned by mount(2) system call. 36f08c3bdfSopenharmony_ci *$ 37f08c3bdfSopenharmony_ci * Verify that mount(2) returns -1 and sets errno to 38f08c3bdfSopenharmony_ci * 39f08c3bdfSopenharmony_ci * 1) ENOMEM - If process exceed maximum number of locked pages. 40f08c3bdfSopenharmony_ci * 2) EPERM - If not super user 41f08c3bdfSopenharmony_ci * 3) EINVAL - Unknown flags were specified. 42f08c3bdfSopenharmony_ci * 43f08c3bdfSopenharmony_ci * Setup: 44f08c3bdfSopenharmony_ci * Setup signal handling. 45f08c3bdfSopenharmony_ci * Pause for SIGUSR1 if option specified. 46f08c3bdfSopenharmony_ci * 47f08c3bdfSopenharmony_ci * Test: 48f08c3bdfSopenharmony_ci * Loop if the proper options are given. 49f08c3bdfSopenharmony_ci * Do necessary setup for each test. 50f08c3bdfSopenharmony_ci * Execute system call 51f08c3bdfSopenharmony_ci * Check return code, if system call failed and errno == expected errno 52f08c3bdfSopenharmony_ci * Issue sys call passed with expected return value and errno. 53f08c3bdfSopenharmony_ci * Otherwise, 54f08c3bdfSopenharmony_ci * Issue sys call failed to produce expected error. 55f08c3bdfSopenharmony_ci * Do cleanup for each test. 56f08c3bdfSopenharmony_ci * 57f08c3bdfSopenharmony_ci * Cleanup: 58f08c3bdfSopenharmony_ci * Print errno log and/or timing stats if options given 59f08c3bdfSopenharmony_ci * 60f08c3bdfSopenharmony_ci * USAGE: <for command-line> 61f08c3bdfSopenharmony_ci * mlockall02 [-c n] [-e] [-i n] [-I x] [-p x] [-t] 62f08c3bdfSopenharmony_ci * where, 63f08c3bdfSopenharmony_ci * -c n : Run n copies concurrently 64f08c3bdfSopenharmony_ci * -e : Turn on errno logging. 65f08c3bdfSopenharmony_ci * -h : Show this help screen 66f08c3bdfSopenharmony_ci * -i n : Execute test n times. 67f08c3bdfSopenharmony_ci * -I x : Execute test for x seconds. 68f08c3bdfSopenharmony_ci * -p : Pause for SIGUSR1 before starting 69f08c3bdfSopenharmony_ci * -P x : Pause for x seconds between iterations. 70f08c3bdfSopenharmony_ci * -t : Turn on syscall timing. 71f08c3bdfSopenharmony_ci * 72f08c3bdfSopenharmony_ci * RESTRICTIONS 73f08c3bdfSopenharmony_ci * Test must run as root. 74f08c3bdfSopenharmony_ci *****************************************************************************/ 75f08c3bdfSopenharmony_ci#include <errno.h> 76f08c3bdfSopenharmony_ci#include <unistd.h> 77f08c3bdfSopenharmony_ci#include <pwd.h> 78f08c3bdfSopenharmony_ci#include <sys/mman.h> 79f08c3bdfSopenharmony_ci#include "test.h" 80f08c3bdfSopenharmony_ci#include "safe_macros.h" 81f08c3bdfSopenharmony_ci#include <sys/resource.h> 82f08c3bdfSopenharmony_ci 83f08c3bdfSopenharmony_civoid setup(); 84f08c3bdfSopenharmony_ciint setup_test(int); 85f08c3bdfSopenharmony_civoid cleanup_test(int); 86f08c3bdfSopenharmony_civoid cleanup(); 87f08c3bdfSopenharmony_ci 88f08c3bdfSopenharmony_cichar *TCID = "mlockall02"; 89f08c3bdfSopenharmony_ciint TST_TOTAL = 3; 90f08c3bdfSopenharmony_ci 91f08c3bdfSopenharmony_cistruct test_case_t { 92f08c3bdfSopenharmony_ci int flag; /* flag value */ 93f08c3bdfSopenharmony_ci int error; /* error description */ 94f08c3bdfSopenharmony_ci char *edesc; /* Expected error no */ 95f08c3bdfSopenharmony_ci} TC[] = { 96f08c3bdfSopenharmony_ci { 97f08c3bdfSopenharmony_ci MCL_CURRENT, ENOMEM, "Process exceeds max locked pages"}, { 98f08c3bdfSopenharmony_ci MCL_CURRENT, EPERM, "Not a superuser"}, { 99f08c3bdfSopenharmony_ci 0, EINVAL, "Unknown flag"} 100f08c3bdfSopenharmony_ci}; 101f08c3bdfSopenharmony_ci 102f08c3bdfSopenharmony_ci#if !defined(UCLINUX) 103f08c3bdfSopenharmony_ci 104f08c3bdfSopenharmony_ciint main(int ac, char **av) 105f08c3bdfSopenharmony_ci{ 106f08c3bdfSopenharmony_ci int lc, i; 107f08c3bdfSopenharmony_ci 108f08c3bdfSopenharmony_ci tst_parse_opts(ac, av, NULL, NULL); 109f08c3bdfSopenharmony_ci 110f08c3bdfSopenharmony_ci setup(); 111f08c3bdfSopenharmony_ci 112f08c3bdfSopenharmony_ci /* check looping state */ 113f08c3bdfSopenharmony_ci for (lc = 0; TEST_LOOPING(lc); lc++) { 114f08c3bdfSopenharmony_ci 115f08c3bdfSopenharmony_ci tst_count = 0; 116f08c3bdfSopenharmony_ci 117f08c3bdfSopenharmony_ci for (i = 0; i < TST_TOTAL; i++) { 118f08c3bdfSopenharmony_ci 119f08c3bdfSopenharmony_ci if (setup_test(i)) { 120f08c3bdfSopenharmony_ci tst_resm(TFAIL, "mlockall() Failed while setup " 121f08c3bdfSopenharmony_ci "for checking error %s", TC[i].edesc); 122f08c3bdfSopenharmony_ci continue; 123f08c3bdfSopenharmony_ci } 124f08c3bdfSopenharmony_ci 125f08c3bdfSopenharmony_ci TEST(mlockall(TC[i].flag)); 126f08c3bdfSopenharmony_ci 127f08c3bdfSopenharmony_ci /* check return code */ 128f08c3bdfSopenharmony_ci if (TEST_RETURN == -1) { 129f08c3bdfSopenharmony_ci if (TEST_ERRNO != TC[i].error) 130f08c3bdfSopenharmony_ci tst_brkm(TFAIL, cleanup, 131f08c3bdfSopenharmony_ci "mlock() Failed with wrong " 132f08c3bdfSopenharmony_ci "errno, expected errno=%s, " 133f08c3bdfSopenharmony_ci "got errno=%d : %s", 134f08c3bdfSopenharmony_ci TC[i].edesc, TEST_ERRNO, 135f08c3bdfSopenharmony_ci strerror(TEST_ERRNO)); 136f08c3bdfSopenharmony_ci else 137f08c3bdfSopenharmony_ci tst_resm(TPASS, 138f08c3bdfSopenharmony_ci "expected failure - errno " 139f08c3bdfSopenharmony_ci "= %d : %s", 140f08c3bdfSopenharmony_ci TEST_ERRNO, 141f08c3bdfSopenharmony_ci strerror(TEST_ERRNO)); 142f08c3bdfSopenharmony_ci } else { 143f08c3bdfSopenharmony_ci if (i <= 1) 144f08c3bdfSopenharmony_ci tst_resm(TCONF, 145f08c3bdfSopenharmony_ci "mlockall02 did not BEHAVE as expected."); 146f08c3bdfSopenharmony_ci else 147f08c3bdfSopenharmony_ci tst_brkm(TFAIL, cleanup, 148f08c3bdfSopenharmony_ci "mlock() Failed, expected " 149f08c3bdfSopenharmony_ci "return value=-1, got %ld", 150f08c3bdfSopenharmony_ci TEST_RETURN); 151f08c3bdfSopenharmony_ci } 152f08c3bdfSopenharmony_ci cleanup_test(i); 153f08c3bdfSopenharmony_ci } 154f08c3bdfSopenharmony_ci } 155f08c3bdfSopenharmony_ci 156f08c3bdfSopenharmony_ci /* cleanup and exit */ 157f08c3bdfSopenharmony_ci cleanup(); 158f08c3bdfSopenharmony_ci 159f08c3bdfSopenharmony_ci tst_exit(); 160f08c3bdfSopenharmony_ci} 161f08c3bdfSopenharmony_ci 162f08c3bdfSopenharmony_ci/* 163f08c3bdfSopenharmony_ci * setup() - performs all ONE TIME setup for this test. 164f08c3bdfSopenharmony_ci */ 165f08c3bdfSopenharmony_civoid setup(void) 166f08c3bdfSopenharmony_ci{ 167f08c3bdfSopenharmony_ci 168f08c3bdfSopenharmony_ci tst_require_root(); 169f08c3bdfSopenharmony_ci 170f08c3bdfSopenharmony_ci tst_sig(FORK, DEF_HANDLER, cleanup); 171f08c3bdfSopenharmony_ci 172f08c3bdfSopenharmony_ci TEST_PAUSE; 173f08c3bdfSopenharmony_ci 174f08c3bdfSopenharmony_ci return; 175f08c3bdfSopenharmony_ci} 176f08c3bdfSopenharmony_ci 177f08c3bdfSopenharmony_ciint setup_test(int i) 178f08c3bdfSopenharmony_ci{ 179f08c3bdfSopenharmony_ci struct rlimit rl; 180f08c3bdfSopenharmony_ci char nobody_uid[] = "nobody"; 181f08c3bdfSopenharmony_ci struct passwd *ltpuser; 182f08c3bdfSopenharmony_ci 183f08c3bdfSopenharmony_ci switch (i) { 184f08c3bdfSopenharmony_ci case 0: 185f08c3bdfSopenharmony_ci rl.rlim_max = 10; 186f08c3bdfSopenharmony_ci rl.rlim_cur = 7; 187f08c3bdfSopenharmony_ci 188f08c3bdfSopenharmony_ci if (setrlimit(RLIMIT_MEMLOCK, &rl) != 0) { 189f08c3bdfSopenharmony_ci tst_resm(TWARN, "setrlimit failed to set the " 190f08c3bdfSopenharmony_ci "resource for RLIMIT_MEMLOCK to check " 191f08c3bdfSopenharmony_ci "for mlockall error %s\n", TC[i].edesc); 192f08c3bdfSopenharmony_ci return 1; 193f08c3bdfSopenharmony_ci } 194f08c3bdfSopenharmony_ci ltpuser = getpwnam(nobody_uid); 195f08c3bdfSopenharmony_ci if (seteuid(ltpuser->pw_uid) == -1) { 196f08c3bdfSopenharmony_ci tst_brkm(TBROK, cleanup, "seteuid() " 197f08c3bdfSopenharmony_ci "failed to change euid to %d " 198f08c3bdfSopenharmony_ci "errno = %d : %s", 199f08c3bdfSopenharmony_ci ltpuser->pw_uid, TEST_ERRNO, 200f08c3bdfSopenharmony_ci strerror(TEST_ERRNO)); 201f08c3bdfSopenharmony_ci return 1; 202f08c3bdfSopenharmony_ci } 203f08c3bdfSopenharmony_ci return 0; 204f08c3bdfSopenharmony_ci case 1: 205f08c3bdfSopenharmony_ci rl.rlim_max = 0; 206f08c3bdfSopenharmony_ci rl.rlim_cur = 0; 207f08c3bdfSopenharmony_ci if (setrlimit(RLIMIT_MEMLOCK, &rl) != 0) { 208f08c3bdfSopenharmony_ci tst_resm(TWARN, "setrlimit failed to " 209f08c3bdfSopenharmony_ci "set the resource for " 210f08c3bdfSopenharmony_ci "RLIMIT_MEMLOCK to check for " 211f08c3bdfSopenharmony_ci "mlockall error %s\n", TC[i].edesc); 212f08c3bdfSopenharmony_ci return 1; 213f08c3bdfSopenharmony_ci } 214f08c3bdfSopenharmony_ci ltpuser = getpwnam(nobody_uid); 215f08c3bdfSopenharmony_ci if (seteuid(ltpuser->pw_uid) == -1) { 216f08c3bdfSopenharmony_ci tst_brkm(TBROK, cleanup, "seteuid() failed to " 217f08c3bdfSopenharmony_ci "change euid to %d errno = %d : %s", 218f08c3bdfSopenharmony_ci ltpuser->pw_uid, TEST_ERRNO, 219f08c3bdfSopenharmony_ci strerror(TEST_ERRNO)); 220f08c3bdfSopenharmony_ci return 1; 221f08c3bdfSopenharmony_ci } 222f08c3bdfSopenharmony_ci return 0; 223f08c3bdfSopenharmony_ci } 224f08c3bdfSopenharmony_ci return 0; 225f08c3bdfSopenharmony_ci} 226f08c3bdfSopenharmony_ci 227f08c3bdfSopenharmony_civoid cleanup_test(int i) 228f08c3bdfSopenharmony_ci{ 229f08c3bdfSopenharmony_ci struct rlimit rl; 230f08c3bdfSopenharmony_ci 231f08c3bdfSopenharmony_ci switch (i) { 232f08c3bdfSopenharmony_ci case 0: 233f08c3bdfSopenharmony_ci seteuid(0); 234f08c3bdfSopenharmony_ci 235f08c3bdfSopenharmony_ci rl.rlim_max = -1; 236f08c3bdfSopenharmony_ci rl.rlim_cur = -1; 237f08c3bdfSopenharmony_ci 238f08c3bdfSopenharmony_ci if (setrlimit(RLIMIT_MEMLOCK, &rl) != 0) { 239f08c3bdfSopenharmony_ci tst_brkm(TFAIL, cleanup, 240f08c3bdfSopenharmony_ci "setrlimit failed to reset the " 241f08c3bdfSopenharmony_ci "resource for RLIMIT_MEMLOCK while " 242f08c3bdfSopenharmony_ci "checking for mlockall error %s\n", 243f08c3bdfSopenharmony_ci TC[i].edesc); 244f08c3bdfSopenharmony_ci } 245f08c3bdfSopenharmony_ci return; 246f08c3bdfSopenharmony_ci case 1: 247f08c3bdfSopenharmony_ci SAFE_SETEUID(cleanup, 0); 248f08c3bdfSopenharmony_ci return; 249f08c3bdfSopenharmony_ci 250f08c3bdfSopenharmony_ci } 251f08c3bdfSopenharmony_ci} 252f08c3bdfSopenharmony_ci 253f08c3bdfSopenharmony_ci/* 254f08c3bdfSopenharmony_ci * cleanup() - performs all ONE TIME cleanup for this test at 255f08c3bdfSopenharmony_ci * completion or premature exit. 256f08c3bdfSopenharmony_ci */ 257f08c3bdfSopenharmony_civoid cleanup(void) 258f08c3bdfSopenharmony_ci{ 259f08c3bdfSopenharmony_ci return; 260f08c3bdfSopenharmony_ci} 261f08c3bdfSopenharmony_ci 262f08c3bdfSopenharmony_ci#else 263f08c3bdfSopenharmony_ci 264f08c3bdfSopenharmony_ciint main(void) 265f08c3bdfSopenharmony_ci{ 266f08c3bdfSopenharmony_ci tst_resm(TINFO, "test is not available on uClinux"); 267f08c3bdfSopenharmony_ci tst_exit(); 268f08c3bdfSopenharmony_ci} 269f08c3bdfSopenharmony_ci 270f08c3bdfSopenharmony_ci#endif /* if !defined(UCLINUX) */ 271