Saturday, September 6, 2014

Unix Prog: System Identification

1. uname

uname function can return the information on current host and operating system.
Given the address of struct utsname, the function will fill the structure.

Definition:
 ubuntu@ip-172-31-23-227:~$ less /usr/include/x86_64-linux-gnu/sys/utsname.h  
 ......  
 /* Structure describing the system and machine. */  
 struct utsname  
  {  
   /* Name of the implementation of the operating system. */  
   char sysname[_UTSNAME_SYSNAME_LENGTH];  
   
   /* Name of this node on the network. */  
   char nodename[_UTSNAME_NODENAME_LENGTH];  
   
   /* Current release level of this implementation. */  
   char release[_UTSNAME_RELEASE_LENGTH];  
   /* Current version level of this release. */  
   char version[_UTSNAME_VERSION_LENGTH];  
   
   /* Name of the hardware type the system is running on. */  
   char machine[_UTSNAME_MACHINE_LENGTH];  
   
 #if _UTSNAME_DOMAIN_LENGTH - 0  
   /* Name of the domain of this node on the network. */  
 # ifdef __USE_GNU  
   char domainname[_UTSNAME_DOMAIN_LENGTH];  
 # else  
   char __domainname[_UTSNAME_DOMAIN_LENGTH];  
 # endif  
 #endif  
  };  
   
 #ifdef __USE_SVID  
 /* Note that SVID assumes all members have the same size. */  
 # define SYS_NMLN _UTSNAME_LENGTH  
 #endif  
   
   
 /* Put information about the system in NAME. */  
 extern int uname (struct utsname *__name) __THROW;  
 ......  

2. uname example:
fileio.c:
 #include<stdio.h>  
 #include<stdlib.h>  
 #include<sys/utsname.h>  
   
 int main(int argc, char* argv[])  
 {  
  struct utsname uts;  
  if(uname(&uts) < 0) {  
   printf("uname error!\n");  
   exit(1);  
  }  
   
  printf("sysname: %s\n", uts.sysname);  
  printf("nodename: %s\n", uts.nodename);  
  printf("release: %s\n", uts.release);  
  printf("version: %s\n", uts.version);  
  printf("machine: %s\n", uts.machine);  
   
  #ifdef __USE_GNU  
  printf("domain name: %s\n", uts.domainname);  
  #else  
  printf("domain name: %s\n", uts.__domainname);  
  #endif  
   
  exit(0);  
 }  

shell:
1) Run the program to print out current system's information
2) Run the "uname" command, which will call "uname" system call inside.
 ubuntu@ip-172-31-23-227:~$ ./io.out  
 sysname: Linux  
 nodename: ip-172-31-23-227  
 release: 3.13.0-29-generic  
 version: #53-Ubuntu SMP Wed Jun 4 21:00:20 UTC 2014  
 machine: x86_64  
 domain name: (none)  
 ubuntu@ip-172-31-23-227:~$ uname
 Linux

3. gethostname

definition:
 ubuntu@ip-172-31-23-227:~$ less /usr/include/unistd.h  
 ......  
 #if defined __USE_BSD || defined __USE_UNIX98 || defined __USE_XOPEN2K  
 /* Put the name of the current host in no more than LEN bytes of NAME.  
   The result is null-terminated if LEN is large enough for the full  
   name and the terminator. */  
 extern int gethostname (char *__name, size_t __len) __THROW __nonnull ((1));  
 #endif  
 ......  

example:
fileio.c:
 #include<stdio.h>  
 #include<stdlib.h>  
 #include<unistd.h>  
   
 #define HOST_NAME_MAX 64  
   
 int main(int argc, char* argv[])  
 {  
  char hostname[64];  
   
  if(gethostname(hostname, HOST_NAME_MAX) < 0) {  
   printf("gethostname error!\n");  
   exit(1);  
  }  
   
  printf("hostname: %s\n", hostname);  
   
  exit(0);  
 }  

shell:
1) Run the program to get current host name which is the node name in struct utsname
2) Run the command "hostname" which call "gethostname" system call.
 ubuntu@ip-172-31-23-227:~$ ./io.out  
 hostname: ip-172-31-23-227  
 ubuntu@ip-172-31-23-227:~$ hostname  
 ip-172-31-23-227  

No comments:

Post a Comment