PatchworkOS  f20e850
A non-POSIX operating system.
Loading...
Searching...
No Matches
root.c
Go to the documentation of this file.
1#include "root.h"
2
4#include <errno.h>
5#include <stdio.h>
6#include <stdlib.h>
7#include <string.h>
8#include <sys/io.h>
9
10#define ROOT_PASSWORD "1234"
11
13{
14 char buffer[MAX_PATH] = {0};
15 if (read(client, buffer, sizeof(buffer) - 1) == ERR)
16 {
17 return ERR;
18 }
19
20 if (strcmp(buffer, ROOT_PASSWORD) != 0)
21 {
22 errno = EPERM;
23 return ERR;
24 }
25
26 fd_t ns = open("/proc/self/ns");
27 if (ns == ERR)
28 {
29 return ERR;
30 }
31
32 key_t key;
33 if (share(&key, ns, CLOCKS_NEVER) == ERR)
34 {
35 close(ns);
36 return ERR;
37 }
38
39 write(client, &key, sizeof(key));
40 close(ns);
41 return 0;
42}
43
44void root_start(void)
45{
46 char* id = sreadfile("/net/local/seqpacket");
47 if (id == NULL)
48 {
49 printf("root: failed to open local seqpacket socket (%s)\n", strerror(errno));
50 abort();
51 }
52
53 if (swritefile(F("/net/local/%s/ctl", id), "bind root && listen") == ERR)
54 {
55 printf("root: failed to bind to root (%s)\n", strerror(errno));
56 goto error;
57 }
58
59 printf("root: listening for connections...\n");
60 while (1)
61 {
62 fd_t client = open(F("/net/local/%s/accept", id));
63 if (client == ERR)
64 {
65 printf("init: failed to accept connection (%s)\n", strerror(errno));
66 goto error;
67 }
68
69 if (root_handle_client(client) == ERR)
70 {
71 printf("root: failed to handle client (%s)\n", strerror(errno));
72 }
73
74 close(client);
75 }
76
77error:
78 free(id);
79 abort();
80}
#define MAX_PATH
Maximum length of filepaths.
Definition MAX_PATH.h:11
#define CLOCKS_NEVER
Definition clock_t.h:16
#define errno
Error number variable.
Definition errno.h:27
#define EPERM
Operation not permitted.
Definition errno.h:37
fd_t open(const char *path)
System call for opening files.
Definition open.c:9
uint64_t close(fd_t fd)
System call for closing files.
Definition close.c:9
#define F(format,...)
Allocates a formatted string on the stack.
Definition io.h:65
uint64_t swritefile(const char *path, const char *string)
Wrapper for writing a null-terminated string directly to a file using a path.
Definition swritefile.c:4
uint64_t read(fd_t fd, void *buffer, uint64_t count)
System call for reading from files.
Definition read.c:9
char * sreadfile(const char *path)
Wrapper for reading an entire file directly into a null-terminated string.
Definition sreadfile.c:3
uint64_t share(key_t *key, fd_t fd, clock_t timeout)
System call for sharing a file descriptor with another process.
Definition share.c:6
uint64_t write(fd_t fd, const void *buffer, uint64_t count)
System call for writing to files.
Definition write.c:9
#define NULL
Pointer error value.
Definition NULL.h:23
#define ERR
Integer error value.
Definition ERR.h:17
__UINT64_TYPE__ fd_t
A file descriptor.
Definition fd_t.h:12
void root_start(void)
Definition root.c:44
EFI_PHYSICAL_ADDRESS buffer
Definition mem.c:15
uint64_t root_handle_client(fd_t client)
Definition root.c:12
#define ROOT_PASSWORD
Definition root.c:10
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
_PUBLIC int printf(const char *_RESTRICT format,...)
Definition printf.c:5
_PUBLIC _NORETURN void abort(void)
Definition abort.c:9
_PUBLIC void free(void *ptr)
Definition free.c:11
_PUBLIC char * strerror(int errnum)
Definition strerror.c:6
_PUBLIC int strcmp(const char *s1, const char *s2)
Definition strcmp.c:3
Key type.
Definition io.h:472