Class: Posix

Inherits:
Object
  • Object
show all
Defined in:
lib/posix/sigset.rb,
ext/posix/posix.c

Defined Under Namespace

Classes: Sigset

Constant Summary collapse

SIG_BLOCK =

Define constants for sigprocmask

INT2FIX(RB_POSIX_SIG_BLOCK)
SIG_UNBLOCK =
INT2FIX(RB_POSIX_SIG_UNBLOCK)
SIG_SETMASK =
INT2FIX(RB_POSIX_SIG_SETMASK)

Class Method Summary collapse

Class Method Details

.dup(_filedes) ⇒ Object

Method binding for dup



60
61
62
63
64
# File 'ext/posix/posix.c', line 60

VALUE posix_dup(VALUE self, VALUE _filedes) {
    int filedes = FIX2INT(_filedes);

    return INT2FIX(dup(filedes));
}

.dup2(_filedes, _filedes2) ⇒ Object



66
67
68
69
70
71
# File 'ext/posix/posix.c', line 66

VALUE posix_dup2(VALUE self, VALUE _filedes, VALUE _filedes2) {
    int filedes = FIX2INT(_filedes);
    int filedes2 = FIX2INT(_filedes2);

    return INT2FIX(dup2(filedes, filedes2));
}

.execve(_binary, _argv, _envp) ⇒ Object

Export an argv that does sane, reasonable things instead of doing weird shit.

Return value is largely irrelevant



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

VALUE posix_execve(VALUE self, VALUE _binary, VALUE _argv, VALUE _envp) {
    /* Iterators */
    int i;
    VALUE akey, keys, envk;

    /* Binary to load in the new process */
    char* binary = StringValueCStr(_binary);

    /* Construct our new process' argv. Onus is on
     * the programmer to set ARGV[0] to something
     * reasonable. */
    char** argv = malloc(sizeof(char*)*RARRAY_LEN(_argv) + 1);
    argv[0] = NULL;

    for (i = 0; i < RARRAY_LEN(_argv); i++) {
        argv[i] = StringValuePtr(RARRAY_PTR(_argv)[i]);
        argv[i+1] = NULL; /* Ensure that we're null terminated */
    }

    /* Construct our environment. Note that this totally ignores the precedent
     * set by Process#spawn, Kernel#exec and fiends */

    char **envp;
    if (RHASH(_envp)->ntbl) {
        envp = malloc(sizeof(char**)*RHASH(_envp)->ntbl->num_entries + 1);
        keys = rb_hash_keys(_envp);
        for (i = 0; i < RARRAY_LEN(keys); i++) {
            akey = RARRAY_PTR(keys)[i];
            envk = rb_hash_aref(_envp, akey);
            asprintf(&envp[i], "%s=%s", StringValuePtr(akey), StringValuePtr(envk));
            envp[i+1] = NULL; /* Ensure that we're null terminated */
    }
    } else {
        envp = malloc(sizeof(char**));
        envp[0] = NULL;
    }

    execve(binary, argv, envp);
    fprintf(stderr, "Error: %s", strerror(errno));
}

.sigprocmask(_how, _set) ⇒ Object

Doesn’t accept a third argument, instead returns the new set

See Also:

  • 2 sigprocmask


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

VALUE posix_sigprocmask(VALUE self, VALUE _how, VALUE _set) {
    int how;
    sigset_t *set;
    sigset_t *oset = NULL; // TODO, storage for returnvalue

    how = FIX2INT(_how);

    if (_set == Qnil) {
        set = NULL;
    } else {
        set = malloc(sizeof(sigset_t));
        rb_Sigset2sigset_t(_set, set);
    }

    if (sigprocmask(how, set, oset) == 0) {
        // TODO Construct a Sigset and return it so that we can interrogate it

        if(set)
            free(set);
        return Qtrue;
    } else {
        if(set)
            free(set);
        rb_raise(rb_eException, "%s", strerror(errno));
    }
}