Files
rmps/agent/job.c

156 lines
3.4 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include "job.h"
#include <sys/utsname.h>
#include <sys/sysinfo.h>
#include <pthread.h>
struct job_t {
long id;
long parent_id;
int type;
char code[8192];
char std_out[8192];
char std_err[8192];
short ret_code;
};
static void gen_fname_hash(char *s, const int len)
{
static const char alphanum[] =
"0123456789"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";
int i;
for (i = 0; i < len; ++i)
s[i] = alphanum[rand() % (sizeof(alphanum) - 1)];
s[len] = 0;
}
void* exec_unix(void *args)
{
struct job_args *job = args;
return 0;
}
void* install_pkg(void *args)
{
struct job_args *job = args;
return 0;
}
void* query_pkg(void *args)
{
struct job_args *job = args;
return 0;
}
void* delete_pkg(void *args)
{
struct job_args *job = args;
return 0;
}
void* list_pkgs(void *args)
{
struct job_args *job = args;
return 0;
}
void* update_pkg(void *args)
{
struct job_args *job = args;
return 0;
}
void* update_pkgs(void *args)
{
struct job_args *job = args;
return 0;
}
void* get_os(void *args)
{
struct job_args *job = args;
char cmd[40], dump_path[14];
int fd;
sprintf(dump_path, "/tmp/");
gen_fname_hash(&dump_path[5], 8);
sprintf(cmd, "sh detect-unix.sh > %s", dump_path);
system(cmd); /* TODO: set proper path */
if ((fd = open(dump_path, O_RDONLY)) == -1) {
fprintf(stderr, "Cannot open file: %s\n", dump_path);
job->buf.meta.len = sprintf((char*)job->buf.chunk.data, "Unknown");
SSL_write(job->ssl, &job->buf, sizeof(struct msg_t));
} else {
job->buf.meta.len = read(fd, job->buf.chunk.data, sizeof(job->buf.chunk.data));
if ((signed)job->buf.meta.len == -1) {
perror("Failed to read from file: ");
}
close(fd);
SSL_write(job->ssl, &job->buf, sizeof(struct msg_t));
job->slot = FREE;
}
unlink(dump_path);
return 0;
}
void* get_kernel(void *args)
{
struct utsname name;
struct job_args *job = args;
if (uname(&name) == -1) {
job->buf.meta.len = sprintf((char*)job->buf.chunk.data, "Unknown");
SSL_write(job->ssl, &job->buf, sizeof(struct msg_t));
} else {
job->buf.meta.len = sprintf( (char*)job->buf.chunk.data,
"%s %s %s %s %s",
name.sysname,
name.nodename,
name.release,
name.version,
name.machine );
SSL_write(job->ssl, &job->buf, sizeof(struct msg_t));
}
job->slot = FREE;
return 0;
}
void* get_uptime(void *args)
{
struct job_args *job = args;
struct sysinfo sys;
if (sysinfo(&sys) != 0) {
job->buf.meta.len = sprintf((char*)job->buf.chunk.data, "Unknown");
SSL_write(job->ssl, &job->buf, sizeof(struct msg_t));
} else {
memcpy(&job->buf.chunk.data, &sys.uptime, sizeof(long));
//sprintf((char*)&job->buf.chunk.data, "%ld", sys.uptime);
SSL_write(job->ssl, &job->buf, sizeof(struct msg_t));
}
job->slot = FREE;
return 0;
}
void* get_memory(void *args)
{
long pages, pagesize, freepages;
struct job_args *job = args;
pages = sysconf(_SC_PHYS_PAGES);
pagesize = sysconf (_SC_PAGESIZE);
freepages = sysconf(_SC_AVPHYS_PAGES);
job->buf.meta.len = sprintf( (char*)job->buf.chunk.data,
"%ld / %ld (MB)",
((pages - freepages) * pagesize) / 1048576 /* 1024*1024 */,
(pages * pagesize) / 1048576 );
SSL_write(job->ssl, &job->buf, sizeof(struct msg_t));
job->slot = FREE;
//return 0;
pthread_detach(pthread_self());
pthread_exit(NULL);
}