ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/libstatgrab/os_info.c
(Generate patch)

Comparing projects/libstatgrab/src/libstatgrab/os_info.c (file contents):
Revision 1.22 by ats, Sun Nov 7 12:31:33 2004 UTC vs.
Revision 1.23 by tdb, Sat Sep 24 13:29:22 2005 UTC

# Line 25 | Line 25
25   #include "config.h"
26   #endif
27  
28 + #ifndef WIN32
29   #include <sys/utsname.h>
30 + #endif
31   #include "statgrab.h"
32   #include <stdlib.h>
33   #ifdef SOLARIS
# Line 51 | Line 53
53   #include <sys/pstat.h>
54   #include <time.h>
55   #endif
56 + #ifdef WIN32
57 + #include <windows.h>
58 + #include "win32.h"
59 + #define WINDOWS2000 "Windows 2000"
60 + #define WINDOWSXP "Windows XP"
61 + #define WINDOWS2003 "Windows Server 2003"
62 + #define BUFSIZE 12
63 + static int runonce = 0;
64 + #endif
65  
66   #include "tools.h"
67  
68 < sg_host_info *sg_get_host_info(){
68 > #ifdef WIN32
69 > static int home_or_pro(const OSVERSIONINFOEX osinfo, char **name)
70 > {
71 >        int r;
72  
73 +        if (osinfo.wSuiteMask & VER_SUITE_PERSONAL) {
74 +                r = sg_concat_string(name, " Home Edition");
75 +        } else {
76 +                r = sg_concat_string(name, " Professional");
77 +        }
78 +        return r;
79 + }
80 +
81 + static char *get_os_name(const OSVERSIONINFOEX osinfo)
82 + {
83 +        char *name;
84 +        char tmp[10];
85 +        int r = 0;
86 +
87 +        /* we only compile on 2k or newer, which is version 5
88 +         * Covers 2000, XP and 2003 */
89 +        if (osinfo.dwMajorVersion != 5) {
90 +                return "Unknown";
91 +        }
92 +        switch(osinfo.dwMinorVersion) {
93 +                case 0: /* Windows 2000 */
94 +                        name = strdup(WINDOWS2000);
95 +                        if(name == NULL) {
96 +                                goto out;
97 +                        }
98 +                        if (osinfo.wProductType == VER_NT_WORKSTATION) {
99 +                                r = home_or_pro(osinfo, &name);
100 +                        } else if (osinfo.wSuiteMask & VER_SUITE_DATACENTER) {
101 +                                r = sg_concat_string(&name, " Datacenter Server");
102 +                        } else if (osinfo.wSuiteMask & VER_SUITE_ENTERPRISE) {
103 +                                r = sg_concat_string(&name, " Advanced Server");
104 +                        } else {
105 +                                r = sg_concat_string(&name, " Server");
106 +                        }
107 +                        break;
108 +                case 1: /* Windows XP */
109 +                        name = strdup(WINDOWSXP);
110 +                        if(name == NULL) {
111 +                                goto out;
112 +                        }
113 +                        r = home_or_pro(osinfo, &name);
114 +                        break;
115 +                case 2: /* Windows 2003 */
116 +                        name = strdup(WINDOWS2003);
117 +                        if(name == NULL) {
118 +                                goto out;
119 +                        }
120 +                        if (osinfo.wSuiteMask & VER_SUITE_DATACENTER) {
121 +                                r = sg_concat_string(&name, " Datacenter Edition");
122 +                        } else if (osinfo.wSuiteMask & VER_SUITE_ENTERPRISE) {
123 +                                r = sg_concat_string(&name, " Enterprise Edition");
124 +                        } else if (osinfo.wSuiteMask & VER_SUITE_BLADE) {
125 +                                r = sg_concat_string(&name, " Web Edition");
126 +                        } else {
127 +                                r = sg_concat_string(&name, " Standard Edition");
128 +                        }
129 +                        break;
130 +                default:
131 +                        name = strdup("Windows 2000 based");
132 +                        break;
133 +        }
134 +        if(r != 0) {
135 +                free (name);
136 +                return NULL;
137 +        }
138 +        /* Add on service pack version */
139 +        if (osinfo.wServicePackMajor != 0) {
140 +                if(osinfo.wServicePackMinor == 0) {
141 +                        if(snprintf(tmp, sizeof(tmp), " SP%d", osinfo.wServicePackMajor) != -1) {
142 +                                r = sg_concat_string(&name, tmp);
143 +                        }
144 +                } else {
145 +                        if(snprintf(tmp, sizeof(tmp), " SP%d.%d", osinfo.wServicePackMajor,
146 +                                        osinfo.wServicePackMinor) != -1) {
147 +                                r = sg_concat_string(&name, tmp);
148 +                        }
149 +                }
150 +                if(r) {
151 +                        free(name);
152 +                        return NULL;
153 +                }
154 +        }
155 +        return name;
156 +
157 + out:
158 +        /* strdup failed */
159 +        sg_set_error_with_errno(SG_ERROR_MALLOC, NULL);
160 +        return NULL;
161 + }
162 + #endif
163 +
164 + sg_host_info *sg_get_host_info()
165 + {
166          static sg_host_info general_stat;
167 + #ifndef WIN32
168          static struct utsname os;
169 + #endif
170  
171   #ifdef HPUX
172          struct pst_static *pstat_static;
# Line 79 | Line 188 | sg_host_info *sg_get_host_info(){
188          time_t curtime;
189          size_t size;
190   #endif
191 + #ifdef WIN32
192 +        unsigned long nameln;
193 +        char *name;
194 +        long long result;
195 +        OSVERSIONINFOEX osinfo;
196 +        SYSTEM_INFO sysinfo;
197 +        char *tmp_name;
198 +        char tmp[10];
199 + #endif
200  
201 + #ifndef WIN32 /* Trust windows to be different */
202          if((uname(&os)) < 0){
203                  sg_set_error_with_errno(SG_ERROR_UNAME, NULL);
204                  return NULL;
205          }
206 <        
206 >
207          general_stat.os_name = os.sysname;
208          general_stat.os_release = os.release;
209          general_stat.os_version = os.version;
210          general_stat.platform = os.machine;
211          general_stat.hostname = os.nodename;
212 + #else /* WIN32 */
213 +        if (!runonce) {
214 +                /* these settings are static after boot, so why get them
215 +                 * constantly? */
216  
217 +                /* get system name */
218 +                nameln = MAX_COMPUTERNAME_LENGTH + 1;
219 +                name = sg_malloc(nameln);
220 +                if(name == NULL) {
221 +                        return NULL;
222 +                }
223 +                if(GetComputerName(name, &nameln) == 0) {
224 +                        free(name);
225 +                        sg_set_error(SG_ERROR_HOST, "GetComputerName");
226 +                        return NULL;
227 +                }
228 +                if(sg_update_string(&general_stat.hostname, name)) {
229 +                        free(name);
230 +                        return NULL;
231 +                }
232 +                free(name);
233 +
234 +                /* get OS name, version and build */
235 +                ZeroMemory(&osinfo, sizeof(OSVERSIONINFOEX));
236 +                osinfo.dwOSVersionInfoSize = sizeof(osinfo);
237 +                if(!GetVersionEx(&osinfo)) {
238 +                        sg_set_error(SG_ERROR_HOST, "GetVersionEx");
239 +                        return NULL;
240 +                }
241 +
242 +                /* Release - single number */
243 +                if(snprintf(tmp, sizeof(tmp), "%ld", osinfo.dwBuildNumber) == -1) {
244 +                        free(tmp);
245 +                        return NULL;
246 +                }
247 +                if(sg_update_string(&general_stat.os_release, tmp)) {
248 +                        free(tmp);
249 +                        return NULL;
250 +                }
251 +
252 +                /* Version */
253 +                /* usually a single digit . single digit, eg 5.0 */
254 +                if(snprintf(tmp, sizeof(tmp), "%ld.%ld", osinfo.dwMajorVersion,
255 +                                        osinfo.dwMinorVersion) == -1) {
256 +                        free(tmp);
257 +                        return NULL;
258 +                }
259 +                if(sg_update_string(&general_stat.os_version, tmp)) {
260 +                        free(tmp);
261 +                        return NULL;
262 +                }
263 +
264 +                /* OS name */
265 +                tmp_name = get_os_name(osinfo);
266 +                if(tmp_name == NULL) {
267 +                        return NULL;
268 +                }
269 +                if(sg_update_string(&general_stat.os_name, tmp_name)) {
270 +                        free(tmp_name);
271 +                        return NULL;
272 +                }
273 +                free(tmp_name);
274 +                runonce = 1;
275 +
276 +                /* Platform */
277 +                GetSystemInfo(&sysinfo);
278 +                switch(sysinfo.wProcessorArchitecture) {
279 +                        case PROCESSOR_ARCHITECTURE_INTEL:
280 +                                if(sg_update_string(&general_stat.platform,
281 +                                                        "Intel")) {
282 +                                        return NULL;
283 +                                }
284 +                                break;
285 +                        case PROCESSOR_ARCHITECTURE_IA64:
286 +                                if(sg_update_string(&general_stat.platform,
287 +                                                        "IA64")) {
288 +                                        return NULL;
289 +                                }
290 +                                break;
291 +                        case PROCESSOR_ARCHITECTURE_AMD64:
292 +                                if(sg_update_string(&general_stat.platform,
293 +                                                        "AMD64")) {
294 +                                        return NULL;
295 +                                }
296 +                                break;
297 +                        default:
298 +                                if(sg_update_string(&general_stat.platform,
299 +                                                        "Unknown")){
300 +                                        return NULL;
301 +                                }
302 +                                break;
303 +                }
304 +        }
305 + #endif /* WIN32 */
306 +
307          /* get uptime */
308   #ifdef HPUX
309          pstat_static = sg_get_pstat_static();
# Line 150 | Line 363 | sg_host_info *sg_get_host_info(){
363          }
364          time(&curtime);
365          general_stat.uptime=curtime-boottime.tv_sec;
366 + #endif
367 + #ifdef WIN32
368 +        if(read_counter_large(SG_WIN32_UPTIME, &result)) {
369 +                sg_set_error(SG_ERROR_PDHREAD, PDH_UPTIME);
370 +                return NULL;
371 +        }
372 +        general_stat.uptime = (time_t) result;
373   #endif
374  
375          return &general_stat;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines