4
1
mirror of https://github.com/DragonOS-Community/DragonOS.git synced 2025-06-20 01:46:31 +00:00

🆕 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

@ -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

@ -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);
}
// 扩展管理的堆空间

@ -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);
}

@ -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);

@ -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

@ -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

@ -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);

@ -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;
}

@ -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[]);

@ -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 用户态系统调用函数