make got-read-gotconfig clear its imsgbuf before exit in an error case
[got-portable.git] / lib / lockfile.c
blob81255a07324d316bb3e4e75c1cfe57ebafad4711
1 /*
2 * Copyright (c) 2019 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include "got_compat.h"
19 #include <sys/stat.h>
20 #include <sys/queue.h>
21 #include <sys/tree.h>
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <string.h>
28 #include <stdio.h>
29 #include <time.h>
31 #include "got_error.h"
32 #include "got_path.h"
34 #include "got_lib_lockfile.h"
36 const struct got_error *
37 got_lockfile_lock(struct got_lockfile **lf, const char *path, int dir_fd)
39 const struct got_error *err = NULL;
40 int attempts = 5;
42 *lf = calloc(1, sizeof(**lf));
43 if (*lf == NULL)
44 return got_error_from_errno("calloc");
45 (*lf)->fd = -1;
47 (*lf)->locked_path = strdup(path);
48 if ((*lf)->locked_path == NULL) {
49 err = got_error_from_errno("strdup");
50 goto done;
53 if (asprintf(&(*lf)->path, "%s%s", path, GOT_LOCKFILE_SUFFIX) == -1) {
54 err = got_error_from_errno("asprintf");
55 goto done;
58 do {
59 if (dir_fd != -1) {
60 (*lf)->fd = openat(dir_fd, (*lf)->path,
61 O_RDWR | O_CREAT | O_EXCL | O_EXLOCK | O_CLOEXEC,
62 GOT_DEFAULT_FILE_MODE);
63 } else {
64 (*lf)->fd = open((*lf)->path,
65 O_RDWR | O_CREAT | O_EXCL | O_EXLOCK | O_CLOEXEC,
66 GOT_DEFAULT_FILE_MODE);
68 if ((*lf)->fd != -1)
69 break;
70 if (errno != EEXIST) {
71 err = got_error_from_errno2("open", (*lf)->path);
72 goto done;
74 sleep(1);
75 } while (--attempts > 0);
77 if ((*lf)->fd == -1) {
78 err = got_error_fmt(GOT_ERR_LOCKFILE_TIMEOUT,
79 "%s", (*lf)->path);
81 done:
82 if (err) {
83 got_lockfile_unlock(*lf, dir_fd);
84 *lf = NULL;
86 return err;
89 const struct got_error *
90 got_lockfile_unlock(struct got_lockfile *lf, int dir_fd)
92 const struct got_error *err = NULL;
94 if (dir_fd != -1) {
95 if (lf->path && lf->fd != -1 &&
96 unlinkat(dir_fd, lf->path, 0) != 0)
97 err = got_error_from_errno("unlinkat");
98 } else if (lf->path && lf->fd != -1 && unlink(lf->path) != 0)
99 err = got_error_from_errno("unlink");
100 if (lf->fd != -1 && close(lf->fd) == -1 && err == NULL)
101 err = got_error_from_errno("close");
102 free(lf->path);
103 free(lf->locked_path);
104 free(lf);
105 return err;