xref: /aosp_15_r20/external/wayland/tests/exec-fd-leak-checker.c (revision 84e872a0dc482bffdb63672969dd03a827d67c73)
1*84e872a0SLloyd Pique /*
2*84e872a0SLloyd Pique  * Copyright © 2012 Collabora, Ltd.
3*84e872a0SLloyd Pique  *
4*84e872a0SLloyd Pique  * Permission is hereby granted, free of charge, to any person obtaining
5*84e872a0SLloyd Pique  * a copy of this software and associated documentation files (the
6*84e872a0SLloyd Pique  * "Software"), to deal in the Software without restriction, including
7*84e872a0SLloyd Pique  * without limitation the rights to use, copy, modify, merge, publish,
8*84e872a0SLloyd Pique  * distribute, sublicense, and/or sell copies of the Software, and to
9*84e872a0SLloyd Pique  * permit persons to whom the Software is furnished to do so, subject to
10*84e872a0SLloyd Pique  * the following conditions:
11*84e872a0SLloyd Pique  *
12*84e872a0SLloyd Pique  * The above copyright notice and this permission notice (including the
13*84e872a0SLloyd Pique  * next paragraph) shall be included in all copies or substantial
14*84e872a0SLloyd Pique  * portions of the Software.
15*84e872a0SLloyd Pique  *
16*84e872a0SLloyd Pique  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17*84e872a0SLloyd Pique  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18*84e872a0SLloyd Pique  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19*84e872a0SLloyd Pique  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20*84e872a0SLloyd Pique  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21*84e872a0SLloyd Pique  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22*84e872a0SLloyd Pique  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23*84e872a0SLloyd Pique  * SOFTWARE.
24*84e872a0SLloyd Pique  */
25*84e872a0SLloyd Pique 
26*84e872a0SLloyd Pique #include <stdlib.h>
27*84e872a0SLloyd Pique #include <stdio.h>
28*84e872a0SLloyd Pique #include <errno.h>
29*84e872a0SLloyd Pique #include <limits.h>
30*84e872a0SLloyd Pique 
31*84e872a0SLloyd Pique #include "test-runner.h"
32*84e872a0SLloyd Pique 
33*84e872a0SLloyd Pique static int
parse_count(const char * str,int * value)34*84e872a0SLloyd Pique parse_count(const char *str, int *value)
35*84e872a0SLloyd Pique {
36*84e872a0SLloyd Pique 	char *end;
37*84e872a0SLloyd Pique 	long v;
38*84e872a0SLloyd Pique 
39*84e872a0SLloyd Pique 	errno = 0;
40*84e872a0SLloyd Pique 	v = strtol(str, &end, 10);
41*84e872a0SLloyd Pique 	if ((errno == ERANGE && (v == LONG_MAX || v == LONG_MIN)) ||
42*84e872a0SLloyd Pique 	    (errno != 0 && v == 0) ||
43*84e872a0SLloyd Pique 	    (end == str) ||
44*84e872a0SLloyd Pique 	    (*end != '\0')) {
45*84e872a0SLloyd Pique 		return -1;
46*84e872a0SLloyd Pique 	}
47*84e872a0SLloyd Pique 
48*84e872a0SLloyd Pique 	if (v < 0 || v > INT_MAX) {
49*84e872a0SLloyd Pique 		return -1;
50*84e872a0SLloyd Pique 	}
51*84e872a0SLloyd Pique 
52*84e872a0SLloyd Pique 	*value = v;
53*84e872a0SLloyd Pique 	return 0;
54*84e872a0SLloyd Pique }
55*84e872a0SLloyd Pique 
main(int argc,char * argv[])56*84e872a0SLloyd Pique int main(int argc, char *argv[])
57*84e872a0SLloyd Pique {
58*84e872a0SLloyd Pique 	int expected;
59*84e872a0SLloyd Pique 
60*84e872a0SLloyd Pique 	if (argc != 2)
61*84e872a0SLloyd Pique 		goto help_out;
62*84e872a0SLloyd Pique 
63*84e872a0SLloyd Pique 	if (parse_count(argv[1], &expected) < 0)
64*84e872a0SLloyd Pique 		goto help_out;
65*84e872a0SLloyd Pique 
66*84e872a0SLloyd Pique 	if (count_open_fds() == expected)
67*84e872a0SLloyd Pique 		return EXIT_SUCCESS;
68*84e872a0SLloyd Pique 	else
69*84e872a0SLloyd Pique 		return EXIT_FAILURE;
70*84e872a0SLloyd Pique 
71*84e872a0SLloyd Pique help_out:
72*84e872a0SLloyd Pique 	fprintf(stderr, "Usage: %s N\n"
73*84e872a0SLloyd Pique 		"where N is the expected number of open file descriptors.\n"
74*84e872a0SLloyd Pique 		"This program exits with a failure if the number "
75*84e872a0SLloyd Pique 		"does not match exactly.\n", argv[0]);
76*84e872a0SLloyd Pique 
77*84e872a0SLloyd Pique 	return EXIT_FAILURE;
78*84e872a0SLloyd Pique }
79