summaryrefslogtreecommitdiff
path: root/daemons/lvmpolld/lvmpolld-cmd-utils.c
blob: 024822159c8862ac5d0b0767a6c954e542dd61ff (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
/*
 * Copyright (C) 2015 Red Hat, Inc.
 *
 * 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "lvmpolld-common.h"

/* extract this info from autoconf/automake files */
#define LVPOLL_CMD "lvpoll"

#define MIN_ARGV_SIZE  8

static const char *const const polling_ops[] = { [PVMOVE] = LVMPD_REQ_PVMOVE,
						 [CONVERT] = LVMPD_REQ_CONVERT,
						 [MERGE] = LVMPD_REQ_MERGE,
						 [MERGE_THIN] = LVMPD_REQ_MERGE_THIN };

const char *polling_op(enum poll_type type)
{
	return type < POLL_TYPE_MAX ? polling_ops[type] : "<undefined>";
}

static int add_to_cmd_arr(const char ***cmdargv, const char *str, unsigned *ind)
{
	const char **newargv = *cmdargv;

	if (*ind && !(*ind % MIN_ARGV_SIZE)) {
		newargv = dm_realloc(*cmdargv, (*ind / MIN_ARGV_SIZE + 1) * MIN_ARGV_SIZE * sizeof(char *));
		if (!newargv)
			return 0;
		*cmdargv = newargv;
	}

	*(*cmdargv + (*ind)++) = str;

	return 1;
}

const char **cmdargv_ctr(const struct lvmpolld_lv *pdlv, const char *lvm_binary, unsigned abort_polling, unsigned handle_missing_pvs)
{
	unsigned i = 0;
	const char **cmd_argv = dm_malloc(MIN_ARGV_SIZE * sizeof(char *));

	if (!cmd_argv)
		return NULL;

	/* path to lvm2 binary */
	if (!add_to_cmd_arr(&cmd_argv, lvm_binary, &i))
		goto err;

	/* cmd to execute */
	if (!add_to_cmd_arr(&cmd_argv, LVPOLL_CMD, &i))
		goto err;

	/* transfer internal polling interval */
	if (pdlv->sinterval &&
	    (!add_to_cmd_arr(&cmd_argv, "--interval", &i) ||
	     !add_to_cmd_arr(&cmd_argv, pdlv->sinterval, &i)))
		goto err;

	/* pass abort param */
	if (abort_polling &&
	    !add_to_cmd_arr(&cmd_argv, "--abort", &i))
		goto err;

	/* pass handle-missing-pvs. used by mirror polling operation */
	if (handle_missing_pvs &&
	    !add_to_cmd_arr(&cmd_argv, "--handlemissingpvs", &i))
		goto err;

	/* one of: "convert", "pvmove", "merge", "merge_thin" */
	if (!add_to_cmd_arr(&cmd_argv, "--polloperation", &i) ||
	    !add_to_cmd_arr(&cmd_argv, polling_ops[pdlv->type], &i))
		goto err;

	/* vg/lv name */
	if (!add_to_cmd_arr(&cmd_argv, pdlv->lvname, &i))
		goto err;

	/* disable metadata backup */
	if (!add_to_cmd_arr(&cmd_argv, "-An", &i))
		goto err;

	/* terminating NULL */
	if (!add_to_cmd_arr(&cmd_argv, NULL, &i))
		goto err;

	return cmd_argv;
err:
	dm_free(cmd_argv);
	return NULL;
}

/* FIXME: in fact exclude should be va list */
static int copy_env(const char ***cmd_envp, unsigned *i, const char *exclude)
{
	const char * const* tmp = (const char * const*) environ;

	if (!tmp)
		return 0;

	while (*tmp) {
		if (strncmp(*tmp, exclude, strlen(exclude)) && !add_to_cmd_arr(cmd_envp, *tmp, i))
			return 0;
		tmp++;
	}

	return 1;
}

const char **cmdenvp_ctr(const struct lvmpolld_lv *pdlv)
{
	unsigned i = 0;
	const char **cmd_envp = dm_malloc(MIN_ARGV_SIZE * sizeof(char *));

	if (!cmd_envp)
		return NULL;

	/* copy whole environment from lvmpolld, exclude LVM_SYSTEM_DIR if set */
	if (!copy_env(&cmd_envp, &i, "LVM_SYSTEM_DIR="))
		goto err;

	/* Add per client LVM_SYSTEM_DIR variable if set */
	if (*pdlv->lvm_system_dir_env && !add_to_cmd_arr(&cmd_envp, pdlv->lvm_system_dir_env, &i))
		goto err;

	/* terminating NULL */
	if (!add_to_cmd_arr(&cmd_envp, NULL, &i))
		goto err;

	return cmd_envp;
err:
	dm_free(cmd_envp);
	return NULL;
}