Skip to content

Commit

Permalink
ipc: protect socket list with spinlock
Browse files Browse the repository at this point in the history
  • Loading branch information
jewelcodes committed Sep 6, 2024
1 parent c53991a commit 3f0bcba
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/ipc/sockinit.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@

#include <errno.h>
#include <stdlib.h>
#include <platform/lock.h>
#include <kernel/logger.h>
#include <kernel/socket.h>
#include <kernel/io.h>
#include <kernel/sched.h>

/* array of system-wide open sockets */
static lock_t lock = LOCK_INITIAL;
static SocketDescriptor *sockets;
static int socketCount;

Expand Down Expand Up @@ -54,13 +56,16 @@ int socket(Thread *t, int domain, int type, int protocol) {
if(!p) return -ESRCH;
if(p->iodCount == MAX_IO_DESCRIPTORS) return -EMFILE;

acquireLockBlocking(&lock);

IODescriptor *iod = NULL; // open I/O descriptor
int sd = openIO(p, (void **) &iod);
if(sd < 0 || !iod) return sd;

iod->type = IO_SOCKET;
iod->data = calloc(1, sizeof(SocketDescriptor));
if(!iod->data) {
releaseLock(&lock);
closeIO(p, iod);
}

Expand All @@ -70,5 +75,9 @@ int socket(Thread *t, int domain, int type, int protocol) {
sock->type = type;
sock->protocol = protocol;

sockets[socketCount] = sock;
socketCount++;

releaseLock(&lock);
return sd;
}

0 comments on commit 3f0bcba

Please sign in to comment.