[systemd-devel] [PATCH v2 4/4] run: introduce timer support option
WaLyong Cho
walyong.cho at samsung.com
Wed Oct 29 23:18:23 PDT 2014
Supported timer options --on-active=, --on-boot=, --on-startup=,
--on-unit-active=, --on-unit-inactive=, --on-calendar=. Each options
corresponding with OnActiveSec=, OnBootSec=, OnStartupSec=,
OnUnitActiveSec=, OnUnitInactiveSec= of timer respectively.
---
man/systemd-run.xml | 42 +++
src/libsystemd/sd-bus/bus-util.c | 14 +-
src/run/run.c | 634 +++++++++++++++++++++++++++++++--------
3 files changed, 571 insertions(+), 119 deletions(-)
diff --git a/man/systemd-run.xml b/man/systemd-run.xml
index 0c9d13d..d359941 100644
--- a/man/systemd-run.xml
+++ b/man/systemd-run.xml
@@ -210,6 +210,37 @@ along with systemd; If not, see <http://www.gnu.org/licenses/>.
<xi:include href="user-system-options.xml" xpointer="host" />
<xi:include href="user-system-options.xml" xpointer="machine" />
+ <varlistentry>
+ <term><option>--on-active=</option></term>
+ <term><option>--on-boot=</option></term>
+ <term><option>--on-startup=</option></term>
+ <term><option>--on-unit-active=</option></term>
+ <term><option>--on-unit-inactive=</option></term>
+
+ <listitem><para>Defines monotonic timers relative to different
+ starting points. Also see <varname>OnActiveSec=</varname>,
+ <varname>OnBootSec=</varname>,
+ <varname>OnStartupSec=</varname>,
+ <varname>OnUnitActiveSec=</varname> and
+ <varname>OnUnitInactiveSec=</varname> in
+ <citerefentry><refentrytitle>systemd.timer</refentrytitle><manvolnum>5</manvolnum></citerefentry>. This
+ option has no effect in conjunction with
+ <option>--scope</option>.</para>
+ </listitem>
+ </varlistentry>
+
+ <varlistentry>
+ <term><option>--on-calendar=</option></term>
+
+ <listitem><para>Defines realtime (i.e. wallclock) timers with
+ calendar event expressions. Also see
+ <varname>OnCalendar=</varname> in
+ <citerefentry><refentrytitle>systemd.timer</refentrytitle><manvolnum>5</manvolnum></citerefentry>. This
+ option has no effect in conjunction with
+ <option>--scope</option>.</para>
+ </listitem>
+ </varlistentry>
+
<xi:include href="standard-options.xml" xpointer="help" />
<xi:include href="standard-options.xml" xpointer="version" />
</variablelist>
@@ -250,6 +281,16 @@ Sep 08 07:37:21 bupkis env[19948]: BOOT_IMAGE=/vmlinuz-3.11.0-0.rc5.git6.2.fc20.
property.</para>
<programlisting># systemd-run -p BlockIOWeight=10 updatedb</programlisting>
+
+ <para>The following command will touch a file after 10 seconds.</para>
+
+ <programlisting># date; systemd-run --on-active=10 touch /tmp/hello
+Mon Oct 27 20:02:57 KST 2014
+Running as unit run-66.timer.
+# journalctl -u run-115.service
+-- Logs begin at Mon 2014-10-27 19:44:57 KST, end at Mon 2014-10-27 20:03:15 KST. --
+Oct 27 20:03:15 container systemd[1]: Starting /bin/touch /tmp/hello...
+Oct 27 20:03:15 container systemd[1]: Started /bin/touch /tmp/hello.</programlisting>
</refsect1>
<refsect1>
@@ -263,6 +304,7 @@ Sep 08 07:37:21 bupkis env[19948]: BOOT_IMAGE=/vmlinuz-3.11.0-0.rc5.git6.2.fc20.
<citerefentry><refentrytitle>systemd.slice</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
<citerefentry><refentrytitle>systemd.exec</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
<citerefentry><refentrytitle>systemd.resource-control</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
+ <citerefentry><refentrytitle>systemd.timer</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
<citerefentry><refentrytitle>machinectl</refentrytitle><manvolnum>1</manvolnum></citerefentry>
</para>
</refsect1>
diff --git a/src/libsystemd/sd-bus/bus-util.c b/src/libsystemd/sd-bus/bus-util.c
index 43acf5b..13fb9e6 100644
--- a/src/libsystemd/sd-bus/bus-util.c
+++ b/src/libsystemd/sd-bus/bus-util.c
@@ -1361,7 +1361,8 @@ int bus_append_unit_property_assignment(sd_bus_message *m, const char *assignmen
if (STR_IN_SET(field,
"CPUAccounting", "MemoryAccounting", "BlockIOAccounting",
- "SendSIGHUP", "SendSIGKILL")) {
+ "SendSIGHUP", "SendSIGKILL",
+ "WakeSystem")) {
r = parse_boolean(eq);
if (r < 0) {
@@ -1522,6 +1523,17 @@ int bus_append_unit_property_assignment(sd_bus_message *m, const char *assignmen
r = sd_bus_message_append(m, "v", "i", sig);
+ } else if (streq(field, "AccuracySec")) {
+ usec_t u;
+
+ r = parse_sec(eq, &u);
+ if (r < 0) {
+ log_error("Failed to parse %s value %s", field, eq);
+ return -EINVAL;
+ }
+
+ r = sd_bus_message_append(m, "v", "t", u);
+
} else {
log_error("Unknown assignment %s.", assignment);
return -EINVAL;
diff --git a/src/run/run.c b/src/run/run.c
index e3b6293..0c92a1c 100644
--- a/src/run/run.c
+++ b/src/run/run.c
@@ -30,6 +30,7 @@
#include "env-util.h"
#include "path-util.h"
#include "bus-error.h"
+#include "calendarspec.h"
static bool arg_scope = false;
static bool arg_remain_after_exit = false;
@@ -47,10 +48,19 @@ static int arg_nice = 0;
static bool arg_nice_set = false;
static char **arg_environment = NULL;
static char **arg_property = NULL;
+static bool with_timer = false;
+static usec_t arg_on_active = 0;
+static usec_t arg_on_boot = 0;
+static usec_t arg_on_startup = 0;
+static usec_t arg_on_unit_active = 0;
+static usec_t arg_on_unit_inactive = 0;
+static char *arg_on_calendar = 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"
+ printf("%s [OPTIONS...] [COMMAND] [ARGS...]\n\n"
+ "Run the specified command in a transient scope or service or timer\n"
+ "unit. If timer option is specified and unit is exist which is\n"
+ "specified with --unit option then command can be ommited.\n\n"
" -h --help Show this help\n"
" --version Show package version\n"
" --user Run as user unit\n"
@@ -67,7 +77,14 @@ static void help(void) {
" --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",
+ " --setenv=NAME=VALUE Set environment\n\n"
+ "Timer options:\n\n"
+ " --on-active=SEC Run after seconds\n"
+ " --on-boot=SEC Run after seconds from machine was booted up\n"
+ " --on-startup=SEC Run after seconds from systemd was first started\n"
+ " --on-unit-active=SEC Run after seconds from the last activation\n"
+ " --on-unit-inactive=SEC Run after seconds from the last deactivation\n"
+ " --on-calendar=SPEC Realtime timer\n",
program_invocation_short_name);
}
@@ -86,32 +103,45 @@ static int parse_argv(int argc, char *argv[]) {
ARG_EXEC_GROUP,
ARG_SERVICE_TYPE,
ARG_NICE,
- ARG_SETENV
+ ARG_SETENV,
+ ARG_ON_ACTIVE,
+ ARG_ON_BOOT,
+ ARG_ON_STARTUP,
+ ARG_ON_UNIT_ACTIVE,
+ ARG_ON_UNIT_INACTIVE,
+ ARG_ON_CALENDAR
};
static const struct option options[] = {
- { "help", no_argument, NULL, 'h' },
- { "version", no_argument, NULL, ARG_VERSION },
- { "user", no_argument, NULL, ARG_USER },
- { "system", no_argument, NULL, ARG_SYSTEM },
- { "scope", no_argument, NULL, ARG_SCOPE },
- { "unit", required_argument, NULL, ARG_UNIT },
- { "description", required_argument, NULL, ARG_DESCRIPTION },
- { "slice", required_argument, NULL, ARG_SLICE },
- { "remain-after-exit", no_argument, NULL, 'r' },
- { "send-sighup", no_argument, NULL, ARG_SEND_SIGHUP },
- { "host", required_argument, NULL, 'H' },
- { "machine", required_argument, NULL, 'M' },
- { "service-type", required_argument, NULL, ARG_SERVICE_TYPE },
- { "uid", required_argument, NULL, ARG_EXEC_USER },
- { "gid", required_argument, NULL, ARG_EXEC_GROUP },
- { "nice", required_argument, NULL, ARG_NICE },
- { "setenv", required_argument, NULL, ARG_SETENV },
- { "property", required_argument, NULL, 'p' },
+ { "help", no_argument, NULL, 'h' },
+ { "version", no_argument, NULL, ARG_VERSION },
+ { "user", no_argument, NULL, ARG_USER },
+ { "system", no_argument, NULL, ARG_SYSTEM },
+ { "scope", no_argument, NULL, ARG_SCOPE },
+ { "unit", required_argument, NULL, ARG_UNIT },
+ { "description", required_argument, NULL, ARG_DESCRIPTION },
+ { "slice", required_argument, NULL, ARG_SLICE },
+ { "remain-after-exit", no_argument, NULL, 'r' },
+ { "send-sighup", no_argument, NULL, ARG_SEND_SIGHUP },
+ { "host", required_argument, NULL, 'H' },
+ { "machine", required_argument, NULL, 'M' },
+ { "service-type", required_argument, NULL, ARG_SERVICE_TYPE },
+ { "uid", required_argument, NULL, ARG_EXEC_USER },
+ { "gid", required_argument, NULL, ARG_EXEC_GROUP },
+ { "nice", required_argument, NULL, ARG_NICE },
+ { "setenv", required_argument, NULL, ARG_SETENV },
+ { "property", required_argument, NULL, 'p' },
+ { "on-active", required_argument, NULL, ARG_ON_ACTIVE },
+ { "on-boot", required_argument, NULL, ARG_ON_BOOT },
+ { "on-startup", required_argument, NULL, ARG_ON_STARTUP },
+ { "on-unit-active", required_argument, NULL, ARG_ON_UNIT_ACTIVE },
+ { "on-unit-inactive", required_argument, NULL, ARG_ON_UNIT_INACTIVE },
+ { "on-calendar", required_argument, NULL, ARG_ON_CALENDAR },
{},
};
int r, c;
+ CalendarSpec *spec = NULL;
assert(argc >= 0);
assert(argv);
@@ -207,6 +237,67 @@ static int parse_argv(int argc, char *argv[]) {
break;
+ case ARG_ON_ACTIVE:
+
+ r = parse_sec(optarg, &arg_on_active);
+ if (r < 0) {
+ log_error("Failed to parse timer value: %s", optarg);
+ return r;
+ }
+
+ break;
+
+ case ARG_ON_BOOT:
+
+ r = parse_sec(optarg, &arg_on_boot);
+ if (r < 0) {
+ log_error("Failed to parse timer value: %s", optarg);
+ return r;
+ }
+
+ break;
+
+ case ARG_ON_STARTUP:
+
+ r = parse_sec(optarg, &arg_on_startup);
+ if (r < 0) {
+ log_error("Failed to parse timer value: %s", optarg);
+ return r;
+ }
+
+ break;
+
+ case ARG_ON_UNIT_ACTIVE:
+
+ r = parse_sec(optarg, &arg_on_unit_active);
+ if (r < 0) {
+ log_error("Failed to parse timer value: %s", optarg);
+ return r;
+ }
+
+ break;
+
+ case ARG_ON_UNIT_INACTIVE:
+
+ r = parse_sec(optarg, &arg_on_unit_inactive);
+ if (r < 0) {
+ log_error("Failed to parse timer value: %s", optarg);
+ return r;
+ }
+
+ break;
+
+ case ARG_ON_CALENDAR:
+
+ r = calendar_spec_from_string(optarg, &spec);
+ if (r < 0) {
+ log_error("Invalid calendar spec: %s", optarg);
+ return r;
+ }
+ free(spec);
+ arg_on_calendar = optarg;
+ break;
+
case '?':
return -EINVAL;
@@ -214,7 +305,9 @@ static int parse_argv(int argc, char *argv[]) {
assert_not_reached("Unhandled option");
}
- if (optind >= argc) {
+ with_timer = !!(arg_on_active || arg_on_boot || arg_on_startup || arg_on_unit_active || arg_on_unit_inactive || arg_on_calendar);
+
+ if (!with_timer && (optind >= argc)) {
log_error("Command line to execute required.");
return -EINVAL;
}
@@ -234,44 +327,75 @@ static int parse_argv(int argc, char *argv[]) {
return -EINVAL;
}
+ if (arg_scope && with_timer) {
+ log_error("Timer options are not supported in --scope mode.");
+ return -EINVAL;
+ }
+
return 1;
}
-static int message_start_transient_unit_new(sd_bus *bus, const char *name, sd_bus_message **ret) {
- _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
- char **i;
+static int get_unit_state_by_name(sd_bus *bus, const char *name, char **state) {
+ _cleanup_bus_message_unref_ sd_bus_message *m = NULL, *reply = NULL;
+ _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
+ char *s;
int r;
assert(bus);
- assert(name);
- assert(ret);
- r = sd_bus_message_new_method_call(
- bus,
- &m,
- "org.freedesktop.systemd1",
- "/org/freedesktop/systemd1",
- "org.freedesktop.systemd1.Manager",
- "StartTransientUnit");
+ r = sd_bus_message_new_method_call(bus,
+ &m,
+ "org.freedesktop.systemd1",
+ "/org/freedesktop/systemd1",
+ "org.freedesktop.systemd1.Manager",
+ "GetUnitFileState");
+
+ r = sd_bus_message_append(m, "s", name);
if (r < 0)
return r;
- r = sd_bus_message_append(m, "ss", name, "fail");
+ r = sd_bus_call(bus, m, 0, &error, &reply);
if (r < 0)
return r;
- r = sd_bus_message_open_container(m, 'a', "(sv)");
+ r = sd_bus_message_read(reply, "s", &s);
if (r < 0)
return r;
+ *state = s;
+
+ return 0;
+}
+
+static int transient_unit_set_properties(sd_bus_message *m, UnitType t) {
+ char **i;
+ int r;
+
STRV_FOREACH(i, arg_property) {
+ const char *eq, *field;
+
+ eq = strchr(*i, '=');
+ if (!eq) {
+ log_error("Not an assignment: %s", *i);
+ return -EINVAL;
+ }
+
+ field = strndupa(*i, eq - *i);
+ eq ++;
+
+ if (!unit_can_have_property(t, field))
+ continue;
+
r = sd_bus_message_open_container(m, 'r', "sv");
if (r < 0)
return r;
r = bus_append_unit_property_assignment(m, *i);
- if (r < 0)
- return r;
+ if (r < 0) {
+ r = sd_bus_message_append(m, "sv", 0);
+ if (r < 0)
+ return r;
+ }
r = sd_bus_message_close_container(m);
if (r < 0)
@@ -300,146 +424,330 @@ static int message_start_transient_unit_new(sd_bus *bus, const char *name, sd_bu
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) {
+static int transient_service_set_properties(sd_bus_message *m, char **argv) {
int r;
- assert(bus);
assert(m);
- r = sd_bus_message_close_container(m);
- if (r < 0)
- return r;
-
- r = sd_bus_message_append(m, "a(sa(sv))", 0);
+ r = transient_unit_set_properties(m, UNIT_SERVICE);
if (r < 0)
return r;
- return sd_bus_call(bus, m, 0, error, reply);
-}
-
-static int start_transient_service(
- sd_bus *bus,
- char **argv,
- sd_bus_error *error) {
-
- _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
- _cleanup_free_ char *name = NULL;
- int r;
-
- if (arg_unit) {
- name = unit_name_mangle_with_suffix(arg_unit, MANGLE_NOGLOB, ".service");
- if (!name)
- return log_oom();
- } else if (asprintf(&name, "run-"PID_FMT".service", getpid()) < 0)
- return log_oom();
-
- r = message_start_transient_unit_new(bus, name, &m);
- if (r < 0)
- return bus_log_create_error(r);
-
if (arg_remain_after_exit) {
r = sd_bus_message_append(m, "(sv)", "RemainAfterExit", "b", arg_remain_after_exit);
if (r < 0)
- return bus_log_create_error(r);
+ return r;
}
if (arg_service_type) {
r = sd_bus_message_append(m, "(sv)", "Type", "s", arg_service_type);
if (r < 0)
- return bus_log_create_error(r);
+ return r;
}
if (arg_exec_user) {
r = sd_bus_message_append(m, "(sv)", "User", "s", arg_exec_user);
if (r < 0)
- return bus_log_create_error(r);
+ return r;
}
if (arg_exec_group) {
r = sd_bus_message_append(m, "(sv)", "Group", "s", arg_exec_group);
if (r < 0)
- return bus_log_create_error(r);
+ return r;
}
if (arg_nice_set) {
r = sd_bus_message_append(m, "(sv)", "Nice", "i", arg_nice);
if (r < 0)
- return bus_log_create_error(r);
+ return r;
}
if (!strv_isempty(arg_environment)) {
r = sd_bus_message_open_container(m, 'r', "sv");
if (r < 0)
- return bus_log_create_error(r);
+ return r;
r = sd_bus_message_append(m, "s", "Environment");
if (r < 0)
- return bus_log_create_error(r);
+ return r;
r = sd_bus_message_open_container(m, 'v', "as");
if (r < 0)
- return bus_log_create_error(r);
+ return r;
r = sd_bus_message_append_strv(m, arg_environment);
if (r < 0)
- return bus_log_create_error(r);
+ return r;
r = sd_bus_message_close_container(m);
if (r < 0)
- return bus_log_create_error(r);
+ return r;
r = sd_bus_message_close_container(m);
if (r < 0)
- return bus_log_create_error(r);
+ return r;
}
- r = sd_bus_message_open_container(m, 'r', "sv");
+ /* Exec container */
+ {
+ r = sd_bus_message_open_container(m, 'r', "sv");
+ if (r < 0)
+ return r;
+
+ r = sd_bus_message_append(m, "s", "ExecStart");
+ if (r < 0)
+ return r;
+
+ r = sd_bus_message_open_container(m, 'v', "a(sasb)");
+ if (r < 0)
+ return r;
+
+ r = sd_bus_message_open_container(m, 'a', "(sasb)");
+ if (r < 0)
+ return r;
+
+ r = sd_bus_message_open_container(m, 'r', "sasb");
+ if (r < 0)
+ return r;
+
+ r = sd_bus_message_append(m, "s", argv[0]);
+ if (r < 0)
+ return r;
+
+ r = sd_bus_message_append_strv(m, argv);
+ if (r < 0)
+ return r;
+
+ r = sd_bus_message_append(m, "b", false);
+ if (r < 0)
+ return r;
+
+ r = sd_bus_message_close_container(m);
+ if (r < 0)
+ return r;
+
+ r = sd_bus_message_close_container(m);
+ if (r < 0)
+ return r;
+
+ r = sd_bus_message_close_container(m);
+ if (r < 0)
+ return r;
+
+ r = sd_bus_message_close_container(m);
+ if (r < 0)
+ return r;
+ }
+
+ return 0;
+}
+
+static int transient_timer_set_properties(sd_bus_message *m) {
+ int r;
+
+ assert(m);
+
+ r = transient_unit_set_properties(m, UNIT_TIMER);
+ if (r < 0)
+ return r;
+
+ if (arg_on_active) {
+ r = sd_bus_message_append(m, "(sv)", "OnActiveSec", "t", arg_on_active);
+ if (r < 0)
+ return r;
+ }
+
+ if (arg_on_boot) {
+ r = sd_bus_message_append(m, "(sv)", "OnBootSec", "t", arg_on_boot);
+ if (r < 0)
+ return r;
+ }
+
+ if (arg_on_startup) {
+ r = sd_bus_message_append(m, "(sv)", "OnStartupSec", "t", arg_on_startup);
+ if (r < 0)
+ return r;
+ }
+
+ if (arg_on_unit_active) {
+ r = sd_bus_message_append(m, "(sv)", "OnUnitActiveSec", "t", arg_on_unit_active);
+ if (r < 0)
+ return r;
+ }
+
+ if (arg_on_unit_inactive) {
+ r = sd_bus_message_append(m, "(sv)", "OnUnitInactiveSec", "t", arg_on_unit_inactive);
+ if (r < 0)
+ return r;
+ }
+
+ if (arg_on_calendar) {
+ r = sd_bus_message_append(m, "(sv)", "OnCalendar", "s", arg_on_calendar);
+ if (r < 0)
+ return r;
+ }
+
+ return 0;
+}
+
+static int transient_scope_set_properties(sd_bus_message *m) {
+ int r;
+
+ assert(m);
+
+ r = transient_unit_set_properties(m, UNIT_SCOPE);
+ if (r < 0)
+ return r;
+
+ r = sd_bus_message_append(m, "(sv)", "PIDs", "au", 1, (uint32_t) getpid());
+ if (r < 0)
+ return r;
+
+ return 0;
+}
+
+static int start_transient_service(
+ sd_bus *bus,
+ char **argv,
+ sd_bus_error *error) {
+
+ _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
+ _cleanup_free_ char *service = NULL;
+ char *state = NULL;
+ int r;
+
+ assert(bus);
+ assert(argv);
+
+ if (arg_unit) {
+ service = unit_name_mangle_with_suffix(arg_unit, MANGLE_NOGLOB, ".service");
+ if (!service)
+ return log_oom();
+
+ if (get_unit_state_by_name(bus, service, &state) == 0) {
+ log_error("Unit %s is already exist with %s state.", service, state);
+ return -EEXIST;
+ }
+ } else if (asprintf(&service, "run-"PID_FMT".service", getpid()) < 0)
+ return log_oom();
+
+ r = sd_bus_message_new_method_call(bus,
+ &m,
+ "org.freedesktop.systemd1",
+ "/org/freedesktop/systemd1",
+ "org.freedesktop.systemd1.Manager",
+ "StartTransientUnit");
if (r < 0)
return bus_log_create_error(r);
- r = sd_bus_message_append(m, "s", "ExecStart");
+ /* name and mode */
+ r = sd_bus_message_append(m, "ss", service, "fail");
if (r < 0)
return bus_log_create_error(r);
- r = sd_bus_message_open_container(m, 'v', "a(sasb)");
+ /* properties */
+ r = sd_bus_message_open_container(m, 'a', "(sv)");
if (r < 0)
return bus_log_create_error(r);
- r = sd_bus_message_open_container(m, 'a', "(sasb)");
+ r = transient_service_set_properties(m, argv);
if (r < 0)
return bus_log_create_error(r);
- r = sd_bus_message_open_container(m, 'r', "sasb");
+ r = sd_bus_message_close_container(m);
if (r < 0)
return bus_log_create_error(r);
- r = sd_bus_message_append(m, "s", argv[0]);
+ /* aux */
+ r = sd_bus_message_append(m, "a(sa(sv))", 0);
if (r < 0)
return bus_log_create_error(r);
- r = sd_bus_message_append_strv(m, argv);
+ /* send dbus */
+ r = sd_bus_call(bus, m, 0, error, NULL);
if (r < 0)
return bus_log_create_error(r);
- r = sd_bus_message_append(m, "b", false);
+ log_info("Running as unit %s.", service);
+
+ return 0;
+}
+
+static int start_transient_timer(
+ sd_bus *bus,
+ char **argv,
+ sd_bus_error *error) {
+
+ _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
+ _cleanup_free_ char *timer = NULL, *service = NULL;
+ char *state = NULL;
+ bool with_aux = true;
+ int r;
+
+ assert(bus);
+ assert(argv);
+
+ if (arg_unit) {
+ switch(unit_name_to_type(arg_unit)) {
+ case UNIT_SERVICE:
+ service = strdup(arg_unit);
+ timer = unit_name_change_suffix(service, ".timer");
+ if (!timer)
+ return log_oom();
+ break;
+
+ case UNIT_TIMER:
+ timer = strdup(arg_unit);
+ service = unit_name_change_suffix(service, ".service");
+ if (!service)
+ return log_oom();
+ break;
+
+ default:
+ service = unit_name_mangle_with_suffix(arg_unit, MANGLE_NOGLOB, ".service");
+ if (!service)
+ return log_oom();
+
+ timer = unit_name_mangle_with_suffix(arg_unit, MANGLE_NOGLOB, ".timer");
+ if (!timer)
+ return log_oom();
+
+ break;
+ }
+
+ if (argv[0] && (get_unit_state_by_name(bus, service, &state) == 0)) {
+ log_error("Unit %s is already exist with %s state.", service, state);
+ return -EEXIST;
+ }
+ } else if ((asprintf(&service, "run-"PID_FMT".service", getpid()) < 0) ||
+ (asprintf(&timer, "run-"PID_FMT".timer", getpid()) < 0))
+ return log_oom();
+
+ r = sd_bus_message_new_method_call(
+ bus,
+ &m,
+ "org.freedesktop.systemd1",
+ "/org/freedesktop/systemd1",
+ "org.freedesktop.systemd1.Manager",
+ "StartTransientUnit");
if (r < 0)
return bus_log_create_error(r);
- r = sd_bus_message_close_container(m);
+ /* name and mode */
+ r = sd_bus_message_append(m, "ss", timer, "fail");
if (r < 0)
return bus_log_create_error(r);
- r = sd_bus_message_close_container(m);
+ /* properties */
+ r = sd_bus_message_open_container(m, 'a', "(sv)");
if (r < 0)
return bus_log_create_error(r);
- r = sd_bus_message_close_container(m);
+ r = transient_timer_set_properties(m);
if (r < 0)
return bus_log_create_error(r);
@@ -447,11 +755,55 @@ static int start_transient_service(
if (r < 0)
return bus_log_create_error(r);
- r = message_start_transient_unit_send(bus, m, error, NULL);
+ /* aux */
+ if (arg_unit)
+ with_aux = get_unit_state_by_name(bus, service, &state) < 0 ? true : false;
+
+ if (with_aux) {
+ r = sd_bus_message_open_container(m, 'a', "(sa(sv))");
+ if (r < 0)
+ return bus_log_create_error(r);
+
+ r = sd_bus_message_open_container(m, 'r', "sa(sv)");
+ if (r < 0)
+ return bus_log_create_error(r);
+
+ r = sd_bus_message_append(m, "s", service);
+ if (r < 0)
+ return bus_log_create_error(r);
+
+ r = sd_bus_message_open_container(m, 'a', "(sv)");
+ if (r < 0)
+ return bus_log_create_error(r);
+
+ r = transient_service_set_properties(m, argv);
+ 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);
+ } else {
+ r = sd_bus_message_append(m, "a(sa(sv))", 0);
+ if (r < 0)
+ return bus_log_create_error(r);
+ }
+
+ /* send dbus */
+ r = sd_bus_call(bus, m, 0, error, NULL);
if (r < 0)
return bus_log_create_error(r);
- log_info("Running as unit %s.", name);
+ log_info("Running as unit %s.", timer);
+ log_info("Will run as unit %s.", service);
return 0;
}
@@ -462,28 +814,55 @@ static int start_transient_scope(
sd_bus_error *error) {
_cleanup_bus_message_unref_ sd_bus_message *m = NULL;
- _cleanup_free_ char *name = NULL;
+ _cleanup_free_ char *scope = NULL;
_cleanup_strv_free_ char **env = NULL, **user_env = NULL;
int r;
assert(bus);
+ assert(argv);
if (arg_unit) {
- name = unit_name_mangle_with_suffix(arg_unit, MANGLE_NOGLOB, ".scope");
- if (!name)
+ scope = unit_name_mangle_with_suffix(arg_unit, MANGLE_NOGLOB, ".scope");
+ if (!scope)
return log_oom();
- } else if (asprintf(&name, "run-"PID_FMT".scope", getpid()) < 0)
+ } else if (asprintf(&scope, "run-"PID_FMT".scope", getpid()) < 0)
return log_oom();
- r = message_start_transient_unit_new(bus, name, &m);
+ r = sd_bus_message_new_method_call(
+ bus,
+ &m,
+ "org.freedesktop.systemd1",
+ "/org/freedesktop/systemd1",
+ "org.freedesktop.systemd1.Manager",
+ "StartTransientUnit");
if (r < 0)
return bus_log_create_error(r);
- r = sd_bus_message_append(m, "(sv)", "PIDs", "au", 1, (uint32_t) getpid());
+ /* name and mode */
+ r = sd_bus_message_append(m, "ss", scope, "fail");
+ if (r < 0)
+ return bus_log_create_error(r);
+
+ /* properties */
+ r = sd_bus_message_open_container(m, 'a', "(sv)");
+ if (r < 0)
+ return bus_log_create_error(r);
+
+ r = transient_scope_set_properties(m);
if (r < 0)
return bus_log_create_error(r);
- r = message_start_transient_unit_send(bus, m, error, NULL);
+ r = sd_bus_message_close_container(m);
+ if (r < 0)
+ return bus_log_create_error(r);
+
+ /* aux */
+ r = sd_bus_message_append(m, "a(sa(sv))", 0);
+ if (r < 0)
+ return bus_log_create_error(r);
+
+ /* send dbus */
+ r = sd_bus_call(bus, m, 0, error, NULL);
if (r < 0)
return bus_log_create_error(r);
@@ -553,7 +932,7 @@ static int start_transient_scope(
if (!env)
return log_oom();
- log_info("Running as unit %s.", name);
+ log_info("Running as unit %s.", scope);
execvpe(argv[0], argv, env);
log_error("Failed to execute: %m");
@@ -563,7 +942,7 @@ static int start_transient_scope(
int main(int argc, char* argv[]) {
_cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
_cleanup_bus_close_unref_ sd_bus *bus = NULL;
- _cleanup_free_ char *description = NULL, *command = NULL;
+ _cleanup_free_ char *description = NULL, *command = NULL, *state = NULL;
int r;
log_parse_environment();
@@ -573,21 +952,38 @@ int main(int argc, char* argv[]) {
if (r <= 0)
goto finish;
- r = find_binary(argv[optind], &command);
- if (r < 0) {
- log_error("Failed to find executable %s: %s", argv[optind], strerror(-r));
- goto finish;
- }
- argv[optind] = command;
-
- if (!arg_description) {
- description = strv_join(argv + optind, " ");
- if (!description) {
- r = log_oom();
- goto finish;
+ if (optind < argc) {
+ r = find_binary(argv[optind], &command);
+ if (r < 0) {
+ log_error("Failed to find executable %s: %s", argv[optind], strerror(-r));
+ return r;
}
+ argv[optind] = command;
+
+ if (!arg_description) {
+ description = strv_join(argv + optind, " ");
+ if (!description) {
+ r = log_oom();
+ return r;
+ }
- arg_description = description;
+ arg_description = description;
+ }
+ } else {
+ if (arg_unit) {
+ if (!arg_description) {
+ description = strv_join((char **)&arg_unit, " ");
+ if (!description) {
+ r = log_oom();
+ return r;
+ }
+
+ arg_description = description;
+ }
+ } else {
+ log_error("Command line to execute required.");
+ return -EINVAL;
+ }
}
r = bus_open_transport_systemd(arg_transport, arg_host, arg_user, &bus);
@@ -598,6 +994,8 @@ int main(int argc, char* argv[]) {
if (arg_scope)
r = start_transient_scope(bus, argv + optind, &error);
+ else if (with_timer)
+ r = start_transient_timer(bus, argv + optind, &error);
else
r = start_transient_service(bus, argv + optind, &error);
--
1.9.3
More information about the systemd-devel
mailing list