Skip to content

Commit

Permalink
kernel+userland(lib): refactor errno and signal list
Browse files Browse the repository at this point in the history
  • Loading branch information
mosmeh committed Jun 22, 2024
1 parent 3196eb0 commit 72b0463
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 74 deletions.
71 changes: 37 additions & 34 deletions kernel/api/signum.h
Original file line number Diff line number Diff line change
@@ -1,36 +1,39 @@
#pragma once

enum {
SIGHUP = 1,
SIGINT,
SIGQUIT,
SIGILL,
SIGTRAP,
SIGABRT,
SIGBUS,
SIGFPE,
SIGKILL,
SIGUSR1,
SIGSEGV,
SIGUSR2,
SIGPIPE,
SIGALRM,
SIGTERM,
SIGSTKFLT,
SIGCHLD,
SIGCONT,
SIGSTOP,
SIGTSTP,
SIGTTIN,
SIGTTOU,
SIGURG,
SIGXCPU,
SIGXFSZ,
SIGVTALRM,
SIGPROF,
SIGWINCH,
SIGIO,
SIGPWR,
SIGSYS,
NSIG
};
#define ENUMERATE_SIGNALS(F) \
F(SIGINVALID, "Invalid signal") \
F(SIGHUP, "Hangup") \
F(SIGINT, "Interrupt") \
F(SIGQUIT, "Quit") \
F(SIGILL, "Illegal instruction") \
F(SIGTRAP, "Trace/breakpoint trap") \
F(SIGABRT, "Aborted") \
F(SIGBUS, "Bus error") \
F(SIGFPE, "Floating point exception") \
F(SIGKILL, "Killed") \
F(SIGUSR1, "User defined signal 1") \
F(SIGSEGV, "Segmentation violation") \
F(SIGUSR2, "User defined signal 2") \
F(SIGPIPE, "Broken pipe") \
F(SIGALRM, "Alarm clock") \
F(SIGTERM, "Terminated") \
F(SIGSTKFLT, "Stack fault") \
F(SIGCHLD, "Child exited") \
F(SIGCONT, "Continued") \
F(SIGSTOP, "Stopped (signal)") \
F(SIGTSTP, "Stopped") \
F(SIGTTIN, "Stopped (tty input)") \
F(SIGTTOU, "Stopped (tty output)") \
F(SIGURG, "Urgent I/O condition") \
F(SIGXCPU, "CPU time limit exceeded") \
F(SIGXFSZ, "File size limit exceeded") \
F(SIGVTALRM, "Virtual timer expired") \
F(SIGPROF, "Profiling timer expired") \
F(SIGWINCH, "Window changed") \
F(SIGIO, "I/O possible") \
F(SIGPWR, "Power failure") \
F(SIGSYS, "Bad system call")

#define ENUM_ITEM(I, MSG) I,
enum { ENUMERATE_SIGNALS(ENUM_ITEM) NSIG };
#undef ENUM_ITEM
3 changes: 3 additions & 0 deletions userland/lib/signal.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@

#include <kernel/api/sys/types.h>

extern const char* const sys_signame[];
extern const char* const sys_siglist[];

int kill(pid_t pid, int sig);
2 changes: 2 additions & 0 deletions userland/lib/stdio.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include <common/stdio.h>
#include <kernel/api/stdio.h>

extern const char* const sys_errlist[];

int putchar(int ch);
int puts(const char* str);
int printf(const char* format, ...) PRINTF_LIKE(1, 2);
Expand Down
49 changes: 10 additions & 39 deletions userland/lib/string.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "string.h"
#include "errno.h"
#include "signal.h"
#include "signum.h"
#include "stdio.h"
#include "stdlib.h"

char* strdup(const char* src) {
Expand All @@ -14,51 +16,20 @@ char* strdup(const char* src) {
return buf;
}

#define ERRNO_MSG(I, MSG) MSG,
static const char* errno_msgs[EMAXERRNO] = {ENUMERATE_ERRNO(ERRNO_MSG)};
#undef ERRNO_MSG
#define NAME(NAME, MSG) STRINGIFY(NAME),
#define MSG(NAME, MSG) MSG,
const char* const sys_errlist[] = {ENUMERATE_ERRNO(MSG)};
const char* const sys_signame[] = {ENUMERATE_SIGNALS(NAME)};
const char* const sys_siglist[] = {ENUMERATE_SIGNALS(MSG)};
#undef NAME
#undef MSG

char* strerror(int errnum) {
if (0 <= errnum && errnum < EMAXERRNO)
return (char*)errno_msgs[errnum];
return (char*)sys_errlist[errnum];
return "Unknown error";
}

const char* const sys_siglist[NSIG] = {
"Invalid signal",
"Hangup",
"Interrupt",
"Quit",
"Illegal instruction",
"Trace/breakpoint trap",
"Aborted",
"Bus error",
"Floating point exception",
"Killed",
"User defined signal 1",
"Segmentation violation",
"User defined signal 2",
"Broken pipe",
"Alarm clock",
"Terminated",
"Stack fault",
"Child exited",
"Continued",
"Stopped (signal)",
"Stopped",
"Stopped (tty input)",
"Stopped (tty output)",
"Urgent I/O condition)",
"CPU time limit exceeded",
"File size limit exceeded",
"Virtual timer expired",
"Profiling timer expired",
"Window changed",
"I/O possible",
"Power failure",
"Bad system call",
};

char* strsignal(int signum) {
if (0 <= signum && signum < NSIG)
return (char*)sys_siglist[signum];
Expand Down
1 change: 0 additions & 1 deletion userland/lib/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@ char* strdup(const char* src);

char* strerror(int errnum);

extern const char* const sys_siglist[];
char* strsignal(int signum);

0 comments on commit 72b0463

Please sign in to comment.