From: Sami Kerola <kerolasa@iki.fi>
To: util-linux@vger.kernel.org
Cc: kerolasa@iki.fi
Subject: [PATCH 09/17] lib: add fileutils function collection
Date: Mon, 5 Mar 2012 20:38:46 +0100 [thread overview]
Message-ID: <1330976334-10751-10-git-send-email-kerolasa@iki.fi> (raw)
In-Reply-To: <1330976334-10751-1-git-send-email-kerolasa@iki.fi>
The fileutils contains xmkstemp function will create temporary file
safe and reusable manner.
Reference: http://www.dwheeler.com/secure-programs/Secure-Programs-HOWTO.html#TEMPORARY-FILES
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
include/Makefile.am | 5 +++--
include/fileutils.h | 6 ++++++
lib/Makefile.am | 2 ++
lib/fileutils.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 66 insertions(+), 2 deletions(-)
create mode 100644 include/fileutils.h
create mode 100644 lib/fileutils.c
diff --git a/include/Makefile.am b/include/Makefile.am
index 4f5453f..5e4e54e 100644
--- a/include/Makefile.am
+++ b/include/Makefile.am
@@ -11,6 +11,7 @@ dist_noinst_HEADERS = \
crc32.h \
env.h \
exitcodes.h \
+ fileutils.h \
fsprobe.h \
ismounted.h \
linux_reboot.h \
@@ -38,5 +39,5 @@ dist_noinst_HEADERS = \
wholedisk.h \
widechar.h \
writeall.h \
- xgetpass.h \
- xalloc.h
+ xalloc.h \
+ xgetpass.h
diff --git a/include/fileutils.h b/include/fileutils.h
new file mode 100644
index 0000000..27b5661
--- /dev/null
+++ b/include/fileutils.h
@@ -0,0 +1,6 @@
+#ifndef UTIL_LINUX_FILEUTILS
+#define UTIL_LINUX_FILEUTILS
+
+extern FILE * xmkstemp(char **tmpname);
+
+#endif
diff --git a/lib/Makefile.am b/lib/Makefile.am
index 19a00f5..c34481d 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -6,6 +6,7 @@ noinst_PROGRAMS = \
test_at \
test_blkdev \
test_canonicalize \
+ test_fileutils \
test_ismounted \
test_mangle \
test_procutils \
@@ -45,6 +46,7 @@ test_loopdev_SOURCES = \
test_loopdev_CFLAGS = -DTEST_PROGRAM_LOOPDEV
endif
+test_fileutils_SOURCES = fileutils.c
test_tt_SOURCES = tt.c $(top_srcdir)/lib/mbsalign.c
test_canonicalize_SOURCES = canonicalize.c
diff --git a/lib/fileutils.c b/lib/fileutils.c
new file mode 100644
index 0000000..b3b7438
--- /dev/null
+++ b/lib/fileutils.c
@@ -0,0 +1,55 @@
+/*
+ * Copyright (C) 2012 Sami Kerola <kerolasa@iki.fi>
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+#include "c.h"
+#include "pathnames.h"
+
+/* Create open temporary file in safe way. Please notice that the
+ * file permissions are -rw------- by default. */
+FILE *xmkstemp(char **tmpname)
+{
+ char *localtmp;
+ char *tmpenv;
+ mode_t old_mode;
+ int fd;
+ FILE *ret;
+
+ tmpenv = getenv("TMPDIR");
+ if (tmpenv)
+ asprintf(&localtmp, "%s/%s.XXXXXX", tmpenv,
+ program_invocation_short_name);
+ else
+ asprintf(&localtmp, "%s/%s.XXXXXX", _PATH_TMP,
+ program_invocation_short_name);
+ old_mode = umask(077);
+ fd = mkstemp(localtmp);
+ umask(old_mode);
+ if (fd == -1)
+ goto err;
+ if (!(ret = fdopen(fd, "w+")))
+ goto err;
+ *tmpname = localtmp;
+ return ret;
+ err:
+ close(fd);
+ return NULL;
+}
+
+#ifdef TEST_PROGRAM
+int main(void)
+{
+ FILE *f;
+ char *tmpname;
+ f = xmkstemp(&tmpname);
+ unlink(tmpname);
+ free(tmpname);
+ fclose(f);
+ return EXIT_FAILURE;
+}
+#endif
--
1.7.9.2
next prev parent reply other threads:[~2012-03-05 19:39 UTC|newest]
Thread overview: 29+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-03-04 13:35 [pull] fixes to todo items etc Sami Kerola
2012-03-05 12:38 ` Karel Zak
2012-03-05 19:38 ` Sami Kerola
2012-03-05 19:38 ` [PATCH 01/17] docs: add deprecation comments Sami Kerola
2012-03-05 19:38 ` [PATCH 02/17] docs: TODO removal, login-utils error printing Sami Kerola
2012-03-05 19:38 ` [PATCH 03/17] sfdisk: use rpmatch to yes/no question Sami Kerola
2012-03-05 19:38 ` [PATCH 04/17] mesg: " Sami Kerola
2012-03-05 19:38 ` [PATCH 05/17] vipw: " Sami Kerola
2012-03-05 19:38 ` [PATCH 06/17] docs: TODO removal, rpmatch task is done Sami Kerola
2012-03-05 19:38 ` [PATCH 07/17] chfn: use pathnames.h for paths Sami Kerola
2012-03-05 19:38 ` [PATCH 08/17] chsh: " Sami Kerola
2012-03-05 19:38 ` Sami Kerola [this message]
[not found] ` <"1330984305.2953.25.camel"@offbook>
[not found] ` <"1 330984305.2953.25.camel"@offbook>
2012-03-05 21:51 ` [PATCH 09/17] lib: add fileutils function collection Davidlohr Bueso
2012-03-09 7:56 ` Sami Kerola
2012-03-09 11:48 ` Davidlohr Bueso
2012-03-09 12:20 ` Karel Zak
2012-03-10 11:49 ` Sami Kerola
2012-03-05 19:38 ` [PATCH 10/17] wall: use xmkstemp for temporary file Sami Kerola
2012-03-05 19:38 ` [PATCH 11/17] setpwnam: use xmkstemp() and lckpwdf() Sami Kerola
2012-03-05 19:38 ` [PATCH 12/17] vipw: " Sami Kerola
2012-03-05 19:38 ` [PATCH 13/17] pathnames: clean up various user database paths Sami Kerola
2012-03-05 20:44 ` Sami Kerola
2012-03-05 19:38 ` [PATCH 14/17] docs: TODO removal, ldattach usage is done Sami Kerola
2012-03-05 19:38 ` [PATCH 15/17] include: add asprintf wrapper Sami Kerola
2012-03-05 19:52 ` Dave Reisner
2012-03-05 20:47 ` Sami Kerola
2012-03-05 19:38 ` [PATCH 16/17] xalloc: use xasprintf in all files Sami Kerola
2012-03-05 19:38 ` [PATCH 17/17] build-sys: fix chkdupexe regression Sami Kerola
2012-03-20 8:07 ` [pull] fixes to todo items etc Karel Zak
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
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1330976334-10751-10-git-send-email-kerolasa@iki.fi \
--to=kerolasa@iki.fi \
--cc=util-linux@vger.kernel.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