Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/jsonrpc-c.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ struct jrpc_procedure {
char * name;
jrpc_function function;
void *data;
int allow:1;
};

struct jrpc_server {
Expand Down
48 changes: 46 additions & 2 deletions src/jsonrpc-c.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/wait.h>

#include "jsonrpc-c.h"

Expand All @@ -32,10 +33,17 @@ static void *get_in_addr(struct sockaddr *sa) {
}

static int send_response(struct jrpc_connection * conn, char *response) {
int l, len, send;
int fd = conn->fd;
if (conn->debug_level > 1)
printf("JSON Response:\n%s\n", response);
write(fd, response, strlen(response));

for (send = 0, len = strlen(response); send < len; send += l) {
l = write(fd, response+send, len - send);
if (l < 0)
break;
}

write(fd, "\n", 1);
return 0;
}
Expand Down Expand Up @@ -72,6 +80,39 @@ static int send_result(struct jrpc_connection * conn, cJSON * result,
return return_value;
}

static int selftest_procedure(struct jrpc_server *server) {
pid_t pid;

int i = server->procedure_count;
while (i--) {
cJSON *fake = cJSON_CreateObject();
jrpc_context ctx;
ctx.error_code = 0;
ctx.error_message = NULL;
ctx.data = server->procedures[i].data;
jrpc_function function = server->procedures[i].function;
char *name = server->procedures[i].name;

pid = fork();
if (pid < 0)
fprintf(stderr, "selftest: Fork failed!\n");
else if (pid == 0) {
printf("selftest:%s\n", name); //WARNING!!!: if delete this print, the selftest will crash!
function(&ctx, fake, fake);

exit(0);
} else {
int stat_loc;

if (waitpid(pid, &stat_loc, 0) != pid)
fprintf(stderr, "selftest: Wait pid(%d) error\n", pid);

if (!stat_loc)
server->procedures[i].allow = 1;
}
}
}

static int invoke_procedure(struct jrpc_server *server,
struct jrpc_connection * conn, char *name, cJSON *params, cJSON *id) {
cJSON *returned = NULL;
Expand All @@ -81,7 +122,8 @@ static int invoke_procedure(struct jrpc_server *server,
ctx.error_message = NULL;
int i = server->procedure_count;
while (i--) {
if (!strcmp(server->procedures[i].name, name)) {
if (!strcmp(server->procedures[i].name, name) &&
server->procedures[i].allow) {
procedure_found = 1;
ctx.data = server->procedures[i].data;
returned = server->procedures[i].function(&ctx, params, id);
Expand Down Expand Up @@ -204,6 +246,7 @@ static void connection_cb(struct ev_loop *loop, ev_io *w, int revents) {
}
}

return close_connection(loop, w);
}

static void accept_cb(struct ev_loop *loop, ev_io *w, int revents) {
Expand Down Expand Up @@ -341,6 +384,7 @@ static int __jrpc_server_start(struct jrpc_server *server) {
#endif

void jrpc_server_run(struct jrpc_server *server){
selftest_procedure(server);
EV_RUN(server->loop, 0);
}

Expand Down
1 change: 1 addition & 0 deletions src/modules/sysstat-lite/src/ioconf.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#else
#define _(string) (string)
#endif
#include <sys/sysmacros.h>

static unsigned int ioc_parsed = 0;
static struct ioc_entry *ioconf[MAX_BLKDEV + 1];
Expand Down
50 changes: 38 additions & 12 deletions src/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ static int debug; /* enable this to printf */
} while(0)

#define PORT 12307 // the port users will be connecting to
#define PROC_BUFF 8192
#define PROC_BUFF (1024 * 512) //8192
unsigned char proc_buff[PROC_BUFF];

#define CMD_BUFF 16384
#define CMD_BUFF (1024 * 1024) //163840
#define PERF_CMD_BUFF (1024 * 1024 * 10) //163840
unsigned char cmd_buff[CMD_BUFF];

struct jrpc_server my_server;
Expand Down Expand Up @@ -226,19 +227,30 @@ cJSON * run_cmd(jrpc_context * ctx, cJSON * params, cJSON *id)
{
FILE *fp;
int size;
unsigned char *buffer;
cJSON *ret;

if (!ctx->data)
return NULL;

fp = popen(ctx->data, "r");
if (fp) {
memset(cmd_buff, 0, CMD_BUFF);
size = fread(cmd_buff, 1, CMD_BUFF - strlen(endstring) - 1 , fp);
buffer = (unsigned char *)malloc(CMD_BUFF);
if (buffer == NULL) {
pclose(fp);
return NULL;
}

memset(buffer, 0, CMD_BUFF);
size = fread(buffer, 1, CMD_BUFF - strlen(endstring) - 1 , fp);
DEBUG_PRINT("run_cmd:size %d:%s\n", size, (char *)ctx->data);
pclose(fp);

strcat(cmd_buff, endstring);
return cJSON_CreateString(cmd_buff);
strcat(buffer, endstring);
ret = cJSON_CreateString(buffer);
free(buffer);

return ret;
}
return NULL;
}
Expand Down Expand Up @@ -270,20 +282,31 @@ cJSON * run_perf_script_cmd(jrpc_context * ctx, cJSON * params, cJSON *id)
{
FILE *fp;
int size;
unsigned char *buffer;
cJSON *ret;

if (!ctx->data)
return NULL;
DEBUG_PRINT("run_perf_cmd\n");
system(ctx->data);
fp = popen("perf script", "r");
if (fp) {
memset(cmd_buff, 0, CMD_BUFF);
size = fread(cmd_buff, 1, CMD_BUFF - strlen(endstring) - 1, fp);
buffer = (unsigned char *)malloc(PERF_CMD_BUFF);
if (buffer == NULL) {
pclose(fp);
return NULL;
}

memset(buffer, 0, PERF_CMD_BUFF);
size = fread(buffer, 1, CMD_BUFF - strlen(endstring) - 1, fp);
DEBUG_PRINT("run_cmd:size %d:%s\n", size, (char *)ctx->data);
pclose(fp);

strcat(cmd_buff, endstring);
return cJSON_CreateString(cmd_buff);
strcat(buffer, endstring);
ret = cJSON_CreateString(buffer);
free(buffer);

return ret;
}
return NULL;
}
Expand All @@ -309,10 +332,12 @@ int main(int argc, char **argv)
* so, don't close them; but we want to mute errors
* just like a typical daemon
*/
#if 0
daemon(0, 1);
fd = open ("/dev/null", O_RDWR, 0);
if (fd != -1)
dup2 (fd, STDERR_FILENO);
#endif

jrpc_server_init(&my_server, PORT);
jrpc_register_procedure(&my_server, say_hello, "SayHello", NULL);
Expand All @@ -335,7 +360,7 @@ int main(int argc, char **argv)
/*********************************************
*
* ****************************************/
jrpc_register_procedure(&my_server, run_builtin_cmd, "GetCmdIotop", "iotop");
//jrpc_register_procedure(&my_server, run_builtin_cmd, "GetCmdIotop", "iotop -n 1 -b");
//jrpc_register_procedure(&my_server, run_builtin_cmd, "GetCmdIopp", "iopp");
jrpc_register_procedure(&my_server, run_builtin_cmd, "GetCmdFree", "free");
jrpc_register_procedure(&my_server, run_builtin_cmd, "GetCmdProcrank", "procrank");
Expand All @@ -344,7 +369,8 @@ int main(int argc, char **argv)
//jrpc_register_procedure(&my_server, run_cmd, "GetCmdTop", "top -n 1 -b | head -n 50");
jrpc_register_procedure(&my_server, run_builtin_cmd, "GetCmdTop", "ps -e -o pid,user,pri,ni,vsize,rss,s,%cpu,%mem,time,cmd --sort=-%cpu ");
//jrpc_register_procedure(&my_server, run_cmd, "GetCmdTopH", "top -n 1 -b | head -n 50");
//jrpc_register_procedure(&my_server, run_cmd, "GetCmdIotop", "iotop -n 1 -b | head -n 50");
jrpc_register_procedure(&my_server, run_cmd, "GetCmdIotop", "iotop -n 1 -b | head -n 50");
jrpc_register_procedure(&my_server, run_cmd, "GetCmdJnettop", "jnettop --display text -t 5 --format '$src$,$srcname$,$srcport$,$srcbytes$,$srcpackets$,$srcbps$,$srcpps$,$dst$,$dstname$,$dstport$,$dstbytes$,$dstpackets$,$dstbps$,$dstpps$,$proto$,$totalbytes$,$totalpackets$,$totalbps$,$totalpps$,$filterdata$'");
//jrpc_register_procedure(&my_server, run_cmd, "GetCmdSmem", "smem -p -s pss -r -n 50");
jrpc_register_procedure(&my_server, run_builtin_cmd, "GetCmdDmesg", "dmesg");
jrpc_register_procedure(&my_server, run_builtin_cmd, "GetCmdDf", "df -h");
Expand Down