File: mount_bind.c

package info (click to toggle)
autofs 5.1.9-1.2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,724 kB
  • sloc: ansic: 36,118; yacc: 1,821; lex: 1,126; sh: 627; makefile: 541
file content (265 lines) | stat: -rw-r--r-- 6,442 bytes parent folder | download
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
/* ----------------------------------------------------------------------- *
 *   
 *  mount_bind.c      - module to mount a local filesystem if possible;
 *			otherwise create a symlink.
 *
 *   Copyright 2000 Transmeta Corporation - All Rights Reserved
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, Inc., 675 Mass Ave, Cambridge MA 02139,
 *   USA; either version 2 of the License, or (at your option) any later
 *   version; incorporated herein by reference.
 *
 * ----------------------------------------------------------------------- */

#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include <stdlib.h>
#include <sys/param.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mount.h>

#define MODULE_MOUNT
#include "automount.h"

#define MODPREFIX "mount(bind): "

int mount_version = AUTOFS_MOUNT_VERSION;	/* Required by protocol */

static int bind_works = 0;

int mount_init(void **context)
{
	char tmp1[] = "/tmp/autoXXXXXX", *t1_dir;
	char tmp2[] = "/tmp/autoXXXXXX", *t2_dir;
	int err;
	struct stat st1, st2;

	t1_dir = mkdtemp(tmp1);
	t2_dir = mkdtemp(tmp2);
	if (t1_dir == NULL || t2_dir == NULL) {
		if (t1_dir)
			rmdir(t1_dir);
		if (t2_dir)
			rmdir(t2_dir);
		return 0;
	}

	if (lstat(t1_dir, &st1) == -1)
		goto out;

	err = spawn_mount(LOGOPT_NONE, "-n", "--bind", t1_dir, t2_dir, NULL);
	if (err == 0 &&
	    lstat(t2_dir, &st2) == 0 &&
	    st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino) {
		bind_works = 1;
	}

	if (spawn_umount(LOGOPT_NONE, "-n", t2_dir, NULL) != 0)
		debug(LOGOPT_ANY, MODPREFIX "umount failed for %s", t2_dir);

out:
	rmdir(t1_dir);
	rmdir(t2_dir);

	return 0;
}

int mount_reinit(void **context)
{
	return 0;
}

