/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */1/* kdc/t_sockact.c - socket activation test harness */2/*3* Copyright (C) 2025 by the Massachusetts Institute of Technology.4* All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9*10* * Redistributions of source code must retain the above copyright11* notice, this list of conditions and the following disclaimer.12*13* * Redistributions in binary form must reproduce the above copyright14* notice, this list of conditions and the following disclaimer in15* the documentation and/or other materials provided with the16* distribution.17*18* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS19* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT20* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS21* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE22* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,23* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES24* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR25* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)26* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,27* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)28* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED29* OF THE POSSIBILITY OF SUCH DAMAGE.30*/3132/*33* Usage: t_sockact address... -- program args...34*35* This program simulates systemd socket activation by creating one or more36* listener sockets at the specified addresses, setting LISTEN_FDS and37* LISTEN_PID in the environment, and executing the specified command. (The38* real systemd would not execute the program until there is input on one of39* the listener sockets, but we do not need to simulate that, and executing the40* command immediately allow easier integration with k5test.py.)41*/4243#include "k5-int.h"44#include "socket-utils.h"4546static int max_fd;4748static void49create_socket(const struct sockaddr *addr)50{51int fd, one = 1;5253fd = socket(addr->sa_family, SOCK_STREAM, 0);54if (fd < 0)55abort();56if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) != 0)57abort();58#if defined(SO_REUSEPORT) && defined(__APPLE__)59if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one)) != 0)60abort();61#endif62#if defined(IPV6_V6ONLY)63if (addr->sa_family == AF_INET6) {64if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &one, sizeof(one)) != 0)65abort();66}67#endif68if (bind(fd, addr, sa_socklen(addr)) != 0)69abort();70if (listen(fd, 5) != 0)71abort();72max_fd = fd;73}7475int76main(int argc, char **argv)77{78const char *addrstr;79struct sockaddr_storage ss = { 0 };80struct addrinfo hints = { 0 }, *ai_list = NULL, *ai = NULL;81char *host, nbuf[128];82int i, port;8384for (i = 1; i < argc; i++) {85if (strcmp(argv[i], "--") == 0)86break;87addrstr = argv[i];88if (*addrstr == '/') {89ss.ss_family = AF_UNIX;90strlcpy(ss2sun(&ss)->sun_path, addrstr,91sizeof(ss2sun(&ss)->sun_path));92create_socket(ss2sa(&ss));93} else {94if (k5_parse_host_string(addrstr, 0, &host, &port) != 0 || !port)95abort();96hints.ai_socktype = SOCK_STREAM;97hints.ai_family = AF_UNSPEC;98hints.ai_flags = AI_PASSIVE;99#ifdef AI_NUMERICSERV100hints.ai_flags |= AI_NUMERICSERV;101#endif102(void)snprintf(nbuf, sizeof(nbuf), "%d", port);103if (getaddrinfo(host, nbuf, &hints, &ai_list) != 0)104abort();105for (ai = ai_list; ai != NULL; ai = ai->ai_next)106create_socket(ai->ai_addr);107freeaddrinfo(ai_list);108free(host);109}110}111argv += i + 1;112113(void)snprintf(nbuf, sizeof(nbuf), "%d", max_fd - 2);114setenv("LISTEN_FDS", nbuf, 1);115(void)snprintf(nbuf, sizeof(nbuf), "%lu", (unsigned long)getpid());116setenv("LISTEN_PID", nbuf, 1);117execv(argv[0], argv);118abort();119return 1;120}121122123