🆕 exec (存在bug

This commit is contained in:
fslongjin
2022-05-31 21:55:06 +08:00
parent 844e66f6bb
commit e2a59dbd43
24 changed files with 494 additions and 90 deletions

View File

@ -13,31 +13,29 @@ CFLAGS := $(GLOBAL_CFLAGS) -I $(shell pwd)/libs
current_CFLAGS := $(CFLAGS)
all:
$(shell if [ ! -e $(tmp_output_dir) ];then mkdir -p $(tmp_output_dir); fi)
$(shell if [ ! -e $(output_dir) ];then mkdir -p $(output_dir); fi)
@list='$(user_sub_dirs)'; for subdir in $$list; do \
echo "make all in $$subdir";\
cd $$subdir;\
$(MAKE) all CFLAGS="$(CFLAGS)" tmp_output_dir="$(tmp_output_dir)" output_dir="$(output_dir)" sys_libs_dir="$(shell pwd)/libs";\
cd ..;\
done
# 系统库
sys_api_lib:
@list='./libs'; for subdir in $$list; do \
echo "make all in $$subdir";\
cd $$subdir;\
$(MAKE) all CFLAGS="$(CFLAGS)";\
cd ..;\
done
$(shell if [ ! -e $(tmp_output_dir) ];then mkdir -p $(tmp_output_dir); fi)
$(shell if [ ! -e $(output_dir) ];then mkdir -p $(output_dir); fi)
# $(MAKE) sys_api_lib
$(MAKE) shell
# objcopy -I elf64-x86-64 -S -R ".eh_frame" -R ".comment" -O binary sys_api_lib $(ROOT_PATH)/bin/user/init.bin
#objcopy -I elf64-x86-64 -S -R ".eh_frame" -R ".comment" -O elf64-x86-64 sys_api_lib $(ROOT_PATH)/bin/user/init.bin
sys_api_lib:
ld -b elf64-x86-64 -z muldefs -o $(tmp_output_dir)/sys_api_lib $(shell find ./libs -name "*.o")
# ld -b elf64-x86-64 -z muldefs -o $(tmp_output_dir)/sys_api_lib $(shell find ./libs -name "*.o")
#ld -b elf64-x86-64 -z muldefs -o sys_api_lib init.o $(shell find . -name "*.o") -T init.lds
shell:
ld -b elf64-x86-64 -z muldefs -o $(tmp_output_dir)/shell $(shell find ./apps/shell -name "*.o") $(shell find ./libs -name "*.o") -T ./apps/shell/shell.lds
objcopy -I elf64-x86-64 -S -R ".eh_frame" -R ".comment" -O elf64-x86-64 $(tmp_output_dir)/shell $(output_dir)/shell.elf
clean:
rm -rf $(GARBAGE)

7
user/apps/about/Makefile Normal file
View File

@ -0,0 +1,7 @@
all: about.o
ld -b elf64-x86-64 -z muldefs -o $(tmp_output_dir)/about $(shell find . -name "*.o") $(shell find $(sys_libs_dir) -name "*.o") -T shell.lds
objcopy -I elf64-x86-64 -S -R ".eh_frame" -R ".comment" -O elf64-x86-64 $(tmp_output_dir)/about $(output_dir)/about.elf
about.o: about.c
gcc $(CFLAGS) -c about.c -o about.o

7
user/apps/about/about.c Normal file
View File

@ -0,0 +1,7 @@
#include <libc/stdio.h>
int main()
{
printf("Hello World!\n");
while(1);
}

View File

@ -1,5 +1,8 @@
all: shell.o cmd.o cmd_help.o
ld -b elf64-x86-64 -z muldefs -o $(tmp_output_dir)/shell $(shell find . -name "*.o") $(shell find $(sys_libs_dir) -name "*.o") -T shell.lds
objcopy -I elf64-x86-64 -S -R ".eh_frame" -R ".comment" -O elf64-x86-64 $(tmp_output_dir)/shell $(output_dir)/shell.elf
shell.o: shell.c
gcc $(CFLAGS) -c shell.c -o shell.o

View File

@ -9,6 +9,7 @@
#include <libc/stdlib.h>
#include <libc/fcntl.h>
#include <libc/dirent.h>
#include <libc/sys/wait.h>
#include "cmd_help.h"
@ -36,6 +37,36 @@ struct built_in_cmd_t shell_cmds[] =
// 总共的内建命令数量
const static int total_built_in_cmd_num = sizeof(shell_cmds) / sizeof(struct built_in_cmd_t);
/**
* @brief 将cwd与文件名进行拼接得到最终的文件绝对路径
*
* @param filename 文件名
* @param result_path_len 结果字符串的大小
* @return char* 结果字符串
*/
static char *get_target_filepath(const char *filename, int *result_path_len)
{
int cwd_len = strlen(shell_current_path);
// 计算文件完整路径的长度
*result_path_len = cwd_len + strlen(filename);
char *file_path = (char *)malloc(*result_path_len + 2);
memset(file_path, 0, *result_path_len + 2);
strcpy(file_path, shell_current_path);
// 在文件路径中加入斜杠
if (cwd_len > 1)
file_path[cwd_len] = '/';
// 拼接完整路径
strcat(file_path, filename);
return file_path;
}
/**
* @brief 寻找对应的主命令编号
*
@ -230,7 +261,7 @@ int shell_cmd_ls(int argc, char **argv)
printf("\n");
closedir(dir);
if (argc > 1)
if (argv != NULL)
free(argv);
return 0;
@ -247,7 +278,7 @@ int shell_cmd_pwd(int argc, char **argv)
{
if (shell_current_path)
printf("%s\n", shell_current_path);
if (argc > 1)
if (argv != NULL)
free(argv);
}
@ -260,23 +291,8 @@ int shell_cmd_pwd(int argc, char **argv)
*/
int shell_cmd_cat(int argc, char **argv)
{
int cwd_len = strlen(shell_current_path);
// 计算文件完整路径的长度
int file_path_len = cwd_len + strlen(argv[1]);
char *file_path = (char *)malloc(file_path_len + 2);
memset(file_path, 0, file_path_len + 2);
strcpy(file_path, shell_current_path);
// 在文件路径中加入斜杠
if (cwd_len > 1)
file_path[cwd_len] = '/';
// 拼接完整路径
strcat(file_path, argv[1]);
int path_len = 0;
char *file_path = get_target_filepath(argv[1], &path_len);
// 打开文件
int fd = open(file_path, 0);
@ -298,6 +314,8 @@ int shell_cmd_cat(int argc, char **argv)
close(fd);
free(buf);
if (argv != NULL)
free(argv);
}
/**
@ -347,9 +365,36 @@ int shell_cmd_rmdir(int argc, char **argv) {}
* @param argv
* @return int
*/
int shell_cmd_exec(int argc, char **argv)
{
pid_t pid = fork();
int retval = 0;
printf(" pid=%d \n",pid);
// todo:
int shell_cmd_exec(int argc, char **argv) {}
while(1);
if (pid == 0)
{
printf("child proc\n");
// 子进程
int path_len = 0;
char *file_path = get_target_filepath(argv[1], &path_len);
printf("before execv, path=%s\n", file_path);
execv(file_path, argv);
free(argv);
while(1);
exit(0);
}
else
{
printf("parent process wait for pid:[ %d ]\n", pid);
while(1);
waitpid(pid, &retval, 0);
printf("parent process wait pid [ %d ], exit code=%d\n", pid, retval);
free(argv);
}
while(1);
}
/**
* @brief 重启命令

View File

@ -37,13 +37,22 @@ int parse_command(char *buf, int *argc, char ***argv);
*
* @param kb_fd 键盘文件描述符
*/
static void main_loop(int kb_fd)
void main_loop(int kb_fd)
{
unsigned char input_buffer[INPUT_BUFFER_SIZE] = {0};
sbrk(24);
pid_t pid = fork();
int retval = 0;
while (1)
printf(" @pid=%d ", pid);
// 初始化当前工作目录的路径
shell_current_path = (char *)malloc(3);
memset(shell_current_path, 0, 3);
memset(shell_current_path, 0, 3);
shell_current_path[0] = '/';
shell_current_path[1] = '\0';
@ -54,6 +63,7 @@ static void main_loop(int kb_fd)
char **argv;
printf("[DragonOS] %s # ", shell_current_path);
memset(input_buffer, 0, INPUT_BUFFER_SIZE);
// 循环读取每一行到buffer

View File

@ -2,7 +2,7 @@ all: libc
CFLAGS += -I .
libc_sub_dirs=math
libc_sub_dirs=math sys
libc: unistd.o fcntl.o malloc.o errno.o printf.o stdlib.o ctype.o string.o dirent.o

View File

@ -138,7 +138,7 @@ static int malloc_enlarge(int64_t size)
}
int64_t free_space = brk_max_addr - brk_managed_addr;
// printf("size=%ld\tfree_space=%ld\n", size, free_space);
if (free_space < size) // 现有堆空间不足
{
if (sbrk(size - free_space) != (void *)(-1))
@ -148,6 +148,8 @@ static int malloc_enlarge(int64_t size)
put_string("malloc_enlarge(): no_mem\n", COLOR_YELLOW, COLOR_BLACK);
return -ENOMEM;
}
// printf("brk max addr = %#018lx\n", brk_max_addr);
}
// 扩展管理的堆空间

