Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rc: use /etc/hostname if available #585

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
19 changes: 19 additions & 0 deletions src/openrc/rc.c
Original file line number Diff line number Diff line change
Expand Up @@ -473,12 +473,31 @@ handle_signal(int sig)
errno = serrno;
}

static void
do_early_hostname(void)
{
/* Set hostname if available */
char *buffer = NULL;
size_t len;

if (rc_getfile(RC_SYSCONFDIR "/hostname", &buffer, &len)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The hostname tool from net-tools on Linux has a -b flag that will fall back to setting the hostname to localhost if the file does not exist or is empty; this would probably be a good idea to implement, as on Linux, the hostname could be an empty string otherwise. hostname also allows for comment lines (line starts with #) in /etc/hostname, which are ignored; this could also be useful.

if (buffer[len - 2] == '\n')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't handle the case where len is 0 or 1, resulting in an out of bounds read and potentially an out of bounds write (!!!)

buffer[--len - 1] = '\0';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please split this into 2 lines; it'd be much more readable that way.

if (sethostname(buffer, len)) {
/* ignore */;
}
free(buffer);
}
}

static void
do_sysinit(void)
{
struct utsname uts;
const char *sys;

do_early_hostname();

/* exec init-early.sh if it exists
* This should just setup the console to use the correct
* font. Maybe it should setup the keyboard too? */
Expand Down
Loading