1 | /* |
---|
2 | * Copyright (c) 1992-1998 Michael A. Cooper. |
---|
3 | * This software may be freely used and distributed provided it is not |
---|
4 | * sold for profit or used in part or in whole for commercial gain |
---|
5 | * without prior written agreement, and the author is credited |
---|
6 | * appropriately. |
---|
7 | */ |
---|
8 | |
---|
9 | #ifndef lint |
---|
10 | static char *RCSid = "$Revision: 1.1.1.1 $"; |
---|
11 | #endif |
---|
12 | |
---|
13 | /* |
---|
14 | * Linux specific functions |
---|
15 | */ |
---|
16 | |
---|
17 | #include "defs.h" |
---|
18 | #include <sys/stat.h> |
---|
19 | #include <linux/kernel.h> |
---|
20 | #include <linux/sys.h> |
---|
21 | #include <unistd.h> |
---|
22 | |
---|
23 | /* |
---|
24 | * Platform Specific Interfaces |
---|
25 | */ |
---|
26 | extern char *GetModelProc(); |
---|
27 | PSI_t GetModelPSI[] = { /* Get system Model */ |
---|
28 | { GetModelProc }, |
---|
29 | { GetModelFile }, |
---|
30 | { NULL }, |
---|
31 | }; |
---|
32 | PSI_t GetSerialPSI[] = { /* Get Serial Number */ |
---|
33 | { NULL }, |
---|
34 | }; |
---|
35 | PSI_t GetRomVerPSI[] = { /* Get ROM Version */ |
---|
36 | { NULL }, |
---|
37 | }; |
---|
38 | PSI_t GetKernArchPSI[] = { /* Get Kernel Architecture */ |
---|
39 | { GetKernArchUname }, |
---|
40 | { NULL }, |
---|
41 | }; |
---|
42 | PSI_t GetAppArchPSI[] = { /* Get Application Architecture */ |
---|
43 | { GetKernArchUname }, /* Not a typo */ |
---|
44 | { NULL }, |
---|
45 | }; |
---|
46 | extern char *GetCpuTypeProc(); |
---|
47 | PSI_t GetCpuTypePSI[] = { /* Get CPU Type */ |
---|
48 | { GetCpuTypeProc }, |
---|
49 | { NULL }, |
---|
50 | }; |
---|
51 | PSI_t GetNumCpuPSI[] = { /* Get Number of CPU's */ |
---|
52 | { NULL }, |
---|
53 | }; |
---|
54 | extern char *GetKernVerProc(); |
---|
55 | extern char *GetKernVerLinux(); |
---|
56 | PSI_t GetKernVerPSI[] = { /* Get Kernel Version */ |
---|
57 | { GetKernVerProc }, |
---|
58 | { GetKernVerLinux }, |
---|
59 | { NULL }, |
---|
60 | }; |
---|
61 | PSI_t GetOSNamePSI[] = { /* Get OS Name */ |
---|
62 | { GetOSNameUname }, |
---|
63 | { NULL }, |
---|
64 | }; |
---|
65 | extern char *GetOSDistLinux(); |
---|
66 | PSI_t GetOSDistPSI[] = { /* Get OS Dist */ |
---|
67 | { GetOSDistLinux }, |
---|
68 | { NULL }, |
---|
69 | }; |
---|
70 | PSI_t GetOSVerPSI[] = { /* Get OS Version */ |
---|
71 | { GetOSVerUname }, |
---|
72 | { NULL }, |
---|
73 | }; |
---|
74 | PSI_t GetManShortPSI[] = { /* Get Short Man Name */ |
---|
75 | { GetManShortSysinfo }, |
---|
76 | { GetManShortDef }, |
---|
77 | { NULL }, |
---|
78 | }; |
---|
79 | PSI_t GetManLongPSI[] = { /* Get Long Man Name */ |
---|
80 | { GetManLongSysinfo }, |
---|
81 | { GetManLongDef }, |
---|
82 | { NULL }, |
---|
83 | }; |
---|
84 | extern char *GetMemorySysinfo(); |
---|
85 | extern char *GetMemoryKcore(); |
---|
86 | PSI_t GetMemoryPSI[] = { /* Get amount of memory */ |
---|
87 | { GetMemoryKcore }, |
---|
88 | { GetMemorySysinfo }, |
---|
89 | { NULL }, |
---|
90 | }; |
---|
91 | extern char *GetVirtMemLinux(); |
---|
92 | PSI_t GetVirtMemPSI[] = { /* Get amount of virtual memory */ |
---|
93 | { GetVirtMemLinux }, |
---|
94 | { NULL }, |
---|
95 | }; |
---|
96 | extern char *GetBootTimeProc(); |
---|
97 | PSI_t GetBootTimePSI[] = { /* Get System Boot Time */ |
---|
98 | { GetBootTimeProc }, |
---|
99 | { NULL }, |
---|
100 | }; |
---|
101 | |
---|
102 | /* |
---|
103 | * Get Boot Time by using the /proc/uptime file. |
---|
104 | */ |
---|
105 | extern char *GetBootTimeProc() |
---|
106 | { |
---|
107 | FILE *fp; |
---|
108 | static char Buff[64]; |
---|
109 | char *cp; |
---|
110 | char *DateStr; |
---|
111 | time_t Uptime; |
---|
112 | time_t BootTime; |
---|
113 | |
---|
114 | fp = fopen(PROC_FILE_UPTIME, "r"); |
---|
115 | if (!fp) { |
---|
116 | SImsg(SIM_GERR, "%s: open readonly failed: %s", |
---|
117 | PROC_FILE_UPTIME, SYSERR); |
---|
118 | return((char *) NULL); |
---|
119 | } |
---|
120 | |
---|
121 | if (!fgets(Buff, sizeof(Buff), fp)) { |
---|
122 | SImsg(SIM_GERR, "%s: Read uptime failed: %s", |
---|
123 | PROC_FILE_UPTIME, SYSERR); |
---|
124 | (void) fclose(fp); |
---|
125 | return((char *) NULL); |
---|
126 | } |
---|
127 | |
---|
128 | if (cp = strchr(Buff, ' ')) |
---|
129 | *cp = CNULL; |
---|
130 | |
---|
131 | Uptime = (time_t) strtol(Buff, NULL, 0); |
---|
132 | if (Uptime <= 0) { |
---|
133 | SImsg(SIM_GERR, "Convert `%s' to long failed", Buff); |
---|
134 | (void) fclose(fp); |
---|
135 | return((char *) NULL); |
---|
136 | } |
---|
137 | |
---|
138 | BootTime = time(NULL); |
---|
139 | BootTime -= Uptime; |
---|
140 | |
---|
141 | DateStr = (char *) ctime(&BootTime); |
---|
142 | if (cp = strchr(DateStr, '\n')) |
---|
143 | *cp = CNULL; |
---|
144 | |
---|
145 | (void) fclose(fp); |
---|
146 | |
---|
147 | return(DateStr); |
---|
148 | } |
---|
149 | |
---|
150 | /* |
---|
151 | * Get System Model using the /proc/cpuinfo file. |
---|
152 | */ |
---|
153 | extern char *GetModelProc() |
---|
154 | { |
---|
155 | FILE *fp; |
---|
156 | static char Buff[256]; |
---|
157 | char *Cpu = NULL; |
---|
158 | char *Vendor = NULL; |
---|
159 | char **Argv; |
---|
160 | int Argc; |
---|
161 | |
---|
162 | if (Buff[0]) |
---|
163 | return(Buff); |
---|
164 | |
---|
165 | fp = fopen(PROC_FILE_CPUINFO, "r"); |
---|
166 | if (!fp) { |
---|
167 | SImsg(SIM_GERR, "%s: open readonly failed: %s", |
---|
168 | PROC_FILE_CPUINFO, SYSERR); |
---|
169 | return((char *) NULL); |
---|
170 | } |
---|
171 | |
---|
172 | while (fgets(Buff, sizeof(Buff), fp)) { |
---|
173 | Argc = StrToArgv(Buff, ":", &Argv, NULL, 0); |
---|
174 | if (Argc < 2) |
---|
175 | continue; |
---|
176 | if (EQ(Argv[0], "cpu")) |
---|
177 | Cpu = Argv[1]; |
---|
178 | else if (EQ(Argv[0], "vendor_id")) |
---|
179 | Vendor = Argv[1]; |
---|
180 | if (Cpu && Vendor) |
---|
181 | break; |
---|
182 | } |
---|
183 | |
---|
184 | Buff[0] = CNULL; |
---|
185 | if (Vendor) |
---|
186 | (void) strcpy(Buff, Vendor); |
---|
187 | if (Cpu) { |
---|
188 | if (Buff[0]) { |
---|
189 | strcat(Buff, " "); |
---|
190 | strcat(Buff, Cpu); |
---|
191 | } else |
---|
192 | strcpy(Buff, Cpu); |
---|
193 | } |
---|
194 | |
---|
195 | (void) fclose(fp); |
---|
196 | |
---|
197 | return(Buff); |
---|
198 | } |
---|
199 | |
---|
200 | /* |
---|
201 | * Get System Memory using size of /proc/kcore |
---|
202 | */ |
---|
203 | extern char *GetMemoryKcore() |
---|
204 | { |
---|
205 | static char *MemStr = NULL; |
---|
206 | Large_t MemBytes = 0; |
---|
207 | Large_t Amount = 0; |
---|
208 | char Kcore[] = "/proc/kcore"; |
---|
209 | struct stat StatBuf; |
---|
210 | |
---|
211 | if (MemStr) |
---|
212 | return(MemStr); |
---|
213 | |
---|
214 | if (stat(Kcore, &StatBuf) != 0) { |
---|
215 | SImsg(SIM_GERR, "%s: stat failed: %s", Kcore, SYSERR); |
---|
216 | return((char *) NULL); |
---|
217 | } |
---|
218 | |
---|
219 | MemBytes = (Large_t) (StatBuf.st_size - 4096); |
---|
220 | Amount = DivRndUp(MemBytes, (Large_t)MBYTES); |
---|
221 | MemStr = GetMemoryStr(Amount); |
---|
222 | |
---|
223 | return(MemStr); |
---|
224 | } |
---|
225 | |
---|
226 | /* |
---|
227 | * Get System Memory using sysinfo() system call |
---|
228 | */ |
---|
229 | extern char *GetMemorySysinfo() |
---|
230 | { |
---|
231 | struct sysinfo si; |
---|
232 | static char *MemStr = NULL; |
---|
233 | Large_t MemBytes = 0; |
---|
234 | Large_t Amount = 0; |
---|
235 | |
---|
236 | if (MemStr) |
---|
237 | return(MemStr); |
---|
238 | |
---|
239 | if (sysinfo(&si) != 0) { |
---|
240 | SImsg(SIM_GERR, "sysinfo() system call failed: %s", SYSERR); |
---|
241 | return((char *) NULL); |
---|
242 | } |
---|
243 | |
---|
244 | /* |
---|
245 | * sysinfo.totalram represents total USABLE physical memory. Memory |
---|
246 | * reserved by the kernel is not included. So this is as close as we |
---|
247 | * can get for now. Sigh! |
---|
248 | */ |
---|
249 | MemBytes = (Large_t) si.totalram; |
---|
250 | Amount = DivRndUp(MemBytes, (Large_t)MBYTES); |
---|
251 | MemStr = GetMemoryStr(Amount); |
---|
252 | |
---|
253 | return(MemStr); |
---|
254 | } |
---|
255 | |
---|
256 | /* |
---|
257 | * Get Virtual Memory using sysinfo() system call |
---|
258 | */ |
---|
259 | extern char *GetVirtMemLinux() |
---|
260 | { |
---|
261 | struct sysinfo si; |
---|
262 | static char *MemStr = NULL; |
---|
263 | Large_t MemBytes = 0; |
---|
264 | Large_t Amount = 0; |
---|
265 | |
---|
266 | if (MemStr) |
---|
267 | return(MemStr); |
---|
268 | |
---|
269 | if (sysinfo(&si) != 0) { |
---|
270 | SImsg(SIM_GERR, "sysinfo() system call failed: %s", SYSERR); |
---|
271 | return((char *) NULL); |
---|
272 | } |
---|
273 | |
---|
274 | MemBytes = (Large_t) (si.totalram + si.totalswap); |
---|
275 | Amount = DivRndUp(MemBytes, (Large_t)MBYTES); |
---|
276 | MemStr = GetMemoryStr(Amount); |
---|
277 | |
---|
278 | return(MemStr); |
---|
279 | } |
---|
280 | |
---|
281 | /* |
---|
282 | * Get CPU Type from /proc/cpuinfo |
---|
283 | */ |
---|
284 | extern char *GetCpuTypeProc() |
---|
285 | { |
---|
286 | FILE *fp; |
---|
287 | static char Buff[256]; |
---|
288 | static char *Cpu = NULL; |
---|
289 | char **Argv; |
---|
290 | int Argc; |
---|
291 | |
---|
292 | if (Cpu) |
---|
293 | return(Cpu); |
---|
294 | |
---|
295 | fp = fopen(PROC_FILE_CPUINFO, "r"); |
---|
296 | if (!fp) { |
---|
297 | SImsg(SIM_GERR, "%s: open readonly failed: %s", |
---|
298 | PROC_FILE_CPUINFO, SYSERR); |
---|
299 | return((char *) NULL); |
---|
300 | } |
---|
301 | |
---|
302 | while (fgets(Buff, sizeof(Buff), fp)) { |
---|
303 | Argc = StrToArgv(Buff, ":", &Argv, NULL, 0); |
---|
304 | if (Argc < 2) |
---|
305 | continue; |
---|
306 | if (EQ(Argv[0], "cpu")) { |
---|
307 | Cpu = Argv[1]; |
---|
308 | break; |
---|
309 | } |
---|
310 | } |
---|
311 | |
---|
312 | (void) fclose(fp); |
---|
313 | |
---|
314 | return(Cpu); |
---|
315 | |
---|
316 | } |
---|
317 | |
---|
318 | /* |
---|
319 | * Get Kernel Version string using /proc/version |
---|
320 | */ |
---|
321 | extern char *GetKernVerProc() |
---|
322 | { |
---|
323 | FILE *fp; |
---|
324 | static char Buff[512]; |
---|
325 | char **Argv; |
---|
326 | int Argc; |
---|
327 | |
---|
328 | if (Buff[0]) |
---|
329 | return(Buff); |
---|
330 | |
---|
331 | fp = fopen(PROC_FILE_VERSION, "r"); |
---|
332 | if (!fp) { |
---|
333 | SImsg(SIM_GERR, "%s: open readonly failed: %s", |
---|
334 | PROC_FILE_VERSION, SYSERR); |
---|
335 | return((char *) NULL); |
---|
336 | } |
---|
337 | |
---|
338 | if (!fgets(Buff, sizeof(Buff), fp)) { |
---|
339 | SImsg(SIM_GERR, "%s: read failed: %s", PROC_FILE_VERSION, SYSERR); |
---|
340 | return((char *) NULL); |
---|
341 | } |
---|
342 | |
---|
343 | (void) fclose(fp); |
---|
344 | |
---|
345 | return(Buff); |
---|
346 | |
---|
347 | } |
---|
348 | |
---|
349 | /* |
---|
350 | * Get Kernel Version string using uname() |
---|
351 | */ |
---|
352 | extern char *GetKernVerLinux() |
---|
353 | { |
---|
354 | static struct utsname uts; |
---|
355 | static char *VerStr = NULL; |
---|
356 | |
---|
357 | if (uname(&uts) != 0) { |
---|
358 | SImsg(SIM_GERR, "uname() system call failed: %s", SYSERR); |
---|
359 | return((char *) NULL); |
---|
360 | } |
---|
361 | |
---|
362 | VerStr = uts.version; |
---|
363 | |
---|
364 | return(VerStr); |
---|
365 | } |
---|
366 | |
---|
367 | /* |
---|
368 | * What distribution (vendor) of Linux are we? |
---|
369 | * This code works on S.u.S.E. Linux. Don't know about others. |
---|
370 | */ |
---|
371 | extern char *GetOSDistLinux() |
---|
372 | { |
---|
373 | static char Buff[256]; |
---|
374 | register char *cp; |
---|
375 | register char *End; |
---|
376 | char IssueFile[] = "/etc/issue"; |
---|
377 | char Welcome[] = "Welcome to "; |
---|
378 | FILE *fp; |
---|
379 | int Found = FALSE; |
---|
380 | |
---|
381 | if (!(fp = fopen(IssueFile, "r"))) { |
---|
382 | SImsg(SIM_GERR, "%s: Cannot open to get OS Dist: %s", |
---|
383 | IssueFile, SYSERR); |
---|
384 | return((char *) NULL); |
---|
385 | } |
---|
386 | |
---|
387 | while (fgets(Buff, sizeof(Buff), fp)) { |
---|
388 | for (cp = Buff; cp && *cp && *cp != '\n' && !isalpha(*cp); ++cp); |
---|
389 | if (*cp == '\n' || !strlen(cp)) |
---|
390 | continue; |
---|
391 | /* Found first nonblank line */ |
---|
392 | Found = TRUE; |
---|
393 | break; |
---|
394 | } |
---|
395 | |
---|
396 | (void) fclose(fp); |
---|
397 | |
---|
398 | if (!Found) |
---|
399 | return((char *) NULL); |
---|
400 | |
---|
401 | if (EQN(cp, Welcome, sizeof(Welcome)-1)) |
---|
402 | cp += sizeof(Welcome)-1; |
---|
403 | |
---|
404 | if (End = strchr(cp, '-')) { |
---|
405 | --End; |
---|
406 | while (*End && isspace(*End)) |
---|
407 | --End; |
---|
408 | *++End = CNULL; |
---|
409 | } |
---|
410 | |
---|
411 | return(cp); |
---|
412 | } |
---|