1 |
#include <sys/utsname.h> |
2 |
#include <ukcprog.h> |
3 |
#include <stdio.h> |
4 |
#ifdef SOLARIS |
5 |
#include <kstat.h> |
6 |
#include <time.h> |
7 |
#endif |
8 |
#ifdef FREEBSD |
9 |
#include <sys/types.h> |
10 |
#include <sys/sysctl.h> |
11 |
#include <time.h> |
12 |
#include <sys/time.h> |
13 |
#endif |
14 |
|
15 |
char *get_os_info(){ |
16 |
char *xml_os_info; |
17 |
struct utsname os; |
18 |
long uptime; |
19 |
#ifdef SOLARIS |
20 |
time_t boottime,curtime; |
21 |
kstat_ctl_t *kc; |
22 |
kstat_t *ksp; |
23 |
kstat_named_t *kn; |
24 |
#endif |
25 |
#ifdef LINUX |
26 |
FILE *f; |
27 |
#endif |
28 |
#ifdef FREEBSD |
29 |
struct timeval boottime; |
30 |
time_t curtime; |
31 |
size_t size; |
32 |
#endif |
33 |
if((uname(&os)) < 0){ |
34 |
errf("Failed to get os stats (%m)"); |
35 |
return NULL; |
36 |
} |
37 |
|
38 |
/* Get the uptime */ |
39 |
#ifdef SOLARIS |
40 |
if ((kc = kstat_open()) == NULL) { |
41 |
errf("kstat_open failure (%m)"); |
42 |
return NULL; |
43 |
} |
44 |
if((ksp=kstat_lookup(kc, "unix", -1, "system_misc"))==NULL){ |
45 |
errf("failed to find lookup information (%m)"); |
46 |
return NULL; |
47 |
} |
48 |
if (kstat_read(kc, ksp, 0) == -1) { |
49 |
errf("Failed to read kernel information (%m)"); |
50 |
return NULL; |
51 |
} |
52 |
if((kn=kstat_data_lookup(ksp, "boot_time")) == NULL){ |
53 |
errf("Failed to get uptime (%m)"); |
54 |
return NULL; |
55 |
} |
56 |
boottime=(kn->value.ui32); |
57 |
time(&curtime); |
58 |
if((kstat_close(kc)) != 0){ |
59 |
errf("Failed to close kstat control structure (%m)"); |
60 |
return NULL; |
61 |
} |
62 |
uptime=curtime-boottime; |
63 |
#endif |
64 |
#ifdef LINUX |
65 |
if ((f=fopen("/proc/uptime", "r")) == NULL) { |
66 |
errf("Failed to open /proc/uptime (%m)"); |
67 |
return NULL; |
68 |
} |
69 |
if((fscanf(f,"%ld",&uptime)) != 1){ |
70 |
errf("Failed to read uptime (%m)"); |
71 |
return NULL; |
72 |
} |
73 |
if ((fclose(f)) != 0) { |
74 |
errf("Failed to close file (%m)"); |
75 |
return NULL; |
76 |
} |
77 |
#endif |
78 |
#ifdef FREEBSD |
79 |
#define BOOT_TIME_NAME "kern.boottime" |
80 |
|
81 |
if (sysctlbyname(BOOT_TIME_NAME, NULL, &size, NULL, NULL) < 0){ |
82 |
errf("sysctlbyname failed (%m)"); |
83 |
return NULL; |
84 |
} |
85 |
if (sysctlbyname(BOOT_TIME_NAME, &boottime, &size, NULL, NULL) < 0){ |
86 |
errf("sysctlbyname failed (%m)"); |
87 |
return NULL; |
88 |
} |
89 |
time(&curtime); |
90 |
uptime=curtime-boottime.tv_sec; |
91 |
|
92 |
#endif |
93 |
|
94 |
|
95 |
xml_os_info=strf("<os><name>%s</name><release>%s</release><version>%s</version><sysname>%s</sysname><platform>%s</platform><uptime>%ld</uptime></os>", os.sysname, os.release, os.version, os.nodename, os.machine, uptime); |
96 |
|
97 |
return xml_os_info; |
98 |
} |