1 /*
2 * Copyright 2019 Intel Corporation
3 * SPDX-License-Identifier: MIT
4 */
5
6 #include "os_file.h"
7 #include "detect_os.h"
8
9 #include <errno.h>
10 #include <fcntl.h>
11 #include <stdlib.h>
12 #include <sys/stat.h>
13
14 #if DETECT_OS_WINDOWS
15 #include <io.h>
16 #define open _open
17 #define fdopen _fdopen
18 #define O_CREAT _O_CREAT
19 #define O_EXCL _O_EXCL
20 #define O_WRONLY _O_WRONLY
21 #else
22 #include <unistd.h>
23 #ifndef F_DUPFD_CLOEXEC
24 #define F_DUPFD_CLOEXEC 1030
25 #endif
26 #endif
27
28
29 FILE *
os_file_create_unique(const char * filename,int filemode)30 os_file_create_unique(const char *filename, int filemode)
31 {
32 int fd = open(filename, O_CREAT | O_EXCL | O_WRONLY, filemode);
33 if (fd == -1)
34 return NULL;
35 return fdopen(fd, "w");
36 }
37
38
39 #if DETECT_OS_WINDOWS
40 int
os_dupfd_cloexec(int fd)41 os_dupfd_cloexec(int fd)
42 {
43 /*
44 * On Windows child processes don't inherit handles by default:
45 * https://devblogs.microsoft.com/oldnewthing/20111216-00/?p=8873
46 */
47 return dup(fd);
48 }
49 #else
50 int
os_dupfd_cloexec(int fd)51 os_dupfd_cloexec(int fd)
52 {
53 int minfd = 3;
54 int newfd = fcntl(fd, F_DUPFD_CLOEXEC, minfd);
55
56 if (newfd >= 0)
57 return newfd;
58
59 if (errno != EINVAL)
60 return -1;
61
62 newfd = fcntl(fd, F_DUPFD, minfd);
63
64 if (newfd < 0)
65 return -1;
66
67 long flags = fcntl(newfd, F_GETFD);
68 if (flags == -1) {
69 close(newfd);
70 return -1;
71 }
72
73 if (fcntl(newfd, F_SETFD, flags | FD_CLOEXEC) == -1) {
74 close(newfd);
75 return -1;
76 }
77
78 return newfd;
79 }
80 #endif
81
82
83 #if DETECT_OS_LINUX
84
85 #include <fcntl.h>
86 #include <sys/stat.h>
87 #include <sys/syscall.h>
88 #include <unistd.h>
89
90 /* copied from <linux/kcmp.h> */
91 #define KCMP_FILE 0
92
93 static ssize_t
readN(int fd,char * buf,size_t len)94 readN(int fd, char *buf, size_t len)
95 {
96 int err = -ENODATA;
97 size_t total = 0;
98 do {
99 ssize_t ret = read(fd, buf + total, len - total);
100
101 if (ret < 0)
102 ret = -errno;
103
104 if (ret == -EINTR || ret == -EAGAIN)
105 continue;
106
107 if (ret <= 0) {
108 err = ret;
109 break;
110 }
111
112 total += ret;
113 } while (total != len);
114
115 return total ? (ssize_t)total : err;
116 }
117
118 char *
os_read_file(const char * filename,size_t * size)119 os_read_file(const char *filename, size_t *size)
120 {
121 /* Note that this also serves as a slight margin to avoid a 2x grow when
122 * the file is just a few bytes larger when we read it than when we
123 * fstat'ed it.
124 * The string's NULL terminator is also included in here.
125 */
126 size_t len = 64;
127
128 int fd = open(filename, O_RDONLY);
129 if (fd == -1) {
130 /* errno set by open() */
131 return NULL;
132 }
133
134 /* Pre-allocate a buffer at least the size of the file if we can read
135 * that information.
136 */
137 struct stat stat;
138 if (fstat(fd, &stat) == 0)
139 len += stat.st_size;
140
141 char *buf = malloc(len);
142 if (!buf) {
143 close(fd);
144 errno = -ENOMEM;
145 return NULL;
146 }
147
148 ssize_t actually_read;
149 size_t offset = 0, remaining = len - 1;
150 while ((actually_read = readN(fd, buf + offset, remaining)) == (ssize_t)remaining) {
151 char *newbuf = realloc(buf, 2 * len);
152 if (!newbuf) {
153 free(buf);
154 close(fd);
155 errno = -ENOMEM;
156 return NULL;
157 }
158
159 buf = newbuf;
160 len *= 2;
161 offset += actually_read;
162 remaining = len - offset - 1;
163 }
164
165 close(fd);
166
167 if (actually_read > 0)
168 offset += actually_read;
169
170 /* Final resize to actual size */
171 len = offset + 1;
172 char *newbuf = realloc(buf, len);
173 if (!newbuf) {
174 free(buf);
175 errno = -ENOMEM;
176 return NULL;
177 }
178 buf = newbuf;
179
180 buf[offset] = '\0';
181
182 if (size)
183 *size = offset;
184
185 return buf;
186 }
187
188 int
os_same_file_description(int fd1,int fd2)189 os_same_file_description(int fd1, int fd2)
190 {
191 pid_t pid = getpid();
192
193 /* Same file descriptor trivially implies same file description */
194 if (fd1 == fd2)
195 return 0;
196
197 return syscall(SYS_kcmp, pid, pid, KCMP_FILE, fd1, fd2);
198 }
199
200 #else
201
202 #include "u_debug.h"
203
204 char *
os_read_file(const char * filename,size_t * size)205 os_read_file(const char *filename, size_t *size)
206 {
207 errno = -ENOSYS;
208 return NULL;
209 }
210
211 int
os_same_file_description(int fd1,int fd2)212 os_same_file_description(int fd1, int fd2)
213 {
214 /* Same file descriptor trivially implies same file description */
215 if (fd1 == fd2)
216 return 0;
217
218 /* Otherwise we can't tell */
219 return -1;
220 }
221
222 #endif
223