summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPatrick McDermott <patrick.mcdermott@libiquity.com>2023-07-05 16:09:27 (EDT)
committer Patrick McDermott <patrick.mcdermott@libiquity.com>2023-07-05 16:43:13 (EDT)
commitd5ce0abc3ece376f2dc6571310d6944fc71bcb0b (patch)
tree8b09b16d25f0f0005bb0b823a9b861a8a5fd13cd
parent69bf47e45df055d3b8a818b2b8b7657767c9a171 (diff)
mknod: Fix specials file writing
-rw-r--r--configure.ac10
-rw-r--r--helpers/mknod.c38
2 files changed, 24 insertions, 24 deletions
diff --git a/configure.ac b/configure.ac
index 28e5d94..bb5bad8 100644
--- a/configure.ac
+++ b/configure.ac
@@ -43,12 +43,12 @@ test -d "${srcdir}/.git" || CFLAGS="${save_CFLAGS}"
funcs_missing=false
AC_CHECK_FUNCS(
[\
- calloc execve fclose fdopen feof ferror fopen fprintf fputc \
- fputs fread free fscanf fseek fwrite getgrgid getpwuid \
+ calloc execve fclose feof ferror fopen fprintf fputc fputs \
+ fread free fscanf fseek fstat fwrite getgrgid getpwuid \
localtime lstat malloc memcmp memcpy memset mkdir mkfifo open \
- printf puts readlink rename scandir sleep snprintf sprintf \
- stat strchr strcmp strcpy strftime strlen strncpy strtol \
- unlink vfprintf
+ printf puts readlink scandir sleep snprintf sprintf stat \
+ strchr strcmp strcpy strftime strlen strncpy strtol unlink \
+ vfprintf
],
[],
[funcs_missing=true])
diff --git a/helpers/mknod.c b/helpers/mknod.c
index 23683d8..e9e9737 100644
--- a/helpers/mknod.c
+++ b/helpers/mknod.c
@@ -251,51 +251,51 @@ main(int argc, char *argv[])
return EXIT_FAILURE;
opened:
- specials_file = fdopen(specials_fd, "a");
+ close(specials_fd);
+
+ specials_file = fopen(specials_file_name, "a");
if (specials_file == NULL) {
_err(_("Failed to open specials file stream"));
- close(specials_fd);
+ unlink(specials_lock_name);
free(specials_file_name);
unlink(name);
return EXIT_FAILURE;
}
- /* If "specials" doesn't exist, write header. */
- if (stat(specials_file_name, &stat_buf) != 0) {
- if (errno != ENOENT) {
- _err(_("Failed to stat specials file"));
- fclose(specials_file);
- free(specials_file_name);
- unlink(name);
- return EXIT_FAILURE;
- }
+ /* If "specials" is empty, write header. */
+ if (fstat(fileno(specials_file), &stat_buf) != 0) {
+ _err(_("Failed to stat specials file"));
+ fclose(specials_file);
+ unlink(specials_lock_name);
+ free(specials_file_name);
+ unlink(name);
+ return EXIT_FAILURE;
+ }
+ if (stat_buf.st_size == 0) {
if (fputs("version=1\n", specials_file) < 0 ||
fputs("type major minor name\n",
specials_file) < 0) {
_err(_("Failed to write specials file"));
fclose(specials_file);
+ unlink(specials_lock_name);
free(specials_file_name);
unlink(name);
return EXIT_FAILURE;
}
}
+
if (fprintf(specials_file, "%c %8ld %8ld %s\n", type, major, minor,
name) < 0) {
_err(_("Failed to write specials file"));
fclose(specials_file);
- free(specials_file_name);
- unlink(name);
- return EXIT_FAILURE;
- }
- fclose(specials_file);
-
- if (rename(specials_lock_name, specials_file_name) != 0) {
- _err(_("Failed to move specials file"));
+ unlink(specials_lock_name);
free(specials_file_name);
unlink(name);
return EXIT_FAILURE;
}
+ fclose(specials_file);
+ unlink(specials_lock_name);
free(specials_file_name);
return EXIT_SUCCESS;