aboutsummaryrefslogtreecommitdiffstats
path: root/source/main.c
blob: 4fb1dcf73abf2b34461f9cf871d49153339e5b22 (plain) (blame)
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
/*
 * Author: nasr
 * Year: 2025-2026
 * */

#include <unistd.h>
#include <signal.h>
#include <stdint.h> 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <dirent.h>
#include <proc/readproc.h>

// TODO(nasr): create and start virtual machines using KVM - QEMU - LIBVIRT
// #include <libvirt/libvirt.h>

#define MAXC_CHAR 256


typedef struct {

	uint8_t *base; 
	size_t idx;
	size_t cap;

} Arena;

Arena 
*ArenaAlloc(void) 
{

	return NULL;
}

void
*ArenaRelease(Arena *arena) 
{

	return NULL;
}

Arena
*ArenaPush(Arena *arena, size_t bytes)
{

	return NULL;
}

void
ArenaPop(Arena *arena)
{

}

uint64_t
ArenaGetPos(Arena *arena)
{

	return 0;
}

void
ArenaClear(Arena *arena)
{
	// TODO(nasr): check if there is more needed to do here
	free(arena);
}

typedef struct 
{

	char vendor[MAXC_CHAR]; 
	char model[MAXC_CHAR]; 
	char frequency[MAXC_CHAR]; 
	char cores[MAXC_CHAR]; 

} Cpu;

typedef struct 
{
	char total[MAXC_CHAR];
	char free[MAXC_CHAR];
} Ram;

typedef struct 
{
	long size;
	char *name;
} Disk;

typedef struct 
{ 

	char hostname[MAXC_CHAR]; 
	char os_version[MAXC_CHAR];
	char uptime[MAXC_CHAR];
	char **procs;
	int	 procs_count;

} Device;


// math heper functions
// @param takes a base and an exponent
int
ipow(int base , int exp ) 
{

	uint64_t result = 0;
	for (int i = 0; i < exp; ++i) {

		result *= base;
	}

	return result;

	return 0;
}

int
fpow(double base, double exp) 
{

	float result = 0;

	for (int i = 0; i < exp; ++i) {

		result *= base;
	}

	return result;
}


void
cpu_data(Cpu *cpu) 
{
	char basic_cpu_info[]	= "/proc/cpuinfo";

	char vendor_id[]		= { "vendor_id"};
	char model[]			= { "model name"};
	char frequency[]		= { "cpu MHz"};
	char cores[]			= { "cpu cores"};

	FILE *file = fopen(basic_cpu_info, "r");
	if (!file)
	{
		//TODO(nasr): write this to the created agent log system
		return;
	}

	char buffer[MAXC_CHAR] = "";
	while(fgets(buffer, sizeof(buffer), file) != NULL)
	{
		char *colon = strchr(buffer, ':');
		if (!colon)
			continue; 

		char *start = colon + 1;
		 
		while (*start == ' ')
			start++;

		char *end = strchr(start, '\n');
   
		if (!end)
			end = start + strlen(start);

		size_t length = (size_t)(end - start);

		if ((strncmp(buffer, vendor_id, sizeof(vendor_id) - 1)) == 0) 
		{
			memcpy(cpu->vendor, start, length);
			cpu->vendor[length] = '\0';
		}

		if ((strncmp(buffer, model, sizeof(model) - 1)) == 0) 
		{
			memcpy(cpu->model, start, length);
			cpu->model[length] = '\0';
		}

		if ((strncmp(buffer, frequency , sizeof(frequency) - 1)) == 0) 
		{
			memcpy(cpu->frequency, start, length);
			cpu->frequency[length] = '\0';
		}

		if ((strncmp(buffer, cores, sizeof(cores) - 1)) == 0) 
		{
			memcpy(cpu->cores, start, length);
			cpu->cores[length] = '\0';
		}
	}

	fclose(file);
}

void
memory_data(Ram *ram) 
{

	char mem_info[]	= "/proc/meminfo";

	char total[]		= { "MemTotal"};
	char free[]			= { "MemFree"};

	FILE *file = fopen(mem_info, "r");
	if (!file)
	{
		//TODO(nasr): write this to the created agent log system
		fclose(file);
		return;
	}

	char buffer[MAXC_CHAR] = "";
	while(fgets(buffer, sizeof(buffer), file) != NULL)
	{
		char *colon = strchr(buffer, ':');
		if (!colon)
			continue; 
 
		char *start = colon + 1;
		 
		while (*start == ' ')
			start++;

		char *end = strchr(start, 'k');

		if (!end)
			end = start + strlen(start);

		size_t length = (size_t)(end - start);


		if ((strncmp(buffer, total, sizeof(total) - 1)) == 0) 
		{
			memcpy(ram->total, start, length);
			ram->total[length] = '\0';
		}

		if ((strncmp(buffer, free, sizeof(free) - 1)) == 0) 
		{
			memcpy(ram->free, start, length);
			ram->free[length] = '\0';
		}

	}
	fclose(file);
}

void
disk_data(Disk *disk) 
{
	// TODO(nasr): get the partitions names, get the paritions sizes
	// TODO(nasr): rewrite the disk struct
	char disk_info[]	= "/proc/partitions";
	FILE *file = fopen(disk_info, "r");
	if (!file)
	{
		fclose(file);
		return;
	}

	fclose(file);
}

