1 /* $NetBSD: dbfile.c,v 1.2 2018/01/04 20:57:29 kamil Exp $ */
2
3 /*-
4 * Copyright (c) 2013 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #if HAVE_NBTOOL_CONFIG_H
30 #include "nbtool_config.h"
31 #endif
32
33 #include <sys/cdefs.h>
34 __RCSID("$NetBSD: dbfile.c,v 1.2 2018/01/04 20:57:29 kamil Exp $");
35
36 #include "namespace.h"
37
38 #include <sys/stat.h>
39 #include <stdio.h>
40 #include <fcntl.h>
41 #include <signal.h>
42 #include <unistd.h>
43 #include <stdlib.h>
44 #include <errno.h>
45 #include <paths.h>
46 #include <db.h>
47
48 int
__dbopen(const char * file,int flags,mode_t mode,struct stat * sb)49 __dbopen(const char *file, int flags, mode_t mode, struct stat *sb)
50 {
51 int fd;
52 int serrno;
53
54 #ifndef O_CLOEXEC
55 #define O_CLOEXEC 0
56 #endif
57
58 if ((fd = open(file, flags | O_CLOEXEC, mode)) == -1)
59 return -1;
60
61 #if O_CLOEXEC == 0
62 if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
63 goto out;
64 #endif
65
66 if (sb && fstat(fd, sb) == -1)
67 goto out;
68
69 return fd;
70 out:
71 serrno = errno;
72 close(fd);
73 errno = serrno;
74 return -1;
75
76 }
77
78 int
__dbtemp(const char * prefix,struct stat * sb)79 __dbtemp(const char *prefix, struct stat *sb)
80 {
81 sigset_t set, oset;
82 int len;
83 int fd, serrno;
84 char *envtmp;
85 char path[PATH_MAX];
86
87 if (issetugid())
88 envtmp = NULL;
89 else
90 envtmp = getenv("TMPDIR");
91
92 len = snprintf(path, sizeof(path), "%s/%sXXXXXX",
93 envtmp ? envtmp : _PATH_TMP, prefix);
94 if ((size_t)len >= sizeof(path)) {
95 errno = ENAMETOOLONG;
96 return -1;
97 }
98
99 (void)sigfillset(&set);
100 (void)sigprocmask(SIG_BLOCK, &set, &oset);
101
102 if ((fd = mkstemp(path)) != -1) {
103 if (unlink(path) == -1)
104 goto out;
105
106 if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
107 goto out;
108
109 if (sb && fstat(fd, sb) == -1)
110 goto out;
111 }
112 (void)sigprocmask(SIG_SETMASK, &oset, NULL);
113 return fd;
114 out:
115 serrno = errno;
116 (void)sigprocmask(SIG_SETMASK, &oset, NULL);
117 close(fd);
118 errno = serrno;
119 return -1;
120 }
121