优化代码&env支持&net fd自动回收支持(fastipc删除流程有问题,需要优化,可能导致卡死
This commit is contained in:
4
.vscode/settings.json
vendored
4
.vscode/settings.json
vendored
@@ -209,7 +209,9 @@
|
||||
"*.def": "c",
|
||||
"poll.h": "c",
|
||||
"u_rpc_1.h": "c",
|
||||
"mk_sys.h": "c"
|
||||
"mk_sys.h": "c",
|
||||
"environ.h": "c",
|
||||
"lwipopts.h": "c"
|
||||
},
|
||||
"cortex-debug.showRTOS": false,
|
||||
"cortex-debug.variableUseNaturalFormat": true,
|
||||
|
||||
2
TODO.md
2
TODO.md
@@ -26,7 +26,7 @@
|
||||
* [ ] posix sema支持
|
||||
* [ ] vfork + exec实现
|
||||
* [ ] 几大组件稳定性测试
|
||||
* [ ] 删除之前用于log的sem
|
||||
* [x] 删除之前用于log的sem
|
||||
### mid prio
|
||||
* [x] net server support
|
||||
* [x] block driver
|
||||
|
||||
@@ -1,20 +1,20 @@
|
||||
message("========use armv7_8.cmake")
|
||||
|
||||
set(CMAKE_C_FLAGS "-mcpu=${CONFIG_ARCH} -Ofast -g3 -mfloat-abi=${CONFIG_FLOAT_TYPE} -mthumb -DMKRTOS \
|
||||
set(CMAKE_C_FLAGS "-mcpu=${CONFIG_ARCH} -Os -g3 -mfloat-abi=${CONFIG_FLOAT_TYPE} -mthumb -DMKRTOS \
|
||||
-std=gnu11 -ffunction-sections -fdata-sections -fno-builtin -u=_printf_float \
|
||||
-nostartfiles -nodefaultlibs -nostdlib -nostdinc \
|
||||
-fno-stack-protector -Wl,--gc-section -D__ARM_ARCH_7M__ \
|
||||
-include ${CMAKE_SOURCE_DIR}/build/autoconf.h \
|
||||
" CACHE STRING "" FORCE)
|
||||
|
||||
set(CMAKE_CXX_FLAGS "-mcpu=${CONFIG_ARCH} -O0 -g3 -mfloat-abi=${CONFIG_FLOAT_TYPE} -mthumb -DMKRTOS -std=c++11 \
|
||||
set(CMAKE_CXX_FLAGS "-mcpu=${CONFIG_ARCH} -Os -g3 -mfloat-abi=${CONFIG_FLOAT_TYPE} -mthumb -DMKRTOS -std=c++11 \
|
||||
-fmessage-length=0 -Xlinker --print-map -Wall -W -fno-stack-protector \
|
||||
-u=_printf_float -D__ARM_ARCH_7M__ \
|
||||
-ffunction-sections -fdata-sections -fno-builtin -nostartfiles -nodefaultlibs -nostdlib -nostdinc -Xlinker \
|
||||
-include ${CMAKE_SOURCE_DIR}/build/autoconf.h \
|
||||
" CACHE STRING "" FORCE)
|
||||
|
||||
set(CMAKE_ASM_FLAGS "-mcpu=${CONFIG_ARCH} -O0 -g3 -mfloat-abi=${CONFIG_FLOAT_TYPE} -mthumb -DMKRTOS \
|
||||
set(CMAKE_ASM_FLAGS "-mcpu=${CONFIG_ARCH} -Os -g3 -mfloat-abi=${CONFIG_FLOAT_TYPE} -mthumb -DMKRTOS \
|
||||
-u=_printf_float -std=gnu11 -ffunction-sections -fdata-sections -fno-builtin \
|
||||
-nostartfiles -nodefaultlibs -nostdlib -nostdinc -Xlinker -fno-stack-protector -D__ARM_ARCH_7M__ \
|
||||
-include ${CMAKE_SOURCE_DIR}/build/autoconf.h \
|
||||
|
||||
@@ -47,6 +47,7 @@ void sema_up(sema_t *obj)
|
||||
umword_t status;
|
||||
thread_t *th = thread_get_current();
|
||||
|
||||
again:
|
||||
status = spinlock_lock(&obj->lock);
|
||||
if (slist_is_empty(&obj->suspend_head))
|
||||
{
|
||||
@@ -70,14 +71,6 @@ void sema_up(sema_t *obj)
|
||||
{
|
||||
thread_sleep_del_and_wakeup(first_wait->thread);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// 超时退出,但是切出来的时候切到了唤醒线程中,所以这里不是suspend状态。
|
||||
thread_sleep_del(first_wait->thread);
|
||||
// 这里引用计数要-1
|
||||
ref_counter_dec_and_release(&first_wait->thread->ref, &first_wait->thread->kobj);
|
||||
}
|
||||
if (obj->cnt < obj->max_cnt)
|
||||
{
|
||||
obj->cnt++;
|
||||
@@ -86,8 +79,15 @@ void sema_up(sema_t *obj)
|
||||
{
|
||||
//还原优先级
|
||||
thread_set_prio(th, obj->hold_th_prio);
|
||||
obj->hold_th = NULL;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// 超时退出,但是切出来的时候没有切到休眠线程,切到了这里。
|
||||
spinlock_set(&obj->lock, status);
|
||||
goto again;
|
||||
}
|
||||
// printk("up1 sema cnt:%d max:%d.\n", obj->cnt, obj->max_cnt);
|
||||
}
|
||||
spinlock_set(&obj->lock, status);
|
||||
if (cpulock_get_status())
|
||||
@@ -118,7 +118,7 @@ again:
|
||||
thread_set_prio(((thread_t*)(obj->hold_th)), thread_get_prio(th));
|
||||
}
|
||||
}
|
||||
remain_sleep = thread_sleep(ticks);
|
||||
remain_sleep = thread_sleep(ticks); //注意:这里可能不是up环保型
|
||||
if (remain_sleep == 0 && ticks != 0)
|
||||
{
|
||||
// 超时退出的,直接从列表中删除
|
||||
@@ -145,7 +145,6 @@ again:
|
||||
obj->hold_th = &th->kobj;
|
||||
obj->hold_th_prio = thread_get_prio(th);
|
||||
}
|
||||
// printk("down sema cnt:%d max:%d.\n", obj->cnt, obj->max_cnt);
|
||||
}
|
||||
spinlock_set(&obj->lock, status);
|
||||
return remain_sleep;
|
||||
|
||||
@@ -92,17 +92,7 @@ static void knl_main(void)
|
||||
|
||||
if (thread_get_ipc_state(init_thread) != THREAD_IPC_ABORT)
|
||||
{
|
||||
#if 0
|
||||
int ret = thread_ipc_call(init_thread, msg_tag_init4(0, 3, 0, 0x0005 /*PM_PROT*/),
|
||||
&tag, ipc_timeout_create2(3000, 3000), &user_id, TRUE);
|
||||
|
||||
if (ret < 0)
|
||||
{
|
||||
printk("%s:%d ret:%d\n", __func__, __LINE__, ret);
|
||||
}
|
||||
#endif
|
||||
#define PM_PROT 0x0005
|
||||
|
||||
#define MAGIC_NS_USERPID 0xbabababa
|
||||
entry_frame_t f;
|
||||
f.regs[0] = msg_tag_init4(0, 3, 0, PM_PROT).raw;
|
||||
@@ -147,7 +137,6 @@ void thread_calc_cpu_usage(void)
|
||||
calc = calc > 1000 ? 1000 : calc;
|
||||
cpu_usage[cur_cpu_id] = 1000 - calc;
|
||||
cpu_usage_last_tick_val[cur_cpu_id] = tick;
|
||||
// printk("%d\n", cpu_usage[arch_get_current_cpu_id()]);
|
||||
}
|
||||
}
|
||||
uint16_t cpu_get_current_usage(void)
|
||||
@@ -283,13 +272,17 @@ bool_t task_knl_kill(thread_t *kill_thread, bool_t is_knl)
|
||||
status2 = spinlock_lock(&del_lock);
|
||||
if (stack_len(&kill_thread->com->fast_ipc_stack) != 0)
|
||||
{
|
||||
// 在通信的时候出现了错误
|
||||
// fast_ipc需要测试场景
|
||||
// 1. 在ipc到其他进程中时其他进程死亡
|
||||
// 2. 在ipc到其他进程中时当前进程死亡
|
||||
int ret;
|
||||
thread_fast_ipc_item_t ipc_item;
|
||||
|
||||
ret = thread_fast_ipc_restore(kill_thread);
|
||||
if (ret >= 0)
|
||||
{
|
||||
// 还原栈和usp TODO: arch相关的
|
||||
// 还原栈和usp FIXME:arch相关的
|
||||
thread_user_pf_restore(kill_thread, (void *)arch_get_user_sp());
|
||||
mpu_switch_to_task(thread_get_bind_task(kill_thread));
|
||||
ref_counter_dec_and_release(&task->ref_cn, &task->kobj);
|
||||
|
||||
@@ -93,6 +93,10 @@ int cd(int argc, char *agrv[])
|
||||
}
|
||||
|
||||
ret = chdir(agrv[1]);
|
||||
if (ret >= 0)
|
||||
{
|
||||
setenv("PWD", agrv[1], 1);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN), cd, cd, cd command);
|
||||
|
||||
@@ -1455,17 +1455,42 @@ void shellExec(Shell *shell)
|
||||
}
|
||||
else
|
||||
{
|
||||
uint8_t params[FS_RPC_BUF_LEN];
|
||||
uint8_t params[96/*FIXME:数组溢出*/];
|
||||
uint8_t envs[64/*FIXME:*/];
|
||||
int params_len = 0;
|
||||
int envs_len = 0;
|
||||
int pid;
|
||||
bool_t bg_run = FALSE;
|
||||
int task_mem_blk = 0;
|
||||
|
||||
// 处理params
|
||||
for (int i = 1; i < shell->parser.paramCount; i++)
|
||||
{
|
||||
memcpy(¶ms[params_len], shell->parser.param[i], strlen(shell->parser.param[i]) + 1); // copy the string
|
||||
params_len += strlen(shell->parser.param[i]) + 1;
|
||||
}
|
||||
if (shell->parser.param[shell->parser.paramCount - 1][0] == '~')
|
||||
{
|
||||
//指定启动的mem,参数少一个
|
||||
task_mem_blk = atoi(&(shell->parser.param[shell->parser.paramCount - 1][1]));
|
||||
shell->parser.paramCount--;
|
||||
}
|
||||
if (strcmp(shell->parser.param[shell->parser.paramCount - 1], "&") == 0)
|
||||
{
|
||||
//后台启动,参数少一个
|
||||
shell->parser.param[shell->parser.paramCount - 1] = NULL;
|
||||
shell->parser.paramCount--;
|
||||
bg_run = TRUE;
|
||||
}
|
||||
// 处理envs
|
||||
for (char **e = __environ; *e; e++)
|
||||
{
|
||||
memcpy(&envs[envs_len], *e, strlen(*e) + 1);
|
||||
envs_len+= strlen(*e)+1;
|
||||
}
|
||||
|
||||
//!< 内建命令中未找到,则执行应用
|
||||
pid = pm_run_app(shell->parser.param[0], 0 /*PM_APP_BG_RUN*/, params, params_len);
|
||||
pid = pm_run_app(shell->parser.param[0], task_mem_blk, params, params_len, envs, envs_len);
|
||||
if (pid < 0)
|
||||
{
|
||||
shellWriteString(shell, shellText[SHELL_TEXT_CMD_NOT_FOUND]);
|
||||
@@ -1474,7 +1499,7 @@ void shellExec(Shell *shell)
|
||||
{
|
||||
pid_t cur_pid;
|
||||
|
||||
if (strcmp(shell->parser.param[shell->parser.paramCount - 1], "&") != 0)
|
||||
if (!bg_run)
|
||||
{
|
||||
shell->parser.param[shell->parser.paramCount - 1] = NULL;
|
||||
shell->parser.paramCount--;
|
||||
|
||||
@@ -22,6 +22,7 @@
|
||||
#include <poll.h>
|
||||
#include <u_path.h>
|
||||
#include "kstat.h"
|
||||
#include "svr_path.h"
|
||||
#define FS_PATH_LEN 64
|
||||
static char cur_path[FS_PATH_LEN] = "/";
|
||||
// AUTO_CALL(101)
|
||||
@@ -36,9 +37,9 @@ void fs_backend_init(void)
|
||||
assert(msg_tag_get_val(tag) >= 0);
|
||||
if (cur_pid != 0)
|
||||
{
|
||||
assert(be_open("/dev/tty", O_RDWR, 0) >= 0);
|
||||
assert(be_open("/dev/tty", O_RDWR, 0) >= 0);
|
||||
assert(be_open("/dev/tty", O_RDWR, 0) >= 0);
|
||||
assert(be_open(TTY_PATCH, O_RDWR, 0) >= 0);
|
||||
assert(be_open(TTY_PATCH, O_RDWR, 0) >= 0);
|
||||
assert(be_open(TTY_PATCH, O_RDWR, 0) >= 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -109,10 +110,6 @@ int be_close(int fd)
|
||||
}
|
||||
switch (u_fd.type)
|
||||
{
|
||||
case FD_TTY:
|
||||
{
|
||||
}
|
||||
break;
|
||||
case FD_FS:
|
||||
{
|
||||
return fs_close(u_fd.priv_fd);
|
||||
@@ -131,6 +128,7 @@ long sys_be_close(va_list ap)
|
||||
|
||||
return be_close(fd);
|
||||
}
|
||||
#if 0
|
||||
static int be_tty_read(char *buf, long size)
|
||||
{
|
||||
pid_t pid;
|
||||
@@ -167,6 +165,7 @@ static int be_tty_read(char *buf, long size)
|
||||
}
|
||||
return r_len;
|
||||
}
|
||||
#endif
|
||||
long be_read(long fd, char *buf, long size)
|
||||
{
|
||||
fd_map_entry_t u_fd;
|
||||
@@ -180,7 +179,11 @@ long be_read(long fd, char *buf, long size)
|
||||
{
|
||||
case FD_TTY:
|
||||
{
|
||||
#if 0
|
||||
return be_tty_read(buf, size);
|
||||
#else
|
||||
return -ENOSYS;
|
||||
#endif
|
||||
}
|
||||
break;
|
||||
case FD_FS:
|
||||
@@ -216,7 +219,7 @@ long be_write(long fd, char *buf, long size)
|
||||
}
|
||||
else
|
||||
{
|
||||
cons_write(buf, size);
|
||||
return -ENOSYS;
|
||||
}
|
||||
return size;
|
||||
}
|
||||
@@ -247,6 +250,7 @@ long be_readv(long fd, const struct iovec *iov, long iovcnt)
|
||||
{
|
||||
case FD_TTY:
|
||||
{
|
||||
#if 0
|
||||
pid_t pid;
|
||||
int read_cn;
|
||||
|
||||
@@ -271,6 +275,9 @@ long be_readv(long fd, const struct iovec *iov, long iovcnt)
|
||||
}
|
||||
}
|
||||
wlen += read_cn;
|
||||
#else
|
||||
return -ENOSYS;
|
||||
#endif
|
||||
}
|
||||
break;
|
||||
case FD_FS:
|
||||
@@ -311,7 +318,7 @@ long be_writev(long fd, const struct iovec *iov, long iovcnt)
|
||||
}
|
||||
else
|
||||
{
|
||||
cons_write(iov[i].iov_base, iov[i].iov_len);
|
||||
return -ENOSYS;
|
||||
}
|
||||
wlen += iov[i].iov_len;
|
||||
}
|
||||
@@ -377,13 +384,7 @@ long be_ioctl(long fd, long req, void *args)
|
||||
{
|
||||
case FD_TTY:
|
||||
{
|
||||
struct winsize *wsz = args;
|
||||
|
||||
wsz->ws_row = 30;
|
||||
wsz->ws_col = 30;
|
||||
wsz->ws_xpixel = 800;
|
||||
wsz->ws_ypixel = 480;
|
||||
ret = 0;
|
||||
return -ENOSYS;
|
||||
}
|
||||
break;
|
||||
case FD_FS:
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
#include <sys/types.h>
|
||||
#define PM_KILL_TASK_ALL 0x1
|
||||
|
||||
int pm_run_app(const char *path, int flags, uint8_t *params, int params_len);
|
||||
int pm_run_app(const char *path, int mem_block, uint8_t *params, int params_len, uint8_t *envs, int envs_len);
|
||||
int pm_kill_task(int pid, int flags, int exit_code);
|
||||
int pm_watch_pid(obj_handler_t sig_hd, pid_t pid, int flags);
|
||||
int pm_copy_data(pid_t src_pid, pid_t dst_pid, addr_t src_addr, addr_t dst_addr, size_t len);
|
||||
|
||||
@@ -35,7 +35,7 @@ typedef struct pm
|
||||
#define PM_APP_BG_RUN 0x1
|
||||
|
||||
void pm_svr_obj_init(pm_t *pm);
|
||||
int pm_rpc_run_app(const char *path, int flags, char *params, int params_len);
|
||||
int pm_rpc_run_app(const char *path, int mem_block, char *params, int params_len, char *env, int envs_len);
|
||||
int pm_rpc_kill_task(int src_pid, int pid, int flags, int exit_code);
|
||||
|
||||
int pm_rpc_watch_pid(pm_t *pm, obj_handler_t sig_rcv_hd, pid_t pid, int flags);
|
||||
|
||||
5
mkrtos_user/lib/sys_svr/inc/svr_path.h
Normal file
5
mkrtos_user/lib/sys_svr/inc/svr_path.h
Normal file
@@ -0,0 +1,5 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#define TTY_PATCH "/dev/tty"
|
||||
#define NET_PATCH "/dev/net"
|
||||
@@ -11,29 +11,38 @@
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
|
||||
RPC_GENERATION_CALL4(pm_t, PM_PROT, PM_RUN_APP, run_app,
|
||||
rpc_ref_file_array_t, rpc_file_array_t, RPC_DIR_IN, RPC_TYPE_DATA, path,
|
||||
rpc_int_t, rpc_int_t, RPC_DIR_IN, RPC_TYPE_DATA, flags,
|
||||
rpc_ref_file_array_t, rpc_file_array_t, RPC_DIR_IN, RPC_TYPE_DATA, params,
|
||||
rpc_int_t, rpc_int_t, RPC_DIR_IN, RPC_TYPE_DATA, params_len)
|
||||
int pm_run_app(const char *path, int flags, uint8_t *params, int params_len)
|
||||
RPC_GENERATION_CALL6(pm_t, PM_PROT, PM_RUN_APP, run_app,
|
||||
rpc_ref_array_uint32_t_uint8_t_64_t, rpc_array_uint32_t_uint8_t_64_t, RPC_DIR_IN, RPC_TYPE_DATA, path,
|
||||
rpc_int_t, rpc_int_t, RPC_DIR_IN, RPC_TYPE_DATA, mem_block,
|
||||
rpc_ref_array_uint32_t_uint8_t_96_t, rpc_array_uint32_t_uint8_t_96_t, RPC_DIR_IN, RPC_TYPE_DATA, params,
|
||||
rpc_int_t, rpc_int_t, RPC_DIR_IN, RPC_TYPE_DATA, params_len,
|
||||
rpc_ref_array_uint32_t_uint8_t_64_t, rpc_array_uint32_t_uint8_t_64_t, RPC_DIR_IN, RPC_TYPE_DATA, envs,
|
||||
rpc_int_t, rpc_int_t, RPC_DIR_IN, RPC_TYPE_DATA, envs_len)
|
||||
int pm_run_app(const char *path, int mem_block, uint8_t *params, int params_len, uint8_t *envs, int envs_len)
|
||||
{
|
||||
rpc_ref_file_array_t rpc_path = {
|
||||
rpc_ref_array_uint32_t_uint8_t_64_t rpc_path = {
|
||||
.data = (uint8_t *)path,
|
||||
.len = MIN(strlen(path) + 1, FS_RPC_BUF_LEN),
|
||||
.len = MIN(strlen(path) + 1, 64),
|
||||
};
|
||||
rpc_int_t rpc_flags = {
|
||||
.data = flags,
|
||||
rpc_int_t rpc_mem_block = {
|
||||
.data = mem_block,
|
||||
};
|
||||
rpc_ref_file_array_t rpc_params = {
|
||||
.data = (uint8_t *)params,
|
||||
.len = MIN(params_len, FS_RPC_BUF_LEN),
|
||||
rpc_ref_array_uint32_t_uint8_t_96_t rpc_params = {
|
||||
.data = (uint8_t *)params?params:"",
|
||||
.len = MIN(params_len, 96),
|
||||
};
|
||||
rpc_int_t rpc_params_len = {
|
||||
.data = params_len,
|
||||
};
|
||||
msg_tag_t tag = pm_t_run_app_call(u_get_global_env()->ns_hd, &rpc_path, &rpc_flags,
|
||||
&rpc_params, &rpc_params_len);
|
||||
rpc_ref_array_uint32_t_uint8_t_64_t rpc_envs = {
|
||||
.data = (uint8_t *)envs?envs:"",
|
||||
.len = MIN(envs_len, 64),
|
||||
};
|
||||
rpc_int_t rpc_envs_len = {
|
||||
.data = envs_len,
|
||||
};
|
||||
msg_tag_t tag = pm_t_run_app_call(u_get_global_env()->ns_hd, &rpc_path, &rpc_mem_block,
|
||||
&rpc_params, &rpc_params_len, &rpc_envs, &rpc_envs_len);
|
||||
|
||||
return msg_tag_get_val(tag);
|
||||
}
|
||||
|
||||
@@ -22,24 +22,29 @@
|
||||
* @brief 运行app
|
||||
* run_app
|
||||
*/
|
||||
RPC_GENERATION_OP4(pm_t, PM_PROT, PM_RUN_APP, run_app,
|
||||
rpc_ref_file_array_t, rpc_file_array_t, RPC_DIR_IN, RPC_TYPE_DATA, path,
|
||||
rpc_int_t, rpc_int_t, RPC_DIR_IN, RPC_TYPE_DATA, flags,
|
||||
rpc_ref_file_array_t, rpc_file_array_t, RPC_DIR_IN, RPC_TYPE_DATA, params,
|
||||
rpc_int_t, rpc_int_t, RPC_DIR_IN, RPC_TYPE_DATA, params_len)
|
||||
RPC_GENERATION_OP6(pm_t, PM_PROT, PM_RUN_APP, run_app,
|
||||
rpc_ref_array_uint32_t_uint8_t_64_t, rpc_array_uint32_t_uint8_t_64_t, RPC_DIR_IN, RPC_TYPE_DATA, path,
|
||||
rpc_int_t, rpc_int_t, RPC_DIR_IN, RPC_TYPE_DATA, mem_block,
|
||||
rpc_ref_array_uint32_t_uint8_t_96_t, rpc_array_uint32_t_uint8_t_96_t, RPC_DIR_IN, RPC_TYPE_DATA, params,
|
||||
rpc_int_t, rpc_int_t, RPC_DIR_IN, RPC_TYPE_DATA, params_len,
|
||||
rpc_ref_array_uint32_t_uint8_t_64_t, rpc_array_uint32_t_uint8_t_64_t, RPC_DIR_IN, RPC_TYPE_DATA, envs,
|
||||
rpc_int_t, rpc_int_t, RPC_DIR_IN, RPC_TYPE_DATA, envs_len
|
||||
)
|
||||
{
|
||||
int16_t ret = -1;
|
||||
|
||||
path->data[path->len - 1] = 0;
|
||||
ret = pm_rpc_run_app(path->data, flags->data, params->data, params_len->data);
|
||||
ret = pm_rpc_run_app(path->data, mem_block->data, params->data, params_len->data, envs->data, envs_len->data);
|
||||
return ret;
|
||||
}
|
||||
|
||||
RPC_GENERATION_DISPATCH4(pm_t, PM_PROT, PM_RUN_APP, run_app,
|
||||
rpc_ref_file_array_t, rpc_file_array_t, RPC_DIR_IN, RPC_TYPE_DATA, path,
|
||||
rpc_int_t, rpc_int_t, RPC_DIR_IN, RPC_TYPE_DATA, flags,
|
||||
rpc_ref_file_array_t, rpc_file_array_t, RPC_DIR_IN, RPC_TYPE_DATA, params,
|
||||
rpc_int_t, rpc_int_t, RPC_DIR_IN, RPC_TYPE_DATA, params_len)
|
||||
RPC_GENERATION_DISPATCH6(pm_t, PM_PROT, PM_RUN_APP, run_app,
|
||||
rpc_ref_array_uint32_t_uint8_t_64_t, rpc_array_uint32_t_uint8_t_64_t, RPC_DIR_IN, RPC_TYPE_DATA, path,
|
||||
rpc_int_t, rpc_int_t, RPC_DIR_IN, RPC_TYPE_DATA, mem_block,
|
||||
rpc_ref_array_uint32_t_uint8_t_96_t, rpc_array_uint32_t_uint8_t_96_t, RPC_DIR_IN, RPC_TYPE_DATA, params,
|
||||
rpc_int_t, rpc_int_t, RPC_DIR_IN, RPC_TYPE_DATA, params_len,
|
||||
rpc_ref_array_uint32_t_uint8_t_64_t, rpc_array_uint32_t_uint8_t_64_t, RPC_DIR_IN, RPC_TYPE_DATA, envs,
|
||||
rpc_int_t, rpc_int_t, RPC_DIR_IN, RPC_TYPE_DATA, envs_len)
|
||||
|
||||
/*kill_task*/
|
||||
RPC_GENERATION_OP3(pm_t, PM_PROT, PM_KILL_TASK, kill_task,
|
||||
|
||||
@@ -6,5 +6,4 @@
|
||||
int app_load(const char *name, uenv_t *cur_env, pid_t *pid,
|
||||
char *argv[], int arg_cn,
|
||||
char *envp[], int envp_cn,
|
||||
obj_handler_t *p_sem_hd,
|
||||
int mem_block);
|
||||
@@ -120,12 +120,10 @@ static void *app_stack_push_array(obj_handler_t task_obj, umword_t **stack, uint
|
||||
int app_load(const char *name, uenv_t *cur_env, pid_t *pid,
|
||||
char *argv[], int arg_cn,
|
||||
char *envp[], int envp_cn,
|
||||
obj_handler_t *p_sem_hd,
|
||||
int mem_block)
|
||||
{
|
||||
msg_tag_t tag;
|
||||
sys_info_t sys_info;
|
||||
assert(p_sem_hd);
|
||||
|
||||
tag = sys_read_info(SYS_PROT, &sys_info, 0);
|
||||
if (msg_tag_get_val(tag) < 0)
|
||||
@@ -187,7 +185,6 @@ int app_load(const char *name, uenv_t *cur_env, pid_t *pid,
|
||||
umword_t ram_base;
|
||||
obj_handler_t hd_task = handler_alloc();
|
||||
obj_handler_t hd_thread = handler_alloc();
|
||||
obj_handler_t hd_sem = handler_alloc();
|
||||
|
||||
if (hd_task == HANDLER_INVALID)
|
||||
{
|
||||
@@ -197,10 +194,6 @@ int app_load(const char *name, uenv_t *cur_env, pid_t *pid,
|
||||
{
|
||||
goto end;
|
||||
}
|
||||
if (hd_sem == HANDLER_INVALID)
|
||||
{
|
||||
goto end;
|
||||
}
|
||||
tag = factory_create_task(FACTORY_PROT, vpage_create_raw3(KOBJ_ALL_RIGHTS, 0, hd_task));
|
||||
if (msg_tag_get_prot(tag) < 0)
|
||||
{
|
||||
@@ -211,11 +204,6 @@ int app_load(const char *name, uenv_t *cur_env, pid_t *pid,
|
||||
{
|
||||
goto end_del_obj;
|
||||
}
|
||||
tag = facotry_create_sema(FACTORY_PROT, vpage_create_raw3(KOBJ_ALL_RIGHTS, 0, hd_sem), 0, (uint32_t)(-1));
|
||||
if (msg_tag_get_prot(tag) < 0)
|
||||
{
|
||||
goto end_del_obj;
|
||||
}
|
||||
tag = task_alloc_ram_base(hd_task, app ? app->i.ram_size : 100 * 1024 /*TODO:*/,
|
||||
&ram_base, mem_block);
|
||||
if (msg_tag_get_prot(tag) < 0)
|
||||
@@ -227,11 +215,6 @@ int app_load(const char *name, uenv_t *cur_env, pid_t *pid,
|
||||
{
|
||||
goto end_del_obj;
|
||||
}
|
||||
tag = task_map(hd_task, hd_sem, SEMA_PROT, 0);
|
||||
if (msg_tag_get_prot(tag) < 0)
|
||||
{
|
||||
goto end_del_obj;
|
||||
}
|
||||
tag = task_map(hd_task, cur_env->ns_hd, LOG_PROT, KOBJ_DELETE_RIGHT);
|
||||
if (msg_tag_get_prot(tag) < 0)
|
||||
{
|
||||
@@ -368,7 +351,6 @@ int app_load(const char *name, uenv_t *cur_env, pid_t *pid,
|
||||
ram_base, 0);
|
||||
assert(msg_tag_get_prot(tag) >= 0);
|
||||
|
||||
*p_sem_hd = hd_sem;
|
||||
/*启动线程运行*/
|
||||
tag = thread_run(hd_thread, 3);
|
||||
assert(msg_tag_get_prot(tag) >= 0);
|
||||
@@ -384,10 +366,6 @@ end_del_obj:
|
||||
{
|
||||
task_unmap(TASK_THIS, vpage_create_raw3(KOBJ_DELETE_RIGHT, 0, hd_task));
|
||||
}
|
||||
if (hd_sem != HANDLER_INVALID)
|
||||
{
|
||||
task_unmap(TASK_THIS, vpage_create_raw3(KOBJ_DELETE_RIGHT, 0, hd_sem));
|
||||
}
|
||||
end:
|
||||
if (hd_task != HANDLER_INVALID)
|
||||
{
|
||||
@@ -397,9 +375,5 @@ end:
|
||||
{
|
||||
handler_free(hd_thread);
|
||||
}
|
||||
if (hd_sem != HANDLER_INVALID)
|
||||
{
|
||||
handler_free(hd_sem);
|
||||
}
|
||||
return -ENOMEM;
|
||||
}
|
||||
|
||||
@@ -16,6 +16,8 @@
|
||||
#include <string.h>
|
||||
#include <u_malloc.h>
|
||||
#include "fd.h"
|
||||
#include "u_path.h"
|
||||
|
||||
static fs_t fs;
|
||||
static int fs_sig_call_back(pid_t pid, umword_t sig_val);
|
||||
|
||||
@@ -378,7 +380,6 @@ int fs_svr_rename(char *oldname, char *newname)
|
||||
{
|
||||
return fatfs_err_conv(f_rename(oldname, newname));
|
||||
}
|
||||
#include "u_path.h"
|
||||
int fs_svr_stat(const char *path, void *_buf)
|
||||
{
|
||||
FILINFO INFO;
|
||||
|
||||
@@ -1,172 +0,0 @@
|
||||
|
||||
// #include <u_types.h>
|
||||
// #include <u_queue.h>
|
||||
// #include <u_util.h>
|
||||
// #include <u_thread.h>
|
||||
// #include <u_log.h>
|
||||
// #include <u_hd_man.h>
|
||||
// #include <u_err.h>
|
||||
// #include <u_sema.h>
|
||||
// #include <assert.h>
|
||||
// #include <errno.h>
|
||||
// #include <u_sleep.h>
|
||||
// #include <pthread.h>
|
||||
// #include <rpc_prot.h>
|
||||
// #include "cons_svr.h"
|
||||
// #include <stdio.h>
|
||||
// #include <u_thread_util.h>
|
||||
// #include <u_thread.h>
|
||||
// #include <string.h>
|
||||
// #include "u_hd_man.h"
|
||||
// #include "u_mutex.h"
|
||||
// #include "u_factory.h"
|
||||
// #include "u_task.h"
|
||||
|
||||
// #if IS_ENABLED(CONFIG_MMU)
|
||||
// #define CONS_STACK_SIZE 1024
|
||||
// #else
|
||||
// #define CONS_STACK_SIZE 512
|
||||
// #endif
|
||||
// static ATTR_ALIGN(8) uint8_t cons_stack[CONS_STACK_SIZE];
|
||||
|
||||
// static cons_t cons_obj;
|
||||
// static obj_handler_t cons_th;
|
||||
// static obj_handler_t sem_th;
|
||||
// static u_mutex_t lock_cons;
|
||||
|
||||
// static void cons_read_lock(void)
|
||||
// {
|
||||
// u_mutex_lock(&lock_cons, 0, 0);
|
||||
// }
|
||||
// static void cons_read_unlock(void)
|
||||
// {
|
||||
// u_mutex_unlock(&lock_cons);
|
||||
// }
|
||||
|
||||
// static void console_read_func(void)
|
||||
// {
|
||||
// while (1)
|
||||
// {
|
||||
// int r_len = ulog_read_bytes(LOG_PROT, cons_obj.r_data_buf, sizeof(cons_obj.r_data_buf));
|
||||
|
||||
// if (r_len > 0)
|
||||
// {
|
||||
// cons_read_lock();
|
||||
// for (int i = 0; i < r_len; i++)
|
||||
// {
|
||||
// q_enqueue(&cons_obj.r_queue, cons_obj.r_data_buf[i]);
|
||||
// }
|
||||
// cons_read_unlock();
|
||||
// if (sem_th)
|
||||
// {
|
||||
// u_sema_up(sem_th);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// handler_free_umap(cons_obj.hd_cons_read);
|
||||
// while (1)
|
||||
// {
|
||||
// u_sleep_ms(1000);
|
||||
// }
|
||||
// }
|
||||
// int console_init(void)
|
||||
// {
|
||||
// msg_tag_t tag;
|
||||
|
||||
// cons_svr_obj_init(&cons_obj);
|
||||
// meta_reg_svr_obj(&cons_obj.svr, CONS_PROT);
|
||||
// u_mutex_init(&lock_cons, handler_alloc());
|
||||
|
||||
// sem_th = handler_alloc();
|
||||
// if (sem_th == HANDLER_INVALID)
|
||||
// {
|
||||
// return -1;
|
||||
// }
|
||||
// tag = facotry_create_sema(FACTORY_PROT, vpage_create_raw3(KOBJ_ALL_RIGHTS, 0, sem_th), 0, INT32_MAX);
|
||||
// if (msg_tag_get_val(tag) < 0)
|
||||
// {
|
||||
// return msg_tag_get_val(tag);
|
||||
// }
|
||||
// // u_thread_create(&cons_th, (char *)cons_stack + sizeof(cons_stack) - 8, NULL, console_read_func);
|
||||
// // u_thread_run(cons_th, 3);
|
||||
// ulog_write_str(LOG_PROT, "cons svr init...\n");
|
||||
// return 0;
|
||||
// }
|
||||
// /**
|
||||
// * @brief 向控制台写入数据
|
||||
// *
|
||||
// * @param data
|
||||
// * @param len
|
||||
// */
|
||||
// int console_write(uint8_t *data, size_t len)
|
||||
// {
|
||||
// u_mutex_lock(&lock_cons, 0, 0);
|
||||
// ulog_write_bytes(LOG_PROT, data, len);
|
||||
// u_mutex_unlock(&lock_cons);
|
||||
// return len;
|
||||
// }
|
||||
// /**
|
||||
// * @brief 向控制台读取数据
|
||||
// *
|
||||
// * @param data
|
||||
// * @param len 如果len为0,则返回数据长度。
|
||||
// * @return int
|
||||
// */
|
||||
// int console_read(uint8_t *data, size_t len)
|
||||
// {
|
||||
// int r_len = 0;
|
||||
// pid_t src_pid = thread_get_src_pid();
|
||||
|
||||
// if (src_pid != cons_obj.active_pid)
|
||||
// {
|
||||
// return -EACCES;
|
||||
// }
|
||||
// if (len == 0)
|
||||
// {
|
||||
// return q_queue_len(&cons_obj.r_queue);
|
||||
// }
|
||||
// if (q_queue_len(&cons_obj.r_queue) == 0)
|
||||
// {
|
||||
// // 回复没有消息
|
||||
// return 0;
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// u_mutex_lock(&lock_cons, 0, 0);
|
||||
// if (q_queue_len(&cons_obj.r_queue) == 0)
|
||||
// {
|
||||
// // 回复没有消息
|
||||
// u_mutex_unlock(&lock_cons);
|
||||
// return 0;
|
||||
// }
|
||||
// int i;
|
||||
// for (i = 0; i < q_queue_len(&cons_obj.r_queue) && i < len; i++)
|
||||
// {
|
||||
// uint8_t e;
|
||||
// if (q_dequeue(&cons_obj.r_queue, &e) < 0)
|
||||
// {
|
||||
// break;
|
||||
// }
|
||||
// data[i] = e;
|
||||
// }
|
||||
// r_len = i;
|
||||
// u_mutex_unlock(&lock_cons);
|
||||
// }
|
||||
// return r_len;
|
||||
// }
|
||||
// /**
|
||||
// * @brief 激活控制台为发送者进程
|
||||
// *
|
||||
// */
|
||||
// void console_active(mk_pid_t pid, obj_handler_t sem)
|
||||
// {
|
||||
// cons_obj.active_pid = pid;
|
||||
// if (sem)
|
||||
// {
|
||||
// if (sem_th)
|
||||
// {
|
||||
// handler_free_umap(sem_th);
|
||||
// }
|
||||
// sem_th = sem;
|
||||
// }
|
||||
// }
|
||||
@@ -1,6 +0,0 @@
|
||||
#pragma once
|
||||
#include <u_types.h>
|
||||
void console_init(void);
|
||||
void console_active(mk_pid_t pid, obj_handler_t sem);
|
||||
int console_write(uint8_t *data, size_t len);
|
||||
int console_read(uint8_t *data, size_t len);
|
||||
@@ -8,7 +8,6 @@
|
||||
* @copyright Copyright (c) 2023
|
||||
*
|
||||
*/
|
||||
#include "cons.h"
|
||||
#include "ns.h"
|
||||
#include "ns_svr.h"
|
||||
#include "parse_cfg.h"
|
||||
@@ -35,7 +34,7 @@
|
||||
#define DEFAULT_INIT_CFG "init.cfg"
|
||||
|
||||
#define STACK_NUM 4
|
||||
#define STACK_COM_ITME_SIZE ((1024+512) * STACK_NUM)
|
||||
#define STACK_COM_ITME_SIZE ((2048) * STACK_NUM)
|
||||
ATTR_ALIGN(8)
|
||||
static uint8_t stack_coms[STACK_COM_ITME_SIZE * STACK_NUM];
|
||||
static uint8_t msg_buf_coms[MSG_BUG_LEN * STACK_NUM];
|
||||
|
||||
@@ -168,7 +168,6 @@ int parse_cfg(const char *parse_cfg_file_name, uenv_t *env)
|
||||
char *args[CMD_PARAMS_CN] = {
|
||||
NULL,
|
||||
};
|
||||
obj_handler_t hd_sem;
|
||||
int mem_block;
|
||||
|
||||
mem_block = parse_cfg_cmd_line();
|
||||
@@ -179,14 +178,13 @@ int parse_cfg(const char *parse_cfg_file_name, uenv_t *env)
|
||||
}
|
||||
printf("parse_cfg cmd_params_num:%d\n", cmd_params_num);
|
||||
int ret = app_load(cmd_line, env, &pid, args, cmd_params_num,
|
||||
NULL, 0, &hd_sem, mem_block);
|
||||
NULL, 0, mem_block);
|
||||
if (ret < 0)
|
||||
{
|
||||
printf("%s load fail, 0x%x\n", cmd_line, ret);
|
||||
}
|
||||
else
|
||||
{
|
||||
// console_active(pid, hd_sem);
|
||||
tty_set_fg_pid(pid);
|
||||
run_cn++;
|
||||
}
|
||||
|
||||
@@ -221,19 +221,23 @@ int pm_rpc_kill_task(int src_pid, int pid, int flags, int exit_code)
|
||||
* @param flags
|
||||
* @return int
|
||||
*/
|
||||
int pm_rpc_run_app(const char *path, int flags, char *params, int params_len)
|
||||
int pm_rpc_run_app(const char *path, int mem_block, char *params, int params_len, char *envs_in, int envs_in_len)
|
||||
{
|
||||
pid_t pid;
|
||||
int ret;
|
||||
obj_handler_t sem;
|
||||
int i;
|
||||
int j = 0;
|
||||
int args_len = 0;
|
||||
int evns_len = 0;
|
||||
printf("pm run %s.\n", path);
|
||||
char *args[CMD_PARAMS_CN] = {
|
||||
(char *)path,
|
||||
};
|
||||
char *envs[8/*FIXME:*/] = {
|
||||
};
|
||||
|
||||
for (i = 1; *params && i < CMD_PARAMS_CN; i++)
|
||||
|
||||
for (i = 1, j = 0; *params && i < CMD_PARAMS_CN; i++)
|
||||
{
|
||||
if (j >= params_len)
|
||||
{
|
||||
@@ -244,17 +248,25 @@ int pm_rpc_run_app(const char *path, int flags, char *params, int params_len)
|
||||
j += strlen(params) + 1;
|
||||
params += strlen(params) + 1;
|
||||
}
|
||||
args_len = i;
|
||||
|
||||
ret = app_load(path, u_get_global_env(), &pid, args, i,
|
||||
NULL, 0, &sem, 0);
|
||||
for (i = 0, j = 0; *envs_in && i < CMD_PARAMS_CN; i++)
|
||||
{
|
||||
if (j >= envs_in_len)
|
||||
{
|
||||
break;
|
||||
}
|
||||
envs[i] = envs_in;
|
||||
printf("envs[%d]: %s\n", i, envs_in);
|
||||
j += strlen(envs_in) + 1;
|
||||
envs_in += strlen(envs_in) + 1;
|
||||
}
|
||||
evns_len = i;
|
||||
|
||||
ret = app_load(path, u_get_global_env(), &pid, args, args_len,
|
||||
envs, evns_len, mem_block);
|
||||
if (ret >= 0)
|
||||
{
|
||||
#if 0
|
||||
if (!(flags & PM_APP_BG_RUN))
|
||||
{
|
||||
tty_set_fg_pid(pid);
|
||||
}
|
||||
#endif
|
||||
return pid;
|
||||
}
|
||||
return ret;
|
||||
|
||||
@@ -8,7 +8,6 @@
|
||||
#include <ctype.h>
|
||||
#include <stdio.h>
|
||||
#include <fcntl.h>
|
||||
#include "cons.h"
|
||||
#include "u_hd_man.h"
|
||||
#include "u_prot.h"
|
||||
#include "u_task.h"
|
||||
@@ -35,7 +34,6 @@ static tty_struct_t sys_tty;
|
||||
|
||||
static obj_handler_t cons_th;
|
||||
static obj_handler_t sem_th;
|
||||
static u_mutex_t lock_cons;
|
||||
static meta_t tty_meta;
|
||||
static obj_handler_t tty_ipc_hd;
|
||||
static fs_t tty_fs;
|
||||
@@ -57,11 +55,11 @@ void tty_struct_init(tty_struct_t *tty)
|
||||
|
||||
static inline void cons_read_lock(void)
|
||||
{
|
||||
u_mutex_lock(&lock_cons, 0, 0);
|
||||
u_mutex_lock(&sys_tty.lock_cons, 0, 0);
|
||||
}
|
||||
static inline void cons_read_unlock(void)
|
||||
{
|
||||
u_mutex_unlock(&lock_cons);
|
||||
u_mutex_unlock(&sys_tty.lock_cons);
|
||||
}
|
||||
|
||||
static void console_read_func(void)
|
||||
@@ -127,7 +125,8 @@ static int cons_init(void)
|
||||
{
|
||||
msg_tag_t tag;
|
||||
|
||||
u_mutex_init(&lock_cons, handler_alloc());
|
||||
u_mutex_init(&sys_tty.lock_cons, handler_alloc());
|
||||
u_mutex_init(&sys_tty.lock_write_cons, handler_alloc());
|
||||
|
||||
sem_th = handler_alloc();
|
||||
if (sem_th == HANDLER_INVALID)
|
||||
@@ -508,6 +507,12 @@ again:
|
||||
tty_write_hw(&sys_tty);
|
||||
return i;
|
||||
}
|
||||
void tty_write_data(void *buf, size_t len)
|
||||
{
|
||||
u_mutex_lock(&sys_tty.lock_write_cons, 0, 0);
|
||||
ulog_write_bytes(LOG_PROT, buf, len);
|
||||
u_mutex_unlock(&sys_tty.lock_write_cons);
|
||||
}
|
||||
static int tty_write(int fd, void *buf, size_t len)
|
||||
{
|
||||
int i;
|
||||
@@ -523,7 +528,7 @@ static int tty_write(int fd, void *buf, size_t len)
|
||||
// 调用写函数
|
||||
ret = tty_write_hw(&sys_tty);
|
||||
#else
|
||||
ulog_write_bytes(LOG_PROT, buf, len);
|
||||
tty_write_data(buf, len);
|
||||
#endif
|
||||
return len;
|
||||
}
|
||||
|
||||
@@ -3,13 +3,14 @@
|
||||
#include <termios.h>
|
||||
#include "u_queue.h"
|
||||
#include "u_types.h"
|
||||
#include <u_mutex.h>
|
||||
#define TTY_QUEUE_DATA_SIZE 257
|
||||
typedef struct tty_struct
|
||||
{
|
||||
struct termios termios; //!< 当前使用的终端信息
|
||||
struct winsize w_size; //!< 窗口大小
|
||||
// queue_t r_queue; //!< 最底层的数据首先读取到这里
|
||||
// uint8_t r_queue_data[TTY_QUEUE_DATA_SIZE];
|
||||
u_mutex_t lock_cons;
|
||||
u_mutex_t lock_write_cons;
|
||||
queue_t w_queue; //!< 写数据的缓存
|
||||
uint8_t w_queue_data[TTY_QUEUE_DATA_SIZE];
|
||||
queue_t pre_queue; //!< 然后通过handler处理机制存放到per_queue中,pre_queue中的数据直接可以给用户,或者进行回显
|
||||
@@ -25,8 +26,8 @@ typedef struct tty_struct
|
||||
uint8_t is_nl;
|
||||
pid_t fg_pid;
|
||||
|
||||
//FIXME:
|
||||
int fd_flags;//!<暂时这样
|
||||
// FIXME:
|
||||
int fd_flags; //!< 暂时这样
|
||||
} tty_struct_t;
|
||||
|
||||
#define C_CC_INIT "\003\034\177\025\004\0\1\0\021\023\032\0\022\017\027\026\0"
|
||||
@@ -128,6 +129,5 @@ typedef struct tty_struct
|
||||
#define L_PENDIN(tty) _L_FLAG((tty), PENDIN)
|
||||
#define L_IEXTEN(tty) _L_FLAG((tty), IEXTEN)
|
||||
|
||||
|
||||
void tty_svr_init(void);
|
||||
void tty_set_fg_pid(pid_t pid);
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
#include <u_sys.h>
|
||||
#include <rpc_prot.h>
|
||||
static fs_t fs;
|
||||
|
||||
extern void fd_man_unreg(int fd);
|
||||
int fs_svr_open(const char *path, int flags, int mode)
|
||||
{
|
||||
return -ENOSYS;
|
||||
@@ -22,6 +22,7 @@ int fs_svr_write(int fd, void *buf, size_t len)
|
||||
void fs_svr_close(int fd)
|
||||
{
|
||||
lwip_close(fd);
|
||||
fd_man_unreg(fd);
|
||||
}
|
||||
int fs_svr_lseek(int fd, int offs, int whence)
|
||||
{
|
||||
|
||||
@@ -24,7 +24,7 @@ static umword_t size;
|
||||
obj_handler_t net_drv_hd = HANDLER_INVALID;
|
||||
|
||||
#define STACK_COM_ITME_SIZE (2 * 1024 /*sizeof(struct pthread) + TP_OFFSET*/)
|
||||
#define STACK_NUM 3
|
||||
#define STACK_NUM 4
|
||||
ATTR_ALIGN(8)
|
||||
static uint8_t stack_coms[STACK_COM_ITME_SIZE * STACK_NUM];
|
||||
static uint8_t msg_buf_coms[MSG_BUG_LEN * STACK_NUM];
|
||||
|
||||
@@ -3,9 +3,91 @@
|
||||
#include "lwip/sockets.h"
|
||||
#include "net_svr.h"
|
||||
#include "rpc_prot.h"
|
||||
#include "u_sig.h"
|
||||
#include "lwipopts.h"
|
||||
#include "u_mutex.h"
|
||||
#include <u_hd_man.h>
|
||||
|
||||
typedef struct fd_man
|
||||
{
|
||||
pid_t pid;
|
||||
int fd;
|
||||
uint8_t used;
|
||||
} fd_man_t;
|
||||
|
||||
static fd_man_t fd_man_list[MEMP_NUM_UDP_PCB + MEMP_NUM_TCP_PCB + MEMP_NUM_TCP_PCB_LISTEN];
|
||||
static u_mutex_t fd_man_lock;
|
||||
|
||||
void fd_man_reg(pid_t pid, int fd)
|
||||
{
|
||||
u_mutex_lock(&fd_man_lock, 0, NULL);
|
||||
for (int i = 0; i < ARRAY_SIZE(fd_man_list); i++)
|
||||
{
|
||||
if (fd_man_list[i].used == 0)
|
||||
{
|
||||
fd_man_list[i].pid = pid;
|
||||
fd_man_list[i].fd = fd;
|
||||
fd_man_list[i].used = 1;
|
||||
int w_ret = pm_sig_watch(pid, 0 /*TODO:现在只有kill */);
|
||||
if (w_ret < 0)
|
||||
{
|
||||
printf("net wath pid %d err.\n", w_ret);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
u_mutex_unlock(&fd_man_lock);
|
||||
}
|
||||
void fd_man_unreg(int fd)
|
||||
{
|
||||
u_mutex_lock(&fd_man_lock, 0, NULL);
|
||||
for (int i = 0; i < ARRAY_SIZE(fd_man_list); i++)
|
||||
{
|
||||
if (fd_man_list[i].used == 1 && fd_man_list[i].fd == fd)
|
||||
{
|
||||
fd_man_list[i].used = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
u_mutex_unlock(&fd_man_lock);
|
||||
}
|
||||
void fd_man_close_without_pid(pid_t pid)
|
||||
{
|
||||
u_mutex_lock(&fd_man_lock, 0, NULL);
|
||||
for (int i = 0; i < ARRAY_SIZE(fd_man_list); i++)
|
||||
{
|
||||
if (fd_man_list[i].used == 1 && fd_man_list[i].pid == pid)
|
||||
{
|
||||
printf("net close fd:%d\n", fd_man_list[i].fd);
|
||||
lwip_close(fd_man_list[i].fd);
|
||||
fd_man_list[i].used = 0;
|
||||
}
|
||||
}
|
||||
u_mutex_unlock(&fd_man_lock);
|
||||
printf("net close--\n");
|
||||
}
|
||||
static int fs_sig_call_back(pid_t pid, umword_t sig_val)
|
||||
{
|
||||
switch (sig_val)
|
||||
{
|
||||
case KILL_SIG:
|
||||
fd_man_close_without_pid(pid);
|
||||
break;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int net_svr_accept(int s, struct sockaddr *addr, socklen_t *addrlen)
|
||||
{
|
||||
return lwip_accept(s, addr, addrlen);
|
||||
int fd;
|
||||
pid_t pid = thread_get_src_pid();
|
||||
|
||||
fd = lwip_accept(s, addr, addrlen);
|
||||
if (fd >= 0)
|
||||
{
|
||||
fd_man_reg(pid, fd);
|
||||
}
|
||||
return fd;
|
||||
}
|
||||
static int net_svr_bind(int s, const struct sockaddr *name, socklen_t namelen)
|
||||
{
|
||||
@@ -13,7 +95,11 @@ static int net_svr_bind(int s, const struct sockaddr *name, socklen_t namelen)
|
||||
}
|
||||
static int net_svr_shutdown(int s, int how)
|
||||
{
|
||||
return lwip_shutdown(s, how);
|
||||
int ret;
|
||||
|
||||
ret = lwip_shutdown(s, how);
|
||||
fd_man_unreg(s);
|
||||
return ret;
|
||||
}
|
||||
static int net_svr_getpeername(int s, struct sockaddr *name, socklen_t *namelen)
|
||||
{
|
||||
@@ -75,7 +161,15 @@ static ssize_t net_svr_sendto(int s, const void *dataptr, size_t size, int flags
|
||||
}
|
||||
static int net_svr_socket(int domain, int type, int protocol)
|
||||
{
|
||||
return lwip_socket(domain, type, protocol);
|
||||
int fd;
|
||||
pid_t pid = thread_get_src_pid();
|
||||
|
||||
fd = lwip_socket(domain, type, protocol);
|
||||
if (fd >= 0)
|
||||
{
|
||||
fd_man_reg(pid, fd);
|
||||
}
|
||||
return fd;
|
||||
}
|
||||
static net_operations_t net_op = {
|
||||
.accept = net_svr_accept,
|
||||
@@ -100,4 +194,8 @@ void net_svr_init(void)
|
||||
|
||||
net_init(&net, &net_op);
|
||||
meta_reg_svr_obj(&net.svr, NET_PROT);
|
||||
|
||||
u_mutex_init(&fd_man_lock, handler_alloc());
|
||||
|
||||
pm_sig_func_set(fs_sig_call_back);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user