Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
// step init vz-init
// /proc /sys /dev mount mount
// /urunc-epoch set the clock set the clock
// /urunc-hostname set the host name set the host name
// /urunc-rosetta register Rosetta register Rosetta
// root root= of the cmdline, or virtiofs tag rootfs
// /dev/vda (ext4) when hull
Expand Down Expand Up @@ -657,6 +658,27 @@ static void init_set_clock(const struct init_ctx *c)
init_log(c, "could not set the clock: %s", strerror(errno));
}

// Sets the kernel's host name from /urunc-hostname, up to the first blank.
// The kernel takes it from ip= on a static network only; with ip=dhcp or no
// network it is the address or "(none)". A name the kernel refuses does not
// stop the boot.
static void init_set_hostname(const struct init_ctx *c)
{
size_t len;
char *buf = init_slurp(c, "/urunc-hostname", &len);
size_t n = 0;

if (buf == NULL)
return;
while (n < len && buf[n] != '\n' && buf[n] != ' ' && buf[n] != '\t')
n++;
if (n == 0 || n > 64)
init_log(c, "ignoring a malformed /urunc-hostname");
else if (c->ops->set_hostname(buf, n) != 0)
init_log(c, "could not set the host name: %s", strerror(errno));
free(buf);
}

static int init_rosetta(const struct init_ctx *c)
{
const struct sysops *ops = c->ops;
Expand Down Expand Up @@ -1233,6 +1255,7 @@ int init_run(const struct sysops *ops, enum urunit_mode mode, int argc,

// The clock comes first, before anything checks a certificate.
init_set_clock(&c);
init_set_hostname(&c);
if (init_rosetta(&c))
return 1;

Expand Down
6 changes: 6 additions & 0 deletions src/sysops.c
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,11 @@ static int sysops_set_clock(time_t epoch)
return clock_settime(CLOCK_REALTIME, &ts);
}

static int sysops_set_hostname(const char *name, size_t len)
{
return sethostname(name, len);
}

static int sysops_setenv(const char *key, const char *val)
{
return setenv(key, val, 1);
Expand Down Expand Up @@ -355,6 +360,7 @@ const struct sysops sysops_real = {
.write_file = sysops_write_file,
.symlink = sysops_symlink,
.set_clock = sysops_set_clock,
.set_hostname = sysops_set_hostname,
.setenv = sysops_setenv,
.access_x = sysops_access_x,
.exists = sysops_exists,
Expand Down
1 change: 1 addition & 0 deletions src/sysops.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ struct sysops {
int (*write_file)(const char *path, const void *buf, size_t len);
int (*symlink)(const char *target, const char *path);
int (*set_clock)(time_t epoch);
int (*set_hostname)(const char *name, size_t len);
int (*setenv)(const char *key, const char *val);
// Returns 0 when path exists and is executable.
int (*access_x)(const char *path);
Expand Down
45 changes: 45 additions & 0 deletions src/tests/test_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,11 @@ static int fake_set_clock(time_t epoch)
return fake_record("set_clock %lld", (long long)epoch);
}

static int fake_set_hostname(const char *name, size_t len)
{
return fake_record("set_hostname %.*s", (int)len, name);
}

static int fake_setenv(const char *key, const char *val)
{
return fake_record("setenv %s %s", key, val);
Expand Down Expand Up @@ -277,6 +282,7 @@ static const struct sysops fake_ops = {
.write_file = fake_write_file,
.symlink = fake_symlink,
.set_clock = fake_set_clock,
.set_hostname = fake_set_hostname,
.setenv = fake_setenv,
.access_x = fake_access_x,
.exists = fake_exists,
Expand Down Expand Up @@ -505,6 +511,7 @@ static void test_init_hull_block(void)
CHECK_EQ_INT(rc, 0);
CHECK_CALLS(EARLY_MOUNTS,
"set_clock 1760000000",
"set_hostname box",
"mkdir_p /mnt/rosetta 755",
"mkdir_p /proc/sys/fs/binfmt_misc 755",
"mount rosetta /mnt/rosetta virtiofs 0 -",
Expand Down Expand Up @@ -701,6 +708,7 @@ static void test_vz_full(void)
CHECK_EQ_INT(rc, 0);
CHECK_CALLS(EARLY_MOUNTS,
"set_clock 1760000000",
"set_hostname box",
"mkdir_p /mnt/rosetta 755",
"mkdir_p /proc/sys/fs/binfmt_misc 755",
"mount rosetta /mnt/rosetta virtiofs 0 -",
Expand Down Expand Up @@ -963,6 +971,42 @@ static void test_default_hosts(void)
free_argv(out);
}

// /urunc-hostname sets the kernel's host name up to the first blank, and a
// name the kernel would refuse is logged and skipped.
static void test_set_hostname(void)
{
char *argv[] = { "/vz-init", "/bin/true", NULL };
char **out = NULL;
int out_argc = 0;

fake_reset();
fake_add("/urunc-rootfs-direct", "", 0);
fake_add("/urunc-hostname", "claude-tassos-1a2b3c4d extra\n", 0);
CHECK_EQ_INT(init_run(&fake_ops, URUNIT_MODE_VZ_INIT, 2, argv, NULL,
&out_argc, &out), 0);
CHECK(strstr(fake_calls, "set_hostname claude-tassos-1a2b3c4d\n") !=
NULL);
free_argv(out);

fake_reset();
fake_add("/urunc-rootfs-direct", "", 0);
fake_add("/urunc-hostname",
"a123456789b123456789c123456789d123456789"
"e123456789f123456789g12345\n", 0);
CHECK_EQ_INT(init_run(&fake_ops, URUNIT_MODE_VZ_INIT, 2, argv, NULL,
&out_argc, &out), 0);
CHECK(strstr(fake_calls, "set_hostname") == NULL);
CHECK(strstr(fake_console, "malformed /urunc-hostname") != NULL);
free_argv(out);

fake_reset();
fake_add("/urunc-rootfs-direct", "", 0);
CHECK_EQ_INT(init_run(&fake_ops, URUNIT_MODE_VZ_INIT, 2, argv, NULL,
&out_argc, &out), 0);
CHECK(strstr(fake_calls, "set_hostname") == NULL);
free_argv(out);
}

// The markers hull stages for an overlay-block root.
static void test_overlay_markers(void)
{
Expand Down Expand Up @@ -1351,6 +1395,7 @@ int run_init_tests(void)
test_init_hull_block_no_root();
test_vz_direct();
test_default_hosts();
test_set_hostname();
test_overlay_markers();
test_dev_by_serial();
test_vz_overlay_block();
Expand Down