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