-
Notifications
You must be signed in to change notification settings - Fork 0
/
systeminfo.c
381 lines (305 loc) · 10.1 KB
/
systeminfo.c
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/*******************************************************************
* Project: SystemInfo
*
* Author: Kfiros (Kfir Shtober)
* Year: 2015
*
* File: systeminfo.c
* Description: SystemInfo is a simple but useful cli utility,
* which targets at providing information about the
* linux system it runs upon. The idea is based on the
* systeminfo.exe binary, which appears on Windows
* systems.
*******************************************************************/
/*******************************************************************
* Includes
*******************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/user.h>
#include <sys/stat.h>
#include <sys/utsname.h>
#include <sys/sysinfo.h>
#include <sys/socket.h>
#include <sys/statvfs.h>
#include <sys/time.h>
#include <utmp.h>
#include <netdb.h>
#include <ifaddrs.h>
#include <linux/if_link.h>
#include <time.h>
#include "systeminfo.h"
/*******************************************************************
* Name: get_kernel_info()
* Description: This function prints information about the currently
* used linux kernel.
*******************************************************************/
static void get_kernel_info() {
int call_rv;
struct utsname uname_info;
/* Get name and information about current kernel */
call_rv = uname(&uname_info);
if (SUCCESS != call_rv) {
return;
}
fprintf(stdout, "%-30s%s\n", "Kernel Version:", uname_info.release);
fprintf(stdout, "%-30s%s\n", "Compilation Info:", uname_info.version);
fprintf(stdout, "%-30s%s\n", "System Type:", uname_info.machine);
fprintf(stdout, "%-30s%s\n", "Hostname:", uname_info.nodename);
}
/*******************************************************************
* Name: get_os_release_info()
* Description: This function prints information about the OS
* release.
*******************************************************************/
static void get_os_release_info() {
int call_rv;
FILE * os_release_fd = NULL;
char line[MAX_FILE_LINE] = {0};
char var[MAX_FILE_VAR] = {0};
char val[MAX_FILE_VAL] = {0};
char * token = NULL;
/* First obtain information saved in os-release file */
os_release_fd = fopen(OS_RELEASE_PATH, "rb");
if (NULL == os_release_fd) {
return;
}
/* Iterate over the lines in os-release file */
while (NULL != fgets(line, MAX_FILE_LINE, os_release_fd)) {
/* Parse current line */
call_rv = sscanf(line, "%[^=]=%[^\n]", var, val);
if (OS_RELEASE_LINE_CELLS_COUNT != call_rv) {
continue;
}
call_rv = strncmp(var, OS_RELEASE_NAME, MAX_FILE_VAR);
if (0 == call_rv) {
token = strtok(val, "\"");
if (NULL != token) {
fprintf(stdout, "%-30s%s\n", "OS Name:", token);
}
}
call_rv = strncmp(var, OS_RELEASE_VERSION, MAX_FILE_VAR);
if (0 == call_rv) {
token = strtok(val, "\"");
if (NULL != token) {
fprintf(stdout, "%-30s%s\n", "OS Version:", token);
}
}
}
if (NULL != os_release_fd) {
fclose(os_release_fd);
}
}
/*******************************************************************
* Name: get_os_info
* Description: This function obtains information related to the
* running operating system.
*******************************************************************/
static void get_os_info() {
/* Get information about the OS release */
get_os_release_info();
/* Get information about current kernel */
get_kernel_info();
}
/*******************************************************************
* Name: get_uptime
* Description: This function prints the system's uptime.
*******************************************************************/
static void get_uptime(long uptime) {
unsigned int days, hours, minutes;
/* Get system's uptime */
days = uptime / (60 * 60 * 24);
hours = (uptime / 60 / 60) % 24;
minutes = (uptime / 60) % 60;
fprintf(stdout, "%-30sUp %d Days, %d Hours, %d Minutes \n", "Uptime:",
days, hours, minutes);
}
/*******************************************************************
* Name: get_boot_time
* Description: This function prints the system's boot time.
*******************************************************************/
static void get_boot_time() {
FILE * stat_fd = NULL;
time_t t_boot;
char line[MAX_FILE_LINE] = {0};
char * token = NULL;
/* Get system's boot time from /proc/stat */
stat_fd = fopen(STAT_PATH, "rb");
if (NULL == stat_fd) {
return;
}
/* Iterate over the lines in /proc/stat file */
while (NULL != fgets(line, MAX_FILE_LINE, stat_fd)) {
token = strtok(line, SPACE_SEP);
if (NULL == token) {
continue;
}
if (0 == strcmp(token, STAT_BTIME)) {
token = strtok(NULL, SPACE_SEP);
break;
}
}
if (NULL == token) {
goto cleanup;
}
t_boot = atoi(token);
fprintf(stdout, "%-30s%s", "System Boot Time:", ctime(&t_boot));
cleanup:
if (NULL != stat_fd) {
fclose(stat_fd);
}
return;
}
/*******************************************************************
* Name: get_free_space
* Description: Calculates the ammount of available space in the
* fs of the root directory.
*******************************************************************/
static void get_free_space() {
int call_rv;
struct statvfs stats = {0};
size_t free_space;
/* Get filesystem statistics */
call_rv = statvfs(ROOT_DIR, &stats);
if (SUCCESS != call_rv) {
return;
}
/* Calculate the free space in MBs */
free_space = (stats.f_bsize * stats.f_bavail) / (MB_SIZE);
fprintf(stdout, "%-30s%zu MB\n", "Free Space:", free_space);
}
/*******************************************************************
* Name: get_sys_stats
* Description: This function obtains general statistical information
* about the system (Current date & time, uptime, free
* ram, and so on)...
*******************************************************************/
void get_sys_stats() {
int call_rv;
struct sysinfo info = {0};
time_t t_now;
/* Get free space in the fs of the root directory */
get_free_space();
/* Get current date and time */
t_now = time(NULL);
fprintf(stdout, "%-30s%s", "Date:", ctime(&t_now));
/* Get system's boot time */
get_boot_time();
/* Get overall system statistics */
call_rv = sysinfo(&info);
if (SUCCESS != call_rv) {
return;
}
/* Get system's uptime */
get_uptime(info.uptime);
fprintf(stdout, "%-30s%d MB\n", "Total RAM:", (unsigned int) (info.totalram / 1024) );
fprintf(stdout, "%-30s%d MB\n", "Total Available RAM:", (unsigned int) info.freeram / 1024 );
fprintf(stdout, "%-30s%d\n", "Number Of Processes:", info.procs );
}
/*******************************************************************
* Name: test_cpuinfo_line
* Description: This function tests for known information in a given
* cpuinfo line.
*******************************************************************/
static inline void test_cpuinfo_line(char * var, char * val) {
if (0 == strncmp(var, CPUINFO_MODEL_NAME, MAX_FILE_VAR)) {
fprintf(stdout, "%-30s%s\n", "Processor Model:", val);
} else if (0 == strncmp(var, CPUINFO_VENDOR_ID, MAX_FILE_VAR)) {
fprintf(stdout, "%-30s%s\n", "Vendor ID:", val);
} else if (0 == strncmp(var, CPUINFO_CPU_FAMILY, MAX_FILE_VAR)) {
fprintf(stdout, "%-30s%s\n", "CPU Family:", val);
} else if (0 == strncmp(var, CPUINFO_STEPPING, MAX_FILE_VAR)) {
fprintf(stdout, "%-30s%s\n", "CPU Stepping:", val);
} else if (0 == strncmp(var, CPUINFO_CPU_CORES, MAX_FILE_VAR)) {
fprintf(stdout, "%-30s%s\n", "CPU Cores:", val);
} else if (0 == strncmp(var, CPUINFO_CPU_MHZ, MAX_FILE_VAR)) {
fprintf(stdout, "%-30s%s\n", "CPU MHz:", val);
}
}
/*******************************************************************
* Name: get_cpu_info
* Description: This function obtains important details about the
* CPU and prints it.
*******************************************************************/
static void get_cpu_info() {
int call_rv;
FILE * cpuinfo_fd = NULL;
char line[MAX_FILE_LINE] = {0};
char var[MAX_FILE_VAR] = {0};
char val[MAX_FILE_VAL] = {0};
cpuinfo_fd = fopen(CPUINFO_PATH, "rb");
if (NULL == cpuinfo_fd) {
return;
}
/* Iterate over the lines in cpuinfo file */
while(NULL != fgets(line, MAX_FILE_LINE, cpuinfo_fd)) {
/* Parse given line in the cpuinfo file */
call_rv = sscanf(line, "%[^\t]\t: %[^\n]", var, val);
if (CPUINFO_LINE_CELLS_COUNT != call_rv) {
continue;
}
test_cpuinfo_line(var, val);
}
if (NULL != cpuinfo_fd) {
fclose(cpuinfo_fd);
}
return;
}
/*******************************************************************
* Name: get_ip_addrs
* Description: Obtains the IP addresses assigned to the system's
network interfaces.
*******************************************************************/
void get_ip_addrs() {
int call_rv;
struct ifaddrs * ifaddr = NULL, * ifa = NULL;
int family;
char host[NI_MAXHOST] = {0};
/* Get interface addresses */
call_rv = getifaddrs(&ifaddr);
if (SUCCESS != call_rv) {
return;
}
fprintf(stdout, "%-30s\n", "Interfaces:");
/* Iterating over the addresses */
for (ifa = ifaddr; NULL != ifa; ifa = ifa->ifa_next) {
if (NULL == ifa->ifa_addr) {
continue;
}
family = ifa->ifa_addr->sa_family;
/* Check for IPv4 & IPv6 addresses only */
if (AF_INET == family || AF_INET6 == family){
/* Get appropriate IP address */
call_rv = getnameinfo(ifa->ifa_addr,
(AF_INET == family) ? sizeof(struct sockaddr_in) :
sizeof(struct sockaddr_in6),
host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
if (SUCCESS != call_rv) {
continue;
}
fprintf(stdout, "%-30s%s (%s) %s\n", "", ifa->ifa_name,
(AF_INET == family) ? "IPv4" : "IPv6", host);
}
}
/* Free getifaddrs' allocated data */
freeifaddrs(ifaddr);
}
/*******************************************************************
* Name: main
* Description: Main function of the program
*******************************************************************/
int main(int argc, char * argv[]) {
/* Eliminate the `unused` warnings */
UNUSED(argc);
UNUSED(argv);
fprintf(stdout, "[*] SystemInfo (Kfiros 2015)\n");
fprintf(stdout, "[*] Obtaining system information... \n");
/* Obtain system information. On any error - continue... */
get_os_info();
get_sys_stats();
get_cpu_info();
get_ip_addrs();
return SUCCESS;
}