xref: /freebsd/bin/pkill/tests/spin_helper.c (revision 2b833162)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2023 Klara, Inc.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 #include <sys/cdefs.h>
28 
29 #include <err.h>
30 #include <errno.h>
31 #include <fcntl.h>
32 #include <limits.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <stdio.h>
36 #include <unistd.h>
37 
38 static int
39 exec_shortargs(char *argv[])
40 {
41 	char *flag_arg = argv[2];
42 	char *sentinel = argv[3];
43 	char * nargv[] = { argv[0], __DECONST(char *, "--spin"), flag_arg,
44 	    sentinel, NULL };
45 	char * const nenvp[] = { NULL };
46 
47 	execve(argv[0], nargv, nenvp);
48 	err(1, "execve");
49 }
50 
51 static int
52 exec_largeargs(char *argv[])
53 {
54 	char *flag_arg = argv[2];
55 	char *sentinel = argv[3];
56 	/*
57 	 * Account for each argument and their NUL terminator, as well as an
58 	 * extra NUL terminator.
59 	 */
60 	size_t bufsz = ARG_MAX -
61 	    ((strlen(argv[0]) + 1) + sizeof("--spin") + (strlen(flag_arg) + 1) +
62 	    (strlen(sentinel) + 1) + 1);
63 	char *s = NULL;
64 	char * nargv[] = { argv[0], __DECONST(char *, "--spin"), flag_arg, NULL,
65 	    sentinel, NULL };
66 	char * const nenvp[] = { NULL };
67 
68 	/*
69 	 * Our heuristic may or may not be accurate, we'll keep trying with
70 	 * smaller argument sizes as needed until we stop getting E2BIG.
71 	 */
72 	do {
73 		if (s == NULL)
74 			s = malloc(bufsz + 1);
75 		else
76 			s = realloc(s, bufsz + 1);
77 		if (s == NULL)
78 			abort();
79 		memset(s, 'x', bufsz);
80 		s[bufsz] = '\0';
81 		nargv[3] = s;
82 
83 		execve(argv[0], nargv, nenvp);
84 		bufsz--;
85 	} while (errno == E2BIG);
86 	err(1, "execve");
87 }
88 
89 int
90 main(int argc, char *argv[])
91 {
92 
93 	if (argc > 1 && strcmp(argv[1], "--spin") == 0) {
94 		int fd;
95 
96 		if (argc < 4) {
97 			fprintf(stderr, "usage: %s --spin flagfile ...\n", argv[0]);
98 			return (1);
99 		}
100 
101 		fd = open(argv[2], O_RDWR | O_CREAT, 0755);
102 		if (fd < 0)
103 			err(1, "%s", argv[2]);
104 		close(fd);
105 
106 		for (;;) {
107 			sleep(1);
108 		}
109 
110 		return (1);
111 	}
112 
113 	if (argc != 4) {
114 		fprintf(stderr, "usage: %s [--short | --long] flagfile sentinel\n",
115 		    argv[0]);
116 		return (1);
117 	}
118 
119 	if (strcmp(argv[1], "--short") == 0)
120 		exec_shortargs(argv);
121 	else
122 		exec_largeargs(argv);
123 }
124