// own impementation
void
get_processes(Device *device)
{
    const char *path = "/proc";
    struct dirent *entry;

    size_t pid_count = 0;
    size_t pid_cap   = 8;

    DIR *dP = opendir(path);
    if (!dP) {
        perror("failed to open /proc");
        return;
    }

    char **pid_list = (char**)malloc(sizeof(char*) * pid_cap);
    if (!pid_list) {
        perror("malloc failed");
        closedir(dP);
        return;
    }

    while ((entry = readdir(dP)) != NULL) {
        if (atoi(entry->d_name) == 0)
            continue;

        if (pid_count == pid_cap) {
            pid_cap *= 2;
            char **tmp = (char**)realloc(pid_list, sizeof(char*) * pid_cap);
            if (!tmp) {
				free(pid_list);
                perror("realloc failed");
                break;
            }
            pid_list = tmp;
        }

        size_t len = strlen(entry->d_name) + 1;
        pid_list[pid_count] = (char*)malloc(len);
        if (!pid_list[pid_count]) {
            perror("malloc failed");
            break;
        }

        memcpy(pid_list[pid_count], entry->d_name, len);
        pid_count++;
    }

    closedir(dP);

    size_t valid_count = 0;
    device->procs = (char**)malloc(sizeof(char*) * pid_count);
    if (!device->procs) {
        perror("malloc failed");
    }

    for (size_t i = 0; i < pid_count; i++) {
        char sub_path[64];

        snprintf(sub_path, sizeof(sub_path), "/proc/%s", pid_list[i]);

        DIR *sdP = opendir(sub_path);
        if (!sdP)
            continue;

        closedir(sdP);

        device->procs[valid_count++] = pid_list[i];
    }
	device->procs_count = valid_count;
}


void
device_data(Device *device) 
{

	const char *uptime_path = "/proc/uptime";
	const char *os_version_path = "/proc/version";

	FILE *uptime_file = fopen(uptime_path, "r");
	if (!uptime_file)
		return;

	FILE *os_version_file = fopen(os_version_path, "r");
	if (!os_version_file)
		return;



	char os_version_content[MAXC_CHAR]	= "";
	char uptime_content[MAXC_CHAR]		= "";

	fgets(os_version_content, MAXC_CHAR, os_version_file);
	fgets(uptime_content,MAXC_CHAR, uptime_file); 

	memcpy(device->uptime, uptime_content, sizeof(uptime_content));
	memcpy(device->os_version, os_version_content ,sizeof(os_version_content));

	fclose(uptime_file);
	fclose(os_version_file);


	get_processes(device);
}

// Library
int
list_processes()
{
    PROCTAB *proc = openproc(PROC_FILLSTAT | PROC_FILLSTATUS);
    proc_t proc_info;

    if (!proc) {
        fprintf(stderr, "failed to open proc table\n");
        return 1;
    }
	proc_t *p;
	while ((p = readproc(proc, NULL)) != NULL) {
		printf("PID: %d CMD: %s STATE: %c\n",
				p->tid,
				p->cmd,
				p->state);
	}
	// iterate all processes
    closeproc(proc);
	return 0;
}

int
terminate_process(char *pid)
{
	pid_t p ;

	if (kill(p, SIGTERM) == -1)
	{
		perror("failed to kill pid: %s", pid);
		return 1;
	}
	return 0;

}

int 
main() 
{
	ezway();
	// Arena *arena; 
	//
	Cpu *cpu;
	Ram *ram;
	Device *device;
	Disk *disk;

	// ArenaPush(arena, sizeof(Cpu));
	// ArenaPush(arena, sizeof(Ram));
	// ArenaPush(arena, sizeof(Disk));
	// ArenaPush(arena, sizeof(Device));
	//
	// ArenaAlloc();

	cpu = (Cpu*)malloc(sizeof(Cpu));
	ram = (Ram*)malloc(sizeof(Ram));
	device = (Device*)malloc(sizeof(Device));
	disk = (Disk*)malloc(sizeof(Disk));

	cpu_data(cpu);
	memory_data(ram);
	device_data(device);
	disk_data(disk);

	printf("================================================================");
	printf("\nmodel: %s\n", cpu->model);
	printf("\nvendor: %s\n", cpu->vendor);
	printf("\nfrequency: %s\n", cpu->frequency);
	printf("\ncores: %s\n", cpu->cores);


	printf("================================================================");


	printf("\ntotal: %s\n", ram->total);
	printf("\nfree: %s\n", ram->free);

	printf("================================================================\n");
	
	printf("os_version: %s\n", device->os_version);
	printf("uptime: %s\n", device->uptime);


	printf("================================================================\n");

	for (int i = 0; i < device->procs_count; i++)
	{

		printf("procs: %s\n", device->procs[i]);
	}

	for (int i = 0; i < device->procs_count; i++) {
		free(device->procs[i]);
	}
	free(device->procs);

	free(cpu);
	free(ram);
	free(device);
	free(disk);

	// ArenaRelease(arena);
	return 0;
}

// TODO(nasr): find a way to use libvirt to create and start a virtual machine
// TODO(nasr): find a way to pass parameters to libvirt

void
create_vm()
{

	// TODO(nasr): create and start virtual machines using libirt

}