| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <fcntl.h> |
| 4 |
#include <stdio.h> |
| 5 |
#include <unistd.h> |
| 6 |
|
| 7 |
#include "alloc-util.h" |
| 8 |
#include "errno-util.h" |
| 9 |
#include "fd-util.h" |
| 10 |
#include "fileio.h" |
| 11 |
#include "fs-util.h" |
| 12 |
#include "iovec-util.h" |
| 13 |
#include "memfd-util.h" |
| 14 |
#include "parse-util.h" |
| 15 |
#include "path-util.h" |
| 16 |
#include "pidref.h" |
| 17 |
#include "process-util.h" |
| 18 |
#include "random-util.h" |
| 19 |
#include "rm-rf.h" |
| 20 |
#include "socket-util.h" |
| 21 |
#include "string-util.h" |
| 22 |
#include "strv.h" |
| 23 |
#include "tests.h" |
| 24 |
#include "tmpfile-util.h" |
| 25 |
|
| 26 |
TEST(script_get_shebang_interpreter) { |
| 27 |
_cleanup_(unlink_tempfilep) char t[] = "/tmp/test-fileio-XXXXXX"; |
| 28 |
_cleanup_fclose_ FILE *f = NULL; |
| 29 |
_cleanup_free_ char *command = NULL; |
| 30 |
|
| 31 |
ASSERT_OK(fmkostemp_safe(t, "w", &f)); |
| 32 |
fputs("#! /bin/script -a -b \ngoo goo", f); |
| 33 |
fflush(f); |
| 34 |
|
| 35 |
ASSERT_OK(script_get_shebang_interpreter(t, &command)); |
| 36 |
ASSERT_STREQ(command, "/bin/script"); |
| 37 |
command = mfree(command); |
| 38 |
|
| 39 |
ASSERT_ERROR(script_get_shebang_interpreter("/bin/sh", NULL), EMEDIUMTYPE); |
| 40 |
|
| 41 |
if (script_get_shebang_interpreter("/usr/bin/yum", &command) >= 0) |
| 42 |
ASSERT_NOT_NULL(startswith(command, "/")); |
| 43 |
} |
| 44 |
|
| 45 |
TEST(get_proc_field) { |
| 46 |
_cleanup_free_ char *p = NULL, *s = NULL; |
| 47 |
unsigned long long total = 0, buffers = 0; |
| 48 |
int r; |
| 49 |
|
| 50 |
r = get_proc_field("/proc/meminfo", "MemTotal", &p); |
| 51 |
if (!IN_SET(r, -ENOENT, -ENOSYS)) { |
| 52 |
ASSERT_OK(r); |
| 53 |
puts(p); |
| 54 |
ASSERT_OK(safe_atollu(p, &total)); |
| 55 |
} |
| 56 |
|
| 57 |
r = get_proc_field("/proc/meminfo", "Buffers", &s); |
| 58 |
if (!IN_SET(r, -ENOENT, -ENOSYS)) { |
| 59 |
ASSERT_OK(r); |
| 60 |
puts(s); |
| 61 |
ASSERT_OK(safe_atollu(s, &buffers)); |
| 62 |
} |
| 63 |
|
| 64 |
if (p) |
| 65 |
ASSERT_LT(buffers, total); |
| 66 |
} |
| 67 |
|
| 68 |
TEST(read_one_line_file) { |
| 69 |
_cleanup_(unlink_tempfilep) char fn[] = "/tmp/test-fileio-1lf-XXXXXX"; |
| 70 |
int fd; |
| 71 |
_cleanup_fclose_ FILE *f = NULL; |
| 72 |
_cleanup_free_ char *buf, *buf2, *buf3, *buf4, *buf5; |
| 73 |
|
| 74 |
ASSERT_OK(fd = mkostemp_safe(fn)); |
| 75 |
|
| 76 |
ASSERT_NOT_NULL(f = fdopen(fd, "we")); |
| 77 |
|
| 78 |
ASSERT_OK_EQ(read_one_line_file(fn, &buf), 0); |
| 79 |
ASSERT_STREQ(buf, ""); |
| 80 |
ASSERT_OK_EQ(read_one_line_file(fn, &buf2), 0); |
| 81 |
ASSERT_STREQ(buf2, ""); |
| 82 |
|
| 83 |
ASSERT_OK(write_string_stream(f, "x", WRITE_STRING_FILE_AVOID_NEWLINE)); |
| 84 |
fflush(f); |
| 85 |
|
| 86 |
ASSERT_OK_EQ(read_one_line_file(fn, &buf3), 1); |
| 87 |
ASSERT_STREQ(buf3, "x"); |
| 88 |
|
| 89 |
ASSERT_OK(write_string_stream(f, "\n", WRITE_STRING_FILE_AVOID_NEWLINE)); |
| 90 |
fflush(f); |
| 91 |
|
| 92 |
ASSERT_OK_EQ(read_one_line_file(fn, &buf4), 2); |
| 93 |
ASSERT_STREQ(buf4, "x"); |
| 94 |
|
| 95 |
ASSERT_OK(write_string_stream(f, "\n", WRITE_STRING_FILE_AVOID_NEWLINE)); |
| 96 |
fflush(f); |
| 97 |
|
| 98 |
ASSERT_OK_EQ(read_one_line_file(fn, &buf5), 2); |
| 99 |
ASSERT_STREQ(buf5, "x"); |
| 100 |
} |
| 101 |
|
| 102 |
TEST(write_string_stream) { |
| 103 |
_cleanup_(unlink_tempfilep) char fn[] = "/tmp/test-write_string_stream-XXXXXX"; |
| 104 |
_cleanup_fclose_ FILE *f = NULL; |
| 105 |
int fd; |
| 106 |
|
| 107 |
ASSERT_OK(fd = mkostemp_safe(fn)); |
| 108 |
|
| 109 |
ASSERT_NOT_NULL(f = fdopen(fd, "r")); |
| 110 |
ASSERT_ERROR(write_string_stream(f, "boohoo", 0), EBADF); |
| 111 |
f = safe_fclose(f); |
| 112 |
|
| 113 |
ASSERT_NOT_NULL(f = fopen(fn, "r+")); |
| 114 |
ASSERT_OK(write_string_stream(f, "boohoo", 0)); |
| 115 |
rewind(f); |
| 116 |
|
| 117 |
_cleanup_free_ char *buf = NULL; |
| 118 |
ASSERT_OK(read_full_file(fn, &buf, /* ret_size= */ NULL)); |
| 119 |
ASSERT_STREQ(buf, "boohoo\n"); |
| 120 |
buf = mfree(buf); |
| 121 |
f = safe_fclose(f); |
| 122 |
|
| 123 |
ASSERT_NOT_NULL(f = fopen(fn, "w+")); |
| 124 |
ASSERT_OK(write_string_stream(f, "boohoo", WRITE_STRING_FILE_AVOID_NEWLINE)); |
| 125 |
rewind(f); |
| 126 |
|
| 127 |
ASSERT_OK(read_full_file(fn, &buf, /* ret_size= */ NULL)); |
| 128 |
ASSERT_STREQ(buf, "boohoo"); |
| 129 |
} |
| 130 |
|
| 131 |
TEST(write_string_file) { |
| 132 |
_cleanup_(unlink_tempfilep) char fn[] = "/tmp/test-write_string_file-XXXXXX"; |
| 133 |
char buf[64] = {}; |
| 134 |
_cleanup_close_ int fd = -EBADF; |
| 135 |
|
| 136 |
ASSERT_OK(fd = mkostemp_safe(fn)); |
| 137 |
|
| 138 |
ASSERT_OK(write_string_file(fn, "boohoo", WRITE_STRING_FILE_CREATE)); |
| 139 |
|
| 140 |
ASSERT_OK_EQ_ERRNO(read(fd, buf, sizeof(buf)), 7); |
| 141 |
ASSERT_STREQ(buf, "boohoo\n"); |
| 142 |
} |
| 143 |
|
| 144 |
TEST(write_string_file_no_create) { |
| 145 |
_cleanup_(unlink_tempfilep) char fn[] = "/tmp/test-write_string_file_no_create-XXXXXX"; |
| 146 |
_cleanup_close_ int fd = -EBADF; |
| 147 |
char buf[64] = {}; |
| 148 |
|
| 149 |
ASSERT_ERROR(write_string_file("/a/file/which/does/not/exists/i/guess", "boohoo", 0), ENOENT); |
| 150 |
|
| 151 |
ASSERT_OK(fd = mkostemp_safe(fn)); |
| 152 |
ASSERT_OK(write_string_file(fn, "boohoo", 0)); |
| 153 |
ASSERT_OK_EQ_ERRNO(read(fd, buf, sizeof buf), (ssize_t) strlen("boohoo\n")); |
| 154 |
ASSERT_STREQ(buf, "boohoo\n"); |
| 155 |
} |
| 156 |
|
| 157 |
TEST(write_string_file_verify) { |
| 158 |
_cleanup_free_ char *buf = NULL, *buf2 = NULL; |
| 159 |
int r; |
| 160 |
|
| 161 |
r = read_one_line_file("/proc/version", &buf); |
| 162 |
if (ERRNO_IS_NEG_PRIVILEGE(r)) |
| 163 |
return; |
| 164 |
ASSERT_OK(r); |
| 165 |
ASSERT_NOT_NULL(buf2 = strjoin(buf, "\n")); |
| 166 |
|
| 167 |
r = write_string_file("/proc/version", buf, 0); |
| 168 |
ASSERT_TRUE(IN_SET(r, -EACCES, -EIO)); |
| 169 |
r = write_string_file("/proc/version", buf2, 0); |
| 170 |
ASSERT_TRUE(IN_SET(r, -EACCES, -EIO)); |
| 171 |
|
| 172 |
ASSERT_OK(write_string_file("/proc/version", buf, WRITE_STRING_FILE_VERIFY_ON_FAILURE)); |
| 173 |
ASSERT_OK(write_string_file("/proc/version", buf2, WRITE_STRING_FILE_VERIFY_ON_FAILURE)); |
| 174 |
|
| 175 |
r = write_string_file("/proc/version", buf, WRITE_STRING_FILE_VERIFY_ON_FAILURE|WRITE_STRING_FILE_AVOID_NEWLINE); |
| 176 |
ASSERT_TRUE(IN_SET(r, -EACCES, -EIO)); |
| 177 |
ASSERT_OK(write_string_file("/proc/version", buf2, WRITE_STRING_FILE_VERIFY_ON_FAILURE|WRITE_STRING_FILE_AVOID_NEWLINE)); |
| 178 |
} |
| 179 |
|
| 180 |
TEST(search_and_fopen) { |
| 181 |
static const char* const dirs[] = { |
| 182 |
"/tmp/foo/bar", |
| 183 |
"/tmp", |
| 184 |
NULL |
| 185 |
}; |
| 186 |
char name[] = "/tmp/test-search_and_fopen.XXXXXX"; |
| 187 |
_cleanup_fclose_ FILE *f = NULL; |
| 188 |
_cleanup_free_ char *p = NULL, *bn = NULL; |
| 189 |
_cleanup_close_ int fd = -EBADF; |
| 190 |
|
| 191 |
ASSERT_OK(fd = mkostemp_safe(name)); |
| 192 |
fd = safe_close(fd); |
| 193 |
|
| 194 |
ASSERT_OK(path_extract_filename(name, &bn)); |
| 195 |
ASSERT_OK(search_and_fopen(bn, "re", NULL, (const char**) dirs, &f, &p)); |
| 196 |
ASSERT_STREQ(bn, path_startswith(p, "/tmp/")); |
| 197 |
f = safe_fclose(f); |
| 198 |
p = mfree(p); |
| 199 |
|
| 200 |
ASSERT_OK(search_and_fopen(bn, NULL, NULL, (const char**) dirs, NULL, &p)); |
| 201 |
ASSERT_STREQ(bn, path_startswith(p, "/tmp/")); |
| 202 |
p = mfree(p); |
| 203 |
|
| 204 |
ASSERT_OK(search_and_fopen(name, "re", NULL, (const char**) dirs, &f, &p)); |
| 205 |
ASSERT_TRUE(path_equal(name, p)); |
| 206 |
f = safe_fclose(f); |
| 207 |
p = mfree(p); |
| 208 |
|
| 209 |
ASSERT_OK(search_and_fopen(name, NULL, NULL, (const char**) dirs, NULL, &p)); |
| 210 |
ASSERT_TRUE(path_equal(name, p)); |
| 211 |
p = mfree(p); |
| 212 |
|
| 213 |
ASSERT_OK(search_and_fopen(bn, "re", "/", (const char**) dirs, &f, &p)); |
| 214 |
ASSERT_STREQ(bn, path_startswith(p, "/tmp/")); |
| 215 |
f = safe_fclose(f); |
| 216 |
p = mfree(p); |
| 217 |
|
| 218 |
ASSERT_OK(search_and_fopen(bn, NULL, "/", (const char**) dirs, NULL, &p)); |
| 219 |
ASSERT_STREQ(bn, path_startswith(p, "/tmp/")); |
| 220 |
p = mfree(p); |
| 221 |
|
| 222 |
ASSERT_ERROR(search_and_fopen("/a/file/which/does/not/exist/i/guess", "re", NULL, (const char**) dirs, &f, &p), ENOENT); |
| 223 |
ASSERT_ERROR(search_and_fopen("/a/file/which/does/not/exist/i/guess", NULL, NULL, (const char**) dirs, NULL, &p), ENOENT); |
| 224 |
ASSERT_ERROR(search_and_fopen("afilewhichdoesnotexistiguess", "re", NULL, (const char**) dirs, &f, &p), ENOENT); |
| 225 |
ASSERT_ERROR(search_and_fopen("afilewhichdoesnotexistiguess", NULL, NULL, (const char**) dirs, NULL, &p), ENOENT); |
| 226 |
|
| 227 |
ASSERT_OK_ERRNO(unlink(name)); |
| 228 |
|
| 229 |
ASSERT_ERROR(search_and_fopen(bn, "re", NULL, (const char**) dirs, &f, &p), ENOENT); |
| 230 |
ASSERT_ERROR(search_and_fopen(bn, NULL, NULL, (const char**) dirs, NULL, &p), ENOENT); |
| 231 |
} |
| 232 |
|
| 233 |
TEST(search_and_fopen_nulstr) { |
| 234 |
static const char dirs[] = |
| 235 |
"/tmp/foo/bar\0" |
| 236 |
"/tmp\0"; |
| 237 |
|
| 238 |
_cleanup_(unlink_tempfilep) char name[] = "/tmp/test-search_and_fopen.XXXXXX"; |
| 239 |
_cleanup_fclose_ FILE *f = NULL; |
| 240 |
_cleanup_free_ char *p = NULL, *bn = NULL; |
| 241 |
_cleanup_close_ int fd = -EBADF; |
| 242 |
|
| 243 |
ASSERT_OK(fd = mkostemp_safe(name)); |
| 244 |
fd = safe_close(fd); |
| 245 |
|
| 246 |
ASSERT_OK(path_extract_filename(name, &bn)); |
| 247 |
ASSERT_OK(search_and_fopen_nulstr(bn, "re", NULL, dirs, &f, &p)); |
| 248 |
ASSERT_STREQ(bn, path_startswith(p, "/tmp/")); |
| 249 |
f = safe_fclose(f); |
| 250 |
p = mfree(p); |
| 251 |
|
| 252 |
ASSERT_OK(search_and_fopen_nulstr(name, "re", NULL, dirs, &f, &p)); |
| 253 |
ASSERT_TRUE(path_equal(name, p)); |
| 254 |
f = safe_fclose(f); |
| 255 |
p = mfree(p); |
| 256 |
|
| 257 |
ASSERT_ERROR(search_and_fopen_nulstr("/a/file/which/does/not/exist/i/guess", "re", NULL, dirs, &f, &p), ENOENT); |
| 258 |
ASSERT_ERROR(search_and_fopen_nulstr("afilewhichdoesnotexistiguess", "re", NULL, dirs, &f, &p), ENOENT); |
| 259 |
|
| 260 |
ASSERT_OK_ERRNO(unlink(name)); |
| 261 |
|
| 262 |
ASSERT_ERROR(search_and_fopen_nulstr(bn, "re", NULL, dirs, &f, &p), ENOENT); |
| 263 |
} |
| 264 |
|
| 265 |
static const char chars[] = |
| 266 |
"Aąę„”\n루\377"; |
| 267 |
|
| 268 |
DISABLE_WARNING_TYPE_LIMITS; |
| 269 |
|
| 270 |
TEST(safe_fgetc) { |
| 271 |
_cleanup_fclose_ FILE *f = NULL; |
| 272 |
char c; |
| 273 |
|
| 274 |
ASSERT_NOT_NULL(f = fmemopen_unlocked((void*) chars, sizeof(chars), "r")); |
| 275 |
|
| 276 |
for (size_t i = 0; i < sizeof(chars); i++) { |
| 277 |
ASSERT_OK_POSITIVE(safe_fgetc(f, &c)); |
| 278 |
ASSERT_EQ(c, chars[i]); |
| 279 |
|
| 280 |
if (ungetc(c, f) == EOF) { |
| 281 |
/* EOF is -1, and hence we can't push value 255 in this way – if char is signed */ |
| 282 |
ASSERT_EQ(c, (char) EOF); |
| 283 |
ASSERT_EQ(CHAR_MIN, -128); /* verify that char is signed on this platform */ |
| 284 |
} else { |
| 285 |
ASSERT_OK_POSITIVE(safe_fgetc(f, &c)); |
| 286 |
ASSERT_EQ(c, chars[i]); |
| 287 |
} |
| 288 |
|
| 289 |
/* But it works when we push it properly cast */ |
| 290 |
ASSERT_TRUE(ungetc((unsigned char) c, f) != EOF); |
| 291 |
ASSERT_OK_POSITIVE(safe_fgetc(f, &c)); |
| 292 |
ASSERT_EQ(c, chars[i]); |
| 293 |
} |
| 294 |
|
| 295 |
ASSERT_OK_ZERO(safe_fgetc(f, &c)); |
| 296 |
} |
| 297 |
|
| 298 |
REENABLE_WARNING; |
| 299 |
|
| 300 |
static const char buffer[] = |
| 301 |
"Some test data\n" |
| 302 |
"루Non-ascii chars: ąę„”\n" |
| 303 |
"terminators\r\n" |
| 304 |
"and even more\n\r" |
| 305 |
"now the same with a NUL\n\0" |
| 306 |
"and more\r\0" |
| 307 |
"and even more\r\n\0" |
| 308 |
"and yet even more\n\r\0" |
| 309 |
"With newlines, and a NUL byte\0" |
| 310 |
"\n" |
| 311 |
"an empty line\n" |
| 312 |
"an ignored line\n" |
| 313 |
"and a very long line that is supposed to be truncated, because it is so long\n"; |
| 314 |
|
| 315 |
static void test_read_line_one_file(FILE *f) { |
| 316 |
_cleanup_free_ char *line = NULL; |
| 317 |
|
| 318 |
ASSERT_OK_EQ(read_line(f, SIZE_MAX, &line), 15); |
| 319 |
ASSERT_STREQ(line, "Some test data"); |
| 320 |
line = mfree(line); |
| 321 |
|
| 322 |
ASSERT_OK_POSITIVE(read_line(f, SIZE_MAX, &line)); |
| 323 |
ASSERT_STREQ(line, "루Non-ascii chars: ąę„”"); |
| 324 |
line = mfree(line); |
| 325 |
|
| 326 |
ASSERT_OK_EQ(read_line(f, SIZE_MAX, &line), 13); |
| 327 |
ASSERT_STREQ(line, "terminators"); |
| 328 |
line = mfree(line); |
| 329 |
|
| 330 |
ASSERT_OK_EQ(read_line(f, SIZE_MAX, &line), 15); |
| 331 |
ASSERT_STREQ(line, "and even more"); |
| 332 |
line = mfree(line); |
| 333 |
|
| 334 |
ASSERT_OK_EQ(read_line(f, SIZE_MAX, &line), 25); |
| 335 |
ASSERT_STREQ(line, "now the same with a NUL"); |
| 336 |
line = mfree(line); |
| 337 |
|
| 338 |
ASSERT_OK_EQ(read_line(f, SIZE_MAX, &line), 10); |
| 339 |
ASSERT_STREQ(line, "and more"); |
| 340 |
line = mfree(line); |
| 341 |
|
| 342 |
ASSERT_OK_EQ(read_line(f, SIZE_MAX, &line), 16); |
| 343 |
ASSERT_STREQ(line, "and even more"); |
| 344 |
line = mfree(line); |
| 345 |
|
| 346 |
ASSERT_OK_EQ(read_line(f, SIZE_MAX, &line), 20); |
| 347 |
ASSERT_STREQ(line, "and yet even more"); |
| 348 |
line = mfree(line); |
| 349 |
|
| 350 |
ASSERT_OK_EQ(read_line(f, 1024, &line), 30); |
| 351 |
ASSERT_STREQ(line, "With newlines, and a NUL byte"); |
| 352 |
line = mfree(line); |
| 353 |
|
| 354 |
ASSERT_OK_EQ(read_line(f, 1024, &line), 1); |
| 355 |
ASSERT_STREQ(line, ""); |
| 356 |
line = mfree(line); |
| 357 |
|
| 358 |
ASSERT_OK_EQ(read_line(f, 1024, &line), 14); |
| 359 |
ASSERT_STREQ(line, "an empty line"); |
| 360 |
line = mfree(line); |
| 361 |
|
| 362 |
ASSERT_OK_EQ(read_line(f, SIZE_MAX, NULL), 16); |
| 363 |
|
| 364 |
ASSERT_ERROR(read_line(f, 16, &line), ENOBUFS); |
| 365 |
line = mfree(line); |
| 366 |
|
| 367 |
/* read_line() stopped when it hit the limit, that means when we continue reading we'll read at the first |
| 368 |
* character after the previous limit. Let's make use of that to continue our test. */ |
| 369 |
ASSERT_OK_EQ(read_line(f, 1024, &line), 62); |
| 370 |
ASSERT_STREQ(line, "line that is supposed to be truncated, because it is so long"); |
| 371 |
line = mfree(line); |
| 372 |
|
| 373 |
ASSERT_OK_EQ(read_line(f, 1024, &line), 0); |
| 374 |
ASSERT_STREQ(line, ""); |
| 375 |
} |
| 376 |
|
| 377 |
TEST(read_line_one_file_1) { |
| 378 |
_cleanup_fclose_ FILE *f = NULL; |
| 379 |
|
| 380 |
ASSERT_NOT_NULL(f = fmemopen_unlocked((void*) buffer, sizeof(buffer), "r")); |
| 381 |
test_read_line_one_file(f); |
| 382 |
} |
| 383 |
|
| 384 |
TEST(reaad_line_one_file_2) { |
| 385 |
_cleanup_(unlink_tempfilep) char name[] = "/tmp/test-fileio.XXXXXX"; |
| 386 |
int fd; |
| 387 |
_cleanup_fclose_ FILE *f = NULL; |
| 388 |
|
| 389 |
ASSERT_OK(fd = mkostemp_safe(name)); |
| 390 |
ASSERT_OK_EQ_ERRNO(write(fd, buffer, sizeof(buffer)), (ssize_t) sizeof(buffer)); |
| 391 |
|
| 392 |
ASSERT_OK_ERRNO(lseek(fd, 0, SEEK_SET)); |
| 393 |
ASSERT_NOT_NULL(f = fdopen(fd, "r")); |
| 394 |
|
| 395 |
test_read_line_one_file(f); |
| 396 |
} |
| 397 |
|
| 398 |
TEST(read_line_one_file_3) { |
| 399 |
_cleanup_fclose_ FILE *f = NULL; |
| 400 |
_cleanup_free_ char *line = NULL; |
| 401 |
int r; |
| 402 |
|
| 403 |
f = fopen("/proc/uptime", "re"); |
| 404 |
if (!f && IN_SET(errno, ENOENT, EPERM)) |
| 405 |
return; |
| 406 |
ASSERT_NOT_NULL(f); |
| 407 |
|
| 408 |
ASSERT_OK(r = read_line(f, LINE_MAX, &line)); |
| 409 |
if (r == 0) { |
| 410 |
ASSERT_NOT_NULL(line); |
| 411 |
ASSERT_STREQ(line, ""); |
| 412 |
} else |
| 413 |
ASSERT_EQ((size_t) r, strlen(line) + 1); |
| 414 |
|
| 415 |
ASSERT_OK_EQ(read_line(f, LINE_MAX, NULL), 0); |
| 416 |
} |
| 417 |
|
| 418 |
TEST(read_line_one_file_4) { |
| 419 |
static const struct { |
| 420 |
size_t length; |
| 421 |
const char *string; |
| 422 |
} eof_endings[] = { |
| 423 |
/* Each of these will be followed by EOF and should generate the one same single string */ |
| 424 |
{ 3, "foo" }, |
| 425 |
{ 4, "foo\n" }, |
| 426 |
{ 4, "foo\r" }, |
| 427 |
{ 4, "foo\0" }, |
| 428 |
{ 5, "foo\n\0" }, |
| 429 |
{ 5, "foo\r\0" }, |
| 430 |
{ 5, "foo\r\n" }, |
| 431 |
{ 5, "foo\n\r" }, |
| 432 |
{ 6, "foo\r\n\0" }, |
| 433 |
{ 6, "foo\n\r\0" }, |
| 434 |
}; |
| 435 |
|
| 436 |
FOREACH_ELEMENT(ending, eof_endings) { |
| 437 |
_cleanup_fclose_ FILE *f = NULL; |
| 438 |
_cleanup_free_ char *s = NULL; |
| 439 |
|
| 440 |
ASSERT_NOT_NULL(f = fmemopen_unlocked((void*) ending->string, ending->length, "r")); |
| 441 |
|
| 442 |
ASSERT_OK_EQ(read_line(f, SIZE_MAX, &s), (ssize_t) ending->length); |
| 443 |
ASSERT_STREQ(s, "foo"); |
| 444 |
|
| 445 |
ASSERT_OK_EQ(read_line(f, SIZE_MAX, NULL), 0); /* Ensure we hit EOF */ |
| 446 |
} |
| 447 |
} |
| 448 |
|
| 449 |
TEST(read_nul_string) { |
| 450 |
static const char test[] = "string nr. 1\0" |
| 451 |
"string nr. 2\n\0" |
| 452 |
"\377empty string follows\0" |
| 453 |
"\0" |
| 454 |
"final string\n is empty\0" |
| 455 |
"\0"; |
| 456 |
|
| 457 |
_cleanup_fclose_ FILE *f = NULL; |
| 458 |
_cleanup_free_ char *s = NULL; |
| 459 |
|
| 460 |
ASSERT_NOT_NULL(f = fmemopen_unlocked((void*) test, sizeof(test)-1, "r")); |
| 461 |
|
| 462 |
ASSERT_OK_EQ(read_nul_string(f, LONG_LINE_MAX, &s), 13); |
| 463 |
ASSERT_STREQ(s, "string nr. 1"); |
| 464 |
s = mfree(s); |
| 465 |
|
| 466 |
ASSERT_OK_EQ(read_nul_string(f, LONG_LINE_MAX, &s), 14); |
| 467 |
ASSERT_STREQ(s, "string nr. 2\n"); |
| 468 |
s = mfree(s); |
| 469 |
|
| 470 |
ASSERT_OK_EQ(read_nul_string(f, LONG_LINE_MAX, &s), 22); |
| 471 |
ASSERT_STREQ(s, "\377empty string follows"); |
| 472 |
s = mfree(s); |
| 473 |
|
| 474 |
ASSERT_OK_EQ(read_nul_string(f, LONG_LINE_MAX, &s), 1); |
| 475 |
ASSERT_STREQ(s, ""); |
| 476 |
s = mfree(s); |
| 477 |
|
| 478 |
ASSERT_OK_EQ(read_nul_string(f, LONG_LINE_MAX, &s), 23); |
| 479 |
ASSERT_STREQ(s, "final string\n is empty"); |
| 480 |
s = mfree(s); |
| 481 |
|
| 482 |
ASSERT_OK_EQ(read_nul_string(f, LONG_LINE_MAX, &s), 1); |
| 483 |
ASSERT_STREQ(s, ""); |
| 484 |
s = mfree(s); |
| 485 |
|
| 486 |
ASSERT_OK_EQ(read_nul_string(f, LONG_LINE_MAX, &s), 0); |
| 487 |
ASSERT_STREQ(s, ""); |
| 488 |
} |
| 489 |
|
| 490 |
TEST(read_full_file_socket) { |
| 491 |
_cleanup_(rm_rf_physical_and_freep) char *z = NULL; |
| 492 |
_cleanup_close_ int listener = -EBADF; |
| 493 |
_cleanup_free_ char *data = NULL, *clientname = NULL; |
| 494 |
union sockaddr_union sa; |
| 495 |
const char *j, *jj; |
| 496 |
size_t size; |
| 497 |
int r; |
| 498 |
|
| 499 |
ASSERT_OK(listener = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0)); |
| 500 |
|
| 501 |
ASSERT_OK(mkdtemp_malloc(NULL, &z)); |
| 502 |
ASSERT_NOT_NULL(j = strjoina(z, "/socket")); |
| 503 |
|
| 504 |
ASSERT_OK(sockaddr_un_set_path(&sa.un, j)); |
| 505 |
|
| 506 |
ASSERT_OK_ERRNO(bind(listener, &sa.sa, sockaddr_un_len(&sa.un))); |
| 507 |
ASSERT_OK_ERRNO(listen(listener, 1)); |
| 508 |
|
| 509 |
/* Make sure the socket doesn't fit into a struct sockaddr_un, but we can still access it */ |
| 510 |
ASSERT_NOT_NULL(jj = strjoina(z, "/a_very_long_patha_very_long_patha_very_long_patha_very_long_patha_very_long_patha_very_long_patha_very_long_patha_very_long_path")); |
| 511 |
ASSERT_GT(strlen(jj), sizeof_field(struct sockaddr_un, sun_path)); |
| 512 |
ASSERT_OK_ERRNO(rename(j, jj)); |
| 513 |
|
| 514 |
/* Bind the *client* socket to some randomized name, to verify that this works correctly. */ |
| 515 |
ASSERT_OK(asprintf(&clientname, "@%" PRIx64 "/test-bindname", random_u64())); |
| 516 |
|
| 517 |
_cleanup_(pidref_done) PidRef pidref = PIDREF_NULL; |
| 518 |
r = ASSERT_OK(pidref_safe_fork("(server)", FORK_DEATHSIG_SIGTERM|FORK_LOG, &pidref)); |
| 519 |
if (r == 0) { |
| 520 |
union sockaddr_union peer = {}; |
| 521 |
socklen_t peerlen = sizeof(peer); |
| 522 |
_cleanup_close_ int rfd = -EBADF; |
| 523 |
/* child */ |
| 524 |
|
| 525 |
ASSERT_OK(rfd = accept4(listener, NULL, NULL, SOCK_CLOEXEC)); |
| 526 |
|
| 527 |
ASSERT_OK_ERRNO(getpeername(rfd, &peer.sa, &peerlen)); |
| 528 |
|
| 529 |
ASSERT_EQ(peer.un.sun_family, AF_UNIX); |
| 530 |
ASSERT_GT(peerlen, offsetof(struct sockaddr_un, sun_path)); |
| 531 |
ASSERT_EQ(peer.un.sun_path[0], 0); |
| 532 |
ASSERT_STREQ(peer.un.sun_path + 1, clientname + 1); |
| 533 |
|
| 534 |
#define TEST_STR "This is a test\nreally." |
| 535 |
|
| 536 |
ASSERT_OK_EQ_ERRNO(write(rfd, TEST_STR, strlen(TEST_STR)), (ssize_t) strlen(TEST_STR)); |
| 537 |
_exit(EXIT_SUCCESS); |
| 538 |
} |
| 539 |
|
| 540 |
ASSERT_ERROR(read_full_file_full(AT_FDCWD, jj, UINT64_MAX, SIZE_MAX, 0, NULL, &data, &size), ENXIO); |
| 541 |
ASSERT_OK(read_full_file_full(AT_FDCWD, jj, UINT64_MAX, SIZE_MAX, READ_FULL_FILE_CONNECT_SOCKET, clientname, &data, &size)); |
| 542 |
ASSERT_EQ(size, strlen(TEST_STR)); |
| 543 |
ASSERT_STREQ(data, TEST_STR); |
| 544 |
|
| 545 |
ASSERT_OK(pidref_wait_for_terminate_and_check("(server)", &pidref, WAIT_LOG)); |
| 546 |
#undef TEST_STR |
| 547 |
} |
| 548 |
|
| 549 |
TEST(read_full_file_full) { |
| 550 |
_cleanup_fclose_ FILE *f = NULL; |
| 551 |
_cleanup_(unlink_and_freep) char *fn = NULL; |
| 552 |
_cleanup_free_ char *rbuf = NULL; |
| 553 |
size_t rbuf_size; |
| 554 |
uint8_t buf[4711]; |
| 555 |
|
| 556 |
random_bytes(buf, sizeof(buf)); |
| 557 |
|
| 558 |
ASSERT_OK(tempfn_random_child(NULL, NULL, &fn)); |
| 559 |
ASSERT_NOT_NULL(f = fopen(fn, "we")); |
| 560 |
ASSERT_EQ(fwrite(buf, 1, sizeof(buf), f), sizeof(buf)); |
| 561 |
ASSERT_OK(fflush_and_check(f)); |
| 562 |
|
| 563 |
ASSERT_OK(read_full_file_full(AT_FDCWD, fn, UINT64_MAX, SIZE_MAX, 0, NULL, &rbuf, &rbuf_size)); |
| 564 |
ASSERT_EQ(rbuf_size, sizeof(buf)); |
| 565 |
ASSERT_EQ(memcmp(buf, rbuf, rbuf_size), 0); |
| 566 |
rbuf = mfree(rbuf); |
| 567 |
|
| 568 |
ASSERT_OK(read_full_file_full(AT_FDCWD, fn, UINT64_MAX, 128, 0, NULL, &rbuf, &rbuf_size)); |
| 569 |
ASSERT_EQ(rbuf_size, 128U); |
| 570 |
ASSERT_EQ(memcmp(buf, rbuf, rbuf_size), 0); |
| 571 |
rbuf = mfree(rbuf); |
| 572 |
|
| 573 |
ASSERT_ERROR(read_full_file_full(AT_FDCWD, fn, UINT64_MAX, 128, READ_FULL_FILE_FAIL_WHEN_LARGER, NULL, &rbuf, &rbuf_size), E2BIG); |
| 574 |
ASSERT_ERROR(read_full_file_full(AT_FDCWD, fn, UINT64_MAX, sizeof(buf)-1, READ_FULL_FILE_FAIL_WHEN_LARGER, NULL, &rbuf, &rbuf_size), E2BIG); |
| 575 |
ASSERT_OK(read_full_file_full(AT_FDCWD, fn, UINT64_MAX, sizeof(buf), READ_FULL_FILE_FAIL_WHEN_LARGER, NULL, &rbuf, &rbuf_size)); |
| 576 |
ASSERT_EQ(rbuf_size, sizeof(buf)); |
| 577 |
ASSERT_EQ(memcmp(buf, rbuf, rbuf_size), 0); |
| 578 |
rbuf = mfree(rbuf); |
| 579 |
|
| 580 |
ASSERT_ERROR(read_full_file_full(AT_FDCWD, fn, 47, 128, READ_FULL_FILE_FAIL_WHEN_LARGER, NULL, &rbuf, &rbuf_size), E2BIG); |
| 581 |
ASSERT_ERROR(read_full_file_full(AT_FDCWD, fn, 47, sizeof(buf)-47-1, READ_FULL_FILE_FAIL_WHEN_LARGER, NULL, &rbuf, &rbuf_size), E2BIG); |
| 582 |
ASSERT_OK(read_full_file_full(AT_FDCWD, fn, 47, sizeof(buf)-47, READ_FULL_FILE_FAIL_WHEN_LARGER, NULL, &rbuf, &rbuf_size)); |
| 583 |
ASSERT_EQ(rbuf_size, sizeof(buf)-47); |
| 584 |
ASSERT_EQ(memcmp(buf+47, rbuf, rbuf_size), 0); |
| 585 |
rbuf = mfree(rbuf); |
| 586 |
|
| 587 |
ASSERT_OK(read_full_file_full(AT_FDCWD, fn, UINT64_MAX, sizeof(buf)+1, READ_FULL_FILE_FAIL_WHEN_LARGER, NULL, &rbuf, &rbuf_size)); |
| 588 |
ASSERT_EQ(rbuf_size, sizeof(buf)); |
| 589 |
ASSERT_EQ(memcmp(buf, rbuf, rbuf_size), 0); |
| 590 |
rbuf = mfree(rbuf); |
| 591 |
|
| 592 |
ASSERT_OK(read_full_file_full(AT_FDCWD, fn, 1234, SIZE_MAX, 0, NULL, &rbuf, &rbuf_size)); |
| 593 |
ASSERT_EQ(rbuf_size, sizeof(buf) - 1234); |
| 594 |
ASSERT_EQ(memcmp(buf + 1234, rbuf, rbuf_size), 0); |
| 595 |
rbuf = mfree(rbuf); |
| 596 |
|
| 597 |
ASSERT_OK(read_full_file_full(AT_FDCWD, fn, 2345, 777, 0, NULL, &rbuf, &rbuf_size)); |
| 598 |
ASSERT_EQ(rbuf_size, 777U); |
| 599 |
ASSERT_EQ(memcmp(buf + 2345, rbuf, rbuf_size), 0); |
| 600 |
rbuf = mfree(rbuf); |
| 601 |
|
| 602 |
ASSERT_OK(read_full_file_full(AT_FDCWD, fn, 4700, 20, 0, NULL, &rbuf, &rbuf_size)); |
| 603 |
ASSERT_EQ(rbuf_size, 11U); |
| 604 |
ASSERT_EQ(memcmp(buf + 4700, rbuf, rbuf_size), 0); |
| 605 |
rbuf = mfree(rbuf); |
| 606 |
|
| 607 |
ASSERT_OK(read_full_file_full(AT_FDCWD, fn, 10000, 99, 0, NULL, &rbuf, &rbuf_size)); |
| 608 |
ASSERT_EQ(rbuf_size, 0U); |
| 609 |
rbuf = mfree(rbuf); |
| 610 |
} |
| 611 |
|
| 612 |
static void test_read_virtual_file_one(size_t max_size) { |
| 613 |
int r; |
| 614 |
|
| 615 |
log_debug("/* %s (max_size=%zu) */", __func__, max_size); |
| 616 |
|
| 617 |
FOREACH_STRING(filename, |
| 618 |
"/proc/1/cmdline", |
| 619 |
"/etc/nsswitch.conf", |
| 620 |
"/sys/kernel/uevent_seqnum", |
| 621 |
"/proc/kcore", |
| 622 |
"/proc/kallsyms", |
| 623 |
"/proc/self/exe", |
| 624 |
"/proc/self/pagemap") { |
| 625 |
|
| 626 |
_cleanup_free_ char *buf = NULL; |
| 627 |
size_t size = 0; |
| 628 |
|
| 629 |
r = read_virtual_file(filename, max_size, &buf, &size); |
| 630 |
if (!ERRNO_IS_NEG_PRIVILEGE(r) && /* /proc/kcore is not accessible to unpriv */ |
| 631 |
!IN_SET(r, |
| 632 |
-ENOENT, /* Some of the files might be absent */ |
| 633 |
-EINVAL, /* too small reads from /proc/self/pagemap trigger EINVAL */ |
| 634 |
-EFBIG, /* /proc/kcore and /proc/self/pagemap should be too large */ |
| 635 |
-EBADF)) { /* /proc/kcore is masked when we are running in docker. */ |
| 636 |
ASSERT_OK(r); |
| 637 |
log_debug("read_virtual_file(\"%s\", %zu): %s (%zu bytes)", filename, max_size, r ? "non-truncated" : "truncated", size); |
| 638 |
} |
| 639 |
} |
| 640 |
} |
| 641 |
|
| 642 |
TEST(read_virtual_file) { |
| 643 |
test_read_virtual_file_one(0); |
| 644 |
test_read_virtual_file_one(1); |
| 645 |
test_read_virtual_file_one(2); |
| 646 |
test_read_virtual_file_one(20); |
| 647 |
test_read_virtual_file_one(4096); |
| 648 |
test_read_virtual_file_one(4097); |
| 649 |
test_read_virtual_file_one(SIZE_MAX); |
| 650 |
} |
| 651 |
|
| 652 |
TEST(fdopen_independent) { |
| 653 |
#define TEST_TEXT "this is some random test text we are going to write to a memfd" |
| 654 |
_cleanup_close_ int fd = -EBADF; |
| 655 |
_cleanup_fclose_ FILE *f = NULL; |
| 656 |
char buf[STRLEN(TEST_TEXT) + 1]; |
| 657 |
int r; |
| 658 |
|
| 659 |
fd = memfd_new("fdopen_independent"); |
| 660 |
if (ERRNO_IS_NEG_NOT_SUPPORTED(fd)) |
| 661 |
return; |
| 662 |
ASSERT_OK(fd); |
| 663 |
|
| 664 |
ASSERT_OK_EQ_ERRNO(write(fd, TEST_TEXT, strlen(TEST_TEXT)), (ssize_t) strlen(TEST_TEXT)); |
| 665 |
/* we'll leave the read offset at the end of the memfd, the fdopen_independent() descriptors should |
| 666 |
* start at the beginning anyway */ |
| 667 |
|
| 668 |
ASSERT_OK(fdopen_independent(fd, "re", &f)); |
| 669 |
zero(buf); |
| 670 |
ASSERT_EQ(fread(buf, 1, sizeof(buf), f), strlen(TEST_TEXT)); |
| 671 |
ASSERT_STREQ(buf, TEST_TEXT); |
| 672 |
ASSERT_OK_ERRNO(r = fcntl(fileno(f), F_GETFL)); |
| 673 |
ASSERT_EQ((r & O_ACCMODE_STRICT), O_RDONLY); |
| 674 |
ASSERT_OK_ERRNO(r = fcntl(fileno(f), F_GETFD)); |
| 675 |
ASSERT_TRUE(FLAGS_SET(r, FD_CLOEXEC)); |
| 676 |
f = safe_fclose(f); |
| 677 |
|
| 678 |
ASSERT_OK(fdopen_independent(fd, "r", &f)); |
| 679 |
zero(buf); |
| 680 |
ASSERT_EQ(fread(buf, 1, sizeof(buf), f), strlen(TEST_TEXT)); |
| 681 |
ASSERT_STREQ(buf, TEST_TEXT); |
| 682 |
ASSERT_OK_ERRNO(r = fcntl(fileno(f), F_GETFL)); |
| 683 |
ASSERT_EQ((r & O_ACCMODE_STRICT), O_RDONLY); |
| 684 |
ASSERT_OK_ERRNO(r = fcntl(fileno(f), F_GETFD)); |
| 685 |
ASSERT_FALSE(FLAGS_SET(r, FD_CLOEXEC)); |
| 686 |
f = safe_fclose(f); |
| 687 |
|
| 688 |
ASSERT_OK(fdopen_independent(fd, "r+e", &f)); |
| 689 |
zero(buf); |
| 690 |
ASSERT_EQ(fread(buf, 1, sizeof(buf), f), strlen(TEST_TEXT)); |
| 691 |
ASSERT_STREQ(buf, TEST_TEXT); |
| 692 |
ASSERT_OK_ERRNO(r = fcntl(fileno(f), F_GETFL)); |
| 693 |
ASSERT_EQ((r & O_ACCMODE_STRICT), O_RDWR); |
| 694 |
ASSERT_OK_ERRNO(r = fcntl(fileno(f), F_GETFD)); |
| 695 |
ASSERT_TRUE(FLAGS_SET(r, FD_CLOEXEC)); |
| 696 |
f = safe_fclose(f); |
| 697 |
} |
| 698 |
|
| 699 |
TEST(write_data_file_atomic_at) { |
| 700 |
_cleanup_(rm_rf_physical_and_freep) char *t = NULL; |
| 701 |
int r; |
| 702 |
|
| 703 |
ASSERT_OK(mkdtemp_malloc("/tmp/test-wdfaa-XXXXXX", &t)); |
| 704 |
ASSERT_TRUE(path_is_absolute(t)); |
| 705 |
|
| 706 |
const char *abs_wdfa = strjoina(t, "/wdfa"); |
| 707 |
const char *rel_wdfa = abs_wdfa + 1; /* path relative to / */ |
| 708 |
const char *abs_zzz = strjoina(t, "/zzz"); |
| 709 |
const char *abs_zzz_wdfa = strjoina(t, "/zzz/wdfa"); |
| 710 |
const char *rel_zzz_wdfa = abs_zzz_wdfa + 1; |
| 711 |
|
| 712 |
struct iovec a = IOVEC_MAKE_STRING("hallo"); |
| 713 |
ASSERT_OK(write_data_file_atomic_at(AT_FDCWD, abs_wdfa, &a, /* flags= */ 0)); |
| 714 |
|
| 715 |
_cleanup_(iovec_done) struct iovec ra = {}; |
| 716 |
ASSERT_OK(read_full_file(abs_wdfa, (char**) &ra.iov_base, &ra.iov_len)); |
| 717 |
ASSERT_TRUE(iovec_equal(&a, &ra)); |
| 718 |
ASSERT_OK_ERRNO(unlink(abs_wdfa)); |
| 719 |
|
| 720 |
ASSERT_OK(write_data_file_atomic_at(XAT_FDROOT, rel_wdfa, &a, /* flags= */ 0)); |
| 721 |
iovec_done(&ra); |
| 722 |
ASSERT_OK(read_full_file(abs_wdfa, (char**) &ra.iov_base, &ra.iov_len)); |
| 723 |
ASSERT_TRUE(iovec_equal(&a, &ra)); |
| 724 |
ASSERT_OK_ERRNO(unlink(abs_wdfa)); |
| 725 |
|
| 726 |
ASSERT_ERROR(write_data_file_atomic_at(AT_FDCWD, NULL, &a, /* flags= */ 0), EINVAL); |
| 727 |
ASSERT_ERROR(write_data_file_atomic_at(AT_FDCWD, "", &a, /* flags= */ 0), EINVAL); |
| 728 |
ASSERT_ERROR(write_data_file_atomic_at(AT_FDCWD, "/", &a, /* flags= */ 0), EISDIR); |
| 729 |
ASSERT_ERROR(write_data_file_atomic_at(AT_FDCWD, ".", &a, /* flags= */ 0), EISDIR); |
| 730 |
ASSERT_ERROR(write_data_file_atomic_at(AT_FDCWD, strjoina(t, "/"), &a, /* flags= */ 0), EISDIR); |
| 731 |
|
| 732 |
_cleanup_free_ char *cwd = NULL; |
| 733 |
ASSERT_OK(safe_getcwd(&cwd)); |
| 734 |
ASSERT_OK_ERRNO(chdir(t)); |
| 735 |
|
| 736 |
ASSERT_OK(write_data_file_atomic_at(AT_FDCWD, "wdfa", &a, /* flags= */ 0)); |
| 737 |
iovec_done(&ra); |
| 738 |
ASSERT_OK(read_full_file(abs_wdfa, (char**) &ra.iov_base, &ra.iov_len)); |
| 739 |
ASSERT_TRUE(iovec_equal(&a, &ra)); |
| 740 |
ASSERT_OK_ERRNO(unlink(abs_wdfa)); |
| 741 |
|
| 742 |
ASSERT_OK(write_data_file_atomic_at(XAT_FDROOT, rel_wdfa, &a, /* flags= */ 0)); |
| 743 |
iovec_done(&ra); |
| 744 |
ASSERT_OK(read_full_file(abs_wdfa, (char**) &ra.iov_base, &ra.iov_len)); |
| 745 |
ASSERT_TRUE(iovec_equal(&a, &ra)); |
| 746 |
ASSERT_OK_ERRNO(unlink(abs_wdfa)); |
| 747 |
|
| 748 |
ASSERT_OK_ERRNO(chdir(cwd)); |
| 749 |
|
| 750 |
ASSERT_ERROR(write_data_file_atomic_at(XAT_FDROOT, rel_zzz_wdfa, &a, /* flags= */ 0), ENOENT); |
| 751 |
ASSERT_OK(write_data_file_atomic_at(XAT_FDROOT, rel_zzz_wdfa, &a, WRITE_DATA_FILE_MKDIR_0755)); |
| 752 |
iovec_done(&ra); |
| 753 |
ASSERT_OK(read_full_file(abs_zzz_wdfa, (char**) &ra.iov_base, &ra.iov_len)); |
| 754 |
ASSERT_TRUE(iovec_equal(&a, &ra)); |
| 755 |
ASSERT_OK_ERRNO(unlink(abs_zzz_wdfa)); |
| 756 |
|
| 757 |
r = write_data_file_atomic_at(AT_FDCWD, abs_zzz, &a, /* flags= */ 0); |
| 758 |
/* In Gitlab CI this fails with EISDIR */ |
| 759 |
ASSERT_TRUE(IN_SET(r, -EEXIST, -EISDIR)); |
| 760 |
|
| 761 |
ASSERT_OK_ERRNO(rmdir(abs_zzz)); |
| 762 |
} |
| 763 |
|
| 764 |
TEST(read_one_line_file_at_xat_fdroot) { |
| 765 |
_cleanup_(rm_rf_physical_and_freep) char *t = NULL; |
| 766 |
_cleanup_free_ char *fn = NULL, *buf = NULL; |
| 767 |
|
| 768 |
ASSERT_OK(mkdtemp_malloc("/tmp/test-r1lf-xatfd-XXXXXX", &t)); |
| 769 |
ASSERT_TRUE(path_is_absolute(t)); |
| 770 |
|
| 771 |
ASSERT_NOT_NULL(fn = path_join(t, "hello")); |
| 772 |
ASSERT_OK(write_string_file(fn, "first line\nsecond line", WRITE_STRING_FILE_CREATE)); |
| 773 |
|
| 774 |
/* XAT_FDROOT is supposed to root the path at the host's "/"; the implementation prepends a "/" so |
| 775 |
* we pass the path without leading slash. */ |
| 776 |
ASSERT_OK_EQ(read_one_line_file_at(XAT_FDROOT, fn + 1, &buf), (int) STRLEN("first line\n")); |
| 777 |
ASSERT_STREQ(buf, "first line"); |
| 778 |
buf = mfree(buf); |
| 779 |
|
| 780 |
/* Sanity check: AT_FDCWD with the absolute path gives the same result. */ |
| 781 |
ASSERT_OK_EQ(read_one_line_file_at(AT_FDCWD, fn, &buf), (int) STRLEN("first line\n")); |
| 782 |
ASSERT_STREQ(buf, "first line"); |
| 783 |
buf = mfree(buf); |
| 784 |
|
| 785 |
/* /proc/version should always be readable via XAT_FDROOT (some build envs may restrict it; tolerate |
| 786 |
* that). */ |
| 787 |
int r = read_one_line_file_at(XAT_FDROOT, "proc/version", &buf); |
| 788 |
if (!ERRNO_IS_NEG_PRIVILEGE(r)) { |
| 789 |
ASSERT_OK(r); |
| 790 |
ASSERT_FALSE(isempty(buf)); |
| 791 |
buf = mfree(buf); |
| 792 |
} |
| 793 |
|
| 794 |
/* Non-existent path through XAT_FDROOT should yield -ENOENT. */ |
| 795 |
ASSERT_ERROR(read_one_line_file_at(XAT_FDROOT, "tmp/this/path/really/should/not/exist", &buf), ENOENT); |
| 796 |
|
| 797 |
/* Now create a Unix socket in the same temp dir, and verify that read_one_line_file_at() returns |
| 798 |
* -ENXIO when pointed at it via XAT_FDROOT — read_one_line_file_at() does not enable the socket |
| 799 |
* fallback. */ |
| 800 |
_cleanup_free_ char *sockpath = NULL; |
| 801 |
ASSERT_NOT_NULL(sockpath = path_join(t, "socket")); |
| 802 |
|
| 803 |
_cleanup_close_ int listener = -EBADF; |
| 804 |
ASSERT_OK(listener = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, /* protocol= */ 0)); |
| 805 |
|
| 806 |
union sockaddr_union sa; |
| 807 |
ASSERT_OK(sockaddr_un_set_path(&sa.un, sockpath)); |
| 808 |
ASSERT_OK_ERRNO(bind(listener, &sa.sa, sockaddr_un_len(&sa.un))); |
| 809 |
ASSERT_OK_ERRNO(listen(listener, 1)); |
| 810 |
|
| 811 |
ASSERT_ERROR(read_one_line_file_at(XAT_FDROOT, sockpath + 1, &buf), ENXIO); |
| 812 |
|
| 813 |
/* But read_full_file_full() with READ_FULL_FILE_CONNECT_SOCKET *does* enable the socket fallback, |
| 814 |
* which routes through xfopenat_unix_socket() and connect_unix_path() — both now teach the |
| 815 |
* XAT_FDROOT codepath. Use that to exercise the socket open via XAT_FDROOT. */ |
| 816 |
static const char test_sock_str[] = "hello via xat_fdroot socket\n"; |
| 817 |
|
| 818 |
_cleanup_(pidref_done) PidRef pidref = PIDREF_NULL; |
| 819 |
int pr = ASSERT_OK(pidref_safe_fork("(server)", FORK_DEATHSIG_SIGTERM|FORK_LOG, &pidref)); |
| 820 |
if (pr == 0) { |
| 821 |
_cleanup_close_ int rfd = -EBADF; |
| 822 |
ASSERT_OK(rfd = accept4(listener, /* addr= */ NULL, /* addrlen= */ NULL, SOCK_CLOEXEC)); |
| 823 |
ASSERT_OK_EQ_ERRNO(write(rfd, test_sock_str, sizeof(test_sock_str) - 1), |
| 824 |
(ssize_t) sizeof(test_sock_str) - 1); |
| 825 |
_exit(EXIT_SUCCESS); |
| 826 |
} |
| 827 |
|
| 828 |
_cleanup_free_ char *data = NULL; |
| 829 |
size_t size; |
| 830 |
ASSERT_OK(read_full_file_full(XAT_FDROOT, sockpath + 1, |
| 831 |
/* offset= */ UINT64_MAX, /* size= */ SIZE_MAX, |
| 832 |
READ_FULL_FILE_CONNECT_SOCKET, /* bind_name= */ NULL, |
| 833 |
&data, &size)); |
| 834 |
ASSERT_EQ(size, sizeof(test_sock_str) - 1); |
| 835 |
ASSERT_STREQ(data, test_sock_str); |
| 836 |
|
| 837 |
ASSERT_OK(pidref_wait_for_terminate_and_check("(server)", &pidref, WAIT_LOG)); |
| 838 |
} |
| 839 |
|
| 840 |
TEST(read_boolean_file) { |
| 841 |
_cleanup_(unlink_tempfilep) char fn[] = "/tmp/test-read-boolean-file-XXXXXX"; |
| 842 |
_cleanup_close_ int fd = -EBADF, dfd = -EBADF; |
| 843 |
const char *rel; |
| 844 |
|
| 845 |
ASSERT_OK(fd = mkostemp_safe(fn)); |
| 846 |
|
| 847 |
ASSERT_OK(write_string_file(fn, "yes", WRITE_STRING_FILE_TRUNCATE)); |
| 848 |
ASSERT_OK_EQ(read_boolean_file(fn), true); |
| 849 |
ASSERT_OK_EQ(read_boolean_file_at(AT_FDCWD, fn), true); |
| 850 |
|
| 851 |
ASSERT_OK(write_string_file(fn, "0", WRITE_STRING_FILE_TRUNCATE)); |
| 852 |
ASSERT_OK_EQ(read_boolean_file(fn), false); |
| 853 |
ASSERT_OK_EQ(read_boolean_file_at(AT_FDCWD, fn), false); |
| 854 |
|
| 855 |
ASSERT_OK(write_string_file(fn, "true\nignored\n", WRITE_STRING_FILE_TRUNCATE)); |
| 856 |
ASSERT_OK_EQ(read_boolean_file(fn), true); |
| 857 |
|
| 858 |
ASSERT_OK(write_string_file(fn, "garbage", WRITE_STRING_FILE_TRUNCATE)); |
| 859 |
ASSERT_ERROR(read_boolean_file(fn), EINVAL); |
| 860 |
|
| 861 |
ASSERT_ERROR(read_boolean_file("/tmp/this-file-better-not-exist-XXX"), ENOENT); |
| 862 |
|
| 863 |
/* Now test XAT_FDROOT: filename is relative, looked up against "/" */ |
| 864 |
ASSERT_TRUE(path_startswith(fn, "/")); |
| 865 |
rel = fn + 1; |
| 866 |
|
| 867 |
ASSERT_OK(write_string_file(fn, "on", WRITE_STRING_FILE_TRUNCATE)); |
| 868 |
ASSERT_OK_EQ(read_boolean_file_at(XAT_FDROOT, rel), true); |
| 869 |
|
| 870 |
ASSERT_OK(write_string_file(fn, "off", WRITE_STRING_FILE_TRUNCATE)); |
| 871 |
ASSERT_OK_EQ(read_boolean_file_at(XAT_FDROOT, rel), false); |
| 872 |
|
| 873 |
ASSERT_ERROR(read_boolean_file_at(XAT_FDROOT, "tmp/this-file-better-not-exist-XXX"), ENOENT); |
| 874 |
|
| 875 |
/* And confirm XAT_FDROOT ignores the cwd: chdir somewhere unrelated, then look up |
| 876 |
* the same relative-to-/ path. */ |
| 877 |
_cleanup_free_ char *cwd = NULL; |
| 878 |
ASSERT_OK(safe_getcwd(&cwd)); |
| 879 |
ASSERT_OK_ERRNO(chdir("/usr")); |
| 880 |
|
| 881 |
ASSERT_OK(write_string_file(fn, "yes", WRITE_STRING_FILE_TRUNCATE)); |
| 882 |
ASSERT_OK_EQ(read_boolean_file_at(XAT_FDROOT, rel), true); |
| 883 |
|
| 884 |
ASSERT_OK_ERRNO(chdir(cwd)); |
| 885 |
|
| 886 |
/* Also test the dir_fd >= 0 path using an actual fd for /tmp. */ |
| 887 |
ASSERT_OK(dfd = open("/tmp", O_DIRECTORY|O_CLOEXEC)); |
| 888 |
ASSERT_OK(write_string_file(fn, "1", WRITE_STRING_FILE_TRUNCATE)); |
| 889 |
_cleanup_free_ char *bn = NULL; |
| 890 |
ASSERT_OK(path_extract_filename(fn, &bn)); |
| 891 |
ASSERT_OK_EQ(read_boolean_file_at(dfd, bn), true); |
| 892 |
} |
| 893 |
|
| 894 |
DEFINE_TEST_MAIN(LOG_DEBUG); |