• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * libwebsockets - small server side websockets and web server implementation
3  *
4  * Copyright (C) 2010 - 2020 Andy Green <andy@warmcat.com>
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to
8  * deal in the Software without restriction, including without limitation the
9  * rights to use, copy, modify, merge, publish, distribute, sublicefsme, and/or
10  * sell copies of the Software, and to permit persofsm to whom the Software is
11  * furnished to do so, subject to the following conditiofsm:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portiofsm of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22  * IN THE SOFTWARE.
23  *
24  * Mount and unmount overlayfs mountpoints (linux only)
25  */
26 
27 #include "private-lib-core.h"
28 #include <unistd.h>
29 
30 #include <libmount/libmount.h>
31 
32 #include <string.h>
33 #include <signal.h>
34 
35 #include <sys/stat.h>
36 #include <sys/types.h>
37 #include <unistd.h>
38 #include <fcntl.h>
39 
40 static int
rm_rf_cb(const char * dirpath,void * user,struct lws_dir_entry * lde)41 rm_rf_cb(const char *dirpath, void *user, struct lws_dir_entry *lde)
42 {
43 	char path[384];
44 
45 	if (!strcmp(lde->name, ".") || !strcmp(lde->name, ".."))
46 		return 0;
47 
48 	lws_snprintf(path, sizeof(path), "%s/%s", dirpath, lde->name);
49 
50 	if (lde->type == LDOT_DIR) {
51 		lws_dir(path, NULL, rm_rf_cb);
52 		rmdir(path);
53 	} else
54 		unlink(path);
55 
56 	return 0;
57 }
58 
59 int
lws_fsmount_mount(struct lws_fsmount * fsm)60 lws_fsmount_mount(struct lws_fsmount *fsm)
61 {
62 	struct libmnt_context *ctx;
63 	char opts[512], c;
64 	int n, m;
65 
66 	/*
67 	 * For robustness, there are a couple of sticky situations caused by
68 	 * previous mounts not cleaning up... 1) still mounted on the mountpoint
69 	 * and 2) junk in the session dir from the dead session.
70 	 *
71 	 * For 1), do a gratuitous umount attempts until it feels nothing to
72 	 * umount...
73 	 */
74 
75 	c = fsm->mp[0];
76 	while (!lws_fsmount_unmount(fsm))
77 		fsm->mp[0] = c;
78 	fsm->mp[0] = c;
79 
80 	/*
81 	 * ... for 2), generate the session dir basepath and destroy everything
82 	 * in it... it's less dangerous than it sounds because there are
83 	 * hardcoded unusual dir names in the base path, so it can't go wild
84 	 * even if the overlay path is empty or /
85 	 */
86 
87 	lws_snprintf(opts, sizeof(opts), "%s/overlays/%s/session",
88 		     fsm->overlay_path, fsm->ovname);
89 	lwsl_info("%s: emptying session dir %s\n", __func__, opts);
90 	lws_dir(opts, NULL, rm_rf_cb);
91 
92 	/*
93 	 * Piece together the options for the overlay mount...
94 	 */
95 
96 	n = lws_snprintf(opts, sizeof(opts), "lowerdir=");
97 	for (m = LWS_ARRAY_SIZE(fsm->layers) - 1; m >= 0; m--)
98 		if (fsm->layers[m]) {
99 			if (n != 9)
100 				opts[n++] = ':';
101 
102 			n += lws_snprintf(&opts[n], sizeof(opts) - n,
103 					  "%s/%s/%s", fsm->layers_path,
104 					  fsm->distro, fsm->layers[m]);
105 		}
106 
107 	n += lws_snprintf(&opts[n], sizeof(opts) - n,
108 			  ",upperdir=%s/overlays/%s/session",
109 			  fsm->overlay_path, fsm->ovname);
110 
111 	n += lws_snprintf(&opts[n], sizeof(opts) - n,
112 			  ",workdir=%s/overlays/%s/work",
113 			  fsm->overlay_path, fsm->ovname);
114 
115 	ctx = mnt_new_context();
116 	if (!ctx)
117 		return 1;
118 
119 	mnt_context_set_fstype(ctx, "overlay");
120 	mnt_context_set_options(ctx, opts);
121 	mnt_context_set_mflags(ctx, MS_NOATIME /* |MS_NOEXEC */);
122 	mnt_context_set_target(ctx, fsm->mp);
123 	mnt_context_set_source(ctx, "none");
124 
125 	lwsl_notice("%s: mount opts %s\n", __func__, opts);
126 	puts(opts);
127 
128 	m = mnt_context_mount(ctx);
129 	lwsl_notice("%s: mountpoint %s: %d\n", __func__, fsm->mp, m);
130 
131 	mnt_free_context(ctx);
132 
133 	return m;
134 }
135 
136 int
lws_fsmount_unmount(struct lws_fsmount * fsm)137 lws_fsmount_unmount(struct lws_fsmount *fsm)
138 {
139 	struct libmnt_context *ctx;
140 	int m;
141 
142 	lwsl_notice("%s: %s\n", __func__, fsm->mp);
143 
144 	ctx = mnt_new_context();
145 	if (!ctx)
146 		return 1;
147 
148 	mnt_context_set_target(ctx, fsm->mp);
149 
150 	m = mnt_context_umount(ctx);
151 	mnt_free_context(ctx);
152 
153 	fsm->mp[0] = '\0';
154 
155 	return m;
156 }
157