Refactor, unify finit_tty_t + tty_node_t --> struct tty

Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
This commit is contained in:
Joachim Nilsson committed 2018-03-18 18:19:31 +01:00
1 parent dc7039b8d8
commit 406339e755
3 files changed
+67 -73

No files matched your search

+5 -5
View File
@@ -57,7 +57,7 @@ static void watcher(void *arg, int fd, int events)
{ {
int len = 0; int len = 0;
char buf[EVENT_SIZE], name[42]; char buf[EVENT_SIZE], name[42];
tty_node_t *entry; struct tty *entry;
struct inotify_event *notified = (struct inotify_event *)buf; struct inotify_event *notified = (struct inotify_event *)buf;
while ((len = read(fd, buf, sizeof(buf)))) { while ((len = read(fd, buf, sizeof(buf)))) {
@@ -72,11 +72,11 @@ static void watcher(void *arg, int fd, int events)
snprintf(name, sizeof(name), "/dev/%s", notified->name); snprintf(name, sizeof(name), "/dev/%s", notified->name);
entry = tty_find(name); entry = tty_find(name);
if (entry && tty_enabled(&entry->data)) { if (entry && tty_enabled(entry)) {
if (notified->mask & IN_CREATE) if (notified->mask & IN_CREATE)
tty_start(&entry->data); tty_start(entry);
else if (entry->data.pid) else if (entry->pid)
tty_stop(&entry->data); tty_stop(entry);
} }
} }
} }
+50 -50
View File
@@ -41,7 +41,7 @@
#ifdef FALLBACK_SHELL #ifdef FALLBACK_SHELL
static pid_t fallback = 0; static pid_t fallback = 0;
#endif #endif
static LIST_HEAD(, tty_node) tty_list = LIST_HEAD_INITIALIZER(); static LIST_HEAD(, tty) tty_list = LIST_HEAD_INITIALIZER();
static char *canonicalize(char *tty) static char *canonicalize(char *tty)
{ {
@@ -88,7 +88,7 @@ static char *canonicalize(char *tty)
void tty_mark(void) void tty_mark(void)
{ {
tty_node_t *tty; struct tty *tty;
LIST_FOREACH(tty, &tty_list, link) LIST_FOREACH(tty, &tty_list, link)
tty->dirty = -1; tty->dirty = -1;
@@ -96,17 +96,17 @@ void tty_mark(void)
void tty_sweep(void) void tty_sweep(void)
{ {
tty_node_t *tty, *tmp; struct tty *tty, *tmp;
LIST_FOREACH_SAFE(tty, &tty_list, link, tmp) { LIST_FOREACH_SAFE(tty, &tty_list, link, tmp) {
if (!tty->dirty) if (!tty->dirty)
continue; continue;
_d("TTY %s dirty, stopping ...", tty->data.name); _d("TTY %s dirty, stopping ...", tty->name);
tty_stop(&tty->data); tty_stop(tty);
if (tty->dirty == -1) { if (tty->dirty == -1) {
_d("TTY %s removed, cleaning up.", tty->data.name); _d("TTY %s removed, cleaning up.", tty->name);
tty_unregister(tty); tty_unregister(tty);
} }
} }
@@ -136,7 +136,7 @@ void tty_sweep(void)
*/ */
int tty_register(char *line, struct rlimit rlimit[], char *file) int tty_register(char *line, struct rlimit rlimit[], char *file)
{ {
tty_node_t *entry; struct tty *entry;
int insert = 0, noclear = 0, nowait = 0, nologin = 0; int insert = 0, noclear = 0, nowait = 0, nologin = 0;
size_t i, num = 0; size_t i, num = 0;
char *tok, *cmd = NULL, *args[TTY_MAX_ARGS]; char *tok, *cmd = NULL, *args[TTY_MAX_ARGS];
@@ -238,13 +238,13 @@ int tty_register(char *line, struct rlimit rlimit[], char *file)
} }
} }
entry->data.name = dev; entry->name = dev;
entry->data.baud = baud ? strdup(baud) : NULL; entry->baud = baud ? strdup(baud) : NULL;
entry->data.term = term ? strdup(term) : NULL; entry->term = term ? strdup(term) : NULL;
entry->data.noclear = noclear; entry->noclear = noclear;
entry->data.nowait = nowait; entry->nowait = nowait;
entry->data.nologin = nologin; entry->nologin = nologin;
entry->data.runlevels = conf_parse_runlevels(runlevels); entry->runlevels = conf_parse_runlevels(runlevels);
/* External getty */ /* External getty */
if (cmd) { if (cmd) {
@@ -255,12 +255,12 @@ int tty_register(char *line, struct rlimit rlimit[], char *file)
tok = cmd; tok = cmd;
else else
tok++; tok++;
entry->data.cmd = cmd; entry->cmd = cmd;
args[1] = strdup(tok); args[1] = strdup(tok);
for (i = 1; i < num; i++) for (i = 1; i < num; i++)
entry->data.args[j++] = strdup(args[i]); entry->args[j++] = strdup(args[i]);
entry->data.args[++j] = NULL; entry->args[++j] = NULL;
} }
_d("Registering %s getty on TTY %s at %s baud with term %s on runlevels %s", _d("Registering %s getty on TTY %s at %s baud with term %s on runlevels %s",
@@ -270,7 +270,7 @@ int tty_register(char *line, struct rlimit rlimit[], char *file)
LIST_INSERT_HEAD(&tty_list, entry, link); LIST_INSERT_HEAD(&tty_list, entry, link);
/* Register configured limits */ /* Register configured limits */
memcpy(entry->data.rlimit, rlimit, sizeof(entry->data.rlimit)); memcpy(entry->rlimit, rlimit, sizeof(entry->rlimit));
if (file && conf_changed(file)) if (file && conf_changed(file))
entry->dirty = 1; /* Modified, restart */ entry->dirty = 1; /* Modified, restart */
@@ -281,7 +281,7 @@ int tty_register(char *line, struct rlimit rlimit[], char *file)
return 0; return 0;
} }
int tty_unregister(tty_node_t *tty) int tty_unregister(struct tty *tty)
{ {
if (!tty) { if (!tty) {
_e("Missing argument"); _e("Missing argument");
@@ -290,20 +290,20 @@ int tty_unregister(tty_node_t *tty)
LIST_REMOVE(tty, link); LIST_REMOVE(tty, link);
if (tty->data.name) if (tty->name)
free(tty->data.name); free(tty->name);
if (tty->data.baud) if (tty->baud)
free(tty->data.baud); free(tty->baud);
if (tty->data.term) if (tty->term)
free(tty->data.term); free(tty->term);
if (tty->data.cmd) { if (tty->cmd) {
int i; int i;
free(tty->data.cmd); free(tty->cmd);
for (i = 0; i < TTY_MAX_ARGS; i++) { for (i = 0; i < TTY_MAX_ARGS; i++) {
if (tty->data.args[i]) if (tty->args[i])
free(tty->data.args[i]); free(tty->args[i]);
tty->data.args[i] = NULL; tty->args[i] = NULL;
} }
} }
free(tty); free(tty);
@@ -311,12 +311,12 @@ int tty_unregister(tty_node_t *tty)
return 0; return 0;
} }
tty_node_t *tty_find(char *dev) struct tty *tty_find(char *dev)
{ {
tty_node_t *entry; struct tty *entry;
LIST_FOREACH(entry, &tty_list, link) { LIST_FOREACH(entry, &tty_list, link) {
if (!strcmp(dev, entry->data.name)) if (!strcmp(dev, entry->name))
return entry; return entry;
} }
@@ -326,7 +326,7 @@ tty_node_t *tty_find(char *dev)
size_t tty_num(void) size_t tty_num(void)
{ {
size_t num = 0; size_t num = 0;
tty_node_t *entry; struct tty *entry;
LIST_FOREACH(entry, &tty_list, link) LIST_FOREACH(entry, &tty_list, link)
num++; num++;
@@ -337,22 +337,22 @@ size_t tty_num(void)
size_t tty_num_active(void) size_t tty_num_active(void)
{ {
size_t num = 0; size_t num = 0;
tty_node_t *entry; struct tty *entry;
LIST_FOREACH(entry, &tty_list, link) { LIST_FOREACH(entry, &tty_list, link) {
if (entry->data.pid) if (entry->pid)
num++; num++;
} }
return num; return num;
} }
tty_node_t *tty_find_by_pid(pid_t pid) struct tty *tty_find_by_pid(pid_t pid)
{ {
tty_node_t *entry; struct tty *entry;
LIST_FOREACH(entry, &tty_list, link) { LIST_FOREACH(entry, &tty_list, link) {
if (entry->data.pid == pid) if (entry->pid == pid)
return entry; return entry;
} }
@@ -375,7 +375,7 @@ static int tty_exist(char *dev)
return result; return result;
} }
void tty_start(finit_tty_t *tty) void tty_start(struct tty *tty)
{ {
char *dev; char *dev;
@@ -408,7 +408,7 @@ void tty_start(finit_tty_t *tty)
tty->pid = run_getty2(dev, tty->cmd, tty->args, tty->noclear, tty->nowait, tty->rlimit); tty->pid = run_getty2(dev, tty->cmd, tty->args, tty->noclear, tty->nowait, tty->rlimit);
} }
void tty_stop(finit_tty_t *tty) void tty_stop(struct tty *tty)
{ {
if (!tty->pid) if (!tty->pid)
return; return;
@@ -425,7 +425,7 @@ void tty_stop(finit_tty_t *tty)
tty->pid = 0; tty->pid = 0;
} }
int tty_enabled(finit_tty_t *tty) int tty_enabled(struct tty *tty)
{ {
if (!tty) if (!tty)
return 0; return 0;
@@ -473,12 +473,12 @@ int tty_fallback(pid_t lost)
return 0; return 0;
} }
static void tty_action(tty_node_t *tty) static void tty_action(struct tty *tty)
{ {
if (!tty_enabled(&tty->data)) if (!tty_enabled(tty))
tty_stop(&tty->data); tty_stop(tty);
else else
tty_start(&tty->data); tty_start(tty);
} }
/* /*
@@ -486,7 +486,7 @@ static void tty_action(tty_node_t *tty)
*/ */
int tty_respawn(pid_t pid) int tty_respawn(pid_t pid)
{ {
tty_node_t *tty = tty_find_by_pid(pid); struct tty *tty = tty_find_by_pid(pid);
if (!tty) if (!tty)
return tty_fallback(pid); return tty_fallback(pid);
@@ -495,7 +495,7 @@ int tty_respawn(pid_t pid)
utmp_set_dead(pid); utmp_set_dead(pid);
/* Clear PID to be able to respawn it. */ /* Clear PID to be able to respawn it. */
tty->data.pid = 0; tty->pid = 0;
tty_action(tty); tty_action(tty);
return 1; return 1;
@@ -506,7 +506,7 @@ int tty_respawn(pid_t pid)
*/ */
void tty_reload(char *dev) void tty_reload(char *dev)
{ {
tty_node_t *tty; struct tty *tty;
if (dev) { if (dev) {
tty = tty_find(dev); tty = tty_find(dev);
@@ -531,7 +531,7 @@ void tty_reload(char *dev)
/* Start all TTYs that exist in the system and are allowed at this runlevel */ /* Start all TTYs that exist in the system and are allowed at this runlevel */
void tty_runlevel(void) void tty_runlevel(void)
{ {
tty_node_t *tty; struct tty *tty;
LIST_FOREACH(tty, &tty_list, link) LIST_FOREACH(tty, &tty_list, link)
tty_action(tty); tty_action(tty);
+12 -18
View File
@@ -32,7 +32,9 @@
#define TTY_MAX_ARGS 16 #define TTY_MAX_ARGS 16
#define EVENT_SIZE ((sizeof(struct inotify_event) + NAME_MAX + 1)) #define EVENT_SIZE ((sizeof(struct inotify_event) + NAME_MAX + 1))
typedef struct { struct tty {
LIST_ENTRY(tty) link;
char *name; char *name;
char *baud; char *baud;
char *term; char *term;
@@ -48,32 +50,24 @@ typedef struct {
/* Limits and scoping */ /* Limits and scoping */
struct rlimit rlimit[RLIMIT_NLIMITS]; struct rlimit rlimit[RLIMIT_NLIMITS];
} finit_tty_t;
typedef struct tty_node { /* Set if modified => reloaded, or -1 when marked for removal */
LIST_ENTRY(tty_node) link; int dirty;
finit_tty_t data; };
/* XXX: Yes, TTYs should be refactored into a separate SVC type. */
int dirty; /* Set if old mtime != new mtime => reloaded,
* or -1 when marked for removal */
} tty_node_t;
//extern LIST_HEAD(, tty_node) tty_list;
void tty_mark (void); void tty_mark (void);
void tty_sweep (void); void tty_sweep (void);
int tty_register (char *line, struct rlimit rlimit[], char *file); int tty_register (char *line, struct rlimit rlimit[], char *file);
int tty_unregister (tty_node_t *tty); int tty_unregister (struct tty *tty);
tty_node_t *tty_find (char *dev); struct tty *tty_find (char *dev);
size_t tty_num (void); size_t tty_num (void);
size_t tty_num_active (void); size_t tty_num_active (void);
tty_node_t *tty_find_by_pid (pid_t pid); struct tty *tty_find_by_pid (pid_t pid);
void tty_start (finit_tty_t *tty); void tty_start (struct tty *tty);
void tty_stop (finit_tty_t *tty); void tty_stop (struct tty *tty);
int tty_enabled (finit_tty_t *tty); int tty_enabled (struct tty *tty);
int tty_respawn (pid_t pid); int tty_respawn (pid_t pid);
void tty_reload (char *dev); void tty_reload (char *dev);
void tty_runlevel (void); void tty_runlevel (void);