1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 2f08c3bdfSopenharmony_ci/* 3f08c3bdfSopenharmony_ci * Copyright (c) 2015 Cedric Hnyda <chnyda@suse.com> 4f08c3bdfSopenharmony_ci */ 5f08c3bdfSopenharmony_ci 6f08c3bdfSopenharmony_ci /* Description: 7f08c3bdfSopenharmony_ci * Verify that: 8f08c3bdfSopenharmony_ci * 1) kcmp returns 0 with two process and two fd refering to the 9f08c3bdfSopenharmony_ci * same open file 10f08c3bdfSopenharmony_ci * 2) kcmp doesn't return 0 with two process and two fd not 11f08c3bdfSopenharmony_ci * refering to the same open file 12f08c3bdfSopenharmony_ci */ 13f08c3bdfSopenharmony_ci 14f08c3bdfSopenharmony_ci#define _GNU_SOURCE 15f08c3bdfSopenharmony_ci 16f08c3bdfSopenharmony_ci#include "tst_test.h" 17f08c3bdfSopenharmony_ci#include "lapi/fcntl.h" 18f08c3bdfSopenharmony_ci#include "lapi/kcmp.h" 19f08c3bdfSopenharmony_ci 20f08c3bdfSopenharmony_ci#define TEST_FILE "test_file" 21f08c3bdfSopenharmony_ci#define TEST_FILE2 "test_file2" 22f08c3bdfSopenharmony_ci 23f08c3bdfSopenharmony_cistatic int fd1; 24f08c3bdfSopenharmony_cistatic int fd2; 25f08c3bdfSopenharmony_cistatic int fd3; 26f08c3bdfSopenharmony_cistatic int pid1; 27f08c3bdfSopenharmony_cistatic int pid2; 28f08c3bdfSopenharmony_ci 29f08c3bdfSopenharmony_cistatic struct test_case { 30f08c3bdfSopenharmony_ci int *pid1; 31f08c3bdfSopenharmony_ci int *pid2; 32f08c3bdfSopenharmony_ci int type; 33f08c3bdfSopenharmony_ci int *fd1; 34f08c3bdfSopenharmony_ci int *fd2; 35f08c3bdfSopenharmony_ci int exp_different; 36f08c3bdfSopenharmony_ci} test_cases[] = { 37f08c3bdfSopenharmony_ci {&pid1, &pid1, KCMP_FILE, &fd1, &fd1, 0}, 38f08c3bdfSopenharmony_ci {&pid2, &pid2, KCMP_FILE, &fd1, &fd2, 0}, 39f08c3bdfSopenharmony_ci {&pid1, &pid2, KCMP_FILE, &fd1, &fd1, 0}, 40f08c3bdfSopenharmony_ci {&pid1, &pid2, KCMP_FILE, &fd1, &fd2, 0}, 41f08c3bdfSopenharmony_ci {&pid1, &pid2, KCMP_FILE, &fd1, &fd3, 1}, 42f08c3bdfSopenharmony_ci}; 43f08c3bdfSopenharmony_ci 44f08c3bdfSopenharmony_cistatic void setup(void) 45f08c3bdfSopenharmony_ci{ 46f08c3bdfSopenharmony_ci fd1 = SAFE_OPEN(TEST_FILE, O_CREAT | O_RDWR | O_TRUNC, 0666); 47f08c3bdfSopenharmony_ci} 48f08c3bdfSopenharmony_ci 49f08c3bdfSopenharmony_cistatic void cleanup(void) 50f08c3bdfSopenharmony_ci{ 51f08c3bdfSopenharmony_ci if (fd1 > 0) 52f08c3bdfSopenharmony_ci SAFE_CLOSE(fd1); 53f08c3bdfSopenharmony_ci} 54f08c3bdfSopenharmony_ci 55f08c3bdfSopenharmony_cistatic void do_child(const struct test_case *test) 56f08c3bdfSopenharmony_ci{ 57f08c3bdfSopenharmony_ci pid2 = getpid(); 58f08c3bdfSopenharmony_ci 59f08c3bdfSopenharmony_ci fd3 = SAFE_OPEN(TEST_FILE2, O_CREAT | O_RDWR, 0666); 60f08c3bdfSopenharmony_ci 61f08c3bdfSopenharmony_ci fd2 = dup(fd1); 62f08c3bdfSopenharmony_ci if (fd2 == -1) { 63f08c3bdfSopenharmony_ci tst_res(TFAIL | TERRNO, "dup() failed unexpectedly"); 64f08c3bdfSopenharmony_ci SAFE_CLOSE(fd3); 65f08c3bdfSopenharmony_ci return; 66f08c3bdfSopenharmony_ci } 67f08c3bdfSopenharmony_ci 68f08c3bdfSopenharmony_ci TEST(kcmp(*(test->pid1), *(test->pid2), test->type, 69f08c3bdfSopenharmony_ci *(test->fd1), *(test->fd2))); 70f08c3bdfSopenharmony_ci 71f08c3bdfSopenharmony_ci SAFE_CLOSE(fd2); 72f08c3bdfSopenharmony_ci SAFE_CLOSE(fd3); 73f08c3bdfSopenharmony_ci 74f08c3bdfSopenharmony_ci if (TST_RET == -1) { 75f08c3bdfSopenharmony_ci tst_res(TFAIL | TTERRNO, "kcmp() failed unexpectedly"); 76f08c3bdfSopenharmony_ci return; 77f08c3bdfSopenharmony_ci } 78f08c3bdfSopenharmony_ci 79f08c3bdfSopenharmony_ci if ((test->exp_different && TST_RET == 0) 80f08c3bdfSopenharmony_ci || (test->exp_different == 0 && TST_RET)) { 81f08c3bdfSopenharmony_ci tst_res(TFAIL, "kcmp() returned %lu instead of %d", 82f08c3bdfSopenharmony_ci TST_RET, test->exp_different); 83f08c3bdfSopenharmony_ci return; 84f08c3bdfSopenharmony_ci } 85f08c3bdfSopenharmony_ci 86f08c3bdfSopenharmony_ci tst_res(TPASS, "kcmp() returned the expected value"); 87f08c3bdfSopenharmony_ci} 88f08c3bdfSopenharmony_ci 89f08c3bdfSopenharmony_cistatic void verify_kcmp(unsigned int n) 90f08c3bdfSopenharmony_ci{ 91f08c3bdfSopenharmony_ci struct test_case *tc = &test_cases[n]; 92f08c3bdfSopenharmony_ci 93f08c3bdfSopenharmony_ci pid1 = getpid(); 94f08c3bdfSopenharmony_ci 95f08c3bdfSopenharmony_ci pid2 = SAFE_FORK(); 96f08c3bdfSopenharmony_ci if (!pid2) 97f08c3bdfSopenharmony_ci do_child(tc); 98f08c3bdfSopenharmony_ci} 99f08c3bdfSopenharmony_ci 100f08c3bdfSopenharmony_cistatic struct tst_test test = { 101f08c3bdfSopenharmony_ci .tcnt = ARRAY_SIZE(test_cases), 102f08c3bdfSopenharmony_ci .setup = setup, 103f08c3bdfSopenharmony_ci .cleanup = cleanup, 104f08c3bdfSopenharmony_ci .forks_child = 1, 105f08c3bdfSopenharmony_ci .test = verify_kcmp, 106f08c3bdfSopenharmony_ci .needs_tmpdir = 1, 107f08c3bdfSopenharmony_ci}; 108