1
+ /*
2
+ * lux - a lightweight unix-like operating system
3
+ * Omar Elghoul, 2024
4
+ *
5
+ * Core Microkernel
6
+ */
7
+
8
+ /* Kernel-Server Communication */
9
+
10
+ #include <stdlib.h>
11
+ #include <string.h>
12
+ #include <platform/platform.h>
13
+ #include <kernel/logger.h>
14
+ #include <kernel/socket.h>
15
+ #include <kernel/servers.h>
16
+
17
+ static int kernelSocket = 0 , lumenSocket = 0 ;
18
+ static int * connections ; // connected socket descriptors
19
+ static struct sockaddr * connaddr ; // connected socket addresses
20
+ static socklen_t * connlen ; // length of connected socket addresses
21
+ static void * buffer ;
22
+ static int connectionCount = 0 ;
23
+
24
+ /* serverInit(): initializes the server subsystem
25
+ * params: none
26
+ * returns: nothing
27
+ */
28
+
29
+ void serverInit () {
30
+ struct sockaddr_un addr ;
31
+ addr .sun_family = AF_UNIX ;
32
+ strcpy (addr .sun_path , SERVER_KERNEL_PATH ); // this is a special path and not a true file
33
+
34
+ kernelSocket = socket (NULL , AF_UNIX , SOCK_DGRAM | SOCK_NONBLOCK , 0 );
35
+ if (kernelSocket < 0 ) {
36
+ KERROR ("failed to open kernel socket: error code %d\n" , -1 * kernelSocket );
37
+ while (1 ) platformHalt ();
38
+ }
39
+
40
+ int status = bind (NULL , kernelSocket , (const struct sockaddr * )& addr , sizeof (struct sockaddr_un ));
41
+ if (status ) {
42
+ KERROR ("failed to bind kernel socket: error code %d\n" , -1 * status );
43
+ while (1 ) platformHalt ();
44
+ }
45
+
46
+ status = listen (NULL , kernelSocket , SERVER_MAX_CONNECTIONS );
47
+ if (status ) {
48
+ KERROR ("failed to listen to kernel socket: error code %d\n" , -1 * status );
49
+ while (1 ) platformHalt ();
50
+ }
51
+
52
+ connections = calloc (SERVER_MAX_CONNECTIONS , sizeof (int ));
53
+ connaddr = calloc (SERVER_MAX_CONNECTIONS , sizeof (struct sockaddr ));
54
+ connlen = calloc (SERVER_MAX_CONNECTIONS , sizeof (socklen_t ));
55
+ buffer = malloc (SERVER_MAX_SIZE );
56
+
57
+ if (!connections || !connaddr || !connlen || !buffer ) {
58
+ KERROR ("failed to allocate memory for incoming connections\n" );
59
+ while (1 ) platformHalt ();
60
+ }
61
+
62
+ KDEBUG ("kernel is listening on socket %d: %s\n" , kernelSocket , addr .sun_path );
63
+ }
0 commit comments