2 01b7ba6b 2019-03-11 stsp * Copyright (c) 2019 Stefan Sperling <stsp@openbsd.org>
4 01b7ba6b 2019-03-11 stsp * Permission to use, copy, modify, and distribute this software for any
5 01b7ba6b 2019-03-11 stsp * purpose with or without fee is hereby granted, provided that the above
6 01b7ba6b 2019-03-11 stsp * copyright notice and this permission notice appear in all copies.
8 01b7ba6b 2019-03-11 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 01b7ba6b 2019-03-11 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 01b7ba6b 2019-03-11 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 01b7ba6b 2019-03-11 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 01b7ba6b 2019-03-11 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 01b7ba6b 2019-03-11 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 01b7ba6b 2019-03-11 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 01b7ba6b 2019-03-11 stsp #include <sys/stat.h>
18 01b7ba6b 2019-03-11 stsp #include <sys/queue.h>
20 01b7ba6b 2019-03-11 stsp #include <errno.h>
21 01b7ba6b 2019-03-11 stsp #include <fcntl.h>
22 01b7ba6b 2019-03-11 stsp #include <stdlib.h>
23 01b7ba6b 2019-03-11 stsp #include <unistd.h>
24 01b7ba6b 2019-03-11 stsp #include <string.h>
25 01b7ba6b 2019-03-11 stsp #include <stdio.h>
26 01b7ba6b 2019-03-11 stsp #include <time.h>
28 01b7ba6b 2019-03-11 stsp #include "got_error.h"
29 324d37e7 2019-05-11 stsp #include "got_path.h"
31 01b7ba6b 2019-03-11 stsp #include "got_lib_lockfile.h"
33 01b7ba6b 2019-03-11 stsp const struct got_error *
34 5345b4c7 2021-07-06 stsp got_lockfile_lock(struct got_lockfile **lf, const char *path, int dir_fd)
36 01b7ba6b 2019-03-11 stsp const struct got_error *err = NULL;
37 01b7ba6b 2019-03-11 stsp int attempts = 5;
39 47112f60 2019-03-11 stsp *lf = calloc(1, sizeof(**lf));
40 01b7ba6b 2019-03-11 stsp if (*lf == NULL)
41 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
42 01b7ba6b 2019-03-11 stsp (*lf)->fd = -1;
44 01b7ba6b 2019-03-11 stsp (*lf)->locked_path = strdup(path);
45 01b7ba6b 2019-03-11 stsp if ((*lf)->locked_path == NULL) {
46 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
50 01b7ba6b 2019-03-11 stsp if (asprintf(&(*lf)->path, "%s%s", path, GOT_LOCKFILE_SUFFIX) == -1) {
51 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
56 5345b4c7 2021-07-06 stsp if (dir_fd != -1) {
57 5345b4c7 2021-07-06 stsp (*lf)->fd = openat(dir_fd, (*lf)->path,
58 5345b4c7 2021-07-06 stsp O_RDONLY | O_CREAT | O_EXCL | O_EXLOCK,
59 5345b4c7 2021-07-06 stsp GOT_DEFAULT_FILE_MODE);
61 5345b4c7 2021-07-06 stsp (*lf)->fd = open((*lf)->path,
62 5345b4c7 2021-07-06 stsp O_RDONLY | O_CREAT | O_EXCL | O_EXLOCK,
63 5345b4c7 2021-07-06 stsp GOT_DEFAULT_FILE_MODE);
65 c789dbac 2019-03-11 stsp if ((*lf)->fd != -1)
67 c789dbac 2019-03-11 stsp if (errno != EEXIST) {
68 638f9024 2019-05-13 stsp err = got_error_from_errno2("open", (*lf)->path);
72 01b7ba6b 2019-03-11 stsp } while (--attempts > 0);
74 01b7ba6b 2019-03-11 stsp if ((*lf)->fd == -1)
75 01b7ba6b 2019-03-11 stsp err = got_error(GOT_ERR_LOCKFILE_TIMEOUT);
78 5345b4c7 2021-07-06 stsp got_lockfile_unlock(*lf, dir_fd);
84 01b7ba6b 2019-03-11 stsp const struct got_error *
85 5345b4c7 2021-07-06 stsp got_lockfile_unlock(struct got_lockfile *lf, int dir_fd)
87 01b7ba6b 2019-03-11 stsp const struct got_error *err = NULL;
89 5345b4c7 2021-07-06 stsp if (dir_fd != -1) {
90 5345b4c7 2021-07-06 stsp if (lf->path && lf->fd != -1 &&
91 5345b4c7 2021-07-06 stsp unlinkat(dir_fd, lf->path, 0) != 0)
92 5345b4c7 2021-07-06 stsp err = got_error_from_errno("unlinkat");
93 5345b4c7 2021-07-06 stsp } else if (lf->path && lf->fd != -1 && unlink(lf->path) != 0)
94 638f9024 2019-05-13 stsp err = got_error_from_errno("unlink");
95 08578a35 2021-01-22 stsp if (lf->fd != -1 && close(lf->fd) == -1 && err == NULL)
96 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
97 01b7ba6b 2019-03-11 stsp free(lf->path);
98 01b7ba6b 2019-03-11 stsp free(lf->locked_path);
100 01b7ba6b 2019-03-11 stsp return err;