logind-user.c revision b9460fdc8b57102b665a662b82efcced4b1af72b
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering/***
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering This file is part of systemd.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Copyright 2011 Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering systemd is free software; you can redistribute it and/or modify it
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering under the terms of the GNU Lesser General Public License as published by
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering (at your option) any later version.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering systemd is distributed in the hope that it will be useful, but
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Lesser General Public License for more details.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering You should have received a copy of the GNU Lesser General Public License
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering***/
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek#include <sys/mount.h>
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek#include <string.h>
b5efdb8af40ea759a1ea584c1bc44ecc81dd00ceLennart Poettering#include <unistd.h>
4ad7f2761da661853dcc29d542efb4727abb1101Nick Owens#include <errno.h>
7263f72499e962b3fd54cdb7c79d49ca72121edeZbigniew Jędrzejewski-Szmek
e4e73a632524c382139034d4271f53b6089ab4cbLennart Poettering#include "util.h"
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering#include "mkdir.h"
e4e73a632524c382139034d4271f53b6089ab4cbLennart Poettering#include "rm-rf.h"
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering#include "hashmap.h"
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering#include "fileio.h"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include "path-util.h"
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering#include "special.h"
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering#include "unit-name.h"
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering#include "bus-util.h"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include "bus-error.h"
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering#include "conf-parser.h"
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering#include "clean-ipc.h"
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering#include "logind-user.h"
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering#include "smack-util.h"
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering#include "formats-util.h"
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart PoetteringUser* user_new(Manager *m, uid_t uid, gid_t gid, const char *name) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering User *u;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering assert(m);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering assert(name);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering u = new0(User, 1);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (!u)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return NULL;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
7c1ff6ac3d9e3acae1d601d40728cf7ccc9a7730Tom Gundersen u->name = strdup(name);
7c1ff6ac3d9e3acae1d601d40728cf7ccc9a7730Tom Gundersen if (!u->name)
7c1ff6ac3d9e3acae1d601d40728cf7ccc9a7730Tom Gundersen goto fail;
7c1ff6ac3d9e3acae1d601d40728cf7ccc9a7730Tom Gundersen
7c1ff6ac3d9e3acae1d601d40728cf7ccc9a7730Tom Gundersen if (asprintf(&u->state_file, "/run/systemd/users/"UID_FMT, uid) < 0)
7c1ff6ac3d9e3acae1d601d40728cf7ccc9a7730Tom Gundersen goto fail;
36d9205d669bcdcb04fa730d1f3549a9fc9a9001Tom Gundersen
36d9205d669bcdcb04fa730d1f3549a9fc9a9001Tom Gundersen if (hashmap_put(m->users, UID_TO_PTR(uid), u) < 0)
36d9205d669bcdcb04fa730d1f3549a9fc9a9001Tom Gundersen goto fail;
36d9205d669bcdcb04fa730d1f3549a9fc9a9001Tom Gundersen
36d9205d669bcdcb04fa730d1f3549a9fc9a9001Tom Gundersen u->manager = m;
36d9205d669bcdcb04fa730d1f3549a9fc9a9001Tom Gundersen u->uid = uid;
36d9205d669bcdcb04fa730d1f3549a9fc9a9001Tom Gundersen u->gid = gid;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return u;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringfail:
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering free(u->state_file);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering free(u->name);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering free(u);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return NULL;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering}
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringvoid user_free(User *u) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering assert(u);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (u->in_gc_queue)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering LIST_REMOVE(gc_queue, u->manager->user_gc_queue, u);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering while (u->sessions)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering session_free(u->sessions);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (u->slice) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering hashmap_remove(u->manager->user_units, u->slice);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering free(u->slice);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering }
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (u->service) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering hashmap_remove(u->manager->user_units, u->service);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering free(u->service);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering }
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering free(u->slice_job);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering free(u->service_job);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering free(u->runtime_path);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering hashmap_remove(u->manager->users, UID_TO_PTR(u->uid));
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering free(u->name);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering free(u->state_file);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering free(u);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering}
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringint user_save(User *u) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering _cleanup_free_ char *temp_path = NULL;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering _cleanup_fclose_ FILE *f = NULL;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering int r;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering assert(u);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering assert(u->state_file);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (!u->started)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return 0;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering r = mkdir_safe_label("/run/systemd/users", 0755, 0, 0);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (r < 0)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering goto finish;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering r = fopen_temporary(u->state_file, &f, &temp_path);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (r < 0)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering goto finish;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering fchmod(fileno(f), 0644);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering fprintf(f,
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering "# This is private data. Do not parse.\n"
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering "NAME=%s\n"
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering "STATE=%s\n",
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering u->name,
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering user_state_to_string(user_get_state(u)));
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (u->runtime_path)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering fprintf(f, "RUNTIME=%s\n", u->runtime_path);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (u->service)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering fprintf(f, "SERVICE=%s\n", u->service);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (u->service_job)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering fprintf(f, "SERVICE_JOB=%s\n", u->service_job);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (u->slice)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering fprintf(f, "SLICE=%s\n", u->slice);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (u->slice_job)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering fprintf(f, "SLICE_JOB=%s\n", u->slice_job);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (u->display)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering fprintf(f, "DISPLAY=%s\n", u->display->id);
b826ab586c9e0a9c0d438a75c28cf3a8ab485929Tom Gundersen
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering if (dual_timestamp_is_set(&u->timestamp))
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering fprintf(f,
b826ab586c9e0a9c0d438a75c28cf3a8ab485929Tom Gundersen "REALTIME="USEC_FMT"\n"
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering "MONOTONIC="USEC_FMT"\n",
b826ab586c9e0a9c0d438a75c28cf3a8ab485929Tom Gundersen u->timestamp.realtime,
b826ab586c9e0a9c0d438a75c28cf3a8ab485929Tom Gundersen u->timestamp.monotonic);
b826ab586c9e0a9c0d438a75c28cf3a8ab485929Tom Gundersen
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering if (u->sessions) {
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering Session *i;
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt bool first;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering fputs("SESSIONS=", f);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering first = true;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering LIST_FOREACH(sessions_by_user, i, u->sessions) {
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering if (first)
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering first = false;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering else
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering fputc(' ', f);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering fputs(i->id, f);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering }
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering fputs("\nSEATS=", f);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering first = true;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering LIST_FOREACH(sessions_by_user, i, u->sessions) {
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering if (!i->seat)
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering continue;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering if (first)
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering first = false;
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt else
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt fputc(' ', f);
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt fputs(i->seat->id, f);
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt }
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
d23a27a964748967e1ad20e86de869a753af555bTom Gundersen fputs("\nACTIVE_SESSIONS=", f);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering first = true;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering LIST_FOREACH(sessions_by_user, i, u->sessions) {
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (!session_is_active(i))
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering continue;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
d23a27a964748967e1ad20e86de869a753af555bTom Gundersen if (first)
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering first = false;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering else
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering fputc(' ', f);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering fputs(i->id, f);
d23a27a964748967e1ad20e86de869a753af555bTom Gundersen }
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering fputs("\nONLINE_SESSIONS=", f);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering first = true;
23432a1c249b9c513e438bffe9778a7ce2ee74feZbigniew Jędrzejewski-Szmek LIST_FOREACH(sessions_by_user, i, u->sessions) {
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (session_get_state(i) == SESSION_CLOSING)
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering continue;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (first)
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering first = false;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering else
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering fputc(' ', f);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering fputs(i->id, f);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering fputs("\nACTIVE_SEATS=", f);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering first = true;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering LIST_FOREACH(sessions_by_user, i, u->sessions) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (!session_is_active(i) || !i->seat)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering continue;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (first)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering first = false;
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering else
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering fputc(' ', f);
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering fputs(i->seat->id, f);
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering }
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering fputs("\nONLINE_SEATS=", f);
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering first = true;
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering LIST_FOREACH(sessions_by_user, i, u->sessions) {
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering if (session_get_state(i) == SESSION_CLOSING || !i->seat)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering continue;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (first)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering first = false;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering else
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering fputc(' ', f);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering fputs(i->seat->id, f);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering fputc('\n', f);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering fflush(f);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (ferror(f) || rename(temp_path, u->state_file) < 0) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r = -errno;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering unlink(u->state_file);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering unlink(temp_path);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringfinish:
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (r < 0)
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek log_error_errno(r, "Failed to save user data %s: %m", u->state_file);
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering return r;
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering}
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poetteringint user_load(User *u) {
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek _cleanup_free_ char *display = NULL, *realtime = NULL, *monotonic = NULL;
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek Session *s = NULL;
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek int r;
8ac4e9e1e54397f6d1745c2a7a806132418c7da2Lennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering assert(u);
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering r = parse_env_file(u->state_file, NEWLINE,
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek "RUNTIME", &u->runtime_path,
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering "SERVICE", &u->service,
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering "SERVICE_JOB", &u->service_job,
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek "SLICE", &u->slice,
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering "SLICE_JOB", &u->slice_job,
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek "DISPLAY", &display,
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering "REALTIME", &realtime,
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering "MONOTONIC", &monotonic,
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek NULL);
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering if (r < 0) {
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek if (r == -ENOENT)
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering return 0;
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek log_error_errno(r, "Failed to read %s: %m", u->state_file);
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering return r;
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek }
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek if (display)
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering s = hashmap_get(u->manager->sessions, display);
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen if (s && s->display && display_is_local(s->display))
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen u->display = s;
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering if (realtime) {
549c1a2564b56f2bb38f1203d59c747ea15817f3Tom Gundersen unsigned long long l;
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering if (sscanf(realtime, "%llu", &l) > 0)
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering u->timestamp.realtime = l;
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek }
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek if (monotonic) {
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek unsigned long long l;
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek if (sscanf(monotonic, "%llu", &l) > 0)
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek u->timestamp.monotonic = l;
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek }
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek return r;
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen}
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersenstatic int user_mkdir_runtime_path(User *u) {
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen char *p;
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen int r;
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen assert(u);
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen r = mkdir_safe_label("/run/user", 0755, 0, 0);
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen if (r < 0)
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen return log_error_errno(r, "Failed to create /run/user: %m");
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek if (!u->runtime_path) {
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek if (asprintf(&p, "/run/user/" UID_FMT, u->uid) < 0)
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek return log_oom();
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering } else
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek p = u->runtime_path;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek if (path_is_mount_point(p, 0) <= 0) {
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering _cleanup_free_ char *t = NULL;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering (void) mkdir(p, 0700);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (mac_smack_use())
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering r = asprintf(&t, "mode=0700,smackfsroot=*,uid=" UID_FMT ",gid=" GID_FMT ",size=%zu", u->uid, u->gid, u->manager->runtime_dir_size);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering else
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering r = asprintf(&t, "mode=0700,uid=" UID_FMT ",gid=" GID_FMT ",size=%zu", u->uid, u->gid, u->manager->runtime_dir_size);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering if (r < 0) {
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering r = log_oom();
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering goto fail;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering }
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering r = mount("tmpfs", p, "tmpfs", MS_NODEV|MS_NOSUID, t);
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering if (r < 0) {
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering if (errno != EPERM) {
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering r = log_error_errno(errno, "Failed to mount per-user tmpfs directory %s: %m", p);
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering goto fail;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering }
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering /* Lacking permissions, maybe
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering * CAP_SYS_ADMIN-less container? In this case,
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering * just use a normal directory. */
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering r = chmod_and_chown(p, 0700, u->uid, u->gid);
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering if (r < 0) {
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering log_error_errno(r, "Failed to change runtime directory ownership and mode: %m");
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering goto fail;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering }
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering }
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering }
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering u->runtime_path = p;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering return 0;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poetteringfail:
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering if (p) {
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering /* Try to clean up, but ignore errors */
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering (void) rmdir(p);
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering free(p);
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering }
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering u->runtime_path = NULL;
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering return r;
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering}
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poetteringstatic int user_start_slice(User *u) {
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering char *job;
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering int r;
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering assert(u);
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering if (!u->slice) {
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering char lu[DECIMAL_STR_MAX(uid_t) + 1], *slice;
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering sprintf(lu, UID_FMT, u->uid);
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering r = slice_build_subslice(SPECIAL_USER_SLICE, lu, &slice);
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering if (r < 0)
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering return r;
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering r = manager_start_unit(u->manager, slice, &error, &job);
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering if (r < 0) {
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering log_error("Failed to start user slice: %s", bus_error_message(&error, r));
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering free(slice);
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering } else {
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering u->slice = slice;
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering free(u->slice_job);
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering u->slice_job = job;
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering }
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering }
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering if (u->slice)
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering hashmap_put(u->manager->user_units, u->slice, u);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering return 0;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering}
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringstatic int user_start_service(User *u) {
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering char *job;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering int r;
fd0b4602f6332c3f1660eb208c8f5c719709a009Lennart Poettering
fd0b4602f6332c3f1660eb208c8f5c719709a009Lennart Poettering assert(u);
fd0b4602f6332c3f1660eb208c8f5c719709a009Lennart Poettering
fd0b4602f6332c3f1660eb208c8f5c719709a009Lennart Poettering if (!u->service) {
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering char lu[DECIMAL_STR_MAX(uid_t) + 1], *service;
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering sprintf(lu, UID_FMT, u->uid);
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering r = unit_name_build("user", lu, ".service", &service);
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering if (r < 0)
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering return log_error_errno(r, "Failed to build service name: %m");
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering r = manager_start_unit(u->manager, service, &error, &job);
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering if (r < 0) {
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering log_error("Failed to start user service: %s", bus_error_message(&error, r));
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering free(service);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering } else {
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering u->service = service;
8ac4e9e1e54397f6d1745c2a7a806132418c7da2Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering free(u->service_job);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering u->service_job = job;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering }
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering }
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (u->service)
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek hashmap_put(u->manager->user_units, u->service, u);
0f84a72e3c0f58d71cff2121e6df1611eaf9c9eaDavid Herrmann
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering return 0;
2e276efc7b0398a3086629a52970bdd4ab7252f9Zbigniew Jędrzejewski-Szmek}
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poetteringint user_start(User *u) {
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering int r;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering assert(u);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (u->started)
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering return 0;
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering log_debug("New user %s logged in.", u->name);
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering /* Make XDG_RUNTIME_DIR */
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering r = user_mkdir_runtime_path(u);
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering if (r < 0)
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering return r;
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering /* Create cgroup */
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering r = user_start_slice(u);
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering if (r < 0)
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering return r;
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek /* Spawn user systemd */
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek r = user_start_service(u);
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek if (r < 0)
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek return r;
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek if (!dual_timestamp_is_set(&u->timestamp))
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek dual_timestamp_get(&u->timestamp);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek u->started = true;
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek /* Save new user data */
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek user_save(u);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek user_send_signal(u, true);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen return 0;
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen}
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersenstatic int user_stop_slice(User *u) {
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen char *job;
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen int r;
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering assert(u);
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering
549c1a2564b56f2bb38f1203d59c747ea15817f3Tom Gundersen if (!u->slice)
549c1a2564b56f2bb38f1203d59c747ea15817f3Tom Gundersen return 0;
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek r = manager_stop_unit(u->manager, u->slice, &error, &job);
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek if (r < 0) {
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek log_error("Failed to stop user slice: %s", bus_error_message(&error, r));
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek return r;
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek }
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek free(u->slice_job);
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek u->slice_job = job;
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek
03664a62914782dbd8f069bbcf8a0c8ca1df7010Lukas Nykryn return r;
03664a62914782dbd8f069bbcf8a0c8ca1df7010Lukas Nykryn}
03664a62914782dbd8f069bbcf8a0c8ca1df7010Lukas Nykryn
03664a62914782dbd8f069bbcf8a0c8ca1df7010Lukas Nykrynstatic int user_stop_service(User *u) {
03664a62914782dbd8f069bbcf8a0c8ca1df7010Lukas Nykryn _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
03664a62914782dbd8f069bbcf8a0c8ca1df7010Lukas Nykryn char *job;
03664a62914782dbd8f069bbcf8a0c8ca1df7010Lukas Nykryn int r;
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek assert(u);
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek if (!u->service)
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek return 0;
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen r = manager_stop_unit(u->manager, u->service, &error, &job);
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen if (r < 0) {
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen log_error("Failed to stop user service: %s", bus_error_message(&error, r));
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen return r;
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen }
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen free(u->service_job);
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen u->service_job = job;
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen return r;
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen}
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersenstatic int user_remove_runtime_path(User *u) {
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering int r;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering assert(u);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering if (!u->runtime_path)
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering return 0;
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek r = rm_rf(u->runtime_path, 0);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek if (r < 0)
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek log_error_errno(r, "Failed to remove runtime directory %s: %m", u->runtime_path);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek /* Ignore cases where the directory isn't mounted, as that's
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek * quite possible, if we lacked the permissions to mount
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek * something */
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek r = umount2(u->runtime_path, MNT_DETACH);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek if (r < 0 && errno != EINVAL && errno != ENOENT)
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek log_error_errno(errno, "Failed to unmount user runtime directory %s: %m", u->runtime_path);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek r = rm_rf(u->runtime_path, REMOVE_ROOT);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek if (r < 0)
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek log_error_errno(r, "Failed to remove runtime directory %s: %m", u->runtime_path);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek free(u->runtime_path);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek u->runtime_path = NULL;
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek return r;
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek}
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmekint user_stop(User *u, bool force) {
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek Session *s;
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek int r = 0, k;
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek assert(u);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek /* Stop jobs have already been queued */
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek if (u->stopping) {
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek user_save(u);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek return r;
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen }
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen LIST_FOREACH(sessions_by_user, s, u->sessions) {
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen k = session_stop(s, force);
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen if (k < 0)
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen r = k;
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen }
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen /* Kill systemd */
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen k = user_stop_service(u);
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen if (k < 0)
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen r = k;
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen /* Kill cgroup */
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen k = user_stop_slice(u);
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen if (k < 0)
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen r = k;
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
cb57dd41595adddb08095298bb1ed258c8ea4877Tom Gundersen u->stopping = true;
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen user_save(u);
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
cb57dd41595adddb08095298bb1ed258c8ea4877Tom Gundersen return r;
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen}
2c1fb4f71206bf970d493294208c5d7597194856Lennart Poettering
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersenint user_finalize(User *u) {
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen Session *s;
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen int r = 0, k;
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen assert(u);
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen if (u->started)
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen log_debug("User %s logged out.", u->name);
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
2c1fb4f71206bf970d493294208c5d7597194856Lennart Poettering LIST_FOREACH(sessions_by_user, s, u->sessions) {
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen k = session_finalize(s);
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen if (k < 0)
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen r = k;
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen }
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen /* Kill XDG_RUNTIME_DIR */
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen k = user_remove_runtime_path(u);
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen if (k < 0)
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen r = k;
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen /* Clean SysV + POSIX IPC objects */
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen if (u->manager->remove_ipc) {
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering k = clean_ipc(u->uid);
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering if (k < 0)
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering r = k;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering }
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering unlink(u->state_file);
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering user_add_to_gc_queue(u);
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering if (u->started) {
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering user_send_signal(u, false);
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering u->started = false;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering }
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering return r;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering}
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poetteringint user_get_idle_hint(User *u, dual_timestamp *t) {
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering Session *s;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering bool idle_hint = true;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering dual_timestamp ts = { 0, 0 };
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering assert(u);
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering LIST_FOREACH(sessions_by_user, s, u->sessions) {
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering dual_timestamp k;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering int ih;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering ih = session_get_idle_hint(s, &k);
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering if (ih < 0)
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering return ih;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering if (!ih) {
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering if (!idle_hint) {
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering if (k.monotonic < ts.monotonic)
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering ts = k;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering } else {
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering idle_hint = false;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering ts = k;
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek }
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering } else if (idle_hint) {
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering if (k.monotonic > ts.monotonic)
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering ts = k;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering }
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering }
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (t)
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering *t = ts;
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering return idle_hint;
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering}
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poetteringint user_check_linger_file(User *u) {
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering _cleanup_free_ char *cc = NULL;
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering char *p = NULL;
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering cc = cescape(u->name);
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering if (!cc)
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering return -ENOMEM;
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering p = strjoina("/var/lib/systemd/linger/", cc);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return access(p, F_OK) >= 0;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering}
8ac4e9e1e54397f6d1745c2a7a806132418c7da2Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poetteringbool user_check_gc(User *u, bool drop_not_started) {
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering assert(u);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering if (drop_not_started && !u->started)
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return false;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (u->sessions)
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return true;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (user_check_linger_file(u) > 0)
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return true;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek if (u->slice_job && manager_job_is_active(u->manager, u->slice_job))
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek return true;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering
2e276efc7b0398a3086629a52970bdd4ab7252f9Zbigniew Jędrzejewski-Szmek if (u->service_job && manager_job_is_active(u->manager, u->service_job))
2e276efc7b0398a3086629a52970bdd4ab7252f9Zbigniew Jędrzejewski-Szmek return true;
2e276efc7b0398a3086629a52970bdd4ab7252f9Zbigniew Jędrzejewski-Szmek
2e276efc7b0398a3086629a52970bdd4ab7252f9Zbigniew Jędrzejewski-Szmek return false;
2e276efc7b0398a3086629a52970bdd4ab7252f9Zbigniew Jędrzejewski-Szmek}
2e276efc7b0398a3086629a52970bdd4ab7252f9Zbigniew Jędrzejewski-Szmek
2e276efc7b0398a3086629a52970bdd4ab7252f9Zbigniew Jędrzejewski-Szmekvoid user_add_to_gc_queue(User *u) {
2e276efc7b0398a3086629a52970bdd4ab7252f9Zbigniew Jędrzejewski-Szmek assert(u);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (u->in_gc_queue)
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering return;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering LIST_PREPEND(gc_queue, u->manager->user_gc_queue, u);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering u->in_gc_queue = true;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering}
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart PoetteringUserState user_get_state(User *u) {
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering Session *i;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering assert(u);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek if (u->stopping)
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek return USER_CLOSING;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (u->slice_job || u->service_job)
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering return USER_OPENING;
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (u->sessions) {
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering bool all_closing = true;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering LIST_FOREACH(sessions_by_user, i, u->sessions) {
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering SessionState state;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering state = session_get_state(i);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (state == SESSION_ACTIVE)
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return USER_ACTIVE;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (state != SESSION_CLOSING)
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering all_closing = false;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering }
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return all_closing ? USER_CLOSING : USER_ONLINE;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering }
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (user_check_linger_file(u) > 0)
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return USER_LINGERING;
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek return USER_CLOSING;
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek}
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmekint user_kill(User *u, int signo) {
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek assert(u);
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek if (!u->slice)
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek return -ESRCH;
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek return manager_kill_unit(u->manager, u->slice, KILL_ALL, signo, NULL);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek}
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmekstatic bool elect_display_filter(Session *s) {
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek /* Return true if the session is a candidate for the user’s ‘primary
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek * session’ or ‘display’. */
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek assert(s);
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek return (s->class == SESSION_USER && !s->stopping);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek}
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmekstatic int elect_display_compare(Session *s1, Session *s2) {
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek /* Indexed by SessionType. Lower numbers mean more preferred. */
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek const int type_ranks[_SESSION_TYPE_MAX] = {
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek [SESSION_UNSPECIFIED] = 0,
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek [SESSION_TTY] = -2,
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen [SESSION_X11] = -3,
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen [SESSION_WAYLAND] = -3,
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen [SESSION_MIR] = -3,
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen [SESSION_WEB] = -1,
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen };
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen /* Calculate the partial order relationship between s1 and s2,
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen * returning < 0 if s1 is preferred as the user’s ‘primary session’,
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen * 0 if s1 and s2 are equally preferred or incomparable, or > 0 if s2
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen * is preferred.
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen *
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen * s1 or s2 may be NULL. */
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen if (!s1 && !s2)
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen return 0;
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek if ((s1 == NULL) != (s2 == NULL))
549c1a2564b56f2bb38f1203d59c747ea15817f3Tom Gundersen return (s1 == NULL) - (s2 == NULL);
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering if (s1->stopping != s2->stopping)
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering return s1->stopping - s2->stopping;
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering if ((s1->class != SESSION_USER) != (s2->class != SESSION_USER))
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering return (s1->class != SESSION_USER) - (s2->class != SESSION_USER);
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek if ((s1->type == _SESSION_TYPE_INVALID) != (s2->type == _SESSION_TYPE_INVALID))
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering return (s1->type == _SESSION_TYPE_INVALID) - (s2->type == _SESSION_TYPE_INVALID);
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering if (s1->type != s2->type)
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering return type_ranks[s1->type] - type_ranks[s2->type];
ff3d6560bead6879a2fed1bf99bfe8273b3723f1Zbigniew Jędrzejewski-Szmek
ff3d6560bead6879a2fed1bf99bfe8273b3723f1Zbigniew Jędrzejewski-Szmek return 0;
ff3d6560bead6879a2fed1bf99bfe8273b3723f1Zbigniew Jędrzejewski-Szmek}
ff3d6560bead6879a2fed1bf99bfe8273b3723f1Zbigniew Jędrzejewski-Szmek
ff3d6560bead6879a2fed1bf99bfe8273b3723f1Zbigniew Jędrzejewski-Szmekvoid user_elect_display(User *u) {
1bf968f36393666f2c57953b1748e6219c027deeTom Gundersen Session *s;
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek assert(u);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
ff3d6560bead6879a2fed1bf99bfe8273b3723f1Zbigniew Jędrzejewski-Szmek /* This elects a primary session for each user, which we call
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek * the "display". We try to keep the assignment stable, but we
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek * "upgrade" to better choices. */
ff3d6560bead6879a2fed1bf99bfe8273b3723f1Zbigniew Jędrzejewski-Szmek log_debug("Electing new display for user %s", u->name);
ff3d6560bead6879a2fed1bf99bfe8273b3723f1Zbigniew Jędrzejewski-Szmek
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek LIST_FOREACH(sessions_by_user, s, u->sessions) {
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek if (!elect_display_filter(s)) {
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering log_debug("Ignoring session %s", s->id);
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek continue;
ff3d6560bead6879a2fed1bf99bfe8273b3723f1Zbigniew Jędrzejewski-Szmek }
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek if (elect_display_compare(s, u->display) < 0) {
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek log_debug("Choosing session %s in preference to %s", s->id, u->display ? u->display->id : "-");
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen u->display = s;
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek }
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek }
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek}
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek
1bf968f36393666f2c57953b1748e6219c027deeTom Gundersenstatic const char* const user_state_table[_USER_STATE_MAX] = {
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek [USER_OFFLINE] = "offline",
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek [USER_OPENING] = "opening",
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek [USER_LINGERING] = "lingering",
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen [USER_ONLINE] = "online",
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen [USER_ACTIVE] = "active",
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen [USER_CLOSING] = "closing"
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen};
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom GundersenDEFINE_STRING_TABLE_LOOKUP(user_state, UserState);
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersenint config_parse_tmpfs_size(
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek const char* unit,
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek const char *filename,
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek unsigned line,
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen const char *section,
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek unsigned section_line,
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek const char *lvalue,
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek int ltype,
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek const char *rvalue,
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek void *data,
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek void *userdata) {
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen size_t *sz = data;
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen const char *e;
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek int r;
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek assert(filename);
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek assert(lvalue);
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek assert(rvalue);
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek assert(data);
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek e = endswith(rvalue, "%");
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen if (e) {
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen unsigned long ul;
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen char *f;
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen errno = 0;
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen ul = strtoul(rvalue, &f, 10);
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen if (errno != 0 || f != e) {
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen log_syntax(unit, LOG_ERR, filename, line, errno ? errno : EINVAL, "Failed to parse percentage value, ignoring: %s", rvalue);
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen return 0;
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen }
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen if (ul <= 0 || ul >= 100) {
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen log_syntax(unit, LOG_ERR, filename, line, errno ? errno : EINVAL, "Percentage value out of range, ignoring: %s", rvalue);
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen return 0;
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen }
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen
f5430a3ef308f3a102899fcaf7fbece757082f2aLennart Poettering *sz = PAGE_ALIGN((size_t) ((physical_memory() * (uint64_t) ul) / (uint64_t) 100));
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen } else {
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen off_t o;
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen r = parse_size(rvalue, 1024, &o);
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen if (r < 0 || (off_t) (size_t) o != o) {
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen log_syntax(unit, LOG_ERR, filename, line, r < 0 ? -r : ERANGE, "Failed to parse size value, ignoring: %s", rvalue);
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen return 0;
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen }
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen *sz = PAGE_ALIGN((size_t) o);
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen }
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen return 0;
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen}
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen