diff options
author | Karel Zak | 2014-06-18 12:57:42 +0200 |
---|---|---|
committer | Karel Zak | 2014-06-18 12:57:42 +0200 |
commit | f272b32c57dcfd440f01c2eae119dce1aea5611e (patch) | |
tree | d9d814f7e719aee08d70b41d6a67bfd369684399 | |
parent | lscpu: fix ifdef HAVE_sys_io_h [checkconfig] (diff) | |
download | kernel-qcow2-util-linux-f272b32c57dcfd440f01c2eae119dce1aea5611e.tar.gz kernel-qcow2-util-linux-f272b32c57dcfd440f01c2eae119dce1aea5611e.tar.xz kernel-qcow2-util-linux-f272b32c57dcfd440f01c2eae119dce1aea5611e.zip |
lib/fileutils: xalloc stuff in library-like code
Signed-off-by: Karel Zak <kzak@redhat.com>
-rw-r--r-- | include/fileutils.h | 5 | ||||
-rw-r--r-- | lib/fileutils.c | 11 |
2 files changed, 10 insertions, 6 deletions
diff --git a/include/fileutils.h b/include/fileutils.h index 2144e5e5d..98798f7ee 100644 --- a/include/fileutils.h +++ b/include/fileutils.h @@ -7,10 +7,11 @@ static inline FILE *xfmkstemp(char **tmpname, char *dir) { int fd; FILE *ret; + fd = xmkstemp(tmpname, dir); - if (fd == -1) { + if (fd == -1) return NULL; - } + if (!(ret = fdopen(fd, "w+" UL_CLOEXECSTR))) { close(fd); return NULL; diff --git a/lib/fileutils.c b/lib/fileutils.c index a1b475757..bffa8ff34 100644 --- a/lib/fileutils.c +++ b/lib/fileutils.c @@ -12,7 +12,6 @@ #include "c.h" #include "fileutils.h" #include "pathnames.h" -#include "xalloc.h" /* Create open temporary file in safe way. Please notice that the * file permissions are -rw------- by default. */ @@ -21,7 +20,7 @@ int xmkstemp(char **tmpname, char *dir) char *localtmp; char *tmpenv; mode_t old_mode; - int fd; + int fd, rc; /* Some use cases must be capable of being moved atomically * with rename(2), which is the reason why dir is here. */ @@ -31,11 +30,15 @@ int xmkstemp(char **tmpname, char *dir) tmpenv = getenv("TMPDIR"); if (tmpenv) - xasprintf(&localtmp, "%s/%s.XXXXXX", tmpenv, + rc = asprintf(&localtmp, "%s/%s.XXXXXX", tmpenv, program_invocation_short_name); else - xasprintf(&localtmp, "%s/%s.XXXXXX", _PATH_TMP, + rc = asprintf(&localtmp, "%s/%s.XXXXXX", _PATH_TMP, program_invocation_short_name); + + if (rc < 0) + return -1; + old_mode = umask(077); fd = mkostemp(localtmp, O_RDWR|O_CREAT|O_EXCL|O_CLOEXEC); umask(old_mode); |