1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 2f08c3bdfSopenharmony_ci/* 3f08c3bdfSopenharmony_ci * Copyright (c) International Business Machines Corp., 2006 4f08c3bdfSopenharmony_ci * Copyright (c) 2014 Cyril Hrubis <chrubis@suse.cz> 5f08c3bdfSopenharmony_ci * Author: Yi Yang <yyangcdl@cn.ibm.com> 6f08c3bdfSopenharmony_ci */ 7f08c3bdfSopenharmony_ci 8f08c3bdfSopenharmony_ci#define _GNU_SOURCE 9f08c3bdfSopenharmony_ci 10f08c3bdfSopenharmony_ci#include <errno.h> 11f08c3bdfSopenharmony_ci#include <string.h> 12f08c3bdfSopenharmony_ci#include <signal.h> 13f08c3bdfSopenharmony_ci#include <sys/types.h> 14f08c3bdfSopenharmony_ci 15f08c3bdfSopenharmony_ci#include "tst_test.h" 16f08c3bdfSopenharmony_ci#include "lapi/fcntl.h" 17f08c3bdfSopenharmony_ci#include "lapi/tee.h" 18f08c3bdfSopenharmony_ci#include "lapi/splice.h" 19f08c3bdfSopenharmony_ci 20f08c3bdfSopenharmony_ci#define TEST_BLOCK_SIZE 1024 21f08c3bdfSopenharmony_ci 22f08c3bdfSopenharmony_ci#define TESTFILE1 "tee_test_file_1" 23f08c3bdfSopenharmony_ci#define TESTFILE2 "tee_test_file_2" 24f08c3bdfSopenharmony_ci 25f08c3bdfSopenharmony_cistatic int fd_in, fd_out; 26f08c3bdfSopenharmony_cistatic char buffer[TEST_BLOCK_SIZE]; 27f08c3bdfSopenharmony_ci 28f08c3bdfSopenharmony_cistatic void check_file(void) 29f08c3bdfSopenharmony_ci{ 30f08c3bdfSopenharmony_ci int i; 31f08c3bdfSopenharmony_ci char teebuffer[TEST_BLOCK_SIZE]; 32f08c3bdfSopenharmony_ci 33f08c3bdfSopenharmony_ci fd_out = SAFE_OPEN(TESTFILE2, O_RDONLY); 34f08c3bdfSopenharmony_ci SAFE_READ(1, fd_out, teebuffer, TEST_BLOCK_SIZE); 35f08c3bdfSopenharmony_ci 36f08c3bdfSopenharmony_ci for (i = 0; i < TEST_BLOCK_SIZE; i++) { 37f08c3bdfSopenharmony_ci if (buffer[i] != teebuffer[i]) 38f08c3bdfSopenharmony_ci break; 39f08c3bdfSopenharmony_ci } 40f08c3bdfSopenharmony_ci 41f08c3bdfSopenharmony_ci if (i < TEST_BLOCK_SIZE) 42f08c3bdfSopenharmony_ci tst_res(TFAIL, "Wrong data read from the buffer at %i", i); 43f08c3bdfSopenharmony_ci else 44f08c3bdfSopenharmony_ci tst_res(TPASS, "Written data has been read back correctly"); 45f08c3bdfSopenharmony_ci 46f08c3bdfSopenharmony_ci SAFE_CLOSE(fd_out); 47f08c3bdfSopenharmony_ci} 48f08c3bdfSopenharmony_ci 49f08c3bdfSopenharmony_cistatic void tee_test(void) 50f08c3bdfSopenharmony_ci{ 51f08c3bdfSopenharmony_ci int pipe1[2]; 52f08c3bdfSopenharmony_ci int pipe2[2]; 53f08c3bdfSopenharmony_ci int ret = 0; 54f08c3bdfSopenharmony_ci 55f08c3bdfSopenharmony_ci fd_in = SAFE_OPEN(TESTFILE1, O_RDONLY); 56f08c3bdfSopenharmony_ci fd_out = SAFE_OPEN(TESTFILE2, O_WRONLY | O_CREAT | O_TRUNC, 0777); 57f08c3bdfSopenharmony_ci 58f08c3bdfSopenharmony_ci SAFE_PIPE(pipe1); 59f08c3bdfSopenharmony_ci SAFE_PIPE(pipe2); 60f08c3bdfSopenharmony_ci 61f08c3bdfSopenharmony_ci ret = splice(fd_in, NULL, pipe1[1], NULL, TEST_BLOCK_SIZE, 0); 62f08c3bdfSopenharmony_ci if (ret < 0) 63f08c3bdfSopenharmony_ci tst_brk(TBROK | TERRNO, "splice(fd_in, pipe1) failed"); 64f08c3bdfSopenharmony_ci 65f08c3bdfSopenharmony_ci ret = tee(pipe1[0], pipe2[1], TEST_BLOCK_SIZE, SPLICE_F_NONBLOCK); 66f08c3bdfSopenharmony_ci if (ret < 0) 67f08c3bdfSopenharmony_ci tst_brk(TBROK | TERRNO, "tee() failed"); 68f08c3bdfSopenharmony_ci 69f08c3bdfSopenharmony_ci ret = splice(pipe2[0], NULL, fd_out, NULL, TEST_BLOCK_SIZE, 0); 70f08c3bdfSopenharmony_ci if (ret < 0) 71f08c3bdfSopenharmony_ci tst_brk(TBROK | TERRNO, "splice(pipe2, fd_out) failed"); 72f08c3bdfSopenharmony_ci 73f08c3bdfSopenharmony_ci SAFE_CLOSE(pipe2[0]); 74f08c3bdfSopenharmony_ci SAFE_CLOSE(pipe2[1]); 75f08c3bdfSopenharmony_ci SAFE_CLOSE(pipe1[0]); 76f08c3bdfSopenharmony_ci SAFE_CLOSE(pipe1[1]); 77f08c3bdfSopenharmony_ci SAFE_CLOSE(fd_out); 78f08c3bdfSopenharmony_ci SAFE_CLOSE(fd_in); 79f08c3bdfSopenharmony_ci 80f08c3bdfSopenharmony_ci check_file(); 81f08c3bdfSopenharmony_ci} 82f08c3bdfSopenharmony_ci 83f08c3bdfSopenharmony_cistatic void setup(void) 84f08c3bdfSopenharmony_ci{ 85f08c3bdfSopenharmony_ci int i; 86f08c3bdfSopenharmony_ci 87f08c3bdfSopenharmony_ci for (i = 0; i < TEST_BLOCK_SIZE; i++) 88f08c3bdfSopenharmony_ci buffer[i] = i & 0xff; 89f08c3bdfSopenharmony_ci 90f08c3bdfSopenharmony_ci fd_in = SAFE_OPEN(TESTFILE1, O_WRONLY | O_CREAT | O_TRUNC, 0777); 91f08c3bdfSopenharmony_ci SAFE_WRITE(SAFE_WRITE_ALL, fd_in, buffer, TEST_BLOCK_SIZE); 92f08c3bdfSopenharmony_ci SAFE_CLOSE(fd_in); 93f08c3bdfSopenharmony_ci} 94f08c3bdfSopenharmony_ci 95f08c3bdfSopenharmony_cistatic void cleanup(void) 96f08c3bdfSopenharmony_ci{ 97f08c3bdfSopenharmony_ci if (fd_in > 0) 98f08c3bdfSopenharmony_ci SAFE_CLOSE(fd_in); 99f08c3bdfSopenharmony_ci 100f08c3bdfSopenharmony_ci if (fd_out > 0) 101f08c3bdfSopenharmony_ci SAFE_CLOSE(fd_out); 102f08c3bdfSopenharmony_ci} 103f08c3bdfSopenharmony_ci 104f08c3bdfSopenharmony_cistatic struct tst_test test = { 105f08c3bdfSopenharmony_ci .setup = setup, 106f08c3bdfSopenharmony_ci .cleanup = cleanup, 107f08c3bdfSopenharmony_ci .test_all = tee_test, 108f08c3bdfSopenharmony_ci .needs_tmpdir = 1, 109f08c3bdfSopenharmony_ci}; 110