From: Bruno Haible <bruno@clisp.org>
To: bug-gnulib@gnu.org
Subject: immutable: Fix initialization failure on Android
Date: Tue, 10 Jan 2023 17:44:55 +0100 [thread overview]
Message-ID: <5273256.097qLLJo0e@nimes> (raw)
On Android, within the Termux app, I see this test failure:
FAIL: test-immutable.sh
=======================
glimm: Cannot open /tmp/glimmdata-5358-1804289383!
Aborted
test-immutable.sh: test case 1 failed
glimm: Cannot open /tmp/glimmdata-5364-1804289383!
Aborted
test-immutable.sh: test case 2 failed
glimm: Cannot open /tmp/glimmdata-5376-1804289383!
Aborted
test-immutable.sh: test case 3 failed
glimm: Cannot open /tmp/glimmdata-5384-1804289383!
Aborted
test-immutable.sh: test case 4 failed
FAIL test-immutable.sh (exit status: 1)
The cause is that /tmp does not exist, in this environment. But TMPDIR
is set to a sensible value.
This patch fixes the failure.
2023-01-10 Bruno Haible <bruno@clisp.org>
immutable: Fix initialization failure on Android.
* lib/immutable.c: Include "pathmax.h".
(do_init_mmap_file): Use $TMPDIR rather than /tmp, if it is safe.
* modules/immutable (Depends-on): Add pathmax.
diff --git a/lib/immutable.c b/lib/immutable.c
index d5fb09b7d6..5881fc2252 100644
--- a/lib/immutable.c
+++ b/lib/immutable.c
@@ -70,6 +70,12 @@ extern
# include <unistd.h>
# include <fcntl.h>
+/* Get PATH_MAX. */
+# include "pathmax.h"
+# ifndef PATH_MAX
+# define PATH_MAX 4096
+# endif
+
# include "glthread/lock.h"
# endif
@@ -116,14 +122,22 @@ static long file_length;
static void
do_init_mmap_file (void)
{
- char filename[100];
- sprintf (filename, "%s/glimmdata-%d-%ld", "/tmp", getpid (), random ());
+ /* Use TMPDIR, except if it is too long. */
+ const char *tmpdir = getenv ("TMPDIR");
+ if (tmpdir == NULL || strlen (tmpdir) > PATH_MAX)
+ tmpdir = "/tmp";
+ /* Now strlen (tmpdir) <= PATH_MAX. */
+
+ char filename[PATH_MAX + 1 + 41 + 1];
+ sprintf (filename, "%s/glimmdata-%d-%ld", tmpdir, getpid (), random ());
+
file_fd = open (filename, O_CREAT | O_TRUNC | O_RDWR | O_CLOEXEC, 0700);
if (file_fd < 0)
{
fprintf (stderr, "glimm: Cannot open %s!\n", filename);
abort ();
}
+
/* Remove the file from the file system as soon as possible, to make
sure there is no leftover after this process terminates or crashes. */
unlink (filename);
diff --git a/modules/immutable b/modules/immutable
index fef376ac07..0b9bc3a161 100644
--- a/modules/immutable
+++ b/modules/immutable
@@ -11,6 +11,7 @@ m4/nocrash.m4
Depends-on:
stdint
+pathmax
open
ssfmalloc
reply other threads:[~2023-01-10 18:24 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://lists.gnu.org/mailman/listinfo/bug-gnulib
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=5273256.097qLLJo0e@nimes \
--to=bruno@clisp.org \
--cc=bug-gnulib@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).