Module: Mountfd::Native

Defined in:
ext/mountfd/mountfd.c

Defined Under Namespace

Classes: Handle

Class Method Summary collapse

Class Method Details

.fsconfig(handle, command, key, value, aux) ⇒ Object



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
# File 'ext/mountfd/mountfd.c', line 184

static VALUE native_fsconfig(VALUE self, VALUE handle, VALUE command, VALUE key,
                             VALUE value, VALUE aux)
{
#ifdef __linux__
    int fd = fd_from(handle);
    unsigned int cmd = NUM2UINT(command);
    int raw_aux = NUM2INT(aux);
    const char *key_ptr;
    const void *value_ptr;
    if (!NIL_P(key)) StringValueCStr(key);
    if (!NIL_P(value)) {
        if (cmd == FSCONFIG_SET_BINARY) StringValue(value);
        else StringValueCStr(value);
    }
    if (cmd == FSCONFIG_SET_BINARY &&
        (raw_aux < 0 || (NIL_P(value) ? raw_aux != 0 : raw_aux > RSTRING_LEN(value))))
        rb_raise(rb_eArgError, "binary fsconfig length must be between zero and the value size");
    key_ptr = NIL_P(key) ? NULL : RSTRING_PTR(key);
    if (NIL_P(value)) value_ptr = NULL;
    else value_ptr = RSTRING_PTR(value);
    if (syscall(SYS_fsconfig, fd, cmd, key_ptr, value_ptr, raw_aux) < 0) {
        int error = errno;
        RB_GC_GUARD(key);
        RB_GC_GUARD(value);
        errno = error;
        mountfd_syscall_failed("fsconfig");
    }
    RB_GC_GUARD(key);
    RB_GC_GUARD(value);
    return Qnil;
#else
    unavailable(); return Qnil;
#endif
}

.fsmount(handle, flags, attrs) ⇒ Object



219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'ext/mountfd/mountfd.c', line 219

static VALUE native_fsmount(VALUE self, VALUE handle, VALUE flags, VALUE attrs)
{
#ifdef __linux__
    int context_fd = fd_from(handle);
    unsigned int raw_flags = NUM2UINT(flags);
    unsigned int raw_attrs = NUM2UINT(attrs);
    int fd = (int)syscall(SYS_fsmount, context_fd, raw_flags, raw_attrs);
    if (fd < 0) mountfd_syscall_failed("fsmount");
    return mountfd_wrap_fd(fd);
#else
    unavailable(); return Qnil;
#endif
}

.fsopen(fsname, flags) ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'ext/mountfd/mountfd.c', line 169

static VALUE native_fsopen(VALUE self, VALUE fsname, VALUE flags)
{
#ifdef __linux__
    unsigned int raw_flags = NUM2UINT(flags);
    const char *name = StringValueCStr(fsname);
    int fd = (int)syscall(SYS_fsopen, name, raw_flags);
    RB_GC_GUARD(fsname);
    if (fd < 0) mountfd_syscall_failed("fsopen");
    set_nonblocking_or_close(fd);
    return mountfd_wrap_fd(fd);
#else
    unavailable(); return Qnil;
#endif
}

.fspick(dfd, path, flags) ⇒ Object



233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'ext/mountfd/mountfd.c', line 233

static VALUE native_fspick(VALUE self, VALUE dfd, VALUE path, VALUE flags)
{
#ifdef __linux__
    int directory_fd = NUM2INT(dfd);
    unsigned int raw_flags = NUM2UINT(flags);
    const char *raw_path = StringValueCStr(path);
    int fd = (int)syscall(SYS_fspick, directory_fd, raw_path, raw_flags);
    RB_GC_GUARD(path);
    if (fd < 0) mountfd_syscall_failed("fspick");
    set_nonblocking_or_close(fd);
    return mountfd_wrap_fd(fd);
#else
    unavailable(); return Qnil;
#endif
}

.linux?Boolean

Returns:

  • (Boolean)


137
138
139
140
141
142
143
144
# File 'ext/mountfd/mountfd.c', line 137

static VALUE native_linux_p(VALUE self)
{
#ifdef __linux__
    return Qtrue;
#else
    return Qfalse;
#endif
}

.mount_setattr(dfd, path, flags, attr_set, attr_clr, propagation, userns_fd) ⇒ Object



291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# File 'ext/mountfd/mountfd.c', line 291

static VALUE native_mount_setattr(VALUE self, VALUE dfd, VALUE path, VALUE flags,
                                  VALUE attr_set, VALUE attr_clr, VALUE propagation,
                                  VALUE userns_fd)
{
#ifdef __linux__
    int directory_fd = fd_from(dfd);
    unsigned int raw_flags = NUM2UINT(flags);
    uint64_t raw_set = NUM2ULL(attr_set);
    uint64_t raw_clr = NUM2ULL(attr_clr);
    uint64_t raw_propagation = NUM2ULL(propagation);
    int has_idmap = (raw_set & MOUNT_ATTR_IDMAP) != 0;
    if ((raw_clr & MOUNT_ATTR_IDMAP) != 0)
        rb_raise(rb_eArgError, "an idmapped mount cannot be cleared");
    if ((has_idmap && NIL_P(userns_fd)) || (!has_idmap && !NIL_P(userns_fd)))
        rb_raise(rb_eArgError, "MOUNT_ATTR_IDMAP and a user namespace fd must be provided together");
    struct mount_attr attr = {
        raw_set, raw_clr, raw_propagation,
        NIL_P(userns_fd) ? 0 : (uint64_t)fd_from(userns_fd)
    };
    const char *raw_path = StringValueCStr(path);
    if (syscall(SYS_mount_setattr, directory_fd, raw_path, raw_flags,
                &attr, MOUNT_ATTR_SIZE_VER0) < 0) {
        int error = errno;
        RB_GC_GUARD(path);
        errno = error;
        mountfd_syscall_failed("mount_setattr");
    }
    RB_GC_GUARD(path);
    return Qnil;
#else
    unavailable(); return Qnil;
#endif
}

