We have now proven that exec() does fail and return to userspace when a file is not a valid executable. We can now go back to executing a normal program.
134 lines
2.2 KiB
C
134 lines
2.2 KiB
C
#include <errno.h>
|
|
#include <luna.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <sys/syscall.h>
|
|
#include <unistd.h>
|
|
|
|
typedef long ssize_t;
|
|
|
|
int print_version()
|
|
{
|
|
char version[4096];
|
|
|
|
FILE* verfile = fopen("/dev/version", "r");
|
|
if (!verfile)
|
|
{
|
|
perror("fopen");
|
|
return 1;
|
|
}
|
|
|
|
size_t nread = fread(version, 4096, 1, verfile);
|
|
if (ferror(verfile))
|
|
{
|
|
perror("fread");
|
|
return 1;
|
|
}
|
|
|
|
version[nread] = 0;
|
|
|
|
if (fclose(verfile) < 0)
|
|
{
|
|
perror("fclose");
|
|
return 1;
|
|
}
|
|
|
|
printf("Your kernel version is %s\n\n", version);
|
|
|
|
return 0;
|
|
}
|
|
|
|
int main()
|
|
{
|
|
if (gettid() == 0) // why are we the idle task?
|
|
{
|
|
fprintf(stderr, "SHENANIGANS! init is tid 0 (which is reserved for the idle task)\n");
|
|
return 1;
|
|
}
|
|
|
|
FILE* serial = fopen("/dev/serial", "r");
|
|
if (!serial)
|
|
{
|
|
perror("fopen");
|
|
return 1;
|
|
}
|
|
if (fputs("Hello from init!\n", serial) < 0)
|
|
{
|
|
perror("fputs");
|
|
return 1;
|
|
}
|
|
if (fclose(serial) < 0)
|
|
{
|
|
perror("fclose");
|
|
return 1;
|
|
}
|
|
|
|
printf("Welcome to Luna!\n");
|
|
|
|
printf("Running as tid %ld\n\n", gettid());
|
|
|
|
sleep(1);
|
|
|
|
if (print_version()) return 1;
|
|
|
|
sleep(2);
|
|
|
|
const char* filename = "/sys/config";
|
|
|
|
printf("Opening %s for reading...\n", filename);
|
|
|
|
FILE* config = fopen(filename, "r");
|
|
if (!config)
|
|
{
|
|
perror("fopen");
|
|
return 1;
|
|
}
|
|
|
|
if (fseek(config, 0, SEEK_END) < 0)
|
|
{
|
|
perror("fseek");
|
|
return 1;
|
|
}
|
|
|
|
long offset = ftell(config);
|
|
if (offset < 0)
|
|
{
|
|
perror("ftell");
|
|
return 1;
|
|
}
|
|
|
|
printf("%s is %ld bytes long\n", filename, offset);
|
|
|
|
rewind(config);
|
|
|
|
char buf[4096];
|
|
|
|
size_t nread = fread(buf, sizeof(buf), 1, config);
|
|
if (ferror(config)) { perror("fread"); }
|
|
else
|
|
{
|
|
buf[nread] = 0;
|
|
|
|
printf("Read %zd bytes\n\n", nread);
|
|
|
|
printf("%s", buf);
|
|
}
|
|
|
|
if (fclose(config) < 0) { perror("fclose"); }
|
|
|
|
sleep(2);
|
|
|
|
printf("\n\nPress any key to restart.\n\n");
|
|
|
|
sleep(2);
|
|
|
|
if (execv("/bin/sym", NULL) < 0)
|
|
{
|
|
perror("execv");
|
|
return 1;
|
|
}
|
|
|
|
return 0;
|
|
}
|