| File: | build-scan/../src/basic/lockfile-util.c | 
| Warning: | line 46, column 33 Potential leak of memory pointed to by 't' | 
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
| 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ | |||
| 2 | ||||
| 3 | #include <errno(*__errno_location ()).h> | |||
| 4 | #include <fcntl.h> | |||
| 5 | #include <stdio.h> | |||
| 6 | #include <string.h> | |||
| 7 | #include <sys/file.h> | |||
| 8 | #include <sys/stat.h> | |||
| 9 | ||||
| 10 | #include "alloc-util.h" | |||
| 11 | #include "fd-util.h" | |||
| 12 | #include "fs-util.h" | |||
| 13 | #include "lockfile-util.h" | |||
| 14 | #include "macro.h" | |||
| 15 | #include "path-util.h" | |||
| 16 | ||||
| 17 | int make_lock_file(const char *p, int operation, LockFile *ret) { | |||
| 18 | _cleanup_close___attribute__((cleanup(closep))) int fd = -1; | |||
| 19 | _cleanup_free___attribute__((cleanup(freep))) char *t = NULL((void*)0); | |||
| 20 | int r; | |||
| 21 | ||||
| 22 | /* | |||
| 23 | * We use UNPOSIX locks if they are available. They have nice | |||
| 24 | * semantics, and are mostly compatible with NFS. However, | |||
| 25 | * they are only available on new kernels. When we detect we | |||
| 26 | * are running on an older kernel, then we fall back to good | |||
| 27 | * old BSD locks. They also have nice semantics, but are | |||
| 28 | * slightly problematic on NFS, where they are upgraded to | |||
| 29 | * POSIX locks, even though locally they are orthogonal to | |||
| 30 | * POSIX locks. | |||
| 31 | */ | |||
| 32 | ||||
| 33 | t = strdup(p); | |||
| 34 | if (!t) | |||
| 35 | return -ENOMEM12; | |||
| 36 | ||||
| 37 | for (;;) { | |||
| 38 | struct flock fl = { | |||
| 39 | .l_type = (operation & ~LOCK_NB4) == LOCK_EX2 ? F_WRLCK1 : F_RDLCK0, | |||
| 40 | .l_whence = SEEK_SET0, | |||
| 41 | }; | |||
| 42 | struct stat st; | |||
| 43 | ||||
| 44 | fd = open(p, O_CREAT0100|O_RDWR02|O_NOFOLLOW0400000|O_CLOEXEC02000000|O_NOCTTY0400, 0600); | |||
| 45 | if (fd < 0) | |||
| 46 | return -errno(*__errno_location ()); | |||
| 
 | ||||
| 47 | ||||
| 48 | r = fcntl(fd, (operation & LOCK_NB4) ? F_OFD_SETLK37 : F_OFD_SETLKW38, &fl); | |||
| 49 | if (r < 0) { | |||
| 50 | ||||
| 51 | /* If the kernel is too old, use good old BSD locks */ | |||
| 52 | if (errno(*__errno_location ()) == EINVAL22) | |||
| 53 | r = flock(fd, operation); | |||
| 54 | ||||
| 55 | if (r < 0) | |||
| 56 | return errno(*__errno_location ()) == EAGAIN11 ? -EBUSY16 : -errno(*__errno_location ()); | |||
| 57 | } | |||
| 58 | ||||
| 59 | /* If we acquired the lock, let's check if the file | |||
| 60 | * still exists in the file system. If not, then the | |||
| 61 | * previous exclusive owner removed it and then closed | |||
| 62 | * it. In such a case our acquired lock is worthless, | |||
| 63 | * hence try again. */ | |||
| 64 | ||||
| 65 | r = fstat(fd, &st); | |||
| 66 | if (r < 0) | |||
| 67 | return -errno(*__errno_location ()); | |||
| 68 | if (st.st_nlink > 0) | |||
| 69 | break; | |||
| 70 | ||||
| 71 | fd = safe_close(fd); | |||
| 72 | } | |||
| 73 | ||||
| 74 | ret->path = t; | |||
| 75 | ret->fd = fd; | |||
| 76 | ret->operation = operation; | |||
| 77 | ||||
| 78 | fd = -1; | |||
| 79 | t = NULL((void*)0); | |||
| 80 | ||||
| 81 | return r; | |||
| 82 | } | |||
| 83 | ||||
| 84 | int make_lock_file_for(const char *p, int operation, LockFile *ret) { | |||
| 85 | const char *fn; | |||
| 86 | char *t; | |||
| 87 | ||||
| 88 | assert(p)do { if ((__builtin_expect(!!(!(p)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("p"), "../src/basic/lockfile-util.c", 88 , __PRETTY_FUNCTION__); } while (0); | |||
| 
 | ||||
| 89 | assert(ret)do { if ((__builtin_expect(!!(!(ret)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("ret"), "../src/basic/lockfile-util.c", 89 , __PRETTY_FUNCTION__); } while (0); | |||
| 90 | ||||
| 91 | fn = basename(p); | |||
| 92 | if (!filename_is_valid(fn)) | |||
| 93 | return -EINVAL22; | |||
| 94 | ||||
| 95 | t = newa(char, strlen(p) + 2 + 4 + 1)({ do { if ((__builtin_expect(!!(!(!size_multiply_overflow(sizeof (char), strlen(p) + 2 + 4 + 1))),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("!size_multiply_overflow(sizeof(char), strlen(p) + 2 + 4 + 1)" ), "../src/basic/lockfile-util.c", 95, __PRETTY_FUNCTION__); } while (0); (char*) __builtin_alloca (sizeof(char)*(strlen(p) + 2 + 4 + 1)); }); | |||
| 96 | stpcpy(stpcpy(stpcpy(mempcpy(t, p, fn - p), ".#"), fn), ".lck"); | |||
| 97 | ||||
| 98 | return make_lock_file(t, operation, ret); | |||
| 99 | } | |||
| 100 | ||||
| 101 | void release_lock_file(LockFile *f) { | |||
| 102 | int r; | |||
| 103 | ||||
| 104 | if (!f) | |||
| 105 | return; | |||
| 106 | ||||
| 107 | if (f->path) { | |||
| 108 | ||||
| 109 | /* If we are the exclusive owner we can safely delete | |||
| 110 | * the lock file itself. If we are not the exclusive | |||
| 111 | * owner, we can try becoming it. */ | |||
| 112 | ||||
| 113 | if (f->fd >= 0 && | |||
| 114 | (f->operation & ~LOCK_NB4) == LOCK_SH1) { | |||
| 115 | static const struct flock fl = { | |||
| 116 | .l_type = F_WRLCK1, | |||
| 117 | .l_whence = SEEK_SET0, | |||
| 118 | }; | |||
| 119 | ||||
| 120 | r = fcntl(f->fd, F_OFD_SETLK37, &fl); | |||
| 121 | if (r < 0 && errno(*__errno_location ()) == EINVAL22) | |||
| 122 | r = flock(f->fd, LOCK_EX2|LOCK_NB4); | |||
| 123 | ||||
| 124 | if (r >= 0) | |||
| 125 | f->operation = LOCK_EX2|LOCK_NB4; | |||
| 126 | } | |||
| 127 | ||||
| 128 | if ((f->operation & ~LOCK_NB4) == LOCK_EX2) | |||
| 129 | unlink_noerrno(f->path); | |||
| 130 | ||||
| 131 | f->path = mfree(f->path); | |||
| 132 | } | |||
| 133 | ||||
| 134 | f->fd = safe_close(f->fd); | |||
| 135 | f->operation = 0; | |||
| 136 | } |