]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
a7334b09 | 2 | |
60918275 | 3 | #include <errno.h> |
f6c2284a | 4 | #include <fcntl.h> |
87d2c1ff | 5 | #include <sys/mman.h> |
eef46c37 | 6 | |
b5efdb8a | 7 | #include "alloc-util.h" |
3f6fd1ba | 8 | #include "build.h" |
cf0fbc49 | 9 | #include "dirent-util.h" |
686d13b9 | 10 | #include "env-file.h" |
0307ea49 | 11 | #include "env-util.h" |
3ffd4af2 | 12 | #include "fd-util.h" |
f6c2284a | 13 | #include "fileio.h" |
f6c2284a | 14 | #include "hostname-util.h" |
a9f5d454 | 15 | #include "log.h" |
f6c2284a | 16 | #include "macro.h" |
6bedfcbb | 17 | #include "parse-util.h" |
cf0fbc49 | 18 | #include "stat-util.h" |
07630cea | 19 | #include "string-util.h" |
4f5dd394 | 20 | #include "util.h" |
9ce17593 | 21 | #include "virt.h" |
56cf987f | 22 | |
9a0e6896 LP |
23 | int saved_argc = 0; |
24 | char **saved_argv = NULL; | |
dcd61450 | 25 | static int saved_in_initrd = -1; |
9086e840 | 26 | |
65457142 | 27 | bool kexec_loaded(void) { |
c47f86e6 ZJS |
28 | _cleanup_free_ char *s = NULL; |
29 | ||
30 | if (read_one_line_file("/sys/kernel/kexec_loaded", &s) < 0) | |
31 | return false; | |
32 | ||
33 | return s[0] == '1'; | |
65457142 | 34 | } |
fb9de93d | 35 | |
87d2c1ff LP |
36 | int prot_from_flags(int flags) { |
37 | ||
38 | switch (flags & O_ACCMODE) { | |
39 | ||
40 | case O_RDONLY: | |
41 | return PROT_READ; | |
42 | ||
43 | case O_WRONLY: | |
44 | return PROT_WRITE; | |
45 | ||
46 | case O_RDWR: | |
47 | return PROT_READ|PROT_WRITE; | |
48 | ||
49 | default: | |
50 | return -EINVAL; | |
51 | } | |
7c99e0c1 | 52 | } |
689b9a22 | 53 | |
9be346c9 | 54 | bool in_initrd(void) { |
825c6fe5 | 55 | struct statfs s; |
0307ea49 | 56 | int r; |
8f33b5b8 | 57 | |
dcd61450 IS |
58 | if (saved_in_initrd >= 0) |
59 | return saved_in_initrd; | |
825c6fe5 LP |
60 | |
61 | /* We make two checks here: | |
62 | * | |
63 | * 1. the flag file /etc/initrd-release must exist | |
64 | * 2. the root file system must be a memory file system | |
65 | * | |
66 | * The second check is extra paranoia, since misdetecting an | |
629ff674 | 67 | * initrd can have bad consequences due the initrd |
825c6fe5 LP |
68 | * emptying when transititioning to the main systemd. |
69 | */ | |
70 | ||
0307ea49 ZJS |
71 | r = getenv_bool_secure("SYSTEMD_IN_INITRD"); |
72 | if (r < 0 && r != -ENXIO) | |
73 | log_debug_errno(r, "Failed to parse $SYSTEMD_IN_INITRD, ignoring: %m"); | |
74 | ||
75 | if (r >= 0) | |
76 | saved_in_initrd = r > 0; | |
77 | else | |
78 | saved_in_initrd = access("/etc/initrd-release", F_OK) >= 0 && | |
79 | statfs("/", &s) >= 0 && | |
80 | is_temporary_fs(&s); | |
9be346c9 | 81 | |
dcd61450 IS |
82 | return saved_in_initrd; |
83 | } | |
84 | ||
85 | void in_initrd_force(bool value) { | |
86 | saved_in_initrd = value; | |
9be346c9 | 87 | } |
069cfc85 | 88 | |
240dbaa4 LP |
89 | int on_ac_power(void) { |
90 | bool found_offline = false, found_online = false; | |
91 | _cleanup_closedir_ DIR *d = NULL; | |
8fb3f009 | 92 | struct dirent *de; |
240dbaa4 LP |
93 | |
94 | d = opendir("/sys/class/power_supply"); | |
95 | if (!d) | |
6d890034 | 96 | return errno == ENOENT ? true : -errno; |
240dbaa4 | 97 | |
8fb3f009 | 98 | FOREACH_DIRENT(de, d, return -errno) { |
240dbaa4 LP |
99 | _cleanup_close_ int fd = -1, device = -1; |
100 | char contents[6]; | |
101 | ssize_t n; | |
240dbaa4 | 102 | |
240dbaa4 LP |
103 | device = openat(dirfd(d), de->d_name, O_DIRECTORY|O_RDONLY|O_CLOEXEC|O_NOCTTY); |
104 | if (device < 0) { | |
3742095b | 105 | if (IN_SET(errno, ENOENT, ENOTDIR)) |
240dbaa4 LP |
106 | continue; |
107 | ||
108 | return -errno; | |
109 | } | |
110 | ||
111 | fd = openat(device, "type", O_RDONLY|O_CLOEXEC|O_NOCTTY); | |
112 | if (fd < 0) { | |
113 | if (errno == ENOENT) | |
114 | continue; | |
115 | ||
116 | return -errno; | |
117 | } | |
118 | ||
119 | n = read(fd, contents, sizeof(contents)); | |
120 | if (n < 0) | |
121 | return -errno; | |
122 | ||
123 | if (n != 6 || memcmp(contents, "Mains\n", 6)) | |
124 | continue; | |
125 | ||
03e334a1 | 126 | safe_close(fd); |
240dbaa4 LP |
127 | fd = openat(device, "online", O_RDONLY|O_CLOEXEC|O_NOCTTY); |
128 | if (fd < 0) { | |
129 | if (errno == ENOENT) | |
130 | continue; | |
131 | ||
132 | return -errno; | |
133 | } | |
134 | ||
135 | n = read(fd, contents, sizeof(contents)); | |
136 | if (n < 0) | |
137 | return -errno; | |
138 | ||
139 | if (n != 2 || contents[1] != '\n') | |
140 | return -EIO; | |
141 | ||
142 | if (contents[0] == '1') { | |
143 | found_online = true; | |
144 | break; | |
145 | } else if (contents[0] == '0') | |
146 | found_offline = true; | |
147 | else | |
148 | return -EIO; | |
149 | } | |
150 | ||
151 | return found_online || !found_offline; | |
152 | } | |
fabe5c0e | 153 | |
bc9fd78c LP |
154 | int container_get_leader(const char *machine, pid_t *pid) { |
155 | _cleanup_free_ char *s = NULL, *class = NULL; | |
156 | const char *p; | |
157 | pid_t leader; | |
158 | int r; | |
159 | ||
160 | assert(machine); | |
161 | assert(pid); | |
162 | ||
1e5057b9 LP |
163 | if (streq(machine, ".host")) { |
164 | *pid = 1; | |
165 | return 0; | |
166 | } | |
167 | ||
b9a8d250 LP |
168 | if (!machine_name_is_valid(machine)) |
169 | return -EINVAL; | |
170 | ||
63c372cb | 171 | p = strjoina("/run/systemd/machines/", machine); |
13df9c39 LP |
172 | r = parse_env_file(NULL, p, |
173 | "LEADER", &s, | |
174 | "CLASS", &class); | |
bc9fd78c LP |
175 | if (r == -ENOENT) |
176 | return -EHOSTDOWN; | |
177 | if (r < 0) | |
178 | return r; | |
179 | if (!s) | |
180 | return -EIO; | |
181 | ||
182 | if (!streq_ptr(class, "container")) | |
183 | return -EIO; | |
184 | ||
185 | r = parse_pid(s, &leader); | |
186 | if (r < 0) | |
187 | return r; | |
188 | if (leader <= 1) | |
189 | return -EIO; | |
190 | ||
191 | *pid = leader; | |
192 | return 0; | |
193 | } | |
194 | ||
3f6fd1ba | 195 | int version(void) { |
91b79ba8 ZJS |
196 | printf("systemd " STRINGIFY(PROJECT_VERSION) " (" GIT_VERSION ")\n%s\n", |
197 | systemd_features); | |
3f6fd1ba LP |
198 | return 0; |
199 | } | |
68c58c67 LP |
200 | |
201 | /* This is a direct translation of str_verscmp from boot.c */ | |
202 | static bool is_digit(int c) { | |
203 | return c >= '0' && c <= '9'; | |
204 | } | |
205 | ||
206 | static int c_order(int c) { | |
207 | if (c == 0 || is_digit(c)) | |
208 | return 0; | |
209 | ||
210 | if ((c >= 'a') && (c <= 'z')) | |
211 | return c; | |
212 | ||
213 | return c + 0x10000; | |
214 | } | |
215 | ||
216 | int str_verscmp(const char *s1, const char *s2) { | |
217 | const char *os1, *os2; | |
218 | ||
219 | assert(s1); | |
220 | assert(s2); | |
221 | ||
222 | os1 = s1; | |
223 | os2 = s2; | |
224 | ||
225 | while (*s1 || *s2) { | |
226 | int first; | |
227 | ||
228 | while ((*s1 && !is_digit(*s1)) || (*s2 && !is_digit(*s2))) { | |
229 | int order; | |
230 | ||
231 | order = c_order(*s1) - c_order(*s2); | |
232 | if (order != 0) | |
233 | return order; | |
234 | s1++; | |
235 | s2++; | |
236 | } | |
237 | ||
238 | while (*s1 == '0') | |
239 | s1++; | |
240 | while (*s2 == '0') | |
241 | s2++; | |
242 | ||
243 | first = 0; | |
244 | while (is_digit(*s1) && is_digit(*s2)) { | |
245 | if (first == 0) | |
246 | first = *s1 - *s2; | |
247 | s1++; | |
248 | s2++; | |
249 | } | |
250 | ||
251 | if (is_digit(*s1)) | |
252 | return 1; | |
253 | if (is_digit(*s2)) | |
254 | return -1; | |
255 | ||
256 | if (first != 0) | |
257 | return first; | |
258 | } | |
259 | ||
260 | return strcmp(os1, os2); | |
261 | } | |
9ce17593 JK |
262 | |
263 | /* Turn off core dumps but only if we're running outside of a container. */ | |
e557b1a6 LP |
264 | void disable_coredumps(void) { |
265 | int r; | |
266 | ||
267 | if (detect_container() > 0) | |
268 | return; | |
269 | ||
57512c89 | 270 | r = write_string_file("/proc/sys/kernel/core_pattern", "|/bin/false", WRITE_STRING_FILE_DISABLE_BUFFER); |
e557b1a6 LP |
271 | if (r < 0) |
272 | log_debug_errno(r, "Failed to turn off coredumps, ignoring: %m"); | |
9ce17593 | 273 | } |