Skip to content

Commit

Permalink
feat xdbd: support shell service
Browse files Browse the repository at this point in the history
  • Loading branch information
ffashion authored and sanpeqf committed Apr 16, 2024
1 parent 60220e5 commit abd5b38
Show file tree
Hide file tree
Showing 15 changed files with 323 additions and 21 deletions.
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ set(SRCS
${PROJECT_SOURCE_DIR}/src/event/select/xdbd_select.c
${PROJECT_SOURCE_DIR}/src/event/xdbd_event.c
${PROJECT_SOURCE_DIR}/src/event/xdbd_timer.c
${PROJECT_SOURCE_DIR}/src/os/unix/xdbd_process.c
${PROJECT_SOURCE_DIR}/src/service/xdbd_service.c
${PROJECT_SOURCE_DIR}/src/service/shell/xdbd_shell_service.c
${PROJECT_SOURCE_DIR}/src/xdbd.c
)

Expand All @@ -44,6 +47,8 @@ set(
${PROJECT_SOURCE_DIR}/src/core/
#FIXME: check platform
${PROJECT_SOURCE_DIR}/src/os/unix/
${PROJECT_SOURCE_DIR}/src/service/
${PROJECT_SOURCE_DIR}/src/service/shell/
)

include_directories(${XADB_INCLUDES})
Expand Down
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,14 @@
# other
1. adb will recognize the shell protocol. so this xdbd version need use adb shell -x to debug for closing the recognize shell protocol. in future we will fix it
# service
```c
struct xdbd_service_t {
xdbd_pid_t pid;
xdbd_event_t *read;
xdbd_event_t *write;
}

xdbd_service_create();
// xdbd_service_fetch();
xdbd_service_destroy();
```
2 changes: 2 additions & 0 deletions include/xdbd.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef __ADBD__H__
#define __ADBD__H__

#include <bfdev/list.h>

#include <bfdev/array.h>
#include "xdbd_pool.h"
Expand All @@ -24,6 +25,7 @@
#define xdbd_msec_t unsigned

typedef struct xdbd_connection_s xdbd_connection_t;
typedef struct xdbd_adb_connection_s xdbd_adb_connection_t;
typedef struct xdbd_listening_s xdbd_listening_t;
typedef struct xdbd_event_s xdbd_event_t;
typedef struct xdbd_buf_s xdbd_buf_t;
Expand Down
12 changes: 11 additions & 1 deletion src/adb/command.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <xdbd_buf.h>
#include <xdbd_adb.h>
#include <xdbd_string.h>

#include <xdbd_service.h>
int xdbd_process_shell_cmd(xdbd_adb_request_t *r, xdbd_adb_packet_t *p, const adb_command_t *cmd);

