public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
From: Stefano Brivio <sbrivio@redhat.com>
To: Danish Prakash <contact@danishpraka.sh>
Cc: passt-dev@passt.top
Subject: Re: [PATCH] pasta.c: modify hostname when detaching new namespace
Date: Thu, 23 May 2024 16:23:47 +0200	[thread overview]
Message-ID: <20240523162347.44e6faf3@elisabeth> (raw)
In-Reply-To: <CAA5kkEAgGgGyD0yFuTAEcmCURFygsebAgSba-WBxxStizUkNcg@mail.gmail.com>

Danish, it would be easier if you answered inline. If Gmail is making
it hard, perhaps switch to an email client (I use claws-mail)? Anyway,
it's not a big issue for the moment:

On Thu, 23 May 2024 19:22:16 +0530
Danish Prakash <contact@danishpraka.sh> wrote:

> Thanks for the review and the links.
> 
> > +       if (!gethostname(hostname + sizeof(HOSTNAME_PREFIX) - 1, HOST_NAME_MAX + 1 - sizeof(HOSTNAME_PREFIX))) {
> > +               if (sethostname(hostname, strlen(hostname)))
> > +                       debug("Unable to set pasta-prefixed hostname");
> >         }  
> 
> The above snippet, although it looks correct,

Wait, I didn't suggest if (!gethostname(...)), I suggested
gethostname(...). The ! there is yours. :)

> doesn't work in cases where the hostname is long enough (~>58 chars).
> It works fine for shorter hostnames.

In any case, it depends on how you define "doesn't work". What should
we do if the original hostname is long enough that we can't prefix
"pasta-" while fitting in 63 characters?

Append it anyway and truncate the original hostname (what my line did),
or leave it like it is (what your snippet does, I guess)? It's a matter
of taste I'd say.

> The call to `gethostname()` sets errno to 36
> (ENAMETOOLONG). Upon looking further, it seems that even though the
> manpage for `gethostname(char *name, size_t len)` states..
> 
> >  If the null-terminated hostname is too large to fit, then the name is truncated, and no error is returned (but see NOTES below).

...then the NOTES section disappeared and this ended up in "ERRORS", in
the Linux man-pages:

       ENAMETOOLONG
              (glibc gethostname()) len is smaller than the actual size.
              (Before glibc 2.1, glibc uses EINVAL for this case.)

> ...It would still throw ENAMETOOLONG if the value of len is smaller
> than `strlen(hostname)+1` (referring to the snippet below). I'm not
> sure if I'm missing out on an edge case while calculating the
> boundaries here because the `memcpy` call is certainly truncating the
> hostname[1] before ending up returning ENAMETOOLONG, which seems
> conflicting[1]:

...no, I don't think you're missing out on any edge case, you simply
missed that part of the man page. Note that we need to play nicely with
other C libraries too (especially musl), so error or not, we should do
the right/same thing.

Perhaps most robust approach:

	if (!gethostname(hostname + sizeof(HOSTNAME_PREFIX) - 1,
			 HOST_NAME_MAX + 1 - sizeof(HOSTNAME_PREFIX)) ||
	    errno == ENAMETOOLONG) {

so that if it's glibc, and it truncates, we'll just go ahead with our
truncated name, but not if there's any other error.

Note that you don't strictly need the NULL byte at the end, see
sethostname(2) -- just make sure you don't print it or anything like
that.

-- 
Stefano


  reply	other threads:[~2024-05-23 14:24 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240523162347.44e6faf3@elisabeth \
    --to=sbrivio@redhat.com \
    --cc=contact@danishpraka.sh \
    --cc=passt-dev@passt.top \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).