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