1*42e22086SApple OSS Distributions /* 2*42e22086SApple OSS Distributions * Copyright (c) 2021 Apple Inc. All rights reserved. 3*42e22086SApple OSS Distributions * 4*42e22086SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ 5*42e22086SApple OSS Distributions * 6*42e22086SApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code 7*42e22086SApple OSS Distributions * as defined in and that are subject to the Apple Public Source License 8*42e22086SApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in 9*42e22086SApple OSS Distributions * compliance with the License. The rights granted to you under the License 10*42e22086SApple OSS Distributions * may not be used to create, or enable the creation or redistribution of, 11*42e22086SApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to 12*42e22086SApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any 13*42e22086SApple OSS Distributions * terms of an Apple operating system software license agreement. 14*42e22086SApple OSS Distributions * 15*42e22086SApple OSS Distributions * Please obtain a copy of the License at 16*42e22086SApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file. 17*42e22086SApple OSS Distributions * 18*42e22086SApple OSS Distributions * The Original Code and all software distributed under the License are 19*42e22086SApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 20*42e22086SApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 21*42e22086SApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 22*42e22086SApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 23*42e22086SApple OSS Distributions * Please see the License for the specific language governing rights and 24*42e22086SApple OSS Distributions * limitations under the License. 25*42e22086SApple OSS Distributions * 26*42e22086SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ 27*42e22086SApple OSS Distributions */ 28*42e22086SApple OSS Distributions 29*42e22086SApple OSS Distributions #include <sys/socket.h> 30*42e22086SApple OSS Distributions #include <sys/sysctl.h> 31*42e22086SApple OSS Distributions #include <sys/time.h> 32*42e22086SApple OSS Distributions 33*42e22086SApple OSS Distributions #include <darwintest.h> 34*42e22086SApple OSS Distributions 35*42e22086SApple OSS Distributions T_DECL(so_linger_negative, "SO_LINGER negative") 36*42e22086SApple OSS Distributions { 37*42e22086SApple OSS Distributions int s = -1; 38*42e22086SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(s = socket(AF_LOCAL, SOCK_DGRAM, 0), "socket(AF_LOCAL, SOCK_DGRAM)"); 39*42e22086SApple OSS Distributions 40*42e22086SApple OSS Distributions struct linger set_l = {}; 41*42e22086SApple OSS Distributions set_l.l_onoff = 1; 42*42e22086SApple OSS Distributions set_l.l_linger = -1; 43*42e22086SApple OSS Distributions T_LOG("l_linger %d", set_l.l_linger); 44*42e22086SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(setsockopt(s, SOL_SOCKET, SO_LINGER, 45*42e22086SApple OSS Distributions &set_l, sizeof(struct linger)), 46*42e22086SApple OSS Distributions "setsockopt SO_LINGER"); 47*42e22086SApple OSS Distributions 48*42e22086SApple OSS Distributions struct linger get_l = {}; 49*42e22086SApple OSS Distributions socklen_t len = sizeof(struct linger); 50*42e22086SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(getsockopt(s, SOL_SOCKET, SO_LINGER, 51*42e22086SApple OSS Distributions &get_l, &len), 52*42e22086SApple OSS Distributions "getsockopt SO_LINGER"); 53*42e22086SApple OSS Distributions 54*42e22086SApple OSS Distributions T_EXPECT_EQ(set_l.l_linger, get_l.l_linger, 55*42e22086SApple OSS Distributions "SO_LINGER negative l_linger %d == %d", set_l.l_linger, get_l.l_linger); 56*42e22086SApple OSS Distributions } 57*42e22086SApple OSS Distributions 58*42e22086SApple OSS Distributions T_DECL(so_linger_overflow, "SO_LINGER overflow") 59*42e22086SApple OSS Distributions { 60*42e22086SApple OSS Distributions int s = -1; 61*42e22086SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(s = socket(AF_LOCAL, SOCK_DGRAM, 0), "socket(AF_LOCAL, SOCK_DGRAM)"); 62*42e22086SApple OSS Distributions 63*42e22086SApple OSS Distributions struct linger set_l = {}; 64*42e22086SApple OSS Distributions set_l.l_onoff = 1; 65*42e22086SApple OSS Distributions set_l.l_linger = SHRT_MAX + 1; 66*42e22086SApple OSS Distributions T_LOG("l_linger %d", set_l.l_linger); 67*42e22086SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(setsockopt(s, SOL_SOCKET, SO_LINGER, 68*42e22086SApple OSS Distributions &set_l, sizeof(struct linger)), 69*42e22086SApple OSS Distributions "setsockopt SO_LINGER"); 70*42e22086SApple OSS Distributions 71*42e22086SApple OSS Distributions struct linger get_l = {}; 72*42e22086SApple OSS Distributions socklen_t len = sizeof(struct linger); 73*42e22086SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(getsockopt(s, SOL_SOCKET, SO_LINGER, 74*42e22086SApple OSS Distributions &get_l, &len), 75*42e22086SApple OSS Distributions "getsockopt SO_LINGER"); 76*42e22086SApple OSS Distributions 77*42e22086SApple OSS Distributions /* 78*42e22086SApple OSS Distributions * Test passes based on the knowledge that l_linger is stored 79*42e22086SApple OSS Distributions * as a short signed integer 80*42e22086SApple OSS Distributions */ 81*42e22086SApple OSS Distributions T_EXPECT_EQ((short)set_l.l_linger, (short)get_l.l_linger, 82*42e22086SApple OSS Distributions "SO_LINGER overflow l_linger (short) %d == (short) %d", 83*42e22086SApple OSS Distributions set_l.l_linger, get_l.l_linger); 84*42e22086SApple OSS Distributions } 85*42e22086SApple OSS Distributions 86*42e22086SApple OSS Distributions T_DECL(so_linger_500, "SO_LINGER 500") 87*42e22086SApple OSS Distributions { 88*42e22086SApple OSS Distributions int s = -1; 89*42e22086SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(s = socket(AF_LOCAL, SOCK_DGRAM, 0), "socket(AF_LOCAL, SOCK_DGRAM)"); 90*42e22086SApple OSS Distributions 91*42e22086SApple OSS Distributions struct clockinfo clkinfo; 92*42e22086SApple OSS Distributions size_t oldlen = sizeof(struct clockinfo); 93*42e22086SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(sysctlbyname("kern.clockrate", &clkinfo, &oldlen, NULL, 0), 94*42e22086SApple OSS Distributions "sysctlbyname(kern.clockrate)"); 95*42e22086SApple OSS Distributions 96*42e22086SApple OSS Distributions struct linger set_l = {}; 97*42e22086SApple OSS Distributions set_l.l_onoff = 1; 98*42e22086SApple OSS Distributions set_l.l_linger = 500; 99*42e22086SApple OSS Distributions 100*42e22086SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(setsockopt(s, SOL_SOCKET, SO_LINGER, 101*42e22086SApple OSS Distributions &set_l, sizeof(struct linger)), 102*42e22086SApple OSS Distributions "setsockopt SO_LINGER"); 103*42e22086SApple OSS Distributions 104*42e22086SApple OSS Distributions struct linger get_l = {}; 105*42e22086SApple OSS Distributions socklen_t len = sizeof(struct linger); 106*42e22086SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(getsockopt(s, SOL_SOCKET, SO_LINGER, 107*42e22086SApple OSS Distributions &get_l, &len), 108*42e22086SApple OSS Distributions "getsockopt SO_LINGER"); 109*42e22086SApple OSS Distributions 110*42e22086SApple OSS Distributions T_EXPECT_EQ(set_l.l_linger, get_l.l_linger, 111*42e22086SApple OSS Distributions "SO_LINGER 500 l_linger %d == %d", set_l.l_linger, get_l.l_linger); 112*42e22086SApple OSS Distributions } 113*42e22086SApple OSS Distributions 114*42e22086SApple OSS Distributions T_DECL(so_linger_sec_negative, "SO_LINGER_SEC negative") 115*42e22086SApple OSS Distributions { 116*42e22086SApple OSS Distributions int s = -1; 117*42e22086SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(s = socket(AF_LOCAL, SOCK_DGRAM, 0), "socket(AF_LOCAL, SOCK_DGRAM)"); 118*42e22086SApple OSS Distributions 119*42e22086SApple OSS Distributions struct clockinfo clkinfo; 120*42e22086SApple OSS Distributions size_t oldlen = sizeof(struct clockinfo); 121*42e22086SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(sysctlbyname("kern.clockrate", &clkinfo, &oldlen, NULL, 0), 122*42e22086SApple OSS Distributions "sysctlbyname(kern.clockrate)"); 123*42e22086SApple OSS Distributions 124*42e22086SApple OSS Distributions struct linger set_l = {}; 125*42e22086SApple OSS Distributions set_l.l_onoff = 1; 126*42e22086SApple OSS Distributions set_l.l_linger = -1; 127*42e22086SApple OSS Distributions T_LOG("l_linger %d * clkinfo.hz %d = %d", set_l.l_linger, clkinfo.hz, (set_l.l_linger * clkinfo.hz)); 128*42e22086SApple OSS Distributions 129*42e22086SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(setsockopt(s, SOL_SOCKET, SO_LINGER_SEC, 130*42e22086SApple OSS Distributions &set_l, sizeof(struct linger)), 131*42e22086SApple OSS Distributions "setsockopt SO_LINGER_SEC"); 132*42e22086SApple OSS Distributions 133*42e22086SApple OSS Distributions struct linger get_l = {}; 134*42e22086SApple OSS Distributions socklen_t len = sizeof(struct linger); 135*42e22086SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(getsockopt(s, SOL_SOCKET, SO_LINGER_SEC, 136*42e22086SApple OSS Distributions &get_l, &len), 137*42e22086SApple OSS Distributions "getsockopt SO_LINGER_SEC"); 138*42e22086SApple OSS Distributions 139*42e22086SApple OSS Distributions T_EXPECT_EQ(set_l.l_linger, get_l.l_linger, 140*42e22086SApple OSS Distributions "SO_LINGER_SEC negative l_linger %d == %d", set_l.l_linger, get_l.l_linger); 141*42e22086SApple OSS Distributions } 142*42e22086SApple OSS Distributions 143*42e22086SApple OSS Distributions T_DECL(so_linger_sec_5_seconds, "SO_LINGER_SEC 5 seconds") 144*42e22086SApple OSS Distributions { 145*42e22086SApple OSS Distributions int s = -1; 146*42e22086SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(s = socket(AF_LOCAL, SOCK_DGRAM, 0), "socket(AF_LOCAL, SOCK_DGRAM)"); 147*42e22086SApple OSS Distributions 148*42e22086SApple OSS Distributions struct clockinfo clkinfo; 149*42e22086SApple OSS Distributions size_t oldlen = sizeof(struct clockinfo); 150*42e22086SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(sysctlbyname("kern.clockrate", &clkinfo, &oldlen, NULL, 0), 151*42e22086SApple OSS Distributions "sysctlbyname(kern.clockrate)"); 152*42e22086SApple OSS Distributions 153*42e22086SApple OSS Distributions struct linger set_l = {}; 154*42e22086SApple OSS Distributions set_l.l_onoff = 1; 155*42e22086SApple OSS Distributions set_l.l_linger = 5; 156*42e22086SApple OSS Distributions T_LOG("l_linger %d * clkinfo.hz %d = %d", set_l.l_linger, clkinfo.hz, (set_l.l_linger * clkinfo.hz)); 157*42e22086SApple OSS Distributions 158*42e22086SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(setsockopt(s, SOL_SOCKET, SO_LINGER_SEC, 159*42e22086SApple OSS Distributions &set_l, sizeof(struct linger)), 160*42e22086SApple OSS Distributions "setsockopt SO_LINGER_SEC"); 161*42e22086SApple OSS Distributions 162*42e22086SApple OSS Distributions struct linger get_l = {}; 163*42e22086SApple OSS Distributions socklen_t len = sizeof(struct linger); 164*42e22086SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(getsockopt(s, SOL_SOCKET, SO_LINGER_SEC, 165*42e22086SApple OSS Distributions &get_l, &len), 166*42e22086SApple OSS Distributions "getsockopt SO_LINGER_SEC"); 167*42e22086SApple OSS Distributions 168*42e22086SApple OSS Distributions T_EXPECT_EQ(set_l.l_linger, get_l.l_linger, 169*42e22086SApple OSS Distributions "SO_LINGER_SEC 5 seconds l_linger %d == %d", set_l.l_linger, get_l.l_linger); 170*42e22086SApple OSS Distributions } 171