/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2023 Klara, Inc.4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8* 1. Redistributions of source code must retain the above copyright9* notice, this list of conditions and the following disclaimer.10* 2. Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND15* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE16* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE17* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE18* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL19* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS20* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)21* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT22* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY23* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF24* SUCH DAMAGE.25*/26#include <sys/cdefs.h>2728#include <err.h>29#include <errno.h>30#include <fcntl.h>31#include <limits.h>32#include <stdlib.h>33#include <string.h>34#include <stdio.h>35#include <unistd.h>3637static int38exec_shortargs(char *argv[])39{40char *flag_arg = argv[2];41char *sentinel = argv[3];42char * nargv[] = { argv[0], __DECONST(char *, "--spin"), flag_arg,43sentinel, NULL };44char * const nenvp[] = { NULL };4546execve(argv[0], nargv, nenvp);47err(1, "execve");48}4950static int51exec_largeargs(char *argv[])52{53char *flag_arg = argv[2];54char *sentinel = argv[3];55/*56* Account for each argument and their NUL terminator, as well as an57* extra NUL terminator.58*/59size_t bufsz = ARG_MAX -60((strlen(argv[0]) + 1) + sizeof("--spin") + (strlen(flag_arg) + 1) +61(strlen(sentinel) + 1) + 1);62char *s = NULL;63char * nargv[] = { argv[0], __DECONST(char *, "--spin"), flag_arg, NULL,64sentinel, NULL };65char * const nenvp[] = { NULL };6667/*68* Our heuristic may or may not be accurate, we'll keep trying with69* smaller argument sizes as needed until we stop getting E2BIG.70*/71do {72if (s == NULL)73s = malloc(bufsz + 1);74else75s = realloc(s, bufsz + 1);76if (s == NULL)77abort();78memset(s, 'x', bufsz);79s[bufsz] = '\0';80nargv[3] = s;8182execve(argv[0], nargv, nenvp);83bufsz--;84} while (errno == E2BIG);85err(1, "execve");86}8788int89main(int argc, char *argv[])90{9192if (argc > 1 && strcmp(argv[1], "--spin") == 0) {93int fd;9495if (argc < 4) {96fprintf(stderr, "usage: %s --spin flagfile ...\n", argv[0]);97return (1);98}99100fd = open(argv[2], O_RDWR | O_CREAT, 0755);101if (fd < 0)102err(1, "%s", argv[2]);103close(fd);104105for (;;) {106sleep(1);107}108109return (1);110}111112if (argc != 4) {113fprintf(stderr, "usage: %s [--short | --long] flagfile sentinel\n",114argv[0]);115return (1);116}117118if (strcmp(argv[1], "--short") == 0)119exec_shortargs(argv);120else121exec_largeargs(argv);122}123124125