summaryrefslogtreecommitdiffstats
path: root/libmount/src/hook_mount_legacy.c
blob: 7e62864673a46584607475536e69b160e71bcf79 (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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
 * This file is part of libmount from util-linux project.
 *
 * Copyright (C) 2022 Karel Zak <kzak@redhat.com>
 *
 * libmount is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 *
 * This is classic mount(2) based mount.
 *
 * Please, see the comment in libmount/src/hooks.c to understand how hooks work.
 */

#include "mountP.h"

/* mount(2) flags for additional propagation changes etc. */
struct hook_data {
	unsigned long flags;
};

static int hookset_deinit(struct libmnt_context *cxt, const struct libmnt_hookset *hs)
{
	void *data = NULL;

	DBG(HOOK, ul_debugobj(hs, "deinit '%s'", hs->name));

	/* remove all our hooks and free hook data */
	while (mnt_context_remove_hook(cxt, hs, 0, &data) == 0) {
		if (data)
			free(data);
		data = NULL;
	}

	return 0;
}

static struct hook_data *new_hook_data(void)
{
	return calloc(1, sizeof(struct hook_data));
}

/* call mount(2) for propagation flags */
static int hook_propagation(struct libmnt_context *cxt,
			    const struct libmnt_hookset *hs,
			    void *data)
{
	int rc;
	struct hook_data *hd = (struct hook_data *) data;
	unsigned long extra = 0;

	assert(hd);
	assert(cxt);
	assert(cxt->fs);
	assert(cxt->optlist);

	DBG(HOOK, ul_debugobj(hs, " calling mount(2) for propagation: 0x%08lx %s",
				hd->flags,
				hd->flags & MS_REC ? " (recursive)" : ""));

	/*
	 * hd->flags are propagation flags as set in prepare_propagation()
	 *
	 * @cxt contains global mount flags, may be modified after preparation
	 * stage (for example when libmount blindly tries FS type then it uses
	 * MS_SILENT)
	 */
	if (mnt_optlist_is_silent(cxt->optlist))
		extra |= MS_SILENT;

	rc = mount("none", mnt_fs_get_target(cxt->fs), NULL,
			hd->flags | extra, NULL);

	if (rc) {
		/* Update global syscall status if only this function called */
		if (mnt_context_propagation_only(cxt)) {
			cxt->syscall_status = -errno;
			cxt->syscall_name = "mount";
		}

		DBG(HOOK, ul_debugobj(hs, "  mount(2) failed [errno=%d %m]", errno));
		rc = -MNT_ERR_APPLYFLAGS;
	}
	return rc;
}

/*
 * add additional mount(2) syscalls to set propagation flags after regular mount(2).
 */
static int prepare_propagation(struct libmnt_context *cxt,
				const struct libmnt_hookset *hs)
{
	struct libmnt_optlist *ol;
	struct libmnt_iter itr;
	struct libmnt_opt *opt;

	assert(cxt);
	assert(cxt->fs);

	ol = mnt_context_get_optlist(cxt);
	if (!ol)
		return -ENOMEM;

	mnt_reset_iter(&itr, MNT_ITER_FORWARD);

	while (mnt_optlist_next_opt(ol, &itr, &opt) == 0) {
		int rc;
		struct hook_data *data;
		const struct libmnt_optmap *map = mnt_opt_get_map(opt);
		const struct libmnt_optmap *ent = mnt_opt_get_mapent(opt);

		if (!map || map != cxt->map_linux)
			continue;
		if (!(ent->id & MS_PROPAGATION))
			continue;

		data = new_hook_data();
		if (!data)
			return -ENOMEM;

		data->flags = ent->id;

		DBG(HOOK, ul_debugobj(hs, " adding mount(2) call for %s", ent->name));
		rc = mnt_context_append_hook(cxt, hs,
					MNT_STAGE_MOUNT_POST,
					data,
					hook_propagation);
		if (rc)
			return rc;

		DBG(HOOK, ul_debugobj(hs, " removing '%s' flag from primary mount(2)", ent->name));
		mnt_optlist_remove_opt(ol, opt);
	}

	return 0;
}

/* call mount(2) for bind,remount */
static int hook_bindremount(struct libmnt_context *cxt,
			    const struct libmnt_hookset *hs, void *data)
{
	int rc;
	struct hook_data *hd = (struct hook_data *) data;
	unsigned long extra = 0;

	DBG(HOOK, ul_debugobj(hs, " mount(2) for bind-remount: 0x%08lx %s",
				hd->flags,
				hd->flags & MS_REC ? " (recursive)" : ""));

	if (mnt_optlist_is_silent(cxt->optlist))
		extra |= MS_SILENT;

	/* for the flags see comment in hook_propagation() */
	rc = mount("none", mnt_fs_get_target(cxt->fs), NULL,
			hd->flags | extra, NULL);

	if (rc)
		DBG(HOOK, ul_debugobj(hs, "  mount(2) failed"
				  " [rc=%d errno=%d %m]", rc, errno));
	return rc;
}

/*
 * add additional mount(2) syscall request to implement "bind,<flags>", the first regular
 * mount(2) is the "bind" operation, the second is "remount,bind,<flags>" call.
 */
static int prepare_bindremount(struct libmnt_context *cxt,
				const struct libmnt_hookset *hs)
{
	struct hook_data *data;
	int rc;

	assert(cxt);

	DBG(HOOK, ul_debugobj(hs, " adding mount(2) call for bint-remount"));

	data = new_hook_data();
	if (!data)
		return -ENOMEM;

	mnt_context_get_mflags(cxt, &data->flags);

	assert(data->flags & MS_BIND);
	assert(!(data->flags & MS_REMOUNT));

	data->flags |= (MS_REMOUNT | MS_BIND);

	rc = mnt_context_append_hook(cxt, hs,
				MNT_STAGE_MOUNT_POST, data, hook_bindremount);
	return rc;
}




/* call mount(2) for regular FS mount, mount flags and options are read from
 * library context struct. There are no private hook data.
 */
static int hook_mount(struct libmnt_context *cxt,
		      const struct libmnt_hookset *hs,
		      void *data __attribute__((__unused__)))
{
	int rc = 0;
	unsigned long flags = 0;
	struct libmnt_optlist *ol = NULL;
	const char *src, *target, *type, *options = NULL;

	src = mnt_fs_get_srcpath(cxt->fs);
	target = mnt_fs_get_target(cxt->fs);
	type = mnt_fs_get_fstype(cxt->fs);

	ol = mnt_context_get_optlist(cxt);
	if (!ol)
		return -ENOMEM;
	if (!target)
		return -EINVAL;
	if (!src)
		src = "none";

	/* FS specific mount options/data */
	if (cxt->flags & MNT_FL_MOUNTDATA)
		options = cxt->mountdata;
	else
		rc = mnt_optlist_get_optstr(ol, &options,
				NULL, MNT_OL_FLTR_UNKNOWN);
	/* mount flags */
	if (!rc)
		rc = mnt_optlist_get_flags(ol, &flags,
				mnt_get_builtin_optmap(MNT_LINUX_MAP), 0);
	if (rc)
		return rc;

	DBG(HOOK, ul_debugobj(hs, "  mount(2) "
		"[source=%s, target=%s, type=%s, flags=0x%08lx, options=%s]",
		src, target, type, flags,
		options ? (cxt->flags & MNT_FL_MOUNTDATA) ? "binary" :
			  options : "<none>"));

	if (mount(src, target, type, flags, options)) {
		cxt->syscall_status = -errno;
		cxt->syscall_name = "mount";
		DBG(HOOK, ul_debugobj(hs, "  mount(2) failed [errno=%d %m]",
					-cxt->syscall_status));
		rc = -cxt->syscall_status;
		return rc;
	}

	cxt->syscall_status = 0;
	return rc;
}

/*
 * analyze library context and register hooks to call one or more mount(2) syscalls
 */
static int hook_prepare(struct libmnt_context *cxt,
			const struct libmnt_hookset *hs,
			void *data __attribute__((__unused__)))
{
	int rc = 0;
	unsigned long flags = 0;

	assert(cxt);
	assert(hs == &hookset_mount_legacy);

#ifdef USE_LIBMOUNT_MOUNTFD_SUPPORT
	/* do nothing when a new __mount succesfully registred */
	if (mnt_context_has_hook(cxt, &hookset_mount, 0, NULL))
		return 0;
#endif
	/* append regular FS mount(2) */
	if (!mnt_context_propagation_only(cxt) && !cxt->helper)
		rc = mnt_context_append_hook(cxt, hs,
				MNT_STAGE_MOUNT, NULL, hook_mount);

	if (!rc)
		rc = mnt_context_get_mflags(cxt, &flags);
	if (rc)
		return rc;

	/* add extra mount(2) calls for each propagation flag  */
	if (flags & MS_PROPAGATION) {
		rc = prepare_propagation(cxt, hs);
		if (rc)
			return rc;
	}

	/* add extra mount(2) call to implement "bind,remount,<flags>" */
	if ((flags & MS_BIND)
	    && (flags & MNT_BIND_SETTABLE)
	    && !(flags & MS_REMOUNT)) {
		rc = prepare_bindremount(cxt, hs);
		if (rc)
			return rc;
	}

	return rc;
}

const struct libmnt_hookset hookset_mount_legacy =
{
	.name = "__legacy-mount",

	.firststage = MNT_STAGE_PREP,
	.firstcall = hook_prepare,

	.deinit = hookset_deinit
};