]> git.ipfire.org Git - thirdparty/glibc.git/blame - support/temp_file.c
powerpc: Add missing arch flags on rounding ifunc variants
[thirdparty/glibc.git] / support / temp_file.c
CommitLineData
c23de0aa 1/* Temporary file handling for tests.
dff8da6b 2 Copyright (C) 1998-2024 Free Software Foundation, Inc.
fb7bff12 3 Copyright The GNU Tools Authors.
c23de0aa
FW
4 This file is part of the GNU C Library.
5
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
10
11 The GNU C Library 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 GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
5a82c748 18 <https://www.gnu.org/licenses/>. */
c23de0aa
FW
19
20/* This is required to get an mkstemp which can create large files on
21 some 32-bit platforms. */
22#define _FILE_OFFSET_BITS 64
23
fb7bff12 24#include <support/check.h>
c23de0aa
FW
25#include <support/temp_file.h>
26#include <support/temp_file-internal.h>
27#include <support/support.h>
28
fb7bff12 29#include <errno.h>
c23de0aa 30#include <paths.h>
c23de0aa
FW
31#include <stdio.h>
32#include <stdlib.h>
33#include <string.h>
fb7bff12 34#include <xunistd.h>
c23de0aa
FW
35
36/* List of temporary files. */
37static struct temp_name_list
38{
706256af 39 struct temp_name_list *next;
c23de0aa 40 char *name;
c22553ef 41 pid_t owner;
fb7bff12 42 bool toolong;
c23de0aa
FW
43} *temp_name_list;
44
45/* Location of the temporary files. Set by the test skeleton via
46 support_set_test_dir. The string is not be freed. */
47static const char *test_dir = _PATH_TMP;
48
fb7bff12
SP
49/* Name of subdirectories in a too long temporary directory tree. */
50static char toolong_subdir[NAME_MAX + 1];
51static bool toolong_initialized;
52static size_t toolong_path_max;
53
54static void
55add_temp_file_internal (const char *name, bool toolong)
c23de0aa
FW
56{
57 struct temp_name_list *newp
58 = (struct temp_name_list *) xcalloc (sizeof (*newp), 1);
59 char *newname = strdup (name);
60 if (newname != NULL)
61 {
62 newp->name = newname;
706256af 63 newp->next = temp_name_list;
c22553ef 64 newp->owner = getpid ();
fb7bff12 65 newp->toolong = toolong;
706256af 66 temp_name_list = newp;
c23de0aa
FW
67 }
68 else
69 free (newp);
70}
71
fb7bff12
SP
72void
73add_temp_file (const char *name)
74{
75 add_temp_file_internal (name, false);
76}
77
c23de0aa 78int
60854f40 79create_temp_file_in_dir (const char *base, const char *dir, char **filename)
c23de0aa
FW
80{
81 char *fname;
82 int fd;
83
60854f40 84 fname = xasprintf ("%s/%sXXXXXX", dir, base);
c23de0aa
FW
85
86 fd = mkstemp (fname);
87 if (fd == -1)
88 {
89 printf ("cannot open temporary file '%s': %m\n", fname);
90 free (fname);
91 return -1;
92 }
93
94 add_temp_file (fname);
95 if (filename != NULL)
96 *filename = fname;
97 else
98 free (fname);
99
100 return fd;
101}
102
60854f40
AZ
103int
104create_temp_file (const char *base, char **filename)
105{
106 return create_temp_file_in_dir (base, test_dir, filename);
107}
108
fb7bff12
SP
109static char *
110create_temp_directory_internal (const char *base, bool toolong)
1ffe1ccb 111{
8adfb0ee
FW
112 char *path = xasprintf ("%s/%sXXXXXX", test_dir, base);
113 if (mkdtemp (path) == NULL)
1ffe1ccb 114 {
8adfb0ee 115 printf ("error: mkdtemp (\"%s\"): %m", path);
1ffe1ccb
FW
116 exit (1);
117 }
fb7bff12 118 add_temp_file_internal (path, toolong);
8adfb0ee 119 return path;
1ffe1ccb
FW
120}
121
fb7bff12
SP
122char *
123support_create_temp_directory (const char *base)
124{
125 return create_temp_directory_internal (base, false);
126}
127
128static void
129ensure_toolong_initialized (void)
130{
131 if (!toolong_initialized)
132 FAIL_EXIT1 ("uninitialized toolong directory tree\n");
133}
134
135static void
136initialize_toolong (const char *base)
137{
138 long name_max = pathconf (base, _PC_NAME_MAX);
139 name_max = (name_max < 0 ? 64
140 : (name_max < sizeof (toolong_subdir) ? name_max
141 : sizeof (toolong_subdir) - 1));
142
143 long path_max = pathconf (base, _PC_PATH_MAX);
144 path_max = (path_max < 0 ? 1024
145 : path_max <= PTRDIFF_MAX ? path_max : PTRDIFF_MAX);
146
147 /* Sanity check to ensure that the test does not create temporary directories
148 in different filesystems because this API doesn't support it. */
149 if (toolong_initialized)
150 {
151 if (name_max != strlen (toolong_subdir))
152 FAIL_UNSUPPORTED ("name_max: Temporary directories in different"
153 " filesystems not supported yet\n");
154 if (path_max != toolong_path_max)
155 FAIL_UNSUPPORTED ("path_max: Temporary directories in different"
156 " filesystems not supported yet\n");
157 return;
158 }
159
160 toolong_path_max = path_max;
161
162 size_t len = name_max;
163 memset (toolong_subdir, 'X', len);
164 toolong_initialized = true;
165}
166
167char *
168support_create_and_chdir_toolong_temp_directory (const char *basename)
169{
170 char *base = create_temp_directory_internal (basename, true);
171 xchdir (base);
172
173 initialize_toolong (base);
174
175 size_t sz = strlen (toolong_subdir);
176
177 /* Create directories and descend into them so that the final path is larger
178 than PATH_MAX. */
179 for (size_t i = 0; i <= toolong_path_max / sz; i++)
180 {
181 int ret = mkdir (toolong_subdir, S_IRWXU);
182 if (ret != 0 && errno == ENAMETOOLONG)
183 FAIL_UNSUPPORTED ("Filesystem does not support creating too long "
184 "directory trees\n");
185 else if (ret != 0)
186 FAIL_EXIT1 ("Failed to create directory tree: %m\n");
187 xchdir (toolong_subdir);
188 }
189 return base;
190}
c23de0aa
FW
191
192void
fb7bff12 193support_chdir_toolong_temp_directory (const char *base)
c23de0aa 194{
fb7bff12
SP
195 ensure_toolong_initialized ();
196
197 xchdir (base);
198
199 size_t sz = strlen (toolong_subdir);
200 for (size_t i = 0; i <= toolong_path_max / sz; i++)
201 xchdir (toolong_subdir);
202}
203
204/* Helper functions called by the test skeleton follow. */
205
206static void
207remove_toolong_subdirs (const char *base)
208{
209 ensure_toolong_initialized ();
210
211 if (chdir (base) != 0)
212 {
213 printf ("warning: toolong cleanup base failed: chdir (\"%s\"): %m\n",
214 base);
215 return;
216 }
217
218 /* Descend. */
219 int levels = 0;
220 size_t sz = strlen (toolong_subdir);
221 for (levels = 0; levels <= toolong_path_max / sz; levels++)
222 if (chdir (toolong_subdir) != 0)
223 {
224 printf ("warning: toolong cleanup failed: chdir (\"%s\"): %m\n",
225 toolong_subdir);
226 break;
227 }
228
229 /* Ascend and remove. */
230 while (--levels >= 0)
231 {
232 if (chdir ("..") != 0)
233 {
234 printf ("warning: toolong cleanup failed: chdir (\"..\"): %m\n");
235 return;
236 }
237 if (remove (toolong_subdir) != 0)
238 {
239 printf ("warning: could not remove subdirectory: %s: %m\n",
240 toolong_subdir);
241 return;
242 }
243 }
c23de0aa
FW
244}
245
246void
247support_delete_temp_files (void)
248{
c22553ef 249 pid_t pid = getpid ();
c23de0aa
FW
250 while (temp_name_list != NULL)
251 {
7f0d9e61 252 /* Only perform the removal if the path was registered in the same
c22553ef
FW
253 process, as identified by the PID. (This assumes that the
254 parent process which registered the temporary file sticks
255 around, to prevent PID reuse.) */
256 if (temp_name_list->owner == pid)
257 {
fb7bff12
SP
258 if (temp_name_list->toolong)
259 remove_toolong_subdirs (temp_name_list->name);
260
c22553ef
FW
261 if (remove (temp_name_list->name) != 0)
262 printf ("warning: could not remove temporary file: %s: %m\n",
263 temp_name_list->name);
264 }
c23de0aa
FW
265 free (temp_name_list->name);
266
706256af 267 struct temp_name_list *next = temp_name_list->next;
c23de0aa
FW
268 free (temp_name_list);
269 temp_name_list = next;
270 }
271}
272
273void
274support_print_temp_files (FILE *f)
275{
276 if (temp_name_list != NULL)
277 {
278 struct temp_name_list *n;
279 fprintf (f, "temp_files=(\n");
706256af 280 for (n = temp_name_list; n != NULL; n = n->next)
c23de0aa
FW
281 fprintf (f, " '%s'\n", n->name);
282 fprintf (f, ")\n");
283 }
284}
fb7bff12
SP
285
286void
287support_set_test_dir (const char *path)
288{
289 test_dir = path;
290}