1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 2f08c3bdfSopenharmony_ci/* 3f08c3bdfSopenharmony_ci * Copyright (c) 2014 Red Hat, Inc. 4f08c3bdfSopenharmony_ci * Copyright (C) 2022 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com> 5f08c3bdfSopenharmony_ci */ 6f08c3bdfSopenharmony_ci 7f08c3bdfSopenharmony_ci/*\ 8f08c3bdfSopenharmony_ci * [Description] 9f08c3bdfSopenharmony_ci * 10f08c3bdfSopenharmony_ci * Test if SysV IPC shared memory is properly working between two different 11f08c3bdfSopenharmony_ci * namespaces. 12f08c3bdfSopenharmony_ci * 13f08c3bdfSopenharmony_ci * [Algorithm] 14f08c3bdfSopenharmony_ci * 15f08c3bdfSopenharmony_ci * 1. Clones two child processes with CLONE_NEWIPC flag, each child 16f08c3bdfSopenharmony_ci * allocates System V shared memory segment (shm) with the _identical_ 17f08c3bdfSopenharmony_ci * key and attaches that segment into its address space. 18f08c3bdfSopenharmony_ci * 2. Child1 writes into the shared memory segment. 19f08c3bdfSopenharmony_ci * 3. Child2 writes into the shared memory segment. 20f08c3bdfSopenharmony_ci * 4. Writes to the shared memory segment with the identical key but from 21f08c3bdfSopenharmony_ci * two different IPC namespaces should not interfere with each other 22f08c3bdfSopenharmony_ci * and so child1 checks whether its shared segment wasn't changed 23f08c3bdfSopenharmony_ci * by child2, if it wasn't test passes, otherwise test fails. 24f08c3bdfSopenharmony_ci */ 25f08c3bdfSopenharmony_ci 26f08c3bdfSopenharmony_ci#define _GNU_SOURCE 27f08c3bdfSopenharmony_ci 28f08c3bdfSopenharmony_ci#include <sys/wait.h> 29f08c3bdfSopenharmony_ci#include <sys/msg.h> 30f08c3bdfSopenharmony_ci#include <sys/types.h> 31f08c3bdfSopenharmony_ci#include "tst_safe_sysv_ipc.h" 32f08c3bdfSopenharmony_ci#include "tst_test.h" 33f08c3bdfSopenharmony_ci#include "common.h" 34f08c3bdfSopenharmony_ci 35f08c3bdfSopenharmony_ci#define TESTKEY 124426L 36f08c3bdfSopenharmony_ci#define SHMSIZE 50 37f08c3bdfSopenharmony_ci 38f08c3bdfSopenharmony_cistatic void chld1_shm(void) 39f08c3bdfSopenharmony_ci{ 40f08c3bdfSopenharmony_ci int id; 41f08c3bdfSopenharmony_ci char *shmem; 42f08c3bdfSopenharmony_ci 43f08c3bdfSopenharmony_ci id = SAFE_SHMGET(TESTKEY, SHMSIZE, IPC_CREAT); 44f08c3bdfSopenharmony_ci 45f08c3bdfSopenharmony_ci shmem = SAFE_SHMAT(id, NULL, 0); 46f08c3bdfSopenharmony_ci *shmem = 'A'; 47f08c3bdfSopenharmony_ci 48f08c3bdfSopenharmony_ci TST_CHECKPOINT_WAKE_AND_WAIT(0); 49f08c3bdfSopenharmony_ci 50f08c3bdfSopenharmony_ci if (*shmem != 'A') 51f08c3bdfSopenharmony_ci tst_res(TFAIL, "shared memory leak between namespaces"); 52f08c3bdfSopenharmony_ci else 53f08c3bdfSopenharmony_ci tst_res(TPASS, "shared memory didn't leak between namespaces"); 54f08c3bdfSopenharmony_ci 55f08c3bdfSopenharmony_ci TST_CHECKPOINT_WAKE(0); 56f08c3bdfSopenharmony_ci 57f08c3bdfSopenharmony_ci SAFE_SHMDT(shmem); 58f08c3bdfSopenharmony_ci SAFE_SHMCTL(id, IPC_RMID, NULL); 59f08c3bdfSopenharmony_ci} 60f08c3bdfSopenharmony_ci 61f08c3bdfSopenharmony_cistatic void chld2_shm(void) 62f08c3bdfSopenharmony_ci{ 63f08c3bdfSopenharmony_ci int id; 64f08c3bdfSopenharmony_ci char *shmem; 65f08c3bdfSopenharmony_ci 66f08c3bdfSopenharmony_ci id = SAFE_SHMGET(TESTKEY, SHMSIZE, IPC_CREAT); 67f08c3bdfSopenharmony_ci 68f08c3bdfSopenharmony_ci shmem = SAFE_SHMAT(id, NULL, 0); 69f08c3bdfSopenharmony_ci 70f08c3bdfSopenharmony_ci TST_CHECKPOINT_WAIT(0); 71f08c3bdfSopenharmony_ci 72f08c3bdfSopenharmony_ci *shmem = 'B'; 73f08c3bdfSopenharmony_ci 74f08c3bdfSopenharmony_ci TST_CHECKPOINT_WAKE_AND_WAIT(0); 75f08c3bdfSopenharmony_ci 76f08c3bdfSopenharmony_ci SAFE_SHMDT(shmem); 77f08c3bdfSopenharmony_ci SAFE_SHMCTL(id, IPC_RMID, NULL); 78f08c3bdfSopenharmony_ci} 79f08c3bdfSopenharmony_ci 80f08c3bdfSopenharmony_cistatic void run(void) 81f08c3bdfSopenharmony_ci{ 82f08c3bdfSopenharmony_ci clone_unshare_test(T_CLONE, CLONE_NEWIPC, chld1_shm); 83f08c3bdfSopenharmony_ci clone_unshare_test(T_CLONE, CLONE_NEWIPC, chld2_shm); 84f08c3bdfSopenharmony_ci} 85f08c3bdfSopenharmony_ci 86f08c3bdfSopenharmony_cistatic struct tst_test test = { 87f08c3bdfSopenharmony_ci .test_all = run, 88f08c3bdfSopenharmony_ci .needs_root = 1, 89f08c3bdfSopenharmony_ci .needs_checkpoints = 1, 90f08c3bdfSopenharmony_ci .forks_child = 1, 91f08c3bdfSopenharmony_ci}; 92