]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/util.c
basic/extract-word,man: clarify "correction" of invalid escapes
[thirdparty/systemd.git] / src / basic / 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 <alloca.h>
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <sched.h>
24 #include <signal.h>
25 #include <stdarg.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <sys/mman.h>
30 #include <sys/prctl.h>
31 #include <sys/statfs.h>
32 #include <sys/sysmacros.h>
33 #include <sys/types.h>
34 #include <unistd.h>
35
36 #include "alloc-util.h"
37 #include "build.h"
38 #include "cgroup-util.h"
39 #include "def.h"
40 #include "dirent-util.h"
41 #include "fd-util.h"
42 #include "fileio.h"
43 #include "format-util.h"
44 #include "hashmap.h"
45 #include "hostname-util.h"
46 #include "log.h"
47 #include "macro.h"
48 #include "missing.h"
49 #include "parse-util.h"
50 #include "path-util.h"
51 #include "process-util.h"
52 #include "set.h"
53 #include "signal-util.h"
54 #include "stat-util.h"
55 #include "string-util.h"
56 #include "strv.h"
57 #include "time-util.h"
58 #include "umask-util.h"
59 #include "user-util.h"
60 #include "util.h"
61
62 /* Put this test here for a lack of better place */
63 assert_cc(EAGAIN == EWOULDBLOCK);
64
65 int saved_argc = 0;
66 char **saved_argv = NULL;
67 static int saved_in_initrd = -1;
68
69 size_t page_size(void) {
70 static thread_local size_t pgsz = 0;
71 long r;
72
73 if (_likely_(pgsz > 0))
74 return pgsz;
75
76 r = sysconf(_SC_PAGESIZE);
77 assert(r > 0);
78
79 pgsz = (size_t) r;
80 return pgsz;
81 }
82
83 static int do_execute(char **directories, usec_t timeout, char *argv[]) {
84 _cleanup_hashmap_free_free_ Hashmap *pids = NULL;
85 _cleanup_set_free_free_ Set *seen = NULL;
86 char **directory;
87
88 /* We fork this all off from a child process so that we can
89 * somewhat cleanly make use of SIGALRM to set a time limit */
90
91 (void) reset_all_signal_handlers();
92 (void) reset_signal_mask();
93
94 assert_se(prctl(PR_SET_PDEATHSIG, SIGTERM) == 0);
95
96 pids = hashmap_new(NULL);
97 if (!pids)
98 return log_oom();
99
100 seen = set_new(&string_hash_ops);
101 if (!seen)
102 return log_oom();
103
104 STRV_FOREACH(directory, directories) {
105 _cleanup_closedir_ DIR *d;
106 struct dirent *de;
107
108 d = opendir(*directory);
109 if (!d) {
110 if (errno == ENOENT)
111 continue;
112
113 return log_error_errno(errno, "Failed to open directory %s: %m", *directory);
114 }
115
116 FOREACH_DIRENT(de, d, break) {
117 _cleanup_free_ char *path = NULL;
118 pid_t pid;
119 int r;
120
121 if (!dirent_is_file(de))
122 continue;
123
124 if (set_contains(seen, de->d_name)) {
125 log_debug("%1$s/%2$s skipped (%2$s was already seen).", *directory, de->d_name);
126 continue;
127 }
128
129 r = set_put_strdup(seen, de->d_name);
130 if (r < 0)
131 return log_oom();
132
133 path = strjoin(*directory, "/", de->d_name);
134 if (!path)
135 return log_oom();
136
137 if (null_or_empty_path(path)) {
138 log_debug("%s is empty (a mask).", path);
139 continue;
140 }
141
142 pid = fork();
143 if (pid < 0) {
144 log_error_errno(errno, "Failed to fork: %m");
145 continue;
146 } else if (pid == 0) {
147 char *_argv[2];
148
149 assert_se(prctl(PR_SET_PDEATHSIG, SIGTERM) == 0);
150
151 if (!argv) {
152 _argv[0] = path;
153 _argv[1] = NULL;
154 argv = _argv;
155 } else
156 argv[0] = path;
157
158 execv(path, argv);
159 return log_error_errno(errno, "Failed to execute %s: %m", path);
160 }
161
162 log_debug("Spawned %s as " PID_FMT ".", path, pid);
163
164 r = hashmap_put(pids, PID_TO_PTR(pid), path);
165 if (r < 0)
166 return log_oom();
167 path = NULL;
168 }
169 }
170
171 /* Abort execution of this process after the timout. We simply
172 * rely on SIGALRM as default action terminating the process,
173 * and turn on alarm(). */
174
175 if (timeout != USEC_INFINITY)
176 alarm((timeout + USEC_PER_SEC - 1) / USEC_PER_SEC);
177
178 while (!hashmap_isempty(pids)) {
179 _cleanup_free_ char *path = NULL;
180 pid_t pid;
181
182 pid = PTR_TO_PID(hashmap_first_key(pids));
183 assert(pid > 0);
184
185 path = hashmap_remove(pids, PID_TO_PTR(pid));
186 assert(path);
187
188 wait_for_terminate_and_warn(path, pid, true);
189 }
190
191 return 0;
192 }
193
194 void execute_directories(const char* const* directories, usec_t timeout, char *argv[]) {
195 pid_t executor_pid;
196 int r;
197 char *name;
198 char **dirs = (char**) directories;
199
200 assert(!strv_isempty(dirs));
201
202 name = basename(dirs[0]);
203 assert(!isempty(name));
204
205 /* Executes all binaries in the directories in parallel and waits
206 * for them to finish. Optionally a timeout is applied. If a file
207 * with the same name exists in more than one directory, the
208 * earliest one wins. */
209
210 executor_pid = fork();
211 if (executor_pid < 0) {
212 log_error_errno(errno, "Failed to fork: %m");
213 return;
214
215 } else if (executor_pid == 0) {
216 r = do_execute(dirs, timeout, argv);
217 _exit(r < 0 ? EXIT_FAILURE : EXIT_SUCCESS);
218 }
219
220 wait_for_terminate_and_warn(name, executor_pid, true);
221 }
222
223 bool plymouth_running(void) {
224 return access("/run/plymouth/pid", F_OK) >= 0;
225 }
226
227 bool display_is_local(const char *display) {
228 assert(display);
229
230 return
231 display[0] == ':' &&
232 display[1] >= '0' &&
233 display[1] <= '9';
234 }
235
236 int socket_from_display(const char *display, char **path) {
237 size_t k;
238 char *f, *c;
239
240 assert(display);
241 assert(path);
242
243 if (!display_is_local(display))
244 return -EINVAL;
245
246 k = strspn(display+1, "0123456789");
247
248 f = new(char, strlen("/tmp/.X11-unix/X") + k + 1);
249 if (!f)
250 return -ENOMEM;
251
252 c = stpcpy(f, "/tmp/.X11-unix/X");
253 memcpy(c, display+1, k);
254 c[k] = 0;
255
256 *path = f;
257
258 return 0;
259 }
260
261 int block_get_whole_disk(dev_t d, dev_t *ret) {
262 char *p, *s;
263 int r;
264 unsigned n, m;
265
266 assert(ret);
267
268 /* If it has a queue this is good enough for us */
269 if (asprintf(&p, "/sys/dev/block/%u:%u/queue", major(d), minor(d)) < 0)
270 return -ENOMEM;
271
272 r = access(p, F_OK);
273 free(p);
274
275 if (r >= 0) {
276 *ret = d;
277 return 0;
278 }
279
280 /* If it is a partition find the originating device */
281 if (asprintf(&p, "/sys/dev/block/%u:%u/partition", major(d), minor(d)) < 0)
282 return -ENOMEM;
283
284 r = access(p, F_OK);
285 free(p);
286
287 if (r < 0)
288 return -ENOENT;
289
290 /* Get parent dev_t */
291 if (asprintf(&p, "/sys/dev/block/%u:%u/../dev", major(d), minor(d)) < 0)
292 return -ENOMEM;
293
294 r = read_one_line_file(p, &s);
295 free(p);
296
297 if (r < 0)
298 return r;
299
300 r = sscanf(s, "%u:%u", &m, &n);
301 free(s);
302
303 if (r != 2)
304 return -EINVAL;
305
306 /* Only return this if it is really good enough for us. */
307 if (asprintf(&p, "/sys/dev/block/%u:%u/queue", m, n) < 0)
308 return -ENOMEM;
309
310 r = access(p, F_OK);
311 free(p);
312
313 if (r >= 0) {
314 *ret = makedev(m, n);
315 return 0;
316 }
317
318 return -ENOENT;
319 }
320
321 bool kexec_loaded(void) {
322 bool loaded = false;
323 char *s;
324
325 if (read_one_line_file("/sys/kernel/kexec_loaded", &s) >= 0) {
326 if (s[0] == '1')
327 loaded = true;
328 free(s);
329 }
330 return loaded;
331 }
332
333 int prot_from_flags(int flags) {
334
335 switch (flags & O_ACCMODE) {
336
337 case O_RDONLY:
338 return PROT_READ;
339
340 case O_WRONLY:
341 return PROT_WRITE;
342
343 case O_RDWR:
344 return PROT_READ|PROT_WRITE;
345
346 default:
347 return -EINVAL;
348 }
349 }
350
351 int fork_agent(pid_t *pid, const int except[], unsigned n_except, const char *path, ...) {
352 bool stdout_is_tty, stderr_is_tty;
353 pid_t parent_pid, agent_pid;
354 sigset_t ss, saved_ss;
355 unsigned n, i;
356 va_list ap;
357 char **l;
358
359 assert(pid);
360 assert(path);
361
362 /* Spawns a temporary TTY agent, making sure it goes away when
363 * we go away */
364
365 parent_pid = getpid();
366
367 /* First we temporarily block all signals, so that the new
368 * child has them blocked initially. This way, we can be sure
369 * that SIGTERMs are not lost we might send to the agent. */
370 assert_se(sigfillset(&ss) >= 0);
371 assert_se(sigprocmask(SIG_SETMASK, &ss, &saved_ss) >= 0);
372
373 agent_pid = fork();
374 if (agent_pid < 0) {
375 assert_se(sigprocmask(SIG_SETMASK, &saved_ss, NULL) >= 0);
376 return -errno;
377 }
378
379 if (agent_pid != 0) {
380 assert_se(sigprocmask(SIG_SETMASK, &saved_ss, NULL) >= 0);
381 *pid = agent_pid;
382 return 0;
383 }
384
385 /* In the child:
386 *
387 * Make sure the agent goes away when the parent dies */
388 if (prctl(PR_SET_PDEATHSIG, SIGTERM) < 0)
389 _exit(EXIT_FAILURE);
390
391 /* Make sure we actually can kill the agent, if we need to, in
392 * case somebody invoked us from a shell script that trapped
393 * SIGTERM or so... */
394 (void) reset_all_signal_handlers();
395 (void) reset_signal_mask();
396
397 /* Check whether our parent died before we were able
398 * to set the death signal and unblock the signals */
399 if (getppid() != parent_pid)
400 _exit(EXIT_SUCCESS);
401
402 /* Don't leak fds to the agent */
403 close_all_fds(except, n_except);
404
405 stdout_is_tty = isatty(STDOUT_FILENO);
406 stderr_is_tty = isatty(STDERR_FILENO);
407
408 if (!stdout_is_tty || !stderr_is_tty) {
409 int fd;
410
411 /* Detach from stdout/stderr. and reopen
412 * /dev/tty for them. This is important to
413 * ensure that when systemctl is started via
414 * popen() or a similar call that expects to
415 * read EOF we actually do generate EOF and
416 * not delay this indefinitely by because we
417 * keep an unused copy of stdin around. */
418 fd = open("/dev/tty", O_WRONLY);
419 if (fd < 0) {
420 log_error_errno(errno, "Failed to open /dev/tty: %m");
421 _exit(EXIT_FAILURE);
422 }
423
424 if (!stdout_is_tty && dup2(fd, STDOUT_FILENO) < 0) {
425 log_error_errno(errno, "Failed to dup2 /dev/tty: %m");
426 _exit(EXIT_FAILURE);
427 }
428
429 if (!stderr_is_tty && dup2(fd, STDERR_FILENO) < 0) {
430 log_error_errno(errno, "Failed to dup2 /dev/tty: %m");
431 _exit(EXIT_FAILURE);
432 }
433
434 if (fd > STDERR_FILENO)
435 close(fd);
436 }
437
438 /* Count arguments */
439 va_start(ap, path);
440 for (n = 0; va_arg(ap, char*); n++)
441 ;
442 va_end(ap);
443
444 /* Allocate strv */
445 l = alloca(sizeof(char *) * (n + 1));
446
447 /* Fill in arguments */
448 va_start(ap, path);
449 for (i = 0; i <= n; i++)
450 l[i] = va_arg(ap, char*);
451 va_end(ap);
452
453 execv(path, l);
454 _exit(EXIT_FAILURE);
455 }
456
457 bool in_initrd(void) {
458 struct statfs s;
459
460 if (saved_in_initrd >= 0)
461 return saved_in_initrd;
462
463 /* We make two checks here:
464 *
465 * 1. the flag file /etc/initrd-release must exist
466 * 2. the root file system must be a memory file system
467 *
468 * The second check is extra paranoia, since misdetecting an
469 * initrd can have bad consequences due the initrd
470 * emptying when transititioning to the main systemd.
471 */
472
473 saved_in_initrd = access("/etc/initrd-release", F_OK) >= 0 &&
474 statfs("/", &s) >= 0 &&
475 is_temporary_fs(&s);
476
477 return saved_in_initrd;
478 }
479
480 void in_initrd_force(bool value) {
481 saved_in_initrd = value;
482 }
483
484 /* hey glibc, APIs with callbacks without a user pointer are so useless */
485 void *xbsearch_r(const void *key, const void *base, size_t nmemb, size_t size,
486 int (*compar) (const void *, const void *, void *), void *arg) {
487 size_t l, u, idx;
488 const void *p;
489 int comparison;
490
491 l = 0;
492 u = nmemb;
493 while (l < u) {
494 idx = (l + u) / 2;
495 p = (void *)(((const char *) base) + (idx * size));
496 comparison = compar(key, p, arg);
497 if (comparison < 0)
498 u = idx;
499 else if (comparison > 0)
500 l = idx + 1;
501 else
502 return (void *)p;
503 }
504 return NULL;
505 }
506
507 int on_ac_power(void) {
508 bool found_offline = false, found_online = false;
509 _cleanup_closedir_ DIR *d = NULL;
510 struct dirent *de;
511
512 d = opendir("/sys/class/power_supply");
513 if (!d)
514 return errno == ENOENT ? true : -errno;
515
516 FOREACH_DIRENT(de, d, return -errno) {
517 _cleanup_close_ int fd = -1, device = -1;
518 char contents[6];
519 ssize_t n;
520
521 device = openat(dirfd(d), de->d_name, O_DIRECTORY|O_RDONLY|O_CLOEXEC|O_NOCTTY);
522 if (device < 0) {
523 if (errno == ENOENT || errno == ENOTDIR)
524 continue;
525
526 return -errno;
527 }
528
529 fd = openat(device, "type", O_RDONLY|O_CLOEXEC|O_NOCTTY);
530 if (fd < 0) {
531 if (errno == ENOENT)
532 continue;
533
534 return -errno;
535 }
536
537 n = read(fd, contents, sizeof(contents));
538 if (n < 0)
539 return -errno;
540
541 if (n != 6 || memcmp(contents, "Mains\n", 6))
542 continue;
543
544 safe_close(fd);
545 fd = openat(device, "online", O_RDONLY|O_CLOEXEC|O_NOCTTY);
546 if (fd < 0) {
547 if (errno == ENOENT)
548 continue;
549
550 return -errno;
551 }
552
553 n = read(fd, contents, sizeof(contents));
554 if (n < 0)
555 return -errno;
556
557 if (n != 2 || contents[1] != '\n')
558 return -EIO;
559
560 if (contents[0] == '1') {
561 found_online = true;
562 break;
563 } else if (contents[0] == '0')
564 found_offline = true;
565 else
566 return -EIO;
567 }
568
569 return found_online || !found_offline;
570 }
571
572 int container_get_leader(const char *machine, pid_t *pid) {
573 _cleanup_free_ char *s = NULL, *class = NULL;
574 const char *p;
575 pid_t leader;
576 int r;
577
578 assert(machine);
579 assert(pid);
580
581 if (!machine_name_is_valid(machine))
582 return -EINVAL;
583
584 p = strjoina("/run/systemd/machines/", machine);
585 r = parse_env_file(p, NEWLINE, "LEADER", &s, "CLASS", &class, NULL);
586 if (r == -ENOENT)
587 return -EHOSTDOWN;
588 if (r < 0)
589 return r;
590 if (!s)
591 return -EIO;
592
593 if (!streq_ptr(class, "container"))
594 return -EIO;
595
596 r = parse_pid(s, &leader);
597 if (r < 0)
598 return r;
599 if (leader <= 1)
600 return -EIO;
601
602 *pid = leader;
603 return 0;
604 }
605
606 int namespace_open(pid_t pid, int *pidns_fd, int *mntns_fd, int *netns_fd, int *userns_fd, int *root_fd) {
607 _cleanup_close_ int pidnsfd = -1, mntnsfd = -1, netnsfd = -1, usernsfd = -1;
608 int rfd = -1;
609
610 assert(pid >= 0);
611
612 if (mntns_fd) {
613 const char *mntns;
614
615 mntns = procfs_file_alloca(pid, "ns/mnt");
616 mntnsfd = open(mntns, O_RDONLY|O_NOCTTY|O_CLOEXEC);
617 if (mntnsfd < 0)
618 return -errno;
619 }
620
621 if (pidns_fd) {
622 const char *pidns;
623
624 pidns = procfs_file_alloca(pid, "ns/pid");
625 pidnsfd = open(pidns, O_RDONLY|O_NOCTTY|O_CLOEXEC);
626 if (pidnsfd < 0)
627 return -errno;
628 }
629
630 if (netns_fd) {
631 const char *netns;
632
633 netns = procfs_file_alloca(pid, "ns/net");
634 netnsfd = open(netns, O_RDONLY|O_NOCTTY|O_CLOEXEC);
635 if (netnsfd < 0)
636 return -errno;
637 }
638
639 if (userns_fd) {
640 const char *userns;
641
642 userns = procfs_file_alloca(pid, "ns/user");
643 usernsfd = open(userns, O_RDONLY|O_NOCTTY|O_CLOEXEC);
644 if (usernsfd < 0 && errno != ENOENT)
645 return -errno;
646 }
647
648 if (root_fd) {
649 const char *root;
650
651 root = procfs_file_alloca(pid, "root");
652 rfd = open(root, O_RDONLY|O_NOCTTY|O_CLOEXEC|O_DIRECTORY);
653 if (rfd < 0)
654 return -errno;
655 }
656
657 if (pidns_fd)
658 *pidns_fd = pidnsfd;
659
660 if (mntns_fd)
661 *mntns_fd = mntnsfd;
662
663 if (netns_fd)
664 *netns_fd = netnsfd;
665
666 if (userns_fd)
667 *userns_fd = usernsfd;
668
669 if (root_fd)
670 *root_fd = rfd;
671
672 pidnsfd = mntnsfd = netnsfd = usernsfd = -1;
673
674 return 0;
675 }
676
677 int namespace_enter(int pidns_fd, int mntns_fd, int netns_fd, int userns_fd, int root_fd) {
678 if (userns_fd >= 0) {
679 /* Can't setns to your own userns, since then you could
680 * escalate from non-root to root in your own namespace, so
681 * check if namespaces equal before attempting to enter. */
682 _cleanup_free_ char *userns_fd_path = NULL;
683 int r;
684 if (asprintf(&userns_fd_path, "/proc/self/fd/%d", userns_fd) < 0)
685 return -ENOMEM;
686
687 r = files_same(userns_fd_path, "/proc/self/ns/user");
688 if (r < 0)
689 return r;
690 if (r)
691 userns_fd = -1;
692 }
693
694 if (pidns_fd >= 0)
695 if (setns(pidns_fd, CLONE_NEWPID) < 0)
696 return -errno;
697
698 if (mntns_fd >= 0)
699 if (setns(mntns_fd, CLONE_NEWNS) < 0)
700 return -errno;
701
702 if (netns_fd >= 0)
703 if (setns(netns_fd, CLONE_NEWNET) < 0)
704 return -errno;
705
706 if (userns_fd >= 0)
707 if (setns(userns_fd, CLONE_NEWUSER) < 0)
708 return -errno;
709
710 if (root_fd >= 0) {
711 if (fchdir(root_fd) < 0)
712 return -errno;
713
714 if (chroot(".") < 0)
715 return -errno;
716 }
717
718 return reset_uid_gid();
719 }
720
721 uint64_t physical_memory(void) {
722 _cleanup_free_ char *root = NULL, *value = NULL;
723 uint64_t mem, lim;
724 size_t ps;
725 long sc;
726
727 /* We return this as uint64_t in case we are running as 32bit process on a 64bit kernel with huge amounts of
728 * memory.
729 *
730 * In order to support containers nicely that have a configured memory limit we'll take the minimum of the
731 * physically reported amount of memory and the limit configured for the root cgroup, if there is any. */
732
733 sc = sysconf(_SC_PHYS_PAGES);
734 assert(sc > 0);
735
736 ps = page_size();
737 mem = (uint64_t) sc * (uint64_t) ps;
738
739 if (cg_get_root_path(&root) < 0)
740 return mem;
741
742 if (cg_get_attribute("memory", root, "memory.limit_in_bytes", &value))
743 return mem;
744
745 if (safe_atou64(value, &lim) < 0)
746 return mem;
747
748 /* Make sure the limit is a multiple of our own page size */
749 lim /= ps;
750 lim *= ps;
751
752 return MIN(mem, lim);
753 }
754
755 uint64_t physical_memory_scale(uint64_t v, uint64_t max) {
756 uint64_t p, m, ps, r;
757
758 assert(max > 0);
759
760 /* Returns the physical memory size, multiplied by v divided by max. Returns UINT64_MAX on overflow. On success
761 * the result is a multiple of the page size (rounds down). */
762
763 ps = page_size();
764 assert(ps > 0);
765
766 p = physical_memory() / ps;
767 assert(p > 0);
768
769 m = p * v;
770 if (m / p != v)
771 return UINT64_MAX;
772
773 m /= max;
774
775 r = m * ps;
776 if (r / ps != m)
777 return UINT64_MAX;
778
779 return r;
780 }
781
782 uint64_t system_tasks_max(void) {
783
784 #if SIZEOF_PID_T == 4
785 #define TASKS_MAX ((uint64_t) (INT32_MAX-1))
786 #elif SIZEOF_PID_T == 2
787 #define TASKS_MAX ((uint64_t) (INT16_MAX-1))
788 #else
789 #error "Unknown pid_t size"
790 #endif
791
792 _cleanup_free_ char *value = NULL, *root = NULL;
793 uint64_t a = TASKS_MAX, b = TASKS_MAX;
794
795 /* Determine the maximum number of tasks that may run on this system. We check three sources to determine this
796 * limit:
797 *
798 * a) the maximum value for the pid_t type
799 * b) the cgroups pids_max attribute for the system
800 * c) the kernel's configure maximum PID value
801 *
802 * And then pick the smallest of the three */
803
804 if (read_one_line_file("/proc/sys/kernel/pid_max", &value) >= 0)
805 (void) safe_atou64(value, &a);
806
807 if (cg_get_root_path(&root) >= 0) {
808 value = mfree(value);
809
810 if (cg_get_attribute("pids", root, "pids.max", &value) >= 0)
811 (void) safe_atou64(value, &b);
812 }
813
814 return MIN3(TASKS_MAX,
815 a <= 0 ? TASKS_MAX : a,
816 b <= 0 ? TASKS_MAX : b);
817 }
818
819 uint64_t system_tasks_max_scale(uint64_t v, uint64_t max) {
820 uint64_t t, m;
821
822 assert(max > 0);
823
824 /* Multiply the system's task value by the fraction v/max. Hence, if max==100 this calculates percentages
825 * relative to the system's maximum number of tasks. Returns UINT64_MAX on overflow. */
826
827 t = system_tasks_max();
828 assert(t > 0);
829
830 m = t * v;
831 if (m / t != v) /* overflow? */
832 return UINT64_MAX;
833
834 return m / max;
835 }
836
837 int update_reboot_parameter_and_warn(const char *param) {
838 int r;
839
840 if (isempty(param)) {
841 if (unlink("/run/systemd/reboot-param") < 0) {
842 if (errno == ENOENT)
843 return 0;
844
845 return log_warning_errno(errno, "Failed to unlink reboot parameter file: %m");
846 }
847
848 return 0;
849 }
850
851 RUN_WITH_UMASK(0022) {
852 r = write_string_file("/run/systemd/reboot-param", param, WRITE_STRING_FILE_CREATE);
853 if (r < 0)
854 return log_warning_errno(r, "Failed to write reboot parameter file: %m");
855 }
856
857 return 0;
858 }
859
860 int version(void) {
861 puts(PACKAGE_STRING "\n"
862 SYSTEMD_FEATURES);
863 return 0;
864 }