Date: 2026-05-01T06:12:07+00:00

Actual source code: fhost.c

      1: #define PETSC_DESIRE_FEATURE_TEST_MACROS /* for getdomainname() */
      2: /*
      3:       Code for manipulating files.
      4: */
      5: #include <petscsys.h>
      6: #if defined(PETSC_HAVE_SYS_UTSNAME_H)
      7:   #include <sys/utsname.h>
      8: #endif
      9: #if defined(PETSC_HAVE_WINDOWS_H)
     10:   #include <windows.h>
     11: #endif
     12: #if defined(PETSC_HAVE_SYS_SYSTEMINFO_H)
     13:   #include <sys/systeminfo.h>
     14: #endif
     15: #if defined(PETSC_HAVE_UNISTD_H)
     16:   #include <unistd.h>
     17: #endif
     18: #if defined(PETSC_HAVE_NETDB_H)
     19:   #include <netdb.h>
     20: #endif
     21: #include <errno.h>

     23: /*@C
     24:   PetscGetHostName - Returns the name of the host. This attempts to
     25:   return the entire Internet name. It may not return the same name
     26:   as `MPI_Get_processor_name()`.

     28:   Not Collective

     30:   Input Parameter:
     31: . nlen - length of name

     33:   Output Parameter:
     34: . name - contains host name.  Must be long enough to hold the name
     35:            This is the fully qualified name, including the domain.

     37:   Level: developer

     39:   Fortran Note:
     40: .vb
     41:   character*(128) name
     42:   call PetscGetHostName(name,ierr)
     43: .ve

     45: .seealso: `PetscGetUserName()`, `PetscGetArchType()`
     46: @*/
     47: PetscErrorCode PetscGetHostName(char name[], size_t nlen)
     48: {
     49:   char *domain = NULL;
     50: #if defined(PETSC_HAVE_UNAME) && !defined(PETSC_HAVE_GETCOMPUTERNAME)
     51:   struct utsname utname;
     52: #endif

     54:   PetscFunctionBegin;
     55: #if defined(PETSC_HAVE_GETCOMPUTERNAME)
     56:   {
     57:     size_t nnlen = nlen;

     59:     GetComputerName((LPTSTR)name, (LPDWORD)(&nnlen));
     60:   }
     61: #elif defined(PETSC_HAVE_UNAME)
     62:   // According to IEEE Std 1003.1-1988 ("POSIX.1") and 1003.1-2024, upon successful completion, uname returns
     63:   // a non-negative value. Otherwise, -1 shall be returned and errno set to indicate the error.
     64:   PetscCheck(uname(&utname) != -1, PETSC_COMM_SELF, PETSC_ERR_SYS, "uname() due to \"%s\"", strerror(errno));
     65:   PetscCall(PetscStrncpy(name, utname.nodename, nlen));
     66: #elif defined(PETSC_HAVE_GETHOSTNAME)
     67:   PetscCheck(!gethostname(name, nlen), PETSC_COMM_SELF, PETSC_ERR_SYS, "gethostname() due to \"%s\"", strerror(errno));
     68: #endif
     69:   /* if there was not enough room then system call will not null terminate name */
     70:   name[nlen - 1] = 0;

     72:   /* See if this name includes the domain */
     73:   PetscCall(PetscStrchr(name, '.', &domain));
     74:   if (!domain) {
     75:     size_t l, ll;

     77:     PetscCall(PetscStrlen(name, &l));
     78:     if (l == nlen - 1) PetscFunctionReturn(PETSC_SUCCESS);
     79:     name[l++] = '.';
     80:     name[l]   = 0;
     81: #if defined(PETSC_HAVE_GETDOMAINNAME)
     82:     PetscCheck(!getdomainname(name + l, (int)(nlen - l)), PETSC_COMM_SELF, PETSC_ERR_SYS, "getdomainname() due to \"%s\"", strerror(errno));
     83: #endif
     84:     /* check if domain name is not a dnsdomainname and nuke it */
     85:     PetscCall(PetscStrlen(name, &ll));
     86:     if (ll > 4) {
     87:       const char *suffixes[] = {".edu", ".com", ".net", ".org", ".mil", NULL};
     88:       PetscInt    index;

     90:       PetscCall(PetscStrendswithwhich(name, suffixes, &index));
     91:       if (!suffixes[index]) {
     92:         PetscCall(PetscInfo(NULL, "Rejecting domainname, likely is NIS %s\n", name));
     93:         name[l - 1] = 0;
     94:       }
     95:     }
     96:   }
     97:   PetscFunctionReturn(PETSC_SUCCESS);
     98: }