Skip to content

Commit

Permalink
protocol: fix uv handle closing
Browse files Browse the repository at this point in the history
  • Loading branch information
tsl0922 committed Feb 6, 2021
1 parent 75105f6 commit 215849b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 19 deletions.
37 changes: 20 additions & 17 deletions src/protocol.c
Original file line number Diff line number Diff line change
Expand Up @@ -151,22 +151,22 @@ static int pty_resize(int pty, int cols, int rows) {
}

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) {
Expand Down Expand Up @@ -262,8 +262,8 @@ static int spawn_process(struct pss_tty *pss) {

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);
Expand Down Expand Up @@ -335,10 +335,13 @@ int callback_tty(struct lws *wsi, enum lws_callback_reasons reason, void *user,

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) {
Expand Down Expand Up @@ -368,7 +371,7 @@ int callback_tty(struct lws *wsi, enum lws_callback_reasons reason, void *user,
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) {
Expand Down Expand Up @@ -400,7 +403,7 @@ int callback_tty(struct lws *wsi, enum lws_callback_reasons reason, void *user,
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:
Expand Down Expand Up @@ -430,7 +433,7 @@ int callback_tty(struct lws *wsi, enum lws_callback_reasons reason, void *user,
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);
Expand All @@ -440,7 +443,7 @@ int callback_tty(struct lws *wsi, enum lws_callback_reasons reason, void *user,
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;
Expand All @@ -456,13 +459,13 @@ int callback_tty(struct lws *wsi, enum lws_callback_reasons reason, void *user,
} 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;
Expand Down Expand Up @@ -510,7 +513,7 @@ int callback_tty(struct lws *wsi, enum lws_callback_reasons reason, void *user,
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);
}

Expand Down
1 change: 1 addition & 0 deletions src/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ static void server_free(struct server *ts) {
}
}
uv_signal_stop(&ts->watcher);
uv_close((uv_handle_t *)&server->watcher, NULL);
uv_loop_close(ts->loop);
free(ts->loop);
free(ts);
Expand Down
4 changes: 2 additions & 2 deletions src/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ struct pty_proc {
ssize_t pty_len;
int err_count;

uv_pipe_t in_pipe;
uv_pipe_t out_pipe;
uv_pipe_t *in_pipe;
uv_pipe_t *out_pipe;

LIST_ENTRY(pty_proc) entry;
};
Expand Down

0 comments on commit 215849b

Please sign in to comment.