mirror of
https://github.com/troglobit/finit.git
synced 2026-10-24 11:54:36 +00:00
Add basic support to specify login TTYs in configuration
This also adds support for monitor /dev for TTYs appering after boot, USB-to-serial converters for example. Some limitations: Baudrate (and other settings) is hardcoded for now, should be read from config file.
This commit is contained in:
1 parent
a18c5f3441
commit
5961748b2a
7 files changed
+200
-57
No files matched your search
@@ -38,7 +38,7 @@ EXEC = finit reboot
|
|||||||
HEADERS = plugin.h svc.h helpers.h queue.h
|
HEADERS = plugin.h svc.h helpers.h queue.h
|
||||||
DISTFILES = LICENSE README ChangeLog finit.conf services
|
DISTFILES = LICENSE README ChangeLog finit.conf services
|
||||||
OBJS = finit.o conf.o helpers.o sig.o svc.o plugin.o
|
OBJS = finit.o conf.o helpers.o sig.o svc.o plugin.o
|
||||||
OBJS += strlcpy.o
|
OBJS += strlcpy.o tty.o
|
||||||
SRCS = $(OBJS:.o=.c)
|
SRCS = $(OBJS:.o=.c)
|
||||||
DEPS = $(addprefix .,$(SRCS:.c=.d))
|
DEPS = $(addprefix .,$(SRCS:.c=.d))
|
||||||
|
|
||||||
|
|||||||
@@ -31,7 +31,6 @@
|
|||||||
#define MATCH_CMD(l, c, x) \
|
#define MATCH_CMD(l, c, x) \
|
||||||
(!strncmp(l, c, strlen(c)) && (x = (l) + strlen(c)))
|
(!strncmp(l, c, strlen(c)) && (x = (l) + strlen(c)))
|
||||||
|
|
||||||
|
|
||||||
static char *build_cmd(char *cmd, char *line, int len)
|
static char *build_cmd(char *cmd, char *line, int len)
|
||||||
{
|
{
|
||||||
int l;
|
int l;
|
||||||
@@ -135,6 +134,20 @@ void parse_finit_conf(char *file)
|
|||||||
svc_register(x, NULL);
|
svc_register(x, NULL);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (MATCH_CMD(line, "console ", x)) {
|
||||||
|
if (console) free(console);
|
||||||
|
console = build_cmd(NULL, x, CMD_SIZE);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (MATCH_CMD(line, "tty ", x)) {
|
||||||
|
char *tty = build_cmd(NULL, x, CMD_SIZE);
|
||||||
|
int baud = 115200; /* TODO: Read from config file. */
|
||||||
|
|
||||||
|
tty_add(tty, baud);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -35,14 +35,14 @@
|
|||||||
#include "sig.h"
|
#include "sig.h"
|
||||||
#include "lite.h"
|
#include "lite.h"
|
||||||
|
|
||||||
int debug = 0;
|
int debug = 0;
|
||||||
int verbose = 1;
|
int verbose = 1;
|
||||||
char *sdown = NULL;
|
char *sdown = NULL;
|
||||||
char *network = NULL;
|
char *network = NULL;
|
||||||
char *username = NULL;
|
char *username = NULL;
|
||||||
char *hostname = NULL;
|
char *hostname = NULL;
|
||||||
char *rcsd = NULL;
|
char *rcsd = NULL;
|
||||||
|
char *console = NULL;
|
||||||
|
|
||||||
static void parse_kernel_cmdline(void)
|
static void parse_kernel_cmdline(void)
|
||||||
{
|
{
|
||||||
@@ -73,7 +73,7 @@ static int run_loop(void)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int UNUSED(args), char *argv[])
|
int main(int UNUSED(args), char* UNUSED(argv[]))
|
||||||
{
|
{
|
||||||
/*
|
/*
|
||||||
* Initial setup of signals, ignore all until we're up.
|
* Initial setup of signals, ignore all until we're up.
|
||||||
@@ -135,6 +135,7 @@ int main(int UNUSED(args), char *argv[])
|
|||||||
#ifdef SYSROOT
|
#ifdef SYSROOT
|
||||||
run(SYSROOT, "/", NULL, MS_MOVE, NULL);
|
run(SYSROOT, "/", NULL, MS_MOVE, NULL);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
_d("Root FS up, calling hooks ...");
|
_d("Root FS up, calling hooks ...");
|
||||||
plugin_run_hooks(HOOK_ROOTFS_UP);
|
plugin_run_hooks(HOOK_ROOTFS_UP);
|
||||||
|
|
||||||
@@ -193,9 +194,8 @@ int main(int UNUSED(args), char *argv[])
|
|||||||
*/
|
*/
|
||||||
plugin_run_hooks(HOOK_SYSTEM_UP);
|
plugin_run_hooks(HOOK_SYSTEM_UP);
|
||||||
|
|
||||||
/* Start GETTY on console */
|
/* Start GETTY on console(s) */
|
||||||
_d("Starting getty on console ...");
|
tty_start();
|
||||||
run_getty(GETTY, argv);
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Enter main loop to monior /dev/initctl and services
|
* Enter main loop to monior /dev/initctl and services
|
||||||
|
|||||||
@@ -38,11 +38,11 @@
|
|||||||
#if defined EMBEDDED_SYSTEM
|
#if defined EMBEDDED_SYSTEM
|
||||||
# define CONSOLE "/dev/console"
|
# define CONSOLE "/dev/console"
|
||||||
# define MDEV "/sbin/mdev"
|
# define MDEV "/sbin/mdev"
|
||||||
# define GETTY "/sbin/getty -L 115200 ttyS0 vt100"
|
# define GETTY "/sbin/getty -L 115200 %s vt100"
|
||||||
#else /* Debian/Ubuntu based distributions */
|
#else /* Debian/Ubuntu based distributions */
|
||||||
# define RANDOMSEED "/var/lib/urandom/random-seed"
|
# define RANDOMSEED "/var/lib/urandom/random-seed"
|
||||||
# define CONSOLE "/dev/tty1"
|
# define CONSOLE "/dev/tty1"
|
||||||
# define GETTY "/sbin/getty -8 38400 tty1"
|
# define GETTY "/sbin/getty -8 38400 %s"
|
||||||
# define REMOUNT_ROOTFS_RW
|
# define REMOUNT_ROOTFS_RW
|
||||||
# define RUNLEVEL 2
|
# define RUNLEVEL 2
|
||||||
# define USE_UDEV
|
# define USE_UDEV
|
||||||
@@ -62,15 +62,20 @@
|
|||||||
#define USERNAME_SIZE 16
|
#define USERNAME_SIZE 16
|
||||||
#define HOSTNAME_SIZE 32
|
#define HOSTNAME_SIZE 32
|
||||||
|
|
||||||
extern char *sdown;
|
extern char *sdown;
|
||||||
extern char *network;
|
extern char *network;
|
||||||
extern char *hostname;
|
extern char *hostname;
|
||||||
extern char *username;
|
extern char *username;
|
||||||
extern char *rcsd;
|
extern char *rcsd;
|
||||||
|
extern char *console;
|
||||||
|
|
||||||
/* conf.c */
|
/* conf.c */
|
||||||
void parse_finit_conf(char *file);
|
void parse_finit_conf(char *file);
|
||||||
|
|
||||||
|
/* tty.c */
|
||||||
|
void tty_start(void);
|
||||||
|
int tty_add(char *tty, int baud);
|
||||||
|
|
||||||
#endif /* FINIT_H_ */
|
#endif /* FINIT_H_ */
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -250,21 +250,6 @@ char *pid_get_name(pid_t pid, char *name, size_t len)
|
|||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* procname_set - Change process name, as seen in process listnings
|
|
||||||
* @name: New name of process
|
|
||||||
* @args: The process' argv[] vector.
|
|
||||||
*/
|
|
||||||
void procname_set(char *name, char *args[])
|
|
||||||
{
|
|
||||||
size_t len = strlen(args[0]) + 1; /* Include terminating '\0' */
|
|
||||||
|
|
||||||
prctl(PR_SET_NAME, name, 0, 0, 0);
|
|
||||||
memset(args[0], 0, len);
|
|
||||||
strlcpy(args[0], name, len);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* procname_kill - Send a signal to a process group by name.
|
* procname_kill - Send a signal to a process group by name.
|
||||||
* @name: Name of process to send signal to.
|
* @name: Name of process to send signal to.
|
||||||
@@ -507,7 +492,7 @@ int run_interactive(char *cmd, char *fmt, ...)
|
|||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
|
|
||||||
pid_t run_getty(char *cmd, char *argv[])
|
pid_t run_getty(char *cmd, int console)
|
||||||
{
|
{
|
||||||
pid_t pid = fork();
|
pid_t pid = fork();
|
||||||
|
|
||||||
@@ -517,31 +502,32 @@ pid_t run_getty(char *cmd, char *argv[])
|
|||||||
sigset_t nmask;
|
sigset_t nmask;
|
||||||
struct sigaction sa;
|
struct sigaction sa;
|
||||||
|
|
||||||
/* Detach from initial controlling TTY */
|
if (console) {
|
||||||
vhangup();
|
/* Detach from initial controlling TTY */
|
||||||
|
vhangup();
|
||||||
|
|
||||||
close(2);
|
close(2);
|
||||||
close(1);
|
close(1);
|
||||||
close(0);
|
close(0);
|
||||||
|
|
||||||
/* Attach TTY to console */
|
/* Attach TTY to console */
|
||||||
if (open(CONSOLE, O_RDWR) != 0)
|
if (open(CONSOLE, O_RDWR) != 0)
|
||||||
exit(1);
|
exit(1);
|
||||||
|
|
||||||
|
dup2(0, STDIN_FILENO);
|
||||||
|
dup2(0, STDOUT_FILENO);
|
||||||
|
dup2(0, STDERR_FILENO);
|
||||||
|
|
||||||
|
prctl(PR_SET_NAME, "console", 0, 0, 0);
|
||||||
|
}
|
||||||
sigemptyset(&nmask);
|
sigemptyset(&nmask);
|
||||||
sigaddset(&nmask, SIGCHLD);
|
sigaddset(&nmask, SIGCHLD);
|
||||||
sigprocmask(SIG_UNBLOCK, &nmask, NULL);
|
sigprocmask(SIG_UNBLOCK, &nmask, NULL);
|
||||||
|
|
||||||
/* Reset signal handlers that were set by the parent process */
|
/* Reset signal handlers that were set by the parent process */
|
||||||
for (i = 1; i < NSIG; i++)
|
for (i = 1; i < NSIG; i++)
|
||||||
DFLSIG(sa, i, 0);
|
DFLSIG(sa, i, 0);
|
||||||
|
|
||||||
dup2(0, STDIN_FILENO);
|
|
||||||
dup2(0, STDOUT_FILENO);
|
|
||||||
dup2(0, STDERR_FILENO);
|
|
||||||
|
|
||||||
procname_set("console", argv);
|
|
||||||
|
|
||||||
while (!fexist(SYNC_SHUTDOWN)) {
|
while (!fexist(SYNC_SHUTDOWN)) {
|
||||||
static const char msg[] = "\nPlease press Enter to activate this console. ";
|
static const char msg[] = "\nPlease press Enter to activate this console. ";
|
||||||
|
|
||||||
@@ -550,10 +536,11 @@ pid_t run_getty(char *cmd, char *argv[])
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
i = write(STDERR_FILENO, msg, sizeof(msg) - 1);
|
if (console) {
|
||||||
while (read(STDIN_FILENO, &c, 1) == 1 && c != '\n')
|
i = write(STDERR_FILENO, msg, sizeof(msg) - 1);
|
||||||
continue;
|
while (read(STDIN_FILENO, &c, 1) == 1 && c != '\n')
|
||||||
|
continue;
|
||||||
|
}
|
||||||
if (fexist(SYNC_STOPPED))
|
if (fexist(SYNC_STOPPED))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
|
|||||||
@@ -88,7 +88,7 @@ void set_hostname (char *hostname);
|
|||||||
|
|
||||||
int run (char *cmd);
|
int run (char *cmd);
|
||||||
int run_interactive (char *cmd, char *fmt, ...);
|
int run_interactive (char *cmd, char *fmt, ...);
|
||||||
pid_t run_getty (char *cmd, char *argv[]);
|
pid_t run_getty (char *cmd, int console);
|
||||||
int run_parts (char *dir, char *cmd);
|
int run_parts (char *dir, char *cmd);
|
||||||
|
|
||||||
/* strlcpy.c */
|
/* strlcpy.c */
|
||||||
|
|||||||
@@ -0,0 +1,138 @@
|
|||||||
|
/* TTY handling in finit
|
||||||
|
*
|
||||||
|
* Copyright (c) 2013 Mattias Walström <lazzer@gmail.com>
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in
|
||||||
|
* all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
* THE SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#include <sys/inotify.h>
|
||||||
|
#include <signal.h>
|
||||||
|
#include <sys/prctl.h>
|
||||||
|
#include <sys/wait.h>
|
||||||
|
|
||||||
|
#include "queue.h"
|
||||||
|
#include "finit.h"
|
||||||
|
#include "helpers.h"
|
||||||
|
#include "lite.h"
|
||||||
|
|
||||||
|
typedef struct finit_tty_t {
|
||||||
|
char *name;
|
||||||
|
int baud;
|
||||||
|
|
||||||
|
int pid; /* Runtime information*/
|
||||||
|
} finit_tty_t;
|
||||||
|
|
||||||
|
typedef struct node {
|
||||||
|
LIST_ENTRY(node) link;
|
||||||
|
finit_tty_t data;
|
||||||
|
} node_t;
|
||||||
|
|
||||||
|
|
||||||
|
/* We need to make room for the filename also, which is sent after inotify_event. */
|
||||||
|
#define EVENT_SIZE ((sizeof(struct inotify_event) + FILENAME_MAX))
|
||||||
|
|
||||||
|
LIST_HEAD(, node) node_list = LIST_HEAD_INITIALIZER();
|
||||||
|
int tty_add(char *tty, int baud)
|
||||||
|
{
|
||||||
|
node_t *entry = malloc(sizeof(*entry));
|
||||||
|
|
||||||
|
if (!entry)
|
||||||
|
return errno = ENOMEM;
|
||||||
|
|
||||||
|
entry->data.name = tty;
|
||||||
|
entry->data.baud = baud;
|
||||||
|
|
||||||
|
LIST_INSERT_HEAD(&node_list, entry, link);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void tty_startstop_one(finit_tty_t *tty, uint32_t mask)
|
||||||
|
{
|
||||||
|
char cmd[strlen(GETTY)+FILENAME_MAX];
|
||||||
|
|
||||||
|
if (mask & IN_CREATE) {
|
||||||
|
_d("Starting %s as %s", tty->name, (strcmp(tty->name, console) == 0) ? "console" : "TTY");
|
||||||
|
snprintf(cmd, sizeof(cmd), GETTY, tty->name);
|
||||||
|
tty->pid = run_getty(cmd, strcmp(tty->name, console) == 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Kill the spawned child, and recollect it. */
|
||||||
|
if ((mask & IN_DELETE) && tty->pid) {
|
||||||
|
int status = 0;
|
||||||
|
_d("Stopping %s", tty->name);
|
||||||
|
kill(tty->pid, SIGKILL);
|
||||||
|
waitpid(tty->pid, &status, 0);
|
||||||
|
tty->pid = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void tty_start(void)
|
||||||
|
{
|
||||||
|
int fd = -1;
|
||||||
|
node_t *entry;
|
||||||
|
|
||||||
|
/* Start all TTYs that already exist in the system. */
|
||||||
|
LIST_FOREACH(entry, &node_list, link) {
|
||||||
|
chdir("/dev");
|
||||||
|
if (fexist (entry->data.name))
|
||||||
|
tty_startstop_one(&entry->data, IN_CREATE);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Start a inotify watcher to catch new TTYs that is discovered (USB for example) */
|
||||||
|
if (!fork()) {
|
||||||
|
fd = inotify_init();
|
||||||
|
|
||||||
|
prctl(PR_SET_NAME, "tty_watcher", 0, 0, 0);
|
||||||
|
if (inotify_add_watch(fd, "/dev", IN_CREATE | IN_DELETE) < 0) {
|
||||||
|
fprintf(stderr, "Failed to add watcher, errno %s\n", strerror(errno));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (1) {
|
||||||
|
int len = 0;;
|
||||||
|
char buf[EVENT_SIZE];
|
||||||
|
struct inotify_event *notified;
|
||||||
|
|
||||||
|
while ((len = read(fd, buf, EVENT_SIZE))) {
|
||||||
|
if (len == -1) {
|
||||||
|
if (errno == EAGAIN || errno == EINTR)
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
notified = (struct inotify_event *) &buf[0];
|
||||||
|
LIST_FOREACH(entry, &node_list, link) {
|
||||||
|
if (strcmp(notified->name, entry->data.name) == 0) {
|
||||||
|
tty_startstop_one(&entry->data, notified->mask);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Local Variables:
|
||||||
|
* version-control: t
|
||||||
|
* indent-tabs-mode: t
|
||||||
|
* c-file-style: "linux"
|
||||||
|
* End:
|
||||||
|
*/
|
||||||
Reference in new issue
Block a user