}
static void close_cb(uv_handle_t *handle) {
- struct pty_proc *proc = container_of((uv_pipe_t *)handle, struct pty_proc, out_pipe);
- free(proc);
+ free(handle);
}
static void pty_proc_free(struct pty_proc *proc) {
- uv_read_stop((uv_stream_t *)&proc->out_pipe);
- if (proc->pty_fd >0) close(proc->pty_fd);
+ uv_read_stop((uv_stream_t *)proc->out_pipe);
+ if (proc->pty_fd >= 0) close(proc->pty_fd);
if (proc->pty_buffer != NULL) {
free(proc->pty_buffer);
proc->pty_buffer = NULL;
for (int i = 0; i < proc->argc; i++) {
free(proc->args[i]);
}
- uv_close((uv_handle_t *)&proc->in_pipe, NULL);
- uv_close((uv_handle_t *)&proc->out_pipe, close_cb);
+ uv_close((uv_handle_t *)proc->in_pipe, close_cb);
+ uv_close((uv_handle_t *)proc->out_pipe, close_cb);
+ free(proc);
}
static void alloc_cb(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf) {
proc->pty_fd = master;
proc->pid = pid;
- proc->out_pipe.data = pss;
- if (fd_duplicate(master, &proc->in_pipe) || fd_duplicate(master, &proc->out_pipe))
+ proc->out_pipe->data = pss;
+ if (fd_duplicate(master, proc->in_pipe) || fd_duplicate(master, proc->out_pipe))
return 1;
lws_callback_on_writable(pss->wsi);
pss->proc = proc = xmalloc(sizeof(struct pty_proc));
memset(proc, 0, sizeof(struct pty_proc));
+ proc->pty_fd = -1;
proc->status = -1;
proc->state = STATE_INIT;
- uv_pipe_init(server->loop, &proc->in_pipe, 0);
- uv_pipe_init(server->loop, &proc->out_pipe, 0);
+ proc->in_pipe = xmalloc(sizeof(uv_pipe_t));
+ uv_pipe_init(server->loop, proc->in_pipe, 0);
+ proc->out_pipe = xmalloc(sizeof(uv_pipe_t));
+ uv_pipe_init(server->loop, proc->out_pipe, 0);
if (server->url_arg) {
while (lws_hdr_copy_fragment(wsi, buf, sizeof(buf), WSI_TOKEN_HTTP_URI_ARGS, n++) > 0) {
if (!pss->initialized) {
if (pss->initial_cmd_index == sizeof(initial_cmds)) {
pss->initialized = true;
- uv_read_start((uv_stream_t *)&proc->out_pipe, alloc_cb, read_cb);
+ uv_read_start((uv_stream_t *)proc->out_pipe, alloc_cb, read_cb);
break;
}
if (send_initial_message(wsi, pss->initial_cmd_index) < 0) {
proc->pty_buffer = NULL;
}
- uv_read_start((uv_stream_t *)&proc->out_pipe, alloc_cb, read_cb);
+ uv_read_start((uv_stream_t *)proc->out_pipe, alloc_cb, read_cb);
break;
case LWS_CALLBACK_RECEIVE:
proc = pss->proc;
switch (command) {
case INPUT:
- if (proc->pty_fd == 0) break;
+ if (proc->pty_fd < 0) break;
if (server->readonly) break;
char *data = xmalloc(pss->len - 1);
uv_write_t *req = xmalloc(sizeof(uv_write_t));
req->data = data;
- int err = uv_write(req, (uv_stream_t *)&proc->in_pipe, &b, 1, write_cb);
+ int err = uv_write(req, (uv_stream_t *)proc->in_pipe, &b, 1, write_cb);
if (err) {
lwsl_err("uv_write: %s (%s)\n", uv_err_name(err), uv_strerror(err));
return -1;
} break;
case PAUSE:
if (proc->state == STATE_INIT) {
- uv_read_stop((uv_stream_t *)&proc->out_pipe);
+ uv_read_stop((uv_stream_t *)proc->out_pipe);
proc->state = STATE_PAUSE;
}
break;
case RESUME:
if (proc->state == STATE_PAUSE) {
- uv_read_start((uv_stream_t *)&proc->out_pipe, alloc_cb, read_cb);
+ uv_read_start((uv_stream_t *)proc->out_pipe, alloc_cb, read_cb);
proc->state = STATE_INIT;
}
break;
pty_proc_free(proc);
} else {
proc->state = STATE_KILL;
- uv_read_stop((uv_stream_t *)&proc->out_pipe);
+ uv_read_stop((uv_stream_t *)proc->out_pipe);
kill_process(proc);
}