summaryrefslogtreecommitdiff
path: root/daemons/dmeventd/plugins/lvm2/dmeventd_lvm.c
blob: cddf6cee8034cfa591de88d6a0a61def312dbe53 (plain)
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
 * Copyright (C) 2010-2015 Red Hat, Inc. All rights reserved.
 *
 * This file is part of LVM2.
 *
 * This copyrighted material is made available to anyone wishing to use,
 * modify, copy, or redistribute it subject to the terms and conditions
 * of the GNU Lesser General Public License v.2.1.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include "lib/misc/lib.h"
#include "dmeventd_lvm.h"
#include "daemons/dmeventd/libdevmapper-event.h"
#include "tools/lvm2cmd.h"

#include <pthread.h>

/*
 * register_device() is called first and performs initialisation.
 * Only one device may be registered or unregistered at a time.
 */
static pthread_mutex_t _register_mutex = PTHREAD_MUTEX_INITIALIZER;

/*
 * Number of active registrations.
 */
static int _register_count = 0;
static struct dm_pool *_mem_pool = NULL;
static void *_lvm_handle = NULL;
static DM_LIST_INIT(_env_registry);

struct env_data {
	struct dm_list list;
	const char *cmd;
	const char *data;
};

DM_EVENT_LOG_FN("#lvm")

static void _lvm2_print_log(int level, const char *file, int line,
			    int dm_errno_or_class, const char *msg)
{
	print_log(level, file, line, dm_errno_or_class, "%s", msg);
}

/*
 * Currently only one event can be processed at a time.
 */
static pthread_mutex_t _event_mutex = PTHREAD_MUTEX_INITIALIZER;

void dmeventd_lvm2_lock(void)
{
	pthread_mutex_lock(&_event_mutex);
}

void dmeventd_lvm2_unlock(void)
{
	pthread_mutex_unlock(&_event_mutex);
}

int dmeventd_lvm2_init(void)
{
	int r = 0;

	pthread_mutex_lock(&_register_mutex);

	if (!_lvm_handle) {
		lvm2_log_fn(_lvm2_print_log);

		if (!(_lvm_handle = lvm2_init_threaded()))
			goto out;

		/*
		 * Need some space for allocations.  1024 should be more
		 * than enough for what we need (device mapper name splitting)
		 */
		if (!_mem_pool && !(_mem_pool = dm_pool_create("mirror_dso", 1024))) {
			lvm2_exit(_lvm_handle);
			_lvm_handle = NULL;
			goto out;
		}

		lvm2_disable_dmeventd_monitoring(_lvm_handle);
		/* FIXME Temporary: move to dmeventd core */
		lvm2_run(_lvm_handle, "_memlock_inc");
		log_debug("lvm plugin initilized.");
	}

	_register_count++;
	r = 1;

out:
	pthread_mutex_unlock(&_register_mutex);
	return r;
}

void dmeventd_lvm2_exit(void)
{
	pthread_mutex_lock(&_register_mutex);

	if (!--_register_count) {
		log_debug("lvm plugin shuting down.");
		lvm2_run(_lvm_handle, "_memlock_dec");
		dm_pool_destroy(_mem_pool);
		_mem_pool = NULL;
		dm_list_init(&_env_registry);
		lvm2_exit(_lvm_handle);
		_lvm_handle = NULL;
		log_debug("lvm plugin exited.");
	}

	pthread_mutex_unlock(&_register_mutex);
}

struct dm_pool *dmeventd_lvm2_pool(void)
{
	return _mem_pool;
}

int dmeventd_lvm2_run(const char *cmdline)
{
	/* coverity[missing_lock] no locking for run part */
	return (lvm2_run(_lvm_handle, cmdline) == LVM2_COMMAND_SUCCEEDED);
}

int dmeventd_lvm2_command(struct dm_pool *mem, char *buffer, size_t size,
			  const char *cmd, const char *device)
{
	static char _internal_prefix[] =  "_dmeventd_";
	char *vg = NULL, *lv = NULL, *layer;
	int r;
	struct env_data *env_data;
	const char *env = NULL;

	if (!dm_split_lvm_name(mem, device, &vg, &lv, &layer)) {
		log_error("Unable to determine VG name from %s.",
			  device);
		return 0;
	}

	/* strip off the mirror component designations */
	if ((layer = strstr(lv, "_mimagetmp")) ||
	    (layer = strstr(lv, "_mlog")))
		*layer = '\0';

	if (!strncmp(cmd, _internal_prefix, sizeof(_internal_prefix) - 1)) {
		/* check if ENVVAR wasn't already resolved */
		dm_list_iterate_items(env_data, &_env_registry)
			if (!strcmp(cmd, env_data->cmd)) {
				env = env_data->data;
				break;
			}

		if (!env) {
			/* run lvm2 command to find out setting value */
			dmeventd_lvm2_lock();
			if (!dmeventd_lvm2_run(cmd) ||
			    !(env = getenv(cmd))) {
				dmeventd_lvm2_unlock();
				log_error("Unable to find configured command.");
				return 0;
			}
			/* output of internal command passed via env var */
			env = dm_pool_strdup(_mem_pool, env); /* copy with lock */
			dmeventd_lvm2_unlock();
			if (!env ||
			    !(env_data = dm_pool_zalloc(_mem_pool, sizeof(*env_data))) ||
			    !(env_data->cmd = dm_pool_strdup(_mem_pool, cmd))) {
				log_error("Unable to allocate env memory.");
				return 0;
			}
			env_data->data = env;
			/* add to ENVVAR registry */
			dm_list_add(&_env_registry, &env_data->list);
		}
		cmd = env;
	}

	r = dm_snprintf(buffer, size, "%s %s/%s", cmd, vg, lv);

	dm_pool_free(mem, vg);

	if (r < 0) {
		log_error("Unable to form LVM command. (too long).");
		return 0;
	}

	return 1;
}