Linux: Close spinlock device after gate is stopped
authorMisael Lopez Cruz <misael.lopez@ti.com>
Fri, 18 Dec 2015 21:50:51 +0000 (15:50 -0600)
committerAngela Stegmaier <angelabaker@ti.com>
Mon, 21 Dec 2015 22:05:04 +0000 (16:05 -0600)
The spinlock device is opened in the GateHWSpinlock_start()
function, but it's never closed.  The device is now being
closed in GateHWSpinlock_stop().

Signed-off-by: Misael Lopez Cruz <misael.lopez@ti.com>
linux/src/api/gates/GateHWSpinlock.c
linux/src/daemon/GateHWSpinlock.c

index b57016a99cfd2aa4ec83f2d220b4248e1885af9d..142f3e1823094f2c76818032a62c0f62eb64fed2 100644 (file)
@@ -70,6 +70,7 @@ typedef UInt32            Error_Block;
  */
 /* GateHWSpinlock Module Local State */
 typedef struct {
+    Int32                           fd;         /* spinlock device handle */
     UInt32 *                        baseAddr;   /* base addr lock registers */
     GateMutex_Handle                gmHandle;   /* handle to gate mutex */
 } GateHWSpinlock_Module_State;
@@ -92,6 +93,7 @@ GateHWSpinlock_Config _GateHWSpinlock_cfgParams;
 
 static GateHWSpinlock_Module_State GateHWSpinlock_state =
 {
+    .fd = -1,
     .baseAddr = NULL,
     .gmHandle = NULL
 };
@@ -170,32 +172,32 @@ Int32 GateHWSpinlock_start(Void)
 {
     Int32               status = GateHWSpinlock_S_SUCCESS;
     UInt32              dst;
-    Int32               fdMem;
     int                 flags;
 
-    fdMem = open ("/dev/mem", O_RDWR | O_SYNC);
-
-    if (fdMem < 0){
+    Mod->fd = open ("/dev/mem", O_RDWR | O_SYNC);
+    if (Mod->fd < 0){
         PRINTVERBOSE0("GateHWSpinlock_start: failed to open the /dev/mem");
         status = GateHWSpinlock_E_OSFAILURE;
     }
 
     /* make sure /dev/mem fd doesn't exist for 'fork() -> exec*()'ed child */
-    flags = fcntl(fdMem, F_GETFD);
+    flags = fcntl(Mod->fd, F_GETFD);
     if (flags != -1) {
-        fcntl(fdMem, F_SETFD, flags | FD_CLOEXEC);
+        fcntl(Mod->fd, F_SETFD, flags | FD_CLOEXEC);
     }
 
     /* map the hardware lock registers into the local address space */
     if (status == GateHWSpinlock_S_SUCCESS) {
         dst = (UInt32)mmap(NULL, _GateHWSpinlock_cfgParams.size,
                             (PROT_READ | PROT_WRITE),
-                            (MAP_SHARED), fdMem,
+                            (MAP_SHARED), Mod->fd,
                             (off_t)_GateHWSpinlock_cfgParams.baseAddr);
 
         if (dst == (UInt32)MAP_FAILED) {
             PRINTVERBOSE0("GateHWSpinlock_start: Memory map failed")
             status = GateHWSpinlock_E_OSFAILURE;
+            close(Mod->fd);
+            Mod->fd = -1;
         }
         else {
             Mod->baseAddr = (UInt32 *)(dst + _GateHWSpinlock_cfgParams.offset);
@@ -235,6 +237,12 @@ Int GateHWSpinlock_stop(Void)
            _GateHWSpinlock_cfgParams.size);
     }
 
+    /* close the spinlock device file */
+    if (Mod->fd >= 0) {
+        close(Mod->fd);
+        Mod->fd = -1;
+    }
+
     return(status);
 }
 
index 115f78b2cf0e85512f54abddd6fcd30513fd3054..9ea4d09877fb6648ee8ae558952b8eb449f09945 100644 (file)
@@ -69,6 +69,7 @@ typedef UInt32            Error_Block;
  */
 /* GateHWSpinlock Module Local State */
 typedef struct {
+    Int32                           fd;         /* spinlock device handle */
     UInt32 *                        baseAddr;   /* base addr lock registers */
     GateMutex_Handle                gmHandle;   /* handle to gate mutex */
 } GateHWSpinlock_Module_State;
@@ -91,6 +92,7 @@ GateHWSpinlock_Config _GateHWSpinlock_cfgParams;
 
 static GateHWSpinlock_Module_State GateHWSpinlock_state =
 {
+    .fd = -1,
     .baseAddr = NULL,
     .gmHandle = NULL
 };
@@ -116,11 +118,9 @@ Int32 GateHWSpinlock_start(Void)
 {
     Int32               status = GateHWSpinlock_S_SUCCESS;
     UInt32              dst;
-    Int32               fdMem;
 
-    fdMem = open ("/dev/mem", O_RDWR | O_SYNC);
-
-    if (fdMem < 0){
+    Mod->fd = open ("/dev/mem", O_RDWR | O_SYNC);
+    if (Mod->fd < 0){
         LOG0("GateHWSpinlock_start: failed to open the /dev/mem");
         status = GateHWSpinlock_E_OSFAILURE;
     }
@@ -129,12 +129,14 @@ Int32 GateHWSpinlock_start(Void)
     if (status == GateHWSpinlock_S_SUCCESS) {
         dst = (UInt32)mmap(NULL, _GateHWSpinlock_cfgParams.size,
                             (PROT_READ | PROT_WRITE),
-                            (MAP_SHARED), fdMem,
+                            (MAP_SHARED), Mod->fd,
                             (off_t)_GateHWSpinlock_cfgParams.baseAddr);
 
         if (dst == (UInt32)MAP_FAILED) {
             LOG0("GateHWSpinlock_start: Memory map failed")
             status = GateHWSpinlock_E_OSFAILURE;
+            close(Mod->fd);
+            Mod->fd = -1;
         }
         else {
             Mod->baseAddr = (UInt32 *)(dst + _GateHWSpinlock_cfgParams.offset);
@@ -174,6 +176,12 @@ Int GateHWSpinlock_stop(Void)
            _GateHWSpinlock_cfgParams.size);
     }
 
+    /* close the spinlock device file */
+    if (Mod->fd >= 0) {
+        close(Mod->fd);
+        Mod->fd = -1;
+    }
+
     return(status);
 }