]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
smbd: Replace SMB_VFS_CLOSE() calls with fd_close()
authorVolker Lendecke <vl@samba.org>
Tue, 28 Dec 2021 11:25:59 +0000 (12:25 +0100)
committerRalph Boehme <slow@samba.org>
Thu, 30 Dec 2021 11:03:35 +0000 (11:03 +0000)
fd_close() mostly wraps SMB_VFS_CLOSE() but also takes care of refcounting
fsp->fh properly and also makes sure that fsp->fh->fd is set to -1 after close.

Signed-off-by: Volker Lendecke <vl@samba.org>
Reviewed-by: Ralph Boehme <slow@samba.org>
source3/smbd/durable.c
source3/smbd/open.c
source3/smbd/pysmbd.c
source3/torture/cmd_vfs.c

index 88e0b70d137cce493ac8eee8877e48eb9a939203..a49bca6fd6192766bb90715e2f35fde0ba1cd95a 100644 (file)
@@ -837,15 +837,15 @@ NTSTATUS vfs_default_durable_reconnect(struct connection_struct *conn,
 
        ret = SMB_VFS_FSTAT(fsp, &fsp->fsp_name->st);
        if (ret == -1) {
+               NTSTATUS close_status;
                status = map_nt_error_from_unix_common(errno);
                DEBUG(1, ("Unable to fstat stream: %s => %s\n",
                          smb_fname_str_dbg(smb_fname),
                          nt_errstr(status)));
-               ret = SMB_VFS_CLOSE(fsp);
-               if (ret == -1) {
-                       DEBUG(0, ("vfs_default_durable_reconnect: "
-                                 "SMB_VFS_CLOSE failed (%s) - leaking file "
-                                 "descriptor\n", strerror(errno)));
+               close_status = fd_close(fsp);
+               if (!NT_STATUS_IS_OK(close_status)) {
+                       DBG_ERR("fd_close failed (%s) - leaking file "
+                               "descriptor\n", nt_errstr(close_status));
                }
                TALLOC_FREE(lck);
                op->compat = NULL;
@@ -855,11 +855,10 @@ NTSTATUS vfs_default_durable_reconnect(struct connection_struct *conn,
        }
 
        if (!S_ISREG(fsp->fsp_name->st.st_ex_mode)) {
-               ret = SMB_VFS_CLOSE(fsp);
-               if (ret == -1) {
-                       DEBUG(0, ("vfs_default_durable_reconnect: "
-                                 "SMB_VFS_CLOSE failed (%s) - leaking file "
-                                 "descriptor\n", strerror(errno)));
+               NTSTATUS close_status = fd_close(fsp);
+               if (!NT_STATUS_IS_OK(close_status)) {
+                       DBG_ERR("fd_close failed (%s) - leaking file "
+                               "descriptor\n", nt_errstr(close_status));
                }
                TALLOC_FREE(lck);
                op->compat = NULL;
@@ -870,11 +869,10 @@ NTSTATUS vfs_default_durable_reconnect(struct connection_struct *conn,
 
        file_id = vfs_file_id_from_sbuf(conn, &fsp->fsp_name->st);
        if (!file_id_equal(&cookie.id, &file_id)) {
-               ret = SMB_VFS_CLOSE(fsp);
-               if (ret == -1) {
-                       DEBUG(0, ("vfs_default_durable_reconnect: "
-                                 "SMB_VFS_CLOSE failed (%s) - leaking file "
-                                 "descriptor\n", strerror(errno)));
+               NTSTATUS close_status = fd_close(fsp);
+               if (!NT_STATUS_IS_OK(close_status)) {
+                       DBG_ERR("fd_close failed (%s) - leaking file "
+                               "descriptor\n", nt_errstr(close_status));
                }
                TALLOC_FREE(lck);
                op->compat = NULL;
@@ -889,11 +887,10 @@ NTSTATUS vfs_default_durable_reconnect(struct connection_struct *conn,
                                                      &fsp->fsp_name->st,
                                                      fsp_str_dbg(fsp));
        if (!ok) {
-               ret = SMB_VFS_CLOSE(fsp);
-               if (ret == -1) {
-                       DEBUG(0, ("vfs_default_durable_reconnect: "
-                                 "SMB_VFS_CLOSE failed (%s) - leaking file "
-                                 "descriptor\n", strerror(errno)));
+               NTSTATUS close_status = fd_close(fsp);
+               if (!NT_STATUS_IS_OK(close_status)) {
+                       DBG_ERR("fd_close failed (%s) - leaking file "
+                               "descriptor\n", nt_errstr(close_status));
                }
                TALLOC_FREE(lck);
                op->compat = NULL;
@@ -904,13 +901,10 @@ NTSTATUS vfs_default_durable_reconnect(struct connection_struct *conn,
 
        status = set_file_oplock(fsp);
        if (!NT_STATUS_IS_OK(status)) {
-               DEBUG(1, ("vfs_default_durable_reconnect failed to set oplock "
-                         "after opening file: %s\n", nt_errstr(status)));
-               ret = SMB_VFS_CLOSE(fsp);
-               if (ret == -1) {
-                       DEBUG(0, ("vfs_default_durable_reconnect: "
-                                 "SMB_VFS_CLOSE failed (%s) - leaking file "
-                                 "descriptor\n", strerror(errno)));
+               NTSTATUS close_status = fd_close(fsp);
+               if (!NT_STATUS_IS_OK(close_status)) {
+                       DBG_ERR("fd_close failed (%s) - leaking file "
+                               "descriptor\n", nt_errstr(close_status));
                }
                TALLOC_FREE(lck);
                op->compat = NULL;
index 7f1aedbd1fb4225d8c1081242f23c8d14c6871aa..6a9a1d9a9dc128de62590c8bf060a7009b1911c2 100644 (file)
@@ -801,10 +801,8 @@ static NTSTATUS non_widelink_open(const struct files_struct *dirfsp,
                 * and errno=ELOOP.
                 */
                if (S_ISLNK(fsp->fsp_name->st.st_ex_mode)) {
-                       ret = SMB_VFS_CLOSE(fsp);
-                       SMB_ASSERT(ret == 0);
-
-                       fsp_set_fd(fsp, -1);
+                       status = fd_close(fsp);
+                       SMB_ASSERT(NT_STATUS_IS_OK(status));
                        fd = -1;
                        status = NT_STATUS_STOPPED_ON_SYMLINK;
                }
@@ -1158,7 +1156,6 @@ static NTSTATUS reopen_from_procfd(struct files_struct *fsp,
        int old_fd;
        int new_fd;
        NTSTATUS status;
-       int ret;
 
        if (!fsp->fsp_flags.have_proc_fds) {
                return NT_STATUS_MORE_PROCESSING_REQUIRED;
@@ -1197,15 +1194,13 @@ static NTSTATUS reopen_from_procfd(struct files_struct *fsp,
                                mode);
        if (new_fd == -1) {
                status = map_nt_error_from_unix(errno);
-               SMB_VFS_CLOSE(fsp);
-               fsp_set_fd(fsp, -1);
+               fd_close(fsp);
                return status;
        }
 
-       ret = SMB_VFS_CLOSE(fsp);
-       fsp_set_fd(fsp, -1);
-       if (ret != 0) {
-               return map_nt_error_from_unix(errno);
+       status = fd_close(fsp);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
        }
 
        fsp_set_fd(fsp, new_fd);
index e9edf6c19a84da450a909310a6e5d5ab609f40af..17a27e8cb35959361f20e08950b7bef432a67b0b 100644 (file)
@@ -282,7 +282,7 @@ static NTSTATUS set_nt_acl_conn(const char *fname,
                DBG_ERR("init_files_struct failed: %s\n",
                        nt_errstr(status));
                if (fsp != NULL) {
-                       SMB_VFS_CLOSE(fsp);
+                       fd_close(fsp);
                }
                TALLOC_FREE(frame);
                return status;
@@ -293,7 +293,7 @@ static NTSTATUS set_nt_acl_conn(const char *fname,
                DEBUG(0,("set_nt_acl_no_snum: fset_nt_acl returned %s.\n", nt_errstr(status)));
        }
 
-       SMB_VFS_CLOSE(fsp);
+       fd_close(fsp);
 
        TALLOC_FREE(frame);
        return status;
@@ -631,7 +631,7 @@ static PyObject *py_smbd_chown(PyObject *self, PyObject *args, PyObject *kwargs)
                DBG_ERR("init_files_struct failed: %s\n",
                        nt_errstr(status));
                if (fsp != NULL) {
-                       SMB_VFS_CLOSE(fsp);
+                       fd_close(fsp);
                }
                TALLOC_FREE(frame);
                /*
@@ -644,13 +644,13 @@ static PyObject *py_smbd_chown(PyObject *self, PyObject *args, PyObject *kwargs)
        ret = SMB_VFS_FCHOWN(fsp, uid, gid);
        if (ret != 0) {
                int saved_errno = errno;
-               SMB_VFS_CLOSE(fsp);
+               fd_close(fsp);
                TALLOC_FREE(frame);
                errno = saved_errno;
                return PyErr_SetFromErrno(PyExc_OSError);
        }
 
-       SMB_VFS_CLOSE(fsp);
+       fd_close(fsp);
        TALLOC_FREE(frame);
 
        Py_RETURN_NONE;
@@ -1230,7 +1230,7 @@ static PyObject *py_smbd_create_file(PyObject *self, PyObject *args, PyObject *k
                DBG_ERR("init_files_struct failed: %s\n",
                        nt_errstr(status));
        } else if (fsp != NULL) {
-               SMB_VFS_CLOSE(fsp);
+               fd_close(fsp);
        }
 
        TALLOC_FREE(frame);
index a2bece1f5dd4858e67e43c98e4c0f215abc87111..72fa784d72bf86dc6288262c903dc3c2f625b3af 100644 (file)
@@ -429,7 +429,7 @@ static NTSTATUS cmd_open(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, c
        }
 
        if (!NT_STATUS_IS_OK(status)) {
-               SMB_VFS_CLOSE(fsp);
+               fd_close(fsp);
                TALLOC_FREE(fsp);
                TALLOC_FREE(smb_fname);
                return status;
@@ -513,7 +513,8 @@ static NTSTATUS cmd_pathfunc(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int arg
 
 static NTSTATUS cmd_close(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
 {
-       int fd, ret;
+       int fd;
+       NTSTATUS status;
 
        if (argc != 2) {
                printf("Usage: close <fd>\n");
@@ -526,15 +527,15 @@ static NTSTATUS cmd_close(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc,
                return NT_STATUS_OK;
        }
 
-       ret = SMB_VFS_CLOSE(vfs->files[fd]);
-       if (ret == -1 )
-               printf("close: error=%d (%s)\n", errno, strerror(errno));
+       status = fd_close(vfs->files[fd]);
+       if (!NT_STATUS_IS_OK(status))
+               printf("close: error=%s\n", nt_errstr(status));
        else
                printf("close: ok\n");
 
        TALLOC_FREE(vfs->files[fd]);
        vfs->files[fd] = NULL;
-       return NT_STATUS_OK;
+       return status;
 }
 
 
@@ -1816,9 +1817,9 @@ static NTSTATUS cmd_set_nt_acl(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int a
 out:
        TALLOC_FREE(sd);
 
-       ret = SMB_VFS_CLOSE(fsp);
-       if (ret == -1 )
-               printf("close: error=%d (%s)\n", errno, strerror(errno));
+       status = fd_close(fsp);
+       if (!NT_STATUS_IS_OK(status))
+               printf("close: error= (%s)\n", nt_errstr(status));
 
        TALLOC_FREE(fsp);