static adb_command_t adb_cmds[] = {
Expand All @@ -19,7 +19,10 @@ static adb_command_t adb_cmds[] = {

int xdbd_process_shell_cmd(xdbd_adb_request_t *r, xdbd_adb_packet_t *p, const adb_command_t *cmd) {
xdbd_adb_packet_t *out;
xdbd_service_t *service;
xdbd_adb_connection_t *ac;

ac = r->ac;
out = xdbd_palloc(r->pool, sizeof(xdbd_adb_packet_t));
if (out == NULL) {
return XDBD_ERR;
Expand All @@ -30,6 +33,13 @@ int xdbd_process_shell_cmd(xdbd_adb_request_t *r, xdbd_adb_packet_t *p, const ad
return XDBD_ERR;
}

service = xdbd_create_service(r->pool, (xdbd_str_t)xdbd_string("shell"));
if (service == NULL) {
return XDBD_ERR;
}

// FIXME:
ac->service = service;
bfdev_log_info("shell_cmd: send okay packet\n");
xdbd_okey_packet(r, out);

Expand Down
20 changes: 19 additions & 1 deletion src/adb/packet.c
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,10 @@ int xdbd_process_close_packet(xdbd_adb_request_t *r, xdbd_adb_packet_t *p) {

int xdbd_process_write_packet(xdbd_adb_request_t *r, xdbd_adb_packet_t *p) {
xdbd_adb_packet_t *out;
xdbd_connection_t *c = r->coonection;
xdbd_adb_connection_t *ac;
xdbd_buf_t *payload;
int n = 0;
ac = r->ac;

xdbd_str_t t_data = xdbd_string("helloworld\r\n");

Expand All @@ -253,6 +256,21 @@ int xdbd_process_write_packet(xdbd_adb_request_t *r, xdbd_adb_packet_t *p) {
return XDBD_ERR;
}


payload = xdbd_create_buf(r->pool, ADB_MAX_PACKET_SIZE);
if (payload == NULL) {
return XDBD_ERR;
}

// FIXME: make it can async
n = ac->service->ops->write(ac->service, r->pool, p->payload);

n = ac->service->ops->read(ac->service, r->pool, payload);


t_data.data = payload->pos;
t_data.size = xdbd_buf_size(payload);

xdbd_write_packet(r, out, &t_data);

xdbd_buf_append_mem(r->out, r->pool, &out->header, sizeof(xdbd_adb_header_t));
Expand Down
26 changes: 23 additions & 3 deletions src/adb/xdbd_adb.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ size_t xdbd_adb_read_adb_header(xdbd_adb_request_t *r) {
int rc;
xdbd_event_t *rev;

c = r->coonection;
c = r->ac->coonection;
rev = c->read;

n = xdbd_buf_size(r->buffer);
Expand Down Expand Up @@ -79,7 +79,7 @@ size_t xdbd_adb_read_adb_payload(xdbd_adb_request_t *r) {
int rc;
xdbd_event_t *rev;

c = r->coonection;
c = r->ac->coonection;
rev = c->read;

n = xdbd_buf_size(r->buffer);
Expand Down Expand Up @@ -196,6 +196,8 @@ void xdbd_adb_process_adb_payload(xdbd_event_t *rev) {
return ;
}
}

//destroy request
}

void xdbd_adb_process_adb_header(xdbd_event_t *rev) {
Expand Down Expand Up @@ -294,6 +296,12 @@ void xdbd_adb_wait_apacket_handler(xdbd_event_t *ev) {

bfdev_log_info("%s receive size %ld\n", (char *)b->start, n);

c->ac = xdbd_adb_create_adb_connection(c);
if (c->ac == NULL) {
xdbd_adb_close_connection(c);
return;
}

c->data = xdbd_adb_create_request(c);
if (c->data == NULL) {
xdbd_adb_close_connection(c);
Expand Down Expand Up @@ -343,7 +351,19 @@ void xdbd_adb_close_connection(xdbd_connection_t *c) {
void xdbd_adb_close_request(xdbd_adb_request_t *r) {
xdbd_connection_t *c;

c = r->coonection;
c = r->ac->coonection;

xdbd_adb_close_connection(c);
}


void xdbd_adb_close_adb_connection(xdbd_adb_connection_t *ac) {
xdbd_connection_t *c;

c = ac->coonection;
xdbd_adb_close_connection(c);
}

void xdbd_adb_destroy_request(xdbd_adb_request_t *r) {
xdbd_destroy_pool(r->pool);
}
30 changes: 24 additions & 6 deletions src/adb/xdbd_adb_request.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@
xdbd_adb_request_t *xdbd_adb_create_request(xdbd_connection_t *c) {
xdbd_adb_request_t *r;
xdbd_adb_packet_t *p;
xdbd_pool_t *pool;

r = xdbd_pcalloc(c->pool, sizeof(xdbd_adb_request_t));
pool = xdbd_create_pool();

r = xdbd_pcalloc(pool, sizeof(xdbd_adb_request_t));
if (r == NULL) {
return NULL;
}

r->pool = c->pool;
r->temp_pool = c->temp_pool;

r->pool = pool;
r->buffer = c->buffer;

r->coonection = c;
r->ac = c->ac;

p = xdbd_pcalloc(r->pool, sizeof(xdbd_adb_packet_t));
if (p == NULL) {
Expand All @@ -34,3 +34,21 @@ xdbd_adb_request_t *xdbd_adb_create_request(xdbd_connection_t *c) {
r->p = p;
return r;
}

xdbd_adb_connection_t *xdbd_adb_create_adb_connection(xdbd_connection_t *c) {
xdbd_adb_connection_t *ac;

// FIXME: remove this
if (c->ac != NULL) {
return c->ac;
}
ac = xdbd_pcalloc(c->pool, sizeof(xdbd_adb_connection_t));
if (ac == NULL) {
return NULL;
}

ac->pool = c->pool;
ac->coonection = c;

return ac;
}
9 changes: 7 additions & 2 deletions src/adb/xdbd_adb_request.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,31 @@

#include "adb.h"
#include "xdbd.h"
#include "xdbd_service.h"
#include <xdbd_pool.h>
#include <xdbd_buf.h>
#include <packet.h>

struct xdbd_adb_connection_s {
xdbd_connection_t *coonection;
xdbd_pool_t *pool;
int state;

// FIXME:
xdbd_service_t *service;
};

struct xdbd_adb_request_s {
xdbd_buf_t *buffer;
xdbd_buf_t *body;
xdbd_pool_t *pool;
xdbd_pool_t *temp_pool;
xdbd_adb_header_t h;
xdbd_adb_packet_t *p;
xdbd_connection_t *coonection;
xdbd_buf_t *out;
xdbd_adb_connection_t *ac;
};

xdbd_adb_request_t *xdbd_adb_create_request(xdbd_connection_t *c);
xdbd_adb_connection_t *xdbd_adb_create_adb_connection(xdbd_connection_t *c);

#endif /*__XDBD_ADB_REQUEST__H__*/
2 changes: 1 addition & 1 deletion src/connection/connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ struct xdbd_connection_s {
struct sockaddr *local_sockaddr;
socklen_t local_socklen;


xdbd_adb_connection_t *ac;
void *data;
xdbd_listening_t *listening;

Expand Down
32 changes: 27 additions & 5 deletions src/os/unix/xdbd_process.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#include "xdbd.h"
#include <xdbd_process.h>
#include <stdlib.h>

static void xdbd_execute_proc(xdbd_t *cycle, void *data);
void xdbd_execute_proc(xdbd_t *xdbd, void *data);

xdbd_pid_t xdbd_spawn_process(xdbd_t *xdbd,
xdbd_spawn_proc_pt proc, void *data, char *name) {
Expand All @@ -13,7 +14,6 @@ xdbd_pid_t xdbd_spawn_process(xdbd_t *xdbd,

case -1:
bfdev_log_err("fork() failed while spawning \"%s\"", name);
// xdbd_close_channel(xdbd_processes[s].channel, cycle->log);
return XDBD_INVALID_PID;

case 0:
Expand All @@ -27,12 +27,34 @@ xdbd_pid_t xdbd_spawn_process(xdbd_t *xdbd,
return pid;
}

xdbd_pid_t xdbd_execute(xdbd_t *cycle, xdbd_exec_ctx_t *ctx) {
return xdbd_spawn_process(cycle, xdbd_execute_proc, ctx, ctx->name);
xdbd_pid_t xdbd_spawn_pty_process(xdbd_t *xdbd,
xdbd_spawn_proc_pt proc, void *data, char *name, xdbd_socket_t *mfd, char *ptyname, struct termios *termp, struct winsize *winp) {
xdbd_pid_t pid;

pid = forkpty(mfd, ptyname, termp, winp);
switch (pid) {

case -1:
bfdev_log_err("fork() failed while spawning \"%s\"", name);
return XDBD_INVALID_PID;

case 0:
proc(xdbd, data);
break;

default:
break;
}

return pid;
}

xdbd_pid_t xdbd_execute(xdbd_t *xdbd, xdbd_exec_ctx_t *ctx) {
return xdbd_spawn_process(xdbd, xdbd_execute_proc, ctx, ctx->name);
}


static void xdbd_execute_proc(xdbd_t *cycle, void *data) {
void xdbd_execute_proc(xdbd_t *xdbd, void *data) {
xdbd_exec_ctx_t *ctx = data;

if (execve(ctx->path, ctx->argv, ctx->envp) == -1) {
Expand Down
14 changes: 12 additions & 2 deletions src/os/unix/xdbd_process.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
#define __NGX_PROCESS__H__
#include <xdbd.h>

#if defined(XDBD_CONFIG_LINUX)
#include <pty.h>
#elif defined(XDBD_CONFIG_OSX)
#include <util.h>
#endif

typedef pid_t xdbd_pid_t;

#define XDBD_INVALID_PID -1
Expand All @@ -11,7 +17,6 @@ typedef void (*xdbd_spawn_proc_pt) (xdbd_t *xdbd, void *data);
typedef struct {
xdbd_pid_t pid;
xdbd_spawn_proc_pt proc;

} xdbd_process_t;

typedef struct {
Expand All @@ -24,7 +29,12 @@ typedef struct {
xdbd_pid_t xdbd_spawn_process(xdbd_t *xdbd,
xdbd_spawn_proc_pt proc, void *data, char *name);

xdbd_pid_t xdbd_execute(xdbd_t *cycle, xdbd_exec_ctx_t *ctx);
xdbd_pid_t xdbd_spawn_pty_process(xdbd_t *xdbd,
xdbd_spawn_proc_pt proc, void *data, char *name, xdbd_socket_t *mfd, char *ptyname, struct termios *termp, struct winsize *winp);

xdbd_pid_t xdbd_execute(xdbd_t *xdbd, xdbd_exec_ctx_t *ctx);

void xdbd_execute_proc(xdbd_t *xdbd, void *data);

#define ngx_getpid getpid
#define ngx_getppid getppid
Expand Down
Loading

0 comments on commit abd5b38

Please sign in to comment.