kobrien
kobrien

Reputation: 3159

Maximum hostname length on Linux

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?

Upvotes: 4

Views: 6139

Answers (1)

alk
alk

Reputation: 70951

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.)

Upvotes: 3

Related Questions