]> git.proxmox.com Git - mirror_lxc.git/blame - src/lxc/file_utils.c
utils: improve lxc_switch_uid_gid()
[mirror_lxc.git] / src / lxc / file_utils.c
CommitLineData
37ef15bb
CB
1/* liblxcapi
2 *
3 * Copyright © 2018 Christian Brauner <christian.brauner@ubuntu.com>.
4 * Copyright © 2018 Canonical Ltd.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2, as
8 * published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20#include "config.h"
21
22#include <errno.h>
23#include <fcntl.h>
24#include <linux/magic.h>
25#include <stdlib.h>
26#include <sys/stat.h>
27#include <sys/types.h>
28
29#include "file_utils.h"
30#include "log.h"
31#include "macro.h"
32#include "string.h"
33
37ef15bb 34lxc_log_define(file_utils, lxc);
37ef15bb
CB
35
36int lxc_write_to_file(const char *filename, const void *buf, size_t count,
37 bool add_newline, mode_t mode)
38{
39 int fd, saved_errno;
40 ssize_t ret;
41
42 fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT | O_CLOEXEC, mode);
43 if (fd < 0)
44 return -1;
45
46 ret = lxc_write_nointr(fd, buf, count);
47 if (ret < 0)
48 goto out_error;
49
50 if ((size_t)ret != count)
51 goto out_error;
52
53 if (add_newline) {
54 ret = lxc_write_nointr(fd, "\n", 1);
55 if (ret != 1)
56 goto out_error;
57 }
58
59 close(fd);
60 return 0;
61
62out_error:
63 saved_errno = errno;
64 close(fd);
65 errno = saved_errno;
66 return -1;
67}
68
69int lxc_read_from_file(const char *filename, void *buf, size_t count)
70{
71 int fd = -1, saved_errno;
72 ssize_t ret;
73
74 fd = open(filename, O_RDONLY | O_CLOEXEC);
75 if (fd < 0)
76 return -1;
77
78 if (!buf || !count) {
79 char buf2[100];
80 size_t count2 = 0;
81
82 while ((ret = lxc_read_nointr(fd, buf2, 100)) > 0)
83 count2 += ret;
84
85 if (ret >= 0)
86 ret = count2;
87 } else {
88 memset(buf, 0, count);
89 ret = lxc_read_nointr(fd, buf, count);
90 }
91
92 saved_errno = errno;
93 close(fd);
94 errno = saved_errno;
95 return ret;
96}
97
98ssize_t lxc_write_nointr(int fd, const void *buf, size_t count)
99{
100 ssize_t ret;
101again:
102 ret = write(fd, buf, count);
103 if (ret < 0 && errno == EINTR)
104 goto again;
105
106 return ret;
107}
108
28143f88
CB
109ssize_t lxc_send_nointr(int sockfd, void *buf, size_t len, int flags)
110{
111 ssize_t ret;
112again:
113 ret = send(sockfd, buf, len, flags);
114 if (ret < 0 && errno == EINTR)
115 goto again;
116
117 return ret;
118}
119
37ef15bb
CB
120ssize_t lxc_read_nointr(int fd, void *buf, size_t count)
121{
122 ssize_t ret;
123again:
124 ret = read(fd, buf, count);
125 if (ret < 0 && errno == EINTR)
126 goto again;
127
128 return ret;
129}
130
de69edd1
CB
131ssize_t lxc_recv_nointr(int sockfd, void *buf, size_t len, int flags)
132{
133 ssize_t ret;
134again:
135 ret = recv(sockfd, buf, len, flags);
136 if (ret < 0 && errno == EINTR)
137 goto again;
138
139 return ret;
140}
141
37ef15bb
CB
142ssize_t lxc_read_nointr_expect(int fd, void *buf, size_t count, const void *expected_buf)
143{
144 ssize_t ret;
145
146 ret = lxc_read_nointr(fd, buf, count);
147 if (ret <= 0)
148 return ret;
149
150 if ((size_t)ret != count)
151 return -1;
152
153 if (expected_buf && memcmp(buf, expected_buf, count) != 0) {
154 errno = EINVAL;
155 return -1;
156 }
157
158 return ret;
159}
160
161bool file_exists(const char *f)
162{
163 struct stat statbuf;
164
165 return stat(f, &statbuf) == 0;
166}
167
168int print_to_file(const char *file, const char *content)
169{
170 FILE *f;
171 int ret = 0;
172
173 f = fopen(file, "w");
174 if (!f)
175 return -1;
176
177 if (fprintf(f, "%s", content) != strlen(content))
178 ret = -1;
179
180 fclose(f);
181 return ret;
182}
183
184int is_dir(const char *path)
185{
186 struct stat statbuf;
187 int ret;
188
189 ret = stat(path, &statbuf);
190 if (ret == 0 && S_ISDIR(statbuf.st_mode))
191 return 1;
192
193 return 0;
194}
195
196/*
197 * Return the number of lines in file @fn, or -1 on error
198 */
199int lxc_count_file_lines(const char *fn)
200{
201 FILE *f;
202 char *line = NULL;
203 size_t sz = 0;
204 int n = 0;
205
206 f = fopen_cloexec(fn, "r");
207 if (!f)
208 return -1;
209
210 while (getline(&line, &sz, f) != -1) {
211 n++;
212 }
213
214 free(line);
215 fclose(f);
216 return n;
217}
218
219int lxc_make_tmpfile(char *template, bool rm)
220{
221 int fd, ret;
222 mode_t msk;
223
224 msk = umask(0022);
225 fd = mkstemp(template);
226 umask(msk);
227 if (fd < 0)
228 return -1;
229
230 if (!rm)
231 return fd;
232
233 ret = unlink(template);
234 if (ret < 0) {
235 close(fd);
236 return -1;
237 }
238
239 return fd;
240}
241
242/* In overlayfs, st_dev is unreliable. So on overlayfs we don't do the
243 * lxc_rmdir_onedev()
244 */
245static bool is_native_overlayfs(const char *path)
246{
247 if (has_fs_type(path, OVERLAY_SUPER_MAGIC) ||
248 has_fs_type(path, OVERLAYFS_SUPER_MAGIC))
249 return true;
250
251 return false;
252}
253
254bool is_fs_type(const struct statfs *fs, fs_type_magic magic_val)
255{
256 return (fs->f_type == (fs_type_magic)magic_val);
257}
258
259bool has_fs_type(const char *path, fs_type_magic magic_val)
260{
261 bool has_type;
262 int ret;
263 struct statfs sb;
264
265 ret = statfs(path, &sb);
266 if (ret < 0)
267 return false;
268
269 return is_fs_type(&sb, magic_val);
270}
271
272bool fhas_fs_type(int fd, fs_type_magic magic_val)
273{
274 int ret;
275 struct statfs sb;
276
277 ret = fstatfs(fd, &sb);
278 if (ret < 0)
279 return false;
280
281 return is_fs_type(&sb, magic_val);
282}
283
284FILE *fopen_cloexec(const char *path, const char *mode)
285{
286 int open_mode = 0;
287 int step = 0;
288 int fd;
289 int saved_errno = 0;
290 FILE *ret;
291
292 if (!strncmp(mode, "r+", 2)) {
293 open_mode = O_RDWR;
294 step = 2;
295 } else if (!strncmp(mode, "r", 1)) {
296 open_mode = O_RDONLY;
297 step = 1;
298 } else if (!strncmp(mode, "w+", 2)) {
299 open_mode = O_RDWR | O_TRUNC | O_CREAT;
300 step = 2;
301 } else if (!strncmp(mode, "w", 1)) {
302 open_mode = O_WRONLY | O_TRUNC | O_CREAT;
303 step = 1;
304 } else if (!strncmp(mode, "a+", 2)) {
305 open_mode = O_RDWR | O_CREAT | O_APPEND;
306 step = 2;
307 } else if (!strncmp(mode, "a", 1)) {
308 open_mode = O_WRONLY | O_CREAT | O_APPEND;
309 step = 1;
310 }
311 for (; mode[step]; step++)
312 if (mode[step] == 'x')
313 open_mode |= O_EXCL;
314 open_mode |= O_CLOEXEC;
315
316 fd = open(path, open_mode, 0666);
317 if (fd < 0)
318 return NULL;
319
320 ret = fdopen(fd, mode);
321 saved_errno = errno;
322 if (!ret)
323 close(fd);
324 errno = saved_errno;
325 return ret;
326}