Patch add abort func (#120)

* 对于除了sigkill以外的信号,也加入队列

* bugfix:libc中,注册信号处理函数时,总是注册sigkill的问题

* 增加getpid系统调用

* 增加了raise、kill、abort
This commit is contained in:
login
2022-12-19 15:03:44 +08:00
committed by GitHub
parent 47f0d12a1f
commit c588d6f77f
13 changed files with 155 additions and 52 deletions

View File

@ -529,7 +529,7 @@ int shell_cmd_kill(int argc, char **argv)
retval = -EINVAL;
goto out;
}
retval = syscall_invoke(SYS_KILL, atoi(argv[1]), SIGKILL, 0, 0, 0, 0, 0, 0);
retval = kill(atoi(argv[1]), SIGKILL);
out:;
free(argv);
return retval;

View File

@ -40,7 +40,10 @@
#define SIGRTMIN 32
#define SIGRTMAX MAX_SIG_NUM
typedef void (*__sighandler_t) (int);
typedef void (*__sighandler_t)(int);
#define SIG_DFL ((__sighandler_t)0) /* Default action. */
#define SIG_IGN ((__sighandler_t)1) /* Ignore signal. */
// 注意该结构体最大16字节
union __sifields {
@ -86,4 +89,6 @@ struct sigaction
};
int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact);
int signal(int signum, __sighandler_t handler);
int signal(int signum, __sighandler_t handler);
int raise(int sig);
int kill(pid_t, int sig);

View File

@ -14,10 +14,10 @@ void __libc_sa_restorer()
/**
* @brief 设置信号处理动作(简单版本)
*
* @param signum
* @param handler
* @return int
*
* @param signum
* @param handler
* @return int
*/
int signal(int signum, __sighandler_t handler)
{
@ -27,12 +27,12 @@ int signal(int signum, __sighandler_t handler)
sa.sa_restorer = &__libc_sa_restorer;
// printf("handler address: %#018lx\n", handler);
// printf("restorer address: %#018lx\n", &__libc_sa_restorer);
sigaction(SIGKILL, &sa, NULL);
sigaction(signum, &sa, NULL);
}
/**
* @brief 设置信号处理动作
*
*
* @param signum 信号
* @param act 处理动作不可为NULL
* @param oldact 返回的旧的处理动作若为NULL则不返回
@ -41,4 +41,27 @@ int signal(int signum, __sighandler_t handler)
int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact)
{
return syscall_invoke(SYS_SIGACTION, (uint64_t)signum, (uint64_t)act, (uint64_t)oldact, 0, 0, 0, 0, 0);
}
/**
* @brief 向当前进程发送一个信号
*
* @param sig signal number
* @return int 错误码
*/
int raise(int sig)
{
return kill(getpid(), sig);
}
/**
* @brief
*
* @param pid 进程的标识符
* @param sig signal number
* @return int 错误码
*/
int kill(pid_t pid, int sig)
{
syscall_invoke(SYS_KILL, pid, sig, 0, 0, 0, 0, 0, 0);
}

View File

@ -1,7 +1,8 @@
#include <libc/src/unistd.h>
#include <libc/src/stdlib.h>
#include <libc/src/ctype.h>
#include <libc/src/stdlib.h>
#include <libc/src/unistd.h>
#include <libsystem/syscall.h>
#include <libc/src/include/signal.h>
int abs(int i)
{
@ -54,4 +55,15 @@ int atoi(const char *str)
void exit(int status)
{
syscall_invoke(SYS_EXIT, status, 0, 0, 0, 0, 0, 0, 0);
}
/**
* @brief 通过发送SIGABRT从而退出当前进程
*
*/
void abort()
{
// step1设置SIGABRT的处理函数为SIG_DFL
signal(SIGABRT, SIG_DFL);
raise(SIGABRT);
}

View File

@ -39,4 +39,10 @@ int atoi(const char * str);
*
* @param status
*/
void exit(int status);
void exit(int status);
/**
* @brief 通过发送SIGABRT从而退出当前进程
*
*/
void abort();

View File

@ -1,10 +1,10 @@
#include <libc/src/errno.h>
#include <libc/src/fcntl.h>
#include <libc/src/stddef.h>
#include <libc/src/stdio.h>
#include <libc/src/string.h>
#include <libc/src/unistd.h>
#include <libsystem/syscall.h>
#include <libc/src/errno.h>
#include <libc/src/stdio.h>
#include <libc/src/stddef.h>
#include <libc/src/string.h>
#include <libc/src/fcntl.h>
/**
* @brief 关闭文件接口
@ -163,11 +163,11 @@ int rmdir(const char *path)
/**
* @brief 删除文件
*
*
* @param path 绝对路径
* @return int
* @return int
*/
int rm(const char * path)
int rm(const char *path)
{
return syscall_invoke(SYS_UNLINK_AT, 0, (uint64_t)path, 0, 0, 0, 0, 0, 0);
}
@ -193,4 +193,14 @@ void swab(void *restrict src, void *restrict dest, ssize_t nbytes)
_src += transfer;
_dest += transfer;
}
}
/**
* @brief 获取当前进程的pid进程标识符
*
* @return pid_t 当前进程的pid
*/
pid_t getpid(void)
{
syscall_invoke(SYS_GETPID, 0, 0, 0, 0, 0, 0, 0, 0);
}

View File

@ -114,4 +114,6 @@ int rm(const char * path);
* @param dest 目的地址
* @param nbytes 交换字节数
*/
void swab(void *restrict src, void *restrict dest, ssize_t nbytes);
void swab(void *restrict src, void *restrict dest, ssize_t nbytes);
pid_t getpid(void);

View File

@ -31,6 +31,7 @@
#define SYS_KILL 23 // kill一个进程(向这个进程发出信号)
#define SYS_SIGACTION 24 // 设置进程的信号处理动作
#define SYS_RT_SIGRETURN 25 // 从信号处理函数返回
#define SYS_GETPID 26 // 获取当前进程的pid进程标识符
/**
* @brief 用户态系统调用函数