xref: /petsc/src/sys/utils/fhost.c (revision 074cc835faa3d6c800494dd2ea2bd8f95d70c354)
1 
2 /*
3       Code for manipulating files.
4 */
5 #include <petscsys.h>
6 #if defined(PETSC_HAVE_STDLIB_H)
7 #include <stdlib.h>
8 #endif
9 #if defined(PETSC_HAVE_SYS_UTSNAME_H)
10 #include <sys/utsname.h>
11 #endif
12 #if defined(PETSC_HAVE_WINDOWS_H)
13 #include <windows.h>
14 #endif
15 #if defined(PETSC_HAVE_SYS_SYSTEMINFO_H)
16 #include <sys/systeminfo.h>
17 #endif
18 #if defined(PETSC_HAVE_UNISTD_H)
19 #include <unistd.h>
20 #endif
21 #if defined(PETSC_HAVE_NETDB_H)
22 #include <netdb.h>
23 #endif
24 
25 #undef __FUNCT__
26 #define __FUNCT__ "PetscGetHostName"
27 /*@C
28     PetscGetHostName - Returns the name of the host. This attempts to
29     return the entire Internet name. It may not return the same name
30     as MPI_Get_processor_name().
31 
32     Not Collective
33 
34     Input Parameter:
35 .   nlen - length of name
36 
37     Output Parameter:
38 .   name - contains host name.  Must be long enough to hold the name
39            This is the fully qualified name, including the domain.
40 
41     Level: developer
42 
43     Concepts: machine name
44     Concepts: host name
45 
46 .seealso: PetscGetUserName()
47 @*/
48 PetscErrorCode  PetscGetHostName(char name[],size_t nlen)
49 {
50   char           *domain;
51   PetscErrorCode ierr;
52 #if defined(PETSC_HAVE_UNAME) && !defined(PETSC_HAVE_GETCOMPUTERNAME)
53   struct utsname utname;
54 #endif
55 
56   PetscFunctionBegin;
57 #if defined(PETSC_HAVE_GETCOMPUTERNAME)
58  {
59     size_t nnlen = nlen;
60     GetComputerName((LPTSTR)name,(LPDWORD)(&nnlen));
61  }
62 #elif defined(PETSC_HAVE_UNAME)
63   uname(&utname);
64   ierr = PetscStrncpy(name,utname.nodename,nlen);CHKERRQ(ierr);
65 #elif defined(PETSC_HAVE_GETHOSTNAME)
66   gethostname(name,nlen);
67 #elif defined(PETSC_HAVE_SYSINFO_3ARG)
68   sysinfo(SI_HOSTNAME,name,nlen);
69 #endif
70   /* if there was not enough room then system call will not null terminate name */
71   name[nlen-1] = 0;
72 
73   /* See if this name includes the domain */
74   ierr = PetscStrchr(name,'.',&domain);CHKERRQ(ierr);
75   if (!domain) {
76     size_t  l,ll;
77     ierr = PetscStrlen(name,&l);CHKERRQ(ierr);
78     if (l == nlen-1) PetscFunctionReturn(0);
79     name[l++] = '.';
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     ierr = PetscStrlen(name,&ll);CHKERRQ(ierr);
87     if (ll > 4) {
88       const char *suffixes[] = {".edu",".com",".net",".org",".mil",0};
89       PetscInt   index;
90       ierr = PetscStrendswithwhich(name,suffixes,&index);CHKERRQ(ierr);
91       if (!suffixes[index]) {
92         ierr = PetscInfo1(0,"Rejecting domainname, likely is NIS %s\n",name);CHKERRQ(ierr);
93         name[l-1] = 0;
94       }
95     }
96   }
97   PetscFunctionReturn(0);
98 }
99