Fawkes API  Fawkes Development Version
hostinfo.cpp
1 
2 /***************************************************************************
3  * hostinfo.h - hostname utilities
4  *
5  * Created: Fri Jan 12 16:12:09 2007
6  * Copyright 2007 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version. A runtime exception applies to
14  * this software (see LICENSE.GPL_WRE file mentioned below for details).
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Library General Public License for more details.
20  *
21  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22  */
23 
24 #include <utils/system/hostinfo.h>
25 
26 #include <core/exceptions/software.h>
27 
28 #include <sys/utsname.h>
29 #include <cstring>
30 #include <cstdlib>
31 
32 namespace fawkes {
33 
34 /** @class HostInfo utils/system/hostinfo.h
35  * Host information.
36  * This class provides access to basic system information like hostname,
37  * domain name, architecture and system information. It's basically a
38  * C++ wrapper to the uname system call.
39  * @author Tim Niemueller
40  */
41 
42 /** Constructor. */
44 {
45  utsname = (struct ::utsname *)malloc(sizeof(struct ::utsname));
46 
47  if ( uname(utsname) != 0 ) {
48  delete utsname;
49  utsname = NULL;
50  throw NullPointerException("Could not call uname");
51  }
52 
53  short__name = NULL;
54  domain_name = NULL;
55 
56  update();
57 }
58 
59 
60 /** Destructor. */
62 {
63  free(utsname);
64  free(short__name);
65  free(domain_name);
66 }
67 
68 
69 /** Update information.
70  * Gathers the information again.
71  */
72 void
74 {
75  if ( short__name != NULL ) {
76  free(short__name);
77  }
78  if (domain_name != NULL) {
79  free(domain_name);
80  }
81 
82  char *dot;
83  if ( (dot = strchr(utsname->nodename, '.')) == NULL ) {
84  short__name = strdup(utsname->nodename);
85  domain_name = strdup("");
86  } else {
87  int short_length = dot - utsname->nodename + 1;
88  int domain_length = strlen(utsname->nodename) - short_length + 1;
89  short__name = (char *)malloc(short_length);
90  short__name[short_length - 1] = 0;
91  strncpy(short__name, utsname->nodename, short_length - 1);
92 
93  domain_name = (char *)malloc(domain_length);
94  domain_name[domain_length - 1] = 0;
95  strncpy(domain_name, dot + 1, domain_length - 1);
96  }
97 }
98 
99 
100 /** Get full hostname.
101  * @return hostname
102  */
103 const char *
105 {
106  return utsname->nodename;
107 }
108 
109 
110 /** Get short hostname (up to first dot).
111  * @return short hostname
112  */
113 const char *
115 {
116  return short__name;
117 }
118 
119 
120 /** Get domain name (after first dot or none if no dot in name).
121  * @return domain name
122  */
123 const char *
125 {
126  return domain_name;
127 }
128 
129 
130 /** Get architecture (like i686 or x86_64).
131  * @return architecture
132  */
133 const char *
135 {
136  return utsname->machine;
137 }
138 
139 
140 /** Get system name (like Linux).
141  * @return system name
142  */
143 const char *
145 {
146  return utsname->sysname;
147 }
148 
149 
150 /** Get system release (kernel version on Linux).
151  * @return system release
152  */
153 const char *
155 {
156  return utsname->release;
157 }
158 
159 
160 /** Get system version (build date on Linux).
161  * @return system version
162  */
163 const char *
165 {
166  return utsname->version;
167 }
168 
169 } // end namespace fawkes
~HostInfo()
Destructor.
Definition: hostinfo.cpp:61
const char * short_name()
Get short hostname (up to first dot).
Definition: hostinfo.cpp:114
const char * arch()
Get architecture (like i686 or x86_64).
Definition: hostinfo.cpp:134
void update()
Update information.
Definition: hostinfo.cpp:73
Fawkes library namespace.
const char * domain()
Get domain name (after first dot or none if no dot in name).
Definition: hostinfo.cpp:124
A NULL pointer was supplied where not allowed.
Definition: software.h:34
const char * sys_version()
Get system version (build date on Linux).
Definition: hostinfo.cpp:164
const char * name()
Get full hostname.
Definition: hostinfo.cpp:104
const char * sys_name()
Get system name (like Linux).
Definition: hostinfo.cpp:144
const char * sys_release()
Get system release (kernel version on Linux).
Definition: hostinfo.cpp:154
HostInfo()
Constructor.
Definition: hostinfo.cpp:43