1*bbb1b6f9SApple OSS Distributions /* 2*bbb1b6f9SApple OSS Distributions * uds-to-self.c 3*bbb1b6f9SApple OSS Distributions * 4*bbb1b6f9SApple OSS Distributions * Test for rdar://137498122 (panic when calling unp_get_locks_in_order without checking if the 2 sockets are equal). 5*bbb1b6f9SApple OSS Distributions */ 6*bbb1b6f9SApple OSS Distributions 7*bbb1b6f9SApple OSS Distributions #include <sys/socket.h> 8*bbb1b6f9SApple OSS Distributions #include <sys/ucred.h> 9*bbb1b6f9SApple OSS Distributions #include <sys/un.h> 10*bbb1b6f9SApple OSS Distributions 11*bbb1b6f9SApple OSS Distributions #include <limits.h> 12*bbb1b6f9SApple OSS Distributions #include <stdio.h> 13*bbb1b6f9SApple OSS Distributions #include <string.h> 14*bbb1b6f9SApple OSS Distributions #include <unistd.h> 15*bbb1b6f9SApple OSS Distributions 16*bbb1b6f9SApple OSS Distributions #include <darwintest.h> 17*bbb1b6f9SApple OSS Distributions 18*bbb1b6f9SApple OSS Distributions static char buffer[LINE_MAX]; 19*bbb1b6f9SApple OSS Distributions 20*bbb1b6f9SApple OSS Distributions #define FILE_PATH "/tmp/uds-to-self.sock" 21*bbb1b6f9SApple OSS Distributions 22*bbb1b6f9SApple OSS Distributions T_GLOBAL_META( 23*bbb1b6f9SApple OSS Distributions T_META_NAMESPACE("xnu.net"), 24*bbb1b6f9SApple OSS Distributions T_META_RADAR_COMPONENT_NAME("xnu"), 25*bbb1b6f9SApple OSS Distributions T_META_RADAR_COMPONENT_VERSION("networking"), 26*bbb1b6f9SApple OSS Distributions T_META_CHECK_LEAKS(false)); 27*bbb1b6f9SApple OSS Distributions 28*bbb1b6f9SApple OSS Distributions 29*bbb1b6f9SApple OSS Distributions T_DECL(uds_self_connection, "self-connecting Unix domain sockets") 30*bbb1b6f9SApple OSS Distributions { 31*bbb1b6f9SApple OSS Distributions int fd; 32*bbb1b6f9SApple OSS Distributions struct sockaddr_un sun = { 0 }; 33*bbb1b6f9SApple OSS Distributions socklen_t solen; 34*bbb1b6f9SApple OSS Distributions ssize_t nsent; 35*bbb1b6f9SApple OSS Distributions ssize_t nrcvd; 36*bbb1b6f9SApple OSS Distributions struct xucred xucred; 37*bbb1b6f9SApple OSS Distributions pid_t pid; 38*bbb1b6f9SApple OSS Distributions uuid_t uuid; 39*bbb1b6f9SApple OSS Distributions audit_token_t token; 40*bbb1b6f9SApple OSS Distributions 41*bbb1b6f9SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(fd = socket(AF_UNIX, SOCK_DGRAM, 0), NULL); 42*bbb1b6f9SApple OSS Distributions 43*bbb1b6f9SApple OSS Distributions sun.sun_family = AF_UNIX; 44*bbb1b6f9SApple OSS Distributions snprintf(sun.sun_path, sizeof(sun.sun_path), FILE_PATH); 45*bbb1b6f9SApple OSS Distributions sun.sun_len = (unsigned char) SUN_LEN(&sun); 46*bbb1b6f9SApple OSS Distributions 47*bbb1b6f9SApple OSS Distributions unlink(FILE_PATH); 48*bbb1b6f9SApple OSS Distributions 49*bbb1b6f9SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(bind(fd, (struct sockaddr *)&sun, sun.sun_len), NULL); 50*bbb1b6f9SApple OSS Distributions 51*bbb1b6f9SApple OSS Distributions solen = sizeof(struct sockaddr_un); 52*bbb1b6f9SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(getsockname(fd, (struct sockaddr *)&sun, &solen), NULL); 53*bbb1b6f9SApple OSS Distributions T_LOG("socket bound to %s", sun.sun_path); 54*bbb1b6f9SApple OSS Distributions 55*bbb1b6f9SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(connect(fd, (struct sockaddr *)&sun, sun.sun_len), NULL); 56*bbb1b6f9SApple OSS Distributions 57*bbb1b6f9SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(getpeername(fd, (struct sockaddr *)&sun, &solen), NULL); 58*bbb1b6f9SApple OSS Distributions T_LOG("socket connected to %s", sun.sun_path); 59*bbb1b6f9SApple OSS Distributions 60*bbb1b6f9SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(nsent = send(fd, buffer, strlen(buffer) + 1, 0), NULL); 61*bbb1b6f9SApple OSS Distributions T_LOG("send %ld bytes\n", nsent); 62*bbb1b6f9SApple OSS Distributions 63*bbb1b6f9SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(nrcvd = recv(fd, buffer, sizeof(buffer), 0), NULL); 64*bbb1b6f9SApple OSS Distributions 65*bbb1b6f9SApple OSS Distributions /* 66*bbb1b6f9SApple OSS Distributions * The return value of getsockopt() is not important, what matters is to not panic 67*bbb1b6f9SApple OSS Distributions */ 68*bbb1b6f9SApple OSS Distributions solen = sizeof(xucred); 69*bbb1b6f9SApple OSS Distributions (void)getsockopt(fd, SOL_LOCAL, LOCAL_PEERCRED, &xucred, &solen); 70*bbb1b6f9SApple OSS Distributions 71*bbb1b6f9SApple OSS Distributions solen = sizeof(pid); 72*bbb1b6f9SApple OSS Distributions (void)getsockopt(fd, SOL_LOCAL, LOCAL_PEERPID, &pid, &solen); 73*bbb1b6f9SApple OSS Distributions 74*bbb1b6f9SApple OSS Distributions solen = sizeof(pid); 75*bbb1b6f9SApple OSS Distributions (void)getsockopt(fd, SOL_LOCAL, LOCAL_PEEREPID, &pid, &solen); 76*bbb1b6f9SApple OSS Distributions 77*bbb1b6f9SApple OSS Distributions solen = sizeof(uuid); 78*bbb1b6f9SApple OSS Distributions (void)getsockopt(fd, SOL_LOCAL, LOCAL_PEERUUID, &uuid, &solen); 79*bbb1b6f9SApple OSS Distributions 80*bbb1b6f9SApple OSS Distributions solen = sizeof(uuid); 81*bbb1b6f9SApple OSS Distributions (void)getsockopt(fd, SOL_LOCAL, LOCAL_PEEREUUID, &uuid, &solen); 82*bbb1b6f9SApple OSS Distributions 83*bbb1b6f9SApple OSS Distributions solen = sizeof(token); 84*bbb1b6f9SApple OSS Distributions (void)getsockopt(fd, SOL_LOCAL, LOCAL_PEERTOKEN, &token, &solen); 85*bbb1b6f9SApple OSS Distributions 86*bbb1b6f9SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(shutdown(fd, SHUT_RDWR), NULL); 87*bbb1b6f9SApple OSS Distributions 88*bbb1b6f9SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(close(fd), NULL); 89*bbb1b6f9SApple OSS Distributions 90*bbb1b6f9SApple OSS Distributions unlink(FILE_PATH); 91*bbb1b6f9SApple OSS Distributions } 92