server: remove sys/queue dep

This commit is contained in:
Shuanglei Tao
2019-11-30 16:17:04 +08:00
parent 59601329ae
commit fe7bad2c63
3 changed files with 6 additions and 43 deletions

View File

@@ -6,7 +6,6 @@
#include <fcntl.h> #include <fcntl.h>
#include <errno.h> #include <errno.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <sys/queue.h>
#if defined(__OpenBSD__) || defined(__APPLE__) #if defined(__OpenBSD__) || defined(__APPLE__)
#include <util.h> #include <util.h>
@@ -106,42 +105,25 @@ check_host_origin(struct lws *wsi) {
return len > 0 && strcasecmp(buf, host_buf) == 0; return len > 0 && strcasecmp(buf, host_buf) == 0;
} }
void
tty_client_remove(struct tty_client *client) {
struct tty_client *iterator;
LIST_FOREACH(iterator, &server->clients, list) {
if (iterator == client) {
LIST_REMOVE(iterator, list);
server->client_count--;
break;
}
}
}
void void
tty_client_destroy(struct tty_client *client) { tty_client_destroy(struct tty_client *client) {
if (!client->running || client->pid <= 0) if (client->pid <= 0)
goto cleanup; goto cleanup;
client->running = false;
// kill process (group) and free resource // kill process (group) and free resource
int pgid = getpgid(client->pid); int pgid = getpgid(client->pid);
int pid = pgid > 0 ? -pgid : client->pid; int pid = pgid > 0 ? -pgid : client->pid;
lwsl_notice("sending %s (%d) to process (group) %d\n", server->sig_name, server->sig_code, pid);
if (kill(pid, server->sig_code) != 0) { if (kill(pid, server->sig_code) != 0) {
if (errno == ESRCH)
goto cleanup;
lwsl_err("kill: %d, errno: %d (%s)\n", pid, errno, strerror(errno)); lwsl_err("kill: %d, errno: %d (%s)\n", pid, errno, strerror(errno));
} }
pid_t pid_out;
client->exit_status = wait_proc(client->pid, &pid_out);
if (pid_out > 0) {
lwsl_notice("process exited with code %d, pid: %d\n", client->exit_status, pid_out);
}
close(client->pty);
cleanup: cleanup:
uv_read_stop((uv_stream_t *) &client->pipe); uv_read_stop((uv_stream_t *) &client->pipe);
close(client->pty);
// free the buffer // free the buffer
if (client->buffer != NULL) if (client->buffer != NULL)
free(client->buffer); free(client->buffer);
@@ -151,9 +133,6 @@ cleanup:
for (int i = 0; i < client->argc; i++) { for (int i = 0; i < client->argc; i++) {
free(client->args[i]); free(client->args[i]);
} }
// remove from client list
tty_client_remove(client);
} }
void void
@@ -224,7 +203,6 @@ spawn_process(struct tty_client *client) {
lwsl_notice("started process, pid: %d\n", pid); lwsl_notice("started process, pid: %d\n", pid);
client->pid = pid; client->pid = pid;
client->pty = pty; client->pty = pty;
client->running = true;
if (client->size.ws_row > 0 && client->size.ws_col > 0) if (client->size.ws_row > 0 && client->size.ws_col > 0)
ioctl(client->pty, TIOCSWINSZ, &client->size); ioctl(client->pty, TIOCSWINSZ, &client->size);
@@ -265,7 +243,6 @@ callback_tty(struct lws *wsi, enum lws_callback_reasons reason,
break; break;
case LWS_CALLBACK_ESTABLISHED: case LWS_CALLBACK_ESTABLISHED:
client->running = false;
client->initialized = false; client->initialized = false;
client->initial_cmd_index = 0; client->initial_cmd_index = 0;
client->authenticated = false; client->authenticated = false;
@@ -286,7 +263,6 @@ callback_tty(struct lws *wsi, enum lws_callback_reasons reason,
} }
} }
LIST_INSERT_HEAD(&server->clients, client, list);
server->client_count++; server->client_count++;
lws_hdr_copy(wsi, buf, sizeof(buf), WSI_TOKEN_GET_URI); lws_hdr_copy(wsi, buf, sizeof(buf), WSI_TOKEN_GET_URI);
@@ -416,6 +392,7 @@ callback_tty(struct lws *wsi, enum lws_callback_reasons reason,
break; break;
case LWS_CALLBACK_CLOSED: case LWS_CALLBACK_CLOSED:
server->client_count--;
lwsl_notice("WS closed from %s, clients: %d\n", client->address, server->client_count); lwsl_notice("WS closed from %s, clients: %d\n", client->address, server->client_count);
tty_client_destroy(client); tty_client_destroy(client);
if (server->once && server->client_count == 0) { if (server->once && server->client_count == 0) {

View File

@@ -109,7 +109,6 @@ tty_server_new(int argc, char **argv, int start) {
ts = xmalloc(sizeof(struct tty_server)); ts = xmalloc(sizeof(struct tty_server));
memset(ts, 0, sizeof(struct tty_server)); memset(ts, 0, sizeof(struct tty_server));
LIST_INIT(&ts->clients);
ts->client_count = 0; ts->client_count = 0;
ts->sig_code = SIGHUP; ts->sig_code = SIGHUP;
sprintf(ts->terminal_type, "%s", "xterm-256color"); sprintf(ts->terminal_type, "%s", "xterm-256color");
@@ -188,14 +187,6 @@ signal_cb(uv_signal_t *watcher, int signum) {
status = wait_proc(-1, &pid); status = wait_proc(-1, &pid);
if (pid > 0) { if (pid > 0) {
lwsl_notice("process exited with code %d, pid: %d\n", status, pid); lwsl_notice("process exited with code %d, pid: %d\n", status, pid);
struct tty_client *iterator;
LIST_FOREACH(iterator, &server->clients, list) {
if (iterator->pid == pid) {
iterator->running = false;
iterator->exit_status = status;
break;
}
}
} }
return; return;
default: default:

View File

@@ -1,6 +1,5 @@
#include <stdbool.h> #include <stdbool.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <sys/queue.h>
#include <uv.h> #include <uv.h>
// client message // client message
@@ -21,7 +20,6 @@ extern struct lws_context *context;
extern struct tty_server *server; extern struct tty_server *server;
struct tty_client { struct tty_client {
bool running;
bool initialized; bool initialized;
int initial_cmd_index; int initial_cmd_index;
bool authenticated; bool authenticated;
@@ -41,8 +39,6 @@ struct tty_client {
ssize_t pty_len; ssize_t pty_len;
uv_pipe_t pipe; uv_pipe_t pipe;
LIST_ENTRY(tty_client) list;
}; };
struct pss_http { struct pss_http {
@@ -53,7 +49,6 @@ struct pss_http {
}; };
struct tty_server { struct tty_server {
LIST_HEAD(client, tty_client) clients; // client list
int client_count; // client count int client_count; // client count
char *prefs_json; // client preferences char *prefs_json; // client preferences
char *credential; // encoded basic auth credential char *credential; // encoded basic auth credential