int mount_mount(struct autofs_point *ap, const char *root, const char *name, int name_len,
		const char *what, const char *fstype, const char *options, void *context)
{
	char fullpath[PATH_MAX];
	char buf[MAX_ERR_BUF];
	int err;
	int i, len;
	int symlnk = (*name != '/' && (ap->flags & MOUNT_FLAG_SYMLINK));
	void (*mountlog)(unsigned int, const char*, ...) = &log_debug;

	if (ap->flags & MOUNT_FLAG_REMOUNT)
		return 0;

	if (defaults_get_mount_verbose())
		mountlog = &log_info;

	/* Extract "symlink" pseudo-option which forces local filesystems
	 * to be symlinked instead of bound.
	 */
	if (*name != '/' && !symlnk && options) {
		const char *comma;
		int o_len = strlen(options) + 1;

		for (comma = options; *comma != '\0';) {
			const char *cp;
			const char *end;

			while (*comma == ',')
				comma++;

			/* Skip leading white space */
			while (*comma == ' ' || *comma == '\t')
				comma++;

			cp = comma;
			while (*comma != '\0' && *comma != ',')
				comma++;

			/* Skip trailing white space */
			end = comma - 1;
			while (*comma == ' ' || *comma == '\t')
				end--;

			o_len = end - cp + 1;
			if (_strncmp("symlink", cp, o_len) == 0)
				symlnk = 1;
		}
	}

	len = mount_fullpath(fullpath, PATH_MAX, root, 0, name);
	if (!len) {
		error(ap->logopt,
		      MODPREFIX "mount point path too long");
		return 1;
	}

	i = len;
	while (--i > 0 && fullpath[i] == '/')
		fullpath[i] = '\0';

	if (options == NULL || *options == '\0')
		options = "defaults";

	if (!strcmp(what, fullpath)) {
		debug(ap->logopt, MODPREFIX
		     "cannot mount or symlink %s to itself", fullpath);
		return 1;
	}

	if (!symlnk && bind_works) {
		int status, existed = 1;
		int flags;

		debug(ap->logopt, MODPREFIX "calling mkdir_path %s", fullpath);

		status = mkdir_path(fullpath, mp_mode);
		if (status && errno != EEXIST) {
			char *estr = strerror_r(errno, buf, MAX_ERR_BUF);
			error(ap->logopt,
			      MODPREFIX "mkdir_path %s failed: %s",
			      fullpath, estr);
			return 1;
		}

		if (!status)
			existed = 0;

		mountlog(ap->logopt, MODPREFIX
			 "calling mount --bind -o %s %s %s",
			  options, what, fullpath);

		err = spawn_bind_mount(ap->logopt, "-o",
				       options, what, fullpath, NULL);

		if (err) {
			if (ap->type != LKP_INDIRECT)
				return 1;

			if (!existed &&
			   (!(ap->flags & MOUNT_FLAG_GHOST) && name_len))
				rmdir_path(ap, fullpath, ap->dev);

			return err;
		} else {
			mountlog(ap->logopt,
			      MODPREFIX "mounted %s type %s on %s",
			      what, fstype, fullpath);
		}

		/* The bind mount has succeeded, now set the mount propagation.
		 *
		 * The default is propagation shared, change it if the master
		 * map entry has a different option specified.
		 */
		flags = MS_SLAVE;
		if (ap->flags & MOUNT_FLAG_PRIVATE)
			flags = MS_PRIVATE;
		else if (ap->flags & MOUNT_FLAG_SHARED)
			flags = MS_SHARED;

		/* Note: If the parent mount is propagation shared propagation
		 *  of child mounts (autofs offset mounts for example) back to
		 *  the target of the bind mount can happen in some cases and
		 *  must be avoided or autofs trigger mounts will deadlock.
		 */
		err = mount(NULL, fullpath, NULL, flags, NULL);
		if (err) {
			warn(ap->logopt,
			     "failed to set propagation for %s",
			     fullpath, root);
		}

		return 0;
	} else {
		char *cp;
		char basepath[PATH_MAX];
		int status;
		struct stat st;

		strcpy(basepath, fullpath);
		cp = strrchr(basepath, '/');

		if (cp != NULL && cp != basepath)
			*cp = '\0';

		if ((status = stat(fullpath, &st)) == 0) {
			if (S_ISDIR(st.st_mode))
				rmdir(fullpath);
		} else {
			debug(ap->logopt,
			      MODPREFIX "calling mkdir_path %s", basepath);
			if (mkdir_path(basepath, mp_mode) && errno != EEXIST) {
				char *estr;
				estr = strerror_r(errno, buf, MAX_ERR_BUF);
				error(ap->logopt,
				      MODPREFIX "mkdir_path %s failed: %s",
				      basepath, estr);
				return 1;
			}
		}

		if (symlink(what, fullpath) && errno != EEXIST) {
			error(ap->logopt,
			      MODPREFIX
			      "failed to create symlink %s -> %s",
			      fullpath, what);
			if ((ap->flags & MOUNT_FLAG_GHOST) && !status) {
				if (mkdir_path(fullpath, mp_mode) && errno != EEXIST) {
					char *estr = strerror_r(errno, buf, MAX_ERR_BUF);
					error(ap->logopt,
					      MODPREFIX "mkdir_path %s failed: %s",
					      fullpath, estr);
				}
			} else {
				if (ap->type == LKP_INDIRECT)
					rmdir_path(ap, fullpath, ap->dev);
			}
			return 1;
		} else {
			mountlog(ap->logopt,
			      MODPREFIX "symlinked %s -> %s", fullpath, what);
			return 0;
		}
	}
}

int mount_done(void *context)
{
	return 0;
}