diff options
author | Alan Coopersmith <alan.coopersmith@oracle.com> | 2023-03-27 18:35:46 -0700 |
---|---|---|
committer | Alan Coopersmith <alan.coopersmith@oracle.com> | 2023-04-22 17:15:24 +0000 |
commit | 4524c578581b427145ae136844fc655a89e94777 (patch) | |
tree | 12d368ed993e0e98786af558bc92e1e39251b5bc | |
parent | f131de92d6c4e2f62934e85b012287276ecf009c (diff) |
Set close-on-exec when opening files
Relies on platforms with O_CLOEXEC support following POSIX requirement
to not copy the close-on-exec flag to the new fd in dup2(), but to leave
it unset instead, since that's how fd's are passed to child processes
to handled compressed files.
Signed-off-by: Alan Coopersmith <alan.coopersmith@oracle.com>
-rw-r--r-- | src/RdFToBuf.c | 2 | ||||
-rw-r--r-- | src/RdFToI.c | 6 | ||||
-rw-r--r-- | src/WrFFrBuf.c | 2 | ||||
-rw-r--r-- | src/WrFFrI.c | 2 | ||||
-rw-r--r-- | src/XpmI.h | 7 | ||||
-rw-r--r-- | src/rgb.c | 2 | ||||
-rw-r--r-- | test/XpmRead.c | 6 | ||||
-rw-r--r-- | test/XpmWrite.c | 6 |
8 files changed, 24 insertions, 9 deletions
diff --git a/src/RdFToBuf.c b/src/RdFToBuf.c index 1b386f8..4ee9265 100644 --- a/src/RdFToBuf.c +++ b/src/RdFToBuf.c @@ -72,7 +72,7 @@ XpmReadFileToBuffer( *buffer_return = NULL; #ifndef VAX11C - fd = open(filename, O_RDONLY); + fd = open(filename, O_RDONLY | O_CLOEXEC); #else fd = open(filename, O_RDONLY, NULL); #endif diff --git a/src/RdFToI.c b/src/RdFToI.c index a16af88..28c0c33 100644 --- a/src/RdFToI.c +++ b/src/RdFToI.c @@ -212,7 +212,7 @@ OpenReadFile( mdata->stream.file = (stdin); mdata->type = XPMFILE; } else { - int fd = open(filename, O_RDONLY); + int fd = open(filename, O_RDONLY | O_CLOEXEC); #if defined(NO_ZPIPE) if ( fd < 0 ) return XpmOpenFailed; @@ -229,11 +229,11 @@ OpenReadFile( return (XpmNoMemory); strcpy(compressfile, filename); strcpy(compressfile + len, ext = ".Z"); - fd = open(compressfile, O_RDONLY); + fd = open(compressfile, O_RDONLY | O_CLOEXEC); if ( fd < 0 ) { strcpy(compressfile + len, ext = ".gz"); - fd = open(compressfile, O_RDONLY); + fd = open(compressfile, O_RDONLY | O_CLOEXEC); if ( fd < 0 ) { XpmFree(compressfile); diff --git a/src/WrFFrBuf.c b/src/WrFFrBuf.c index 0e57cc8..085b5a8 100644 --- a/src/WrFFrBuf.c +++ b/src/WrFFrBuf.c @@ -45,7 +45,7 @@ XpmWriteFileFromBuffer( char *buffer) { size_t fcheck, len; - FILE *fp = fopen(filename, "w"); + FILE *fp = fopen(filename, "w" FOPEN_CLOEXEC); if (!fp) return XpmOpenFailed; diff --git a/src/WrFFrI.c b/src/WrFFrI.c index 234197a..6855c84 100644 --- a/src/WrFFrI.c +++ b/src/WrFFrI.c @@ -336,7 +336,7 @@ OpenWriteFile( #ifndef NO_ZPIPE size_t len; #endif - int fd = open(filename, O_WRONLY|O_CREAT|O_TRUNC, 0644); + int fd = open(filename, O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0644); if ( fd < 0 ) return(XpmOpenFailed); #ifndef NO_ZPIPE @@ -103,6 +103,13 @@ extern FILE *popen(); # endif #endif +#ifdef O_CLOEXEC +# define FOPEN_CLOEXEC "e" +#else +# define FOPEN_CLOEXEC "" +# define O_CLOEXEC 0 +#endif + #define XPMMAXCMTLEN BUFSIZ typedef struct { unsigned int type; @@ -66,7 +66,7 @@ xpmReadRgbNames( xpmRgbName *rgb; /* Open the rgb text file. Abort if error. */ - if ((rgbf = fopen(rgb_fname, "r")) == NULL) + if ((rgbf = fopen(rgb_fname, "r" FOPEN_CLOEXEC)) == NULL) return 0; /* Loop reading each line in the file. */ diff --git a/test/XpmRead.c b/test/XpmRead.c index df7a826..a231419 100644 --- a/test/XpmRead.c +++ b/test/XpmRead.c @@ -33,6 +33,10 @@ #include "TestAllFiles.h" +#ifndef O_CLOEXEC +# define O_CLOEXEC 0 +#endif + #ifndef g_assert_no_errno /* defined in glib 2.66 & later */ #define g_assert_no_errno(n) g_assert_cmpint(n, >=, 0) #endif @@ -147,7 +151,7 @@ TestReadFileToBuffer(const gchar *filepath) g_assert_nonnull(buffer); /* Read file ourselves and verify the data matches */ - g_assert_no_errno(fd = open(filepath, O_RDONLY)); + g_assert_no_errno(fd = open(filepath, O_RDONLY | O_CLOEXEC)); while ((rd = read(fd, readbuf, sizeof(readbuf))) > 0) { g_assert_cmpmem(b, rd, readbuf, rd); b += rd; diff --git a/test/XpmWrite.c b/test/XpmWrite.c index 49b5b21..998b1bb 100644 --- a/test/XpmWrite.c +++ b/test/XpmWrite.c @@ -38,6 +38,10 @@ #include "TestAllFiles.h" #include "CompareXpmImage.h" +#ifndef O_CLOEXEC +# define O_CLOEXEC 0 +#endif + #ifndef g_assert_no_errno /* defined in glib 2.66 & later */ #define g_assert_no_errno(n) g_assert_cmpint(n, >=, 0) #endif @@ -295,7 +299,7 @@ TestWriteFileFromBuffer(const gchar *filepath) ssize_t rd; /* Read file ourselves and verify the data matches */ - g_assert_no_errno(fd = open(newfilepath, O_RDONLY)); + g_assert_no_errno(fd = open(newfilepath, O_RDONLY | O_CLOEXEC)); while ((rd = read(fd, readbuf, sizeof(readbuf))) > 0) { g_assert_cmpmem(b, rd, readbuf, rd); b += rd; |