inhibit.c revision 4654e558a3c297a71f05c3b2db6a2744fcf3cdea
/*-*- 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 <getopt.h>
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <dbus.h>
#include <unistd.h>
#include "dbus-common.h"
#include "util.h"
#include "build.h"
#include "strv.h"
static const char* arg_what = "idle:sleep:shutdown";
static const char* arg_why = "Unknown reason";
static const char* arg_mode = "block";
static enum {
} arg_action = ACTION_INHIBIT;
int r;
bus,
"org.freedesktop.login1",
"/org/freedesktop/login1",
"org.freedesktop.login1.Manager",
"Inhibit",
&reply,
NULL,
if (r < 0)
return r;
DBUS_TYPE_UNIX_FD, &r,
return -EIO;
return r;
}
unsigned n = 0;
int r;
bus,
"org.freedesktop.login1",
"/org/freedesktop/login1",
"org.freedesktop.login1.Manager",
"ListInhibitors",
&reply,
NULL,
if (r < 0)
return r;
return -ENOMEM;
return -EIO;
return -EIO;
return -EIO;
printf(" Who: %s (UID %lu, PID %lu)\n"
" What: %s\n"
" Why: %s\n"
" Mode: %s\n\n",
what,
why,
mode);
n++;
}
printf("%u inhibitors listed.\n", n);
return 0;
}
static int help(void) {
printf("%s [OPTIONS...] {COMMAND} ...\n\n"
" -h --help Show this help\n"
" --version Show package version\n"
" --what=WHAT Operations to inhibit, colon separated list of:\n"
" shutdown, sleep, idle, handle-power-key,\n"
" handle-suspend-key, handle-hibernate-key,\n"
" handle-lid-switch\n"
" --who=STRING A descriptive string who is inhibiting\n"
" --why=STRING A descriptive string why is being inhibited\n"
" --mode=MODE One of block or delay\n"
" --list List active inhibitors\n",
return 0;
}
enum {
ARG_VERSION = 0x100,
};
};
int c;
switch (c) {
case 'h':
help();
return 0;
case ARG_VERSION:
return 0;
case ARG_WHAT:
break;
case ARG_WHO:
break;
case ARG_WHY:
break;
case ARG_MODE:
break;
case ARG_LIST:
break;
default:
log_error("Unknown option code %c", c);
return -EINVAL;
}
}
log_error("Missing command line to execute.");
return -EINVAL;
}
return 1;
}
int r, exit_code = 0;
log_open();
if (r <= 0)
goto finish;
if (!bus) {
r = -EIO;
goto finish;
}
if (arg_action == ACTION_LIST) {
if (r < 0) {
goto finish;
}
} else {
char *w = NULL;
if (!arg_who)
free(w);
if (fd < 0) {
r = fd;
goto finish;
}
if (pid < 0) {
log_error("Failed to fork: %m");
r = -errno;
goto finish;
}
if (pid == 0) {
/* Child */
close_all_fds(NULL, 0);
}
if (r >= 0)
exit_code = r;
}
if (bus) {
}
return r < 0 ? EXIT_FAILURE : exit_code;
}