Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set CLOEXEC flag when opening files and sockets #1206

Merged
merged 13 commits into from
Jan 6, 2023

Conversation

ensc
Copy link
Contributor

@ensc ensc commented Dec 27, 2022

Avoids leaking of file descriptors in multi threaded programs by:

  • setting O_CLOEXEC flag in rrd_open()
  • using the "e" flag for fopen() and providing a fallback implementation for environments without this flag

Solves #1202

Signed-off-by: Enrico Scholz <[email protected]>
@ensc ensc changed the title Feature cloexec Set CLOEXEC flag when opening files and sockest Dec 28, 2022
@ensc ensc changed the title Set CLOEXEC flag when opening files and sockest Set CLOEXEC flag when opening files and sockets Dec 28, 2022
ensc added 10 commits December 28, 2022 15:10
Although the "e" fopen() flag (atomic FD_CLOEXEC support) is scheduled
for being added to the next POSIX version, it is not supported by all
platforms.

Check whether it is accepted and working.

Because this flag can be tested at runtime only, configure.ac uses
AC_RUN_IFELSE.  Cross compiling fallback assumes that "e" is supported.

Signed-off-by: Enrico Scholz <[email protected]>
Signed-off-by: Enrico Scholz <[email protected]>
Just define 'O_CLOEXEC' as 0;  it is used always like in

| f = open(..., flags | O_CLOEXEC);

Signed-off-by: Enrico Scholz <[email protected]>
When fopen() does not support the "e" flag, parse the mode string and
run an 'open(..., O_CLOEXEC) + fdopen()' sequence when it is set.

Signed-off-by: Enrico Scholz <[email protected]>
Avoid leaking file descriptors by set the O_CLOEXEC flag.  This flag is
part of POSIX.1-2008 and there is implemented a fallback for systems
without it.

Signed-off-by: Enrico Scholz <[email protected]>
Signed-off-by: Enrico Scholz <[email protected]>
Signed-off-by: Enrico Scholz <[email protected]>
@ensc ensc marked this pull request as ready for review December 29, 2022 11:06
@ensc
Copy link
Contributor Author

ensc commented Dec 29, 2022

I do not know why the linux builds in the CI are failing; they succeed locally. Perhaps a problem with the github buildsystem.

@oetiker
Copy link
Owner

oetiker commented Jan 4, 2023

looking into the test issue ... guess this might be unrelated to your changes

@codecov-commenter
Copy link

codecov-commenter commented Jan 4, 2023

Codecov Report

Merging #1206 (3f45243) into master (f0b2954) will increase coverage by 0.01%.
The diff coverage is 44.44%.

@@            Coverage Diff             @@
##           master    #1206      +/-   ##
==========================================
+ Coverage   50.98%   50.99%   +0.01%     
==========================================
  Files          45       45              
  Lines       18043    18043              
==========================================
+ Hits         9199     9201       +2     
+ Misses       8844     8842       -2     
Impacted Files Coverage Δ
src/rrd_cgi.c 0.00% <0.00%> (ø)
src/rrd_graph.c 51.60% <0.00%> (+0.02%) ⬆️
src/rrd_xport.c 58.71% <0.00%> (ø)
src/rrd_daemon.c 47.85% <33.33%> (ø)
src/rrd_client.c 62.87% <100.00%> (ø)
src/rrd_dump.c 63.10% <100.00%> (ø)
src/rrd_open.c 59.35% <100.00%> (ø)
src/rrd_modify.c 68.96% <0.00%> (+0.14%) ⬆️

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@oetiker oetiker merged commit 6d158d1 into oetiker:master Jan 6, 2023
#include <stdlib.h>
#include <string.h>

#ifdef _POSIX_C_SOURCE
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ensc Could you please check:

#ifdef _POSIX_C_SOURCE
#  include <fcntl.h>

In msys2 mingw-w64 builds we see these warnings e.g. [1]:

compat-cloexec.c:11: warning: "O_CREAT" redefined
   11 | #  define O_CREAT       0
      | 
In file included from ./rrd_config_bottom.h:64,
                 from ./rrd_config.h:634,
                 from compat-cloexec.h:4,
                 from compat-cloexec.c:1:
D:/a/_temp/msys64/mingw64/include/fcntl.h:40: note: this is the location of the previous definition
   40 | #define O_CREAT _O_CREAT
      | 
compat-cloexec.c:12: warning: "O_WRONLY" redefined
   12 | #  define O_WRONLY      0
      | 
