1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 2f08c3bdfSopenharmony_ci/* 3f08c3bdfSopenharmony_ci* Copyright (c) Ulrich Drepper <drepper@redhat.com> 4f08c3bdfSopenharmony_ci* Copyright (c) International Business Machines Corp., 2009 5f08c3bdfSopenharmony_ci*/ 6f08c3bdfSopenharmony_ci 7f08c3bdfSopenharmony_ci/* 8f08c3bdfSopenharmony_ci* Test Name: socket02 9f08c3bdfSopenharmony_ci* 10f08c3bdfSopenharmony_ci* Description: 11f08c3bdfSopenharmony_ci* This program tests the new flag SOCK_CLOEXEC and SOCK_NONBLOCK introduced 12f08c3bdfSopenharmony_ci* in socket() in kernel 2.6.27. 13f08c3bdfSopenharmony_ci*/ 14f08c3bdfSopenharmony_ci 15f08c3bdfSopenharmony_ci#include <stdio.h> 16f08c3bdfSopenharmony_ci#include <unistd.h> 17f08c3bdfSopenharmony_ci#include <netinet/in.h> 18f08c3bdfSopenharmony_ci#include <sys/socket.h> 19f08c3bdfSopenharmony_ci#include "lapi/fcntl.h" 20f08c3bdfSopenharmony_ci#include "tst_test.h" 21f08c3bdfSopenharmony_ci 22f08c3bdfSopenharmony_cistatic int fd; 23f08c3bdfSopenharmony_ci 24f08c3bdfSopenharmony_cistatic struct tcase { 25f08c3bdfSopenharmony_ci int type; 26f08c3bdfSopenharmony_ci int flag; 27f08c3bdfSopenharmony_ci int fl_flag; 28f08c3bdfSopenharmony_ci char *des; 29f08c3bdfSopenharmony_ci} tcases[] = { 30f08c3bdfSopenharmony_ci {SOCK_STREAM, 0, F_GETFD, "no close-on-exec"}, 31f08c3bdfSopenharmony_ci {SOCK_STREAM | SOCK_CLOEXEC, FD_CLOEXEC, F_GETFD, "close-on-exec"}, 32f08c3bdfSopenharmony_ci {SOCK_STREAM, 0, F_GETFL, "no non-blocking"}, 33f08c3bdfSopenharmony_ci {SOCK_STREAM | SOCK_NONBLOCK, O_NONBLOCK, F_GETFL, "non-blocking"} 34f08c3bdfSopenharmony_ci}; 35f08c3bdfSopenharmony_ci 36f08c3bdfSopenharmony_cistatic void verify_socket(unsigned int n) 37f08c3bdfSopenharmony_ci{ 38f08c3bdfSopenharmony_ci int res; 39f08c3bdfSopenharmony_ci struct tcase *tc = &tcases[n]; 40f08c3bdfSopenharmony_ci 41f08c3bdfSopenharmony_ci fd = socket(PF_INET, tc->type, 0); 42f08c3bdfSopenharmony_ci if (fd == -1) 43f08c3bdfSopenharmony_ci tst_brk(TFAIL | TERRNO, "socket() failed"); 44f08c3bdfSopenharmony_ci 45f08c3bdfSopenharmony_ci res = SAFE_FCNTL(fd, tc->fl_flag); 46f08c3bdfSopenharmony_ci 47f08c3bdfSopenharmony_ci if (tc->flag != 0 && (res & tc->flag) == 0) { 48f08c3bdfSopenharmony_ci tst_res(TFAIL, "socket() failed to set %s flag", tc->des); 49f08c3bdfSopenharmony_ci return; 50f08c3bdfSopenharmony_ci } 51f08c3bdfSopenharmony_ci 52f08c3bdfSopenharmony_ci if (tc->flag == 0 && (res & tc->flag) != 0) { 53f08c3bdfSopenharmony_ci tst_res(TFAIL, "socket() failed to set %s flag", tc->des); 54f08c3bdfSopenharmony_ci return; 55f08c3bdfSopenharmony_ci } 56f08c3bdfSopenharmony_ci 57f08c3bdfSopenharmony_ci tst_res(TPASS, "socket() passed to set %s flag", tc->des); 58f08c3bdfSopenharmony_ci 59f08c3bdfSopenharmony_ci SAFE_CLOSE(fd); 60f08c3bdfSopenharmony_ci} 61f08c3bdfSopenharmony_ci 62f08c3bdfSopenharmony_cistatic void cleanup(void) 63f08c3bdfSopenharmony_ci{ 64f08c3bdfSopenharmony_ci if (fd > 0) 65f08c3bdfSopenharmony_ci SAFE_CLOSE(fd); 66f08c3bdfSopenharmony_ci} 67f08c3bdfSopenharmony_ci 68f08c3bdfSopenharmony_cistatic struct tst_test test = { 69f08c3bdfSopenharmony_ci .tcnt = ARRAY_SIZE(tcases), 70f08c3bdfSopenharmony_ci .test = verify_socket, 71f08c3bdfSopenharmony_ci .cleanup = cleanup 72f08c3bdfSopenharmony_ci}; 73