]> git.ipfire.org Git - thirdparty/shadow.git/commitdiff
lib/fs/mkstemp/: mkostemp(): Split API from fmkomstemp()
authorAlejandro Colomar <alx@kernel.org>
Wed, 4 Dec 2024 12:47:37 +0000 (13:47 +0100)
committerSerge Hallyn <serge@hallyn.com>
Tue, 3 Jun 2025 14:49:44 +0000 (09:49 -0500)
This reduces the complexity of fmkomstemp().

Signed-off-by: Alejandro Colomar <alx@kernel.org>
lib/Makefile.am
lib/fs/mkstemp/fmkomstemp.h
lib/fs/mkstemp/mkomstemp.c [new file with mode: 0644]
lib/fs/mkstemp/mkomstemp.h [new file with mode: 0644]

index 0cc39310481774d49e8adcdbeab67335bf4c3861..d9641b94607dd608941cb6150e79f80ef9999e74 100644 (file)
@@ -102,6 +102,8 @@ libshadow_la_SOURCES = \
        fputsx.c \
        fs/mkstemp/fmkomstemp.c \
        fs/mkstemp/fmkomstemp.h \
+       fs/mkstemp/mkomstemp.c \
+       fs/mkstemp/mkomstemp.h \
        fs/readlink/areadlink.c \
        fs/readlink/areadlink.h \
        fs/readlink/readlinknul.c \
index 4770bc92a7c45215da84977ed40804b5b58285b9..c0649d99ede8da98ad934779edb466261dc67f9b 100644 (file)
@@ -9,28 +9,26 @@
 #include <config.h>
 
 #include <stdio.h>
-#include <stdlib.h>
-#include <sys/stat.h>
 #include <sys/types.h>
 #include <unistd.h>
 
+#include "fs/mkstemp/mkomstemp.h"
+
 
 inline FILE *fmkomstemp(char *template, unsigned int flags, mode_t m);
 
 
+// FILE make with-open(2)-flags with-mode secure temporary
 inline FILE *
 fmkomstemp(char *template, unsigned int flags, mode_t m)
 {
        int   fd;
        FILE  *fp;
 
-       fd = mkostemp(template, flags);
+       fd = mkomstemp(template, flags, m);
        if (fd == -1)
                return NULL;
 
-       if (fchmod(fd, m) == -1)
-               goto fail;
-
        fp = fdopen(fd, "w");
        if (fp == NULL)
                goto fail;
diff --git a/lib/fs/mkstemp/mkomstemp.c b/lib/fs/mkstemp/mkomstemp.c
new file mode 100644 (file)
index 0000000..9c6e320
--- /dev/null
@@ -0,0 +1,12 @@
+// SPDX-FileCopyrightText: 2024, Alejandro Colomar <alx@kernel.org>
+// SPDX-License-Identifier: BSD-3-Clause
+
+
+#include <config.h>
+
+#include "fs/mkstemp/mkomstemp.h"
+
+#include <sys/types.h>
+
+
+extern inline int mkomstemp(char *template, unsigned int flags, mode_t m);
diff --git a/lib/fs/mkstemp/mkomstemp.h b/lib/fs/mkstemp/mkomstemp.h
new file mode 100644 (file)
index 0000000..6ba5aa3
--- /dev/null
@@ -0,0 +1,41 @@
+// SPDX-FileCopyrightText: 2024, Alejandro Colomar <alx@kernel.org>
+// SPDX-License-Identifier: BSD-3-Clause
+
+
+#ifndef SHADOW_INCLUDE_LIB_FS_MKSTEMP_MKOMSTEMP_H_
+#define SHADOW_INCLUDE_LIB_FS_MKSTEMP_MKOMSTEMP_H_
+
+
+#include <config.h>
+
+#include <stdlib.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+
+inline int mkomstemp(char *template, unsigned int flags, mode_t m);
+
+
+// make with-open(2)-like-flags with-mode secure temporary
+inline int
+mkomstemp(char *template, unsigned int flags, mode_t m)
+{
+       int  fd;
+
+       fd = mkostemp(template, flags);
+       if (fd == -1)
+               return -1;
+
+       if (fchmod(fd, m) == -1)
+               goto fail;
+
+       return fd;
+fail:
+       close(fd);
+       unlink(template);
+       return -1;
+}
+
+
+#endif  // include guard