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 01b7ba6b 2019-03-11 stsp got_lockfile_lock(struct got_lockfile **lf, const char *path)
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 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)
61 c789dbac 2019-03-11 stsp if (errno != EEXIST) {
62 638f9024 2019-05-13 stsp err = got_error_from_errno2("open", (*lf)->path);
66 01b7ba6b 2019-03-11 stsp } while (--attempts > 0);
68 01b7ba6b 2019-03-11 stsp if ((*lf)->fd == -1)
69 01b7ba6b 2019-03-11 stsp err = got_error(GOT_ERR_LOCKFILE_TIMEOUT);
72 01b7ba6b 2019-03-11 stsp got_lockfile_unlock(*lf);
78 01b7ba6b 2019-03-11 stsp const struct got_error *
79 01b7ba6b 2019-03-11 stsp got_lockfile_unlock(struct got_lockfile *lf)
81 01b7ba6b 2019-03-11 stsp const struct got_error *err = NULL;
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);