LCOV - code coverage report
Current view: top level - lib/util - close_low_fd.c (source / functions) Hit Total Coverage
Test: coverage report for v4-17-test 1498b464 Lines: 11 20 55.0 %
Date: 2024-06-13 04:01:37 Functions: 1 1 100.0 %

          Line data    Source code
       1             : /*
       2             :  * Unix SMB/CIFS implementation.
       3             :  * Samba utility functions
       4             :  * Copyright (C) Volker Lendecke 2014
       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 "replace.h"
      21             : #include "system/filesys.h"
      22             : #include "close_low_fd.h"
      23             : 
      24         125 : _PUBLIC_ int close_low_fd(int fd)
      25             : {
      26             : #ifndef VALGRIND
      27             :         int ret, dev_null;
      28             : 
      29         125 :         dev_null = open("/dev/null", O_RDWR, 0);
      30             : 
      31         125 :         if ((dev_null == -1) && (errno == ENFILE)) {
      32             :                 /*
      33             :                  * Try to free up an fd
      34             :                  */
      35           0 :                 ret = close(fd);
      36           0 :                 if (ret != 0) {
      37           0 :                         return errno;
      38             :                 }
      39             :         }
      40             : 
      41         125 :         dev_null = open("/dev/null", O_RDWR, 0);
      42         125 :         if (dev_null == -1) {
      43           0 :                 dev_null = open("/dev/null", O_WRONLY, 0);
      44             :         }
      45         125 :         if (dev_null == -1) {
      46           0 :                 return errno;
      47             :         }
      48             : 
      49         125 :         if (dev_null == fd) {
      50             :                 /*
      51             :                  * This can happen in the ENFILE case above
      52             :                  */
      53           0 :                 return 0;
      54             :         }
      55             : 
      56         125 :         ret = dup2(dev_null, fd);
      57         125 :         if (ret == -1) {
      58           0 :                 int err = errno;
      59           0 :                 close(dev_null);
      60           0 :                 return err;
      61             :         }
      62         125 :         close(dev_null);
      63             : #endif
      64         125 :         return 0;
      65             : }

Generated by: LCOV version 1.13