Module: Kernel

Defined in:
(unknown)

Instance Method Summary collapse

Instance Method Details

#execute_sh(rb_stdin, rb_timeout, rb_command) ⇒ Object

Ruby classes and functions



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

VALUE rb_execute_sh(VALUE self, VALUE rb_stdin, VALUE rb_timeout, VALUE rb_command) {
    int stdin_ends[2];
    int stdout_ends[2];
    int stderr_ends[2];

    if(pipe(stdin_ends) == -1 || pipe(stdout_ends) == -1 || pipe(stderr_ends) == -1) {
        rb_raise(sse_PipeFailureError, "%s:%d (%d) pipe: %s\n", __FILE__, __LINE__, errno, strerror(errno));
    }

    char **command_list = split(StringValueCStr(rb_command), (char*)" ");
    char *stdin_buf = StringValueCStr(rb_stdin);
    pid_t pid = fork();

    if(pid > 0) {
        size_t stdout_buf_size = 0;
        size_t stderr_buf_size = 0;
        size_t stdout_buf_max = 1;
        size_t stderr_buf_max = 1;
        char *stdout_buf = malloc(1);
        char *stderr_buf = malloc(1);

        stdout_buf[0] = '\0';
        stderr_buf[0] = '\0';

        time_t start = time(NULL);
        time_t timeout = NUM2INT(rb_timeout);
        int sent_stdin = 0;

        close(stdin_ends[0]);
        close(stdout_ends[1]);
        close(stderr_ends[1]);

        while(difftime(time(NULL), start) < timeout) {
            int status = waitpid(pid, NULL, WNOHANG);

            if(status == -1) {
                rb_raise(sse_WaitpidFailureError, "%s:%d (%d) waitpid: %s\n", __FILE__, __LINE__, errno, strerror(errno));
            } else if(status == 0) {
                fd_set write_set;
                fd_set read_set;
                struct timeval select_timeout = { 0, 0 };
                int maxfd = (stdin_ends[1] > stdout_ends[0] ? (stdin_ends[1] > stderr_ends[0] ? stdin_ends[1] : stderr_ends[0]) : (stdout_ends[0] > stderr_ends[0] ? stdout_ends[0] : stderr_ends[0]));

                FD_ZERO(&write_set);
                FD_ZERO(&read_set);

                if(sent_stdin == 0) {
                    FD_SET(stdin_ends[1], &write_set);
                }

                FD_SET(stdout_ends[0], &read_set);
                FD_SET(stderr_ends[0], &read_set);

                select(maxfd+1, &read_set, &write_set, NULL, &select_timeout);

                if(FD_ISSET(stdin_ends[1], &write_set)) {
                    sent_stdin = 1;

                    if(write(stdin_ends[1], stdin_buf, strlen(stdin_buf)) == -1) {
                        rb_raise(sse_WriteFailureError, "%s:%d (%d) write: %s\n", __FILE__, __LINE__, errno, strerror(errno));
                    }

                    close(stdin_ends[1]);
                }

                if(FD_ISSET(stdout_ends[0], &read_set)) {
                    char tmp_buf[251];
                    ssize_t bytes_read = read(stdout_ends[0], tmp_buf, 250);

                    if(bytes_read == -1) {
                        rb_raise(sse_ReadFailureError, "%s:%d (%d) read: %s\n", __FILE__, __LINE__, errno, strerror(errno));
                    }

                    tmp_buf[bytes_read] = '\0';

                    while((stdout_buf_size + bytes_read+1) >= stdout_buf_max) {
                        stdout_buf_max *= 2;
                        stdout_buf = realloc(stdout_buf, stdout_buf_max);
                    }

                    strncpy(stdout_buf, tmp_buf, bytes_read);
                    stdout_buf_size += bytes_read;
                    stdout_buf[stdout_buf_size] = '\0';
                }

                if(FD_ISSET(stderr_ends[0], &read_set)) {
                    char tmp_buf[251];
                    ssize_t bytes_read = read(stderr_ends[0], tmp_buf, 250);

                    if(bytes_read == -1) {
                        rb_raise(sse_ReadFailureError, "%s:%d (%d) read: %s\n", __FILE__, __LINE__, errno, strerror(errno));
                    }

                    tmp_buf[bytes_read] = '\0';

                    while((stderr_buf_size + bytes_read+1) >= stderr_buf_max) {
                        stderr_buf_max *= 2;
                        stderr_buf = realloc(stderr_buf, stderr_buf_max);
                    }

                    strncpy(stderr_buf, tmp_buf, bytes_read);
                    stderr_buf_size += bytes_read;
                    stderr_buf[stderr_buf_size] = '\0';
                }
            } else {
                close(stdout_ends[0]);
                close(stderr_ends[1]);
                VALUE result = rb_ary_new2(3);
                rb_ary_store(result, 0, INT2NUM(difftime(time(NULL), start)));
                rb_ary_store(result, 1, rb_str_new_cstr(stdout_buf));
                rb_ary_store(result, 2, rb_str_new_cstr(stderr_buf));
                return result;
            }
        }

        kill(SIGINT, pid);
        rb_raise(sse_TimeoutError, "Proccess took too long to finish.\n");
    } else if(pid == 0) {
        dup2(stdin_ends[0], STDIN_FILENO);
        dup2(stdout_ends[1], STDOUT_FILENO);
        dup2(stderr_ends[1], STDERR_FILENO);
        close(stdin_ends[1]);
        close(stdout_ends[0]);
        close(stderr_ends[0]);

        if(execvp(command_list[0], command_list) == -1) {
            rb_raise(sse_ForkFailureError, "%s:%d (%d) execvp: %s\n", __FILE__, __LINE__, errno, strerror(errno));
        }
    } else {
        rb_raise(sse_FileNotFoundError, "%s:%d (%d) execvp: %s\n", __FILE__, __LINE__, errno, strerror(errno));
    }

    VALUE thunderfury_blessed_blade_of_the_windseeker = rb_ary_new2(3);
    rb_ary_store(thunderfury_blessed_blade_of_the_windseeker, 0, INT2NUM(42));
    rb_ary_store(thunderfury_blessed_blade_of_the_windseeker, 1, rb_str_new_cstr("this is stdout"));
    rb_ary_store(thunderfury_blessed_blade_of_the_windseeker, 2, rb_str_new_cstr("this is stderr"));
    return thunderfury_blessed_blade_of_the_windseeker;
}