View File

@ -1,6 +1,7 @@
#include <libc/unistd.h>
#include <libc/stdlib.h>
#include <libc/ctype.h>
#include <libsystem/syscall.h>
int abs(int i)
{
@ -43,4 +44,14 @@ int atoi(const char *str)
}
return neg ? n : -n;
}
/**
* @brief 退出进程
*
* @param status
*/
void exit(int status)
{
syscall_invoke(SYS_EXIT, status, 0, 0, 0, 0, 0, 0, 0);
}

View File

@ -32,4 +32,11 @@ long long llabs(long long i);
* @param str
* @return int
*/
int atoi(const char * str);
int atoi(const char * str);
/**
* @brief 退出进程
*
* @param status
*/
void exit(int status);

View File

@ -0,0 +1,8 @@
all: wait.o
CFLAGS += -I .
wait.o: wait.c
gcc $(CFLAGS) -c wait.c -o wait.o

26
user/libs/libc/sys/wait.c Normal file
View File

@ -0,0 +1,26 @@
#include "wait.h"
#include <libsystem/syscall.h>
/**
* @brief 等待所有子进程退出
*
* @param stat_loc 返回的子进程结束状态
* @return pid_t
*/
pid_t wait(int *stat_loc)
{
return waitpid((pid_t)(-1), stat_loc, 0);
}
/**
* @brief 等待指定pid的子进程退出
*
* @param pid 子进程的pid
* @param stat_loc 返回的子进程结束状态
* @param options 额外的控制选项
* @return pid_t
*/
pid_t waitpid(pid_t pid, int *stat_loc, int options)
{
return (pid_t)syscall_invoke(SYS_WAIT4, (uint64_t)pid, (uint64_t)stat_loc, options, 0, 0, 0, 0, 0);
}