D:/a/_temp/msys64/mingw64/include/fcntl.h:37: note: this is the location of the previous definition
   37 | #define O_WRONLY _O_WRONLY
      | 
compat-cloexec.c:13: warning: "O_RDONLY" redefined
   13 | #  define O_RDONLY      0
      | 
D:/a/_temp/msys64/mingw64/include/fcntl.h:36: note: this is the location of the previous definition
   36 | #define O_RDONLY _O_RDONLY
      | 
compat-cloexec.c:14: warning: "O_APPEND" redefined
   14 | #  define O_APPEND      0
      | 
D:/a/_temp/msys64/mingw64/include/fcntl.h:39: note: this is the location of the previous definition
   39 | #define O_APPEND _O_APPEND
      | 
compat-cloexec.c:15: warning: "O_RDWR" redefined
   15 | #  define O_RDWR        0
      | 
D:/a/_temp/msys64/mingw64/include/fcntl.h:38: note: this is the location of the previous definition
   38 | #define O_RDWR _O_RDWR
      | 

configure says:

checking for fcntl.h... yes

So, HAVE_FCNTL_H is defined.

[1] https://github.com/oetiker/rrdtool-1.x/actions/runs/4740218558/jobs/8415805010#step:5:901

#include <stdlib.h>
#include <stdbool.h>

#ifdef _POSIX_C_SOURCE
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ensc Warnings under msys2 mingw-w64 as in compat-cloexec.c:

  CC       test_compat-cloexec.o
test_compat-cloexec.c:10: warning: "O_RDONLY" redefined
   10 | #  define O_RDONLY      0
      | 
In file included from ../src/rrd_config_bottom.h:64,
                 from ../src/rrd_config.h:634,
                 from ../src/compat-cloexec.h:4,
                 from test_compat-cloexec.c:1:
C:/msys64/mingw64/include/fcntl.h:36: note: this is the location of the previous definition
   36 | #define O_RDONLY _O_RDONLY
      | 
test_compat-cloexec.c:11: warning: "O_RDWR" redefined
   11 | #  define O_RDWR        0
      | 
C:/msys64/mingw64/include/fcntl.h:38: note: this is the location of the previous definition
   38 | #define O_RDWR _O_RDWR
      | 
test_compat-cloexec.c:12: warning: "O_APPEND" redefined
   12 | #  define O_APPEND      0
      | 
C:/msys64/mingw64/include/fcntl.h:39: note: this is the location of the previous definition
   39 | #define O_APPEND _O_APPEND
      | 
  CC       compat-cloexec.o
../src/compat-cloexec.c:11: warning: "O_CREAT" redefined
   11 | #  define O_CREAT       0
      | 
In file included from ../src/rrd_config_bottom.h:64,
                 from ../src/rrd_config.h:634,
                 from ../src/compat-cloexec.h:4,
                 from ../src/compat-cloexec.c:1:
C:/msys64/mingw64/include/fcntl.h:40: note: this is the location of the previous definition
   40 | #define O_CREAT _O_CREAT
      | 
../src/compat-cloexec.c:12: warning: "O_WRONLY" redefined
   12 | #  define O_WRONLY      0
      | 
C:/msys64/mingw64/include/fcntl.h:37: note: this is the location of the previous definition
   37 | #define O_WRONLY _O_WRONLY
      | 
../src/compat-cloexec.c:13: warning: "O_RDONLY" redefined
   13 | #  define O_RDONLY      0
      | 
C:/msys64/mingw64/include/fcntl.h:36: note: this is the location of the previous definition
   36 | #define O_RDONLY _O_RDONLY
      | 
../src/compat-cloexec.c:14: warning: "O_APPEND" redefined
   14 | #  define O_APPEND      0
      | 
C:/msys64/mingw64/include/fcntl.h:39: note: this is the location of the previous definition
   39 | #define O_APPEND _O_APPEND
      | 
../src/compat-cloexec.c:15: warning: "O_RDWR" redefined
   15 | #  define O_RDWR        0
      | 
C:/msys64/mingw64/include/fcntl.h:38: note: this is the location of the previous definition
   38 | #define O_RDWR _O_RDWR
      | 
  CCLD     compat-cloexec.exe

FILE *f;

f = _rrd_fopen("/dev/null", "r");
check_file(f, O_RDONLY, false, __LINE__);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test fails under msys2 mingw-w64:

test_compat-cloexec.c:59 fopen() failed
FAIL compat-cloexec.exe (exit status: 3)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we skip it on windows ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants