aboutsummaryrefslogtreecommitdiffstats
path: root/mtools
diff options
context:
space:
mode:
Diffstat (limited to 'mtools')
-rwxr-xr-xmtools/syslinux.c44
1 files changed, 35 insertions, 9 deletions
diff --git a/mtools/syslinux.c b/mtools/syslinux.c
index 937f6b87..ac189c61 100755
--- a/mtools/syslinux.c
+++ b/mtools/syslinux.c
@@ -1,6 +1,7 @@
/* ----------------------------------------------------------------------- *
*
* Copyright 1998-2008 H. Peter Anvin - All Rights Reserved
+ * Copyright 2010 Intel Corporation; author: H. Peter Anvin
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -18,8 +19,7 @@
* We need device write permission anyway.
*/
-#define _XOPEN_SOURCE 500 /* Required on glibc 2.x */
-#define _BSD_SOURCE
+#define _GNU_SOURCE
#include <alloca.h>
#include <errno.h>
#include <fcntl.h>
@@ -51,6 +51,12 @@ void __attribute__ ((noreturn)) die(const char *msg)
exit(1);
}
+void __attribute__ ((noreturn)) die_err(const char *msg)
+{
+ fprintf(stderr, "%s: %s: %s\n", program, msg, strerror(errno));
+ exit(1);
+}
+
/*
* read/write wrapper functions
*/
@@ -124,7 +130,8 @@ int main(int argc, char *argv[])
int dev_fd;
struct stat st;
int status;
- char mtools_conf[] = "/tmp/syslinux-mtools-XXXXXX";
+ const char *tmpdir;
+ char *mtools_conf;
int mtc_fd;
FILE *mtc, *mtp;
struct libfat_filesystem *fs;
@@ -155,12 +162,26 @@ int main(int argc, char *argv[])
}
/*
+ * Temp directory of choice...
+ */
+ tmpdir = getenv("TMPDIR");
+ if (!tmpdir) {
+#ifdef P_tmpdir
+ tmpdir = P_tmpdir;
+#elif defined(_PATH_TMP)
+ tmpdir = _PATH_TMP;
+#else
+ tmpdir = "/tmp";
+#endif
+ }
+
+ /*
* First make sure we can open the device at all, and that we have
* read/write permission.
*/
dev_fd = open(opt.device, O_RDWR);
if (dev_fd < 0 || fstat(dev_fd, &st) < 0) {
- perror(opt.device);
+ die_err(opt.device);
exit(1);
}
@@ -183,11 +204,14 @@ int main(int argc, char *argv[])
/*
* Create an mtools configuration file
*/
+ if (asprintf(&mtools_conf, "%s//syslinux-mtools-XXXXXX", tmpdir) < 0 ||
+ !mtools_conf)
+ die_err(tmpdir);
+
mtc_fd = mkstemp(mtools_conf);
- if (mtc_fd < 0 || !(mtc = fdopen(mtc_fd, "w"))) {
- perror(program);
- exit(1);
- }
+ if (mtc_fd < 0 || !(mtc = fdopen(mtc_fd, "w")))
+ die_err(mtools_conf);
+
fprintf(mtc,
/* These are needed for some flash memories */
"MTOOLS_SKIP_CHECK=1\n"
@@ -197,7 +221,9 @@ int main(int argc, char *argv[])
" offset=%llu\n",
(unsigned long)mypid,
dev_fd, (unsigned long long)opt.offset);
- fclose(mtc);
+
+ if (ferror(mtc) || fclose(mtc))
+ die_err(mtools_conf);
/*
* Run mtools to create the LDLINUX.SYS file