diff --git a/doc/ChangeLog.md b/doc/ChangeLog.md index 1f191dbb..a79f8a3c 100644 --- a/doc/ChangeLog.md +++ b/doc/ChangeLog.md @@ -18,7 +18,12 @@ All relevant changes are documented in this file. service. New is a `restart` object with the restart counter, `restart-max`, the current restart delay and its limit, and `manual` and `started`. `starts` is now there for all services, not only those - with `manual-start` + with `manual-start`. Also new are the settings of the service: `stop`, + `reload`, `notify`, `remain`, `scripts`, `directories`, `log` or `tty`, + `cgroup-config`, `rlimits`, `provides`, `conflicts`, `if`, `groups`, + `capabilities`, `pam`, `name`, `id`, `protected`, `nowarn`, and the + state Finit keeps for its own use in `internal`, see + [Commands & Status](initctl.md), with a D-Bus property for each - Add `restart-max-sec`, a limit for the delay between restarts of a crashing service. When set, the delay doubles for each restart, from `restart-sec` up to the limit, and comes down again as the service keeps diff --git a/doc/dbus.md b/doc/dbus.md index 1e9ebdb3..a07c514c 100644 --- a/doc/dbus.md +++ b/doc/dbus.md @@ -193,6 +193,71 @@ All read-only; observable via `Properties.Get` and `Properties.GetAll`. | `ManualStart` | `b` | `manual-start` set in the `.conf` | | `Forking` | `b` | Daemon forks to background | | `Started` | `b` | Run/task completed successfully | +| `Notify` | `s` | Readiness protocol: `none`, `pid`, `systemd`, or `s6` | +| `Remain` | `b` | `remain-after-exit` set in the `.conf` | +| `If` | `s` | The `if` setting, raw | +| `Provides` | `s` | Conditions from `provides`, comma separated | +| `Conflicts` | `s` | Services from `conflicts`, comma separated | +| `ExtraGroups` | `s` | Supplementary groups, comma separated | +| `Capabilities` | `s` | The `capabilities` setting, comma separated | +| `Pam` | `s` | PAM service name, empty when not set | +| `StopSignal` | `s` | Signal that stops the service, e.g. `SIGTERM` | +| `StopTimeout` | `u` | Seconds to wait for it to stop, before `SIGKILL` | +| `StopScope` | `s` | `pgroup`, `process`, `cgroup`, or `mixed` | +| `ReloadSignal` | `s` | `SIGHUP`, or `none` when a reload restarts the service | +| `RestartSec` | `u` | The `restart-sec` setting, 0 when not set | +| `Respawn` | `b` | `respawn` set in the `.conf` | +| `OnCrash` | `s` | `ignore`, `reboot`, or `script` | +| `RuntimeDir` | `s` | The `runtime-dir` setting, likewise for the four below | +| `StateDir` | `s` | | +| `CacheDir` | `s` | | +| `LogsDir` | `s` | | +| `ConfigDir` | `s` | | +| `RuntimeDirPreserve` | `s` | `no`, `restart`, or `yes` | +| `RuntimeDirMode` | `u` | Mode of the runtime directory, likewise for the four below | +| `StateDirMode` | `u` | | +| `CacheDirMode` | `u` | | +| `LogsDirMode` | `u` | | +| `ConfigDirMode` | `u` | | +| `Id` | `s` | The `:ID` part of the identity, empty when there is none | +| `Job` | `u` | Job number, internal | +| `OldPid` | `u` | PID of the previous run, internal | +| `Dirty` | `b` | Changed in the `.conf`, pending the next reload, internal | +| `Removed` | `b` | Gone from the `.conf`, pending removal, internal | +| `Starting` | `b` | Started, waiting for its PID file or readiness, internal | +| `Protected` | `b` | Built-in service, kept when the configuration is reloaded | +| `NoWarn` | `b` | No warning when the command is missing | +| `ReloadPropagate` | `b` | A reload of a dependency reloads this service too, `~` in its conditions | +| `CgroupName` | `s` | Control group the service joins, empty for the default | +| `CgroupConfig` | `s` | Its cgroup settings, `key:value` comma separated | +| `CgroupDelegate` | `b` | The service manages its own cgroup subtree | +| `Rlimits` | `s` | Resource limits as `name=soft:hard`, comma separated, a number or `unlimited` | +| `ExecStartPre` | `s` | Script, empty when not set, likewise for the five below | +| `ExecStartPreTimeout` | `u` | Seconds the script may run, one for each script | +| `ExecStartReady` | `s` | | +| `ExecStartReadyTimeout` | `u` | | +| `ExecStop` | `s` | | +| `ExecStopTimeout` | `u` | | +| `ExecStopPost` | `s` | | +| `ExecStopPostTimeout` | `u` | | +| `ExecReload` | `s` | | +| `ExecReloadTimeout` | `u` | | +| `ExecCleanup` | `s` | | +| `ExecCleanupTimeout` | `u` | | +| `Log` | `b` | Output is logged, not for a `tty` | +| `LogNull` | `b` | Output is discarded | +| `LogConsole` | `b` | Output goes to the console | +| `LogFile` | `s` | Log file, empty for syslog | +| `LogPriority` | `s` | Syslog facility and level, empty for the default | +| `LogIdentity` | `s` | Syslog identity, empty for the default | +| `TtyDevice` | `s` | Device of a `tty`, this and the below are empty or false for other types | +| `TtyBaud` | `s` | Baud rate, empty for the default | +| `TtyTerm` | `s` | The `TERM` to set | +| `TtyNoClear` | `b` | `noclear` set in the `.conf` | +| `TtyNoWait` | `b` | `nowait` set in the `.conf` | +| `TtyNoLogin` | `b` | `nologin` set in the `.conf` | +| `TtyNoTty` | `b` | `notty` set in the `.conf` | +| `TtyRescue` | `b` | `rescue` set in the `.conf` | On every state transition the object also emits the standard `org.freedesktop.DBus.Properties.PropertiesChanged` signal: `State` diff --git a/doc/initctl.md b/doc/initctl.md index 19063220..55cd5f69 100644 --- a/doc/initctl.md +++ b/doc/initctl.md @@ -147,8 +147,8 @@ Apr 8 15:02:12 alpine authpriv.notice dropbear[2634]: Password auth succeeded f ``` With `-j` the same command gives JSON, for scripts and other programs. -The view above is kept short for reading, the JSON has everything -`initctl` gets from Finit about the service: +The view above is kept short for reading, the JSON also has the +settings of the service: ``` alpine:~# initctl -j status dialer @@ -168,8 +168,58 @@ alpine:~# initctl -j status dialer "restart": { "count": 4, "max": -1, + "sec": 10, "delay": 160, - "limit": 300 + "limit": 300, + "respawn": false, + "oncrash": "ignore" + }, + "name": "dialer", + "id": "", + "internal": { + "job": 12, + "oldpid": 1234, + "starting": false, + "dirty": false, + "removed": false + }, + "protected": false, + "nowarn": false, + "notify": "none", + "remain": false, + "stop": { + "signal": "SIGTERM", + "timeout": 3, + "scope": "pgroup" + }, + "reload": { + "signal": "SIGHUP", + "propagate": false + }, + "scripts": { + "exec-stop-post": { "command": "/usr/libexec/dialer-down", "timeout": 3 } + }, + "directories": { + "runtime": { "name": "dialer", "mode": "0755" }, + "preserve": "no" + }, + "cgroup-config": { + "group": "", + "config": "", + "delegate": false + }, + "rlimits": { + "core": { "soft": 0, "hard": "unlimited" }, + "nofile": { "soft": 1024, "hard": 4096 }, + ... + }, + "log": { + "enabled": true, + "null": false, + "console": false, + "file": "", + "priority": "", + "identity": "dialer" }, "pidfile": "none", "pid": 0, @@ -183,10 +233,24 @@ alpine:~# initctl -j status dialer In `restart`, `count` is the restart counter that `restart-max`, here `max`, is compared to, -1 for `restart = "always"`. The total since boot is in `restarts`. `delay` is the number of seconds before the -next restart, should the service crash, and `limit` is its -`restart-max-sec`, 0 when not set. `environment`, `condition`, `exit`, -and the cgroup members `cgroup`, `memory`, `cpu`, and `limits` are left -out when they do not apply. +next restart, should the service crash, `sec` is `restart-sec`, and +`limit` is `restart-max-sec`, both 0 when not set. All times are in +seconds. + +The settings use the names from the [configuration](config/service-opts.md): +`scripts` has the `exec-*` scripts that are set, each with its timeout, +`directories` the `runtime`, `state`, `cache`, `logs`, and `config` +directories that are set, and `preserve`. `cgroup-config` is the +`cgroup` setting of the service, where `cgroup` is the control group +it runs in right now. `rlimits` has every resource limit, the ones +from `rlimit` and the ones the service inherits. A `tty` has a `tty` +object with its device and flags where other types have `log`. +`internal` is state Finit keeps for its own use, it may change between +releases. + +Left out when they do not apply are `environment`, `condition`, `exit`, +`if`, `provides`, `conflicts`, `groups`, `capabilities`, `pam`, and the +cgroup members `cgroup`, `memory`, `cpu`, and `limits`. ``` alpine:~# initctl -j status dialer | jq .restart.delay diff --git a/src/dbus.c b/src/dbus.c index efc10beb..350c07dc 100644 --- a/src/dbus.c +++ b/src/dbus.c @@ -804,6 +804,107 @@ SVC_PROP_BOOL(svc_prop_manual, manual) SVC_PROP_BOOL(svc_prop_forking, forking) SVC_PROP_BOOL(svc_prop_started, started) +/* The settings of the service, for initctl -j status */ +#define SVC_PROP_EXPR(fn, writer, expr) \ +static int fn(link_writer_t *w, void *arg) \ +{ \ + svc_t *svc = arg; \ + \ + writer(w, expr); \ + return 0; \ +} + +static int svc_prop_provides(link_writer_t *w, void *arg) +{ + char buf[MAX_NUM_PROVIDES * MAX_COND_LEN]; + + link_w_string(w, svc_provides_str(arg, buf, sizeof(buf))); + return 0; +} + +static int svc_prop_rlimits(link_writer_t *w, void *arg) +{ + svc_t *svc = arg; + char buf[1024]; + + link_w_string(w, rlimits2str(svc->rlimit, buf, sizeof(buf))); + return 0; +} + +static int svc_prop_stop_signal(link_writer_t *w, void *arg) +{ + svc_t *svc = arg; + char buf[16]; + + /* Not sig_name(), initctl has only sig2str() for its own transport */ + snprintf(buf, sizeof(buf), "SIG%s", sig2str(svc->sighalt)); + link_w_string(w, buf); + return 0; +} + +SVC_PROP_BOOL(svc_prop_remain, remain) +SVC_PROP_STR (svc_prop_if, ifstmt) +SVC_PROP_STR (svc_prop_conflicts, conflict) +SVC_PROP_STR (svc_prop_extra_groups, supgroups) +SVC_PROP_STR (svc_prop_capabilities, capabilities) +SVC_PROP_STR (svc_prop_pam, pam) +SVC_PROP_U32 (svc_prop_stop_timeout, killdelay / 1000) +SVC_PROP_U32 (svc_prop_restart_sec, restart_saved / 1000) +SVC_PROP_BOOL(svc_prop_respawn, respawn) +SVC_PROP_BOOL(svc_prop_protected, protect) +SVC_PROP_STR (svc_prop_id, id) +SVC_PROP_U32 (svc_prop_job, job) +SVC_PROP_U32 (svc_prop_old_pid, oldpid) +SVC_PROP_BOOL(svc_prop_dirty, dirty) +SVC_PROP_BOOL(svc_prop_removed, removed) +SVC_PROP_BOOL(svc_prop_starting, starting) +SVC_PROP_BOOL(svc_prop_no_warn, nowarn) +SVC_PROP_BOOL(svc_prop_reload_propagate, flux_reload) +SVC_PROP_STR (svc_prop_cgroup_name, cgroup.name) +SVC_PROP_STR (svc_prop_cgroup_config, cgroup.cfg) +SVC_PROP_BOOL(svc_prop_cgroup_delegate, cgroup.delegate) +SVC_PROP_U32 (svc_prop_runtime_dir_mode, dir_mode[0]) +SVC_PROP_U32 (svc_prop_state_dir_mode, dir_mode[1]) +SVC_PROP_U32 (svc_prop_cache_dir_mode, dir_mode[2]) +SVC_PROP_U32 (svc_prop_logs_dir_mode, dir_mode[3]) +SVC_PROP_U32 (svc_prop_config_dir_mode, dir_mode[4]) +SVC_PROP_STR (svc_prop_runtime_dir, runtime_dir) +SVC_PROP_STR (svc_prop_state_dir, state_dir) +SVC_PROP_STR (svc_prop_cache_dir, cache_dir) +SVC_PROP_STR (svc_prop_logs_dir, logs_dir) +SVC_PROP_STR (svc_prop_config_dir, config_dir) +SVC_PROP_STR (svc_prop_exec_start_pre, pre_script) +SVC_PROP_U32 (svc_prop_exec_start_pre_timeout, pre_tmo / 1000) +SVC_PROP_STR (svc_prop_exec_start_ready, ready_script) +SVC_PROP_U32 (svc_prop_exec_start_ready_timeout, ready_tmo / 1000) +SVC_PROP_STR (svc_prop_exec_stop, stop_script) +SVC_PROP_U32 (svc_prop_exec_stop_timeout, stop_tmo / 1000) +SVC_PROP_STR (svc_prop_exec_stop_post, post_script) +SVC_PROP_U32 (svc_prop_exec_stop_post_timeout, post_tmo / 1000) +SVC_PROP_STR (svc_prop_exec_reload, reload_script) +SVC_PROP_U32 (svc_prop_exec_reload_timeout, reload_tmo / 1000) +SVC_PROP_STR (svc_prop_exec_cleanup, cleanup_script) +SVC_PROP_U32 (svc_prop_exec_cleanup_timeout, cleanup_tmo / 1000) +SVC_PROP_EXPR(svc_prop_notify, link_w_string, svc_notify_str(svc)) +SVC_PROP_EXPR(svc_prop_stop_scope, link_w_string, svc_scope_str(svc->stop_scope)) +SVC_PROP_EXPR(svc_prop_reload_signal, link_w_string, svc->sighup ? "SIGHUP" : "none") +SVC_PROP_EXPR(svc_prop_on_crash, link_w_string, svc_oncrash_str(svc)) +SVC_PROP_EXPR(svc_prop_runtime_dir_preserve, link_w_string, svc_preserve_str(svc)) +SVC_PROP_EXPR(svc_prop_log, link_w_bool, !svc_is_tty(svc) ? svc->log.enabled : 0) +SVC_PROP_EXPR(svc_prop_log_null, link_w_bool, !svc_is_tty(svc) ? svc->log.null : 0) +SVC_PROP_EXPR(svc_prop_log_console, link_w_bool, !svc_is_tty(svc) ? svc->log.console : 0) +SVC_PROP_EXPR(svc_prop_log_file, link_w_string, !svc_is_tty(svc) ? svc->log.file : "") +SVC_PROP_EXPR(svc_prop_log_priority, link_w_string, !svc_is_tty(svc) ? svc->log.prio : "") +SVC_PROP_EXPR(svc_prop_log_identity, link_w_string, !svc_is_tty(svc) ? svc->log.ident : "") +SVC_PROP_EXPR(svc_prop_tty_device, link_w_string, svc_is_tty(svc) ? svc->dev : "") +SVC_PROP_EXPR(svc_prop_tty_baud, link_w_string, svc_is_tty(svc) ? svc->baud : "") +SVC_PROP_EXPR(svc_prop_tty_term, link_w_string, svc_is_tty(svc) ? svc->term : "") +SVC_PROP_EXPR(svc_prop_tty_no_clear, link_w_bool, svc_is_tty(svc) ? svc->noclear : 0) +SVC_PROP_EXPR(svc_prop_tty_no_wait, link_w_bool, svc_is_tty(svc) ? svc->nowait : 0) +SVC_PROP_EXPR(svc_prop_tty_no_login, link_w_bool, svc_is_tty(svc) ? svc->nologin : 0) +SVC_PROP_EXPR(svc_prop_tty_no_tty, link_w_bool, svc_is_tty(svc) ? svc->notty : 0) +SVC_PROP_EXPR(svc_prop_tty_rescue, link_w_bool, svc_is_tty(svc) ? svc->rescue : 0) + static const link_property_t service_properties[] = { { .name = "Identity", .sig = "s", .getter = svc_prop_identity }, { .name = "Name", .sig = "s", .getter = svc_prop_name }, @@ -830,6 +931,71 @@ static const link_property_t service_properties[] = { { .name = "ManualStart", .sig = "b", .getter = svc_prop_manual }, { .name = "Forking", .sig = "b", .getter = svc_prop_forking }, { .name = "Started", .sig = "b", .getter = svc_prop_started }, + { .name = "Notify", .sig = "s", .getter = svc_prop_notify }, + { .name = "Remain", .sig = "b", .getter = svc_prop_remain }, + { .name = "If", .sig = "s", .getter = svc_prop_if }, + { .name = "Provides", .sig = "s", .getter = svc_prop_provides }, + { .name = "Conflicts", .sig = "s", .getter = svc_prop_conflicts }, + { .name = "ExtraGroups", .sig = "s", .getter = svc_prop_extra_groups }, + { .name = "Capabilities", .sig = "s", .getter = svc_prop_capabilities }, + { .name = "Pam", .sig = "s", .getter = svc_prop_pam }, + { .name = "StopSignal", .sig = "s", .getter = svc_prop_stop_signal }, + { .name = "StopTimeout", .sig = "u", .getter = svc_prop_stop_timeout }, + { .name = "StopScope", .sig = "s", .getter = svc_prop_stop_scope }, + { .name = "ReloadSignal", .sig = "s", .getter = svc_prop_reload_signal }, + { .name = "RestartSec", .sig = "u", .getter = svc_prop_restart_sec }, + { .name = "Respawn", .sig = "b", .getter = svc_prop_respawn }, + { .name = "OnCrash", .sig = "s", .getter = svc_prop_on_crash }, + { .name = "RuntimeDir", .sig = "s", .getter = svc_prop_runtime_dir }, + { .name = "StateDir", .sig = "s", .getter = svc_prop_state_dir }, + { .name = "CacheDir", .sig = "s", .getter = svc_prop_cache_dir }, + { .name = "LogsDir", .sig = "s", .getter = svc_prop_logs_dir }, + { .name = "ConfigDir", .sig = "s", .getter = svc_prop_config_dir }, + { .name = "RuntimeDirPreserve", .sig = "s", .getter = svc_prop_runtime_dir_preserve }, + { .name = "RuntimeDirMode", .sig = "u", .getter = svc_prop_runtime_dir_mode }, + { .name = "StateDirMode", .sig = "u", .getter = svc_prop_state_dir_mode }, + { .name = "CacheDirMode", .sig = "u", .getter = svc_prop_cache_dir_mode }, + { .name = "LogsDirMode", .sig = "u", .getter = svc_prop_logs_dir_mode }, + { .name = "ConfigDirMode", .sig = "u", .getter = svc_prop_config_dir_mode }, + { .name = "Protected", .sig = "b", .getter = svc_prop_protected }, + { .name = "Id", .sig = "s", .getter = svc_prop_id }, + { .name = "Job", .sig = "u", .getter = svc_prop_job }, + { .name = "OldPid", .sig = "u", .getter = svc_prop_old_pid }, + { .name = "Dirty", .sig = "b", .getter = svc_prop_dirty }, + { .name = "Removed", .sig = "b", .getter = svc_prop_removed }, + { .name = "Starting", .sig = "b", .getter = svc_prop_starting }, + { .name = "NoWarn", .sig = "b", .getter = svc_prop_no_warn }, + { .name = "ReloadPropagate", .sig = "b", .getter = svc_prop_reload_propagate }, + { .name = "CgroupName", .sig = "s", .getter = svc_prop_cgroup_name }, + { .name = "CgroupConfig", .sig = "s", .getter = svc_prop_cgroup_config }, + { .name = "CgroupDelegate", .sig = "b", .getter = svc_prop_cgroup_delegate }, + { .name = "Rlimits", .sig = "s", .getter = svc_prop_rlimits }, + { .name = "ExecStartPre", .sig = "s", .getter = svc_prop_exec_start_pre }, + { .name = "ExecStartPreTimeout", .sig = "u", .getter = svc_prop_exec_start_pre_timeout }, + { .name = "ExecStartReady", .sig = "s", .getter = svc_prop_exec_start_ready }, + { .name = "ExecStartReadyTimeout", .sig = "u", .getter = svc_prop_exec_start_ready_timeout }, + { .name = "ExecStop", .sig = "s", .getter = svc_prop_exec_stop }, + { .name = "ExecStopTimeout", .sig = "u", .getter = svc_prop_exec_stop_timeout }, + { .name = "ExecStopPost", .sig = "s", .getter = svc_prop_exec_stop_post }, + { .name = "ExecStopPostTimeout", .sig = "u", .getter = svc_prop_exec_stop_post_timeout }, + { .name = "ExecReload", .sig = "s", .getter = svc_prop_exec_reload }, + { .name = "ExecReloadTimeout", .sig = "u", .getter = svc_prop_exec_reload_timeout }, + { .name = "ExecCleanup", .sig = "s", .getter = svc_prop_exec_cleanup }, + { .name = "ExecCleanupTimeout", .sig = "u", .getter = svc_prop_exec_cleanup_timeout }, + { .name = "Log", .sig = "b", .getter = svc_prop_log }, + { .name = "LogNull", .sig = "b", .getter = svc_prop_log_null }, + { .name = "LogConsole", .sig = "b", .getter = svc_prop_log_console }, + { .name = "LogFile", .sig = "s", .getter = svc_prop_log_file }, + { .name = "LogPriority", .sig = "s", .getter = svc_prop_log_priority }, + { .name = "LogIdentity", .sig = "s", .getter = svc_prop_log_identity }, + { .name = "TtyDevice", .sig = "s", .getter = svc_prop_tty_device }, + { .name = "TtyBaud", .sig = "s", .getter = svc_prop_tty_baud }, + { .name = "TtyTerm", .sig = "s", .getter = svc_prop_tty_term }, + { .name = "TtyNoClear", .sig = "b", .getter = svc_prop_tty_no_clear }, + { .name = "TtyNoWait", .sig = "b", .getter = svc_prop_tty_no_wait }, + { .name = "TtyNoLogin", .sig = "b", .getter = svc_prop_tty_no_login }, + { .name = "TtyNoTty", .sig = "b", .getter = svc_prop_tty_no_tty }, + { .name = "TtyRescue", .sig = "b", .getter = svc_prop_tty_rescue }, { NULL, NULL, NULL } }; diff --git a/src/initctl.c b/src/initctl.c index 0f15a4c3..d4a45cc2 100644 --- a/src/initctl.c +++ b/src/initctl.c @@ -1475,6 +1475,16 @@ static int svc_compare(svc_t *svc, char *arg) * from a raw svc_t on the legacy socket path or from Service1 * properties on the D-Bus path, rendered by the same printers. */ +/* In the order of the script[] and dir[] members of svc_row */ +#define NUM_SCRIPTS 6 +static const char *script_names[NUM_SCRIPTS] = { + "exec-start-pre", "exec-start-ready", "exec-stop", + "exec-stop-post", "exec-reload", "exec-cleanup", +}; +static const char *dir_names[NUM_SVCDIRS] = { + "runtime", "state", "cache", "logs", "config", +}; + struct svc_row { char ident[MAX_IDENT_LEN]; char state[16]; @@ -1500,6 +1510,56 @@ struct svc_row { int manual; int forking; int started; + + /* Settings, only in the JSON output */ + char name[MAX_ARG_LEN]; + char id[MAX_ID_LEN]; + uint32_t job; + uint32_t oldpid; + int dirty; + int removed; + int starting; + char rlimits[1024]; /* name=soft:hard,... */ + char cgroup_name[16]; + char cgroup_cfg[128]; + uint32_t dir_mode[NUM_SVCDIRS]; + int cgroup_delegate; + int protect; + int nowarn; + int flux_reload; + char notify[8]; + char ifstmt[MAX_IDENT_LEN]; + char provides[MAX_NUM_PROVIDES * MAX_COND_LEN]; + char conflicts[MAX_ARG_LEN]; + char supgroups[MAX_SUPGROUPS_LEN]; + char caps[MAX_CMD_LEN]; + char pam[MAX_ARG_LEN]; + char stop_signal[16]; + char stop_scope[8]; + char reload_signal[8]; + char oncrash[8]; + char dir[NUM_SVCDIRS][MAX_ARG_LEN]; + char preserve[8]; + char script[NUM_SCRIPTS][MAX_CMD_LEN]; + uint32_t script_tmo[NUM_SCRIPTS]; /* seconds */ + char log_file[64]; + char log_prio[20]; + char log_ident[20]; + char tty_dev[32]; + char tty_baud[10]; + char tty_term[10]; + uint32_t stop_timeout; /* seconds */ + uint32_t restart_sec; /* restart-sec, 0 = not set */ + int remain; + int respawn; + int log; + int log_null; + int log_console; + int tty_noclear; + int tty_nowait; + int tty_nologin; + int tty_notty; + int tty_rescue; }; static void fill_row_from_svc(struct svc_row *r, svc_t *svc) @@ -1532,6 +1592,73 @@ static void fill_row_from_svc(struct svc_row *r, svc_t *svc) r->manual = svc->manual; r->forking = svc->forking; r->started = svc->started; + + strlcpy(r->notify, svc_notify_str(svc), sizeof(r->notify)); + r->remain = svc->remain; + strlcpy(r->ifstmt, svc->ifstmt, sizeof(r->ifstmt)); + svc_provides_str(svc, r->provides, sizeof(r->provides)); + strlcpy(r->conflicts, svc->conflict, sizeof(r->conflicts)); + strlcpy(r->supgroups, svc->supgroups, sizeof(r->supgroups)); + strlcpy(r->caps, svc->capabilities, sizeof(r->caps)); + strlcpy(r->pam, svc->pam, sizeof(r->pam)); + snprintf(r->stop_signal, sizeof(r->stop_signal), "SIG%s", sig2str(svc->sighalt)); + r->stop_timeout = (uint32_t)(svc->killdelay / 1000); + strlcpy(r->stop_scope, svc_scope_str(svc->stop_scope), sizeof(r->stop_scope)); + strlcpy(r->reload_signal, svc->sighup ? "SIGHUP" : "none", sizeof(r->reload_signal)); + r->restart_sec = (uint32_t)(svc->restart_saved / 1000); + r->respawn = svc->respawn; + strlcpy(r->name, svc->name, sizeof(r->name)); + strlcpy(r->id, svc->id, sizeof(r->id)); + r->job = (uint32_t)svc->job; + r->oldpid = (uint32_t)svc->oldpid; + r->dirty = svc->dirty; + r->removed = svc->removed; + r->starting = svc->starting; + rlimits2str(svc->rlimit, r->rlimits, sizeof(r->rlimits)); + strlcpy(r->cgroup_name, svc->cgroup.name, sizeof(r->cgroup_name)); + strlcpy(r->cgroup_cfg, svc->cgroup.cfg, sizeof(r->cgroup_cfg)); + r->cgroup_delegate = svc->cgroup.delegate; + for (int i = 0; i < NUM_SVCDIRS; i++) + r->dir_mode[i] = (uint32_t)svc->dir_mode[i]; + r->protect = svc->protect; + r->nowarn = svc->nowarn; + r->flux_reload = svc->flux_reload; + strlcpy(r->oncrash, svc_oncrash_str(svc), sizeof(r->oncrash)); + strlcpy(r->dir[0], svc->runtime_dir, sizeof(r->dir[0])); + strlcpy(r->dir[1], svc->state_dir, sizeof(r->dir[1])); + strlcpy(r->dir[2], svc->cache_dir, sizeof(r->dir[2])); + strlcpy(r->dir[3], svc->logs_dir, sizeof(r->dir[3])); + strlcpy(r->dir[4], svc->config_dir, sizeof(r->dir[4])); + strlcpy(r->preserve, svc_preserve_str(svc), sizeof(r->preserve)); + strlcpy(r->script[0], svc->pre_script, sizeof(r->script[0])); + r->script_tmo[0] = (uint32_t)(svc->pre_tmo / 1000); + strlcpy(r->script[1], svc->ready_script, sizeof(r->script[1])); + r->script_tmo[1] = (uint32_t)(svc->ready_tmo / 1000); + strlcpy(r->script[2], svc->stop_script, sizeof(r->script[2])); + r->script_tmo[2] = (uint32_t)(svc->stop_tmo / 1000); + strlcpy(r->script[3], svc->post_script, sizeof(r->script[3])); + r->script_tmo[3] = (uint32_t)(svc->post_tmo / 1000); + strlcpy(r->script[4], svc->reload_script, sizeof(r->script[4])); + r->script_tmo[4] = (uint32_t)(svc->reload_tmo / 1000); + strlcpy(r->script[5], svc->cleanup_script, sizeof(r->script[5])); + r->script_tmo[5] = (uint32_t)(svc->cleanup_tmo / 1000); + if (svc_is_tty(svc)) { + strlcpy(r->tty_dev, svc->dev, sizeof(r->tty_dev)); + strlcpy(r->tty_baud, svc->baud, sizeof(r->tty_baud)); + strlcpy(r->tty_term, svc->term, sizeof(r->tty_term)); + r->tty_noclear = svc->noclear; + r->tty_nowait = svc->nowait; + r->tty_nologin = svc->nologin; + r->tty_notty = svc->notty; + r->tty_rescue = svc->rescue; + } else { + r->log = svc->log.enabled; + r->log_null = svc->log.null; + r->log_console = svc->log.console; + strlcpy(r->log_file, svc->log.file, sizeof(r->log_file)); + strlcpy(r->log_prio, svc->log.prio, sizeof(r->log_prio)); + strlcpy(r->log_ident, svc->log.ident, sizeof(r->log_ident)); + } } static const char *pidfile_str(struct svc_row *r) @@ -1700,6 +1827,176 @@ static char *json_escape(const char *str) return buf; } +static const char *json_bool(int val) +{ + return val ? "true" : "false"; +} + +/* "a,b" as [ "a", "b" ], nothing when the list is empty */ +static void json_list(FILE *fp, const char *indent, const char *key, const char *list) +{ + char *buf = strdupa(list), *tok, *ptr; + const char *sep = ""; + + if (!list[0]) + return; + + fprintf(fp, "%s \"%s\": [", indent, key); + for (tok = strtok_r(buf, ",", &ptr); tok; tok = strtok_r(NULL, ",", &ptr)) { + fprintf(fp, "%s \"%s\"", sep, json_escape(tok)); + sep = ","; + } + fprintf(fp, " ],\n"); +} + +/* "nofile=1024:unlimited,..." from rlimits2str() as an object */ +static void json_rlimits(FILE *fp, const char *indent, const char *rlimits) +{ + char *buf = strdupa(rlimits), *tok, *ptr; + const char *sep = ""; + + fprintf(fp, "%s \"rlimits\": {", indent); + for (tok = strtok_r(buf, ",", &ptr); tok; tok = strtok_r(NULL, ",", &ptr)) { + char *soft = strchr(tok, '='), *hard; + const char *q[2]; + + if (!soft || !(hard = strchr(soft, ':'))) + continue; + *soft++ = 0; + *hard++ = 0; + + /* unlimited is the only value that is not a number */ + q[0] = soft[0] == 'u' ? "\"" : ""; + q[1] = hard[0] == 'u' ? "\"" : ""; + fprintf(fp, "%s\n%s \"%s\": { \"soft\": %s%s%s, \"hard\": %s%s%s }", + sep, indent, tok, q[0], soft, q[0], q[1], hard, q[1]); + sep = ","; + } + if (sep[0]) + fprintf(fp, "\n%s ", indent); + fprintf(fp, "},\n"); +} + +/* The settings of a service, what the plain view leaves out */ +static void json_settings(FILE *fp, struct svc_row *r, const char *indent) +{ + const char *sep = ""; + int i; + + fprintf(fp, "%s \"name\": \"%s\",\n", indent, json_escape(r->name)); + fprintf(fp, "%s \"id\": \"%s\",\n", indent, json_escape(r->id)); + fprintf(fp, + "%s \"internal\": {\n" + "%s \"job\": %u,\n" + "%s \"oldpid\": %u,\n" + "%s \"starting\": %s,\n" + "%s \"dirty\": %s,\n" + "%s \"removed\": %s\n" + "%s },\n", + indent, + indent, r->job, + indent, r->oldpid, + indent, json_bool(r->starting), + indent, json_bool(r->dirty), + indent, json_bool(r->removed), + indent); + fprintf(fp, "%s \"protected\": %s,\n", indent, json_bool(r->protect)); + fprintf(fp, "%s \"nowarn\": %s,\n", indent, json_bool(r->nowarn)); + fprintf(fp, "%s \"notify\": \"%s\",\n", indent, r->notify); + fprintf(fp, "%s \"remain\": %s,\n", indent, json_bool(r->remain)); + if (r->ifstmt[0]) + fprintf(fp, "%s \"if\": \"%s\",\n", indent, json_escape(r->ifstmt)); + json_list(fp, indent, "provides", r->provides); + json_list(fp, indent, "conflicts", r->conflicts); + + fprintf(fp, + "%s \"stop\": {\n" + "%s \"signal\": \"%s\",\n" + "%s \"timeout\": %u,\n" + "%s \"scope\": \"%s\"\n" + "%s },\n" + "%s \"reload\": {\n" + "%s \"signal\": \"%s\",\n" + "%s \"propagate\": %s\n" + "%s },\n", + indent, + indent, r->stop_signal, + indent, r->stop_timeout, + indent, r->stop_scope, + indent, + indent, + indent, r->reload_signal, + indent, json_bool(r->flux_reload), + indent); + + fprintf(fp, "%s \"scripts\": {", indent); + for (i = 0; i < NUM_SCRIPTS; i++) { + if (!r->script[i][0]) + continue; + + fprintf(fp, "%s\n%s \"%s\": { \"command\": \"%s\", \"timeout\": %u }", + sep, indent, script_names[i], json_escape(r->script[i]), r->script_tmo[i]); + sep = ","; + } + if (sep[0]) + fprintf(fp, "\n%s ", indent); + fprintf(fp, "},\n"); + sep = ""; + + fprintf(fp, "%s \"directories\": {", indent); + for (i = 0; i < NUM_SVCDIRS; i++) { + if (!r->dir[i][0]) + continue; + + fprintf(fp, "%s\n%s \"%s\": { \"name\": \"%s\", \"mode\": \"%04o\" }", + sep, indent, dir_names[i], json_escape(r->dir[i]), r->dir_mode[i]); + sep = ","; + } + if (sep[0]) + fprintf(fp, ",\n%s \"preserve\": \"%s\"\n%s ", indent, r->preserve, indent); + fprintf(fp, "},\n"); + + fprintf(fp, "%s \"cgroup-config\": {\n", indent); + fprintf(fp, "%s \"group\": \"%s\",\n", indent, json_escape(r->cgroup_name)); + fprintf(fp, "%s \"config\": \"%s\",\n", indent, json_escape(r->cgroup_cfg)); + fprintf(fp, "%s \"delegate\": %s\n%s },\n", indent, json_bool(r->cgroup_delegate), indent); + + json_rlimits(fp, indent, r->rlimits); + + if (!strcmp(r->type, "tty")) { + fprintf(fp, "%s \"tty\": {\n", indent); + fprintf(fp, "%s \"device\": \"%s\",\n", indent, json_escape(r->tty_dev)); + fprintf(fp, "%s \"baud\": \"%s\",\n", indent, r->tty_baud); + fprintf(fp, "%s \"term\": \"%s\",\n", indent, json_escape(r->tty_term)); + fprintf(fp, + "%s \"noclear\": %s,\n" + "%s \"nowait\": %s,\n" + "%s \"nologin\": %s,\n" + "%s \"notty\": %s,\n" + "%s \"rescue\": %s\n" + "%s },\n", + indent, json_bool(r->tty_noclear), + indent, json_bool(r->tty_nowait), + indent, json_bool(r->tty_nologin), + indent, json_bool(r->tty_notty), + indent, json_bool(r->tty_rescue), + indent); + } else { + fprintf(fp, + "%s \"log\": {\n" + "%s \"enabled\": %s,\n" + "%s \"null\": %s,\n" + "%s \"console\": %s,\n", + indent, + indent, json_bool(r->log), + indent, json_bool(r->log_null), + indent, json_bool(r->log_console)); + fprintf(fp, "%s \"file\": \"%s\",\n", indent, json_escape(r->log_file)); + fprintf(fp, "%s \"priority\": \"%s\",\n", indent, json_escape(r->log_prio)); + fprintf(fp, "%s \"identity\": \"%s\"\n%s },\n", indent, json_escape(r->log_ident), indent); + } +} + static int json_status_one(FILE *fp, struct svc_row *r, char *indent, int prev) { const char *pidfn = pidfile_str(r); @@ -1763,19 +2060,26 @@ static int json_status_one(FILE *fp, struct svc_row *r, char *indent, int prev) "%s \"restart\": {\n" "%s \"count\": %d,\n" "%s \"max\": %d,\n" + "%s \"sec\": %u,\n" "%s \"delay\": %u,\n" - "%s \"limit\": %u\n" + "%s \"limit\": %u,\n" + "%s \"respawn\": %s,\n" + "%s \"oncrash\": \"%s\"\n" "%s },\n", - indent, r->manual ? "true" : "false", - indent, r->started ? "true" : "false", + indent, json_bool(r->manual), + indent, json_bool(r->started), indent, r->starts, indent, r->restart_tot, indent, indent, r->restart_cnt, indent, r->restart_max, + indent, r->restart_sec, indent, r->restart_delay, indent, r->restart_delay_max, + indent, json_bool(r->respawn), + indent, r->oncrash, indent); + json_settings(fp, r, indent); /* Add memory and CPU information if cgroup support is available */ if (cgrp && r->pid > 1) { @@ -1885,14 +2189,19 @@ static int json_status_one(FILE *fp, struct svc_row *r, char *indent, int prev) "%s \"pidfile\": \"%s\",\n" "%s \"pid\": %u,\n" "%s \"user\": \"%s\",\n" - "%s \"group\": \"%s\",\n" - "%s \"uptime\": %ld,\n" - "%s \"runlevels\": %s\n" - "%s}", + "%s \"group\": \"%s\",\n", indent, pidfn, indent, r->pid, indent, r->user, - indent, r->grp, + indent, r->grp); + json_list(fp, indent, "groups", r->supgroups); + json_list(fp, indent, "capabilities", r->caps); + if (r->pam[0]) + fprintf(fp, "%s \"pam\": \"%s\",\n", indent, json_escape(r->pam)); + fprintf(fp, + "%s \"uptime\": %ld,\n" + "%s \"runlevels\": %s\n" + "%s}", indent, (long)r->uptime, indent, runlevel_arr((int)r->runlevels), indent); @@ -2082,6 +2391,72 @@ static const struct row_field { ROW_BOOL("ManualStart", manual), ROW_BOOL("Forking", forking), ROW_BOOL("Started", started), + ROW_STR ("Notify", notify), + ROW_BOOL("Remain", remain), + ROW_STR ("If", ifstmt), + ROW_STR ("Provides", provides), + ROW_STR ("Conflicts", conflicts), + ROW_STR ("ExtraGroups", supgroups), + ROW_STR ("Capabilities", caps), + ROW_STR ("Pam", pam), + ROW_STR ("StopSignal", stop_signal), + ROW_U32 ("StopTimeout", stop_timeout), + ROW_STR ("StopScope", stop_scope), + ROW_STR ("ReloadSignal", reload_signal), + ROW_U32 ("RestartSec", restart_sec), + ROW_BOOL("Respawn", respawn), + ROW_STR ("Name", name), + ROW_STR ("Id", id), + ROW_U32 ("Job", job), + ROW_U32 ("OldPid", oldpid), + ROW_BOOL("Dirty", dirty), + ROW_BOOL("Removed", removed), + ROW_BOOL("Starting", starting), + ROW_STR ("Rlimits", rlimits), + ROW_STR ("CgroupName", cgroup_name), + ROW_STR ("CgroupConfig", cgroup_cfg), + ROW_BOOL("CgroupDelegate", cgroup_delegate), + ROW_U32 ("RuntimeDirMode", dir_mode[0]), + ROW_U32 ("StateDirMode", dir_mode[1]), + ROW_U32 ("CacheDirMode", dir_mode[2]), + ROW_U32 ("LogsDirMode", dir_mode[3]), + ROW_U32 ("ConfigDirMode", dir_mode[4]), + ROW_BOOL("Protected", protect), + ROW_BOOL("NoWarn", nowarn), + ROW_BOOL("ReloadPropagate", flux_reload), + ROW_STR ("OnCrash", oncrash), + ROW_STR ("RuntimeDir", dir[0]), + ROW_STR ("StateDir", dir[1]), + ROW_STR ("CacheDir", dir[2]), + ROW_STR ("LogsDir", dir[3]), + ROW_STR ("ConfigDir", dir[4]), + ROW_STR ("RuntimeDirPreserve", preserve), + ROW_STR ("ExecStartPre", script[0]), + ROW_U32 ("ExecStartPreTimeout", script_tmo[0]), + ROW_STR ("ExecStartReady", script[1]), + ROW_U32 ("ExecStartReadyTimeout", script_tmo[1]), + ROW_STR ("ExecStop", script[2]), + ROW_U32 ("ExecStopTimeout", script_tmo[2]), + ROW_STR ("ExecStopPost", script[3]), + ROW_U32 ("ExecStopPostTimeout", script_tmo[3]), + ROW_STR ("ExecReload", script[4]), + ROW_U32 ("ExecReloadTimeout", script_tmo[4]), + ROW_STR ("ExecCleanup", script[5]), + ROW_U32 ("ExecCleanupTimeout", script_tmo[5]), + ROW_BOOL("Log", log), + ROW_BOOL("LogNull", log_null), + ROW_BOOL("LogConsole", log_console), + ROW_STR ("LogFile", log_file), + ROW_STR ("LogPriority", log_prio), + ROW_STR ("LogIdentity", log_ident), + ROW_STR ("TtyDevice", tty_dev), + ROW_STR ("TtyBaud", tty_baud), + ROW_STR ("TtyTerm", tty_term), + ROW_BOOL("TtyNoClear", tty_noclear), + ROW_BOOL("TtyNoWait", tty_nowait), + ROW_BOOL("TtyNoLogin", tty_nologin), + ROW_BOOL("TtyNoTty", tty_notty), + ROW_BOOL("TtyRescue", tty_rescue), { NULL, 0, 0, 0 } }; diff --git a/src/service.c b/src/service.c index b56aadaa..0644ff48 100644 --- a/src/service.c +++ b/src/service.c @@ -2013,23 +2013,16 @@ static void parse_sighalt(svc_t *svc, char *arg) svc->sighalt = signo; } -static const char *scopes[] = { - [SVC_SCOPE_PGROUP] = "pgroup", - [SVC_SCOPE_PROCESS] = "process", - [SVC_SCOPE_CGROUP] = "cgroup", - [SVC_SCOPE_MIXED] = "mixed", -}; - static svc_scope_t parse_scope(svc_t *svc, const char *arg) { - size_t i; + svc_scope_t i; if (!arg) return SVC_SCOPE_PGROUP; - for (i = 0; i < NELEMS(scopes); i++) { - if (!strcmp(arg, scopes[i])) - return (svc_scope_t)i; + for (i = SVC_SCOPE_PGROUP; i <= SVC_SCOPE_MIXED; i++) { + if (!strcmp(arg, svc_scope_str(i))) + return i; } warnx("%s: unknown stop-scope '%s', using pgroup", svc_ident(svc, NULL, 0), arg); @@ -2059,7 +2052,7 @@ static void scope_check(svc_t *svc) continue; logit(LOG_WARNING, "%s: stop-scope %s also reaches %s, give it a cgroup name of its own", - svc_ident(scoped, a, sizeof(a)), scopes[scoped->stop_scope], + svc_ident(scoped, a, sizeof(a)), svc_scope_str(scoped->stop_scope), svc_ident(scoped == svc ? s : svc, b, sizeof(b))); return; } diff --git a/src/svc.h b/src/svc.h index 6f070ee8..6cd22d0a 100644 --- a/src/svc.h +++ b/src/svc.h @@ -379,6 +379,77 @@ static inline int svc_has_cond(svc_t *svc) return 0; } +static inline const char *svc_scope_str(svc_scope_t scope) +{ + switch (scope) { + case SVC_SCOPE_PGROUP: + return "pgroup"; + case SVC_SCOPE_PROCESS: + return "process"; + case SVC_SCOPE_CGROUP: + return "cgroup"; + case SVC_SCOPE_MIXED: + return "mixed"; + } + + return "unknown"; +} + +static inline const char *svc_notify_str(svc_t *svc) +{ + switch (svc->notify) { + case SVC_NOTIFY_NONE: + return "none"; + case SVC_NOTIFY_PID: + return "pid"; + case SVC_NOTIFY_SYSTEMD: + return "systemd"; + case SVC_NOTIFY_S6: + return "s6"; + } + + return "unknown"; +} + +static inline const char *svc_oncrash_str(svc_t *svc) +{ + switch (svc->oncrash_action) { + case SVC_ONCRASH_REBOOT: + return "reboot"; + case SVC_ONCRASH_SCRIPT: + return "script"; + } + + return "ignore"; +} + +static inline const char *svc_preserve_str(svc_t *svc) +{ + switch (svc->dir_preserve) { + case SVC_DIR_PRESERVE_RESTART: + return "restart"; + case SVC_DIR_PRESERVE_YES: + return "yes"; + } + + return "no"; +} + +/* The conditions in provides, comma separated */ +static inline char *svc_provides_str(svc_t *svc, char *buf, size_t len) +{ + int i; + + buf[0] = 0; + for (i = 0; i < svc->num_provides; i++) { + if (i) + strlcat(buf, ",", len); + strlcat(buf, svc->provides[i], len); + } + + return buf; +} + static inline const char *svc_typestr(svc_t *svc) { switch (svc->type) { diff --git a/src/util.c b/src/util.c index 72c59c1b..78dc48c4 100644 --- a/src/util.c +++ b/src/util.c @@ -722,6 +722,30 @@ char *rlim2str(int rlim) return "unknown"; } +/* All of @rlimit as "name=soft:hard,...", a value is a number or unlimited */ +char *rlimits2str(struct rlimit *rlimit, char *buf, size_t len) +{ + const struct rlimit_name *rn; + size_t pos = 0; + + buf[0] = 0; + for (rn = rlimit_names; rn->name && pos < len; rn++) { + rlim_t lim[2] = { rlimit[rn->val].rlim_cur, rlimit[rn->val].rlim_max }; + int i; + + pos += snprintf(&buf[pos], len - pos, "%s%s", pos ? "," : "", rn->name); + for (i = 0; i < 2 && pos < len; i++) { + if (lim[i] == RLIM_INFINITY) + pos += snprintf(&buf[pos], len - pos, "%cunlimited", i ? ':' : '='); + else + pos += snprintf(&buf[pos], len - pos, "%c%llu", i ? ':' : '=', + (unsigned long long)lim[i]); + } + } + + return buf; +} + char *lim2str(struct rlimit *rlim) { char tmp[25]; diff --git a/src/util.h b/src/util.h index 7780dc49..be519de1 100644 --- a/src/util.h +++ b/src/util.h @@ -95,6 +95,7 @@ char *code2str (int code); int str2rlim (char *str); char *rlim2str (int rlim); char *lim2str (struct rlimit *rlim); +char *rlimits2str (struct rlimit *rlimit, char *buf, size_t len); void do_sleep (unsigned int sec); void do_usleep (unsigned int usec); diff --git a/test/Makefile.am b/test/Makefile.am index 846ec291..2845abb4 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -73,6 +73,7 @@ EXTRA_DIST += run-task-tricks.sh EXTRA_DIST += runparts.sh EXTRA_DIST += sysvparts.sh EXTRA_DIST += start-stop-service.sh +EXTRA_DIST += status-json.sh EXTRA_DIST += start-stop-service-sub-config.sh EXTRA_DIST += start-kill-service.sh EXTRA_DIST += start-kill-stop.sh @@ -148,6 +149,7 @@ TESTS += run-restart-forever.sh TESTS += run-task-tricks.sh TESTS += runparts.sh TESTS += start-stop-service.sh +TESTS += status-json.sh TESTS += start-stop-service-sub-config.sh TESTS += start-kill-service.sh TESTS += start-kill-stop.sh diff --git a/test/restart-backoff.sh b/test/restart-backoff.sh index 37040b58..eedc12db 100755 --- a/test/restart-backoff.sh +++ b/test/restart-backoff.sh @@ -78,7 +78,7 @@ assert_delay 4 5 4 5 say 'The restart counter stops where the delay peaks' assert "Delay and limit in status" "$(texec initctl status crash | sed -n 's/.*Restart in : //p')" = "4 sec (max 4)" run "initctl -j status crash" -assert "Same in JSON" "$(texec initctl -j status crash | jq -c .restart)" = '{"count":2,"max":-1,"delay":4,"limit":4}' +assert "Same in JSON" "$(texec initctl -j status crash | jq -c .restart)" = '{"count":2,"max":-1,"sec":1,"delay":4,"limit":4,"respawn":false,"oncrash":"ignore"}' assert "Restart counter is 2" "$(texec initctl status crash | awk '/Restarts/{ print $4 }')" = "(2/-1)" sep diff --git a/test/status-json.sh b/test/status-json.sh new file mode 100755 index 00000000..6a01f69c --- /dev/null +++ b/test/status-json.sh @@ -0,0 +1,98 @@ +#!/bin/sh +# Verify initctl -j status: it has the settings of a service, and gives +# the same over D-Bus as over the initctl socket. +set -eu + +TEST_DIR=$(dirname "$0") + +BUS=/run/finit/bus + +# Check member $1 of the JSON status in $json +assert_json() +{ + assert "$1 is $2" "$(echo "$json" | jq -c "$1")" = "$2" +} + +test_teardown() +{ + say "Running test teardown." + run "test -e $BUS.hidden && mv $BUS.hidden $BUS || true" + run "rm -f $FINIT_CONF" +} + +# shellcheck source=/dev/null +. "$TEST_DIR/lib/setup.sh" + +run "cat > $FINIT_CONF" <