1 /* Temporary files with automatic cleanup.
2 Copyright (C) 2006-2021 Free Software Foundation, Inc.
3 Written by Bruno Haible <bruno@clisp.org>, 2006.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
21 #include "clean-temp-simple.h"
22 #include "clean-temp-private.h"
33 #include "fatal-signal.h"
34 #include "asyncsafe-spin.h"
35 #include "glthread/lock.h"
36 #include "thread-optim.h"
38 #include "gl_linkedhash_list.h"
41 #define _(str) gettext (str)
44 /* Lock that protects the file_cleanup_list from concurrent modification in
46 gl_lock_define_initialized (static, file_cleanup_list_lock)
48 /* List of all temporary files without temporary directories. */
49 static gl_list_t /* <char *> */ volatile file_cleanup_list;
52 /* List of all temporary directories. */
53 struct all_tempdirs dir_cleanup_list /* = { NULL, 0, 0 } */;
56 /* List of all open file descriptors to temporary files. */
57 gl_list_t /* <closeable_fd *> */ volatile descriptors;
60 /* For the subdirs and for the files, we use a gl_list_t of type LINKEDHASH.
61 Why? We need a data structure that
63 1) Can contain an arbitrary number of 'char *' values. The strings
64 are compared via strcmp, not pointer comparison.
65 2) Has insertion and deletion operations that are fast: ideally O(1),
66 or possibly O(log n). This is important for GNU sort, which may
67 create a large number of temporary files.
68 3) Allows iteration through all elements from within a signal handler.
69 4) May or may not allow duplicates. It doesn't matter here, since
70 any file or subdir can only be removed once.
72 Criterion 1) would allow any gl_list_t or gl_oset_t implementation.
74 Criterion 2) leaves only GL_LINKEDHASH_LIST, GL_TREEHASH_LIST, or
77 Criterion 3) puts at disadvantage GL_TREEHASH_LIST and GL_TREE_OSET.
78 Namely, iteration through the elements of a binary tree requires access
79 to many ->left, ->right, ->parent pointers. However, the rebalancing
80 code for insertion and deletion in an AVL or red-black tree is so
81 complicated that we cannot assume that >left, ->right, ->parent pointers
82 are in a consistent state throughout these operations. Therefore, to
83 avoid a crash in the signal handler, all destructive operations to the
84 lists would have to be protected by a
85 block_fatal_signals ();
87 unblock_fatal_signals ();
88 pair. Which causes extra system calls.
90 Criterion 3) would also discourage GL_ARRAY_LIST and GL_CARRAY_LIST,
91 if they were not already excluded. Namely, these implementations use
92 xrealloc(), leaving a time window in which in the list->elements pointer
93 points to already deallocated memory. To avoid a crash in the signal
94 handler at such a moment, all destructive operations would have to
95 protected by block/unblock_fatal_signals (), in this case too.
97 A list of type GL_LINKEDHASH_LIST without duplicates fulfills all
99 2) Insertion and deletion are O(1) on average.
100 3) The gl_list_iterator, gl_list_iterator_next implementations do
101 not trigger memory allocations, nor other system calls, and are
102 therefore safe to be called from a signal handler.
103 Furthermore, since SIGNAL_SAFE_LIST is defined, the implementation
104 of the destructive functions ensures that the list structure is
105 safe to be traversed at any moment, even when interrupted by an
109 /* String equality and hash code functions used by the lists. */
112 clean_temp_string_equals (const void *x1, const void *x2)
114 const char *s1 = (const char *) x1;
115 const char *s2 = (const char *) x2;
116 return strcmp (s1, s2) == 0;
119 #define SIZE_BITS (sizeof (size_t) * CHAR_BIT)
121 /* A hash function for NUL-terminated char* strings using
122 the method described by Bruno Haible.
123 See https://www.haible.de/bruno/hashfunc.html. */
125 clean_temp_string_hash (const void *x)
127 const char *s = (const char *) x;
131 h = *s + ((h << 9) | (h >> (SIZE_BITS - 9)));
137 /* The set of fatal signal handlers.
138 Cached here because we are not allowed to call get_fatal_signal_set ()
139 from a signal handler. */
140 static const sigset_t *fatal_signal_set /* = NULL */;
143 init_fatal_signal_set (void)
145 if (fatal_signal_set == NULL)
146 fatal_signal_set = get_fatal_signal_set ();
150 /* Close a file descriptor.
151 Avoids race conditions with normal thread code or signal-handler code that
152 might want to close the same file descriptor. */
154 clean_temp_asyncsafe_close (struct closeable_fd *element)
160 asyncsafe_spin_lock (&element->lock, fatal_signal_set, &saved_mask);
161 if (!element->closed)
163 ret = close (element->fd);
165 element->closed = true;
172 asyncsafe_spin_unlock (&element->lock, &saved_mask);
173 element->done = true;
178 /* Initializations for use of this function. */
180 clean_temp_init_asyncsafe_close (void)
182 init_fatal_signal_set ();
185 /* The signal handler. It gets called asynchronously. */
186 static _GL_ASYNC_SAFE void
187 cleanup_action (int sig _GL_UNUSED)
191 /* First close all file descriptors to temporary files. */
193 gl_list_t fds = descriptors;
197 gl_list_iterator_t iter;
200 iter = gl_list_iterator (fds);
201 while (gl_list_iterator_next (&iter, &element, NULL))
203 clean_temp_asyncsafe_close ((struct closeable_fd *) element);
205 gl_list_iterator_free (&iter);
210 gl_list_t files = file_cleanup_list;
214 gl_list_iterator_t iter;
217 iter = gl_list_iterator (files);
218 while (gl_list_iterator_next (&iter, &element, NULL))
220 const char *file = (const char *) element;
223 gl_list_iterator_free (&iter);
227 for (i = 0; i < dir_cleanup_list.tempdir_count; i++)
229 struct tempdir *dir = dir_cleanup_list.tempdir_list[i];
233 gl_list_iterator_t iter;
236 /* First cleanup the files in the subdirectories. */
237 iter = gl_list_iterator (dir->files);
238 while (gl_list_iterator_next (&iter, &element, NULL))
240 const char *file = (const char *) element;
243 gl_list_iterator_free (&iter);
245 /* Then cleanup the subdirectories. */
246 iter = gl_list_iterator (dir->subdirs);
247 while (gl_list_iterator_next (&iter, &element, NULL))
249 const char *subdir = (const char *) element;
252 gl_list_iterator_free (&iter);
254 /* Then cleanup the temporary directory itself. */
255 rmdir (dir->dirname);
261 /* Set to -1 if initialization of this facility failed. */
262 static int volatile init_failed /* = 0 */;
264 /* Initializes this facility. */
266 do_clean_temp_init (void)
268 /* Initialize the data used by the cleanup handler. */
269 init_fatal_signal_set ();
270 /* Register the cleanup handler. */
271 if (at_fatal_signal (&cleanup_action) < 0)
275 /* Ensure that do_clean_temp_init is called once only. */
276 gl_once_define(static, clean_temp_once)
278 /* Initializes this facility upon first use.
279 Return 0 upon success, or -1 if there was a memory allocation problem. */
281 clean_temp_init (void)
283 gl_once (clean_temp_once, do_clean_temp_init);
288 /* Remove a file, with optional error message.
289 Return 0 upon success, or -1 if there was some problem. */
291 clean_temp_unlink (const char *absolute_file_name, bool cleanup_verbose)
293 if (unlink (absolute_file_name) < 0 && cleanup_verbose
297 _("cannot remove temporary file %s"), absolute_file_name);
304 /* ============= Temporary files without temporary directories ============= */
306 /* Register the given ABSOLUTE_FILE_NAME as being a file that needs to be
308 Should be called before the file ABSOLUTE_FILE_NAME is created.
309 Return 0 upon success, or -1 if there was a memory allocation problem. */
311 register_temporary_file (const char *absolute_file_name)
313 bool mt = gl_multithreaded ();
315 if (mt) gl_lock_lock (file_cleanup_list_lock);
319 /* Make sure that this facility and the file_cleanup_list are initialized. */
320 if (file_cleanup_list == NULL)
322 if (clean_temp_init () < 0)
328 gl_list_nx_create_empty (GL_LINKEDHASH_LIST,
329 clean_temp_string_equals,
330 clean_temp_string_hash,
332 if (file_cleanup_list == NULL)
339 /* Add absolute_file_name to file_cleanup_list, without duplicates. */
340 if (gl_list_search (file_cleanup_list, absolute_file_name) == NULL)
342 char *absolute_file_name_copy = strdup (absolute_file_name);
343 if (absolute_file_name_copy == NULL)
348 if (gl_list_nx_add_first (file_cleanup_list, absolute_file_name_copy)
351 free (absolute_file_name_copy);
358 if (mt) gl_lock_unlock (file_cleanup_list_lock);
363 /* Unregister the given ABSOLUTE_FILE_NAME as being a file that needs to be
365 Should be called when the file ABSOLUTE_FILE_NAME could not be created. */
367 unregister_temporary_file (const char *absolute_file_name)
369 bool mt = gl_multithreaded ();
371 if (mt) gl_lock_lock (file_cleanup_list_lock);
373 gl_list_t list = file_cleanup_list;
376 gl_list_node_t node = gl_list_search (list, absolute_file_name);
379 char *old_string = (char *) gl_list_node_value (list, node);
381 gl_list_remove_node (list, node);
386 if (mt) gl_lock_unlock (file_cleanup_list_lock);
389 /* Remove the given ABSOLUTE_FILE_NAME and unregister it.
390 CLEANUP_VERBOSE determines whether errors are reported to standard error.
391 Return 0 upon success, or -1 if there was some problem. */
393 cleanup_temporary_file (const char *absolute_file_name, bool cleanup_verbose)
397 err = clean_temp_unlink (absolute_file_name, cleanup_verbose);
398 unregister_temporary_file (absolute_file_name);