LCOV - code coverage report
Current view: top level - src/port - noblock.c (source / functions) Coverage Total Hit
Test: PostgreSQL 20devel Lines: 35.7 % 14 5
Test Date: 2026-07-03 19:57:34 Functions: 50.0 % 2 1
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 25.0 % 8 2

             Branch data     Line data    Source code
       1                 :             : /*-------------------------------------------------------------------------
       2                 :             :  *
       3                 :             :  * noblock.c
       4                 :             :  *    set a file descriptor as blocking or non-blocking
       5                 :             :  *
       6                 :             :  * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
       7                 :             :  * Portions Copyright (c) 1994, Regents of the University of California
       8                 :             :  *
       9                 :             :  * IDENTIFICATION
      10                 :             :  *    src/port/noblock.c
      11                 :             :  *
      12                 :             :  *-------------------------------------------------------------------------
      13                 :             :  */
      14                 :             : 
      15                 :             : #include "c.h"
      16                 :             : 
      17                 :             : #include <fcntl.h>
      18                 :             : 
      19                 :             : 
      20                 :             : /*
      21                 :             :  * Put socket into nonblock mode.
      22                 :             :  * Returns true on success, false on failure.
      23                 :             :  */
      24                 :             : bool
      25                 :       15633 : pg_set_noblock(pgsocket sock)
      26                 :             : {
      27                 :             : #if !defined(WIN32)
      28                 :             :     int         flags;
      29                 :             : 
      30                 :       15633 :     flags = fcntl(sock, F_GETFL);
      31         [ -  + ]:       15633 :     if (flags < 0)
      32                 :           0 :         return false;
      33         [ -  + ]:       15633 :     if (fcntl(sock, F_SETFL, (flags | O_NONBLOCK)) == -1)
      34                 :           0 :         return false;
      35                 :       15633 :     return true;
      36                 :             : #else
      37                 :             :     unsigned long ioctlsocket_ret = 1;
      38                 :             : 
      39                 :             :     /* Returns non-0 on failure, while fcntl() returns -1 on failure */
      40                 :             :     return (ioctlsocket(sock, FIONBIO, &ioctlsocket_ret) == 0);
      41                 :             : #endif
      42                 :             : }
      43                 :             : 
      44                 :             : /*
      45                 :             :  * Put socket into blocking mode.
      46                 :             :  * Returns true on success, false on failure.
      47                 :             :  */
      48                 :             : bool
      49                 :           0 : pg_set_block(pgsocket sock)
      50                 :             : {
      51                 :             : #if !defined(WIN32)
      52                 :             :     int         flags;
      53                 :             : 
      54                 :           0 :     flags = fcntl(sock, F_GETFL);
      55         [ #  # ]:           0 :     if (flags < 0)
      56                 :           0 :         return false;
      57         [ #  # ]:           0 :     if (fcntl(sock, F_SETFL, (flags & ~O_NONBLOCK)) == -1)
      58                 :           0 :         return false;
      59                 :           0 :     return true;
      60                 :             : #else
      61                 :             :     unsigned long ioctlsocket_ret = 0;
      62                 :             : 
      63                 :             :     /* Returns non-0 on failure, while fcntl() returns -1 on failure */
      64                 :             :     return (ioctlsocket(sock, FIONBIO, &ioctlsocket_ret) == 0);
      65                 :             : #endif
      66                 :             : }
        

Generated by: LCOV version 2.0-1