]> git.corax.cc Git - corax/commitdiff
Add soclose() and sofree() functions
authorMatthias Kruk <m@m10k.eu>
Fri, 4 Oct 2019 06:38:29 +0000 (15:38 +0900)
committerMatthias Kruk <m@m10k.eu>
Fri, 4 Oct 2019 06:38:29 +0000 (15:38 +0900)
kernel/core/socket.c
kernel/core/socket.h

index 51dd1b8f37bc29a8af43b9294edeb7a89fe61b9c..d5528e40687eb9ca4bfda9f52df47b5e92bd2510 100644 (file)
@@ -164,3 +164,66 @@ gtfo:
 
        return(ret_val);
 }
+
+int soclose(int sockfd)
+{
+       int ret_val;
+
+       ret_val = -EINVAL;
+
+       if(sockfd >= 0 && sockfd < (sizeof(_sockets) / sizeof(_sockets[0]))) {
+               struct socket *so;
+
+               ret_val = -EBADF;
+               so = _sockets[sockfd];
+
+               /*
+                * `so' may be SOCKET_PREALLOC, if it is currently being
+                * allocated by a different thread - in which case we must
+                * leave it alone.
+                */
+               if(so && so != SOCKET_PREALLOC) {
+                       /*
+                        * Reduce the socket's refcount and free it if
+                        * there are no more references to it.
+                        */
+                       so->so_refs--;
+
+                       if(!so->so_refs) {
+                               ret_val = sofree(sockfd);
+                       } else {
+                               ret_val = 0;
+                       }
+               }
+       }
+
+       return(ret_val);
+}
+
+int sofree(int sockfd)
+{
+       int ret_val;
+
+       ret_val = -EINVAL;
+
+       /*
+        * The same checks should already have been performed by the caller, but
+        * better safe than sorry.
+        */
+       if(sockfd >= 0 && sockfd < (sizeof(_sockets) / sizeof(_sockets[0]))) {
+               struct socket *so;
+
+               ret_val = -EBADF;
+               so = _sockets[sockfd];
+
+               if(so && so != SOCKET_PREALLOC) {
+                       _sockets[sockfd] = NULL;
+                       _nsockets--;
+
+                       /* TODO: PRU_DETACH */
+                       kfree(so);
+               }
+       }
+
+       return(ret_val);
+}
index a36681a099c40a89fcea5338537c6e5239f3353e..a5eed13b52ebb474feea26021b208f97de25142c 100644 (file)
@@ -8,6 +8,7 @@
 
 struct socket {
        u16_t so_type;
+       u16_t so_refs;
        /* that's all for now */
 #if 0
        u16_t so_options;
@@ -20,5 +21,7 @@ struct socket {
 };
 
 int socreate(int, int, int);
+int soclose(int);
+int sofree(int);
 
 #endif /* __SOCKET_H */