Method: Pledge#pledge

Defined in:
ext/pledge/pledge.c

#pledge(*args) ⇒ Object



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

static VALUE rb_pledge(int argc, VALUE* argv, VALUE pledge_class) {
  VALUE promises = Qnil;
  VALUE execpromises = Qnil;
  const char * prom = NULL;
  const char * execprom = NULL;

#ifdef HAVE_PLEDGE
  rb_scan_args(argc, argv, "11", &promises, &execpromises);

  if (!NIL_P(promises)) {
    SafeStringValue(promises);
    promises = rb_str_dup(promises);

    /* required for ruby to work */
    rb_str_cat2(promises, " stdio");
    promises = rb_funcall(promises, rb_intern("strip"), 0);
    SafeStringValue(promises);
    prom = RSTRING_PTR(promises);
  }

  if (!NIL_P(execpromises)) {
    SafeStringValue(execpromises);
    execprom = RSTRING_PTR(execpromises);
  }

  if (pledge(prom, execprom) != 0) {
    switch(errno) {
    case EINVAL:
        rb_raise(ePledgeInvalidPromise, "invalid promise in promises string");
    case EPERM:
        rb_raise(ePledgePermissionIncreaseAttempt, "attempt to increase permissions");
    default:
        rb_raise(ePledgeError, "pledge error");
    }
  }
#else
  rb_raise(rb_eNotImpError, "pledge not supported");
#endif

  return Qnil;
}