代码之家  ›  专栏  ›  技术社区  ›  Daniel W.

在哪种情况下`gethostname()`返回false?

php
  •  -2
  • Daniel W.  · 技术社区  · 4 年前

    谈论 https://www.php.net/gethostname

    我正在编写一个依赖于主机名的函数。

    在什么情况下,此方法返回 false ?

    0 回复  |  直到 4 年前
        1
  •  2
  •   Maik Lowrey    4 年前

    这是一个有趣的问题。因为在linux下,我在控制台中查询主机名。它总是在那里。那么,什么时候会出现错误呢?当我关掉电脑的时候?!

    链接 https://man7.org/linux/man-pages/man2/gethostname.2.html 来自@CherryDT 帮助。对我来说,最不可能出现的错误是主机名太长。但是多长才是长呢?

    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)).
    
    推荐文章