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 01b7ba6b 2019-03-11 stsp got_lockfile_lock(struct got_lockfile **lf, const char *path)
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 3eb93f3c 2019-04-11 stsp (*lf)->fd = open((*lf)->path,
57 3eb93f3c 2019-04-11 stsp O_RDONLY | O_CREAT | O_EXCL | O_EXLOCK,
58 3eb93f3c 2019-04-11 stsp GOT_DEFAULT_FILE_MODE);
59 c789dbac 2019-03-11 stsp if ((*lf)->fd != -1)
60 c789dbac 2019-03-11 stsp break;
61 c789dbac 2019-03-11 stsp if (errno != EEXIST) {
62 638f9024 2019-05-13 stsp err = got_error_from_errno2("open", (*lf)->path);
63 c789dbac 2019-03-11 stsp goto done;
64 01b7ba6b 2019-03-11 stsp }
65 c789dbac 2019-03-11 stsp sleep(1);
66 01b7ba6b 2019-03-11 stsp } while (--attempts > 0);
67 01b7ba6b 2019-03-11 stsp
68 01b7ba6b 2019-03-11 stsp if ((*lf)->fd == -1)
69 01b7ba6b 2019-03-11 stsp err = got_error(GOT_ERR_LOCKFILE_TIMEOUT);
70 01b7ba6b 2019-03-11 stsp done:
71 01b7ba6b 2019-03-11 stsp if (err) {
72 01b7ba6b 2019-03-11 stsp got_lockfile_unlock(*lf);
73 01b7ba6b 2019-03-11 stsp *lf = NULL;
74 01b7ba6b 2019-03-11 stsp }
75 01b7ba6b 2019-03-11 stsp return err;
76 01b7ba6b 2019-03-11 stsp }
77 01b7ba6b 2019-03-11 stsp
78 01b7ba6b 2019-03-11 stsp const struct got_error *
79 01b7ba6b 2019-03-11 stsp got_lockfile_unlock(struct got_lockfile *lf)
80 01b7ba6b 2019-03-11 stsp {
81 01b7ba6b 2019-03-11 stsp const struct got_error *err = NULL;
82 01b7ba6b 2019-03-11 stsp
83 01b7ba6b 2019-03-11 stsp if (lf->path && lf->fd != -1 && unlink(lf->path) != 0)
84 638f9024 2019-05-13 stsp err = got_error_from_errno("unlink");
85 01b7ba6b 2019-03-11 stsp if (lf->fd != -1 && close(lf->fd) != 0 && err == NULL)
86 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
87 01b7ba6b 2019-03-11 stsp free(lf->path);
88 01b7ba6b 2019-03-11 stsp free(lf->locked_path);
89 01b7ba6b 2019-03-11 stsp free(lf);
90 01b7ba6b 2019-03-11 stsp return err;
91 01b7ba6b 2019-03-11 stsp }