summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlan Coopersmith <alan.coopersmith@oracle.com>2023-03-28 12:08:41 -0700
committerAlan Coopersmith <alan.coopersmith@oracle.com>2023-03-28 12:10:58 -0700
commit324358180ddeaae482c5f66bb5647a4918710296 (patch)
tree19ff56ff256ec673a13b16d52b8264f064c0abf1
parent78eb3c75688a1e92ec435e5bc3ded063cd339a73 (diff)
downloadxorg-lib-libXaw-324358180ddeaae482c5f66bb5647a4918710296.tar.gz
Set close-on-exec when opening files
Signed-off-by: Alan Coopersmith <alan.coopersmith@oracle.com>
-rw-r--r--src/AsciiSrc.c14
-rw-r--r--src/MultiSrc.c12
-rw-r--r--src/TextPop.c9
3 files changed, 25 insertions, 10 deletions
diff --git a/src/AsciiSrc.c b/src/AsciiSrc.c
index 7833514..f493b06 100644
--- a/src/AsciiSrc.c
+++ b/src/AsciiSrc.c
@@ -55,6 +55,10 @@ in this Software without prior written authorization from The Open Group.
#include <sys/stat.h>
#include <fcntl.h>
+#ifndef O_CLOEXEC
+#define O_CLOEXEC 0
+#endif
+
#if (defined(ASCII_STRING) || defined(ASCII_DISK))
#include <X11/Xaw/AsciiText.h> /* for Widget Classes */
#endif
@@ -1280,7 +1284,7 @@ WriteToFile(String string, String name, unsigned length)
{
int fd;
- if ((fd = creat(name, 0666)) == -1)
+ if ((fd = open(name, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0666)) == -1)
return (False);
if (write(fd, string, length) == -1) {
@@ -1337,7 +1341,7 @@ WritePiecesToFile(AsciiSrcObject src, String name)
}
}
- if ((fd = creat(name, 0666)) == -1)
+ if ((fd = open(name, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0666)) == -1)
return (False);
for (piece = src->ascii_src.first_piece; piece; piece = piece->next)
@@ -1443,7 +1447,7 @@ InitStringOrFile(AsciiSrcObject src, Bool newString)
XtErrorMsg("NoFile", "asciiSourceCreate", "XawError",
"Creating a read only disk widget and no file specified.",
NULL, NULL);
- open_mode = O_RDONLY;
+ open_mode = O_RDONLY | O_CLOEXEC;
fdopen_mode = "r";
break;
case XawtextAppend:
@@ -1453,9 +1457,9 @@ InitStringOrFile(AsciiSrcObject src, Bool newString)
src->ascii_src.is_tempfile = True;
}
else {
-/* O_NOFOLLOW is a FreeBSD & Linux extension */
+/* O_NOFOLLOW was a FreeBSD & Linux extension, now adopted by POSIX */
#ifdef O_NOFOLLOW
- open_mode = O_RDWR | O_NOFOLLOW;
+ open_mode = O_RDWR | O_NOFOLLOW | O_CLOEXEC;
#else
open_mode = O_RDWR; /* unsafe; subject to race conditions */
#endif /* O_NOFOLLOW */
diff --git a/src/MultiSrc.c b/src/MultiSrc.c
index a9e84ba..5544b8b 100644
--- a/src/MultiSrc.c
+++ b/src/MultiSrc.c
@@ -75,6 +75,10 @@ in this Software without prior written authorization from The Open Group.
#include <sys/stat.h>
#include <fcntl.h>
+#ifndef O_CLOEXEC
+#define O_CLOEXEC 0
+#endif
+
#define MAGIC_VALUE ((XawTextPosition)-1)
#define streq(a, b) (strcmp((a), (b)) == 0)
@@ -1089,7 +1093,7 @@ WriteToFile(String string, String name)
int fd;
Bool result = True;
- if ((fd = creat(name, 0666)) == -1)
+ if ((fd = open(name, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0666)) == -1)
return (False);
if (write(fd, string, strlen(string)) == -1)
@@ -1215,7 +1219,7 @@ InitStringOrFile(MultiSrcObject src, Bool newString)
XtErrorMsg("NoFile", "multiSourceCreate", "XawError",
"Creating a read only disk widget and no file specified.",
NULL, 0);
- open_mode = O_RDONLY;
+ open_mode = O_RDONLY | O_CLOEXEC;
fdopen_mode = "r";
break;
case XawtextAppend:
@@ -1225,9 +1229,9 @@ InitStringOrFile(MultiSrcObject src, Bool newString)
src->multi_src.is_tempfile = True;
}
else {
-/* O_NOFOLLOW is a BSD & Linux extension */
+/* O_NOFOLLOW was a FreeBSD & Linux extension, now adopted by POSIX */
#ifdef O_NOFOLLOW
- open_mode = O_RDWR | O_NOFOLLOW;
+ open_mode = O_RDWR | O_NOFOLLOW | O_CLOEXEC;
#else
open_mode = O_RDWR; /* unsafe; subject to race conditions */
#endif
diff --git a/src/TextPop.c b/src/TextPop.c
index 9bf35ee..499c340 100644
--- a/src/TextPop.c
+++ b/src/TextPop.c
@@ -61,6 +61,12 @@ in this Software without prior written authorization from The Open Group.
#include <X11/Xaw/Toggle.h>
#include "XawI18n.h"
+#ifdef O_CLOEXEC
+#define FOPEN_CLOEXEC "e"
+#else
+#define FOPEN_CLOEXEC ""
+#endif
+
static _Xconst char* INSERT_FILE = "Enter Filename:";
static _Xconst char* SEARCH_LABEL_1 = "Use <Tab> to change fields.";
static _Xconst char* SEARCH_LABEL_2 = "Use ^q<Tab> for <Tab>.";
@@ -305,7 +311,8 @@ InsertFileNamed(Widget tw, String str)
XawTextBlock text;
XawTextPosition pos;
- if (str == NULL || strlen(str) == 0 || (file = fopen(str, "r")) == NULL)
+ if (str == NULL || strlen(str) == 0 ||
+ (file = fopen(str, "r" FOPEN_CLOEXEC)) == NULL)
return (False);
pos = XawTextGetInsertionPoint(tw);