diff --git a/lib/Makefile.am b/lib/Makefile.am index 84948f4a2..a3a4024f7 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -105,6 +105,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 \ diff --git a/lib/fs/mkstemp/fmkomstemp.h b/lib/fs/mkstemp/fmkomstemp.h index 4770bc92a..4c59ea05d 100644 --- a/lib/fs/mkstemp/fmkomstemp.h +++ b/lib/fs/mkstemp/fmkomstemp.h @@ -9,11 +9,11 @@ #include #include -#include -#include #include #include +#include "fs/mkstemp/mkomstemp.h" + inline FILE *fmkomstemp(char *template, unsigned int flags, mode_t m); @@ -24,13 +24,10 @@ 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 index 000000000..9c6e3206c --- /dev/null +++ b/lib/fs/mkstemp/mkomstemp.c @@ -0,0 +1,12 @@ +// SPDX-FileCopyrightText: 2024, Alejandro Colomar +// SPDX-License-Identifier: BSD-3-Clause + + +#include + +#include "fs/mkstemp/mkomstemp.h" + +#include + + +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 index 000000000..5d55f9f89 --- /dev/null +++ b/lib/fs/mkstemp/mkomstemp.h @@ -0,0 +1,40 @@ +// SPDX-FileCopyrightText: 2024, Alejandro Colomar +// SPDX-License-Identifier: BSD-3-Clause + + +#ifndef SHADOW_INCLUDE_LIB_FS_MKSTEMP_MKOMSTEMP_H_ +#define SHADOW_INCLUDE_LIB_FS_MKSTEMP_MKOMSTEMP_H_ + + +#include + +#include +#include +#include +#include + + +inline int mkomstemp(char *template, unsigned int flags, mode_t m); + + +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