xref: /minix/lib/libc/db/db/dbfile.c (revision 84d9c625)
1 /*	$NetBSD: dbfile.c,v 1.1 2013/12/01 00:22:48 christos 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.1 2013/12/01 00:22:48 christos Exp $");
35 
36 #include <sys/stat.h>
37 #include <stdio.h>
38 #include <fcntl.h>
39 #include <signal.h>
40 #include <unistd.h>
41 #include <stdlib.h>
42 #include <errno.h>
43 #include <paths.h>
44 #include <db.h>
45 
46 int
__dbopen(const char * file,int flags,mode_t mode,struct stat * sb)47 __dbopen(const char *file, int flags, mode_t mode, struct stat *sb)
48 {
49 	int fd;
50 	int serrno;
51 
52 #ifndef O_CLOEXEC
53 #define O_CLOEXEC 0
54 #endif
55 
56 	if ((fd = open(file, flags | O_CLOEXEC, mode)) == -1)
57 		return -1;
58 
59 #if O_CLOEXEC == 0
60 	if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
61 		goto out;
62 #endif
63 
64 	if (sb && fstat(fd, sb) == -1)
65 		goto out;
66 
67 	return fd;
68 out:
69 	serrno = errno;
70 	close(fd);
71 	errno = serrno;
72 	return -1;
73 
74 }
75 
76 int
__dbtemp(const char * prefix,struct stat * sb)77 __dbtemp(const char *prefix, struct stat *sb)
78 {
79 	sigset_t set, oset;
80 	int len;
81 	int fd, serrno;
82 	char *envtmp;
83 	char path[PATH_MAX];
84 
85 	if (issetugid())
86 		envtmp = NULL;
87 	else
88 		envtmp = getenv("TMPDIR");
89 
90 	len = snprintf(path, sizeof(path), "%s/%sXXXXXX",
91 	    envtmp ? envtmp : _PATH_TMP, prefix);
92 	if ((size_t)len >= sizeof(path)) {
93 		errno = ENAMETOOLONG;
94 		return -1;
95 	}
96 
97 	(void)sigfillset(&set);
98 	(void)sigprocmask(SIG_BLOCK, &set, &oset);
99 
100 	if ((fd = mkstemp(path)) != -1) {
101 		if (unlink(path) == -1)
102 			goto out;
103 
104 		if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
105 			goto out;
106 
107 		if (sb && fstat(fd, sb) == -1)
108 			goto out;
109 	}
110 	(void)sigprocmask(SIG_SETMASK, &oset, NULL);
111 	return fd;
112 out:
113 	serrno = errno;
114 	(void)sigprocmask(SIG_SETMASK, &oset, NULL);
115 	close(fd);
116 	errno = serrno;
117 	return -1;
118 }
119