xref: /petsc/src/sys/utils/fhost.c (revision 0b4b7b1c20c2ed4ade67e3d50a7710fe0ffbfca5)
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>
22 
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()`.
27 
28   Not Collective
29 
30   Input Parameter:
31 . nlen - length of name
32 
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.
36 
37   Level: developer
38 
39   Fortran Notes:
40   This routine has the format
41 .vb
42        character*(64) name
43        call PetscGetHostName(name,ierr)
44 .ve
45 
46 .seealso: `PetscGetUserName()`, `PetscGetArchType()`
47 @*/
48 PetscErrorCode PetscGetHostName(char name[], size_t nlen)
49 {
50   char *domain = NULL;
51 #if defined(PETSC_HAVE_UNAME) && !defined(PETSC_HAVE_GETCOMPUTERNAME)
52   struct utsname utname;
53 #endif
54 
55   PetscFunctionBegin;
56 #if defined(PETSC_HAVE_GETCOMPUTERNAME)
57   {
58     size_t nnlen = nlen;
59 
60     GetComputerName((LPTSTR)name, (LPDWORD)(&nnlen));
61   }
62 #elif defined(PETSC_HAVE_UNAME)
63   PetscCheck(!uname(&utname), PETSC_COMM_SELF, PETSC_ERR_SYS, "uname() due to \"%s\"", strerror(errno));
64   PetscCall(PetscStrncpy(name, utname.nodename, nlen));
65 #elif defined(PETSC_HAVE_GETHOSTNAME)
66   PetscCheck(!gethostname(name, nlen), PETSC_COMM_SELF, PETSC_ERR_SYS, "gethostname() due to \"%s\"", strerror(errno));
67 #endif
68   /* if there was not enough room then system call will not null terminate name */
69   name[nlen - 1] = 0;
70 
71   /* See if this name includes the domain */
72   PetscCall(PetscStrchr(name, '.', &domain));
73   if (!domain) {
74     size_t l, ll;
75 
76     PetscCall(PetscStrlen(name, &l));
77     if (l == nlen - 1) PetscFunctionReturn(PETSC_SUCCESS);
78     name[l++] = '.';
79     name[l]   = 0;
80 #if defined(PETSC_HAVE_GETDOMAINNAME)
81     PetscCheck(!getdomainname(name + l, (int)(nlen - l)), PETSC_COMM_SELF, PETSC_ERR_SYS, "getdomainname() due to \"%s\"", strerror(errno));
82 #endif
83     /* check if domain name is not a dnsdomainname and nuke it */
84     PetscCall(PetscStrlen(name, &ll));
85     if (ll > 4) {
86       const char *suffixes[] = {".edu", ".com", ".net", ".org", ".mil", NULL};
87       PetscInt    index;
88 
89       PetscCall(PetscStrendswithwhich(name, suffixes, &index));
90       if (!suffixes[index]) {
91         PetscCall(PetscInfo(NULL, "Rejecting domainname, likely is NIS %s\n", name));
92         name[l - 1] = 0;
93       }
94     }
95   }
96   PetscFunctionReturn(PETSC_SUCCESS);
97 }
98