2 This file is part of systemd.
4 Copyright 2010 Lennart Poettering
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.
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.
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/>.
30 #include "alloc-util.h"
31 #include "dirent-util.h"
37 //#include "missing.h"
39 #include "parse-util.h"
40 #include "path-util.h"
41 #include "string-util.h"
43 //#include "time-util.h"
44 #include "user-util.h"
47 int unlink_noerrno(const char *path) {
58 #if 0 /// UNNEEDED by elogind
59 int rmdir_parents(const char *path, const char *stop) {
68 /* Skip trailing slashes */
69 while (l > 0 && path[l-1] == '/')
75 /* Skip last component */
76 while (l > 0 && path[l-1] != '/')
79 /* Skip trailing slashes */
80 while (l > 0 && path[l-1] == '/')
90 if (path_startswith(stop, t)) {
107 int rename_noreplace(int olddirfd, const char *oldpath, int newdirfd, const char *newpath) {
111 ret = renameat2(olddirfd, oldpath, newdirfd, newpath, RENAME_NOREPLACE);
115 /* renameat2() exists since Linux 3.15, btrfs added support for it later.
116 * If it is not implemented, fallback to another method. */
117 if (!IN_SET(errno, EINVAL, ENOSYS))
120 /* The link()/unlink() fallback does not work on directories. But
121 * renameat() without RENAME_NOREPLACE gives the same semantics on
122 * directories, except when newpath is an *empty* directory. This is
124 ret = fstatat(olddirfd, oldpath, &buf, AT_SYMLINK_NOFOLLOW);
125 if (ret >= 0 && S_ISDIR(buf.st_mode)) {
126 ret = renameat(olddirfd, oldpath, newdirfd, newpath);
127 return ret >= 0 ? 0 : -errno;
130 /* If it is not a directory, use the link()/unlink() fallback. */
131 ret = linkat(olddirfd, oldpath, newdirfd, newpath, 0);
135 ret = unlinkat(olddirfd, oldpath, 0);
137 /* backup errno before the following unlinkat() alters it */
139 (void) unlinkat(newdirfd, newpath, 0);
148 int readlinkat_malloc(int fd, const char *p, char **ret) {
163 n = readlinkat(fd, p, c, l-1);
170 if ((size_t) n < l-1) {
181 int readlink_malloc(const char *p, char **ret) {
182 return readlinkat_malloc(AT_FDCWD, p, ret);
185 #if 0 /// UNNEEDED by elogind
186 int readlink_value(const char *p, char **ret) {
187 _cleanup_free_ char *link = NULL;
191 r = readlink_malloc(p, &link);
195 value = basename(link);
199 value = strdup(value);
208 int readlink_and_make_absolute(const char *p, char **r) {
209 _cleanup_free_ char *target = NULL;
216 j = readlink_malloc(p, &target);
220 k = file_in_same_dir(p, target);
228 int readlink_and_canonicalize(const char *p, char **r) {
235 j = readlink_and_make_absolute(p, &t);
239 s = canonicalize_file_name(t);
246 path_kill_slashes(*r);
251 int readlink_and_make_absolute_root(const char *root, const char *path, char **ret) {
252 _cleanup_free_ char *target = NULL, *t = NULL;
256 full = prefix_roota(root, path);
257 r = readlink_malloc(full, &target);
261 t = file_in_same_dir(path, target);
272 int chmod_and_chown(const char *path, mode_t mode, uid_t uid, gid_t gid) {
275 /* Under the assumption that we are running privileged we
276 * first change the access mode and only then hand out
277 * ownership to avoid a window where access is too open. */
279 if (mode != MODE_INVALID)
280 if (chmod(path, mode) < 0)
283 if (uid != UID_INVALID || gid != GID_INVALID)
284 if (chown(path, uid, gid) < 0)
290 #if 0 /// UNNEEDED by elogind
291 int fchmod_and_fchown(int fd, mode_t mode, uid_t uid, gid_t gid) {
294 /* Under the assumption that we are running privileged we
295 * first change the access mode and only then hand out
296 * ownership to avoid a window where access is too open. */
298 if (mode != MODE_INVALID)
299 if (fchmod(fd, mode) < 0)
302 if (uid != UID_INVALID || gid != GID_INVALID)
303 if (fchown(fd, uid, gid) < 0)
310 int fchmod_umask(int fd, mode_t m) {
315 r = fchmod(fd, m & (~u)) < 0 ? -errno : 0;
321 int fd_warn_permissions(const char *path, int fd) {
324 if (fstat(fd, &st) < 0)
327 if (st.st_mode & 0111)
328 log_warning("Configuration file %s is marked executable. Please remove executable permission bits. Proceeding anyway.", path);
330 if (st.st_mode & 0002)
331 log_warning("Configuration file %s is marked world-writable. Please remove world writability permission bits. Proceeding anyway.", path);
333 if (getpid() == 1 && (st.st_mode & 0044) != 0044)
334 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);
339 int touch_file(const char *path, bool parents, usec_t stamp, uid_t uid, gid_t gid, mode_t mode) {
340 _cleanup_close_ int fd;
346 mkdir_parents(path, 0755);
348 fd = open(path, O_WRONLY|O_CREAT|O_CLOEXEC|O_NOCTTY,
349 (mode == 0 || mode == MODE_INVALID) ? 0644 : mode);
353 if (mode != MODE_INVALID) {
354 r = fchmod(fd, mode);
359 if (uid != UID_INVALID || gid != GID_INVALID) {
360 r = fchown(fd, uid, gid);
365 if (stamp != USEC_INFINITY) {
366 struct timespec ts[2];
368 timespec_store(&ts[0], stamp);
370 r = futimens(fd, ts);
372 r = futimens(fd, NULL);
379 int touch(const char *path) {
380 return touch_file(path, false, USEC_INFINITY, UID_INVALID, GID_INVALID, MODE_INVALID);
383 #if 0 /// UNNEEDED by elogind
384 int symlink_idempotent(const char *from, const char *to) {
385 _cleanup_free_ char *p = NULL;
391 if (symlink(from, to) < 0) {
395 r = readlink_malloc(to, &p);
406 int symlink_atomic(const char *from, const char *to) {
407 _cleanup_free_ char *t = NULL;
413 r = tempfn_random(to, NULL, &t);
417 if (symlink(from, t) < 0)
420 if (rename(t, to) < 0) {
428 int mknod_atomic(const char *path, mode_t mode, dev_t dev) {
429 _cleanup_free_ char *t = NULL;
434 r = tempfn_random(path, NULL, &t);
438 if (mknod(t, mode, dev) < 0)
441 if (rename(t, path) < 0) {
449 int mkfifo_atomic(const char *path, mode_t mode) {
450 _cleanup_free_ char *t = NULL;
455 r = tempfn_random(path, NULL, &t);
459 if (mkfifo(t, mode) < 0)
462 if (rename(t, path) < 0) {
471 int get_files_in_directory(const char *path, char ***list) {
472 _cleanup_closedir_ DIR *d = NULL;
473 size_t bufsize = 0, n = 0;
474 _cleanup_strv_free_ char **l = NULL;
478 /* Returns all files in a directory in *list, and the number
479 * of files as return value. If list is NULL returns only the
491 if (!de && errno > 0)
496 dirent_ensure_type(d, de);
498 if (!dirent_is_file(de))
502 /* one extra slot is needed for the terminating NULL */
503 if (!GREEDY_REALLOC(l, bufsize, n + 2))
506 l[n] = strdup(de->d_name);
517 l = NULL; /* avoid freeing */