Compare commits

..

3 Commits

Author SHA1 Message Date
aa90e4a8d9 libc: Implement freopen() 2022-10-19 17:32:59 +02:00
f3af3e252b Kernel: refresh task_misbehave()
That function was severely outdated.
2022-10-19 17:26:36 +02:00
ef8ba3dec4 Kernel: Do not hang when a user task misbehaves
This was for testing/debugging. But we DEFINITELY don't want that.
2022-10-19 17:25:56 +02:00
5 changed files with 35 additions and 3 deletions

View File

@ -174,6 +174,14 @@ int main()
}
else { printf("Success!! Got PID %ld\n", child); }
child = fork();
if (child < 0)
{
perror("fork");
return 1;
}
if (child == 0) { *(int*)(0xdeadbeef) = 1234; }
int status;
for (;;)
{

View File

@ -51,8 +51,6 @@ extern "C" void common_handler(Context* context)
StackTracer tracer(context->rbp);
tracer.trace_with_ip(context->rip);
hang(); // FIXME: Remove this when multiple address spaces are working.
Scheduler::task_misbehave(context, -3);
}
}

View File

@ -287,8 +287,17 @@ void Scheduler::task_misbehave(Context* context, int64_t status)
{
ASSERT(Interrupts::is_in_handler());
kdbgln("exit: task %ld misbehaved, used %ld ms of cpu time", sched_current_task->id, sched_current_task->cpu_time);
sched_current_task->state = sched_current_task->Exited;
if (sched_current_task->id == 1) sched_current_task->state = sched_current_task->Exited;
else
sched_current_task->state = sched_current_task->Dying;
sched_current_task->exit_status = status;
if (sched_current_task->id != 1)
{
sched_for_each_child(sched_current_task, [](Task* child) {
if (child->state != child->Exited) child->ppid = 1;
return true;
});
}
task_yield(context);
}

View File

@ -42,6 +42,9 @@ extern "C"
/* Returns a new file associated with the file descriptor fd. */
FILE* fdopen(int fd, const char* mode);
/* Opens the file specified by pathname and points the file handle stream to it. */
FILE* freopen(const char* pathname, const char* mode, FILE* stream);
/* Returns the file descriptor associated with the file stream. */
int fileno(FILE* stream);

View File

@ -51,6 +51,20 @@ extern "C"
return stream;
}
FILE* freopen(const char* pathname, const char* mode,
FILE* stream) // FIXME: If pathname is NULL, open the original file with the new mode.
{
int fd = open(pathname, O_RDWR); // FIXME: Use the mode string.
if (fd < 0) { return 0; }
fflush(stream); // To make it future-proof.
fclose(stream);
stream->f_fd = fd;
clearerr(stream);
return stream;
}
int fileno(FILE* stream)
{
return stream->f_fd;