On Fri, 24 May 2024 18:18:53 +0530
Danish Prakash <contact(a)danishpraka.sh> wrote:
When invoking pasta without any arguments,
it's difficult
to tell whether we are in the new namespace or not leaving
users a bit confused. This change modifies the host namespace
to add a prefix "pasta-" to make it a bit more obvious.
Signed-off-by: Danish Prakash <contact(a)danishpraka.sh>
---
pasta.c | 9 +++++++++
1 file changed, 9 insertions(+)
diff --git a/pasta.c b/pasta.c
index 31e1e00..90afd74 100644
--- a/pasta.c
+++ b/pasta.c
@@ -50,6 +50,8 @@
#include "netlink.h"
#include "log.h"
+#define HOSTNAME_PREFIX "pasta-"
+
/* PID of child, in case we created a namespace */
int pasta_child_pid;
@@ -178,6 +180,7 @@ struct pasta_spawn_cmd_arg {
*/
static int pasta_spawn_cmd(void *arg)
{
+ char hostname[HOST_NAME_MAX + 1] = HOSTNAME_PREFIX;
const struct pasta_spawn_cmd_arg *a;
sigset_t set;
@@ -188,6 +191,12 @@ static int pasta_spawn_cmd(void *arg)
if (write_file("/proc/sys/net/ipv4/ping_group_range", "0 0"))
warn("Cannot set ping_group_range, ICMP requests might fail");
+ if (!gethostname(hostname + sizeof(HOSTNAME_PREFIX) - 1, HOST_NAME_MAX + 1 -
sizeof(HOSTNAME_PREFIX)) ||
Following the Linux kernel coding style also means we try to stick into
80 columns where possible:
https://www.kernel.org/doc/html/latest/process/coding-style.html#breaking-l…
...so there was a reason why I proposed this line like I did, with the
line splits. These subtleties, I can also fix them up on merge.
+ errno == ENAMETOOLONG ) {
Excess whitespace between ENAMETOOLONG and ). Same here, I would fix
this up on merge.
+ if (sethostname(hostname, strlen(hostname)))
So, I mentioned before that you don't really need to set a NULL
terminating byte for sethostname() itself, because it takes a length.
But strlen() needs it. If gethostname() truncated the hostname,
according to POSIX, it's unspecified whether we'll have a NULL byte at
the end of 'hostname', and strlen() would read out-of-bounds, past the
end of 'hostname'.
That's not an issue with glibc, but if POSIX says it's not guaranteed,
we shouldn't take anything for granted.
I would suggest that you simply add a NULL byte at HOST_NAME_MAX,
unconditionally, that should cover the normal case as well as the
ENAMETOOLONG case. I haven't tested this by the way.
+ warn("Unable to set pasta-prefixed
hostname");
+ }
+
/* Wait for the parent to be ready: see main() */
sigemptyset(&set);
sigaddset(&set, SIGUSR1);
I didn't get any follow up on this patch. Did you actually mean to
abandon it?
If you prefer, I can also pick it up from there.
--
Stefano