Home > database >  In which case `gethostname()` returns false?
In which case `gethostname()` returns false?

Time:11-02

Talking about https://www.php.net/gethostname

I'm writing a function that relies on the hostname.

In what scenario this method returns false?

CodePudding user response:

That is an interesting question. Because under linux, I query the hostname in the console. And it's always there. So when does an error occur? When I switch off my computer?!

The link https://man7.org/linux/man-pages/man2/gethostname.2.html from @CherryDT helps. And for me the most unlikely error would be if the hostname is too long. But how long is long?

ERRORS
       EFAULT name is an invalid address.

       EINVAL len is negative or, for sethostname(), len is larger than
              the maximum allowed size.

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

       EPERM  For sethostname(), the caller did not have the
              CAP_SYS_ADMIN capability in the user namespace associated
              with its UTS namespace (see namespaces(7)).

CodePudding user response:

Speaking from experience:

  1. domain not found
  2. bad input
  3. dns not working on the host

Actually, it looks like the function will just echo back the input with php 7.3

php7.3  -r 'print_r(gethostbyname("jwf")); '
jwf

php7.3  -r 'print_r(gethostbyname(".")); '
.

So maybe you have to go all the way back to 5.6 or even earlier to get a false from it?

  •  Tags:  
  • php
  • Related