1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 2f08c3bdfSopenharmony_ci/* 3f08c3bdfSopenharmony_ci * Copyright (c) International Business Machines Corp., 2009 4f08c3bdfSopenharmony_ci * Veerendra C <vechandr@in.ibm.com> 5f08c3bdfSopenharmony_ci * Copyright (C) 2022 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com> 6f08c3bdfSopenharmony_ci */ 7f08c3bdfSopenharmony_ci 8f08c3bdfSopenharmony_ci/*\ 9f08c3bdfSopenharmony_ci * [Description] 10f08c3bdfSopenharmony_ci * 11f08c3bdfSopenharmony_ci * Test if SysV IPC shared memory is properly used between two namespaces. 12f08c3bdfSopenharmony_ci * 13f08c3bdfSopenharmony_ci * [Algorithm] 14f08c3bdfSopenharmony_ci * 15f08c3bdfSopenharmony_ci * Create two 'containers'. 16f08c3bdfSopenharmony_ci * In container1, create Shared Memory segment with a specific key. 17f08c3bdfSopenharmony_ci * In container2, try to access the MQ created in container1. 18f08c3bdfSopenharmony_ci * 19f08c3bdfSopenharmony_ci * Test is PASS if flag = none and the shmem seg is accessible in container2. 20f08c3bdfSopenharmony_ci * If flag = unshare/clone and the shmem seg is not accessible in container2. 21f08c3bdfSopenharmony_ci * If shmem seg is not accessible in container2, creates new shmem with same 22f08c3bdfSopenharmony_ci * key to double check isloation in IPCNS. 23f08c3bdfSopenharmony_ci * 24f08c3bdfSopenharmony_ci * Test is FAIL if flag = none and the shmem seg is not accessible, if 25f08c3bdfSopenharmony_ci * flag = unshare/clone and shmem seg is accessible in container2 or if the new 26f08c3bdfSopenharmony_ci * shmem seg creation Fails. 27f08c3bdfSopenharmony_ci */ 28f08c3bdfSopenharmony_ci 29f08c3bdfSopenharmony_ci#define _GNU_SOURCE 30f08c3bdfSopenharmony_ci 31f08c3bdfSopenharmony_ci#include <sys/wait.h> 32f08c3bdfSopenharmony_ci#include <sys/msg.h> 33f08c3bdfSopenharmony_ci#include <sys/types.h> 34f08c3bdfSopenharmony_ci#include "tst_safe_sysv_ipc.h" 35f08c3bdfSopenharmony_ci#include "tst_test.h" 36f08c3bdfSopenharmony_ci#include "common.h" 37f08c3bdfSopenharmony_ci 38f08c3bdfSopenharmony_ci#define TESTKEY 124426L 39f08c3bdfSopenharmony_ci 40f08c3bdfSopenharmony_cistatic char *str_op; 41f08c3bdfSopenharmony_cistatic int use_clone; 42f08c3bdfSopenharmony_ci 43f08c3bdfSopenharmony_cistatic void check_shmem1(void) 44f08c3bdfSopenharmony_ci{ 45f08c3bdfSopenharmony_ci int id; 46f08c3bdfSopenharmony_ci 47f08c3bdfSopenharmony_ci id = SAFE_SHMGET(TESTKEY, 100, IPC_CREAT); 48f08c3bdfSopenharmony_ci 49f08c3bdfSopenharmony_ci tst_res(TINFO, "container1: able to create shared mem segment"); 50f08c3bdfSopenharmony_ci 51f08c3bdfSopenharmony_ci TST_CHECKPOINT_WAKE_AND_WAIT(0); 52f08c3bdfSopenharmony_ci 53f08c3bdfSopenharmony_ci SAFE_SHMCTL(id, IPC_RMID, NULL); 54f08c3bdfSopenharmony_ci} 55f08c3bdfSopenharmony_ci 56f08c3bdfSopenharmony_cistatic void check_shmem2(void) 57f08c3bdfSopenharmony_ci{ 58f08c3bdfSopenharmony_ci TST_CHECKPOINT_WAIT(0); 59f08c3bdfSopenharmony_ci 60f08c3bdfSopenharmony_ci TEST(shmget(TESTKEY, 100, 0)); 61f08c3bdfSopenharmony_ci 62f08c3bdfSopenharmony_ci if (TST_RET < 0) { 63f08c3bdfSopenharmony_ci if (use_clone == T_NONE) 64f08c3bdfSopenharmony_ci tst_res(TFAIL, "Plain cloned process didn't find shmem segment"); 65f08c3bdfSopenharmony_ci else 66f08c3bdfSopenharmony_ci tst_res(TPASS, "%s: in namespace2 unable to access the shmem seg created in namespace1", str_op); 67f08c3bdfSopenharmony_ci } else { 68f08c3bdfSopenharmony_ci if (use_clone == T_NONE) 69f08c3bdfSopenharmony_ci tst_res(TPASS, "Plain cloned process able to access shmem segment created"); 70f08c3bdfSopenharmony_ci else 71f08c3bdfSopenharmony_ci tst_res(TFAIL, "%s: in namespace2 found the shmem segment created in namespace1", str_op); 72f08c3bdfSopenharmony_ci } 73f08c3bdfSopenharmony_ci 74f08c3bdfSopenharmony_ci TST_CHECKPOINT_WAKE(0); 75f08c3bdfSopenharmony_ci} 76f08c3bdfSopenharmony_ci 77f08c3bdfSopenharmony_cistatic void run(void) 78f08c3bdfSopenharmony_ci{ 79f08c3bdfSopenharmony_ci clone_unshare_test(use_clone, CLONE_NEWIPC, check_shmem1); 80f08c3bdfSopenharmony_ci clone_unshare_test(use_clone, CLONE_NEWIPC, check_shmem2); 81f08c3bdfSopenharmony_ci} 82f08c3bdfSopenharmony_ci 83f08c3bdfSopenharmony_cistatic void setup(void) 84f08c3bdfSopenharmony_ci{ 85f08c3bdfSopenharmony_ci use_clone = get_clone_unshare_enum(str_op); 86f08c3bdfSopenharmony_ci} 87f08c3bdfSopenharmony_ci 88f08c3bdfSopenharmony_cistatic struct tst_test test = { 89f08c3bdfSopenharmony_ci .test_all = run, 90f08c3bdfSopenharmony_ci .setup = setup, 91f08c3bdfSopenharmony_ci .forks_child = 1, 92f08c3bdfSopenharmony_ci .needs_root = 1, 93f08c3bdfSopenharmony_ci .needs_checkpoints = 1, 94f08c3bdfSopenharmony_ci .options = (struct tst_option[]) { 95f08c3bdfSopenharmony_ci { "m:", &str_op, "Test execution mode <clone|unshare|none>" }, 96f08c3bdfSopenharmony_ci {}, 97f08c3bdfSopenharmony_ci }, 98f08c3bdfSopenharmony_ci}; 99