Debian Codegraph @

subprojects/libglnx/tests/test-libglnx-xattrs.c

raw
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
2 *
3 * Copyright (C) 2017 Red Hat, Inc.
4 * SPDX-License-Identifier: LGPL-2.0-or-later
5 *
6 * This 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 of the License, or (at your option) any later version.
10 *
11 * This 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 this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
20 */
21
22 #include "libglnx-config.h"
23 #include "libglnx.h"
24 #include <glib.h>
25 #include <stdlib.h>
26 #include <gio/gio.h>
27 #include <string.h>
28
29 #define XATTR_THREAD_RUN_TIME_USECS (5 * G_USEC_PER_SEC)
30
31 struct XattrWorker {
32 int dfd;
33 gboolean is_writer;
34 guint n_attrs_read;
35 };
36
37 typedef enum {
38 WRITE_RUN_MUTATE,
39 WRITE_RUN_CREATE,
40 } WriteType;
41
42 static gboolean
43 set_random_xattr_value (int fd, const char *name, GError **error)
44 {
45 const guint8 randxattrbyte = g_random_int_range (0, 256);
46 const guint32 randxattrvalue_len = (g_random_int () % 256) + 1; /* Picked to be not too small or large */
47 g_autofree char *randxattrvalue = g_malloc (randxattrvalue_len);
48
49 memset (randxattrvalue, randxattrbyte, randxattrvalue_len);
50
51 if (fsetxattr (fd, name, randxattrvalue, randxattrvalue_len, 0) < 0)
52 {
53 glnx_set_error_from_errno (error);
54 return FALSE;
55 }
56
57 return TRUE;
58 }
59
60 static gboolean
61 add_random_xattrs (int fd, GError **error)
62 {
63 const guint nattrs = MIN (2, g_random_int () % 16);
64
65 for (guint i = 0; i < nattrs; i++)
66 {
67 guint32 randxattrname_v = g_random_int ();
68 g_autofree char *randxattrname = g_strdup_printf ("user.test%u", randxattrname_v);
69
70 if (!set_random_xattr_value (fd, randxattrname, error))
71 return FALSE;
72 }
73
74 return TRUE;
75 }
76
77 static gboolean
78 do_write_run (GLnxDirFdIterator *dfd_iter, GError **error)
79 {
80 WriteType wtype = g_random_int () % 2;
81
82 if (wtype == WRITE_RUN_CREATE)
83 {
84 guint32 randname_v = g_random_int ();
85 g_autofree char *randname = g_strdup_printf ("file%u", randname_v);
86 glnx_autofd int fd = -1;
87
88 again:
89 fd = openat (dfd_iter->fd, randname, O_CREAT | O_EXCL, 0644);
90 if (fd < 0)
91 {
92 if (errno == EEXIST)
93 {
94 g_printerr ("Congratulations! I suggest purchasing a lottery ticket today!\n");
95 goto again;
96 }
97 else
98 {
99 glnx_set_error_from_errno (error);
100 return FALSE;
101 }
102 }
103
104 if (!add_random_xattrs (fd, error))
105 return FALSE;
106 }
107 else if (wtype == WRITE_RUN_MUTATE)
108 {
109 while (TRUE)
110 {
111 struct dirent *dent;
112 if (!glnx_dirfd_iterator_next_dent (dfd_iter, &dent, NULL, error))
113 return FALSE;
114 if (!dent)
115 break;
116
117 glnx_autofd int fd = -1;
118 if (!glnx_openat_rdonly (dfd_iter->fd, dent->d_name, FALSE, &fd, error))
119 return FALSE;
120
121 g_autoptr(GVariant) current_xattrs = NULL;
122 if (!glnx_fd_get_all_xattrs (fd, &current_xattrs, NULL, error))
123 return FALSE;
124
125 for (size_t i = 0; i < g_variant_n_children (current_xattrs); i++)
126 {
127 const char *name, *value;
128 g_variant_get_child (current_xattrs, i, "(^&ay^&ay)", &name, &value);
129
130 /* We don't want to potentially test/change xattrs like security.selinux
131 * that were injected by the system.
132 */
133 if (!g_str_has_prefix (name, "user.test"))
134 continue;
135
136 if (!set_random_xattr_value (fd, name, error))
137 return FALSE;
138 }
139 }
140 }
141 else
142 g_assert_not_reached ();
143
144 return TRUE;
145 }
146
147 static gboolean
148 do_read_run (GLnxDirFdIterator *dfd_iter,
149 guint *out_n_read,
150 GError **error)
151 {
152 guint nattrs = 0;
153 while (TRUE)
154 {
155 struct dirent *dent;
156 if (!glnx_dirfd_iterator_next_dent (dfd_iter, &dent, NULL, error))
157 return FALSE;
158 if (!dent)
159 break;
160
161 glnx_autofd int fd = -1;
162 if (!glnx_openat_rdonly (dfd_iter->fd, dent->d_name, FALSE, &fd, error))
163 return FALSE;
164
165 g_autoptr(GVariant) current_xattrs = NULL;
166 if (!glnx_fd_get_all_xattrs (fd, &current_xattrs, NULL, error))
167 return FALSE;
168
169 /* We don't actually care about the values, just use the variable
170 * to avoid compiler warnings.
171 */
172 nattrs += g_variant_n_children (current_xattrs);
173 }
174
175 *out_n_read = nattrs;
176 return TRUE;
177 }
178
179 static gpointer
180 xattr_thread (gpointer data)
181 {
182 g_autoptr(GError) local_error = NULL;
183 GError **error = &local_error;
184 struct XattrWorker *worker = data;
185 gint64 end_time = g_get_monotonic_time () + XATTR_THREAD_RUN_TIME_USECS;
186 guint n_read = 0;
187
188 while (g_get_monotonic_time () < end_time)
189 {
190 g_auto(GLnxDirFdIterator) dfd_iter = { 0, };
191
192 if (!glnx_dirfd_iterator_init_at (worker->dfd, ".", TRUE, &dfd_iter, error))
193 goto out;
194
195 if (worker->is_writer)
196 {
197 if (!do_write_run (&dfd_iter, error))
198 goto out;
199 }
200 else
201 {
202 if (!do_read_run (&dfd_iter, &n_read, error))
203 goto out;
204 }
205 }
206
207 out:
208 g_assert_no_error (local_error);
209
210 return GINT_TO_POINTER (n_read);
211 }
212
213 static void
214 test_xattr_races (void)
215 {
216 /* If for some reason we're built in a VM which only has one vcpu, let's still
217 * at least make the test do something.
218 */
219 /* FIXME - this deadlocks for me on 4.9.4-201.fc25.x86_64, whether
220 * using overlayfs or xfs as source/dest.
221 */
222 #if GLIB_CHECK_VERSION (2, 36, 0)
223 const guint nprocs = MAX (4, g_get_num_processors ());
224 #else
225 const guint nprocs = 4;
226 #endif
227 struct XattrWorker wdata[nprocs];
228 GThread *threads[nprocs];
229 g_autoptr(GError) local_error = NULL;
230 GError **error = &local_error;
231 g_auto(GLnxTmpDir) tmpdir = { 0, };
232 g_autofree char *tmpdir_path = g_strdup_printf ("%s/libglnx-xattrs-XXXXXX",
233 getenv ("TMPDIR") ?: "/var/tmp");
234 guint nread = 0;
235
236 if (!glnx_mkdtempat (AT_FDCWD, tmpdir_path, 0700,
237 &tmpdir, error))
238 goto out;
239
240 /* Support people building/testing on tmpfs https://github.com/flatpak/flatpak/issues/686 */
241 if (fsetxattr (tmpdir.fd, "user.test", "novalue", strlen ("novalue"), 0) < 0)
242 {
243 if (errno == EOPNOTSUPP)
244 {
245 g_test_skip ("no xattr support");
246 return;
247 }
248 else
249 {
250 glnx_set_error_from_errno (error);
251 goto out;
252 }
253 }
254
255 for (guint i = 0; i < nprocs; i++)
256 {
257 struct XattrWorker *worker = &wdata[i];
258 worker->dfd = tmpdir.fd;
259 worker->is_writer = i % 2 == 0;
260 threads[i] = g_thread_new (NULL, xattr_thread, worker);
261 }
262
263 for (guint i = 0; i < nprocs; i++)
264 {
265 if (wdata[i].is_writer)
266 (void) g_thread_join (threads[i]);
267 else
268 nread += GPOINTER_TO_UINT (g_thread_join (threads[i]));
269 }
270
271 g_print ("Read %u xattrs race free!\n", nread);
272
273 out:
274 g_assert_no_error (local_error);
275 }
276
277 int main (int argc, char **argv)
278 {
279 int ret;
280
281 g_test_init (&argc, &argv, NULL);
282
283 g_test_add_func ("/xattr-races", test_xattr_races);
284
285 ret = g_test_run();
286
287 return ret;
288 }