mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 01:17:35 +00:00
Implement argc/argv support for spawned tasks.
Celebrate the new functionality with a simple /bin/cat implementation. :^)
This commit is contained in:
parent
53abfa7ea1
commit
df87dda63c
13 changed files with 105 additions and 17 deletions
|
@ -65,7 +65,7 @@ DWORD handle(DWORD function, DWORD arg1, DWORD arg2, DWORD arg3)
|
|||
case Syscall::PosixGettimeofday:
|
||||
return current->sys$gettimeofday((timeval*)arg1);
|
||||
case Syscall::Spawn:
|
||||
return current->sys$spawn((const char*)arg1);
|
||||
return current->sys$spawn((const char*)arg1, (const char**)arg2);
|
||||
case Syscall::GetDirEntries:
|
||||
return current->sys$get_dir_entries((int)arg1, (void*)arg2, (size_t)arg3);
|
||||
case Syscall::PosixLstat:
|
||||
|
@ -108,6 +108,8 @@ DWORD handle(DWORD function, DWORD arg1, DWORD arg2, DWORD arg3)
|
|||
current->sys$exit((int)arg1);
|
||||
ASSERT_NOT_REACHED();
|
||||
return 0;
|
||||
case Syscall::GetArguments:
|
||||
return current->sys$get_arguments((int*)arg1, (char***)arg2);
|
||||
default:
|
||||
kprintf("int0x80: Unknown function %x requested {%x, %x, %x}\n", function, arg1, arg2, arg3);
|
||||
break;
|
||||
|
|
|
@ -31,6 +31,7 @@ enum Function {
|
|||
PosixGetcwd = 0x1999,
|
||||
PosixGettimeofday = 0x2000,
|
||||
PosixGethostname = 0x2001,
|
||||
GetArguments = 0x2002,
|
||||
};
|
||||
|
||||
void initialize();
|
||||
|
|
|
@ -185,18 +185,19 @@ int Task::sys$gethostname(char* buffer, size_t size)
|
|||
if (size < (hn.length() + 1))
|
||||
return -ENAMETOOLONG;
|
||||
memcpy(buffer, hn.characters(), size);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int Task::sys$spawn(const char* path)
|
||||
int Task::sys$spawn(const char* path, const char** args)
|
||||
{
|
||||
int error = 0;
|
||||
auto* child = Task::createUserTask(path, m_uid, m_gid, m_pid, error);
|
||||
auto* child = Task::createUserTask(path, m_uid, m_gid, m_pid, error, args);
|
||||
if (child)
|
||||
return child->pid();
|
||||
return error;
|
||||
}
|
||||
|
||||
Task* Task::createUserTask(const String& path, uid_t uid, gid_t gid, pid_t parentPID, int& error)
|
||||
Task* Task::createUserTask(const String& path, uid_t uid, gid_t gid, pid_t parentPID, int& error, const char** args)
|
||||
{
|
||||
auto parts = path.split('/');
|
||||
if (parts.isEmpty()) {
|
||||
|
@ -216,9 +217,20 @@ Task* Task::createUserTask(const String& path, uid_t uid, gid_t gid, pid_t paren
|
|||
return nullptr;
|
||||
}
|
||||
|
||||
Vector<String> taskArguments;
|
||||
if (args) {
|
||||
for (size_t i = 0; args[i]; ++i) {
|
||||
taskArguments.append(args[i]);
|
||||
}
|
||||
} else {
|
||||
taskArguments.append(parts.last());
|
||||
}
|
||||
|
||||
InterruptDisabler disabler; // FIXME: Get rid of this, jesus christ. This "critical" section is HUGE.
|
||||
Task* t = new Task(parts.takeLast(), uid, gid, parentPID, Ring3);
|
||||
|
||||
t->m_arguments = move(taskArguments);
|
||||
|
||||
ExecSpace space;
|
||||
space.hookableAlloc = [&] (const String& name, size_t size) {
|
||||
if (!size)
|
||||
|
@ -257,6 +269,25 @@ Task* Task::createUserTask(const String& path, uid_t uid, gid_t gid, pid_t paren
|
|||
return t;
|
||||
}
|
||||
|
||||
int Task::sys$get_arguments(int* argc, char*** argv)
|
||||
{
|
||||
auto* region = allocateRegion(4096, "argv");
|
||||
if (!region)
|
||||
return -ENOMEM;
|
||||
MemoryManager::the().mapRegion(*this, *region);
|
||||
char* argpage = (char*)region->linearAddress.get();
|
||||
*argc = m_arguments.size();
|
||||
*argv = (char**)argpage;
|
||||
char* bufptr = argpage + (sizeof(char*) * m_arguments.size());
|
||||
for (size_t i = 0; i < m_arguments.size(); ++i) {
|
||||
(*argv)[i] = bufptr;
|
||||
memcpy(bufptr, m_arguments[i].characters(), m_arguments[i].length());
|
||||
bufptr += m_arguments[i].length();
|
||||
*(bufptr++) = '\0';
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
Task* Task::createKernelTask(void (*e)(), String&& name)
|
||||
{
|
||||
Task* task = new Task(move(name), (uid_t)0, (gid_t)0, (pid_t)0, Ring0);
|
||||
|
|
|
@ -16,7 +16,7 @@ class Task : public InlineLinkedListNode<Task> {
|
|||
friend class InlineLinkedListNode<Task>;
|
||||
public:
|
||||
static Task* createKernelTask(void (*entry)(), String&& name);
|
||||
static Task* createUserTask(const String& path, uid_t, gid_t, pid_t parentPID, int& error);
|
||||
static Task* createUserTask(const String& path, uid_t, gid_t, pid_t parentPID, int& error, const char** args = nullptr);
|
||||
~Task();
|
||||
|
||||
static Vector<Task*> allTasks();
|
||||
|
@ -92,7 +92,7 @@ public:
|
|||
int sys$kill(pid_t pid, int sig);
|
||||
int sys$geterror() { return m_error; }
|
||||
void sys$exit(int status);
|
||||
int sys$spawn(const char* path);
|
||||
int sys$spawn(const char* path, const char** args);
|
||||
pid_t sys$waitpid(pid_t);
|
||||
void* sys$mmap(void*, size_t size);
|
||||
int sys$munmap(void*, size_t size);
|
||||
|
@ -101,6 +101,7 @@ public:
|
|||
int sys$sleep(unsigned seconds);
|
||||
int sys$gettimeofday(timeval*);
|
||||
int sys$gethostname(char* name, size_t length);
|
||||
int sys$get_arguments(int* argc, char*** argv);
|
||||
|
||||
static void initialize();
|
||||
|
||||
|
@ -170,6 +171,8 @@ private:
|
|||
LinearAddress m_nextRegion;
|
||||
|
||||
pid_t m_parentPID { 0 };
|
||||
|
||||
Vector<String> m_arguments;
|
||||
};
|
||||
|
||||
extern void task_init();
|
||||
|
|
Binary file not shown.
|
@ -10,5 +10,6 @@ cp ../Userland/date mnt/bin/date
|
|||
cp ../Userland/true mnt/bin/true
|
||||
cp ../Userland/false mnt/bin/false
|
||||
cp ../Userland/hostname mnt/bin/hostname
|
||||
cp ../Userland/cat mnt/bin/cat
|
||||
umount mnt
|
||||
sync
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue