io: Fix sporadic test failures in io/tst-stat
[platform/upstream/glibc.git] / support / support.h
1 /* Common extra functions.
2    Copyright (C) 2016-2021 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9
10    The GNU C Library 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 GNU
13    Lesser General Public License for more details.
14
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, see
17    <https://www.gnu.org/licenses/>.  */
18
19 /* This header file should only contain definitions compatible with
20    C90.  (Using __attribute__ is fine because <features.h> provides a
21    fallback.)  */
22
23 #ifndef SUPPORT_H
24 #define SUPPORT_H
25
26 #include <stdbool.h>
27 #include <stddef.h>
28 #include <sys/cdefs.h>
29 /* For mode_t.  */
30 #include <sys/stat.h>
31 /* For ssize_t and off64_t.  */
32 #include <sys/types.h>
33 /* For locale_t.  */
34 #include <locale.h>
35
36 __BEGIN_DECLS
37
38 /* Write a message to standard output.  Can be used in signal
39    handlers.  */
40 void write_message (const char *message) __attribute__ ((nonnull (1)));
41
42 /* Avoid all the buffer overflow messages on stderr.  */
43 void ignore_stderr (void);
44
45 /* Set fortification error handler.  Used when tests want to verify that bad
46    code is caught by the library.  */
47 void set_fortify_handler (void (*handler) (int sig));
48
49 /* Report an out-of-memory error for the allocation of SIZE bytes in
50    FUNCTION, terminating the process.  */
51 void oom_error (const char *function, size_t size)
52   __attribute__ ((nonnull (1)));
53
54 /* Return a pointer to a memory region of SIZE bytes.  The memory is
55    initialized to zero and will be shared with subprocesses (across
56    fork).  The returned pointer must be freed using
57    support_shared_free; it is not compatible with the malloc
58    functions.  */
59 void *support_shared_allocate (size_t size);
60
61 /* Deallocate a pointer returned by support_shared_allocate.  */
62 void support_shared_free (void *);
63
64 /* Write CONTENTS to the file PATH.  Create or truncate the file as
65    needed.  The file mode is 0666 masked by the umask.  Terminate the
66    process on error.  */
67 void support_write_file_string (const char *path, const char *contents);
68
69 /* Quote the contents of the byte array starting at BLOB, of LENGTH
70    bytes, in such a way that the result string can be included in a C
71    literal (in single/double quotes, without putting the quotes into
72    the result).  */
73 char *support_quote_blob (const void *blob, size_t length);
74
75 /* Quote the contents of the string, in such a way that the result
76    string can be included in a C literal (in single/double quotes,
77    without putting the quotes into the result).  */
78 char *support_quote_string (const char *);
79
80 /* Returns non-zero if the file descriptor is a regular file on a file
81    system which supports holes (that is, seeking and writing does not
82    allocate storage for the range of zeros).  FD must refer to a
83    regular file open for writing, and initially empty.  */
84 int support_descriptor_supports_holes (int fd);
85
86 /* Error-checking wrapper functions which terminate the process on
87    error.  */
88
89 void *xmalloc (size_t) __attribute__ ((malloc));
90 void *xcalloc (size_t n, size_t s) __attribute__ ((malloc));
91 void *xrealloc (void *p, size_t n);
92 void *xposix_memalign (size_t alignment, size_t n);
93 char *xasprintf (const char *format, ...)
94   __attribute__ ((format (printf, 1, 2), malloc));
95 char *xstrdup (const char *);
96 char *xstrndup (const char *, size_t);
97 char *xsetlocale (int category, const char *locale);
98 locale_t xnewlocale (int category_mask, const char *locale, locale_t base);
99 char *xuselocale (locale_t newloc);
100
101 /* These point to the TOP of the source/build tree, not your (or
102    support's) subdirectory.  */
103 extern const char support_srcdir_root[];
104 extern const char support_objdir_root[];
105
106 /* Corresponds to the path to the runtime linker used by the testsuite,
107    e.g. OBJDIR_PATH/elf/ld-linux-x86-64.so.2  */
108 extern const char support_objdir_elf_ldso[];
109
110 /* Corresponds to the --prefix= passed to configure.  */
111 extern const char support_install_prefix[];
112 /* Corresponds to the install's lib/ or lib64/ directory.  */
113 extern const char support_libdir_prefix[];
114 /* Corresponds to the install's bin/ directory.  */
115 extern const char support_bindir_prefix[];
116 /* Corresponds to the install's sbin/ directory.  */
117 extern const char support_sbindir_prefix[];
118 /* Corresponds to the install's system /lib or /lib64 directory.  */
119 extern const char support_slibdir_prefix[];
120 /* Corresponds to the install's sbin/ directory (without prefix).  */
121 extern const char support_install_rootsbindir[];
122 /* Corresponds to the install's compiled locale directory.  */
123 extern const char support_complocaledir_prefix[];
124
125 /* Copies the file at the path FROM to TO.  If TO does not exist, it
126    is created.  If TO is a regular file, it is truncated before
127    copying.  The file mode is copied, but the permissions are not.  */
128 extern void support_copy_file (const char *from, const char *to);
129
130 extern ssize_t support_copy_file_range (int, off64_t *, int, off64_t *,
131                                         size_t, unsigned int);
132
133 /* Return true if PATH supports 64-bit time_t interfaces for file
134    operations (such as fstatat or utimensat).  */
135 extern bool support_path_support_time64_value (const char *path, int64_t at,
136                                                int64_t mt);
137 static __inline bool support_path_support_time64 (const char *path)
138 {
139   /* 1s and 2s after y2038 limit.  */
140   return support_path_support_time64_value (path, 0x80000001ULL,
141                                             0x80000002ULL);
142 }
143
144 /* Return true if stat supports nanoseconds resolution.  PATH is used
145    for tests and its ctime may change.  */
146 extern bool support_stat_nanoseconds (const char *path);
147
148 /* Return true if select modify the timeout to reflect the amount of time
149    no slept.  */
150 extern bool support_select_modifies_timeout (void);
151
152 /* Return true if select normalize the timeout input by taking in account
153    tv_usec larger than 1000000.  */
154 extern bool support_select_normalizes_timeout (void);
155
156 __END_DECLS
157
158 #endif /* SUPPORT_H */