mirror of
https://github.com/Fennix-Project/Kernel.git
synced 2025-05-27 06:54:35 +00:00
Allow kernel shell to execute binaries
This commit is contained in:
parent
ac2147b69b
commit
8be2e10a8c
@ -56,8 +56,7 @@ int SpawnInit()
|
|||||||
compat = Tasking::Linux;
|
compat = Tasking::Linux;
|
||||||
|
|
||||||
return Execute::Spawn(Config.InitPath, argv, envp,
|
return Execute::Spawn(Config.InitPath, argv, envp,
|
||||||
nullptr, false,
|
nullptr, false, compat, true);
|
||||||
compat, true);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void KernelMainThread()
|
void KernelMainThread()
|
||||||
|
124
kshell/shell.cpp
124
kshell/shell.cpp
@ -20,6 +20,7 @@
|
|||||||
#include <filesystem.hpp>
|
#include <filesystem.hpp>
|
||||||
#include <driver.hpp>
|
#include <driver.hpp>
|
||||||
#include <lock.hpp>
|
#include <lock.hpp>
|
||||||
|
#include <exec.hpp>
|
||||||
#include <debug.h>
|
#include <debug.h>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
|
|
||||||
@ -34,6 +35,17 @@ struct Command
|
|||||||
void (*Function)(const char *);
|
void (*Function)(const char *);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
bool ignoreBuiltin = false;
|
||||||
|
|
||||||
|
void __cmd_builtin(const char *)
|
||||||
|
{
|
||||||
|
ignoreBuiltin = !ignoreBuiltin;
|
||||||
|
if (ignoreBuiltin)
|
||||||
|
printf("Builtin commands are now ignored.\n");
|
||||||
|
else
|
||||||
|
printf("Builtin commands are now accepted.\n");
|
||||||
|
}
|
||||||
|
|
||||||
static Command commands[] = {
|
static Command commands[] = {
|
||||||
{"lsof", cmd_lsof},
|
{"lsof", cmd_lsof},
|
||||||
{"ls", cmd_ls},
|
{"ls", cmd_ls},
|
||||||
@ -41,12 +53,6 @@ static Command commands[] = {
|
|||||||
{"cd", cmd_cd},
|
{"cd", cmd_cd},
|
||||||
{"cat", cmd_cat},
|
{"cat", cmd_cat},
|
||||||
{"echo", cmd_echo},
|
{"echo", cmd_echo},
|
||||||
{"mkdir", nullptr},
|
|
||||||
{"touch", nullptr},
|
|
||||||
{"rm", nullptr},
|
|
||||||
{"rmdir", nullptr},
|
|
||||||
{"mv", nullptr},
|
|
||||||
{"cp", nullptr},
|
|
||||||
{"clear", cmd_clear},
|
{"clear", cmd_clear},
|
||||||
{"help", nullptr},
|
{"help", nullptr},
|
||||||
{"exit", cmd_exit},
|
{"exit", cmd_exit},
|
||||||
@ -57,19 +63,9 @@ static Command commands[] = {
|
|||||||
{"killall", cmd_killall},
|
{"killall", cmd_killall},
|
||||||
{"top", cmd_top},
|
{"top", cmd_top},
|
||||||
{"mem", cmd_mem},
|
{"mem", cmd_mem},
|
||||||
{"mount", nullptr},
|
|
||||||
{"umount", nullptr},
|
|
||||||
{"uname", cmd_uname},
|
{"uname", cmd_uname},
|
||||||
{"whoami", cmd_whoami},
|
{"whoami", cmd_whoami},
|
||||||
{"passwd", nullptr},
|
|
||||||
{"su", nullptr},
|
|
||||||
{"login", nullptr},
|
|
||||||
{"logout", nullptr},
|
|
||||||
{"uptime", cmd_uptime},
|
{"uptime", cmd_uptime},
|
||||||
{"chown", nullptr},
|
|
||||||
{"chgrp", nullptr},
|
|
||||||
{"chmod", nullptr},
|
|
||||||
{"chroot", nullptr},
|
|
||||||
{"lspci", cmd_lspci},
|
{"lspci", cmd_lspci},
|
||||||
{"lsacpi", cmd_lsacpi},
|
{"lsacpi", cmd_lsacpi},
|
||||||
{"lsmod", cmd_lsmod},
|
{"lsmod", cmd_lsmod},
|
||||||
@ -80,6 +76,7 @@ static Command commands[] = {
|
|||||||
{"depmod", nullptr},
|
{"depmod", nullptr},
|
||||||
{"panic", cmd_panic},
|
{"panic", cmd_panic},
|
||||||
{"dump", cmd_dump},
|
{"dump", cmd_dump},
|
||||||
|
{"builtin", __cmd_builtin},
|
||||||
};
|
};
|
||||||
|
|
||||||
std::atomic_uint32_t CurX = 0x10, CurY = 0x10;
|
std::atomic_uint32_t CurX = 0x10, CurY = 0x10;
|
||||||
@ -692,6 +689,16 @@ void StartKernelShell()
|
|||||||
bool Found = false;
|
bool Found = false;
|
||||||
for (size_t i = 0; i < sizeof(commands) / sizeof(Command); i++)
|
for (size_t i = 0; i < sizeof(commands) / sizeof(Command); i++)
|
||||||
{
|
{
|
||||||
|
if (unlikely(strncmp(strBuf.c_str(), "builtin", strBuf.length()) == 0))
|
||||||
|
{
|
||||||
|
__cmd_builtin(nullptr);
|
||||||
|
Found = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ignoreBuiltin)
|
||||||
|
break;
|
||||||
|
|
||||||
std::string cmd_extracted;
|
std::string cmd_extracted;
|
||||||
for (size_t i = 0; i < strBuf.length(); i++)
|
for (size_t i = 0; i < strBuf.length(); i++)
|
||||||
{
|
{
|
||||||
@ -731,18 +738,83 @@ void StartKernelShell()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!Found)
|
if (Found)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
std::string cmd_only;
|
||||||
|
for (size_t i = 0; i < strBuf.length(); i++)
|
||||||
{
|
{
|
||||||
std::string cmd_only;
|
if (strBuf[i] == ' ')
|
||||||
for (size_t i = 0; i < strBuf.length(); i++)
|
break;
|
||||||
{
|
cmd_only += strBuf[i];
|
||||||
if (strBuf[i] == ' ')
|
|
||||||
break;
|
|
||||||
cmd_only += strBuf[i];
|
|
||||||
}
|
|
||||||
printf("%s: command not found\n",
|
|
||||||
cmd_only.c_str());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string path = "/bin/";
|
||||||
|
path += cmd_only;
|
||||||
|
if (fs->PathExists(path.c_str()))
|
||||||
|
{
|
||||||
|
const char *envp[5] = {
|
||||||
|
"PATH=/bin:/usr/bin",
|
||||||
|
"TERM=tty",
|
||||||
|
"HOME=/root",
|
||||||
|
"USER=root",
|
||||||
|
nullptr};
|
||||||
|
|
||||||
|
const char **argv;
|
||||||
|
if (strBuf.length() > cmd_only.length())
|
||||||
|
{
|
||||||
|
std::string arg_only;
|
||||||
|
for (size_t i = cmd_only.length() + 1; i < strBuf.length(); i++)
|
||||||
|
arg_only += strBuf[i];
|
||||||
|
|
||||||
|
argv = new const char *[3];
|
||||||
|
argv[0] = path.c_str();
|
||||||
|
argv[1] = new char[arg_only.length() + 1];
|
||||||
|
strcpy((char *)argv[1], arg_only.c_str());
|
||||||
|
argv[2] = nullptr;
|
||||||
|
|
||||||
|
debug("argv[0]: %s; argv[1]: %s", argv[0], argv[1]);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
argv = new const char *[2];
|
||||||
|
argv[0] = path.c_str();
|
||||||
|
argv[1] = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
Tasking::TaskCompatibility compat = Tasking::Native;
|
||||||
|
if (Config.UseLinuxSyscalls)
|
||||||
|
compat = Tasking::Linux;
|
||||||
|
|
||||||
|
int ret = Execute::Spawn((char *)path.c_str(), argv, envp,
|
||||||
|
nullptr, false, compat, false);
|
||||||
|
if (argv[1])
|
||||||
|
delete argv[1];
|
||||||
|
delete argv;
|
||||||
|
if (ret >= 0)
|
||||||
|
{
|
||||||
|
Tasking::TCB *tcb;
|
||||||
|
Tasking::PCB *pcb;
|
||||||
|
pcb = TaskManager->GetProcessByID(ret);
|
||||||
|
if (pcb == nullptr)
|
||||||
|
{
|
||||||
|
printf("Failed to get process by ID\n");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
pcb->SetWorkingDirectory(cwd);
|
||||||
|
tcb = TaskManager->GetThreadByID(ret, pcb);
|
||||||
|
if (tcb == nullptr)
|
||||||
|
{
|
||||||
|
printf("Failed to get thread by ID\n");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
TaskManager->WaitForThread(tcb);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("%s: command not found\n",
|
||||||
|
cmd_only.c_str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user