Compare commits

...

2 Commits

Author SHA1 Message Date
2c813f5901 apps: Add more simple apps
Now that we can start them at will from the command line, bring them on!!
2022-10-19 21:11:38 +02:00
47bdfecedb Devices: Add /dev/uptime
This file contains how many milliseconds have passed since boot at the time of reading it :)
2022-10-19 21:11:12 +02:00
8 changed files with 97 additions and 41 deletions

View File

@ -1,4 +1,4 @@
APPS := init sym sh APPS := init sym sh crash uname uptime
APPS_DIR := $(LUNA_ROOT)/apps APPS_DIR := $(LUNA_ROOT)/apps
APPS_SRC := $(APPS_DIR)/src APPS_SRC := $(APPS_DIR)/src

5
apps/src/crash.c Normal file
View File

@ -0,0 +1,5 @@
int main()
{
int* ptr = (int*)0xdeadbeef;
*ptr = 6;
}

View File

@ -1,43 +1,8 @@
#include <luna.h> #include <luna.h>
#include <setjmp.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/wait.h> #include <sys/wait.h>
#include <unistd.h> #include <unistd.h>
int print_version()
{
char version[4096];
FILE* fp = fopen("/dev/version", "r");
if (!fp)
{
perror("fopen");
return 1;
}
size_t nread = fread(version, 4096, 1, fp);
if (ferror(fp))
{
perror("fread");
return 1;
}
version[nread] = 0;
if (fclose(fp) < 0)
{
perror("fclose");
return 1;
}
printf("Your kernel version is %s\n\n", version);
return 0;
}
int main() int main()
{ {
if (getpid() != 1) if (getpid() != 1)
@ -46,11 +11,7 @@ int main()
return 1; return 1;
} }
printf("Welcome to Luna!\n"); printf("Welcome to Luna!\n\n");
printf("Running as PID %ld\n", getpid());
if (print_version()) return 1;
msleep(200); msleep(200);

26
apps/src/uname.c Normal file
View File

@ -0,0 +1,26 @@
#include <stdio.h>
int main()
{
FILE* fp = fopen("/dev/version", "r");
if (!fp)
{
perror("fopen");
return 1;
}
char buf[4096];
size_t nread = fread(buf, sizeof(buf) - 1, 1, fp);
if (ferror(fp))
{
perror("fread");
return 1;
}
buf[nread] = 0; // null terminate it :)
printf("%s\n", buf);
fclose(fp);
}

29
apps/src/uptime.c Normal file
View File

@ -0,0 +1,29 @@
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE* fp = fopen("/dev/uptime", "r");
if (!fp)
{
perror("fopen");
return 1;
}
char buf[4096];
size_t nread = fread(buf, sizeof(buf) - 1, 1, fp);
if (ferror(fp))
{
perror("fread");
return 1;
}
buf[nread] = 0; // null terminate it :)
long ms_uptime = atol(buf);
printf("up for %ld seconds\n", ms_uptime / 1000);
fclose(fp);
}

View File

@ -0,0 +1,9 @@
#pragma once
#include "fs/VFS.h"
namespace UptimeDevice
{
VFS::Node* create_new(const char* devname);
ssize_t read(VFS::Node* node, size_t offset, size_t size, char* buffer);
}

View File

@ -3,6 +3,7 @@
#include "fs/devices/Keyboard.h" #include "fs/devices/Keyboard.h"
#include "fs/devices/Random.h" #include "fs/devices/Random.h"
#include "fs/devices/Serial.h" #include "fs/devices/Serial.h"
#include "fs/devices/Uptime.h"
#include "fs/devices/Version.h" #include "fs/devices/Version.h"
#include "std/stdlib.h" #include "std/stdlib.h"
#include "std/string.h" #include "std/string.h"
@ -29,6 +30,7 @@ VFS::Node* DeviceFS::get()
devfs_files[devfs_file_count++] = SerialDevice::create_new("serial"); devfs_files[devfs_file_count++] = SerialDevice::create_new("serial");
devfs_files[devfs_file_count++] = RandomDevice::create_new("random"); devfs_files[devfs_file_count++] = RandomDevice::create_new("random");
devfs_files[devfs_file_count++] = KeyboardDevice::create_new("kbd"); devfs_files[devfs_file_count++] = KeyboardDevice::create_new("kbd");
devfs_files[devfs_file_count++] = UptimeDevice::create_new("uptime");
return devfs_root; return devfs_root;
} }

View File

@ -0,0 +1,24 @@
#include "fs/devices/Uptime.h"
#include "std/stdio.h"
#include "std/stdlib.h"
#include "std/string.h"
#include "thread/PIT.h"
VFS::Node* UptimeDevice::create_new(const char* devname)
{
VFS::Node* dev = new VFS::Node;
dev->read_func = UptimeDevice::read;
dev->inode = 0;
dev->length = 0;
dev->type = VFS_DEVICE;
dev->flags = 0;
strncpy(dev->name, devname, sizeof(dev->name));
return dev;
}
ssize_t UptimeDevice::read(VFS::Node* node, size_t, size_t size, char* buffer)
{
if (!node) return -1;
snprintf(buffer, size + 1, "%ld", PIT::ms_since_boot); // FIXME: Support offseting this read
return (ssize_t)size;
}