]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/login/logind-user.c
Merge pull request #3757 from poettering/efi-search
[thirdparty/systemd.git] / src / login / logind-user.c
1 /***
2 This file is part of systemd.
3
4 Copyright 2011 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 <errno.h>
21 #include <string.h>
22 #include <sys/mount.h>
23 #include <unistd.h>
24
25 #include "alloc-util.h"
26 #include "bus-common-errors.h"
27 #include "bus-error.h"
28 #include "bus-util.h"
29 #include "clean-ipc.h"
30 #include "conf-parser.h"
31 #include "escape.h"
32 #include "fd-util.h"
33 #include "fileio.h"
34 #include "formats-util.h"
35 #include "fs-util.h"
36 #include "hashmap.h"
37 #include "label.h"
38 #include "logind-user.h"
39 #include "mkdir.h"
40 #include "mount-util.h"
41 #include "parse-util.h"
42 #include "path-util.h"
43 #include "rm-rf.h"
44 #include "smack-util.h"
45 #include "special.h"
46 #include "stdio-util.h"
47 #include "string-table.h"
48 #include "unit-name.h"
49 #include "user-util.h"
50 #include "util.h"
51
52 int user_new(User **out, Manager *m, uid_t uid, gid_t gid, const char *name) {
53 _cleanup_(user_freep) User *u = NULL;
54 char lu[DECIMAL_STR_MAX(uid_t) + 1];
55 int r;
56
57 assert(out);
58 assert(m);
59 assert(name);
60
61 u = new0(User, 1);
62 if (!u)
63 return -ENOMEM;
64
65 u->manager = m;
66 u->uid = uid;
67 u->gid = gid;
68 xsprintf(lu, UID_FMT, uid);
69
70 u->name = strdup(name);
71 if (!u->name)
72 return -ENOMEM;
73
74 if (asprintf(&u->state_file, "/run/systemd/users/"UID_FMT, uid) < 0)
75 return -ENOMEM;
76
77 if (asprintf(&u->runtime_path, "/run/user/"UID_FMT, uid) < 0)
78 return -ENOMEM;
79
80 r = slice_build_subslice(SPECIAL_USER_SLICE, lu, &u->slice);
81 if (r < 0)
82 return r;
83
84 r = unit_name_build("user", lu, ".service", &u->service);
85 if (r < 0)
86 return r;
87
88 r = hashmap_put(m->users, UID_TO_PTR(uid), u);
89 if (r < 0)
90 return r;
91
92 r = hashmap_put(m->user_units, u->slice, u);
93 if (r < 0)
94 return r;
95
96 r = hashmap_put(m->user_units, u->service, u);
97 if (r < 0)
98 return r;
99
100 *out = u;
101 u = NULL;
102 return 0;
103 }
104
105 User *user_free(User *u) {
106 if (!u)
107 return NULL;
108
109 if (u->in_gc_queue)
110 LIST_REMOVE(gc_queue, u->manager->user_gc_queue, u);
111
112 while (u->sessions)
113 session_free(u->sessions);
114
115 if (u->service)
116 hashmap_remove_value(u->manager->user_units, u->service, u);
117
118 if (u->slice)
119 hashmap_remove_value(u->manager->user_units, u->slice, u);
120
121 hashmap_remove_value(u->manager->users, UID_TO_PTR(u->uid), u);
122
123 u->slice_job = mfree(u->slice_job);
124 u->service_job = mfree(u->service_job);
125
126 u->service = mfree(u->service);
127 u->slice = mfree(u->slice);
128 u->runtime_path = mfree(u->runtime_path);
129 u->state_file = mfree(u->state_file);
130 u->name = mfree(u->name);
131
132 return mfree(u);
133 }
134
135 static int user_save_internal(User *u) {
136 _cleanup_free_ char *temp_path = NULL;
137 _cleanup_fclose_ FILE *f = NULL;
138 int r;
139
140 assert(u);
141 assert(u->state_file);
142
143 r = mkdir_safe_label("/run/systemd/users", 0755, 0, 0);
144 if (r < 0)
145 goto fail;
146
147 r = fopen_temporary(u->state_file, &f, &temp_path);
148 if (r < 0)
149 goto fail;
150
151 fchmod(fileno(f), 0644);
152
153 fprintf(f,
154 "# This is private data. Do not parse.\n"
155 "NAME=%s\n"
156 "STATE=%s\n",
157 u->name,
158 user_state_to_string(user_get_state(u)));
159
160 /* LEGACY: no-one reads RUNTIME= anymore, drop it at some point */
161 if (u->runtime_path)
162 fprintf(f, "RUNTIME=%s\n", u->runtime_path);
163
164 if (u->service_job)
165 fprintf(f, "SERVICE_JOB=%s\n", u->service_job);
166
167 if (u->slice_job)
168 fprintf(f, "SLICE_JOB=%s\n", u->slice_job);
169
170 if (u->display)
171 fprintf(f, "DISPLAY=%s\n", u->display->id);
172
173 if (dual_timestamp_is_set(&u->timestamp))
174 fprintf(f,
175 "REALTIME="USEC_FMT"\n"
176 "MONOTONIC="USEC_FMT"\n",
177 u->timestamp.realtime,
178 u->timestamp.monotonic);
179
180 if (u->sessions) {
181 Session *i;
182 bool first;
183
184 fputs("SESSIONS=", f);
185 first = true;
186 LIST_FOREACH(sessions_by_user, i, u->sessions) {
187 if (first)
188 first = false;
189 else
190 fputc(' ', f);
191
192 fputs(i->id, f);
193 }
194
195 fputs("\nSEATS=", f);
196 first = true;
197 LIST_FOREACH(sessions_by_user, i, u->sessions) {
198 if (!i->seat)
199 continue;
200
201 if (first)
202 first = false;
203 else
204 fputc(' ', f);
205
206 fputs(i->seat->id, f);
207 }
208
209 fputs("\nACTIVE_SESSIONS=", f);
210 first = true;
211 LIST_FOREACH(sessions_by_user, i, u->sessions) {
212 if (!session_is_active(i))
213 continue;
214
215 if (first)
216 first = false;
217 else
218 fputc(' ', f);
219
220 fputs(i->id, f);
221 }
222
223 fputs("\nONLINE_SESSIONS=", f);
224 first = true;
225 LIST_FOREACH(sessions_by_user, i, u->sessions) {
226 if (session_get_state(i) == SESSION_CLOSING)
227 continue;
228
229 if (first)
230 first = false;
231 else
232 fputc(' ', f);
233
234 fputs(i->id, f);
235 }
236
237 fputs("\nACTIVE_SEATS=", f);
238 first = true;
239 LIST_FOREACH(sessions_by_user, i, u->sessions) {
240 if (!session_is_active(i) || !i->seat)
241 continue;
242
243 if (first)
244 first = false;
245 else
246 fputc(' ', f);
247
248 fputs(i->seat->id, f);
249 }
250
251 fputs("\nONLINE_SEATS=", f);
252 first = true;
253 LIST_FOREACH(sessions_by_user, i, u->sessions) {
254 if (session_get_state(i) == SESSION_CLOSING || !i->seat)
255 continue;
256
257 if (first)
258 first = false;
259 else
260 fputc(' ', f);
261
262 fputs(i->seat->id, f);
263 }
264 fputc('\n', f);
265 }
266
267 r = fflush_and_check(f);
268 if (r < 0)
269 goto fail;
270
271 if (rename(temp_path, u->state_file) < 0) {
272 r = -errno;
273 goto fail;
274 }
275
276 return 0;
277
278 fail:
279 (void) unlink(u->state_file);
280
281 if (temp_path)
282 (void) unlink(temp_path);
283
284 return log_error_errno(r, "Failed to save user data %s: %m", u->state_file);
285 }
286
287 int user_save(User *u) {
288 assert(u);
289
290 if (!u->started)
291 return 0;
292
293 return user_save_internal (u);
294 }
295
296 int user_load(User *u) {
297 _cleanup_free_ char *display = NULL, *realtime = NULL, *monotonic = NULL;
298 Session *s = NULL;
299 int r;
300
301 assert(u);
302
303 r = parse_env_file(u->state_file, NEWLINE,
304 "SERVICE_JOB", &u->service_job,
305 "SLICE_JOB", &u->slice_job,
306 "DISPLAY", &display,
307 "REALTIME", &realtime,
308 "MONOTONIC", &monotonic,
309 NULL);
310 if (r < 0) {
311 if (r == -ENOENT)
312 return 0;
313
314 return log_error_errno(r, "Failed to read %s: %m", u->state_file);
315 }
316
317 if (display)
318 s = hashmap_get(u->manager->sessions, display);
319
320 if (s && s->display && display_is_local(s->display))
321 u->display = s;
322
323 if (realtime)
324 timestamp_deserialize(realtime, &u->timestamp.realtime);
325 if (monotonic)
326 timestamp_deserialize(monotonic, &u->timestamp.monotonic);
327
328 return r;
329 }
330
331 static int user_mkdir_runtime_path(User *u) {
332 int r;
333
334 assert(u);
335
336 r = mkdir_safe_label("/run/user", 0755, 0, 0);
337 if (r < 0)
338 return log_error_errno(r, "Failed to create /run/user: %m");
339
340 if (path_is_mount_point(u->runtime_path, 0) <= 0) {
341 _cleanup_free_ char *t = NULL;
342
343 (void) mkdir_label(u->runtime_path, 0700);
344
345 if (mac_smack_use())
346 r = asprintf(&t, "mode=0700,smackfsroot=*,uid=" UID_FMT ",gid=" GID_FMT ",size=%zu", u->uid, u->gid, u->manager->runtime_dir_size);
347 else
348 r = asprintf(&t, "mode=0700,uid=" UID_FMT ",gid=" GID_FMT ",size=%zu", u->uid, u->gid, u->manager->runtime_dir_size);
349 if (r < 0) {
350 r = log_oom();
351 goto fail;
352 }
353
354 r = mount("tmpfs", u->runtime_path, "tmpfs", MS_NODEV|MS_NOSUID, t);
355 if (r < 0) {
356 if (errno != EPERM) {
357 r = log_error_errno(errno, "Failed to mount per-user tmpfs directory %s: %m", u->runtime_path);
358 goto fail;
359 }
360
361 /* Lacking permissions, maybe
362 * CAP_SYS_ADMIN-less container? In this case,
363 * just use a normal directory. */
364
365 r = chmod_and_chown(u->runtime_path, 0700, u->uid, u->gid);
366 if (r < 0) {
367 log_error_errno(r, "Failed to change runtime directory ownership and mode: %m");
368 goto fail;
369 }
370 }
371
372 r = label_fix(u->runtime_path, false, false);
373 if (r < 0)
374 log_warning_errno(r, "Failed to fix label of '%s', ignoring: %m", u->runtime_path);
375 }
376
377 return 0;
378
379 fail:
380 /* Try to clean up, but ignore errors */
381 (void) rmdir(u->runtime_path);
382 return r;
383 }
384
385 static int user_start_slice(User *u) {
386 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
387 const char *description;
388 char *job;
389 int r;
390
391 assert(u);
392
393 u->slice_job = mfree(u->slice_job);
394 description = strjoina("User Slice of ", u->name);
395
396 r = manager_start_slice(
397 u->manager,
398 u->slice,
399 description,
400 "systemd-logind.service",
401 "systemd-user-sessions.service",
402 u->manager->user_tasks_max,
403 &error,
404 &job);
405 if (r >= 0)
406 u->slice_job = job;
407 else if (!sd_bus_error_has_name(&error, BUS_ERROR_UNIT_EXISTS))
408 /* we don't fail due to this, let's try to continue */
409 log_error_errno(r, "Failed to start user slice %s, ignoring: %s (%s)",
410 u->slice, bus_error_message(&error, r), error.name);
411
412 return 0;
413 }
414
415 static int user_start_service(User *u) {
416 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
417 char *job;
418 int r;
419
420 assert(u);
421
422 u->service_job = mfree(u->service_job);
423
424 r = manager_start_unit(
425 u->manager,
426 u->service,
427 &error,
428 &job);
429 if (r < 0) {
430 /* we don't fail due to this, let's try to continue */
431 log_error_errno(r, "Failed to start user service, ignoring: %s", bus_error_message(&error, r));
432 } else {
433 u->service_job = job;
434 }
435
436 return 0;
437 }
438
439 int user_start(User *u) {
440 int r;
441
442 assert(u);
443
444 if (u->started && !u->stopping)
445 return 0;
446
447 /*
448 * If u->stopping is set, the user is marked for removal and the slice
449 * and service stop-jobs are queued. We have to clear that flag before
450 * queing the start-jobs again. If they succeed, the user object can be
451 * re-used just fine (pid1 takes care of job-ordering and proper
452 * restart), but if they fail, we want to force another user_stop() so
453 * possibly pending units are stopped.
454 * Note that we don't clear u->started, as we have no clue what state
455 * the user is in on failure here. Hence, we pretend the user is
456 * running so it will be properly taken down by GC. However, we clearly
457 * return an error from user_start() in that case, so no further
458 * reference to the user is taken.
459 */
460 u->stopping = false;
461
462 if (!u->started) {
463 log_debug("New user %s logged in.", u->name);
464
465 /* Make XDG_RUNTIME_DIR */
466 r = user_mkdir_runtime_path(u);
467 if (r < 0)
468 return r;
469 }
470
471 /* Create cgroup */
472 r = user_start_slice(u);
473 if (r < 0)
474 return r;
475
476 /* Save the user data so far, because pam_systemd will read the
477 * XDG_RUNTIME_DIR out of it while starting up systemd --user.
478 * We need to do user_save_internal() because we have not
479 * "officially" started yet. */
480 user_save_internal(u);
481
482 /* Spawn user systemd */
483 r = user_start_service(u);
484 if (r < 0)
485 return r;
486
487 if (!u->started) {
488 if (!dual_timestamp_is_set(&u->timestamp))
489 dual_timestamp_get(&u->timestamp);
490 user_send_signal(u, true);
491 u->started = true;
492 }
493
494 /* Save new user data */
495 user_save(u);
496
497 return 0;
498 }
499
500 static int user_stop_slice(User *u) {
501 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
502 char *job;
503 int r;
504
505 assert(u);
506
507 r = manager_stop_unit(u->manager, u->slice, &error, &job);
508 if (r < 0) {
509 log_error("Failed to stop user slice: %s", bus_error_message(&error, r));
510 return r;
511 }
512
513 free(u->slice_job);
514 u->slice_job = job;
515
516 return r;
517 }
518
519 static int user_stop_service(User *u) {
520 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
521 char *job;
522 int r;
523
524 assert(u);
525
526 r = manager_stop_unit(u->manager, u->service, &error, &job);
527 if (r < 0) {
528 log_error("Failed to stop user service: %s", bus_error_message(&error, r));
529 return r;
530 }
531
532 free(u->service_job);
533 u->service_job = job;
534
535 return r;
536 }
537
538 static int user_remove_runtime_path(User *u) {
539 int r;
540
541 assert(u);
542
543 r = rm_rf(u->runtime_path, 0);
544 if (r < 0)
545 log_error_errno(r, "Failed to remove runtime directory %s: %m", u->runtime_path);
546
547 /* Ignore cases where the directory isn't mounted, as that's
548 * quite possible, if we lacked the permissions to mount
549 * something */
550 r = umount2(u->runtime_path, MNT_DETACH);
551 if (r < 0 && errno != EINVAL && errno != ENOENT)
552 log_error_errno(errno, "Failed to unmount user runtime directory %s: %m", u->runtime_path);
553
554 r = rm_rf(u->runtime_path, REMOVE_ROOT);
555 if (r < 0)
556 log_error_errno(r, "Failed to remove runtime directory %s: %m", u->runtime_path);
557
558 return r;
559 }
560
561 int user_stop(User *u, bool force) {
562 Session *s;
563 int r = 0, k;
564 assert(u);
565
566 /* Stop jobs have already been queued */
567 if (u->stopping) {
568 user_save(u);
569 return r;
570 }
571
572 LIST_FOREACH(sessions_by_user, s, u->sessions) {
573 k = session_stop(s, force);
574 if (k < 0)
575 r = k;
576 }
577
578 /* Kill systemd */
579 k = user_stop_service(u);
580 if (k < 0)
581 r = k;
582
583 /* Kill cgroup */
584 k = user_stop_slice(u);
585 if (k < 0)
586 r = k;
587
588 u->stopping = true;
589
590 user_save(u);
591
592 return r;
593 }
594
595 int user_finalize(User *u) {
596 Session *s;
597 int r = 0, k;
598
599 assert(u);
600
601 if (u->started)
602 log_debug("User %s logged out.", u->name);
603
604 LIST_FOREACH(sessions_by_user, s, u->sessions) {
605 k = session_finalize(s);
606 if (k < 0)
607 r = k;
608 }
609
610 /* Kill XDG_RUNTIME_DIR */
611 k = user_remove_runtime_path(u);
612 if (k < 0)
613 r = k;
614
615 /* Clean SysV + POSIX IPC objects */
616 if (u->manager->remove_ipc) {
617 k = clean_ipc(u->uid);
618 if (k < 0)
619 r = k;
620 }
621
622 unlink(u->state_file);
623 user_add_to_gc_queue(u);
624
625 if (u->started) {
626 user_send_signal(u, false);
627 u->started = false;
628 }
629
630 return r;
631 }
632
633 int user_get_idle_hint(User *u, dual_timestamp *t) {
634 Session *s;
635 bool idle_hint = true;
636 dual_timestamp ts = DUAL_TIMESTAMP_NULL;
637
638 assert(u);
639
640 LIST_FOREACH(sessions_by_user, s, u->sessions) {
641 dual_timestamp k;
642 int ih;
643
644 ih = session_get_idle_hint(s, &k);
645 if (ih < 0)
646 return ih;
647
648 if (!ih) {
649 if (!idle_hint) {
650 if (k.monotonic < ts.monotonic)
651 ts = k;
652 } else {
653 idle_hint = false;
654 ts = k;
655 }
656 } else if (idle_hint) {
657
658 if (k.monotonic > ts.monotonic)
659 ts = k;
660 }
661 }
662
663 if (t)
664 *t = ts;
665
666 return idle_hint;
667 }
668
669 int user_check_linger_file(User *u) {
670 _cleanup_free_ char *cc = NULL;
671 char *p = NULL;
672
673 cc = cescape(u->name);
674 if (!cc)
675 return -ENOMEM;
676
677 p = strjoina("/var/lib/systemd/linger/", cc);
678
679 return access(p, F_OK) >= 0;
680 }
681
682 bool user_check_gc(User *u, bool drop_not_started) {
683 assert(u);
684
685 if (drop_not_started && !u->started)
686 return false;
687
688 if (u->sessions)
689 return true;
690
691 if (user_check_linger_file(u) > 0)
692 return true;
693
694 if (u->slice_job && manager_job_is_active(u->manager, u->slice_job))
695 return true;
696
697 if (u->service_job && manager_job_is_active(u->manager, u->service_job))
698 return true;
699
700 return false;
701 }
702
703 void user_add_to_gc_queue(User *u) {
704 assert(u);
705
706 if (u->in_gc_queue)
707 return;
708
709 LIST_PREPEND(gc_queue, u->manager->user_gc_queue, u);
710 u->in_gc_queue = true;
711 }
712
713 UserState user_get_state(User *u) {
714 Session *i;
715
716 assert(u);
717
718 if (u->stopping)
719 return USER_CLOSING;
720
721 if (!u->started || u->slice_job || u->service_job)
722 return USER_OPENING;
723
724 if (u->sessions) {
725 bool all_closing = true;
726
727 LIST_FOREACH(sessions_by_user, i, u->sessions) {
728 SessionState state;
729
730 state = session_get_state(i);
731 if (state == SESSION_ACTIVE)
732 return USER_ACTIVE;
733 if (state != SESSION_CLOSING)
734 all_closing = false;
735 }
736
737 return all_closing ? USER_CLOSING : USER_ONLINE;
738 }
739
740 if (user_check_linger_file(u) > 0)
741 return USER_LINGERING;
742
743 return USER_CLOSING;
744 }
745
746 int user_kill(User *u, int signo) {
747 assert(u);
748
749 return manager_kill_unit(u->manager, u->slice, KILL_ALL, signo, NULL);
750 }
751
752 static bool elect_display_filter(Session *s) {
753 /* Return true if the session is a candidate for the user’s ‘primary
754 * session’ or ‘display’. */
755 assert(s);
756
757 return (s->class == SESSION_USER && !s->stopping);
758 }
759
760 static int elect_display_compare(Session *s1, Session *s2) {
761 /* Indexed by SessionType. Lower numbers mean more preferred. */
762 const int type_ranks[_SESSION_TYPE_MAX] = {
763 [SESSION_UNSPECIFIED] = 0,
764 [SESSION_TTY] = -2,
765 [SESSION_X11] = -3,
766 [SESSION_WAYLAND] = -3,
767 [SESSION_MIR] = -3,
768 [SESSION_WEB] = -1,
769 };
770
771 /* Calculate the partial order relationship between s1 and s2,
772 * returning < 0 if s1 is preferred as the user’s ‘primary session’,
773 * 0 if s1 and s2 are equally preferred or incomparable, or > 0 if s2
774 * is preferred.
775 *
776 * s1 or s2 may be NULL. */
777 if (!s1 && !s2)
778 return 0;
779
780 if ((s1 == NULL) != (s2 == NULL))
781 return (s1 == NULL) - (s2 == NULL);
782
783 if (s1->stopping != s2->stopping)
784 return s1->stopping - s2->stopping;
785
786 if ((s1->class != SESSION_USER) != (s2->class != SESSION_USER))
787 return (s1->class != SESSION_USER) - (s2->class != SESSION_USER);
788
789 if ((s1->type == _SESSION_TYPE_INVALID) != (s2->type == _SESSION_TYPE_INVALID))
790 return (s1->type == _SESSION_TYPE_INVALID) - (s2->type == _SESSION_TYPE_INVALID);
791
792 if (s1->type != s2->type)
793 return type_ranks[s1->type] - type_ranks[s2->type];
794
795 return 0;
796 }
797
798 void user_elect_display(User *u) {
799 Session *s;
800
801 assert(u);
802
803 /* This elects a primary session for each user, which we call
804 * the "display". We try to keep the assignment stable, but we
805 * "upgrade" to better choices. */
806 log_debug("Electing new display for user %s", u->name);
807
808 LIST_FOREACH(sessions_by_user, s, u->sessions) {
809 if (!elect_display_filter(s)) {
810 log_debug("Ignoring session %s", s->id);
811 continue;
812 }
813
814 if (elect_display_compare(s, u->display) < 0) {
815 log_debug("Choosing session %s in preference to %s", s->id, u->display ? u->display->id : "-");
816 u->display = s;
817 }
818 }
819 }
820
821 static const char* const user_state_table[_USER_STATE_MAX] = {
822 [USER_OFFLINE] = "offline",
823 [USER_OPENING] = "opening",
824 [USER_LINGERING] = "lingering",
825 [USER_ONLINE] = "online",
826 [USER_ACTIVE] = "active",
827 [USER_CLOSING] = "closing"
828 };
829
830 DEFINE_STRING_TABLE_LOOKUP(user_state, UserState);
831
832 int config_parse_tmpfs_size(
833 const char* unit,
834 const char *filename,
835 unsigned line,
836 const char *section,
837 unsigned section_line,
838 const char *lvalue,
839 int ltype,
840 const char *rvalue,
841 void *data,
842 void *userdata) {
843
844 size_t *sz = data;
845 int r;
846
847 assert(filename);
848 assert(lvalue);
849 assert(rvalue);
850 assert(data);
851
852 /* First, try to parse as percentage */
853 r = parse_percent(rvalue);
854 if (r > 0 && r < 100)
855 *sz = physical_memory_scale(r, 100U);
856 else {
857 uint64_t k;
858
859 /* If the passed argument was not a percentage, or out of range, parse as byte size */
860
861 r = parse_size(rvalue, 1024, &k);
862 if (r < 0 || k <= 0 || (uint64_t) (size_t) k != k) {
863 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse size value, ignoring: %s", rvalue);
864 return 0;
865 }
866
867 *sz = PAGE_ALIGN((size_t) k);
868 }
869
870 return 0;
871 }
872
873 int config_parse_user_tasks_max(
874 const char* unit,
875 const char *filename,
876 unsigned line,
877 const char *section,
878 unsigned section_line,
879 const char *lvalue,
880 int ltype,
881 const char *rvalue,
882 void *data,
883 void *userdata) {
884
885 uint64_t *m = data;
886 uint64_t k;
887 int r;
888
889 assert(filename);
890 assert(lvalue);
891 assert(rvalue);
892 assert(data);
893
894 /* First, try to parse as percentage */
895 r = parse_percent(rvalue);
896 if (r > 0 && r < 100)
897 k = system_tasks_max_scale(r, 100U);
898 else {
899
900 /* If the passed argument was not a percentage, or out of range, parse as byte size */
901
902 r = safe_atou64(rvalue, &k);
903 if (r < 0) {
904 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse tasks maximum, ignoring: %s", rvalue);
905 return 0;
906 }
907 }
908
909 if (k <= 0 || k >= UINT64_MAX) {
910 log_syntax(unit, LOG_ERR, filename, line, 0, "Tasks maximum out of range, ignoring: %s", rvalue);
911 return 0;
912 }
913
914 *m = k;
915 return 0;
916 }