]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/fs-util.c
Merge pull request #2495 from heftig/master
[thirdparty/systemd.git] / src / basic / fs-util.c
1 /***
2 This file is part of systemd.
3
4 Copyright 2010 Lennart Poettering
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include <dirent.h>
21 #include <errno.h>
22 #include <stddef.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/stat.h>
27 #include <time.h>
28 #include <unistd.h>
29
30 #include "alloc-util.h"
31 #include "dirent-util.h"
32 #include "fd-util.h"
33 #include "fileio.h"
34 #include "fs-util.h"
35 #include "log.h"
36 #include "macro.h"
37 #include "missing.h"
38 #include "mkdir.h"
39 #include "parse-util.h"
40 #include "path-util.h"
41 #include "string-util.h"
42 #include "strv.h"
43 #include "time-util.h"
44 #include "user-util.h"
45 #include "util.h"
46
47 int unlink_noerrno(const char *path) {
48 PROTECT_ERRNO;
49 int r;
50
51 r = unlink(path);
52 if (r < 0)
53 return -errno;
54
55 return 0;
56 }
57
58 int rmdir_parents(const char *path, const char *stop) {
59 size_t l;
60 int r = 0;
61
62 assert(path);
63 assert(stop);
64
65 l = strlen(path);
66
67 /* Skip trailing slashes */
68 while (l > 0 && path[l-1] == '/')
69 l--;
70
71 while (l > 0) {
72 char *t;
73
74 /* Skip last component */
75 while (l > 0 && path[l-1] != '/')
76 l--;
77
78 /* Skip trailing slashes */
79 while (l > 0 && path[l-1] == '/')
80 l--;
81
82 if (l <= 0)
83 break;
84
85 t = strndup(path, l);
86 if (!t)
87 return -ENOMEM;
88
89 if (path_startswith(stop, t)) {
90 free(t);
91 return 0;
92 }
93
94 r = rmdir(t);
95 free(t);
96
97 if (r < 0)
98 if (errno != ENOENT)
99 return -errno;
100 }
101
102 return 0;
103 }
104
105
106 int rename_noreplace(int olddirfd, const char *oldpath, int newdirfd, const char *newpath) {
107 struct stat buf;
108 int ret;
109
110 ret = renameat2(olddirfd, oldpath, newdirfd, newpath, RENAME_NOREPLACE);
111 if (ret >= 0)
112 return 0;
113
114 /* renameat2() exists since Linux 3.15, btrfs added support for it later.
115 * If it is not implemented, fallback to another method. */
116 if (!IN_SET(errno, EINVAL, ENOSYS))
117 return -errno;
118
119 /* The link()/unlink() fallback does not work on directories. But
120 * renameat() without RENAME_NOREPLACE gives the same semantics on
121 * directories, except when newpath is an *empty* directory. This is
122 * good enough. */
123 ret = fstatat(olddirfd, oldpath, &buf, AT_SYMLINK_NOFOLLOW);
124 if (ret >= 0 && S_ISDIR(buf.st_mode)) {
125 ret = renameat(olddirfd, oldpath, newdirfd, newpath);
126 return ret >= 0 ? 0 : -errno;
127 }
128
129 /* If it is not a directory, use the link()/unlink() fallback. */
130 ret = linkat(olddirfd, oldpath, newdirfd, newpath, 0);
131 if (ret < 0)
132 return -errno;
133
134 ret = unlinkat(olddirfd, oldpath, 0);
135 if (ret < 0) {
136 /* backup errno before the following unlinkat() alters it */
137 ret = errno;
138 (void) unlinkat(newdirfd, newpath, 0);
139 errno = ret;
140 return -errno;
141 }
142
143 return 0;
144 }
145
146 int readlinkat_malloc(int fd, const char *p, char **ret) {
147 size_t l = 100;
148 int r;
149
150 assert(p);
151 assert(ret);
152
153 for (;;) {
154 char *c;
155 ssize_t n;
156
157 c = new(char, l);
158 if (!c)
159 return -ENOMEM;
160
161 n = readlinkat(fd, p, c, l-1);
162 if (n < 0) {
163 r = -errno;
164 free(c);
165 return r;
166 }
167
168 if ((size_t) n < l-1) {
169 c[n] = 0;
170 *ret = c;
171 return 0;
172 }
173
174 free(c);
175 l *= 2;
176 }
177 }
178
179 int readlink_malloc(const char *p, char **ret) {
180 return readlinkat_malloc(AT_FDCWD, p, ret);
181 }
182
183 int readlink_value(const char *p, char **ret) {
184 _cleanup_free_ char *link = NULL;
185 char *value;
186 int r;
187
188 r = readlink_malloc(p, &link);
189 if (r < 0)
190 return r;
191
192 value = basename(link);
193 if (!value)
194 return -ENOENT;
195
196 value = strdup(value);
197 if (!value)
198 return -ENOMEM;
199
200 *ret = value;
201
202 return 0;
203 }
204
205 int readlink_and_make_absolute(const char *p, char **r) {
206 _cleanup_free_ char *target = NULL;
207 char *k;
208 int j;
209
210 assert(p);
211 assert(r);
212
213 j = readlink_malloc(p, &target);
214 if (j < 0)
215 return j;
216
217 k = file_in_same_dir(p, target);
218 if (!k)
219 return -ENOMEM;
220
221 *r = k;
222 return 0;
223 }
224
225 int readlink_and_canonicalize(const char *p, char **r) {
226 char *t, *s;
227 int j;
228
229 assert(p);
230 assert(r);
231
232 j = readlink_and_make_absolute(p, &t);
233 if (j < 0)
234 return j;
235
236 s = canonicalize_file_name(t);
237 if (s) {
238 free(t);
239 *r = s;
240 } else
241 *r = t;
242
243 path_kill_slashes(*r);
244
245 return 0;
246 }
247
248 int readlink_and_make_absolute_root(const char *root, const char *path, char **ret) {
249 _cleanup_free_ char *target = NULL, *t = NULL;
250 const char *full;
251 int r;
252
253 full = prefix_roota(root, path);
254 r = readlink_malloc(full, &target);
255 if (r < 0)
256 return r;
257
258 t = file_in_same_dir(path, target);
259 if (!t)
260 return -ENOMEM;
261
262 *ret = t;
263 t = NULL;
264
265 return 0;
266 }
267
268 int chmod_and_chown(const char *path, mode_t mode, uid_t uid, gid_t gid) {
269 assert(path);
270
271 /* Under the assumption that we are running privileged we
272 * first change the access mode and only then hand out
273 * ownership to avoid a window where access is too open. */
274
275 if (mode != MODE_INVALID)
276 if (chmod(path, mode) < 0)
277 return -errno;
278
279 if (uid != UID_INVALID || gid != GID_INVALID)
280 if (chown(path, uid, gid) < 0)
281 return -errno;
282
283 return 0;
284 }
285
286 int fchmod_and_fchown(int fd, mode_t mode, uid_t uid, gid_t gid) {
287 assert(fd >= 0);
288
289 /* Under the assumption that we are running privileged we
290 * first change the access mode and only then hand out
291 * ownership to avoid a window where access is too open. */
292
293 if (mode != MODE_INVALID)
294 if (fchmod(fd, mode) < 0)
295 return -errno;
296
297 if (uid != UID_INVALID || gid != GID_INVALID)
298 if (fchown(fd, uid, gid) < 0)
299 return -errno;
300
301 return 0;
302 }
303
304 int fchmod_umask(int fd, mode_t m) {
305 mode_t u;
306 int r;
307
308 u = umask(0777);
309 r = fchmod(fd, m & (~u)) < 0 ? -errno : 0;
310 umask(u);
311
312 return r;
313 }
314
315 int fd_warn_permissions(const char *path, int fd) {
316 struct stat st;
317
318 if (fstat(fd, &st) < 0)
319 return -errno;
320
321 if (st.st_mode & 0111)
322 log_warning("Configuration file %s is marked executable. Please remove executable permission bits. Proceeding anyway.", path);
323
324 if (st.st_mode & 0002)
325 log_warning("Configuration file %s is marked world-writable. Please remove world writability permission bits. Proceeding anyway.", path);
326
327 if (getpid() == 1 && (st.st_mode & 0044) != 0044)
328 log_warning("Configuration file %s is marked world-inaccessible. This has no effect as configuration data is accessible via APIs without restrictions. Proceeding anyway.", path);
329
330 return 0;
331 }
332
333 int touch_file(const char *path, bool parents, usec_t stamp, uid_t uid, gid_t gid, mode_t mode) {
334 _cleanup_close_ int fd;
335 int r;
336
337 assert(path);
338
339 if (parents)
340 mkdir_parents(path, 0755);
341
342 fd = open(path, O_WRONLY|O_CREAT|O_CLOEXEC|O_NOCTTY,
343 (mode == 0 || mode == MODE_INVALID) ? 0644 : mode);
344 if (fd < 0)
345 return -errno;
346
347 if (mode != MODE_INVALID) {
348 r = fchmod(fd, mode);
349 if (r < 0)
350 return -errno;
351 }
352
353 if (uid != UID_INVALID || gid != GID_INVALID) {
354 r = fchown(fd, uid, gid);
355 if (r < 0)
356 return -errno;
357 }
358
359 if (stamp != USEC_INFINITY) {
360 struct timespec ts[2];
361
362 timespec_store(&ts[0], stamp);
363 ts[1] = ts[0];
364 r = futimens(fd, ts);
365 } else
366 r = futimens(fd, NULL);
367 if (r < 0)
368 return -errno;
369
370 return 0;
371 }
372
373 int touch(const char *path) {
374 return touch_file(path, false, USEC_INFINITY, UID_INVALID, GID_INVALID, MODE_INVALID);
375 }
376
377 int symlink_idempotent(const char *from, const char *to) {
378 _cleanup_free_ char *p = NULL;
379 int r;
380
381 assert(from);
382 assert(to);
383
384 if (symlink(from, to) < 0) {
385 if (errno != EEXIST)
386 return -errno;
387
388 r = readlink_malloc(to, &p);
389 if (r < 0)
390 return r;
391
392 if (!streq(p, from))
393 return -EINVAL;
394 }
395
396 return 0;
397 }
398
399 int symlink_atomic(const char *from, const char *to) {
400 _cleanup_free_ char *t = NULL;
401 int r;
402
403 assert(from);
404 assert(to);
405
406 r = tempfn_random(to, NULL, &t);
407 if (r < 0)
408 return r;
409
410 if (symlink(from, t) < 0)
411 return -errno;
412
413 if (rename(t, to) < 0) {
414 unlink_noerrno(t);
415 return -errno;
416 }
417
418 return 0;
419 }
420
421 int mknod_atomic(const char *path, mode_t mode, dev_t dev) {
422 _cleanup_free_ char *t = NULL;
423 int r;
424
425 assert(path);
426
427 r = tempfn_random(path, NULL, &t);
428 if (r < 0)
429 return r;
430
431 if (mknod(t, mode, dev) < 0)
432 return -errno;
433
434 if (rename(t, path) < 0) {
435 unlink_noerrno(t);
436 return -errno;
437 }
438
439 return 0;
440 }
441
442 int mkfifo_atomic(const char *path, mode_t mode) {
443 _cleanup_free_ char *t = NULL;
444 int r;
445
446 assert(path);
447
448 r = tempfn_random(path, NULL, &t);
449 if (r < 0)
450 return r;
451
452 if (mkfifo(t, mode) < 0)
453 return -errno;
454
455 if (rename(t, path) < 0) {
456 unlink_noerrno(t);
457 return -errno;
458 }
459
460 return 0;
461 }
462
463 int get_files_in_directory(const char *path, char ***list) {
464 _cleanup_closedir_ DIR *d = NULL;
465 size_t bufsize = 0, n = 0;
466 _cleanup_strv_free_ char **l = NULL;
467
468 assert(path);
469
470 /* Returns all files in a directory in *list, and the number
471 * of files as return value. If list is NULL returns only the
472 * number. */
473
474 d = opendir(path);
475 if (!d)
476 return -errno;
477
478 for (;;) {
479 struct dirent *de;
480
481 errno = 0;
482 de = readdir(d);
483 if (!de && errno > 0)
484 return -errno;
485 if (!de)
486 break;
487
488 dirent_ensure_type(d, de);
489
490 if (!dirent_is_file(de))
491 continue;
492
493 if (list) {
494 /* one extra slot is needed for the terminating NULL */
495 if (!GREEDY_REALLOC(l, bufsize, n + 2))
496 return -ENOMEM;
497
498 l[n] = strdup(de->d_name);
499 if (!l[n])
500 return -ENOMEM;
501
502 l[++n] = NULL;
503 } else
504 n++;
505 }
506
507 if (list) {
508 *list = l;
509 l = NULL; /* avoid freeing */
510 }
511
512 return n;
513 }