21
user/libs/libc/sys/wait.h Normal file
View File

@ -0,0 +1,21 @@
#pragma once
#include "types.h"
/**
* @brief 等待所有子进程退出
*
* @param stat_loc 返回的子进程结束状态
* @return pid_t
*/
pid_t wait(int *stat_loc);
/**
* @brief 等待指定pid的子进程退出
*
* @param pid 子进程的pid
* @param stat_loc 返回的子进程结束状态
* @param options 额外的控制选项
* @return pid_t
*/
pid_t waitpid(pid_t pid, int *stat_loc, int options);

View File

@ -123,16 +123,26 @@ int64_t chdir(char *dest_path)
}
else
{
int retval = syscall_invoke(SYS_CHDIR, (uint64_t)dest_path, 0,0,0,0,0,0,0);
if(retval == 0)
{
errno = 0;
return 0;
}
else
{
errno = retval;
return -1;
}
return syscall_invoke(SYS_CHDIR, (uint64_t)dest_path, 0, 0, 0, 0, 0, 0, 0);
}
}
/**
* @brief 执行新的程序
*
* @param path 文件路径
* @param argv 参数列表
* @return int
*/
int execv(const char *path, char *const argv[])
{
if (path == NULL)
{
errno = -ENOENT;
return -1;
}
int retval = syscall_invoke(SYS_EXECVE, (uint64_t)path, (uint64_t)argv, 0, 0, 0, 0, 0, 0);
if(retval != 0)
return -1;
else return 0;
}

View File

@ -80,3 +80,12 @@ void *sbrk(int64_t increment);
* @return int64_t 成功0,失败:负值(错误码)
*/
int64_t chdir(char *dest_path);
/**
* @brief 执行新的程序
*
* @param path 文件路径
* @param argv 参数列表
* @return int
*/
int execv(const char* path, char * const argv[]);

View File

@ -14,9 +14,13 @@
#define SYS_VFORK 8
#define SYS_BRK 9
#define SYS_SBRK 10
#define SYS_REBOOT 11
#define SYS_REBOOT 11 // 重启
#define SYS_CHDIR 12 // 切换工作目录
#define SYS_GET_DENTS 13 // 获取目录中的数据
#define SYS_EXECVE 14 // 执行新的应用程序
#define SYS_WAIT4 15 // 等待进程退出
#define SYS_EXIT 16 // 进程退出
/**
* @brief 用户态系统调用函数