Refactor tty handling to use service backend enabling conditions etc.

Initial refactor of the tty implementation to use the service/run/task
general backend.  This enables all the features of services also for
ttys, except logging because it makes no sense.

Work in progress:

 - plugins/tty.c does not work anymore, could possibly be removed in
   favor of usinga (a new) condition instead (if-tty-exists)
 - fallback tty does not work anymore, should we remove it, or can we
   handle it as an optional built-in with (a new) condition?
 - @console does not work anymore, needs to generate N cloned services

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg committed 2021-04-14 16:29:19 +02:00
1 parent 3cf5d36972
commit 30020263ba
12 files changed
+285 -497

No files matched your search

+90 -21
View File
@@ -538,7 +538,8 @@ static int service_start(svc_t *svc)
}
args[i] = NULL;
redirect(svc);
if (!svc_is_tty(svc))
redirect(svc);
sig_unblock();
/*
@@ -556,6 +557,8 @@ static int service_start(svc_t *svc)
if (svc_is_runtask(svc))
status = exec_runtask(args[0], &args[1]);
else if (svc_is_tty(svc))
status = tty_exec(svc);
else
status = execvp(args[0], &args[1]);
@@ -573,7 +576,10 @@ static int service_start(svc_t *svc)
_d("Starting %s %s", svc->cmd, buf);
}
cgroup_service(group_name(svc, grnam, sizeof(grnam)), pid, &svc->cgroup);
if (svc_is_tty(svc))
cgroup_user("getty", pid);
else
cgroup_service(group_name(svc, grnam, sizeof(grnam)), pid, &svc->cgroup);
logit(LOG_CONSOLE | LOG_NOTICE, "Starting %s[%d]", svc_ident(svc, NULL, 0), pid);
@@ -649,6 +655,9 @@ static void service_cleanup(svc_t *svc)
basename(svc->cmd), fn);
/* No longer running, update books. */
if (svc_is_tty(svc) && svc->pid > 1)
utmp_set_dead(svc->pid); /* Set DEAD_PROCESS UTMP entry */
svc->oldpid = svc->pid;
svc->start_time = svc->pid = 0;
}
@@ -925,13 +934,10 @@ static void parse_name(svc_t *svc, char *arg)
}
strlcpy(svc->name, name, sizeof(svc->name));
/* Warn if svc generates same condition as an existing service */
svc_validate(svc);
}
/**
* parse_cmdline_args - Update the command line args in the svc struct
/*
* Update the command line args in the svc struct
*
* strtok internal pointer must be positioned at first command line arg
* when this function is called.
@@ -1071,6 +1077,7 @@ int service_register(int type, char *cfg, struct rlimit rlimit[], char *file)
char *username = NULL, *log = NULL, *pid = NULL;
char *name = NULL, *halt = NULL, *delay = NULL;
char *id = NULL, *env = NULL, *cgroup = NULL;
struct tty tty = { 0 };
int levels = 0;
int manual = 0;
char *line;
@@ -1103,7 +1110,7 @@ int service_register(int type, char *cfg, struct rlimit rlimit[], char *file)
}
}
cmd = strtok(line, " ");
cmd = strtok(line, " \t");
if (!cmd) {
incomplete:
_e("Incomplete service '%s', cannot register", cfg);
@@ -1111,6 +1118,9 @@ int service_register(int type, char *cfg, struct rlimit rlimit[], char *file)
}
while (cmd) {
if (type == SVC_TYPE_TTY && cmd[0] == '@')
break; /* @console */
if (cmd[0] == '@') /* @username[:group] */
username = &cmd[1];
else if (cmd[0] == '[') /* [runlevels] */
@@ -1155,7 +1165,33 @@ int service_register(int type, char *cfg, struct rlimit rlimit[], char *file)
if (!id)
id = "";
svc = svc_find(cmd, id);
if (type == SVC_TYPE_TTY) {
size_t i, len = 0;
if (tty_parse_args(cmd, &tty))
return errno;
len += tty.num + 5;
for (i = 0; i < tty.num; i++)
len += strlen(tty.args[i]);
line = alloca(len);
if (!line)
return errno;
snprintf(line, len, "%s ", tty.cmd ?: "tty");
for (i = 0; i < tty.num; i++) {
strlcat(line, tty.args[i], len);
strlcat(line, " ", len);
}
cmd = strtok(line, " \t");
if (!cmd)
return errno;
svc = svc_find_by_tty(tty.dev);
} else
svc = svc_find(cmd, id);
if (!svc) {
_d("Creating new svc for %s id #%s type %d", cmd, id, type);
svc = svc_new(cmd, id, type);
@@ -1197,7 +1233,43 @@ int service_register(int type, char *cfg, struct rlimit rlimit[], char *file)
conf_parse_cond(svc, cond);
parse_name(svc, name);
if (type == SVC_TYPE_TTY) {
char *ptr;
if (tty.dev)
strlcpy(svc->dev, tty.dev, sizeof(svc->dev));
if (tty.baud)
strlcpy(svc->baud, tty.baud, sizeof(svc->baud));
if (tty.term)
strlcpy(svc->term, tty.term, sizeof(svc->term));
svc->noclear = tty.noclear;
svc->nowait = tty.nowait;
svc->nologin = tty.nologin;
/* TTYs cannot be redirected */
log = NULL;
/* Create name:id tuple for identiy, e.g., tty:S0 */
ptr = strrchr(svc->dev, '/');
if (ptr) {
ptr++;
if (!strncmp(ptr, "tty", 3))
ptr += 3;
} else
ptr = svc->dev;
if (!id || id[0] == 0)
id = ptr;
strlcpy(svc->name, "tty", sizeof(svc->name));
strlcpy(svc->id, id, sizeof(svc->id));
} else
parse_name(svc, name);
/*
* Warn if svc generates same condition (based on name:id)
* as an existing service.
*/
svc_validate(svc);
if (halt)
parse_sighalt(svc, halt);
if (delay)
@@ -1206,6 +1278,8 @@ int service_register(int type, char *cfg, struct rlimit rlimit[], char *file)
parse_log(svc, log);
if (desc)
strlcpy(svc->desc, desc, sizeof(svc->desc));
else if (type == SVC_TYPE_TTY)
snprintf(svc->desc, sizeof(svc->desc), "Getty on %s", svc->dev);
if (env)
parse_env(svc, env);
if (file)
@@ -1258,9 +1332,6 @@ void service_monitor(pid_t lost, int status)
if (fexist(SYNC_SHUTDOWN) || lost <= 1)
return;
if (tty_respawn(lost))
return;
svc = svc_find_by_pid(lost);
if (!svc) {
_d("collected unknown PID %d", lost);
@@ -1276,7 +1347,7 @@ void service_monitor(pid_t lost, int status)
return;
/* Try removing PID file (in case service does not clean up after itself) */
if (svc_is_daemon(svc)) {
if (svc_is_daemon(svc) || svc_is_tty(svc)) {
service_cleanup(svc);
} else if (svc_is_runtask(svc)) {
/* run/task should run at least once per runlevel */
@@ -1376,7 +1447,7 @@ static void svc_set_state(svc_t *svc, svc_state_t new)
*state = new;
/* if PID isn't collected within SVC_TERM_TIMEOUT msec, kill it! */
if ((*state == SVC_STOPPING_STATE)) {
if (*state == SVC_STOPPING_STATE) {
_d("%s is stopping, wait %d sec before sending SIGKILL ...",
svc->cmd, svc->killdelay / 1000);
service_timeout_cancel(svc);
@@ -1421,15 +1492,13 @@ restart:
if (!svc->pid) {
char cond[MAX_COND_LEN];
/* PID was collected normally, no need to kill it */
_d("%s: stopped normally, no need to send SIGKILL :)", svc->cmd);
_d("%s: stopped, cleaning up timers and conditions ...", svc->cmd);
service_timeout_cancel(svc);
_d("%s: clearing pid condition ...", svc->name);
cond_clear(mkcond(svc, cond, sizeof(cond)));
switch (svc->type) {
case SVC_TYPE_SERVICE:
case SVC_TYPE_TTY:
svc_set_state(svc, SVC_HALTED_STATE);
break;
@@ -1477,7 +1546,7 @@ restart:
}
if (!svc->pid) {
if (svc_is_daemon(svc)) {
if (svc_is_daemon(svc) || svc_is_tty(svc)) {
svc_restarting(svc);
svc_set_state(svc, SVC_HALTED_STATE);
@@ -1596,7 +1665,7 @@ void service_step_all(int types)
void service_worker(void *unused)
{
service_step_all(SVC_TYPE_SERVICE | SVC_TYPE_RUNTASK);
service_step_all(SVC_TYPE_RESPAWN | SVC_TYPE_RUNTASK);
}
/**