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