| 484 | } |
| 485 | |
| 486 | static int posix_lock_file_open(const posix_log_path_t *path, bool *created_out) { |
| 487 | int flags = O_RDWR | O_CLOEXEC | O_NOFOLLOW | O_NONBLOCK; |
| 488 | int fd = openat(path->dir_fd, path->lock, flags | O_CREAT | O_EXCL, 0600); |
| 489 | bool created = fd >= 0; |
| 490 | if (fd < 0 && errno == EEXIST) { |
| 491 | fd = openat(path->dir_fd, path->lock, flags); |
| 492 | } |
| 493 | struct stat status; |
| 494 | struct stat by_path; |
| 495 | if (fd < 0 || !fd_cloexec(fd) || !fd_regular_current_user(fd, &status) || |
| 496 | fchmod(fd, 0600) != 0 || |
| 497 | fstatat(path->dir_fd, path->lock, &by_path, AT_SYMLINK_NOFOLLOW) != 0 || |
| 498 | !S_ISREG(by_path.st_mode) || by_path.st_uid != geteuid() || by_path.st_nlink != 1 || |
| 499 | status.st_dev != by_path.st_dev || status.st_ino != by_path.st_ino) { |
| 500 | if (fd >= 0) { |
| 501 | (void)close(fd); |
| 502 | } |
| 503 | return -1; |
| 504 | } |
| 505 | *created_out = created; |
| 506 | return fd; |
| 507 | } |
| 508 | |
| 509 | static bool posix_lock_exclusive(int fd) { |
| 510 | while (flock(fd, LOCK_EX) != 0) { |
no test coverage detected