Maximum hostname length on Linux

淺唱寂寞╮ 提交于 2019-12-22 09:11:27

问题


I am gathering the hostnames of all nodes in an mpi application. I'm using gethostname and I want to ensure I have enough space to store the resulting string. I'm specifically avoiding MPI's getprocessorname as I want the machine's name and I don't want to have to parse it from that.

There appear to be two options,

bits/local_lim.h:#define HOST_NAME_MAX      64
bits/posix1_lim.h:#define _POSIX_HOST_NAME_MAX  255 

What are the advantages of each? Should I include one of these headers directly or is there a posix flag I should use before some other include?


回答1:


To stay portable you should defintitly stick to HOST_NAME_MAX.

From the Linux man-page for gethostname() (quoting the POSIX spex):

SUSv2 guarantees that "Host names are limited to 255 bytes". POSIX.1-2001 guarantees that "Host names (not including the terminating null byte) are limited to HOST_NAME_MAX bytes". On Linux, HOST_NAME_MAX is defined with the value 64, which has been the limit since Linux 1.0 (earlier kernels imposed a limit of 8 bytes).


Please read here for the current POSIX specification, which also mentions HOST_NAME_MAX as the limit. (This is different from earlier version of POSIX where 255 was defined as limit.)



来源:https://stackoverflow.com/questions/18851234/maximum-hostname-length-on-linux

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!