run.c revision 56f64d95763a799ba4475daf44d8e9f72a1bd474
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2013 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 <stdio.h>
#include <getopt.h>
#include "sd-bus.h"
#include "bus-util.h"
#include "strv.h"
#include "build.h"
#include "unit-name.h"
#include "env-util.h"
#include "path-util.h"
#include "bus-error.h"
static bool arg_scope = false;
static bool arg_remain_after_exit = false;
static const char *arg_description = NULL;
static bool arg_send_sighup = false;
static bool arg_user = false;
static const char *arg_service_type = NULL;
static const char *arg_exec_user = NULL;
static const char *arg_exec_group = NULL;
static int arg_nice = 0;
static bool arg_nice_set = false;
static char **arg_environment = NULL;
static char **arg_property = NULL;
static void help(void) {
printf("%s [OPTIONS...] COMMAND [ARGS...]\n\n"
"Run the specified command in a transient scope or service unit.\n\n"
" -h --help Show this help\n"
" --version Show package version\n"
" --user Run as user unit\n"
" -H --host=[USER@]HOST Operate on remote host\n"
" -M --machine=CONTAINER Operate on local container\n"
" --scope Run this as scope rather than service\n"
" --unit=UNIT Run under the specified unit name\n"
" -p --property=NAME=VALUE Set unit property\n"
" --description=TEXT Description for unit\n"
" --slice=SLICE Run in the specified slice\n"
" -r --remain-after-exit Leave service around until explicitly stopped\n"
" --send-sighup Send SIGHUP when terminating\n"
" --service-type=TYPE Service type\n"
" --uid=USER Run as system user\n"
" --gid=GROUP Run as system group\n"
" --nice=NICE Nice level\n"
" --setenv=NAME=VALUE Set environment\n",
}
enum {
ARG_VERSION = 0x100,
};
{},
};
int r, c;
switch (c) {
case 'h':
help();
return 0;
case ARG_VERSION:
return 0;
case ARG_USER:
arg_user = true;
break;
case ARG_SYSTEM:
arg_user = false;
break;
case ARG_SCOPE:
arg_scope = true;
break;
case ARG_UNIT:
break;
case ARG_DESCRIPTION:
break;
case ARG_SLICE:
break;
case ARG_SEND_SIGHUP:
arg_send_sighup = true;
break;
case 'r':
arg_remain_after_exit = true;
break;
case 'H':
break;
case 'M':
break;
case ARG_SERVICE_TYPE:
break;
case ARG_EXEC_USER:
break;
case ARG_EXEC_GROUP:
break;
case ARG_NICE:
log_error("Failed to parse nice value");
return -EINVAL;
}
arg_nice_set = true;
break;
case ARG_SETENV:
return log_oom();
break;
case 'p':
return log_oom();
break;
case '?':
return -EINVAL;
default:
assert_not_reached("Unhandled option");
}
log_error("Command line to execute required.");
return -EINVAL;
}
log_error("Execution in user context is not supported on non-local systems.");
return -EINVAL;
}
log_error("Scope execution is not supported on non-local systems.");
return -EINVAL;
}
log_error("--remain-after-exit and --service-type= are not supported in --scope mode.");
return -EINVAL;
}
return 1;
}
char **i;
int r;
bus,
&m,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"StartTransientUnit");
if (r < 0)
return r;
if (r < 0)
return r;
if (r < 0)
return r;
STRV_FOREACH(i, arg_property) {
if (r < 0)
return r;
r = bus_append_unit_property_assignment(m, *i);
if (r < 0)
return r;
r = sd_bus_message_close_container(m);
if (r < 0)
return r;
}
if (r < 0)
return r;
_cleanup_free_ char *slice;
if (!slice)
return -ENOMEM;
if (r < 0)
return r;
}
if (arg_send_sighup) {
if (r < 0)
return r;
}
*ret = m;
m = NULL;
return 0;
}
static int message_start_transient_unit_send(sd_bus *bus, sd_bus_message *m, sd_bus_error *error, sd_bus_message **reply) {
int r;
assert(m);
r = sd_bus_message_close_container(m);
if (r < 0)
return r;
r = sd_bus_message_append(m, "a(sa(sv))", 0);
if (r < 0)
return r;
}
static int start_transient_service(
char **argv,
sd_bus_error *error) {
int r;
if (arg_unit) {
if (!name)
return log_oom();
return log_oom();
if (r < 0)
return bus_log_create_error(r);
if (arg_remain_after_exit) {
if (r < 0)
return bus_log_create_error(r);
}
if (arg_service_type) {
if (r < 0)
return bus_log_create_error(r);
}
if (arg_exec_user) {
if (r < 0)
return bus_log_create_error(r);
}
if (arg_exec_group) {
if (r < 0)
return bus_log_create_error(r);
}
if (arg_nice_set) {
if (r < 0)
return bus_log_create_error(r);
}
if (!strv_isempty(arg_environment)) {
if (r < 0)
return bus_log_create_error(r);
if (r < 0)
return bus_log_create_error(r);
if (r < 0)
return bus_log_create_error(r);
if (r < 0)
return bus_log_create_error(r);
r = sd_bus_message_close_container(m);
if (r < 0)
return bus_log_create_error(r);
r = sd_bus_message_close_container(m);
if (r < 0)
return bus_log_create_error(r);
}
if (r < 0)
return bus_log_create_error(r);
if (r < 0)
return bus_log_create_error(r);
if (r < 0)
return bus_log_create_error(r);
if (r < 0)
return bus_log_create_error(r);
if (r < 0)
return bus_log_create_error(r);
if (r < 0)
return bus_log_create_error(r);
r = sd_bus_message_append_strv(m, argv);
if (r < 0)
return bus_log_create_error(r);
r = sd_bus_message_append(m, "b", false);
if (r < 0)
return bus_log_create_error(r);
r = sd_bus_message_close_container(m);
if (r < 0)
return bus_log_create_error(r);
r = sd_bus_message_close_container(m);
if (r < 0)
return bus_log_create_error(r);
r = sd_bus_message_close_container(m);
if (r < 0)
return bus_log_create_error(r);
r = sd_bus_message_close_container(m);
if (r < 0)
return bus_log_create_error(r);
if (r < 0)
return bus_log_create_error(r);
return 0;
}
static int start_transient_scope(
char **argv,
sd_bus_error *error) {
int r;
if (arg_unit) {
if (!name)
return log_oom();
return log_oom();
if (r < 0)
return bus_log_create_error(r);
if (r < 0)
return bus_log_create_error(r);
if (r < 0)
return bus_log_create_error(r);
if (arg_nice_set) {
return -errno;
}
}
if (arg_exec_group) {
if (r < 0)
return -errno;
}
}
if (arg_exec_user) {
if (r < 0)
if (r < 0)
return log_oom();
if (r < 0)
return log_oom();
if (r < 0)
return log_oom();
if (r < 0)
return log_oom();
if (!arg_exec_group) {
return -errno;
}
}
return -errno;
}
}
if (!env)
return log_oom();
return -errno;
}
int r;
log_open();
if (r <= 0)
goto finish;
if (r < 0) {
log_error_errno(r, "Failed to find executable %s%s: %m",
goto finish;
}
if (!arg_description) {
if (!description) {
r = log_oom();
goto finish;
}
}
if (r < 0) {
log_error_errno(r, "Failed to create bus connection: %m");
goto finish;
}
if (arg_scope)
else
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}