Compare commits
21 Commits
7541540fd3
...
e73af4baa8
Author | SHA1 | Date | |
---|---|---|---|
e73af4baa8 | |||
65c79b35cd | |||
fa241a4849 | |||
295d94000e | |||
39305ace07 | |||
deaff4cd14 | |||
2e8d2d4703 | |||
538e34002c | |||
2e4ab68f4c | |||
845f4ede9a | |||
d6efcba17d | |||
734ae6b0bc | |||
3818776f12 | |||
8aa4b22669 | |||
8b915516bf | |||
446a7e3d8f | |||
ed89b34883 | |||
027535bb8f | |||
2f56f44c3f | |||
9a0a80b2c5 | |||
516ba1138d |
@ -26,4 +26,3 @@ luna_app(login.cpp login)
|
|||||||
luna_app(ipc-test.cpp ipc-test)
|
luna_app(ipc-test.cpp ipc-test)
|
||||||
luna_app(mount.cpp mount)
|
luna_app(mount.cpp mount)
|
||||||
luna_app(umount.cpp umount)
|
luna_app(umount.cpp umount)
|
||||||
luna_app(ps.cpp ps)
|
|
||||||
|
@ -40,8 +40,6 @@ Result<int> luna_main(int argc, char** argv)
|
|||||||
}
|
}
|
||||||
|
|
||||||
gid = grp->gr_gid;
|
gid = grp->gr_gid;
|
||||||
|
|
||||||
endgrent();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -75,8 +73,6 @@ Result<int> luna_main(int argc, char** argv)
|
|||||||
|
|
||||||
gid = pw->pw_gid;
|
gid = pw->pw_gid;
|
||||||
}
|
}
|
||||||
|
|
||||||
endpwent();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
44
apps/ps.cpp
44
apps/ps.cpp
@ -1,44 +0,0 @@
|
|||||||
#include <errno.h>
|
|
||||||
#include <os/ArgumentParser.h>
|
|
||||||
#include <os/File.h>
|
|
||||||
#include <pwd.h>
|
|
||||||
#include <sys/pstat.h>
|
|
||||||
#include <time.h>
|
|
||||||
|
|
||||||
Result<int> luna_main(int argc, char** argv)
|
|
||||||
{
|
|
||||||
os::ArgumentParser parser;
|
|
||||||
parser.add_description("Show a list of processes on the system.");
|
|
||||||
parser.add_system_program_info("ps"_sv);
|
|
||||||
parser.parse(argc, argv);
|
|
||||||
|
|
||||||
os::println("UID PID PPID TIME CMD");
|
|
||||||
|
|
||||||
pid_t last = pstat(-1, nullptr);
|
|
||||||
for (pid_t pid = 1; pid <= last; pid++)
|
|
||||||
{
|
|
||||||
static process ps;
|
|
||||||
if (pstat(pid, &ps) < 0)
|
|
||||||
{
|
|
||||||
if (errno == ESRCH) continue;
|
|
||||||
return err(errno);
|
|
||||||
}
|
|
||||||
|
|
||||||
struct tm tm;
|
|
||||||
gmtime_r(&ps.ps_time.tv_sec, &tm);
|
|
||||||
|
|
||||||
char timebuf[256];
|
|
||||||
strftime(timebuf, sizeof(timebuf), "%H:%M:%S", &tm);
|
|
||||||
|
|
||||||
const char* user = "???";
|
|
||||||
|
|
||||||
passwd* pw = getpwuid(ps.ps_uid);
|
|
||||||
if (pw) user = pw->pw_name;
|
|
||||||
|
|
||||||
os::println("%-8s %6d %6d %10s %s", user, ps.ps_pid, ps.ps_ppid, timebuf, ps.ps_name);
|
|
||||||
}
|
|
||||||
|
|
||||||
endpwent();
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
@ -74,7 +74,6 @@ Result<int> luna_main(int argc, char** argv)
|
|||||||
struct passwd* pw = getpwuid(getuid());
|
struct passwd* pw = getpwuid(getuid());
|
||||||
if (pw) { username = pw->pw_name; }
|
if (pw) { username = pw->pw_name; }
|
||||||
else { username = getenv("USER"); }
|
else { username = getenv("USER"); }
|
||||||
endpwent();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
while (1)
|
while (1)
|
||||||
|
@ -78,8 +78,6 @@ Result<int> luna_main(int argc, char** argv)
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
endpwent();
|
|
||||||
|
|
||||||
if ((prompt_password || getuid() != geteuid()) && *entry->pw_passwd)
|
if ((prompt_password || getuid() != geteuid()) && *entry->pw_passwd)
|
||||||
{
|
{
|
||||||
char* pass = getpass();
|
char* pass = getpass();
|
||||||
|
@ -31,7 +31,7 @@ set(SOURCES
|
|||||||
src/sys/file.cpp
|
src/sys/file.cpp
|
||||||
src/sys/id.cpp
|
src/sys/id.cpp
|
||||||
src/sys/mkdir.cpp
|
src/sys/mkdir.cpp
|
||||||
src/sys/pstat.cpp
|
src/sys/mknod.cpp
|
||||||
src/sys/waitpid.cpp
|
src/sys/waitpid.cpp
|
||||||
src/sys/getdents.cpp
|
src/sys/getdents.cpp
|
||||||
src/sys/stat.cpp
|
src/sys/stat.cpp
|
||||||
|
36
kernel/src/sys/mknod.cpp
Normal file
36
kernel/src/sys/mknod.cpp
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
#include "Log.h"
|
||||||
|
#include "fs/VFS.h"
|
||||||
|
#include "memory/MemoryManager.h"
|
||||||
|
#include "sys/Syscall.h"
|
||||||
|
#include "thread/Scheduler.h"
|
||||||
|
#include <bits/makedev.h>
|
||||||
|
#include <luna/PathParser.h>
|
||||||
|
|
||||||
|
Result<u64> sys_mknod(Registers*, SyscallArgs args)
|
||||||
|
{
|
||||||
|
auto path = TRY(MemoryManager::strdup_from_user(args[0]));
|
||||||
|
mode_t mode = (mode_t)args[1];
|
||||||
|
dev_t dev = (dev_t)args[2];
|
||||||
|
|
||||||
|
Thread* current = Scheduler::current();
|
||||||
|
|
||||||
|
u32 maj = luna_dev_major(dev);
|
||||||
|
u32 min = luna_dev_minor(dev);
|
||||||
|
|
||||||
|
auto parser = TRY(PathParser::create(path.chars()));
|
||||||
|
|
||||||
|
auto dirname = TRY(parser.dirname());
|
||||||
|
auto basename = TRY(parser.basename());
|
||||||
|
|
||||||
|
TRY(VFS::validate_filename(basename.view()));
|
||||||
|
|
||||||
|
auto parent = TRY(VFS::resolve_path(dirname.chars(), current->auth, current->current_directory));
|
||||||
|
if (!VFS::can_write(parent, current->auth)) return err(EACCES);
|
||||||
|
|
||||||
|
auto inode = TRY(parent->fs()->create_device_inode(maj, min));
|
||||||
|
TRY(parent->add_entry(inode, basename.chars()));
|
||||||
|
|
||||||
|
TRY(inode->chmod(mode));
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
@ -1,41 +0,0 @@
|
|||||||
#include "memory/MemoryManager.h"
|
|
||||||
#include "sys/Syscall.h"
|
|
||||||
#include "thread/Scheduler.h"
|
|
||||||
#include <bits/pstat.h>
|
|
||||||
|
|
||||||
static void set_timespec(struct timespec& ts, u64 ticks)
|
|
||||||
{
|
|
||||||
ts.tv_sec = ticks / 1000;
|
|
||||||
ts.tv_nsec = (ticks % 1000) * 1000 * 1000;
|
|
||||||
}
|
|
||||||
|
|
||||||
Result<u64> sys_pstat(Registers*, SyscallArgs args)
|
|
||||||
{
|
|
||||||
pid_t pid = (pid_t)args[0];
|
|
||||||
struct process* ps = (struct process*)args[1];
|
|
||||||
|
|
||||||
// If pid == -1, return the PID of the last spawned thread.
|
|
||||||
if (pid == -1) return g_threads.expect_last()->id;
|
|
||||||
|
|
||||||
auto* thread = TRY(Result<Thread*>::from_option(Scheduler::find_by_pid(pid), ESRCH));
|
|
||||||
|
|
||||||
struct process proc;
|
|
||||||
proc.ps_pid = (pid_t)thread->id;
|
|
||||||
proc.ps_ppid = thread->parent ? (pid_t)thread->parent->id : 0;
|
|
||||||
proc.ps_uid = thread->auth.uid;
|
|
||||||
proc.ps_gid = thread->auth.gid;
|
|
||||||
proc.ps_euid = thread->auth.euid;
|
|
||||||
proc.ps_egid = thread->auth.egid;
|
|
||||||
proc.ps_state = (int)thread->state;
|
|
||||||
proc.ps_flags = thread->is_kernel ? PS_FLAG_KRNL : 0;
|
|
||||||
set_timespec(proc.ps_time, thread->ticks);
|
|
||||||
set_timespec(proc.ps_ktime, thread->ticks_in_kernel);
|
|
||||||
set_timespec(proc.ps_utime, thread->ticks_in_user);
|
|
||||||
strlcpy(proc.ps_name, thread->name.chars(), sizeof(proc.ps_name));
|
|
||||||
strlcpy(proc.ps_cwd, thread->current_directory_path.is_empty() ? "/" : thread->current_directory_path.chars(),
|
|
||||||
sizeof(proc.ps_cwd));
|
|
||||||
|
|
||||||
if (!MemoryManager::copy_to_user_typed(ps, &proc)) return err(EFAULT);
|
|
||||||
|
|
||||||
return (u64)pid;
|
|
||||||
}
|
|
@ -25,7 +25,6 @@ set(SOURCES
|
|||||||
src/sys/ioctl.cpp
|
src/sys/ioctl.cpp
|
||||||
src/sys/utsname.cpp
|
src/sys/utsname.cpp
|
||||||
src/sys/mount.cpp
|
src/sys/mount.cpp
|
||||||
src/sys/pstat.cpp
|
|
||||||
)
|
)
|
||||||
|
|
||||||
if(${LUNA_ARCH} STREQUAL "x86_64")
|
if(${LUNA_ARCH} STREQUAL "x86_64")
|
||||||
|
@ -1,34 +0,0 @@
|
|||||||
/* bits/pstat.h: The process structure and flags for the pstat() system call. */
|
|
||||||
|
|
||||||
#ifndef _BITS_PSTAT_H
|
|
||||||
#define _BITS_PSTAT_H
|
|
||||||
|
|
||||||
#include <bits/timespec.h>
|
|
||||||
#include <sys/types.h>
|
|
||||||
|
|
||||||
#define PS_IDLE 1
|
|
||||||
#define PS_RUNNABLE 2
|
|
||||||
#define PS_SLEEPING 3
|
|
||||||
#define PS_WAITING 4
|
|
||||||
#define PS_ENDED 5
|
|
||||||
|
|
||||||
#define PS_FLAG_KRNL 1
|
|
||||||
|
|
||||||
struct process
|
|
||||||
{
|
|
||||||
pid_t ps_pid;
|
|
||||||
pid_t ps_ppid;
|
|
||||||
uid_t ps_uid;
|
|
||||||
uid_t ps_euid;
|
|
||||||
gid_t ps_gid;
|
|
||||||
gid_t ps_egid;
|
|
||||||
int ps_state;
|
|
||||||
int ps_flags;
|
|
||||||
struct timespec ps_time;
|
|
||||||
struct timespec ps_ktime;
|
|
||||||
struct timespec ps_utime;
|
|
||||||
char ps_name[129];
|
|
||||||
char ps_cwd[256];
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
@ -1,23 +0,0 @@
|
|||||||
/* sys/pstat.h: The pstat() system call. */
|
|
||||||
|
|
||||||
#ifndef _SYS_PSTAT_H
|
|
||||||
#define _SYS_PSTAT_H
|
|
||||||
|
|
||||||
#include <bits/pstat.h>
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
extern "C"
|
|
||||||
{
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/* Retrieve information about a process. */
|
|
||||||
pid_t pstat(pid_t pid, struct process* ps);
|
|
||||||
|
|
||||||
/* Get a string representing a process' state. */
|
|
||||||
const char* ps_state_name(int state);
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#endif
|
|
@ -15,6 +15,9 @@ extern "C"
|
|||||||
/* Create a directory. */
|
/* Create a directory. */
|
||||||
int mkdir(const char* path, mode_t mode);
|
int mkdir(const char* path, mode_t mode);
|
||||||
|
|
||||||
|
/* Create a special file. */
|
||||||
|
int mknod(const char* path, mode_t mode, dev_t dev);
|
||||||
|
|
||||||
/* Change the mode bits of a file. */
|
/* Change the mode bits of a file. */
|
||||||
int chmod(const char* path, mode_t mode);
|
int chmod(const char* path, mode_t mode);
|
||||||
|
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <fcntl.h>
|
|
||||||
#include <grp.h>
|
#include <grp.h>
|
||||||
#include <luna/Vector.h>
|
#include <luna/Vector.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
@ -21,7 +20,6 @@ extern "C"
|
|||||||
{
|
{
|
||||||
f = fopen("/etc/group", "r");
|
f = fopen("/etc/group", "r");
|
||||||
if (!f) return nullptr;
|
if (!f) return nullptr;
|
||||||
fcntl(fileno(f), F_SETFD, FD_CLOEXEC);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
while (true)
|
while (true)
|
||||||
@ -84,9 +82,14 @@ extern "C"
|
|||||||
|
|
||||||
while ((entry = getgrent()))
|
while ((entry = getgrent()))
|
||||||
{
|
{
|
||||||
if (!strcmp(entry->gr_name, name)) { return entry; }
|
if (!strcmp(entry->gr_name, name))
|
||||||
|
{
|
||||||
|
endgrent();
|
||||||
|
return entry;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
endgrent();
|
||||||
return entry;
|
return entry;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -98,9 +101,14 @@ extern "C"
|
|||||||
|
|
||||||
while ((entry = getgrent()))
|
while ((entry = getgrent()))
|
||||||
{
|
{
|
||||||
if (entry->gr_gid == gid) { return entry; }
|
if (entry->gr_gid == gid)
|
||||||
|
{
|
||||||
|
endgrent();
|
||||||
|
return entry;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
endgrent();
|
||||||
return entry;
|
return entry;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
#include <fcntl.h>
|
|
||||||
#include <pwd.h>
|
#include <pwd.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
@ -46,7 +45,6 @@ extern "C"
|
|||||||
{
|
{
|
||||||
f = fopen("/etc/passwd", "r");
|
f = fopen("/etc/passwd", "r");
|
||||||
if (!f) return nullptr;
|
if (!f) return nullptr;
|
||||||
fcntl(fileno(f), F_SETFD, FD_CLOEXEC);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
while (true)
|
while (true)
|
||||||
@ -95,9 +93,14 @@ extern "C"
|
|||||||
|
|
||||||
while ((entry = getpwent()))
|
while ((entry = getpwent()))
|
||||||
{
|
{
|
||||||
if (!strcmp(entry->pw_name, name)) { return entry; }
|
if (!strcmp(entry->pw_name, name))
|
||||||
|
{
|
||||||
|
endpwent();
|
||||||
|
return entry;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
endpwent();
|
||||||
return entry;
|
return entry;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -109,9 +112,14 @@ extern "C"
|
|||||||
|
|
||||||
while ((entry = getpwent()))
|
while ((entry = getpwent()))
|
||||||
{
|
{
|
||||||
if (entry->pw_uid == uid) { return entry; }
|
if (entry->pw_uid == uid)
|
||||||
|
{
|
||||||
|
endpwent();
|
||||||
|
return entry;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
endpwent();
|
||||||
return entry;
|
return entry;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,26 +0,0 @@
|
|||||||
#include <bits/errno-return.h>
|
|
||||||
#include <sys/pstat.h>
|
|
||||||
#include <sys/syscall.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
|
|
||||||
extern "C"
|
|
||||||
{
|
|
||||||
pid_t pstat(pid_t pid, struct process* ps)
|
|
||||||
{
|
|
||||||
long rc = syscall(SYS_pstat, pid, ps);
|
|
||||||
__errno_return(rc, pid_t);
|
|
||||||
}
|
|
||||||
|
|
||||||
const char* ps_state_name(int state)
|
|
||||||
{
|
|
||||||
switch (state)
|
|
||||||
{
|
|
||||||
case PS_IDLE: return "Idle";
|
|
||||||
case PS_RUNNABLE: return "Running";
|
|
||||||
case PS_SLEEPING: return "Sleeping";
|
|
||||||
case PS_WAITING: return "Waiting";
|
|
||||||
case PS_ENDED: return "Zombie";
|
|
||||||
default: return "???";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -12,6 +12,12 @@ extern "C"
|
|||||||
__errno_return(rc, int);
|
__errno_return(rc, int);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int mknod(const char* path, mode_t mode, dev_t dev)
|
||||||
|
{
|
||||||
|
long rc = syscall(SYS_mknod, path, mode, dev);
|
||||||
|
__errno_return(rc, int);
|
||||||
|
}
|
||||||
|
|
||||||
int chmod(const char* path, mode_t mode)
|
int chmod(const char* path, mode_t mode)
|
||||||
{
|
{
|
||||||
long rc = syscall(SYS_fchmodat, AT_FDCWD, path, mode, 0);
|
long rc = syscall(SYS_fchmodat, AT_FDCWD, path, mode, 0);
|
||||||
|
@ -2,10 +2,10 @@
|
|||||||
|
|
||||||
#define enumerate_syscalls(_e) \
|
#define enumerate_syscalls(_e) \
|
||||||
_e(exit) _e(clock_gettime) _e(mmap) _e(munmap) _e(usleep) _e(openat) _e(close) _e(read) _e(getpid) _e(write) \
|
_e(exit) _e(clock_gettime) _e(mmap) _e(munmap) _e(usleep) _e(openat) _e(close) _e(read) _e(getpid) _e(write) \
|
||||||
_e(lseek) _e(mkdir) _e(execve) _e(fork) _e(waitpid) _e(getppid) _e(fcntl) _e(getdents) _e(getuid) _e(geteuid) \
|
_e(lseek) _e(mkdir) _e(execve) _e(mknod) _e(fork) _e(waitpid) _e(getppid) _e(fcntl) _e(getdents) _e(getuid) \
|
||||||
_e(getgid) _e(getegid) _e(setuid) _e(setgid) _e(seteuid) _e(setegid) _e(fchmodat) _e(fchownat) _e(ioctl) \
|
_e(geteuid) _e(getgid) _e(getegid) _e(setuid) _e(setgid) _e(seteuid) _e(setegid) _e(fchmodat) _e(fchownat) \
|
||||||
_e(fstatat) _e(chdir) _e(getcwd) _e(unlinkat) _e(uname) _e(sethostname) _e(dup2) _e(pipe) _e(mount) \
|
_e(ioctl) _e(fstatat) _e(chdir) _e(getcwd) _e(unlinkat) _e(uname) _e(sethostname) _e(dup2) _e(pipe) \
|
||||||
_e(umount) _e(pstat)
|
_e(mount) _e(umount)
|
||||||
|
|
||||||
enum Syscalls
|
enum Syscalls
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user