Actual source code: fhost.c
petsc-3.12.5 2020-03-29
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
22: /*@C
23: PetscGetHostName - Returns the name of the host. This attempts to
24: return the entire Internet name. It may not return the same name
25: as MPI_Get_processor_name().
27: Not Collective
29: Input Parameter:
30: . nlen - length of name
32: Output Parameter:
33: . name - contains host name. Must be long enough to hold the name
34: This is the fully qualified name, including the domain.
36: Level: developer
39: Fortran Version:
40: In Fortran this routine has the format
42: $ character*(64) name
43: $ call PetscGetHostName(name,ierr)
45: .seealso: PetscGetUserName(),PetscGetArchType()
46: @*/
47: PetscErrorCode PetscGetHostName(char name[],size_t nlen)
48: {
49: char *domain;
51: #if defined(PETSC_HAVE_UNAME) && !defined(PETSC_HAVE_GETCOMPUTERNAME)
52: struct utsname utname;
53: #endif
56: #if defined(PETSC_HAVE_GETCOMPUTERNAME)
57: {
58: size_t nnlen = nlen;
59: GetComputerName((LPTSTR)name,(LPDWORD)(&nnlen));
60: }
61: #elif defined(PETSC_HAVE_UNAME)
62: uname(&utname);
63: PetscStrncpy(name,utname.nodename,nlen);
64: #elif defined(PETSC_HAVE_GETHOSTNAME)
65: gethostname(name,nlen);
66: #elif defined(PETSC_HAVE_SYSINFO_3ARG)
67: sysinfo(SI_HOSTNAME,name,nlen);
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: PetscStrchr(name,'.',&domain);
74: if (!domain) {
75: size_t l,ll;
76: PetscStrlen(name,&l);
77: if (l == nlen-1) return(0);
78: name[l++] = '.';
79: name[l] = 0;
80: #if defined(PETSC_HAVE_SYSINFO_3ARG)
81: sysinfo(SI_SRPC_DOMAIN,name+l,nlen-l);
82: #elif defined(PETSC_HAVE_GETDOMAINNAME)
83: if (getdomainname(name+l,nlen - l)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"getdomainname()");
84: #endif
85: /* check if domain name is not a dnsdomainname and nuke it */
86: PetscStrlen(name,&ll);
87: if (ll > 4) {
88: const char *suffixes[] = {".edu",".com",".net",".org",".mil",0};
89: PetscInt index;
90: PetscStrendswithwhich(name,suffixes,&index);
91: if (!suffixes[index]) {
92: PetscInfo1(0,"Rejecting domainname, likely is NIS %s\n",name);
93: name[l-1] = 0;
94: }
95: }
96: }
97: return(0);
98: }