logind-button.c revision 0d0f0c50d3a1d90f03972a6abb82e6413daaa583
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2012 Lennart Poettering
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <assert.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include "conf-parser.h"
#include "util.h"
#include "logind-button.h"
#include "special.h"
#include "dbus-common.h"
Button *b;
assert(m);
if (!b)
return NULL;
if (!b->name) {
free(b);
return NULL;
}
free(b);
return NULL;
}
b->manager = m;
b->fd = -1;
return b;
}
void button_free(Button *b) {
assert(b);
if (b->fd >= 0) {
close_nointr_nofail(b->fd);
}
free(b);
}
char *s;
assert(b);
if (!s)
return -ENOMEM;
b->seat = s;
return 0;
}
int button_open(Button *b) {
char name[256], *p;
struct epoll_event ev;
int r;
assert(b);
if (b->fd >= 0) {
close_nointr_nofail(b->fd);
b->fd = -1;
}
if (!p)
return log_oom();
free(p);
if (b->fd < 0) {
return -errno;
}
log_error("Failed to get input name: %m");
r = -errno;
goto fail;
}
log_error("Failed to add to epoll: %m");
r = -errno;
goto fail;
}
if (r < 0) {
goto fail;
}
return 0;
fail:
close_nointr_nofail(b->fd);
b->fd = -1;
return r;
}
assert(b);
if (!b->seat)
return NULL;
if (!seat)
return NULL;
}
int r;
assert(b);
if (handle == HANDLE_OFF)
return 0;
if (handle == HANDLE_NO_SESSION) {
log_error("Refusing power-off, user is logged in.");
warn_melody();
return -EPERM;
}
} else if (handle == HANDLE_TTY_SESSION ||
handle == HANDLE_ANY_SESSION) {
unsigned n;
Session *s;
s = button_get_session(b);
/* Silently ignore events of graphical sessions */
if (handle == HANDLE_TTY_SESSION &&
s && s->type == SESSION_X11)
return 0;
if (n > 1 || (n == 1 && !s)) {
log_error("Refusing power-off, other user is logged in.");
warn_melody();
return -EPERM;
}
}
if (handle != HANDLE_ALWAYS) {
log_error("Refusing power-off, shutdown is inhibited.");
warn_melody();
return -EPERM;
}
}
log_info("Powering off...");
r = bus_manager_shutdown_or_sleep_now_or_later(b->manager, SPECIAL_POWEROFF_TARGET, INHIBIT_SHUTDOWN, &error);
if (r < 0) {
}
return r;
}
int r;
assert(b);
if (handle == HANDLE_OFF)
return 0;
if (handle == HANDLE_NO_SESSION) {
log_error("Refusing suspend, user is logged in.");
warn_melody();
return -EPERM;
}
} else if (handle == HANDLE_TTY_SESSION ||
handle == HANDLE_ANY_SESSION) {
unsigned n;
Session *s;
s = button_get_session(b);
/* Silently ignore events of graphical sessions */
if (handle == HANDLE_TTY_SESSION &&
s && s->type == SESSION_X11)
return 0;
if (n > 1 || (n == 1 && !s)) {
log_error("Refusing suspend, other user is logged in.");
warn_melody();
return -EPERM;
}
}
if (handle != HANDLE_ALWAYS) {
log_error("Refusing suspend, sleeping is inhibited.");
warn_melody();
return -EPERM;
}
}
log_info("Suspending...");
r = bus_manager_shutdown_or_sleep_now_or_later(b->manager, SPECIAL_SUSPEND_TARGET, INHIBIT_SLEEP, &error);
if (r < 0) {
}
return r;
}
int button_process(Button *b) {
struct input_event ev;
ssize_t l;
assert(b);
if (l < 0)
return -EIO;
case KEY_POWER:
case KEY_POWER2:
log_info("Power key pressed.");
case KEY_SLEEP:
case KEY_SUSPEND:
log_info("Sleep key pressed.");
}
case SW_LID:
log_info("Lid closed.");
}
}
return 0;
}
static const char* const handle_button_table[_HANDLE_BUTTON_MAX] = {
[HANDLE_OFF] = "off",
[HANDLE_NO_SESSION] = "no-session",
[HANDLE_TTY_SESSION] = "tty-session",
[HANDLE_ANY_SESSION] = "any-session",
[HANDLE_ALWAYS] = "always"
};
DEFINE_CONFIG_PARSE_ENUM(config_parse_handle_button, handle_button, HandleButton, "Failed to parse handle button setting");