* [PATCH v2] pasta.c: modify hostname when detaching new namespace
[not found] <20240520083343.10973-1-contact@danishpraka.sh>
@ 2024-07-30 6:01 ` Danish Prakash
2024-07-30 17:09 ` Stefano Brivio
0 siblings, 1 reply; 2+ messages in thread
From: Danish Prakash @ 2024-07-30 6:01 UTC (permalink / raw)
To: passt-dev, sbrivio; +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 | 10 ++++++++++
1 file changed, 10 insertions(+)
diff --git a/pasta.c b/pasta.c
index 31e1e000ee1b..7ce502d3f299 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,13 @@ 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)) || errno == ENAMETOOLONG) {
+ hostname[HOST_NAME_MAX] = '\0';
+ if (sethostname(hostname, strlen(hostname)))
+ warn("Unable to set pasta-prefixed hostname");
+ }
+
/* Wait for the parent to be ready: see main() */
sigemptyset(&set);
sigaddset(&set, SIGUSR1);
--
@@ -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,13 @@ 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)) || errno == ENAMETOOLONG) {
+ hostname[HOST_NAME_MAX] = '\0';
+ if (sethostname(hostname, strlen(hostname)))
+ warn("Unable to set pasta-prefixed hostname");
+ }
+
/* Wait for the parent to be ready: see main() */
sigemptyset(&set);
sigaddset(&set, SIGUSR1);
--
2.45.2
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH v2] pasta.c: modify hostname when detaching new namespace
2024-07-30 6:01 ` [PATCH v2] pasta.c: modify hostname when detaching new namespace Danish Prakash
@ 2024-07-30 17:09 ` Stefano Brivio
0 siblings, 0 replies; 2+ messages in thread
From: Stefano Brivio @ 2024-07-30 17:09 UTC (permalink / raw)
To: Danish Prakash; +Cc: passt-dev
On Tue, 30 Jul 2024 11:31:23 +0530
Danish Prakash <contact@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@danishpraka.sh>
Thanks for following up with this! I just applied it with some coding
style fixes:
> ---
> pasta.c | 10 ++++++++++
> 1 file changed, 10 insertions(+)
>
> diff --git a/pasta.c b/pasta.c
> index 31e1e000ee1b..7ce502d3f299 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,13 @@ 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 -
This exceeds 80 columns.
> + sizeof(HOSTNAME_PREFIX)) || errno == ENAMETOOLONG) {
This is indented with spaces instead of tabs, and "errno ==
ENAMETOOLONG" is not aligned with !gethostname(), despite logically
being at the same level.
See also my previous emails about this:
https://archives.passt.top/passt-dev/20240523162347.44e6faf3@elisabeth/
https://archives.passt.top/passt-dev/20240524193918.151fd2fc@elisabeth/
> + hostname[HOST_NAME_MAX] = '\0';
And this should be aligned with the rest of the clause body, that is,
indented one level on top of the conditional. With (two) tabs, not
spaces.
> + if (sethostname(hostname, strlen(hostname)))
> + warn("Unable to set pasta-prefixed hostname");
> + }
> +
> /* Wait for the parent to be ready: see main() */
> sigemptyset(&set);
> sigaddset(&set, SIGUSR1);
--
Stefano
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2024-07-30 17:10 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
[not found] <20240520083343.10973-1-contact@danishpraka.sh>
2024-07-30 6:01 ` [PATCH v2] pasta.c: modify hostname when detaching new namespace Danish Prakash
2024-07-30 17:09 ` 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).