LCOV - code coverage report
Current view: top level - source3/smbd - fd_handle.c (source / functions) Hit Total Coverage
Test: coverage report for vadcx-master-patch-75612 fe003de8 Lines: 44 47 93.6 %
Date: 2024-02-29 22:57:05 Functions: 15 15 100.0 %

          Line data    Source code
       1             : /*
       2             :    Unix SMB/CIFS implementation.
       3             :    fd_handle structure handling
       4             :    Copyright (C) Ralph Boehme 2020
       5             : 
       6             :    This program is free software; you can redistribute it and/or modify
       7             :    it under the terms of the GNU General Public License as published by
       8             :    the Free Software Foundation; either version 3 of the License, or
       9             :    (at your option) any later version.
      10             : 
      11             :    This program is distributed in the hope that it will be useful,
      12             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      13             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      14             :    GNU General Public License for more details.
      15             : 
      16             :    You should have received a copy of the GNU General Public License
      17             :    along with this program.  If not, see <http://www.gnu.org/licenses/>.
      18             : */
      19             : 
      20             : #include "includes.h"
      21             : #include "fd_handle.h"
      22             : 
      23             : struct fd_handle {
      24             :         size_t ref_count;
      25             :         int fd;
      26             :         uint64_t position_information;
      27             :         off_t pos;
      28             :         /*
      29             :          * NT Create options, but we only look at
      30             :          * NTCREATEX_FLAG_DENY_DOS and
      31             :          * NTCREATEX_FLAG_DENY_FCB.
      32             :          */
      33             :         uint32_t private_options;
      34             :         uint64_t gen_id;
      35             : };
      36             : 
      37     6354952 : static int fd_handle_destructor(struct fd_handle *fh)
      38             : {
      39     6354952 :         SMB_ASSERT((fh->fd == -1) || (fh->fd == AT_FDCWD));
      40     6354952 :         return 0;
      41             : }
      42             : 
      43     6355006 : struct fd_handle *fd_handle_create(TALLOC_CTX *mem_ctx)
      44             : {
      45     6355006 :         struct fd_handle *fh = NULL;
      46             : 
      47     6355006 :         fh = talloc_zero(mem_ctx, struct fd_handle);
      48     6355006 :         if (fh == NULL) {
      49           0 :                 return NULL;
      50             :         }
      51     6355006 :         fh->fd = -1;
      52             : 
      53     6355006 :         talloc_set_destructor(fh, fd_handle_destructor);
      54             : 
      55     6355006 :         return fh;
      56             : }
      57             : 
      58    10727113 : size_t fh_get_refcount(struct fd_handle *fh)
      59             : {
      60    10727113 :         return fh->ref_count;
      61             : }
      62             : 
      63     6284248 : void fh_set_refcount(struct fd_handle *fh, size_t ref_count)
      64             : {
      65     6284248 :         fh->ref_count = ref_count;
      66     6284248 : }
      67             : 
      68       26830 : uint64_t fh_get_position_information(struct fd_handle *fh)
      69             : {
      70       26830 :         return fh->position_information;
      71             : }
      72             : 
      73       16040 : void fh_set_position_information(struct fd_handle *fh, uint64_t posinfo)
      74             : {
      75       16040 :         fh->position_information = posinfo;
      76       16040 : }
      77             : 
      78       16968 : off_t fh_get_pos(struct fd_handle *fh)
      79             : {
      80       16968 :         return fh->pos;
      81             : }
      82             : 
      83      155407 : void fh_set_pos(struct fd_handle *fh, off_t pos)
      84             : {
      85      155407 :         fh->pos = pos;
      86      155407 : }
      87             : 
      88      443775 : uint32_t fh_get_private_options(struct fd_handle *fh)
      89             : {
      90      443775 :         return fh->private_options;
      91             : }
      92             : 
      93      490646 : void fh_set_private_options(struct fd_handle *fh, uint32_t private_options)
      94             : {
      95      490646 :         fh->private_options = private_options;
      96      490646 : }
      97             : 
      98     1669864 : uint64_t fh_get_gen_id(struct fd_handle *fh)
      99             : {
     100     1669864 :         return fh->gen_id;
     101             : }
     102             : 
     103     6134256 : void fh_set_gen_id(struct fd_handle *fh, uint64_t gen_id)
     104             : {
     105     6134256 :         fh->gen_id = gen_id;
     106     6134256 : }
     107             : 
     108             : /****************************************************************************
     109             :  Helper functions for working with fsp->fh->fd
     110             : ****************************************************************************/
     111             : 
     112     1671115 : int fsp_get_io_fd(const struct files_struct *fsp)
     113             : {
     114     1671115 :         if (fsp->fsp_flags.is_pathref) {
     115           0 :                 DBG_ERR("fsp [%s] is a path referencing fsp\n",
     116             :                         fsp_str_dbg(fsp));
     117             : #ifdef DEVELOPER
     118           0 :                 smb_panic("fsp is a pathref");
     119             : #endif
     120             :                 return -1;
     121             :         }
     122             : 
     123     1671115 :         return fsp->fh->fd;
     124             : }
     125             : 
     126    57888774 : int fsp_get_pathref_fd(const struct files_struct *fsp)
     127             : {
     128    57888774 :         return fsp->fh->fd;
     129             : }
     130             : 
     131    20004527 : void fsp_set_fd(struct files_struct *fsp, int fd)
     132             : {
     133             :         /*
     134             :          * Deliberately allow setting an fd if the existing fd is the
     135             :          * same. This happens if a VFS module assigns the fd to
     136             :          * fsp->fh->fd in its openat VFS function. The canonical place
     137             :          * where the assignment is done is in fd_open(), but some VFS
     138             :          * modules do it anyway.
     139             :          */
     140             : 
     141    20004527 :         SMB_ASSERT(fsp->fh->fd == -1 ||
     142             :                    fsp->fh->fd == fd ||
     143             :                    fd == -1 ||
     144             :                    fd == AT_FDCWD);
     145             : 
     146    20004527 :         fsp->fh->fd = fd;
     147    20004527 : }

Generated by: LCOV version 1.14