public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
* [PATCH] pasta.c: modify hostname when detaching new namespace
@ 2024-05-20  8:35 Danish Prakash
       [not found] ` <20240521193400.4f1b15c5@elisabeth>
  2024-05-24 12:48 ` Danish Prakash
  0 siblings, 2 replies; 10+ messages in thread
From: Danish Prakash @ 2024-05-20  8:35 UTC (permalink / raw)
  To: sbrivio, passt-dev; +Cc: Danish Prakash

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@danishpraka.sh>
---
 pasta.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/pasta.c b/pasta.c
index 31e1e00..840a2b1 100644
--- a/pasta.c
+++ b/pasta.c
@@ -180,6 +180,8 @@ static int pasta_spawn_cmd(void *arg)
 {
 	const struct pasta_spawn_cmd_arg *a;
 	sigset_t set;
+	char hostname[HOST_NAME_MAX+1], pasta_hostname[HOST_NAME_MAX+1];
+	char *hostname_prefix = "pasta-";
 
 	/* We run in a detached PID and mount namespace: mount /proc over */
 	if (mount("", "/proc", "proc", 0, NULL))
@@ -188,6 +190,17 @@ 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, HOST_NAME_MAX+1) == 0) {
+		if ((strlen(hostname) + strlen(hostname_prefix)) > HOST_NAME_MAX) {
+			hostname[strlen(hostname)-strlen(hostname_prefix)] = '\0';
+		}
+		sprintf(pasta_hostname, "%s%s", hostname_prefix, hostname);
+
+		if (sethostname(pasta_hostname, strlen(pasta_hostname)) != 0) {
+			warn("Unable to set pasta-prefixed hostname");
+		}
+	}
+
 	/* Wait for the parent to be ready: see main() */
 	sigemptyset(&set);
 	sigaddset(&set, SIGUSR1);
-- 
@@ -180,6 +180,8 @@ static int pasta_spawn_cmd(void *arg)
 {
 	const struct pasta_spawn_cmd_arg *a;
 	sigset_t set;
+	char hostname[HOST_NAME_MAX+1], pasta_hostname[HOST_NAME_MAX+1];
+	char *hostname_prefix = "pasta-";
 
 	/* We run in a detached PID and mount namespace: mount /proc over */
 	if (mount("", "/proc", "proc", 0, NULL))
@@ -188,6 +190,17 @@ 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, HOST_NAME_MAX+1) == 0) {
+		if ((strlen(hostname) + strlen(hostname_prefix)) > HOST_NAME_MAX) {
+			hostname[strlen(hostname)-strlen(hostname_prefix)] = '\0';
+		}
+		sprintf(pasta_hostname, "%s%s", hostname_prefix, hostname);
+
+		if (sethostname(pasta_hostname, strlen(pasta_hostname)) != 0) {
+			warn("Unable to set pasta-prefixed hostname");
+		}
+	}
+
 	/* Wait for the parent to be ready: see main() */
 	sigemptyset(&set);
 	sigaddset(&set, SIGUSR1);
-- 
2.45.1



^ permalink raw reply related	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2024-07-29 17:55 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-05-20  8:35 [PATCH] pasta.c: modify hostname when detaching new namespace Danish Prakash
     [not found] ` <20240521193400.4f1b15c5@elisabeth>
2024-05-23 13:52   ` Danish Prakash
2024-05-23 14:23     ` Stefano Brivio
2024-05-24 12:45       ` Danish Prakash
2024-05-24 17:38         ` Stefano Brivio
2024-05-24 12:48 ` Danish Prakash
2024-05-24 17:39   ` Stefano Brivio
2024-06-25 22:27     ` Stefano Brivio
2024-07-29 13:56     ` Danish Prakash
2024-07-29 17:54       ` Stefano Brivio

Code repositories for project(s) associated with this public inbox

	https://passt.top/passt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for IMAP folder(s).