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
|
/*
* Copyright (C) NGINX, Inc.
*/
#include <nxt_main.h>
#if (NXT_HAVE_FREEBSD_NMOUNT)
#include <sys/param.h>
#include <sys/uio.h>
#endif
#if (NXT_HAVE_LINUX_MOUNT)
nxt_int_t
nxt_fs_mount(nxt_task_t *task, nxt_fs_mount_t *mnt)
{
int rc;
const char *fsname;
unsigned long flags;
flags = 0;
switch (mnt->type) {
case NXT_FS_BIND:
if (nxt_slow_path(mnt->flags != 0)) {
nxt_log(task, NXT_LOG_WARN,
"bind mount ignores additional flags");
}
fsname = "bind";
flags = MS_BIND | MS_REC;
break;
case NXT_FS_PROC:
fsname = "proc";
goto getflags;
case NXT_FS_TMP:
fsname = "tmpfs";
goto getflags;
default:
fsname = (const char *) mnt->name;
getflags:
if (mnt->flags & NXT_FS_FLAGS_NODEV) {
flags |= MS_NODEV;
}
if (mnt->flags & NXT_FS_FLAGS_NOEXEC) {
flags |= MS_NOEXEC;
}
if (mnt->flags & NXT_FS_FLAGS_NOSUID) {
flags |= MS_NOSUID;
}
if (!(mnt->flags & NXT_FS_FLAGS_NOTIME)) {
flags |= MS_RELATIME;
}
}
rc = mount((const char *) mnt->src, (const char *) mnt->dst, fsname, flags,
mnt->data);
if (nxt_slow_path(rc < 0)) {
nxt_alert(task, "mount(\"%s\", \"%s\", \"%s\", %ul, \"%s\") %E",
mnt->src, mnt->dst, fsname, flags, mnt->data, nxt_errno);
return NXT_ERROR;
}
return NXT_OK;
}
#elif (NXT_HAVE_FREEBSD_NMOUNT)
nxt_int_t
nxt_fs_mount(nxt_task_t *task, nxt_fs_mount_t *mnt)
{
int flags;
u_char *data, *p, *end;
size_t iovlen;
nxt_int_t ret;
const char *fsname;
struct iovec iov[128];
char errmsg[256];
if (nxt_slow_path((mnt->flags & NXT_FS_FLAGS_NODEV) && !mnt->builtin)) {
nxt_alert(task, "nmount(2) doesn't support \"nodev\" option");
return NXT_ERROR;
}
flags = 0;
switch (mnt->type) {
case NXT_FS_BIND:
fsname = "nullfs";
break;
case NXT_FS_PROC:
fsname = "procfs";
goto getflags;
case NXT_FS_TMP:
fsname = "tmpfs";
goto getflags;
default:
fsname = (const char *) mnt->name;
getflags:
if (mnt->flags & NXT_FS_FLAGS_NOEXEC) {
flags |= MNT_NOEXEC;
}
if (mnt->flags & NXT_FS_FLAGS_NOSUID) {
flags |= MNT_NOSUID;
}
if (mnt->flags & NXT_FS_FLAGS_NOTIME) {
flags |= MNT_NOATIME;
}
if (mnt->flags & NXT_FS_FLAGS_RDONLY) {
flags |= MNT_RDONLY;
}
}
iov[0].iov_base = (void *) "fstype";
iov[0].iov_len = 7;
iov[1].iov_base = (void *) fsname;
iov[1].iov_len = nxt_strlen(fsname) + 1;
iov[2].iov_base = (void *) "fspath";
iov[2].iov_len = 7;
iov[3].iov_base = (void *) mnt->dst;
iov[3].iov_len = nxt_strlen(mnt->dst) + 1;
iov[4].iov_base = (void *) "target";
iov[4].iov_len = 7;
iov[5].iov_base = (void *) mnt->src;
iov[5].iov_len = nxt_strlen(mnt->src) + 1;
iov[6].iov_base = (void *) "errmsg";
iov[6].iov_len = 7;
iov[7].iov_base = (void *) errmsg;
iov[7].iov_len = sizeof(errmsg);
iovlen = 8;
data = NULL;
if (mnt->data != NULL) {
data = (u_char *) nxt_strdup(mnt->data);
if (nxt_slow_path(data == NULL)) {
return NXT_ERROR;
}
end = data - 1;
do {
p = end + 1;
end = nxt_strchr(p, '=');
if (end == NULL) {
break;
}
*end = '\0';
iov[iovlen].iov_base = (void *) p;
iov[iovlen].iov_len = (end - p) + 1;
iovlen++;
p = end + 1;
end = nxt_strchr(p, ',');
if (end != NULL) {
*end = '\0';
}
iov[iovlen].iov_base = (void *) p;
iov[iovlen].iov_len = nxt_strlen(p) + 1;
iovlen++;
} while (end != NULL && nxt_nitems(iov) > (iovlen + 2));
}
ret = NXT_OK;
if (nxt_slow_path(nmount(iov, iovlen, flags) < 0)) {
nxt_alert(task, "nmount(%p, %d, 0) %s", iov, iovlen, errmsg);
ret = NXT_ERROR;
}
if (data != NULL) {
free(data);
}
return ret;
}
#endif
#if (NXT_HAVE_LINUX_UMOUNT2)
void
nxt_fs_unmount(const u_char *path)
{
if (nxt_slow_path(umount2((const char *) path, MNT_DETACH) < 0)) {
nxt_thread_log_error(NXT_LOG_WARN, "umount2(%s, MNT_DETACH) %E",
path, nxt_errno);
}
}
#elif (NXT_HAVE_UNMOUNT)
void
nxt_fs_unmount(const u_char *path)
{
if (nxt_slow_path(unmount((const char *) path, MNT_FORCE) < 0)) {
nxt_thread_log_error(NXT_LOG_WARN, "unmount(%s) %E", path, nxt_errno);
}
}
#endif
|