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 4fccd2fe 2023-03-08 thomas #include "got_compat.h"
18 4fccd2fe 2023-03-08 thomas
19 01b7ba6b 2019-03-11 stsp #include <sys/stat.h>
20 8b925c6c 2022-07-16 thomas #include <sys/queue.h>
21 01b7ba6b 2019-03-11 stsp
22 01b7ba6b 2019-03-11 stsp #include <errno.h>
23 01b7ba6b 2019-03-11 stsp #include <fcntl.h>
24 01b7ba6b 2019-03-11 stsp #include <stdlib.h>
25 01b7ba6b 2019-03-11 stsp #include <unistd.h>
26 01b7ba6b 2019-03-11 stsp #include <string.h>
27 01b7ba6b 2019-03-11 stsp #include <stdio.h>
28 01b7ba6b 2019-03-11 stsp #include <time.h>
29 01b7ba6b 2019-03-11 stsp
30 01b7ba6b 2019-03-11 stsp #include "got_error.h"
31 324d37e7 2019-05-11 stsp #include "got_path.h"
32 01b7ba6b 2019-03-11 stsp
33 01b7ba6b 2019-03-11 stsp #include "got_lib_lockfile.h"
34 01b7ba6b 2019-03-11 stsp
35 01b7ba6b 2019-03-11 stsp const struct got_error *
36 5345b4c7 2021-07-06 stsp got_lockfile_lock(struct got_lockfile **lf, const char *path, int dir_fd)
37 01b7ba6b 2019-03-11 stsp {
38 01b7ba6b 2019-03-11 stsp const struct got_error *err = NULL;
39 01b7ba6b 2019-03-11 stsp int attempts = 5;
40 01b7ba6b 2019-03-11 stsp
41 47112f60 2019-03-11 stsp *lf = calloc(1, sizeof(**lf));
42 01b7ba6b 2019-03-11 stsp if (*lf == NULL)
43 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
44 01b7ba6b 2019-03-11 stsp (*lf)->fd = -1;
45 01b7ba6b 2019-03-11 stsp
46 01b7ba6b 2019-03-11 stsp (*lf)->locked_path = strdup(path);
47 01b7ba6b 2019-03-11 stsp if ((*lf)->locked_path == NULL) {
48 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
49 01b7ba6b 2019-03-11 stsp goto done;
50 01b7ba6b 2019-03-11 stsp }
51 01b7ba6b 2019-03-11 stsp
52 01b7ba6b 2019-03-11 stsp if (asprintf(&(*lf)->path, "%s%s", path, GOT_LOCKFILE_SUFFIX) == -1) {
53 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
54 01b7ba6b 2019-03-11 stsp goto done;
55 01b7ba6b 2019-03-11 stsp }
56 01b7ba6b 2019-03-11 stsp
57 01b7ba6b 2019-03-11 stsp do {
58 5345b4c7 2021-07-06 stsp if (dir_fd != -1) {
59 5345b4c7 2021-07-06 stsp (*lf)->fd = openat(dir_fd, (*lf)->path,
60 e81b604b 2023-06-22 thomas O_RDWR | O_CREAT | O_EXCL | O_EXLOCK | O_CLOEXEC,
61 5345b4c7 2021-07-06 stsp GOT_DEFAULT_FILE_MODE);
62 5345b4c7 2021-07-06 stsp } else {
63 5345b4c7 2021-07-06 stsp (*lf)->fd = open((*lf)->path,
64 e81b604b 2023-06-22 thomas O_RDWR | O_CREAT | O_EXCL | O_EXLOCK | O_CLOEXEC,
65 5345b4c7 2021-07-06 stsp GOT_DEFAULT_FILE_MODE);
66 5345b4c7 2021-07-06 stsp }
67 c789dbac 2019-03-11 stsp if ((*lf)->fd != -1)
68 c789dbac 2019-03-11 stsp break;
69 c789dbac 2019-03-11 stsp if (errno != EEXIST) {
70 638f9024 2019-05-13 stsp err = got_error_from_errno2("open", (*lf)->path);
71 c789dbac 2019-03-11 stsp goto done;
72 01b7ba6b 2019-03-11 stsp }
73 c789dbac 2019-03-11 stsp sleep(1);
74 01b7ba6b 2019-03-11 stsp } while (--attempts > 0);
75 01b7ba6b 2019-03-11 stsp
76 e8f803b5 2023-06-25 thomas if ((*lf)->fd == -1) {
77 e8f803b5 2023-06-25 thomas err = got_error_fmt(GOT_ERR_LOCKFILE_TIMEOUT,
78 e8f803b5 2023-06-25 thomas "%s", (*lf)->path);
79 e8f803b5 2023-06-25 thomas }
80 01b7ba6b 2019-03-11 stsp done:
81 01b7ba6b 2019-03-11 stsp if (err) {
82 5345b4c7 2021-07-06 stsp got_lockfile_unlock(*lf, dir_fd);
83 01b7ba6b 2019-03-11 stsp *lf = NULL;
84 01b7ba6b 2019-03-11 stsp }
85 01b7ba6b 2019-03-11 stsp return err;
86 01b7ba6b 2019-03-11 stsp }
87 01b7ba6b 2019-03-11 stsp
88 01b7ba6b 2019-03-11 stsp const struct got_error *
89 5345b4c7 2021-07-06 stsp got_lockfile_unlock(struct got_lockfile *lf, int dir_fd)
90 01b7ba6b 2019-03-11 stsp {
91 01b7ba6b 2019-03-11 stsp const struct got_error *err = NULL;
92 01b7ba6b 2019-03-11 stsp
93 5345b4c7 2021-07-06 stsp if (dir_fd != -1) {
94 5345b4c7 2021-07-06 stsp if (lf->path && lf->fd != -1 &&
95 5345b4c7 2021-07-06 stsp unlinkat(dir_fd, lf->path, 0) != 0)
96 5345b4c7 2021-07-06 stsp err = got_error_from_errno("unlinkat");
97 5345b4c7 2021-07-06 stsp } else if (lf->path && lf->fd != -1 && unlink(lf->path) != 0)
98 638f9024 2019-05-13 stsp err = got_error_from_errno("unlink");
99 08578a35 2021-01-22 stsp if (lf->fd != -1 && close(lf->fd) == -1 && err == NULL)
100 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
101 01b7ba6b 2019-03-11 stsp free(lf->path);
102 01b7ba6b 2019-03-11 stsp free(lf->locked_path);
103 01b7ba6b 2019-03-11 stsp free(lf);
104 01b7ba6b 2019-03-11 stsp return err;
105 01b7ba6b 2019-03-11 stsp }