Fawkes API Fawkes Development Version
|
00001 00002 /*************************************************************************** 00003 * hostinfo.h - hostname utilities 00004 * 00005 * Created: Fri Jan 12 16:12:09 2007 00006 * Copyright 2007 Tim Niemueller [www.niemueller.de] 00007 * 00008 ****************************************************************************/ 00009 00010 /* This program is free software; you can redistribute it and/or modify 00011 * it under the terms of the GNU General Public License as published by 00012 * the Free Software Foundation; either version 2 of the License, or 00013 * (at your option) any later version. A runtime exception applies to 00014 * this software (see LICENSE.GPL_WRE file mentioned below for details). 00015 * 00016 * This program is distributed in the hope that it will be useful, 00017 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00019 * GNU Library General Public License for more details. 00020 * 00021 * Read the full text in the LICENSE.GPL_WRE file in the doc directory. 00022 */ 00023 00024 #include <utils/system/hostinfo.h> 00025 00026 #include <core/exceptions/software.h> 00027 00028 #include <sys/utsname.h> 00029 #include <cstring> 00030 #include <cstdlib> 00031 00032 namespace fawkes { 00033 00034 /** @class HostInfo utils/system/hostinfo.h 00035 * Host information. 00036 * This class provides access to basic system information like hostname, 00037 * domain name, architecture and system information. It's basically a 00038 * C++ wrapper to the uname system call. 00039 * @author Tim Niemueller 00040 */ 00041 00042 /** Constructor. */ 00043 HostInfo::HostInfo() 00044 { 00045 utsname = (struct ::utsname *)malloc(sizeof(struct ::utsname)); 00046 00047 if ( uname(utsname) != 0 ) { 00048 delete utsname; 00049 utsname = NULL; 00050 throw NullPointerException("Could not call uname"); 00051 } 00052 00053 short__name = NULL; 00054 domain_name = NULL; 00055 00056 update(); 00057 } 00058 00059 00060 /** Destructor. */ 00061 HostInfo::~HostInfo() 00062 { 00063 free(utsname); 00064 free(short__name); 00065 free(domain_name); 00066 } 00067 00068 00069 /** Update information. 00070 * Gathers the information again. 00071 */ 00072 void 00073 HostInfo::update() 00074 { 00075 if ( short__name != NULL ) { 00076 free(short__name); 00077 } 00078 if (domain_name != NULL) { 00079 free(domain_name); 00080 } 00081 00082 char *dot; 00083 if ( (dot = strchr(utsname->nodename, '.')) == NULL ) { 00084 short__name = strdup(utsname->nodename); 00085 domain_name = strdup(""); 00086 } else { 00087 int short_length = dot - utsname->nodename + 1; 00088 int domain_length = strlen(utsname->nodename) - short_length + 1; 00089 short__name = (char *)malloc(short_length); 00090 short__name[short_length - 1] = 0; 00091 strncpy(short__name, utsname->nodename, short_length - 1); 00092 00093 domain_name = (char *)malloc(domain_length); 00094 domain_name[domain_length - 1] = 0; 00095 strncpy(domain_name, dot + 1, domain_length - 1); 00096 } 00097 } 00098 00099 00100 /** Get full hostname. 00101 * @return hostname 00102 */ 00103 const char * 00104 HostInfo::name() 00105 { 00106 return utsname->nodename; 00107 } 00108 00109 00110 /** Get short hostname (up to first dot). 00111 * @return short hostname 00112 */ 00113 const char * 00114 HostInfo::short_name() 00115 { 00116 return short__name; 00117 } 00118 00119 00120 /** Get domain name (after first dot or none if no dot in name). 00121 * @return domain name 00122 */ 00123 const char * 00124 HostInfo::domain() 00125 { 00126 return domain_name; 00127 } 00128 00129 00130 /** Get architecture (like i686 or x86_64). 00131 * @return architecture 00132 */ 00133 const char * 00134 HostInfo::arch() 00135 { 00136 return utsname->machine; 00137 } 00138 00139 00140 /** Get system name (like Linux). 00141 * @return system name 00142 */ 00143 const char * 00144 HostInfo::sys_name() 00145 { 00146 return utsname->sysname; 00147 } 00148 00149 00150 /** Get system release (kernel version on Linux). 00151 * @return system release 00152 */ 00153 const char * 00154 HostInfo::sys_release() 00155 { 00156 return utsname->release; 00157 } 00158 00159 00160 /** Get system version (build date on Linux). 00161 * @return system version 00162 */ 00163 const char * 00164 HostInfo::sys_version() 00165 { 00166 return utsname->version; 00167 } 00168 00169 } // end namespace fawkes