| 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 <err.h> |
| 28 |
#include <string.h> |
| 29 |
|
| 30 |
#include "libglnx-testlib.h" |
| 31 |
|
| 32 |
static void |
| 33 |
test_close (void) |
| 34 |
{ |
| 35 |
g_autoptr(GError) error = NULL; |
| 36 |
int errsv; |
| 37 |
int fd = -2; |
| 38 |
int fd_borrowed; |
| 39 |
|
| 40 |
g_test_summary ("Exercise glnx_close_fd"); |
| 41 |
|
| 42 |
/* Closing a non-fd is a no-op, and preserves errno. |
| 43 |
* EILSEQ is an arbitrary valid value of errno that is unlikely |
| 44 |
* to be set accidentally as a side-effect of I/O. */ |
| 45 |
g_test_message ("Closing a non-fd is a no-op and preserves errno..."); |
| 46 |
errno = EILSEQ; |
| 47 |
glnx_close_fd (&fd); |
| 48 |
errsv = errno; |
| 49 |
g_assert_cmpint (fd, ==, -1); |
| 50 |
g_assert_cmpint (errsv, ==, EILSEQ); |
| 51 |
|
| 52 |
/* Closing a valid fd really closes it, and preserves errno. */ |
| 53 |
g_test_message ("Closing a valid fd preserves errno..."); |
| 54 |
glnx_opendirat (AT_FDCWD, "/", TRUE, &fd, &error); |
| 55 |
g_assert_no_error (error); |
| 56 |
g_assert_cmpint (fd, >=, 0); |
| 57 |
fd_borrowed = fd; |
| 58 |
errno = EILSEQ; |
| 59 |
glnx_close_fd (&fd); |
| 60 |
errsv = errno; |
| 61 |
g_assert_cmpint (fd, ==, -1); |
| 62 |
g_assert_cmpint (errsv, ==, EILSEQ); |
| 63 |
_glnx_test_assert_fd_was_closed (fd_borrowed); |
| 64 |
} |
| 65 |
|
| 66 |
/* Exercise glnx_close_fd in the case where close() fails. |
| 67 |
* The only convenient way we can arrange for this to happen is to use |
| 68 |
* an invalid fd, which is a programming error. |
| 69 |
* |
| 70 |
* This function is only run under g_test_undefined(), and assumes the |
| 71 |
* implementation detail that GLib responds to that programming error |
| 72 |
* with a critical warning rather than a fatal error. */ |
| 73 |
static void |
| 74 |
test_close_ebadf_subprocess (void) |
| 75 |
{ |
| 76 |
g_autoptr(GError) error = NULL; |
| 77 |
int errsv; |
| 78 |
int fd = -2; |
| 79 |
int non_fd; |
| 80 |
|
| 81 |
/* Preparation: Open a fd, and close it, leaving non_fd set to the |
| 82 |
* file descriptor number. */ |
| 83 |
glnx_opendirat (AT_FDCWD, "/", TRUE, &fd, &error); |
| 84 |
g_assert_no_error (error); |
| 85 |
g_assert_cmpint (fd, >=, 0); |
| 86 |
close (fd); |
| 87 |
non_fd = fd; |
| 88 |
_glnx_test_assert_fd_was_closed (non_fd); |
| 89 |
|
| 90 |
/* "Closing" the non-fd provokes a critical warning. */ |
| 91 |
g_log_set_always_fatal (G_LOG_FATAL_MASK); |
| 92 |
g_log_set_fatal_mask ("GLib", G_LOG_FATAL_MASK); |
| 93 |
|
| 94 |
errno = EILSEQ; |
| 95 |
glnx_close_fd (&non_fd); |
| 96 |
errsv = errno; |
| 97 |
g_assert_cmpint (non_fd, ==, -1); |
| 98 |
|
| 99 |
/* We preserved errno. */ |
| 100 |
g_assert_cmpint (errsv, ==, EILSEQ); |
| 101 |
g_print ("Closing invalid fd preserved errno\n"); |
| 102 |
} |
| 103 |
|
| 104 |
static void |
| 105 |
test_close_ebadf (void) |
| 106 |
{ |
| 107 |
g_test_summary ("Exercise glnx_close_fd when close() fails"); |
| 108 |
|
| 109 |
/* If close() fails, it still preserves errno. |
| 110 |
* The only convenient way to make close() fail on-demand is EBADF. */ |
| 111 |
|
| 112 |
if (g_test_subprocess ()) |
| 113 |
{ |
| 114 |
test_close_ebadf_subprocess (); |
| 115 |
return; |
| 116 |
} |
| 117 |
|
| 118 |
if (g_test_undefined ()) |
| 119 |
{ |
| 120 |
g_test_message ("Closing invalid fd preserves errno..."); |
| 121 |
|
| 122 |
#if GLIB_CHECK_VERSION (2, 38, 0) |
| 123 |
g_test_trap_subprocess (NULL, 0, G_TEST_SUBPROCESS_DEFAULT); |
| 124 |
#else |
| 125 |
if (g_test_trap_fork (0, 0)) |
| 126 |
{ |
| 127 |
test_close_ebadf_subprocess (); |
| 128 |
_exit (0); |
| 129 |
} |
| 130 |
#endif |
| 131 |
|
| 132 |
g_test_trap_assert_passed (); |
| 133 |
g_test_trap_assert_stdout ("*Closing invalid fd preserved errno*"); |
| 134 |
#if !GLIB_CHECK_VERSION(2, 76, 0) |
| 135 |
/* We can assert that our backport emits this message */ |
| 136 |
g_test_trap_assert_stderr ("*_glnx_close(fd:*) failed with EBADF*"); |
| 137 |
#else |
| 138 |
/* We can't assert anything this specific about GLib's, |
| 139 |
* but as an implementation detail, it's currently very similar */ |
| 140 |
# if 0 |
| 141 |
g_test_trap_assert_stderr ("*g_close(fd:*) failed with EBADF*"); |
| 142 |
# endif |
| 143 |
#endif |
| 144 |
} |
| 145 |
else |
| 146 |
{ |
| 147 |
g_test_skip ("Can't test this without provoking undefined behaviour"); |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
static gboolean |
| 152 |
renameat_test_setup (int *out_srcfd, int *out_destfd, |
| 153 |
GError **error) |
| 154 |
{ |
| 155 |
glnx_autofd int srcfd = -1; |
| 156 |
glnx_autofd int destfd = -1; |
| 157 |
|
| 158 |
(void) glnx_shutil_rm_rf_at (AT_FDCWD, "srcdir", NULL, NULL); |
| 159 |
if (mkdir ("srcdir", 0755) < 0) |
| 160 |
err (1, "mkdir"); |
| 161 |
if (!glnx_opendirat (AT_FDCWD, "srcdir", TRUE, &srcfd, error)) |
| 162 |
return FALSE; |
| 163 |
(void) glnx_shutil_rm_rf_at (AT_FDCWD, "destdir", NULL, NULL); |
| 164 |
if (mkdir ("destdir", 0755) < 0) |
| 165 |
err (1, "mkdir"); |
| 166 |
if (!glnx_opendirat (AT_FDCWD, "destdir", TRUE, &destfd, error)) |
| 167 |
return FALSE; |
| 168 |
|
| 169 |
if (!glnx_file_replace_contents_at (srcfd, "foo", (guint8*)"foo contents", strlen ("foo contents"), |
| 170 |
GLNX_FILE_REPLACE_NODATASYNC, NULL, error)) |
| 171 |
return FALSE; |
| 172 |
if (!glnx_file_replace_contents_at (destfd, "bar", (guint8*)"bar contents", strlen ("bar contents"), |
| 173 |
GLNX_FILE_REPLACE_NODATASYNC, NULL, error)) |
| 174 |
return FALSE; |
| 175 |
|
| 176 |
*out_srcfd = srcfd; srcfd = -1; |
| 177 |
*out_destfd = destfd; destfd = -1; |
| 178 |
return TRUE; |
| 179 |
} |
| 180 |
|
| 181 |
static void |
| 182 |
test_renameat2_noreplace (void) |
| 183 |
{ |
| 184 |
_GLNX_TEST_DECLARE_ERROR(local_error, error); |
| 185 |
glnx_autofd int srcfd = -1; |
| 186 |
glnx_autofd int destfd = -1; |
| 187 |
struct stat stbuf; |
| 188 |
|
| 189 |
if (!renameat_test_setup (&srcfd, &destfd, error)) |
| 190 |
return; |
| 191 |
|
| 192 |
if (glnx_renameat2_noreplace (srcfd, "foo", destfd, "bar") == 0) |
| 193 |
g_assert_not_reached (); |
| 194 |
else |
| 195 |
{ |
| 196 |
g_assert_cmpint (errno, ==, EEXIST); |
| 197 |
} |
| 198 |
|
| 199 |
if (glnx_renameat2_noreplace (srcfd, "foo", destfd, "baz") < 0) |
| 200 |
return (void)glnx_throw_errno_prefix (error, "renameat"); |
| 201 |
if (!glnx_fstatat (destfd, "bar", &stbuf, AT_SYMLINK_NOFOLLOW, error)) |
| 202 |
return; |
| 203 |
|
| 204 |
if (fstatat (srcfd, "foo", &stbuf, AT_SYMLINK_NOFOLLOW) == 0) |
| 205 |
g_assert_not_reached (); |
| 206 |
else |
| 207 |
g_assert_cmpint (errno, ==, ENOENT); |
| 208 |
} |
| 209 |
|
| 210 |
static void |
| 211 |
test_renameat2_exchange (void) |
| 212 |
{ |
| 213 |
_GLNX_TEST_DECLARE_ERROR(local_error, error); |
| 214 |
|
| 215 |
glnx_autofd int srcfd = -1; |
| 216 |
glnx_autofd int destfd = -1; |
| 217 |
if (!renameat_test_setup (&srcfd, &destfd, error)) |
| 218 |
return; |
| 219 |
|
| 220 |
if (glnx_renameat2_exchange (AT_FDCWD, "srcdir", AT_FDCWD, "destdir") < 0) |
| 221 |
return (void)glnx_throw_errno_prefix (error, "renameat"); |
| 222 |
|
| 223 |
/* Ensure the dir fds are the same */ |
| 224 |
struct stat stbuf; |
| 225 |
if (!glnx_fstatat (srcfd, "foo", &stbuf, AT_SYMLINK_NOFOLLOW, error)) |
| 226 |
return; |
| 227 |
if (!glnx_fstatat (destfd, "bar", &stbuf, AT_SYMLINK_NOFOLLOW, error)) |
| 228 |
return; |
| 229 |
/* But the dirs should be swapped */ |
| 230 |
if (!glnx_fstatat (AT_FDCWD, "destdir/foo", &stbuf, AT_SYMLINK_NOFOLLOW, error)) |
| 231 |
return; |
| 232 |
if (!glnx_fstatat (AT_FDCWD, "srcdir/bar", &stbuf, AT_SYMLINK_NOFOLLOW, error)) |
| 233 |
return; |
| 234 |
} |
| 235 |
|
| 236 |
static void |
| 237 |
test_tmpfile (void) |
| 238 |
{ |
| 239 |
_GLNX_TEST_DECLARE_ERROR(local_error, error); |
| 240 |
|
| 241 |
g_auto(GLnxTmpfile) tmpf = { 0, }; |
| 242 |
if (!glnx_open_tmpfile_linkable_at (AT_FDCWD, ".", O_WRONLY|O_CLOEXEC, &tmpf, error)) |
| 243 |
return; |
| 244 |
if (glnx_loop_write (tmpf.fd, "foo", strlen ("foo")) < 0) |
| 245 |
return (void)glnx_throw_errno_prefix (error, "write"); |
| 246 |
if (glnx_link_tmpfile_at (&tmpf, GLNX_LINK_TMPFILE_NOREPLACE, AT_FDCWD, "foo", error)) |
| 247 |
return; |
| 248 |
} |
| 249 |
|
| 250 |
static void |
| 251 |
test_stdio_file (void) |
| 252 |
{ |
| 253 |
_GLNX_TEST_DECLARE_ERROR(local_error, error); |
| 254 |
g_auto(GLnxTmpfile) tmpf = { 0, }; |
| 255 |
g_autoptr(FILE) f = NULL; |
| 256 |
|
| 257 |
if (!glnx_open_anonymous_tmpfile (O_RDWR|O_CLOEXEC, &tmpf, error)) |
| 258 |
return; |
| 259 |
f = fdopen (tmpf.fd, "w"); |
| 260 |
tmpf.fd = -1; /* Ownership was transferred via fdopen() */ |
| 261 |
if (!f) |
| 262 |
return (void)glnx_throw_errno_prefix (error, "fdopen"); |
| 263 |
if (fwrite ("hello", 1, strlen ("hello"), f) != strlen ("hello")) |
| 264 |
return (void)glnx_throw_errno_prefix (error, "fwrite"); |
| 265 |
if (!glnx_stdio_file_flush (f, error)) |
| 266 |
return; |
| 267 |
} |
| 268 |
|
| 269 |
static void |
| 270 |
test_fstatat (void) |
| 271 |
{ |
| 272 |
_GLNX_TEST_DECLARE_ERROR(local_error, error); |
| 273 |
struct stat stbuf = { 0, }; |
| 274 |
|
| 275 |
if (!glnx_fstatat_allow_noent (AT_FDCWD, ".", &stbuf, 0, error)) |
| 276 |
return; |
| 277 |
g_assert_cmpint (errno, ==, 0); |
| 278 |
g_assert_no_error (local_error); |
| 279 |
g_assert (S_ISDIR (stbuf.st_mode)); |
| 280 |
if (!glnx_fstatat_allow_noent (AT_FDCWD, "nosuchfile", &stbuf, 0, error)) |
| 281 |
return; |
| 282 |
g_assert_cmpint (errno, ==, ENOENT); |
| 283 |
g_assert_no_error (local_error); |
| 284 |
|
| 285 |
/* test NULL parameter for stat */ |
| 286 |
if (!glnx_fstatat_allow_noent (AT_FDCWD, ".", NULL, 0, error)) |
| 287 |
return; |
| 288 |
g_assert_cmpint (errno, ==, 0); |
| 289 |
g_assert_no_error (local_error); |
| 290 |
if (!glnx_fstatat_allow_noent (AT_FDCWD, "nosuchfile", NULL, 0, error)) |
| 291 |
return; |
| 292 |
g_assert_cmpint (errno, ==, ENOENT); |
| 293 |
g_assert_no_error (local_error); |
| 294 |
} |
| 295 |
|
| 296 |
static void |
| 297 |
test_filecopy (void) |
| 298 |
{ |
| 299 |
_GLNX_TEST_DECLARE_ERROR(local_error, error); |
| 300 |
const char foo[] = "foo"; |
| 301 |
struct stat stbuf; |
| 302 |
|
| 303 |
if (!glnx_ensure_dir (AT_FDCWD, "subdir", 0755, error)) |
| 304 |
return; |
| 305 |
|
| 306 |
if (!glnx_file_replace_contents_at (AT_FDCWD, foo, (guint8*)foo, sizeof (foo), |
| 307 |
GLNX_FILE_REPLACE_NODATASYNC, NULL, error)) |
| 308 |
return; |
| 309 |
|
| 310 |
/* Copy it into both the same dir and a subdir */ |
| 311 |
if (!glnx_file_copy_at (AT_FDCWD, foo, NULL, AT_FDCWD, "bar", |
| 312 |
GLNX_FILE_COPY_NOXATTRS, NULL, error)) |
| 313 |
return; |
| 314 |
if (!glnx_file_copy_at (AT_FDCWD, foo, NULL, AT_FDCWD, "subdir/bar", |
| 315 |
GLNX_FILE_COPY_NOXATTRS, NULL, error)) |
| 316 |
return; |
| 317 |
if (!glnx_fstatat (AT_FDCWD, "subdir/bar", &stbuf, 0, error)) |
| 318 |
return; |
| 319 |
|
| 320 |
if (glnx_file_copy_at (AT_FDCWD, foo, NULL, AT_FDCWD, "bar", |
| 321 |
GLNX_FILE_COPY_NOXATTRS, NULL, error)) |
| 322 |
g_assert_not_reached (); |
| 323 |
g_assert_error (local_error, G_IO_ERROR, G_IO_ERROR_EXISTS); |
| 324 |
g_clear_error (&local_error); |
| 325 |
|
| 326 |
if (!glnx_file_copy_at (AT_FDCWD, foo, NULL, AT_FDCWD, "bar", |
| 327 |
GLNX_FILE_COPY_NOXATTRS | GLNX_FILE_COPY_OVERWRITE, |
| 328 |
NULL, error)) |
| 329 |
return; |
| 330 |
|
| 331 |
if (symlinkat ("nosuchtarget", AT_FDCWD, "link") < 0) |
| 332 |
return (void) glnx_throw_errno_prefix (error, "symlinkat"); |
| 333 |
|
| 334 |
/* Shouldn't be able to overwrite a symlink without GLNX_FILE_COPY_OVERWRITE */ |
| 335 |
if (glnx_file_copy_at (AT_FDCWD, foo, NULL, AT_FDCWD, "link", |
| 336 |
GLNX_FILE_COPY_NOXATTRS, |
| 337 |
NULL, error)) |
| 338 |
g_assert_not_reached (); |
| 339 |
g_assert_error (local_error, G_IO_ERROR, G_IO_ERROR_EXISTS); |
| 340 |
g_clear_error (&local_error); |
| 341 |
|
| 342 |
/* Test overwriting symlink */ |
| 343 |
if (!glnx_file_copy_at (AT_FDCWD, foo, NULL, AT_FDCWD, "link", |
| 344 |
GLNX_FILE_COPY_NOXATTRS | GLNX_FILE_COPY_OVERWRITE, |
| 345 |
NULL, error)) |
| 346 |
return; |
| 347 |
|
| 348 |
if (!glnx_fstatat_allow_noent (AT_FDCWD, "nosuchtarget", &stbuf, AT_SYMLINK_NOFOLLOW, error)) |
| 349 |
return; |
| 350 |
g_assert_cmpint (errno, ==, ENOENT); |
| 351 |
g_assert_no_error (local_error); |
| 352 |
|
| 353 |
if (!glnx_fstatat (AT_FDCWD, "link", &stbuf, AT_SYMLINK_NOFOLLOW, error)) |
| 354 |
return; |
| 355 |
g_assert (S_ISREG (stbuf.st_mode)); |
| 356 |
} |
| 357 |
|
| 358 |
static void |
| 359 |
test_filecopy_procfs (void) |
| 360 |
{ |
| 361 |
const char * const pseudo_files[] = |
| 362 |
{ |
| 363 |
/* A file in /proc that stat()s as empty (at least on Linux 5.15) */ |
| 364 |
"/proc/version", |
| 365 |
/* A file in /sys that stat()s as empty (at least on Linux 5.15) */ |
| 366 |
"/sys/fs/cgroup/cgroup.controllers", |
| 367 |
/* A file in /sys that stat()s as non-empty (at least on Linux 5.15) */ |
| 368 |
"/sys/fs/ext4/features/meta_bg_resize", |
| 369 |
}; |
| 370 |
gsize i; |
| 371 |
|
| 372 |
for (i = 0; i < G_N_ELEMENTS (pseudo_files); i++) |
| 373 |
{ |
| 374 |
_GLNX_TEST_DECLARE_ERROR(local_error, error); |
| 375 |
g_autofree char *contents = NULL; |
| 376 |
g_autofree char *contents_of_copy = NULL; |
| 377 |
gsize len; |
| 378 |
gsize len_copy; |
| 379 |
|
| 380 |
if (!g_file_get_contents (pseudo_files[i], &contents, &len, error)) |
| 381 |
{ |
| 382 |
g_test_message ("Not testing %s: %s", |
| 383 |
pseudo_files[i], local_error->message); |
| 384 |
g_clear_error (&local_error); |
| 385 |
continue; |
| 386 |
} |
| 387 |
|
| 388 |
if (!glnx_file_copy_at (AT_FDCWD, pseudo_files[i], NULL, |
| 389 |
AT_FDCWD, "copy", |
| 390 |
(GLNX_FILE_COPY_OVERWRITE | |
| 391 |
GLNX_FILE_COPY_NOCHOWN | |
| 392 |
GLNX_FILE_COPY_NOXATTRS), |
| 393 |
NULL, error)) |
| 394 |
return; |
| 395 |
|
| 396 |
g_assert_no_error (local_error); |
| 397 |
|
| 398 |
if (!g_file_get_contents ("copy", &contents_of_copy, &len_copy, error)) |
| 399 |
return; |
| 400 |
|
| 401 |
g_assert_no_error (local_error); |
| 402 |
|
| 403 |
g_assert_cmpstr (contents, ==, contents_of_copy); |
| 404 |
g_assert_cmpuint (len, ==, len_copy); |
| 405 |
} |
| 406 |
} |
| 407 |
|
| 408 |
static void |
| 409 |
test_name_to_handle_at (void) |
| 410 |
{ |
| 411 |
g_autoptr(GError) error = NULL; |
| 412 |
g_autofree struct file_handle *handle1 = NULL; |
| 413 |
g_autofree struct file_handle *handle2 = NULL; |
| 414 |
g_autofree struct file_handle *handle3 = NULL; |
| 415 |
uint64_t mnt_id1 = 0; |
| 416 |
uint64_t mnt_id2 = 0; |
| 417 |
uint64_t mnt_id3 = 0; |
| 418 |
glnx_autofd int dfd = -1; |
| 419 |
gboolean ok; |
| 420 |
|
| 421 |
/* Create a test directory and file */ |
| 422 |
ok = glnx_shutil_mkdir_p_at_open (AT_FDCWD, "handle_test", 0755, &dfd, NULL, &error); |
| 423 |
g_assert_no_error (error); |
| 424 |
g_assert_true (ok); |
| 425 |
|
| 426 |
ok = glnx_file_replace_contents_at (dfd, "testfile", |
| 427 |
(const guint8 *)"test", 4, |
| 428 |
GLNX_FILE_REPLACE_NODATASYNC, NULL, &error); |
| 429 |
g_assert_no_error (error); |
| 430 |
g_assert_true (ok); |
| 431 |
|
| 432 |
ok = glnx_name_to_handle_at (dfd, "testfile", 0, &handle1, &mnt_id1, &error); |
| 433 |
|
| 434 |
/* Skip the test if the syscall is not supported */ |
| 435 |
if (!ok && g_error_matches (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED)) |
| 436 |
return g_test_skip ("name_to_handle_at not supported"); |
| 437 |
|
| 438 |
/* Test 1: Get handle for a regular file */ |
| 439 |
g_assert_no_error (error); |
| 440 |
g_assert_true (ok); |
| 441 |
g_assert_nonnull (handle1); |
| 442 |
g_assert_cmpuint (mnt_id1, >, 0); |
| 443 |
|
| 444 |
/* Test 2: Get handle for the same file again - should have same handle and mount ID */ |
| 445 |
ok = glnx_name_to_handle_at (dfd, "testfile", 0, &handle2, &mnt_id2, &error); |
| 446 |
g_assert_no_error (error); |
| 447 |
g_assert_true (ok); |
| 448 |
g_assert_nonnull (handle2); |
| 449 |
g_assert_cmpuint (mnt_id1, ==, mnt_id2); |
| 450 |
g_assert_cmpuint (handle1->handle_bytes, ==, handle2->handle_bytes); |
| 451 |
g_assert_cmpuint (handle1->handle_type, ==, handle2->handle_type); |
| 452 |
g_assert_cmpmem (handle1->f_handle, handle1->handle_bytes, |
| 453 |
handle2->f_handle, handle2->handle_bytes); |
| 454 |
|
| 455 |
g_clear_pointer (&handle1, g_free); |
| 456 |
g_clear_pointer (&handle2, g_free); |
| 457 |
|
| 458 |
/* Test 3: Get handle for a directory */ |
| 459 |
ok = glnx_name_to_handle_at (AT_FDCWD, "handle_test", 0, &handle1, &mnt_id1, &error); |
| 460 |
g_assert_no_error (error); |
| 461 |
g_assert_true (ok); |
| 462 |
g_assert_nonnull (handle1); |
| 463 |
g_assert_cmpuint (mnt_id1, >, 0); |
| 464 |
|
| 465 |
g_clear_pointer (&handle1, g_free); |
| 466 |
|
| 467 |
/* Test 4: Test with AT_EMPTY_PATH */ |
| 468 |
ok = glnx_name_to_handle_at (dfd, "", AT_EMPTY_PATH, &handle1, &mnt_id1, &error); |
| 469 |
g_assert_no_error (error); |
| 470 |
g_assert_true (ok); |
| 471 |
g_assert_nonnull (handle1); |
| 472 |
g_assert_cmpuint (mnt_id1, >, 0); |
| 473 |
|
| 474 |
g_clear_pointer (&handle1, g_free); |
| 475 |
|
| 476 |
/* Test 5: Create symlink and test AT_SYMLINK_FOLLOW */ |
| 477 |
g_assert_no_errno (symlinkat ("testfile", dfd, "testlink")); |
| 478 |
|
| 479 |
ok = glnx_name_to_handle_at (dfd, "testlink", 0, &handle1, &mnt_id1, &error); |
| 480 |
g_assert_no_error (error); |
| 481 |
g_assert_true (ok); |
| 482 |
g_assert_nonnull (handle1); |
| 483 |
|
| 484 |
ok = glnx_name_to_handle_at (dfd, "testlink", AT_SYMLINK_FOLLOW, &handle2, &mnt_id2, &error); |
| 485 |
g_assert_no_error (error); |
| 486 |
g_assert_true (ok); |
| 487 |
g_assert_nonnull (handle2); |
| 488 |
|
| 489 |
/* files are on the same mount, so we should get the same kind of handle */ |
| 490 |
g_assert_true (handle1->handle_bytes == handle2->handle_bytes); |
| 491 |
g_assert_true (handle1->handle_type == handle2->handle_type); |
| 492 |
/* handle1 != handle2 */ |
| 493 |
g_assert_false (memcmp (handle1->f_handle, handle2->f_handle, handle1->handle_bytes) == 0); |
| 494 |
|
| 495 |
/* Following the symlink should give us the same handle as the target */ |
| 496 |
ok = glnx_name_to_handle_at (dfd, "testfile", 0, &handle3, &mnt_id3, &error); |
| 497 |
g_assert_no_error (error); |
| 498 |
g_assert_true (ok); |
| 499 |
g_assert_cmpmem (handle2->f_handle, handle2->handle_bytes, |
| 500 |
handle3->f_handle, handle3->handle_bytes); |
| 501 |
|
| 502 |
g_clear_pointer (&handle1, g_free); |
| 503 |
g_clear_pointer (&handle2, g_free); |
| 504 |
g_clear_pointer (&handle3, g_free); |
| 505 |
|
| 506 |
/* Test 6: Error case - non-existent file */ |
| 507 |
ok = glnx_name_to_handle_at (dfd, "nosuchfile", 0, &handle1, &mnt_id1, &error); |
| 508 |
g_assert_false (ok); |
| 509 |
g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND); |
| 510 |
g_clear_error (&error); |
| 511 |
} |
| 512 |
|
| 513 |
static void |
| 514 |
test_fd_reopen (void) |
| 515 |
{ |
| 516 |
g_autoptr(GError) error = NULL; |
| 517 |
glnx_autofd int dfd = -1; |
| 518 |
glnx_autofd int opath_fd = -1; |
| 519 |
glnx_autofd int regular_fd = -1; |
| 520 |
glnx_autofd int testfile_fd = -1; |
| 521 |
glnx_autofd int link_opath_fd = -1; |
| 522 |
glnx_autofd int reopened_fd = -1; |
| 523 |
struct stat st1, st2; |
| 524 |
const char *test_data = "test content"; |
| 525 |
char buf[100]; |
| 526 |
ssize_t n; |
| 527 |
gboolean ok; |
| 528 |
int flags; |
| 529 |
|
| 530 |
/* Create a test directory and file */ |
| 531 |
ok = glnx_shutil_mkdir_p_at_open (AT_FDCWD, "reopen_test", 0755, &dfd, NULL, &error); |
| 532 |
g_assert_no_error (error); |
| 533 |
g_assert_true (ok); |
| 534 |
g_assert_no_errno (dfd); |
| 535 |
|
| 536 |
glnx_file_replace_contents_at (dfd, "testfile", |
| 537 |
(const void *) test_data, strlen (test_data), |
| 538 |
GLNX_FILE_REPLACE_NODATASYNC, NULL, &error); |
| 539 |
g_assert_no_error (error); |
| 540 |
|
| 541 |
/* Test 1: Reopen O_PATH fd as regular fd for reading and writing */ |
| 542 |
opath_fd = openat (dfd, "testfile", O_PATH | O_CLOEXEC); |
| 543 |
g_assert_no_errno (opath_fd); |
| 544 |
|
| 545 |
regular_fd = glnx_fd_reopen (opath_fd, O_RDWR, &error); |
| 546 |
g_assert_no_errno (regular_fd); |
| 547 |
g_assert_no_error (error); |
| 548 |
|
| 549 |
flags = fcntl (regular_fd, F_GETFL); |
| 550 |
g_assert_no_errno (flags); |
| 551 |
g_assert_cmpint (flags & (O_RDONLY | O_WRONLY | O_RDWR), ==, O_RDWR); |
| 552 |
g_assert_cmpint (flags & (O_PATH | O_DIRECTORY | O_NOFOLLOW), ==, 0); |
| 553 |
flags = fcntl (regular_fd, F_GETFD); |
| 554 |
g_assert_no_errno (flags); |
| 555 |
g_assert_cmpint (flags & FD_CLOEXEC, ==, FD_CLOEXEC); |
| 556 |
|
| 557 |
/* Verify we can read from the reopened fd */ |
| 558 |
n = read (regular_fd, buf, sizeof (buf)); |
| 559 |
g_assert_cmpmem (buf, n, test_data, strlen (test_data)); |
| 560 |
|
| 561 |
g_clear_fd (®ular_fd, NULL); |
| 562 |
g_clear_fd (&opath_fd, NULL); |
| 563 |
|
| 564 |
/* Test 2: Reopen directory fd with O_DIRECTORY */ |
| 565 |
opath_fd = openat (AT_FDCWD, "reopen_test", O_PATH | O_CLOEXEC); |
| 566 |
g_assert_no_errno (opath_fd); |
| 567 |
|
| 568 |
reopened_fd = glnx_fd_reopen (opath_fd, O_RDONLY | O_DIRECTORY, &error); |
| 569 |
g_assert_no_error (error); |
| 570 |
g_assert_no_errno (reopened_fd); |
| 571 |
|
| 572 |
flags = fcntl (reopened_fd, F_GETFL); |
| 573 |
g_assert_no_errno (flags); |
| 574 |
g_assert_cmpint (flags & (O_RDONLY | O_WRONLY | O_RDWR), ==, O_RDONLY); |
| 575 |
g_assert_cmpint (flags & (O_PATH | O_DIRECTORY | O_NOFOLLOW), ==, O_DIRECTORY); |
| 576 |
flags = fcntl (reopened_fd, F_GETFD); |
| 577 |
g_assert_no_errno (flags); |
| 578 |
g_assert_cmpint (flags & FD_CLOEXEC, ==, FD_CLOEXEC); |
| 579 |
|
| 580 |
/* Verify both fds point to the same inode */ |
| 581 |
g_assert_no_errno (fstat (opath_fd, &st1)); |
| 582 |
g_assert_no_errno (fstat (reopened_fd, &st2)); |
| 583 |
g_assert_cmpint (st1.st_ino, ==, st2.st_ino); |
| 584 |
|
| 585 |
g_clear_fd (&reopened_fd, NULL); |
| 586 |
g_clear_fd (&opath_fd, NULL); |
| 587 |
|
| 588 |
/* Test 3: Reopen AT_FDCWD */ |
| 589 |
reopened_fd = glnx_fd_reopen (AT_FDCWD, O_RDONLY | O_DIRECTORY, &error); |
| 590 |
g_assert_no_error (error); |
| 591 |
g_assert_no_errno (reopened_fd); |
| 592 |
|
| 593 |
g_clear_fd (&reopened_fd, NULL); |
| 594 |
|
| 595 |
/* Test 4: Test that O_NOFOLLOW is rejected */ |
| 596 |
opath_fd = openat (dfd, "testfile", O_PATH | O_CLOEXEC); |
| 597 |
g_assert_no_errno (opath_fd); |
| 598 |
|
| 599 |
regular_fd = glnx_fd_reopen (opath_fd, O_RDONLY | O_NOFOLLOW, &error); |
| 600 |
g_assert_cmpint (regular_fd, <, 0); |
| 601 |
g_assert_error (error, G_IO_ERROR, G_IO_ERROR_TOO_MANY_LINKS); |
| 602 |
g_clear_error (&error); |
| 603 |
|
| 604 |
g_clear_fd (&opath_fd, NULL); |
| 605 |
|
| 606 |
/* Test 5: Reopen O_PATH fd to symlink with O_PATH (should work) */ |
| 607 |
g_assert_no_errno (symlinkat ("testfile", dfd, "testlink")); |
| 608 |
|
| 609 |
link_opath_fd = openat (dfd, "testlink", O_PATH | O_NOFOLLOW); |
| 610 |
g_assert_no_errno (link_opath_fd); |
| 611 |
|
| 612 |
/* Verify it's a symlink */ |
| 613 |
g_assert_no_errno (fstatat (link_opath_fd, "", &st1, AT_EMPTY_PATH | AT_SYMLINK_NOFOLLOW)); |
| 614 |
g_assert_true (S_ISLNK (st1.st_mode)); |
| 615 |
|
| 616 |
/* Reopen with O_PATH should work */ |
| 617 |
reopened_fd = glnx_fd_reopen (link_opath_fd, O_PATH, &error); |
| 618 |
g_assert_no_error (error); |
| 619 |
g_assert_no_errno (reopened_fd); |
| 620 |
|
| 621 |
flags = fcntl (reopened_fd, F_GETFL); |
| 622 |
g_assert_no_errno (flags); |
| 623 |
g_assert_cmpint (flags & (O_RDONLY | O_WRONLY | O_RDWR), ==, O_RDONLY); |
| 624 |
g_assert_cmpint (flags & (O_PATH | O_DIRECTORY | O_NOFOLLOW), ==, O_PATH); |
| 625 |
flags = fcntl (reopened_fd, F_GETFD); |
| 626 |
g_assert_no_errno (flags); |
| 627 |
g_assert_cmpint (flags & FD_CLOEXEC, ==, FD_CLOEXEC); |
| 628 |
|
| 629 |
/* Verify both point to the same symlink */ |
| 630 |
g_assert_no_errno (fstatat (reopened_fd, "", &st2, AT_EMPTY_PATH | AT_SYMLINK_NOFOLLOW)); |
| 631 |
g_assert_cmpint (st1.st_ino, ==, st2.st_ino); |
| 632 |
g_assert_true (S_ISLNK (st2.st_mode)); |
| 633 |
|
| 634 |
g_clear_fd (&reopened_fd, NULL); |
| 635 |
|
| 636 |
/* Test 6: Reopening O_PATH fd to symlink without O_PATH should fail with ELOOP */ |
| 637 |
reopened_fd = glnx_fd_reopen (link_opath_fd, O_RDONLY, &error); |
| 638 |
g_assert_cmpint (reopened_fd, <, 0); |
| 639 |
g_assert_error (error, G_IO_ERROR, G_IO_ERROR_TOO_MANY_LINKS); |
| 640 |
g_clear_error (&error); |
| 641 |
|
| 642 |
g_clear_fd (&link_opath_fd, NULL); |
| 643 |
|
| 644 |
/* Test 7: Verify read index is reset */ |
| 645 |
testfile_fd = openat (dfd, "testfile", O_RDONLY | O_CLOEXEC); |
| 646 |
g_assert_no_errno (testfile_fd); |
| 647 |
|
| 648 |
/* Read some data to advance the read index */ |
| 649 |
n = read (testfile_fd, buf, 4); |
| 650 |
g_assert_cmpint (n, ==, 4); |
| 651 |
|
| 652 |
/* Reopen should reset the read index */ |
| 653 |
reopened_fd = glnx_fd_reopen (testfile_fd, O_RDONLY, &error); |
| 654 |
g_assert_no_error (error); |
| 655 |
g_assert_no_errno (reopened_fd); |
| 656 |
|
| 657 |
/* Should read from the beginning again */ |
| 658 |
n = read (reopened_fd, buf, sizeof (buf)); |
| 659 |
g_assert_cmpmem (buf, n, test_data, strlen (test_data)); |
| 660 |
|
| 661 |
g_clear_fd (&reopened_fd, NULL); |
| 662 |
g_clear_fd (&testfile_fd, NULL); |
| 663 |
} |
| 664 |
|
| 665 |
int main (int argc, char **argv) |
| 666 |
{ |
| 667 |
_GLNX_TEST_SCOPED_TEMP_DIR; |
| 668 |
int ret; |
| 669 |
|
| 670 |
g_test_init (&argc, &argv, NULL); |
| 671 |
|
| 672 |
g_test_add_func ("/close", test_close); |
| 673 |
g_test_add_func ("/close/ebadf", test_close_ebadf); |
| 674 |
g_test_add_func ("/tmpfile", test_tmpfile); |
| 675 |
g_test_add_func ("/stdio-file", test_stdio_file); |
| 676 |
g_test_add_func ("/filecopy", test_filecopy); |
| 677 |
g_test_add_func ("/filecopy-procfs", test_filecopy_procfs); |
| 678 |
g_test_add_func ("/renameat2-noreplace", test_renameat2_noreplace); |
| 679 |
g_test_add_func ("/renameat2-exchange", test_renameat2_exchange); |
| 680 |
g_test_add_func ("/fstat", test_fstatat); |
| 681 |
g_test_add_func ("/name-to-handle-at", test_name_to_handle_at); |
| 682 |
g_test_add_func ("/fd-reopen", test_fd_reopen); |
| 683 |
|
| 684 |
ret = g_test_run(); |
| 685 |
|
| 686 |
return ret; |
| 687 |
} |