summaryrefslogtreecommitdiff
path: root/src/lib/libc/stdlib/mkstemp.c
diff options
context:
space:
mode:
authormillert <>2024-01-19 19:45:02 +0000
committermillert <>2024-01-19 19:45:02 +0000
commitbe8f1fea763f42a1109f6b1eb3f56ae521cfdec3 (patch)
tree469de603d2d83787131e234cccbf7802f902cf67 /src/lib/libc/stdlib/mkstemp.c
parent1d17a25a597033d38c420a0a3add7e5e82dd4c02 (diff)
downloadopenbsd-be8f1fea763f42a1109f6b1eb3f56ae521cfdec3.tar.gz
openbsd-be8f1fea763f42a1109f6b1eb3f56ae521cfdec3.tar.bz2
openbsd-be8f1fea763f42a1109f6b1eb3f56ae521cfdec3.zip
Make our mktemp(3) callback-driven and split into multiple files.
Previously, calling any of the mktemp(3) family would pull in lstat(2), open(2) and mkdir(2). Now, only the necessary system calls will be reachable from the binary. OK deraadt@ guenther@
Diffstat (limited to 'src/lib/libc/stdlib/mkstemp.c')
-rw-r--r--src/lib/libc/stdlib/mkstemp.c64
1 files changed, 64 insertions, 0 deletions
diff --git a/src/lib/libc/stdlib/mkstemp.c b/src/lib/libc/stdlib/mkstemp.c
new file mode 100644
index 0000000000..75a9d27d1a
--- /dev/null
+++ b/src/lib/libc/stdlib/mkstemp.c
@@ -0,0 +1,64 @@
1/* $OpenBSD: mkstemp.c,v 1.1 2024/01/19 19:45:02 millert Exp $ */
2/*
3 * Copyright (c) 2024 Todd C. Miller
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18#include <sys/stat.h>
19#include <errno.h>
20#include <fcntl.h>
21#include <stdlib.h>
22
23#define MKOSTEMP_FLAGS (O_APPEND | O_CLOEXEC | O_DSYNC | O_RSYNC | O_SYNC)
24
25static int
26mkstemp_cb(const char *path, int flags)
27{
28 flags |= O_CREAT | O_EXCL | O_RDWR;
29 return open(path, flags, S_IRUSR|S_IWUSR);
30}
31
32int
33mkostemps(char *path, int slen, int flags)
34{
35 if (flags & ~MKOSTEMP_FLAGS) {
36 errno = EINVAL;
37 return -1;
38 }
39 return __mktemp4(path, slen, flags, mkstemp_cb);
40}
41
42int
43mkostemp(char *path, int flags)
44{
45 if (flags & ~MKOSTEMP_FLAGS) {
46 errno = EINVAL;
47 return -1;
48 }
49 return __mktemp4(path, 0, flags, mkstemp_cb);
50}
51DEF_WEAK(mkostemp);
52
53int
54mkstemp(char *path)
55{
56 return __mktemp4(path, 0, 0, mkstemp_cb);
57}
58DEF_WEAK(mkstemp);
59
60int
61mkstemps(char *path, int slen)
62{
63 return __mktemp4(path, slen, 0, mkstemp_cb);
64}