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 <core/exceptions/software.h>
25#include <sys/utsname.h>
26#include <utils/system/hostinfo.h>
27
28#include <cstdlib>
29#include <cstring>
30
31namespace fawkes {
32
33/** @class HostInfo utils/system/hostinfo.h
34 * Host information.
35 * This class provides access to basic system information like hostname,
36 * domain name, architecture and system information. It's basically a
37 * C++ wrapper to the uname system call.
38 * @author Tim Niemueller
39 */
40
41/** Constructor. */
43{
44 utsname = (struct ::utsname *)malloc(sizeof(struct ::utsname));
45
46 if (uname(utsname) != 0) {
47 ::free(utsname);
48 utsname = NULL;
49 throw NullPointerException("Could not call uname");
50 }
51
52 shortname_ = NULL;
53 domain_name = NULL;
54
55 update();
56}
57
58/** Destructor. */
60{
61 free(utsname);
62 free(shortname_);
63 free(domain_name);
64}
65
66/** Update information.
67 * Gathers the information again.
68 */
69void
71{
72 if (shortname_ != NULL) {
73 free(shortname_);
74 }
75 if (domain_name != NULL) {
76 free(domain_name);
77 }
78
79 char *dot;
80 if ((dot = strchr(utsname->nodename, '.')) == NULL) {
81 shortname_ = strdup(utsname->nodename);
82 domain_name = strdup("");
83 } else {
84 int short_length = dot - utsname->nodename + 1;
85 int domain_length = strlen(utsname->nodename) - short_length + 1;
86 shortname_ = (char *)malloc(short_length);
87 shortname_[short_length - 1] = 0;
88 strncpy(shortname_, utsname->nodename, short_length - 1);
89
90 domain_name = (char *)malloc(domain_length);
91 domain_name[domain_length - 1] = 0;
92 strncpy(domain_name, dot + 1, domain_length - 1);
93 }
94}
95
96/** Get full hostname.
97 * @return hostname
98 */
99const char *
101{
102 return utsname->nodename;
103}
104
105/** Get short hostname (up to first dot).
106 * @return short hostname
107 */
108const char *
110{
111 return shortname_;
112}
113
114/** Get domain name (after first dot or none if no dot in name).
115 * @return domain name
116 */
117const char *
119{
120 return domain_name;
121}
122
123/** Get architecture (like i686 or x86_64).
124 * @return architecture
125 */
126const char *
128{
129 return utsname->machine;
130}
131
132/** Get system name (like Linux).
133 * @return system name
134 */
135const char *
137{
138 return utsname->sysname;
139}
140
141/** Get system release (kernel version on Linux).
142 * @return system release
143 */
144const char *
146{
147 return utsname->release;
148}
149
150/** Get system version (build date on Linux).
151 * @return system version
152 */
153const char *
155{
156 return utsname->version;
157}
158
159} // end namespace fawkes
const char * sys_release()
Get system release (kernel version on Linux).
Definition: hostinfo.cpp:145
const char * name()
Get full hostname.
Definition: hostinfo.cpp:100
const char * domain()
Get domain name (after first dot or none if no dot in name).
Definition: hostinfo.cpp:118
void update()
Update information.
Definition: hostinfo.cpp:70
const char * arch()
Get architecture (like i686 or x86_64).
Definition: hostinfo.cpp:127
~HostInfo()
Destructor.
Definition: hostinfo.cpp:59
const char * short_name()
Get short hostname (up to first dot).
Definition: hostinfo.cpp:109
HostInfo()
Constructor.
Definition: hostinfo.cpp:42
const char * sys_version()
Get system version (build date on Linux).
Definition: hostinfo.cpp:154
const char * sys_name()
Get system name (like Linux).
Definition: hostinfo.cpp:136
A NULL pointer was supplied where not allowed.
Definition: software.h:32
Fawkes library namespace.