]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/base-filesystem.c
Merge pull request #2076 from keszybz/downgrade-masked-unit-message
[thirdparty/systemd.git] / src / shared / base-filesystem.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4 This file is part of systemd.
5
6 Copyright 2014 Kay Sievers
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <stdbool.h>
25 #include <stdlib.h>
26 #include <sys/stat.h>
27 #include <syslog.h>
28 #include <unistd.h>
29
30 #include "alloc-util.h"
31 #include "base-filesystem.h"
32 #include "fd-util.h"
33 #include "log.h"
34 #include "macro.h"
35 #include "string-util.h"
36 #include "umask-util.h"
37 #include "user-util.h"
38 #include "util.h"
39
40 typedef struct BaseFilesystem {
41 const char *dir;
42 mode_t mode;
43 const char *target;
44 const char *exists;
45 bool ignore_failure;
46 } BaseFilesystem;
47
48 static const BaseFilesystem table[] = {
49 { "bin", 0, "usr/bin\0", NULL },
50 { "lib", 0, "usr/lib\0", NULL },
51 { "root", 0755, NULL, NULL, true },
52 { "sbin", 0, "usr/sbin\0", NULL },
53 { "usr", 0755, NULL, NULL },
54 { "var", 0755, NULL, NULL },
55 { "etc", 0755, NULL, NULL },
56 #if defined(__i386__) || defined(__x86_64__)
57 { "lib64", 0, "usr/lib/x86_64-linux-gnu\0"
58 "usr/lib64\0", "ld-linux-x86-64.so.2" },
59 #endif
60 };
61
62 int base_filesystem_create(const char *root, uid_t uid, gid_t gid) {
63 _cleanup_close_ int fd = -1;
64 unsigned i;
65 int r = 0;
66
67 fd = open(root, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW);
68 if (fd < 0)
69 return log_error_errno(errno, "Failed to open root file system: %m");
70
71 for (i = 0; i < ELEMENTSOF(table); i ++) {
72 if (faccessat(fd, table[i].dir, F_OK, AT_SYMLINK_NOFOLLOW) >= 0)
73 continue;
74
75 if (table[i].target) {
76 const char *target = NULL, *s;
77
78 /* check if one of the targets exists */
79 NULSTR_FOREACH(s, table[i].target) {
80 if (faccessat(fd, s, F_OK, AT_SYMLINK_NOFOLLOW) < 0)
81 continue;
82
83 /* check if a specific file exists at the target path */
84 if (table[i].exists) {
85 _cleanup_free_ char *p = NULL;
86
87 p = strjoin(s, "/", table[i].exists, NULL);
88 if (!p)
89 return log_oom();
90
91 if (faccessat(fd, p, F_OK, AT_SYMLINK_NOFOLLOW) < 0)
92 continue;
93 }
94
95 target = s;
96 break;
97 }
98
99 if (!target)
100 continue;
101
102 r = symlinkat(target, fd, table[i].dir);
103 if (r < 0 && errno != EEXIST)
104 return log_error_errno(errno, "Failed to create symlink at %s/%s: %m", root, table[i].dir);
105
106 if (uid != UID_INVALID || gid != UID_INVALID) {
107 if (fchownat(fd, table[i].dir, uid, gid, AT_SYMLINK_NOFOLLOW) < 0)
108 return log_error_errno(errno, "Failed to chown symlink at %s/%s: %m", root, table[i].dir);
109 }
110
111 continue;
112 }
113
114 RUN_WITH_UMASK(0000)
115 r = mkdirat(fd, table[i].dir, table[i].mode);
116 if (r < 0 && errno != EEXIST) {
117 log_full_errno(table[i].ignore_failure ? LOG_DEBUG : LOG_ERR, errno,
118 "Failed to create directory at %s/%s: %m", root, table[i].dir);
119
120 if (!table[i].ignore_failure)
121 return -errno;
122 }
123
124 if (uid != UID_INVALID || gid != UID_INVALID) {
125 if (fchownat(fd, table[i].dir, uid, gid, AT_SYMLINK_NOFOLLOW) < 0)
126 return log_error_errno(errno, "Failed to chown directory at %s/%s: %m", root, table[i].dir);
127 }
128 }
129
130 return 0;
131 }