.move_mount(from_dfd, from_path, to_dfd, to_path, flags) ⇒ Object



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
# File 'ext/mountfd/mountfd.c', line 264

static VALUE native_move_mount(VALUE self, VALUE from_dfd, VALUE from_path,
                               VALUE to_dfd, VALUE to_path, VALUE flags)
{
#ifdef __linux__
    int source_fd = fd_from(from_dfd);
    int target_fd = NUM2INT(to_dfd);
    unsigned int raw_flags = NUM2UINT(flags);
    const char *source_path, *target_path;
    StringValueCStr(from_path);
    StringValueCStr(to_path);
    source_path = RSTRING_PTR(from_path);
    target_path = RSTRING_PTR(to_path);
    if (syscall(SYS_move_mount, source_fd, source_path, target_fd, target_path, raw_flags) < 0) {
        int error = errno;
        RB_GC_GUARD(from_path);
        RB_GC_GUARD(to_path);
        errno = error;
        mountfd_syscall_failed("move_mount");
    }
    RB_GC_GUARD(from_path);
    RB_GC_GUARD(to_path);
    return Qnil;
#else
    unavailable(); return Qnil;
#endif
}

.open_tree(dfd, path, flags) ⇒ Object



249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'ext/mountfd/mountfd.c', line 249

static VALUE native_open_tree(VALUE self, VALUE dfd, VALUE path, VALUE flags)
{
#ifdef __linux__
    int directory_fd = NUM2INT(dfd);
    unsigned int raw_flags = NUM2UINT(flags);
    const char *raw_path = StringValueCStr(path);
    int fd = (int)syscall(SYS_open_tree, directory_fd, raw_path, raw_flags);
    RB_GC_GUARD(path);
    if (fd < 0) mountfd_syscall_failed("open_tree");
    return mountfd_wrap_fd(fd);
#else
    unavailable(); return Qnil;
#endif
}

.read_diagnostics(handle) ⇒ Object



343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
# File 'ext/mountfd/mountfd.c', line 343

static VALUE native_read_diagnostics(VALUE self, VALUE handle)
{
#ifdef __linux__
    char buffer[4096];
    int fd = fd_from(handle);
    VALUE output = rb_str_new(NULL, 0);
    ssize_t length;
    for (;;) {
        length = read(fd, buffer, sizeof(buffer));
        if (length > 0) rb_str_cat(output, buffer, length);
        else if (length < 0 && errno == EINTR) continue;
        else break;
    }
    if (length < 0 && errno != EAGAIN && errno != EWOULDBLOCK && errno != ENODATA)
        rb_sys_fail("read(fs_context)");
    return output;
#else
    unavailable(); return Qnil;
#endif
}

.syscall_available?(name) ⇒ Boolean

Returns:

  • (Boolean)


146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'ext/mountfd/mountfd.c', line 146

static VALUE native_syscall_available(VALUE self, VALUE name)
{
#ifdef __linux__
    const char *value = StringValueCStr(name);
    long result;
    if (strcmp(value, "fsopen") == 0) {
        result = syscall(SYS_fsopen, "__mountfd_probe__", FSOPEN_CLOEXEC);
        if (result >= 0) mountfd_close((int)result);
    } else if (strcmp(value, "mount_setattr") == 0) {
        result = syscall(SYS_mount_setattr, -1, "", 0, NULL, 0);
    } else if (strcmp(value, "statmount") == 0) {
        result = syscall(SYS_statmount, NULL, NULL, 0, 0);
    } else if (strcmp(value, "listmount") == 0) {
        result = syscall(SYS_listmount, NULL, NULL, 0, 0);
    } else {
        rb_raise(rb_eArgError, "unknown syscall: %s", value);
    }
    return result >= 0 || errno != ENOSYS ? Qtrue : Qfalse;
#else
    return Qfalse;
#endif
}

.umount2(path, flags) ⇒ Object



325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# File 'ext/mountfd/mountfd.c', line 325

static VALUE native_umount2(VALUE self, VALUE path, VALUE flags)
{
#ifdef __linux__
    int raw_flags = NUM2INT(flags);
    const char *raw_path = StringValueCStr(path);
    if (umount2(raw_path, raw_flags) < 0) {
        int error = errno;
        RB_GC_GUARD(path);
        errno = error;
        mountfd_syscall_failed("umount2");
    }
    RB_GC_GUARD(path);
    return Qnil;
#else
    unavailable(); return Qnil;
#endif
}