]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/fs-util.c
util: drop two unused calls from src/basic/
[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_umask(int fd, mode_t m) {
287 mode_t u;
288 int r;
289
290 u = umask(0777);
291 r = fchmod(fd, m & (~u)) < 0 ? -errno : 0;
292 umask(u);
293
294 return r;
295 }
296
297 int fd_warn_permissions(const char *path, int fd) {
298 struct stat st;
299
300 if (fstat(fd, &st) < 0)
301 return -errno;
302
303 if (st.st_mode & 0111)
304 log_warning("Configuration file %s is marked executable. Please remove executable permission bits. Proceeding anyway.", path);
305
306 if (st.st_mode & 0002)
307 log_warning("Configuration file %s is marked world-writable. Please remove world writability permission bits. Proceeding anyway.", path);
308
309 if (getpid() == 1 && (st.st_mode & 0044) != 0044)
310 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);
311
312 return 0;
313 }
314
315 int touch_file(const char *path, bool parents, usec_t stamp, uid_t uid, gid_t gid, mode_t mode) {
316 _cleanup_close_ int fd;
317 int r;
318
319 assert(path);
320
321 if (parents)
322 mkdir_parents(path, 0755);
323
324 fd = open(path, O_WRONLY|O_CREAT|O_CLOEXEC|O_NOCTTY,
325 (mode == 0 || mode == MODE_INVALID) ? 0644 : mode);
326 if (fd < 0)
327 return -errno;
328
329 if (mode != MODE_INVALID) {
330 r = fchmod(fd, mode);
331 if (r < 0)
332 return -errno;
333 }
334
335 if (uid != UID_INVALID || gid != GID_INVALID) {
336 r = fchown(fd, uid, gid);
337 if (r < 0)
338 return -errno;
339 }
340
341 if (stamp != USEC_INFINITY) {
342 struct timespec ts[2];
343
344 timespec_store(&ts[0], stamp);
345 ts[1] = ts[0];
346 r = futimens(fd, ts);
347 } else
348 r = futimens(fd, NULL);
349 if (r < 0)
350 return -errno;
351
352 return 0;
353 }
354
355 int touch(const char *path) {
356 return touch_file(path, false, USEC_INFINITY, UID_INVALID, GID_INVALID, MODE_INVALID);
357 }
358
359 int symlink_idempotent(const char *from, const char *to) {
360 _cleanup_free_ char *p = NULL;
361 int r;
362
363 assert(from);
364 assert(to);
365
366 if (symlink(from, to) < 0) {
367 if (errno != EEXIST)
368 return -errno;
369
370 r = readlink_malloc(to, &p);
371 if (r < 0)
372 return r;
373
374 if (!streq(p, from))
375 return -EINVAL;
376 }
377
378 return 0;
379 }
380
381 int symlink_atomic(const char *from, const char *to) {
382 _cleanup_free_ char *t = NULL;
383 int r;
384
385 assert(from);
386 assert(to);
387
388 r = tempfn_random(to, NULL, &t);
389 if (r < 0)
390 return r;
391
392 if (symlink(from, t) < 0)
393 return -errno;
394
395 if (rename(t, to) < 0) {
396 unlink_noerrno(t);
397 return -errno;
398 }
399
400 return 0;
401 }
402
403 int mknod_atomic(const char *path, mode_t mode, dev_t dev) {
404 _cleanup_free_ char *t = NULL;
405 int r;
406
407 assert(path);
408
409 r = tempfn_random(path, NULL, &t);
410 if (r < 0)
411 return r;
412
413 if (mknod(t, mode, dev) < 0)
414 return -errno;
415
416 if (rename(t, path) < 0) {
417 unlink_noerrno(t);
418 return -errno;
419 }
420
421 return 0;
422 }
423
424 int mkfifo_atomic(const char *path, mode_t mode) {
425 _cleanup_free_ char *t = NULL;
426 int r;
427
428 assert(path);
429
430 r = tempfn_random(path, NULL, &t);
431 if (r < 0)
432 return r;
433
434 if (mkfifo(t, mode) < 0)
435 return -errno;
436
437 if (rename(t, path) < 0) {
438 unlink_noerrno(t);
439 return -errno;
440 }
441
442 return 0;
443 }
444
445 int get_files_in_directory(const char *path, char ***list) {
446 _cleanup_closedir_ DIR *d = NULL;
447 size_t bufsize = 0, n = 0;
448 _cleanup_strv_free_ char **l = NULL;
449
450 assert(path);
451
452 /* Returns all files in a directory in *list, and the number
453 * of files as return value. If list is NULL returns only the
454 * number. */
455
456 d = opendir(path);
457 if (!d)
458 return -errno;
459
460 for (;;) {
461 struct dirent *de;
462
463 errno = 0;
464 de = readdir(d);
465 if (!de && errno > 0)
466 return -errno;
467 if (!de)
468 break;
469
470 dirent_ensure_type(d, de);
471
472 if (!dirent_is_file(de))
473 continue;
474
475 if (list) {
476 /* one extra slot is needed for the terminating NULL */
477 if (!GREEDY_REALLOC(l, bufsize, n + 2))
478 return -ENOMEM;
479
480 l[n] = strdup(de->d_name);
481 if (!l[n])
482 return -ENOMEM;
483
484 l[++n] = NULL;
485 } else
486 n++;
487 }
488
489 if (list) {
490 *list = l;
491 l = NULL; /* avoid freeing */
492 }
493
494 return n;
495 }