]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2010 Lennart Poettering
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
31 #include <sys/prctl.h>
32 #include <sys/statfs.h>
33 #include <sys/sysmacros.h>
34 #include <sys/types.h>
37 #include "alloc-util.h"
38 #include "btrfs-util.h"
40 #include "cgroup-util.h"
42 #include "device-nodes.h"
43 #include "dirent-util.h"
46 #include "format-util.h"
48 #include "hostname-util.h"
52 #include "parse-util.h"
53 #include "path-util.h"
54 #include "process-util.h"
55 #include "procfs-util.h"
57 #include "signal-util.h"
58 #include "stat-util.h"
59 #include "string-util.h"
61 #include "time-util.h"
62 #include "umask-util.h"
63 #include "user-util.h"
68 char **saved_argv
= NULL
;
69 static int saved_in_initrd
= -1;
71 size_t page_size(void) {
72 static thread_local
size_t pgsz
= 0;
75 if (_likely_(pgsz
> 0))
78 r
= sysconf(_SC_PAGESIZE
);
85 bool plymouth_running(void) {
86 return access("/run/plymouth/pid", F_OK
) >= 0;
89 bool display_is_local(const char *display
) {
98 int socket_from_display(const char *display
, char **path
) {
105 if (!display_is_local(display
))
108 k
= strspn(display
+1, "0123456789");
110 f
= new(char, STRLEN("/tmp/.X11-unix/X") + k
+ 1);
114 c
= stpcpy(f
, "/tmp/.X11-unix/X");
115 memcpy(c
, display
+1, k
);
123 bool kexec_loaded(void) {
124 _cleanup_free_
char *s
= NULL
;
126 if (read_one_line_file("/sys/kernel/kexec_loaded", &s
) < 0)
132 int prot_from_flags(int flags
) {
134 switch (flags
& O_ACCMODE
) {
143 return PROT_READ
|PROT_WRITE
;
150 bool in_initrd(void) {
153 if (saved_in_initrd
>= 0)
154 return saved_in_initrd
;
156 /* We make two checks here:
158 * 1. the flag file /etc/initrd-release must exist
159 * 2. the root file system must be a memory file system
161 * The second check is extra paranoia, since misdetecting an
162 * initrd can have bad consequences due the initrd
163 * emptying when transititioning to the main systemd.
166 saved_in_initrd
= access("/etc/initrd-release", F_OK
) >= 0 &&
167 statfs("/", &s
) >= 0 &&
170 return saved_in_initrd
;
173 void in_initrd_force(bool value
) {
174 saved_in_initrd
= value
;
177 /* hey glibc, APIs with callbacks without a user pointer are so useless */
178 void *xbsearch_r(const void *key
, const void *base
, size_t nmemb
, size_t size
,
179 int (*compar
) (const void *, const void *, void *), void *arg
) {
184 assert(!size_multiply_overflow(nmemb
, size
));
190 p
= (const uint8_t*) base
+ idx
* size
;
191 comparison
= compar(key
, p
, arg
);
194 else if (comparison
> 0)
202 int on_ac_power(void) {
203 bool found_offline
= false, found_online
= false;
204 _cleanup_closedir_
DIR *d
= NULL
;
207 d
= opendir("/sys/class/power_supply");
209 return errno
== ENOENT
? true : -errno
;
211 FOREACH_DIRENT(de
, d
, return -errno
) {
212 _cleanup_close_
int fd
= -1, device
= -1;
216 device
= openat(dirfd(d
), de
->d_name
, O_DIRECTORY
|O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
218 if (IN_SET(errno
, ENOENT
, ENOTDIR
))
224 fd
= openat(device
, "type", O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
232 n
= read(fd
, contents
, sizeof(contents
));
236 if (n
!= 6 || memcmp(contents
, "Mains\n", 6))
240 fd
= openat(device
, "online", O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
248 n
= read(fd
, contents
, sizeof(contents
));
252 if (n
!= 2 || contents
[1] != '\n')
255 if (contents
[0] == '1') {
258 } else if (contents
[0] == '0')
259 found_offline
= true;
264 return found_online
|| !found_offline
;
267 int container_get_leader(const char *machine
, pid_t
*pid
) {
268 _cleanup_free_
char *s
= NULL
, *class = NULL
;
276 if (!machine_name_is_valid(machine
))
279 p
= strjoina("/run/systemd/machines/", machine
);
280 r
= parse_env_file(p
, NEWLINE
, "LEADER", &s
, "CLASS", &class, NULL
);
288 if (!streq_ptr(class, "container"))
291 r
= parse_pid(s
, &leader
);
301 int namespace_open(pid_t pid
, int *pidns_fd
, int *mntns_fd
, int *netns_fd
, int *userns_fd
, int *root_fd
) {
302 _cleanup_close_
int pidnsfd
= -1, mntnsfd
= -1, netnsfd
= -1, usernsfd
= -1;
310 mntns
= procfs_file_alloca(pid
, "ns/mnt");
311 mntnsfd
= open(mntns
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
);
319 pidns
= procfs_file_alloca(pid
, "ns/pid");
320 pidnsfd
= open(pidns
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
);
328 netns
= procfs_file_alloca(pid
, "ns/net");
329 netnsfd
= open(netns
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
);
337 userns
= procfs_file_alloca(pid
, "ns/user");
338 usernsfd
= open(userns
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
);
339 if (usernsfd
< 0 && errno
!= ENOENT
)
346 root
= procfs_file_alloca(pid
, "root");
347 rfd
= open(root
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
|O_DIRECTORY
);
362 *userns_fd
= usernsfd
;
367 pidnsfd
= mntnsfd
= netnsfd
= usernsfd
= -1;
372 int namespace_enter(int pidns_fd
, int mntns_fd
, int netns_fd
, int userns_fd
, int root_fd
) {
373 if (userns_fd
>= 0) {
374 /* Can't setns to your own userns, since then you could
375 * escalate from non-root to root in your own namespace, so
376 * check if namespaces equal before attempting to enter. */
377 _cleanup_free_
char *userns_fd_path
= NULL
;
379 if (asprintf(&userns_fd_path
, "/proc/self/fd/%d", userns_fd
) < 0)
382 r
= files_same(userns_fd_path
, "/proc/self/ns/user", 0);
390 if (setns(pidns_fd
, CLONE_NEWPID
) < 0)
394 if (setns(mntns_fd
, CLONE_NEWNS
) < 0)
398 if (setns(netns_fd
, CLONE_NEWNET
) < 0)
402 if (setns(userns_fd
, CLONE_NEWUSER
) < 0)
406 if (fchdir(root_fd
) < 0)
413 return reset_uid_gid();
416 uint64_t physical_memory(void) {
417 _cleanup_free_
char *root
= NULL
, *value
= NULL
;
422 /* We return this as uint64_t in case we are running as 32bit process on a 64bit kernel with huge amounts of
425 * In order to support containers nicely that have a configured memory limit we'll take the minimum of the
426 * physically reported amount of memory and the limit configured for the root cgroup, if there is any. */
428 sc
= sysconf(_SC_PHYS_PAGES
);
432 mem
= (uint64_t) sc
* (uint64_t) ps
;
434 if (cg_get_root_path(&root
) < 0)
437 if (cg_get_attribute("memory", root
, "memory.limit_in_bytes", &value
))
440 if (safe_atou64(value
, &lim
) < 0)
443 /* Make sure the limit is a multiple of our own page size */
447 return MIN(mem
, lim
);
450 uint64_t physical_memory_scale(uint64_t v
, uint64_t max
) {
451 uint64_t p
, m
, ps
, r
;
455 /* Returns the physical memory size, multiplied by v divided by max. Returns UINT64_MAX on overflow. On success
456 * the result is a multiple of the page size (rounds down). */
461 p
= physical_memory() / ps
;
477 uint64_t system_tasks_max(void) {
479 uint64_t a
= TASKS_MAX
, b
= TASKS_MAX
;
480 _cleanup_free_
char *root
= NULL
;
482 /* Determine the maximum number of tasks that may run on this system. We check three sources to determine this
485 * a) the maximum tasks value the kernel allows on this architecture
486 * b) the cgroups pids_max attribute for the system
487 * c) the kernel's configured maximum PID value
489 * And then pick the smallest of the three */
491 (void) procfs_tasks_get_limit(&a
);
493 if (cg_get_root_path(&root
) >= 0) {
494 _cleanup_free_
char *value
= NULL
;
496 if (cg_get_attribute("pids", root
, "pids.max", &value
) >= 0)
497 (void) safe_atou64(value
, &b
);
500 return MIN3(TASKS_MAX
,
501 a
<= 0 ? TASKS_MAX
: a
,
502 b
<= 0 ? TASKS_MAX
: b
);
505 uint64_t system_tasks_max_scale(uint64_t v
, uint64_t max
) {
510 /* Multiply the system's task value by the fraction v/max. Hence, if max==100 this calculates percentages
511 * relative to the system's maximum number of tasks. Returns UINT64_MAX on overflow. */
513 t
= system_tasks_max();
517 if (m
/ t
!= v
) /* overflow? */
524 puts(PACKAGE_STRING
"\n"
529 /* This is a direct translation of str_verscmp from boot.c */
530 static bool is_digit(int c
) {
531 return c
>= '0' && c
<= '9';
534 static int c_order(int c
) {
535 if (c
== 0 || is_digit(c
))
538 if ((c
>= 'a') && (c
<= 'z'))
544 int str_verscmp(const char *s1
, const char *s2
) {
545 const char *os1
, *os2
;
556 while ((*s1
&& !is_digit(*s1
)) || (*s2
&& !is_digit(*s2
))) {
559 order
= c_order(*s1
) - c_order(*s2
);
572 while (is_digit(*s1
) && is_digit(*s2
)) {
588 return strcmp(os1
, os2
);
591 /* Turn off core dumps but only if we're running outside of a container. */
592 void disable_coredumps(void) {
595 if (detect_container() > 0)
598 r
= write_string_file("/proc/sys/kernel/core_pattern", "|/bin/false", 0);
600 log_debug_errno(r
, "Failed to turn off coredumps, ignoring: %m");