184 lines
3.2 KiB
C
184 lines
3.2 KiB
C
#include <luna.h>
|
|
#include <setjmp.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <sys/stat.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()
|
|
{
|
|
if (getpid() != 1)
|
|
{
|
|
fprintf(stderr, "init should be started as PID 1\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 PID %ld, PPID %ld\n\n", getpid(), getppid());
|
|
|
|
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"); }
|
|
|
|
printf("\n\nGot random number %d\n\n", rand());
|
|
|
|
sleep(2);
|
|
|
|
printf("Press any key to restart.\n\n");
|
|
|
|
int stderr_fd = fileno(stderr);
|
|
int new_stderr_fd = dup(stderr_fd);
|
|
if (new_stderr_fd < 0)
|
|
{
|
|
perror("dup");
|
|
return 1;
|
|
}
|
|
FILE* new_stderr = fdopen(new_stderr_fd, "rw");
|
|
if (!new_stderr)
|
|
{
|
|
perror("fdopen");
|
|
return 1;
|
|
}
|
|
|
|
fprintf(new_stderr, "Bye!\n\n");
|
|
|
|
fclose(new_stderr);
|
|
|
|
const char* pathname = "/etc";
|
|
|
|
printf("Creating directory %s\n", pathname);
|
|
|
|
if (mkdir(pathname, 0) < 0)
|
|
{
|
|
perror("mkdir");
|
|
return 1;
|
|
}
|
|
|
|
printf("Success!!\n");
|
|
|
|
printf("Forking...\n");
|
|
|
|
pid_t child = fork();
|
|
|
|
if (child < 0)
|
|
{
|
|
perror("fork");
|
|
return 1;
|
|
}
|
|
if (child == 0)
|
|
{
|
|
msleep(500);
|
|
printf("I am the child (PID %ld), my parent is PID %ld!!\n", getpid(), getppid());
|
|
execv("/bin/sym", NULL);
|
|
perror("execv");
|
|
return 1;
|
|
}
|
|
else { printf("Success!! Got PID %ld\n", child); }
|
|
|
|
jmp_buf env;
|
|
int val = setjmp(env);
|
|
if (val == 0) { printf("Returning from setjmp!\n"); }
|
|
else
|
|
{
|
|
printf("Returning from longjmp! val=%d\n", val);
|
|
return 0;
|
|
}
|
|
|
|
longjmp(env, 3);
|
|
}
|