getty-generator: use the new main function definer

I changed the nulstr loop to a normal FOREACH_STRING loop. It seems clearer
this way.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2018-12-04 11:53:48 +01:00
parent ec6e959750
commit 9d22f97b87

View file

@ -13,13 +13,13 @@
#include "mkdir.h" #include "mkdir.h"
#include "path-util.h" #include "path-util.h"
#include "process-util.h" #include "process-util.h"
#include "string-util.h" #include "strv.h"
#include "terminal-util.h" #include "terminal-util.h"
#include "unit-name.h" #include "unit-name.h"
#include "util.h" #include "util.h"
#include "virt.h" #include "virt.h"
static const char *arg_dest = "/tmp"; static const char *arg_dest = NULL;
static int add_symlink(const char *fservice, const char *tservice) { static int add_symlink(const char *fservice, const char *tservice) {
char *from, *to; char *from, *to;
@ -99,37 +99,21 @@ static int verify_tty(const char *name) {
return 0; return 0;
} }
int main(int argc, char *argv[]) { static int run(const char *dest, const char *dest_early, const char *dest_late) {
static const char virtualization_consoles[] =
"hvc0\0"
"xvc0\0"
"hvsi0\0"
"sclp_line0\0"
"ttysclp0\0"
"3270!tty1\0";
_cleanup_free_ char *active = NULL; _cleanup_free_ char *active = NULL;
const char *j; const char *j;
int r; int r;
log_setup_generator(); assert_se(arg_dest = dest);
if (argc > 1 && argc != 4) {
log_error("This program takes three or no arguments.");
return EXIT_FAILURE;
}
if (argc > 1)
arg_dest = argv[1];
if (detect_container() > 0) { if (detect_container() > 0) {
_cleanup_free_ char *container_ttys = NULL; _cleanup_free_ char *container_ttys = NULL;
log_debug("Automatically adding console shell."); log_debug("Automatically adding console shell.");
if (add_symlink("console-getty.service", "console-getty.service") < 0) r = add_symlink("console-getty.service", "console-getty.service");
return EXIT_FAILURE; if (r < 0)
return r;
/* When $container_ttys is set for PID 1, spawn /* When $container_ttys is set for PID 1, spawn
* gettys on all ptys named therein. Note that despite * gettys on all ptys named therein. Note that despite
@ -157,13 +141,14 @@ int main(int argc, char *argv[]) {
if (!t) if (!t)
continue; continue;
if (add_container_getty(t) < 0) r = add_container_getty(t);
return EXIT_FAILURE; if (r < 0)
return r;
} }
} }
/* Don't add any further magic if we are in a container */ /* Don't add any further magic if we are in a container */
return EXIT_SUCCESS; return 0;
} }
if (read_one_line_file("/sys/class/tty/console/active", &active) >= 0) { if (read_one_line_file("/sys/class/tty/console/active", &active) >= 0) {
@ -176,10 +161,8 @@ int main(int argc, char *argv[]) {
_cleanup_free_ char *tty = NULL; _cleanup_free_ char *tty = NULL;
tty = strndup(word, l); tty = strndup(word, l);
if (!tty) { if (!tty)
log_oom(); return log_oom();
return EXIT_FAILURE;
}
/* We assume that gettys on virtual terminals are /* We assume that gettys on virtual terminals are
* started via manual configuration and do this magic * started via manual configuration and do this magic
@ -191,23 +174,33 @@ int main(int argc, char *argv[]) {
if (verify_tty(tty) < 0) if (verify_tty(tty) < 0)
continue; continue;
if (add_serial_getty(tty) < 0) r = add_serial_getty(tty);
return EXIT_FAILURE; if (r < 0)
return r;
} }
} }
/* Automatically add in a serial getty on the first /* Automatically add in a serial getty on the first
* virtualizer console */ * virtualizer console */
NULSTR_FOREACH(j, virtualization_consoles) { FOREACH_STRING(j,
char *p; "hvc0",
"xvc0",
"hvsi0",
"sclp_line0",
"ttysclp0",
"3270!tty1") {
const char *p;
p = strjoina("/sys/class/tty/", j); p = strjoina("/sys/class/tty/", j);
if (access(p, F_OK) < 0) if (access(p, F_OK) < 0)
continue; continue;
if (add_serial_getty(j) < 0) r = add_serial_getty(j);
return EXIT_FAILURE; if (r < 0)
return r;
} }
return EXIT_SUCCESS; return 0;
} }
DEFINE_MAIN_GENERATOR_FUNCTION(run);