| .. | .. |
|---|
| 25 | 25 | #define __S_IFREG S_IFREG |
|---|
| 26 | 26 | #endif |
|---|
| 27 | 27 | |
|---|
| 28 | +unsigned int rnd(unsigned int max, unsigned int *seed) |
|---|
| 29 | +{ |
|---|
| 30 | + return rand_r(seed) * ((uint64_t)max + 1) / RAND_MAX; |
|---|
| 31 | +} |
|---|
| 32 | + |
|---|
| 33 | +int remove_dir(const char *dir) |
|---|
| 34 | +{ |
|---|
| 35 | + int err = rmdir(dir); |
|---|
| 36 | + |
|---|
| 37 | + if (err && errno == ENOTEMPTY) { |
|---|
| 38 | + err = delete_dir_tree(dir); |
|---|
| 39 | + if (err) |
|---|
| 40 | + return err; |
|---|
| 41 | + return 0; |
|---|
| 42 | + } |
|---|
| 43 | + |
|---|
| 44 | + if (err && errno != ENOENT) |
|---|
| 45 | + return -errno; |
|---|
| 46 | + |
|---|
| 47 | + return 0; |
|---|
| 48 | +} |
|---|
| 49 | + |
|---|
| 50 | +int drop_caches(void) |
|---|
| 51 | +{ |
|---|
| 52 | + int drop_caches = |
|---|
| 53 | + open("/proc/sys/vm/drop_caches", O_WRONLY | O_CLOEXEC); |
|---|
| 54 | + int i; |
|---|
| 55 | + |
|---|
| 56 | + if (drop_caches == -1) |
|---|
| 57 | + return -errno; |
|---|
| 58 | + i = write(drop_caches, "3", 1); |
|---|
| 59 | + close(drop_caches); |
|---|
| 60 | + |
|---|
| 61 | + if (i != 1) |
|---|
| 62 | + return -errno; |
|---|
| 63 | + |
|---|
| 64 | + return 0; |
|---|
| 65 | +} |
|---|
| 66 | + |
|---|
| 28 | 67 | int mount_fs(const char *mount_dir, const char *backing_dir, |
|---|
| 29 | 68 | int read_timeout_ms) |
|---|
| 30 | 69 | { |
|---|
| .. | .. |
|---|
| 77 | 116 | size_t size = sizeof(struct signature_blob) + strlen(add_data) + 1; |
|---|
| 78 | 117 | struct signature_blob *sb = malloc(size); |
|---|
| 79 | 118 | |
|---|
| 119 | + if (!sb) |
|---|
| 120 | + return 0; |
|---|
| 121 | + |
|---|
| 80 | 122 | *sb = (struct signature_blob){ |
|---|
| 81 | 123 | .version = INCFS_SIGNATURE_VERSION, |
|---|
| 82 | 124 | .hash_section_size = sizeof(struct hash_section), |
|---|
| .. | .. |
|---|
| 87 | 129 | .salt_size = 0, |
|---|
| 88 | 130 | .hash_size = SHA256_DIGEST_SIZE, |
|---|
| 89 | 131 | }, |
|---|
| 90 | | - .signing_section_size = sizeof(uint32_t) + strlen(add_data) + 1, |
|---|
| 132 | + .signing_section_size = strlen(add_data) + 1, |
|---|
| 91 | 133 | }; |
|---|
| 92 | 134 | |
|---|
| 93 | 135 | memcpy(sb->hash_section.hash, root_hash, SHA256_DIGEST_SIZE); |
|---|
| .. | .. |
|---|
| 195 | 237 | |
|---|
| 196 | 238 | int open_log_file(const char *mount_dir) |
|---|
| 197 | 239 | { |
|---|
| 198 | | - char cmd_file[255]; |
|---|
| 199 | | - int cmd_fd; |
|---|
| 240 | + char file[255]; |
|---|
| 241 | + int fd; |
|---|
| 200 | 242 | |
|---|
| 201 | | - snprintf(cmd_file, ARRAY_SIZE(cmd_file), "%s/.log", mount_dir); |
|---|
| 202 | | - cmd_fd = open(cmd_file, O_RDWR | O_CLOEXEC); |
|---|
| 203 | | - if (cmd_fd < 0) |
|---|
| 243 | + snprintf(file, ARRAY_SIZE(file), "%s/.log", mount_dir); |
|---|
| 244 | + fd = open(file, O_RDWR | O_CLOEXEC); |
|---|
| 245 | + if (fd < 0) |
|---|
| 204 | 246 | perror("Can't open log file"); |
|---|
| 205 | | - return cmd_fd; |
|---|
| 247 | + return fd; |
|---|
| 248 | +} |
|---|
| 249 | + |
|---|
| 250 | +int open_blocks_written_file(const char *mount_dir) |
|---|
| 251 | +{ |
|---|
| 252 | + char file[255]; |
|---|
| 253 | + int fd; |
|---|
| 254 | + |
|---|
| 255 | + snprintf(file, ARRAY_SIZE(file), |
|---|
| 256 | + "%s/%s", mount_dir, INCFS_BLOCKS_WRITTEN_FILENAME); |
|---|
| 257 | + fd = open(file, O_RDONLY | O_CLOEXEC); |
|---|
| 258 | + |
|---|
| 259 | + if (fd < 0) |
|---|
| 260 | + perror("Can't open blocks_written file"); |
|---|
| 261 | + return fd; |
|---|
| 206 | 262 | } |
|---|
| 207 | 263 | |
|---|
| 208 | 264 | int wait_for_pending_reads(int fd, int timeout_ms, |
|---|
| .. | .. |
|---|
| 233 | 289 | return read_res / sizeof(*prs); |
|---|
| 234 | 290 | } |
|---|
| 235 | 291 | |
|---|
| 236 | | -char *concat_file_name(const char *dir, char *file) |
|---|
| 292 | +int wait_for_pending_reads2(int fd, int timeout_ms, |
|---|
| 293 | + struct incfs_pending_read_info2 *prs, int prs_count) |
|---|
| 294 | +{ |
|---|
| 295 | + ssize_t read_res = 0; |
|---|
| 296 | + |
|---|
| 297 | + if (timeout_ms > 0) { |
|---|
| 298 | + int poll_res = 0; |
|---|
| 299 | + struct pollfd pollfd = { |
|---|
| 300 | + .fd = fd, |
|---|
| 301 | + .events = POLLIN |
|---|
| 302 | + }; |
|---|
| 303 | + |
|---|
| 304 | + poll_res = poll(&pollfd, 1, timeout_ms); |
|---|
| 305 | + if (poll_res < 0) |
|---|
| 306 | + return -errno; |
|---|
| 307 | + if (poll_res == 0) |
|---|
| 308 | + return 0; |
|---|
| 309 | + if (!(pollfd.revents | POLLIN)) |
|---|
| 310 | + return 0; |
|---|
| 311 | + } |
|---|
| 312 | + |
|---|
| 313 | + read_res = read(fd, prs, prs_count * sizeof(*prs)); |
|---|
| 314 | + if (read_res < 0) |
|---|
| 315 | + return -errno; |
|---|
| 316 | + |
|---|
| 317 | + return read_res / sizeof(*prs); |
|---|
| 318 | +} |
|---|
| 319 | + |
|---|
| 320 | +char *concat_file_name(const char *dir, const char *file) |
|---|
| 237 | 321 | { |
|---|
| 238 | 322 | char full_name[FILENAME_MAX] = ""; |
|---|
| 239 | 323 | |
|---|