logind-session.c revision 5eda94dda25bccda928c4b33c790dbe748573a22
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2011 Lennart Poettering
under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include "logind-session.h"
#include "strv.h"
#include "util.h"
#include "cgroup-util.h"
Session *s;
assert(m);
if (!s)
return NULL;
if (!s->state_file) {
free(s);
return NULL;
}
free(s);
return NULL;
}
s->manager = m;
s->pipe_fd = -1;
s->user = u;
dual_timestamp_get(&s->timestamp);
return s;
}
void session_free(Session *s) {
assert(s);
if (s->user) {
}
if (s->seat)
free(s->cgroup_path);
strv_free(s->controllers);
free(s->remote_host);
free(s->state_file);
free(s);
}
int session_save(Session *s) {
FILE *f;
int r = 0;
assert(s);
if (r < 0)
return r;
if (!f)
return -errno;
fprintf(f,
"# This is private data. Do not parse.\n"
"UID=%lu\n"
"USER=%s\n"
"ACTIVE=%i\n"
"REMOTE=%i\n"
"KILL_PROCESSES=%i\n",
s->remote,
s->kill_processes);
if (s->cgroup_path)
fprintf(f,
"CGROUP=%s\n",
s->cgroup_path);
if (s->seat)
fprintf(f,
"SEAT=%s\n",
if (s->tty)
fprintf(f,
"TTY=%s\n",
s->tty);
if (s->display)
fprintf(f,
"DISPLAY=%s\n",
s->display);
if (s->remote_host)
fprintf(f,
"REMOTE_HOST=%s\n",
s->remote_host);
fprintf(f,
"VTNR=%i\n",
s->vtnr);
if (s->leader > 0)
fprintf(f,
"LEADER=%lu\n",
(unsigned long) s->leader);
if (s->audit_id > 0)
fprintf(f,
"AUDIT=%llu\n",
(unsigned long long) s->audit_id);
fflush(f);
if (ferror(f)) {
r = -errno;
unlink(s->state_file);
}
fclose(f);
return r;
}
int session_load(Session *s) {
assert(s);
return 0;
}
int session_activate(Session *s) {
int r;
assert(s);
if (s->vtnr < 0)
return -ENOTSUP;
if (!s->seat)
return -ENOTSUP;
return 0;
if (r < 0)
return r;
return 0;
}
bool x11_display_is_local(const char *display) {
return
display[0] == ':' &&
}
static int session_link_x11_socket(Session *s) {
char *t, *f, *c;
size_t k;
assert(s);
return 0;
return 0;
f = new(char, sizeof("/tmp/.X11-unix/X") + k);
if (!f) {
log_error("Out of memory");
return -ENOMEM;
}
c = stpcpy(f, "/tmp/.X11-unix/X");
c[k] = 0;
free(f);
return -ENOENT;
}
if (!t) {
log_error("Out of memory");
free(f);
return -ENOMEM;
}
if (link(f, t) < 0) {
unlink(t);
if (link(f, t) >= 0)
goto done;
}
if (symlink(f, t) < 0) {
unlink(t);
if (symlink(f, t) >= 0)
goto done;
}
log_error("Failed to link %s to %s: %m", f, t);
free(f);
free(t);
return -errno;
}
}
done:
log_info("Linked %s to %s.", f, t);
free(f);
free(t);
return 0;
}
static int session_create_cgroup(Session *s) {
char **k;
char *p;
int r;
assert(s);
if (!s->cgroup_path) {
log_error("Out of memory");
return -ENOMEM;
}
} else
p = s->cgroup_path;
if (s->leader > 0)
else
r = cg_create(SYSTEMD_CGROUP_CONTROLLER, p);
if (r < 0) {
free(p);
s->cgroup_path = NULL;
return r;
}
s->cgroup_path = p;
if (s->leader > 0)
r = cg_create_and_attach(*k, p, s->leader);
else
r = cg_create(*k, p);
if (r < 0)
}
return 0;
}
int session_start(Session *s) {
int r;
assert(s);
/* Create user first */
r = user_start(s->user);
if (r < 0)
return r;
/* Create cgroup */
r = session_create_cgroup(s);
if (r < 0)
return r;
/* Create X11 symlink */
return 0;
}
static bool session_shall_kill(Session *s) {
assert(s);
return s->kill_processes;
}
static int session_kill_cgroup(Session *s) {
int r;
char **k;
assert(s);
if (!s->cgroup_path)
return 0;
if (session_shall_kill(s)) {
if (r < 0)
} else {
if (r < 0)
else if (r > 0) {
if (r < 0)
} else
r = -EBUSY;
}
cg_trim(*k, s->cgroup_path, true);
free(s->cgroup_path);
s->cgroup_path = NULL;
return r;
}
static int session_unlink_x11_socket(Session *s) {
char *t;
int r;
assert(s);
return 0;
if (!t) {
log_error("Out of memory");
return -ENOMEM;
}
r = unlink(t);
free(t);
return r < 0 ? -errno : 0;
}
int session_stop(Session *s) {
int r = 0, k;
assert(s);
/* Kill cgroup */
k = session_kill_cgroup(s);
if (k < 0)
r = k;
/* Remove X11 symlink */
return r;
}
bool session_is_active(Session *s) {
assert(s);
if (!s->seat)
return true;
}
int session_check_gc(Session *s) {
int r;
assert(s);
if (s->pipe_fd >= 0) {
if (r < 0)
return r;
if (r <= 0)
return 1;
}
if (s->cgroup_path) {
if (r < 0)
return r;
if (r <= 0)
return 1;
}
return 0;
}
static const char* const session_type_table[_SESSION_TYPE_MAX] = {
[SESSION_TERMINAL] = "terminal",
[SESSION_X11] = "x11"
};