1 /* t-exechelp.c - Module test for exechelp.c
2 * Copyright (C) 2009 Free Software Foundation, Inc.
4 * This file is part of GnuPG.
6 * GnuPG is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * GnuPG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <https://www.gnu.org/licenses/>.
34 print_open_fds (int *array)
41 for (n=0; array[n] != -1; n++)
43 printf ("open file descriptors: %d", n);
47 for (n=0; array[n] != -1; n++)
48 printf ("%d%s", array[n], array[n+1] == -1?"":" ");
55 xget_all_open_fds (void)
59 array = get_all_open_fds ();
62 fprintf (stderr, "%s:%d: get_all_open_fds failed: %s\n",
63 __FILE__, __LINE__, strerror (errno));
70 /* That is a very crude test. To do a proper test we would need to
71 fork a test process and best return information by some other means
72 than file descriptors. */
74 test_close_all_fds (void)
76 int max_fd = get_max_fds ();
79 int initial_count, count, n;
83 snprintf (buffer, sizeof buffer, "/bin/ls -l /proc/%d/fd", (int)getpid ());
88 printf ("max. file descriptors: %d\n", max_fd);
89 array = xget_all_open_fds ();
90 print_open_fds (array);
91 for (initial_count=n=0; array[n] != -1; n++)
95 /* Some dups to get more file descriptors and close one. */
102 array = xget_all_open_fds ();
104 print_open_fds (array);
105 for (count=n=0; array[n] != -1; n++)
107 if (count != initial_count+3)
109 fprintf (stderr, "%s:%d: dup or close failed\n",
115 /* Close the non standard ones. */
116 close_all_fds (3, NULL);
118 /* Get a list to check whether they are all closed. */
119 array = xget_all_open_fds ();
121 print_open_fds (array);
122 for (count=n=0; array[n] != -1; n++)
124 if (count > initial_count)
126 fprintf (stderr, "%s:%d: not all files were closed\n",
130 initial_count = count;
133 /* Now let's check the realloc we use. We do this and the next
134 tests only if we are allowed to open enought descriptors. */
135 if (get_max_fds () > 32)
137 int except[] = { 20, 23, 24, -1 };
139 for (n=initial_count; n < 31; n++)
141 array = xget_all_open_fds ();
143 print_open_fds (array);
145 for (n=0; n < 5; n++)
148 array = xget_all_open_fds ();
150 print_open_fds (array);
154 /* Check whether the except list works. */
155 close_all_fds (3, except);
156 array = xget_all_open_fds ();
158 print_open_fds (array);
159 for (count=n=0; array[n] != -1; n++)
163 if (count != initial_count + DIM(except)-1)
165 fprintf (stderr, "%s:%d: close_all_fds failed\n",
175 main (int argc, char **argv)
179 if (argc && !strcmp (argv[0], "--verbose"))
185 test_close_all_fds ();