| 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 gboolean |
| 33 |
renameat_test_setup (int *out_srcfd, int *out_destfd, |
| 34 |
GError **error) |
| 35 |
{ |
| 36 |
glnx_autofd int srcfd = -1; |
| 37 |
glnx_autofd int destfd = -1; |
| 38 |
|
| 39 |
(void) glnx_shutil_rm_rf_at (AT_FDCWD, "srcdir", NULL, NULL); |
| 40 |
if (mkdir ("srcdir", 0755) < 0) |
| 41 |
err (1, "mkdir"); |
| 42 |
if (!glnx_opendirat (AT_FDCWD, "srcdir", TRUE, &srcfd, error)) |
| 43 |
return FALSE; |
| 44 |
(void) glnx_shutil_rm_rf_at (AT_FDCWD, "destdir", NULL, NULL); |
| 45 |
if (mkdir ("destdir", 0755) < 0) |
| 46 |
err (1, "mkdir"); |
| 47 |
if (!glnx_opendirat (AT_FDCWD, "destdir", TRUE, &destfd, error)) |
| 48 |
return FALSE; |
| 49 |
|
| 50 |
if (!glnx_file_replace_contents_at (srcfd, "foo", (guint8*)"foo contents", strlen ("foo contents"), |
| 51 |
GLNX_FILE_REPLACE_NODATASYNC, NULL, error)) |
| 52 |
return FALSE; |
| 53 |
if (!glnx_file_replace_contents_at (destfd, "bar", (guint8*)"bar contents", strlen ("bar contents"), |
| 54 |
GLNX_FILE_REPLACE_NODATASYNC, NULL, error)) |
| 55 |
return FALSE; |
| 56 |
|
| 57 |
*out_srcfd = srcfd; srcfd = -1; |
| 58 |
*out_destfd = destfd; destfd = -1; |
| 59 |
return TRUE; |
| 60 |
} |
| 61 |
|
| 62 |
static void |
| 63 |
test_renameat2_noreplace (void) |
| 64 |
{ |
| 65 |
_GLNX_TEST_DECLARE_ERROR(local_error, error); |
| 66 |
glnx_autofd int srcfd = -1; |
| 67 |
glnx_autofd int destfd = -1; |
| 68 |
struct stat stbuf; |
| 69 |
|
| 70 |
if (!renameat_test_setup (&srcfd, &destfd, error)) |
| 71 |
return; |
| 72 |
|
| 73 |
if (glnx_renameat2_noreplace (srcfd, "foo", destfd, "bar") == 0) |
| 74 |
g_assert_not_reached (); |
| 75 |
else |
| 76 |
{ |
| 77 |
g_assert_cmpint (errno, ==, EEXIST); |
| 78 |
} |
| 79 |
|
| 80 |
if (glnx_renameat2_noreplace (srcfd, "foo", destfd, "baz") < 0) |
| 81 |
return (void)glnx_throw_errno_prefix (error, "renameat"); |
| 82 |
if (!glnx_fstatat (destfd, "bar", &stbuf, AT_SYMLINK_NOFOLLOW, error)) |
| 83 |
return; |
| 84 |
|
| 85 |
if (fstatat (srcfd, "foo", &stbuf, AT_SYMLINK_NOFOLLOW) == 0) |
| 86 |
g_assert_not_reached (); |
| 87 |
else |
| 88 |
g_assert_cmpint (errno, ==, ENOENT); |
| 89 |
} |
| 90 |
|
| 91 |
static void |
| 92 |
test_renameat2_exchange (void) |
| 93 |
{ |
| 94 |
_GLNX_TEST_DECLARE_ERROR(local_error, error); |
| 95 |
|
| 96 |
glnx_autofd int srcfd = -1; |
| 97 |
glnx_autofd int destfd = -1; |
| 98 |
if (!renameat_test_setup (&srcfd, &destfd, error)) |
| 99 |
return; |
| 100 |
|
| 101 |
if (glnx_renameat2_exchange (AT_FDCWD, "srcdir", AT_FDCWD, "destdir") < 0) |
| 102 |
return (void)glnx_throw_errno_prefix (error, "renameat"); |
| 103 |
|
| 104 |
/* Ensure the dir fds are the same */ |
| 105 |
struct stat stbuf; |
| 106 |
if (!glnx_fstatat (srcfd, "foo", &stbuf, AT_SYMLINK_NOFOLLOW, error)) |
| 107 |
return; |
| 108 |
if (!glnx_fstatat (destfd, "bar", &stbuf, AT_SYMLINK_NOFOLLOW, error)) |
| 109 |
return; |
| 110 |
/* But the dirs should be swapped */ |
| 111 |
if (!glnx_fstatat (AT_FDCWD, "destdir/foo", &stbuf, AT_SYMLINK_NOFOLLOW, error)) |
| 112 |
return; |
| 113 |
if (!glnx_fstatat (AT_FDCWD, "srcdir/bar", &stbuf, AT_SYMLINK_NOFOLLOW, error)) |
| 114 |
return; |
| 115 |
} |
| 116 |
|
| 117 |
static void |
| 118 |
test_tmpfile (void) |
| 119 |
{ |
| 120 |
_GLNX_TEST_DECLARE_ERROR(local_error, error); |
| 121 |
|
| 122 |
g_auto(GLnxTmpfile) tmpf = { 0, }; |
| 123 |
if (!glnx_open_tmpfile_linkable_at (AT_FDCWD, ".", O_WRONLY|O_CLOEXEC, &tmpf, error)) |
| 124 |
return; |
| 125 |
if (glnx_loop_write (tmpf.fd, "foo", strlen ("foo")) < 0) |
| 126 |
return (void)glnx_throw_errno_prefix (error, "write"); |
| 127 |
if (glnx_link_tmpfile_at (&tmpf, GLNX_LINK_TMPFILE_NOREPLACE, AT_FDCWD, "foo", error)) |
| 128 |
return; |
| 129 |
} |
| 130 |
|
| 131 |
static void |
| 132 |
test_stdio_file (void) |
| 133 |
{ |
| 134 |
_GLNX_TEST_DECLARE_ERROR(local_error, error); |
| 135 |
g_auto(GLnxTmpfile) tmpf = { 0, }; |
| 136 |
g_autoptr(FILE) f = NULL; |
| 137 |
|
| 138 |
if (!glnx_open_anonymous_tmpfile (O_RDWR|O_CLOEXEC, &tmpf, error)) |
| 139 |
return; |
| 140 |
f = fdopen (tmpf.fd, "w"); |
| 141 |
tmpf.fd = -1; /* Ownership was transferred via fdopen() */ |
| 142 |
if (!f) |
| 143 |
return (void)glnx_throw_errno_prefix (error, "fdopen"); |
| 144 |
if (fwrite ("hello", 1, strlen ("hello"), f) != strlen ("hello")) |
| 145 |
return (void)glnx_throw_errno_prefix (error, "fwrite"); |
| 146 |
if (!glnx_stdio_file_flush (f, error)) |
| 147 |
return; |
| 148 |
} |
| 149 |
|
| 150 |
static void |
| 151 |
test_fstatat (void) |
| 152 |
{ |
| 153 |
_GLNX_TEST_DECLARE_ERROR(local_error, error); |
| 154 |
struct stat stbuf = { 0, }; |
| 155 |
|
| 156 |
if (!glnx_fstatat_allow_noent (AT_FDCWD, ".", &stbuf, 0, error)) |
| 157 |
return; |
| 158 |
g_assert_cmpint (errno, ==, 0); |
| 159 |
g_assert_no_error (local_error); |
| 160 |
g_assert (S_ISDIR (stbuf.st_mode)); |
| 161 |
if (!glnx_fstatat_allow_noent (AT_FDCWD, "nosuchfile", &stbuf, 0, error)) |
| 162 |
return; |
| 163 |
g_assert_cmpint (errno, ==, ENOENT); |
| 164 |
g_assert_no_error (local_error); |
| 165 |
|
| 166 |
/* test NULL parameter for stat */ |
| 167 |
if (!glnx_fstatat_allow_noent (AT_FDCWD, ".", NULL, 0, error)) |
| 168 |
return; |
| 169 |
g_assert_cmpint (errno, ==, 0); |
| 170 |
g_assert_no_error (local_error); |
| 171 |
if (!glnx_fstatat_allow_noent (AT_FDCWD, "nosuchfile", NULL, 0, error)) |
| 172 |
return; |
| 173 |
g_assert_cmpint (errno, ==, ENOENT); |
| 174 |
g_assert_no_error (local_error); |
| 175 |
} |
| 176 |
|
| 177 |
static void |
| 178 |
test_filecopy (void) |
| 179 |
{ |
| 180 |
_GLNX_TEST_DECLARE_ERROR(local_error, error); |
| 181 |
const char foo[] = "foo"; |
| 182 |
struct stat stbuf; |
| 183 |
|
| 184 |
if (!glnx_ensure_dir (AT_FDCWD, "subdir", 0755, error)) |
| 185 |
return; |
| 186 |
|
| 187 |
if (!glnx_file_replace_contents_at (AT_FDCWD, foo, (guint8*)foo, sizeof (foo), |
| 188 |
GLNX_FILE_REPLACE_NODATASYNC, NULL, error)) |
| 189 |
return; |
| 190 |
|
| 191 |
/* Copy it into both the same dir and a subdir */ |
| 192 |
if (!glnx_file_copy_at (AT_FDCWD, foo, NULL, AT_FDCWD, "bar", |
| 193 |
GLNX_FILE_COPY_NOXATTRS, NULL, error)) |
| 194 |
return; |
| 195 |
if (!glnx_file_copy_at (AT_FDCWD, foo, NULL, AT_FDCWD, "subdir/bar", |
| 196 |
GLNX_FILE_COPY_NOXATTRS, NULL, error)) |
| 197 |
return; |
| 198 |
if (!glnx_fstatat (AT_FDCWD, "subdir/bar", &stbuf, 0, error)) |
| 199 |
return; |
| 200 |
|
| 201 |
if (glnx_file_copy_at (AT_FDCWD, foo, NULL, AT_FDCWD, "bar", |
| 202 |
GLNX_FILE_COPY_NOXATTRS, NULL, error)) |
| 203 |
g_assert_not_reached (); |
| 204 |
g_assert_error (local_error, G_IO_ERROR, G_IO_ERROR_EXISTS); |
| 205 |
g_clear_error (&local_error); |
| 206 |
|
| 207 |
if (!glnx_file_copy_at (AT_FDCWD, foo, NULL, AT_FDCWD, "bar", |
| 208 |
GLNX_FILE_COPY_NOXATTRS | GLNX_FILE_COPY_OVERWRITE, |
| 209 |
NULL, error)) |
| 210 |
return; |
| 211 |
|
| 212 |
if (symlinkat ("nosuchtarget", AT_FDCWD, "link") < 0) |
| 213 |
return (void) glnx_throw_errno_prefix (error, "symlinkat"); |
| 214 |
|
| 215 |
/* Shouldn't be able to overwrite a symlink without GLNX_FILE_COPY_OVERWRITE */ |
| 216 |
if (glnx_file_copy_at (AT_FDCWD, foo, NULL, AT_FDCWD, "link", |
| 217 |
GLNX_FILE_COPY_NOXATTRS, |
| 218 |
NULL, error)) |
| 219 |
g_assert_not_reached (); |
| 220 |
g_assert_error (local_error, G_IO_ERROR, G_IO_ERROR_EXISTS); |
| 221 |
g_clear_error (&local_error); |
| 222 |
|
| 223 |
/* Test overwriting symlink */ |
| 224 |
if (!glnx_file_copy_at (AT_FDCWD, foo, NULL, AT_FDCWD, "link", |
| 225 |
GLNX_FILE_COPY_NOXATTRS | GLNX_FILE_COPY_OVERWRITE, |
| 226 |
NULL, error)) |
| 227 |
return; |
| 228 |
|
| 229 |
if (!glnx_fstatat_allow_noent (AT_FDCWD, "nosuchtarget", &stbuf, AT_SYMLINK_NOFOLLOW, error)) |
| 230 |
return; |
| 231 |
g_assert_cmpint (errno, ==, ENOENT); |
| 232 |
g_assert_no_error (local_error); |
| 233 |
|
| 234 |
if (!glnx_fstatat (AT_FDCWD, "link", &stbuf, AT_SYMLINK_NOFOLLOW, error)) |
| 235 |
return; |
| 236 |
g_assert (S_ISREG (stbuf.st_mode)); |
| 237 |
} |
| 238 |
|
| 239 |
static void |
| 240 |
test_filecopy_procfs (void) |
| 241 |
{ |
| 242 |
const char * const pseudo_files[] = |
| 243 |
{ |
| 244 |
/* A file in /proc that stat()s as empty (at least on Linux 5.15) */ |
| 245 |
"/proc/version", |
| 246 |
/* A file in /sys that stat()s as empty (at least on Linux 5.15) */ |
| 247 |
"/sys/fs/cgroup/cgroup.controllers", |
| 248 |
/* A file in /sys that stat()s as non-empty (at least on Linux 5.15) */ |
| 249 |
"/sys/fs/ext4/features/meta_bg_resize", |
| 250 |
}; |
| 251 |
gsize i; |
| 252 |
|
| 253 |
for (i = 0; i < G_N_ELEMENTS (pseudo_files); i++) |
| 254 |
{ |
| 255 |
_GLNX_TEST_DECLARE_ERROR(local_error, error); |
| 256 |
g_autofree char *contents = NULL; |
| 257 |
g_autofree char *contents_of_copy = NULL; |
| 258 |
gsize len; |
| 259 |
gsize len_copy; |
| 260 |
|
| 261 |
if (!g_file_get_contents (pseudo_files[i], &contents, &len, error)) |
| 262 |
{ |
| 263 |
g_test_message ("Not testing %s: %s", |
| 264 |
pseudo_files[i], local_error->message); |
| 265 |
g_clear_error (&local_error); |
| 266 |
continue; |
| 267 |
} |
| 268 |
|
| 269 |
if (!glnx_file_copy_at (AT_FDCWD, pseudo_files[i], NULL, |
| 270 |
AT_FDCWD, "copy", |
| 271 |
(GLNX_FILE_COPY_OVERWRITE | |
| 272 |
GLNX_FILE_COPY_NOCHOWN | |
| 273 |
GLNX_FILE_COPY_NOXATTRS), |
| 274 |
NULL, error)) |
| 275 |
return; |
| 276 |
|
| 277 |
g_assert_no_error (local_error); |
| 278 |
|
| 279 |
if (!g_file_get_contents ("copy", &contents_of_copy, &len_copy, error)) |
| 280 |
return; |
| 281 |
|
| 282 |
g_assert_no_error (local_error); |
| 283 |
|
| 284 |
g_assert_cmpstr (contents, ==, contents_of_copy); |
| 285 |
g_assert_cmpuint (len, ==, len_copy); |
| 286 |
} |
| 287 |
} |
| 288 |
|
| 289 |
static void |
| 290 |
test_fd_reopen (void) |
| 291 |
{ |
| 292 |
g_autoptr(GError) error = NULL; |
| 293 |
glnx_autofd int dfd = -1; |
| 294 |
glnx_autofd int opath_fd = -1; |
| 295 |
glnx_autofd int regular_fd = -1; |
| 296 |
glnx_autofd int testfile_fd = -1; |
| 297 |
glnx_autofd int link_opath_fd = -1; |
| 298 |
glnx_autofd int reopened_fd = -1; |
| 299 |
struct stat st1, st2; |
| 300 |
const char *test_data = "test content"; |
| 301 |
char buf[100]; |
| 302 |
ssize_t n; |
| 303 |
gboolean ok; |
| 304 |
int flags; |
| 305 |
|
| 306 |
/* Create a test directory and file */ |
| 307 |
ok = glnx_shutil_mkdir_p_at_open (AT_FDCWD, "reopen_test", 0755, &dfd, NULL, &error); |
| 308 |
g_assert_no_error (error); |
| 309 |
g_assert_true (ok); |
| 310 |
g_assert_no_errno (dfd); |
| 311 |
|
| 312 |
glnx_file_replace_contents_at (dfd, "testfile", |
| 313 |
(const void *) test_data, strlen (test_data), |
| 314 |
GLNX_FILE_REPLACE_NODATASYNC, NULL, &error); |
| 315 |
g_assert_no_error (error); |
| 316 |
|
| 317 |
/* Test 1: Reopen O_PATH fd as regular fd for reading and writing */ |
| 318 |
opath_fd = openat (dfd, "testfile", O_PATH | O_CLOEXEC); |
| 319 |
g_assert_no_errno (opath_fd); |
| 320 |
|
| 321 |
regular_fd = glnx_fd_reopen (opath_fd, O_RDWR, &error); |
| 322 |
g_assert_no_errno (regular_fd); |
| 323 |
g_assert_no_error (error); |
| 324 |
|
| 325 |
flags = fcntl (regular_fd, F_GETFL); |
| 326 |
g_assert_no_errno (flags); |
| 327 |
g_assert_cmpint (flags & (O_RDONLY | O_WRONLY | O_RDWR), ==, O_RDWR); |
| 328 |
g_assert_cmpint (flags & (O_PATH | O_DIRECTORY | O_NOFOLLOW), ==, 0); |
| 329 |
flags = fcntl (regular_fd, F_GETFD); |
| 330 |
g_assert_no_errno (flags); |
| 331 |
g_assert_cmpint (flags & FD_CLOEXEC, ==, FD_CLOEXEC); |
| 332 |
|
| 333 |
/* Verify we can read from the reopened fd */ |
| 334 |
n = read (regular_fd, buf, sizeof (buf)); |
| 335 |
g_assert_cmpmem (buf, n, test_data, strlen (test_data)); |
| 336 |
|
| 337 |
g_clear_fd (®ular_fd, NULL); |
| 338 |
g_clear_fd (&opath_fd, NULL); |
| 339 |
|
| 340 |
/* Test 2: Reopen directory fd with O_DIRECTORY */ |
| 341 |
opath_fd = openat (AT_FDCWD, "reopen_test", O_PATH | O_CLOEXEC); |
| 342 |
g_assert_no_errno (opath_fd); |
| 343 |
|
| 344 |
reopened_fd = glnx_fd_reopen (opath_fd, O_RDONLY | O_DIRECTORY, &error); |
| 345 |
g_assert_no_error (error); |
| 346 |
g_assert_no_errno (reopened_fd); |
| 347 |
|
| 348 |
flags = fcntl (reopened_fd, F_GETFL); |
| 349 |
g_assert_no_errno (flags); |
| 350 |
g_assert_cmpint (flags & (O_RDONLY | O_WRONLY | O_RDWR), ==, O_RDONLY); |
| 351 |
g_assert_cmpint (flags & (O_PATH | O_DIRECTORY | O_NOFOLLOW), ==, O_DIRECTORY); |
| 352 |
flags = fcntl (reopened_fd, F_GETFD); |
| 353 |
g_assert_no_errno (flags); |
| 354 |
g_assert_cmpint (flags & FD_CLOEXEC, ==, FD_CLOEXEC); |
| 355 |
|
| 356 |
/* Verify both fds point to the same inode */ |
| 357 |
g_assert_no_errno (fstat (opath_fd, &st1)); |
| 358 |
g_assert_no_errno (fstat (reopened_fd, &st2)); |
| 359 |
g_assert_cmpint (st1.st_ino, ==, st2.st_ino); |
| 360 |
|
| 361 |
g_clear_fd (&reopened_fd, NULL); |
| 362 |
g_clear_fd (&opath_fd, NULL); |
| 363 |
|
| 364 |
/* Test 3: Reopen AT_FDCWD */ |
| 365 |
reopened_fd = glnx_fd_reopen (AT_FDCWD, O_RDONLY | O_DIRECTORY, &error); |
| 366 |
g_assert_no_error (error); |
| 367 |
g_assert_no_errno (reopened_fd); |
| 368 |
|
| 369 |
g_clear_fd (&reopened_fd, NULL); |
| 370 |
|
| 371 |
/* Test 4: Test that O_NOFOLLOW is rejected */ |
| 372 |
opath_fd = openat (dfd, "testfile", O_PATH | O_CLOEXEC); |
| 373 |
g_assert_no_errno (opath_fd); |
| 374 |
|
| 375 |
regular_fd = glnx_fd_reopen (opath_fd, O_RDONLY | O_NOFOLLOW, &error); |
| 376 |
g_assert_cmpint (regular_fd, <, 0); |
| 377 |
g_assert_error (error, G_IO_ERROR, G_IO_ERROR_TOO_MANY_LINKS); |
| 378 |
g_clear_error (&error); |
| 379 |
|
| 380 |
g_clear_fd (&opath_fd, NULL); |
| 381 |
|
| 382 |
/* Test 5: Reopen O_PATH fd to symlink with O_PATH (should work) */ |
| 383 |
g_assert_no_errno (symlinkat ("testfile", dfd, "testlink")); |
| 384 |
|
| 385 |
link_opath_fd = openat (dfd, "testlink", O_PATH | O_NOFOLLOW); |
| 386 |
g_assert_no_errno (link_opath_fd); |
| 387 |
|
| 388 |
/* Verify it's a symlink */ |
| 389 |
g_assert_no_errno (fstatat (link_opath_fd, "", &st1, AT_EMPTY_PATH | AT_SYMLINK_NOFOLLOW)); |
| 390 |
g_assert_true (S_ISLNK (st1.st_mode)); |
| 391 |
|
| 392 |
/* Reopen with O_PATH should work */ |
| 393 |
reopened_fd = glnx_fd_reopen (link_opath_fd, O_PATH, &error); |
| 394 |
g_assert_no_error (error); |
| 395 |
g_assert_no_errno (reopened_fd); |
| 396 |
|
| 397 |
flags = fcntl (reopened_fd, F_GETFL); |
| 398 |
g_assert_no_errno (flags); |
| 399 |
g_assert_cmpint (flags & (O_RDONLY | O_WRONLY | O_RDWR), ==, O_RDONLY); |
| 400 |
g_assert_cmpint (flags & (O_PATH | O_DIRECTORY | O_NOFOLLOW), ==, O_PATH); |
| 401 |
flags = fcntl (reopened_fd, F_GETFD); |
| 402 |
g_assert_no_errno (flags); |
| 403 |
g_assert_cmpint (flags & FD_CLOEXEC, ==, FD_CLOEXEC); |
| 404 |
|
| 405 |
/* Verify both point to the same symlink */ |
| 406 |
g_assert_no_errno (fstatat (reopened_fd, "", &st2, AT_EMPTY_PATH | AT_SYMLINK_NOFOLLOW)); |
| 407 |
g_assert_cmpint (st1.st_ino, ==, st2.st_ino); |
| 408 |
g_assert_true (S_ISLNK (st2.st_mode)); |
| 409 |
|
| 410 |
g_clear_fd (&reopened_fd, NULL); |
| 411 |
|
| 412 |
/* Test 6: Reopening O_PATH fd to symlink without O_PATH should fail with ELOOP */ |
| 413 |
reopened_fd = glnx_fd_reopen (link_opath_fd, O_RDONLY, &error); |
| 414 |
g_assert_cmpint (reopened_fd, <, 0); |
| 415 |
g_assert_error (error, G_IO_ERROR, G_IO_ERROR_TOO_MANY_LINKS); |
| 416 |
g_clear_error (&error); |
| 417 |
|
| 418 |
g_clear_fd (&link_opath_fd, NULL); |
| 419 |
|
| 420 |
/* Test 7: Verify read index is reset */ |
| 421 |
testfile_fd = openat (dfd, "testfile", O_RDONLY | O_CLOEXEC); |
| 422 |
g_assert_no_errno (testfile_fd); |
| 423 |
|
| 424 |
/* Read some data to advance the read index */ |
| 425 |
n = read (testfile_fd, buf, 4); |
| 426 |
g_assert_cmpint (n, ==, 4); |
| 427 |
|
| 428 |
/* Reopen should reset the read index */ |
| 429 |
reopened_fd = glnx_fd_reopen (testfile_fd, O_RDONLY, &error); |
| 430 |
g_assert_no_error (error); |
| 431 |
g_assert_no_errno (reopened_fd); |
| 432 |
|
| 433 |
/* Should read from the beginning again */ |
| 434 |
n = read (reopened_fd, buf, sizeof (buf)); |
| 435 |
g_assert_cmpmem (buf, n, test_data, strlen (test_data)); |
| 436 |
|
| 437 |
g_clear_fd (&reopened_fd, NULL); |
| 438 |
g_clear_fd (&testfile_fd, NULL); |
| 439 |
} |
| 440 |
|
| 441 |
int main (int argc, char **argv) |
| 442 |
{ |
| 443 |
_GLNX_TEST_SCOPED_TEMP_DIR; |
| 444 |
int ret; |
| 445 |
|
| 446 |
g_test_init (&argc, &argv, NULL); |
| 447 |
|
| 448 |
g_test_add_func ("/tmpfile", test_tmpfile); |
| 449 |
g_test_add_func ("/stdio-file", test_stdio_file); |
| 450 |
g_test_add_func ("/filecopy", test_filecopy); |
| 451 |
g_test_add_func ("/filecopy-procfs", test_filecopy_procfs); |
| 452 |
g_test_add_func ("/renameat2-noreplace", test_renameat2_noreplace); |
| 453 |
g_test_add_func ("/renameat2-exchange", test_renameat2_exchange); |
| 454 |
g_test_add_func ("/fstat", test_fstatat); |
| 455 |
g_test_add_func ("/fd-reopen", test_fd_reopen); |
| 456 |
|
| 457 |
ret = g_test_run(); |
| 458 |
|
| 459 |
return ret; |
| 460 |
} |