Blame


1 01b7ba6b 2019-03-11 stsp /*
2 01b7ba6b 2019-03-11 stsp * Copyright (c) 2019 Stefan Sperling <stsp@openbsd.org>
3 01b7ba6b 2019-03-11 stsp *
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.
7 01b7ba6b 2019-03-11 stsp *
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.
15 01b7ba6b 2019-03-11 stsp */
16 01b7ba6b 2019-03-11 stsp
17 01b7ba6b 2019-03-11 stsp #include <sys/stat.h>
18 01b7ba6b 2019-03-11 stsp #include <sys/queue.h>
19 01b7ba6b 2019-03-11 stsp
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>
27 01b7ba6b 2019-03-11 stsp
28 01b7ba6b 2019-03-11 stsp #include "got_error.h"
29 324d37e7 2019-05-11 stsp #include "got_path.h"
30 01b7ba6b 2019-03-11 stsp
31 01b7ba6b 2019-03-11 stsp #include "got_lib_lockfile.h"
32 01b7ba6b 2019-03-11 stsp
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)
35 01b7ba6b 2019-03-11 stsp {
36 01b7ba6b 2019-03-11 stsp const struct got_error *err = NULL;
37 01b7ba6b 2019-03-11 stsp int attempts = 5;
38 01b7ba6b 2019-03-11 stsp
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;
43 01b7ba6b 2019-03-11 stsp
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");
47 01b7ba6b 2019-03-11 stsp goto done;
48 01b7ba6b 2019-03-11 stsp }
49 01b7ba6b 2019-03-11 stsp
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");
52 01b7ba6b 2019-03-11 stsp goto done;
53 01b7ba6b 2019-03-11 stsp }
54 01b7ba6b 2019-03-11 stsp
55 01b7ba6b 2019-03-11 stsp do {
56 5345b4c7 2021-07-06 stsp if (dir_fd != -1) {
57 5345b4c7 2021-07-06 stsp (*lf)->fd = openat(dir_fd, (*lf)->path,
58 e7ae0baf 2021-12-31 stsp O_RDONLY | O_CREAT | O_EXCL | O_EXLOCK | O_CLOEXEC,
59 5345b4c7 2021-07-06 stsp GOT_DEFAULT_FILE_MODE);
60 5345b4c7 2021-07-06 stsp } else {
61 5345b4c7 2021-07-06 stsp (*lf)->fd = open((*lf)->path,
62 8bd0cdad 2021-12-31 stsp O_RDONLY | O_CREAT | O_EXCL | O_EXLOCK | O_CLOEXEC,
63 5345b4c7 2021-07-06 stsp GOT_DEFAULT_FILE_MODE);
64 5345b4c7 2021-07-06 stsp }
65 c789dbac 2019-03-11 stsp if ((*lf)->fd != -1)
66 c789dbac 2019-03-11 stsp break;
67 c789dbac 2019-03-11 stsp if (errno != EEXIST) {
68 638f9024 2019-05-13 stsp err = got_error_from_errno2("open", (*lf)->path);
69 c789dbac 2019-03-11 stsp goto done;
70 01b7ba6b 2019-03-11 stsp }
71 c789dbac 2019-03-11 stsp sleep(1);
72 01b7ba6b 2019-03-11 stsp } while (--attempts > 0);
73 01b7ba6b 2019-03-11 stsp
74 01b7ba6b 2019-03-11 stsp if ((*lf)->fd == -1)
75 01b7ba6b 2019-03-11 stsp err = got_error(GOT_ERR_LOCKFILE_TIMEOUT);
76 01b7ba6b 2019-03-11 stsp done:
77 01b7ba6b 2019-03-11 stsp if (err) {
78 5345b4c7 2021-07-06 stsp got_lockfile_unlock(*lf, dir_fd);
79 01b7ba6b 2019-03-11 stsp *lf = NULL;
80 01b7ba6b 2019-03-11 stsp }
81 01b7ba6b 2019-03-11 stsp return err;
82 01b7ba6b 2019-03-11 stsp }
83 01b7ba6b 2019-03-11 stsp
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)
86 01b7ba6b 2019-03-11 stsp {
87 01b7ba6b 2019-03-11 stsp const struct got_error *err = NULL;
88 01b7ba6b 2019-03-11 stsp
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);
99 01b7ba6b 2019-03-11 stsp free(lf);
100 01b7ba6b 2019-03-11 stsp return err;
101 01b7ba6b 2019-03-11 stsp }