summaryrefslogtreecommitdiff
path: root/source/base/PlatformInfo.c
blob: 44c45cc600c1332b5277bb5455a8aea768d82b14 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
//
// PlatformInfo.c - MrsWatson
// Created by Nik Reiman on 1/2/12.
// Copyright (c) 2012 Teragon Audio. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
//   this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright notice,
//   this list of conditions and the following disclaimer in the documentation
//   and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//

#include <stdio.h>
#include <string.h>

#include "base/PlatformInfo.h"
#include "logging/EventLogger.h"

#if LINUX
#include <sys/utsname.h>
#include "base/File.h"

#define LSB_FILE_PATH "/etc/lsb-release"
#define LSB_DISTRIBUTION "DISTRIB_DESCRIPTION"
#endif

static PlatformType _getPlatformType()
{
#if MACOSX
    return PLATFORM_MACOSX;
#elif WINDOWS
    return PLATFORM_WINDOWS;
#elif LINUX
    return PLATFORM_LINUX;
#else
    return PLATFORM_UNSUPPORTED;
#endif
}

static const char *_getShortPlatformName(void)
{
#if MACOSX
    return "Mac OS X";
#elif WINDOWS

    if (platformInfoIsRuntime64Bit()) {
        return "Windows 64-bit";
    } else {
        return "Windows 32-bit";
    }

#elif LINUX

    if (platformInfoIsRuntime64Bit()) {
        return "Linux-x86_64";
    } else {
        return "Linux-i686";
    }

#else
    return "Unsupported";
#endif
}

#if LINUX
static void _findLsbDistribution(void *item, void *userData)
{
    CharString line = (CharString)item;
    CharString distributionName = (CharString)userData;
    LinkedList tokens = charStringSplit(line, '=');

    if (tokens != NULL && linkedListLength(tokens) == 2) {
        CharString *tokensArray = (CharString *)linkedListToArray(tokens);
        CharString key = tokensArray[0];
        CharString value = tokensArray[1];

        if (!strcmp(key->data, LSB_DISTRIBUTION)) {
            charStringCopy(distributionName, value);
        }

        free(tokensArray);
    }

    freeLinkedListAndItems(tokens, (LinkedListFreeItemFunc)freeCharString);
}
#endif

#if MACOSX
extern void _getMacVersionString(CharString outString);
#endif

static CharString _getPlatformName(void)
{
    CharString result = newCharString();
#if MACOSX
    charStringCopyCString(result, _getShortPlatformName());
    _getMacVersionString(result);
#elif LINUX
    CharString distributionName = newCharString();
    struct utsname systemInfo;
    File lsbRelease = NULL;
    LinkedList lsbReleaseLines = NULL;

    if (uname(&systemInfo) != 0) {
        logWarn("Could not get system information from uname");
        charStringCopyCString(result, "Linux (Unknown platform)");
        freeCharString(distributionName);
        return result;
    }

    charStringCopyCString(distributionName, "(Unknown distribution)");

    lsbRelease = newFileWithPathCString(LSB_FILE_PATH);

    if (fileExists(lsbRelease)) {
        lsbReleaseLines = fileReadLines(lsbRelease);

        if (lsbReleaseLines != NULL && linkedListLength(lsbReleaseLines) > 0) {
            linkedListForeach(lsbReleaseLines, _findLsbDistribution, distributionName);
        }
    }

    if (charStringIsEmpty(result)) {
        snprintf(result->data, result->capacity, "Linux %s, kernel %s %s",
                 distributionName->data, systemInfo.release, systemInfo.machine);
    }

    freeCharString(distributionName);
    freeLinkedListAndItems(lsbReleaseLines, (LinkedListFreeItemFunc)freeCharString);
    freeFile(lsbRelease);
#elif WINDOWS
    OSVERSIONINFOEX versionInformation;
    memset(&versionInformation, 0, sizeof(OSVERSIONINFOEX));
    versionInformation.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
    GetVersionEx((OSVERSIONINFO *)&versionInformation);
    // Generic string which will also work with newer versions of windows
    snprintf(result->data, result->capacity, "Windows %d.%d",
             versionInformation.dwMajorVersion, versionInformation.dwMinorVersion);

    // This is a bit lame, but it seems that this is the standard way of getting
    // the platform name on Windows.
    switch (versionInformation.dwMajorVersion) {
    case 6:
        switch (versionInformation.dwMinorVersion) {
        case 2:
            charStringCopyCString(result, "Windows 8");
            break;

        case 1:
            charStringCopyCString(result, "Windows 7");
            break;

        case 0:
            charStringCopyCString(result, "Windows Vista");
            break;
        }

        break;

    case 5:
        switch (versionInformation.dwMinorVersion) {
        case 2:
            charStringCopyCString(result, "Windows Server 2003");
            break;

        case 1:
            charStringCopyCString(result, "Windows XP");
            break;

        case 0:
            charStringCopyCString(result, "Windows 2000");
            break;
        }

        break;
    }

#else
    charStringCopyCString(result, "Unsupported platform");
#endif

    return result;
}

boolByte platformInfoIsRuntime64Bit(void)
{
    return (boolByte)(sizeof(void *) == 8);
}

boolByte platformInfoIsHost64Bit(void)
{
    boolByte result = false;

#if LINUX
    struct utsname systemInfo;

    if (uname(&systemInfo) != 0) {
        logError("Could not get system bitness from uname");
    } else {
        result = (boolByte)(strcmp(systemInfo.machine, "x86_64") == 0);
    }

#elif MACOSX
#elif WINDOWS
    typedef BOOL (WINAPI * IsWow64ProcessFuncPtr)(HANDLE, PBOOL);
    BOOL isProcessRunningInWow64 = false;
    IsWow64ProcessFuncPtr isWow64ProcessFunc = NULL;

    // The IsWow64Process() function is not available on all versions of Windows,
    // so it must be looked up first and called only if it exists.
    isWow64ProcessFunc = (IsWow64ProcessFuncPtr)GetProcAddress(GetModuleHandle(TEXT("kernel32")), "IsWow64Process");

    if (isWow64ProcessFunc != NULL) {
        if (isWow64ProcessFunc(GetCurrentProcess(), &isProcessRunningInWow64)) {
            // IsWow64Process will only return true if the current process is a 32-bit
            // application running on 64-bit Windows.
            if (isProcessRunningInWow64) {
                result = true;
            } else {
                // If false, then we can assume that the host has the same bitness as
                // the executable.
                result = platformInfoIsRuntime64Bit();
            }
        }
    }

#else
    logUnsupportedFeature("Get host 64-bitness");
#endif

    return result;
}

boolByte platformInfoIsLittleEndian(void)
{
    int num = 1;
    return (boolByte)(*(char *)&num == 1);
}

PlatformInfo newPlatformInfo(void)
{
    PlatformInfo platformInfo = (PlatformInfo)malloc(sizeof(PlatformInfoMembers));
    platformInfo->type = _getPlatformType();
    platformInfo->name = _getPlatformName();
    platformInfo->shortName = newCharStringWithCString(_getShortPlatformName());
    platformInfo->is64Bit = platformInfoIsHost64Bit();
    return platformInfo;
}

void freePlatformInfo(PlatformInfo self)
{
    if (self != NULL) {
        freeCharString(self->name);
        freeCharString(self->shortName);
        free(self);
    }
}