From e0c18c05e8798c5be7f01d9a43e74da9f8fc5d75 Mon Sep 17 00:00:00 2001 From: dongjiuzhu1 Date: Fri, 22 Dec 2023 11:06:12 +0800 Subject: [PATCH] fs/driver: using nx_unlink to call unlink ops to release some resource if driver complete unlink ops, we need to call it to release some resource, otherwise, it will only remove inode. Signed-off-by: dongjiuzhu1 --- drivers/serial/pty.c | 37 +++++++++++++-------------------- fs/driver/fs_unregisterdriver.c | 10 +++++++++ 2 files changed, 24 insertions(+), 23 deletions(-) diff --git a/drivers/serial/pty.c b/drivers/serial/pty.c index cb36328355..e9272cdb72 100644 --- a/drivers/serial/pty.c +++ b/drivers/serial/pty.c @@ -285,32 +285,21 @@ static int pty_open(FAR struct file *filep) } } - /* If one side of the driver has been unlinked, then refuse further - * opens. - */ + /* First open? */ - if (devpair->pp_unlinked) + if (devpair->pp_nopen == 0) { - ret = -EIDRM; + /* Yes, create the internal pipe */ + + ret = pty_pipe(devpair); } - else + + /* Increment the count of open references on the driver */ + + if (ret >= 0) { - /* First open? */ - - if (devpair->pp_nopen == 0) - { - /* Yes, create the internal pipe */ - - ret = pty_pipe(devpair); - } - - /* Increment the count of open references on the driver */ - - if (ret >= 0) - { - devpair->pp_nopen++; - DEBUGASSERT(devpair->pp_nopen > 0); - } + devpair->pp_nopen++; + DEBUGASSERT(devpair->pp_nopen > 0); } nxmutex_unlock(&devpair->pp_lock); @@ -318,7 +307,7 @@ static int pty_open(FAR struct file *filep) } /**************************************************************************** - * Name: pty_open + * Name: pty_close ****************************************************************************/ static int pty_close(FAR struct file *filep) @@ -372,6 +361,7 @@ static int pty_close(FAR struct file *filep) { /* Yes.. Free the device pair now (without freeing the semaphore) */ + nxmutex_unlock(&devpair->pp_lock); pty_destroy(devpair); return OK; } @@ -961,6 +951,7 @@ static int pty_unlink(FAR struct inode *inode) if (devpair->pp_nopen == 0) { + nxmutex_unlock(&devpair->pp_lock); pty_destroy(devpair); return OK; } diff --git a/fs/driver/fs_unregisterdriver.c b/fs/driver/fs_unregisterdriver.c index fd6b6ee297..0b5d9b6c76 100644 --- a/fs/driver/fs_unregisterdriver.c +++ b/fs/driver/fs_unregisterdriver.c @@ -44,6 +44,16 @@ int unregister_driver(FAR const char *path) { int ret; + /* Call unlink to release driver resource and inode. */ + + ret = nx_unlink(path); + if (ret >= 0) + { + return ret; + } + + /* If unlink failed, only remove inode. */ + ret = inode_lock(); if (ret >= 0) {