~ubuntu-branches/ubuntu/quantal/open-vm-tools/quantal-201207201942

« back to all changes in this revision

Viewing changes to lib/misc/hostinfo_misc.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Baumann
  • Date: 2009-03-20 10:19:00 UTC
  • mfrom: (1.1.4 upstream) (2.4.3 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090320101900-1o604camiubq2de8
Tags: 2009.03.18-154848-2
Correcting patch system depends (Closes: #520493).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*********************************************************
2
 
 * Copyright (C) 1998 VMware, Inc. All rights reserved.
3
 
 *
4
 
 * This program is free software; you can redistribute it and/or modify it
5
 
 * under the terms of the GNU Lesser General Public License as published
6
 
 * by the Free Software Foundation version 2.1 and no later version.
7
 
 *
8
 
 * This program is distributed in the hope that it will be useful, but
9
 
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10
 
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the Lesser GNU General Public
11
 
 * License for more details.
12
 
 *
13
 
 * You should have received a copy of the GNU Lesser General Public License
14
 
 * along with this program; if not, write to the Free Software Foundation, Inc.,
15
 
 * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA.
16
 
 *
17
 
 *********************************************************/
18
 
 
19
 
#include <stdio.h>
20
 
#include <stdlib.h>
21
 
 
22
 
#if defined(_WIN32)
23
 
#include <windows.h>
24
 
#include <winbase.h>
25
 
#else
26
 
#include <string.h>
27
 
#include <errno.h>
28
 
#include <sys/time.h>
29
 
#include <sys/utsname.h>
30
 
#endif
31
 
 
32
 
#include "vmware.h"
33
 
#include "hostinfo.h"
34
 
#include "safetime.h"
35
 
#include "str.h"
36
 
 
37
 
static Bool hostinfoOSVersionInitialized;
38
 
 
39
 
#if defined(_WIN32)
40
 
static int hostinfoOSVersion[4];
41
 
static DWORD hostinfoOSPlatform;
42
 
#else
43
 
#if defined(__APPLE__)
44
 
#define SYS_NMLN _SYS_NAMELEN
45
 
#endif
46
 
static int hostinfoOSVersion[3];
47
 
static char hostinfoOSVersionString[SYS_NMLN];
48
 
#endif
49
 
 
50
 
 
51
 
#if defined(_WIN32)
52
 
/*
53
 
 *----------------------------------------------------------------------
54
 
 *
55
 
 * HostinfoOSVersionInit --
56
 
 *
57
 
 *      Compute the OS version information
58
 
 *
59
 
 * Results:
60
 
 *      None.
61
 
 *
62
 
 * Side effects:
63
 
 *      hostinfoOS* variables are filled in.
64
 
 *
65
 
 *----------------------------------------------------------------------
66
 
 */
67
 
 
68
 
void
69
 
HostinfoOSVersionInit(void)
70
 
{
71
 
   OSVERSIONINFO info;
72
 
   OSVERSIONINFOEX infoEx;
73
 
 
74
 
   if (hostinfoOSVersionInitialized) {
75
 
      return;
76
 
   }
77
 
 
78
 
   info.dwOSVersionInfoSize = sizeof (info);
79
 
   if (!GetVersionEx(&info)) {
80
 
      Warning("Unable to get OS version.\n");
81
 
      NOT_IMPLEMENTED();
82
 
   }
83
 
   ASSERT(ARRAYSIZE(hostinfoOSVersion) >= 4);
84
 
   hostinfoOSVersion[0] = info.dwMajorVersion;
85
 
   hostinfoOSVersion[1] = info.dwMinorVersion;
86
 
   hostinfoOSVersion[2] = info.dwBuildNumber & 0xffff;
87
 
   /*
88
 
    * Get the service pack number. We don't care much about NT4 hosts
89
 
    * so we can use OSVERSIONINFOEX without checking for Windows NT 4.0 SP6 
90
 
    * or later versions.
91
 
    */
92
 
   infoEx.dwOSVersionInfoSize = sizeof infoEx;
93
 
   if (GetVersionEx((OSVERSIONINFO*)&infoEx)) {
94
 
      hostinfoOSVersion[3] = infoEx.wServicePackMajor;
95
 
   }
96
 
   hostinfoOSPlatform = info.dwPlatformId;
97
 
 
98
 
   hostinfoOSVersionInitialized = TRUE;
99
 
}
100
 
 
101
 
 
102
 
/*
103
 
 *----------------------------------------------------------------------
104
 
 *
105
 
 * Hostinfo_OSIsWinNT --
106
 
 *
107
 
 *      This is Windows NT or descendant.
108
 
 *
109
 
 * Results:
110
 
 *      TRUE if Windows NT or descendant.
111
 
 *
112
 
 * Side effects:
113
 
 *      None.
114
 
 *
115
 
 *----------------------------------------------------------------------
116
 
 */
117
 
 
118
 
Bool
119
 
Hostinfo_OSIsWinNT(void)
120
 
{
121
 
   HostinfoOSVersionInit();
122
 
   return hostinfoOSPlatform == VER_PLATFORM_WIN32_NT;
123
 
}
124
 
#else
125
 
 
126
 
 
127
 
/*
128
 
 *----------------------------------------------------------------------
129
 
 *
130
 
 * HostinfoOSVersionInit --
131
 
 *
132
 
 *      Compute the OS version information
133
 
 *
134
 
 * Results:
135
 
 *      None.
136
 
 *
137
 
 * Side effects:
138
 
 *      hostinfoOS* variables are filled in.
139
 
 *
140
 
 *----------------------------------------------------------------------
141
 
 */
142
 
 
143
 
static void
144
 
HostinfoOSVersionInit(void)
145
 
{
146
 
   struct utsname u;
147
 
   char extra[SYS_NMLN] = "";
148
 
 
149
 
   if (hostinfoOSVersionInitialized) {
150
 
      return;
151
 
   }
152
 
 
153
 
   if (uname(&u) < 0) {
154
 
      Warning("%s unable to get host OS version (uname): %s\n",
155
 
              __FUNCTION__, strerror(errno));
156
 
      NOT_IMPLEMENTED();
157
 
   }
158
 
 
159
 
   Str_Strcpy(hostinfoOSVersionString, u.release, SYS_NMLN);
160
 
 
161
 
   ASSERT(ARRAYSIZE(hostinfoOSVersion) >= 3);
162
 
   if (sscanf(u.release, "%d.%d.%d%s",
163
 
              &hostinfoOSVersion[0], &hostinfoOSVersion[1],
164
 
              &hostinfoOSVersion[2], extra) < 1) {
165
 
      Warning("%s unable to parse host OS version string: %s\n",
166
 
              __FUNCTION__, u.release);
167
 
      NOT_IMPLEMENTED();
168
 
   }
169
 
 
170
 
   hostinfoOSVersionInitialized = TRUE;
171
 
}
172
 
 
173
 
 
174
 
/*
175
 
 *----------------------------------------------------------------------
176
 
 *
177
 
 * Hostinfo_OSVersionString --
178
 
 *
179
 
 *      Returns the host version information as returned in the
180
 
 *      release field of uname(2)
181
 
 *
182
 
 * Results:
183
 
 *      const char * - pointer to static buffer containing the release
184
 
 *
185
 
 * Side effects:
186
 
 *      None
187
 
 *
188
 
 *----------------------------------------------------------------------
189
 
 */
190
 
 
191
 
const char *
192
 
Hostinfo_OSVersionString(void)
193
 
{
194
 
   HostinfoOSVersionInit();
195
 
   return hostinfoOSVersionString;
196
 
}
197
 
#endif
198
 
 
199
 
 
200
 
/*
201
 
 *----------------------------------------------------------------------
202
 
 *
203
 
 * Hostinfo_OSVersion --
204
 
 *
205
 
 *      Host OS release info.
206
 
 *
207
 
 * Results:
208
 
 *      The i-th component of a dotted release string.
209
 
 *      0 if i is greater than the number of components we support.
210
 
 *
211
 
 * Side effects:
212
 
 *      None.
213
 
 *
214
 
 *----------------------------------------------------------------------
215
 
 */
216
 
 
217
 
int
218
 
Hostinfo_OSVersion(int i)
219
 
{
220
 
   HostinfoOSVersionInit();
221
 
   return i < ARRAYSIZE(hostinfoOSVersion) ? hostinfoOSVersion[i] : 0;
222
 
}
223
 
 
224
 
 
225
 
/*
226
 
 *----------------------------------------------------------------------
227
 
 *
228
 
 * Hostinfo_GetTimeOfDay --
229
 
 *
230
 
 *      Return the current time of day according to the host.  We want
231
 
 *      UTC time (seconds since Jan 1, 1970).
232
 
 *
233
 
 * Results:
234
 
 *      Time of day in microseconds.
235
 
 *
236
 
 * Side effects:
237
 
 *      None.
238
 
 *
239
 
 *----------------------------------------------------------------------
240
 
 */
241
 
 
242
 
void 
243
 
Hostinfo_GetTimeOfDay(VmTimeType *time)
244
 
{
245
 
#if defined(_WIN32)
246
 
   struct _timeb t;
247
 
 
248
 
   _ftime(&t);
249
 
 
250
 
   *time = (t.time * 1000000) + t.millitm * 1000;
251
 
#else  // assume POSIX
252
 
   struct timeval tv;
253
 
 
254
 
   gettimeofday(&tv, NULL);
255
 
 
256
 
   *time = ((int64)tv.tv_sec * 1000000) + tv.tv_usec;
257
 
#endif
258
 
}