File: locking.c

package info (click to toggle)
dmraid 1.0.0.rc16-4.2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 3,544 kB
  • sloc: ansic: 16,884; sh: 3,002; makefile: 282
file content (119 lines) | stat: -rw-r--r-- 2,187 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
 * Copyright (C) 2004,2005  Heinz Mauelshagen, Red Hat GmbH.
 *                          All rights reserved.
 *
 * See file LICENSE at the top of this source tree for license information.
 */

#ifndef __KLIBC__
# include <sys/file.h>
#endif

#include "internal.h"

/* File locking private data. */
static const char *lock_file = "/var/lock/dmraid/.lock";
static int lf = -1;

/* flock file. */
static int
lock(struct lib_context *lc, struct resource *res)
{
	/* Already locked. */
	if (lf > -1)
		return 1;

	log_warn(lc, "locking %s", lock_file);
	if ((lf = open(lock_file, O_CREAT | O_APPEND | O_RDWR, 0777)) < 0)
		LOG_ERR(lc, 0, "opening lockfile %s", lock_file);

	if (flock(lf, LOCK_EX)) {
		close(lf);
		lf = -1;
		LOG_ERR(lc, 0, "flock lockfile %s", lock_file);
	}

	return 1;
}

/* Unlock file. */
static void
unlock(struct lib_context *lc, struct resource *res)
{
	/* Not locked! */
	if (lf == -1)
		return;

	log_warn(lc, "unlocking %s", lock_file);
	unlink(lock_file);
	if (flock(lf, LOCK_NB | LOCK_UN))
		log_err(lc, "flock lockfile %s", lock_file);

	if (close(lf))
		log_err(lc, "close lockfile %s", lock_file);

	lf = -1;
}

/* File base locking interface. */
static struct locking file_locking = {
	.name = "file",
	.lock = lock,
	.unlock = unlock,
};

static int
init_file_locking(struct lib_context *lc)
{
	int ret = 0;
	char *dir;

	if (!(dir = get_dirname(lc, (char *) lock_file)))
		return 0;

	if (!mk_dir(lc, dir))
		goto out;

	/* Fail on read-only file system. */
	if (access(dir, R_OK | W_OK) && errno == EROFS)
		goto out;

	lc->lock = &file_locking;
	ret = 1;

out:
	dbg_free(dir);

	return ret;
}

/*
 * External locking interface.
 */

/* Initialize locking. */
int
init_locking(struct lib_context *lc)
{
	if (OPT_IGNORELOCKING(lc))
		return 1;

	if (lc->locking_name)
		BUG(lc, 0, "no locking selection yet");

	return init_file_locking(lc);
}

/* Hide locking. */
int
lock_resource(struct lib_context *lc, struct resource *res)
{
	return OPT_IGNORELOCKING(lc) ? 1 : lc->lock->lock(lc, res);
}

/* Hide unlocking. */
void
unlock_resource(struct lib_context *lc, struct resource *res)
{
	return OPT_IGNORELOCKING(lc) ? 1 : lc->lock->unlock(lc, res);
}