Merge pull request #319 from Stormalong/fix_scheduler_web_api

Fix crash when trying to set schedule via web api
pull/356/head
sfeakes 2024-07-04 08:45:39 -05:00 committed by GitHub
commit a54f55c60b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 21 additions and 27 deletions

View File

@ -60,7 +60,7 @@ bool remount_root_ro(bool readonly) {
}
}
bool passJson_scObj(char* line, int length, aqs_cron *values)
bool passJson_scObj(const char* line, int length, aqs_cron *values)
{
int keystart=0;
//int keyend=0;
@ -130,9 +130,8 @@ bool passJson_scObj(char* line, int length, aqs_cron *values)
return (captured >= 7)?true:false;
}
int save_schedules_js(char* inBuf, int inSize, char* outBuf, int outSize)
int save_schedules_js(const char* inBuf, int inSize, char* outBuf, int outSize)
{
int length=0;
FILE *fp;
int i;
bool inarray = false;
@ -141,8 +140,7 @@ int save_schedules_js(char* inBuf, int inSize, char* outBuf, int outSize)
if ( !_aqconfig_.enable_scheduler) {
LOG(SCHD_LOG,LOG_WARNING, "Schedules are disabled\n");
length += sprintf(outBuf, "{\"message\":\"Error Schedules disabled\"}");
return length;
return sprintf(outBuf, "{\"message\":\"Error Schedules disabled\"}");
}
LOG(SCHD_LOG,LOG_NOTICE, "Saving Schedule:\n");
@ -154,16 +152,14 @@ int save_schedules_js(char* inBuf, int inSize, char* outBuf, int outSize)
if (fp == NULL) {
LOG(SCHD_LOG,LOG_ERR, "Open file failed '%s'\n", CRON_FILE);
remount_root_ro(true);
length += sprintf(outBuf, "{\"message\":\"Error Saving Schedules\"}");
return length;
return sprintf(outBuf, "{\"message\":\"Error Saving Schedules\"}");
}
fprintf(fp, "#***** AUTO GENERATED DO NOT EDIT *****\n");
fprintf(fp, "PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin\n");
LOG(SCHD_LOG,LOG_DEBUG, "Schedules Message body:\n'%.*s'\n", inSize, inBuf);
length += sprintf(outBuf, "{\"message\":\"Saved Schedules\"}");
for (i=0; i < inSize; i++) {
if ( inBuf[i] == '[' ) {
inarray=true;
@ -174,24 +170,20 @@ int save_schedules_js(char* inBuf, int inSize, char* outBuf, int outSize)
LOG(SCHD_LOG,LOG_DEBUG, "Write to cron Min:%s Hour:%s DayM:%s Month:%s DayW:%s URL:%s Value:%s\n",cline.minute,cline.hour,cline.daym,cline.month,cline.dayw,cline.url,cline.value);
LOG(SCHD_LOG,LOG_INFO, "%s%s %s %s %s %s curl -s -S --show-error -o /dev/null localhost:%s%s -d value=%s -X PUT\n",(cline.enabled?"":"#"),cline.minute, cline.hour, cline.daym, cline.month, cline.dayw, _aqconfig_.socket_port, cline.url, cline.value);
fprintf(fp, "%s%s %s %s %s %s root curl -s -S --show-error -o /dev/null localhost:%s%s -d value=%s -X PUT\n",(cline.enabled?"":"#"),cline.minute, cline.hour, cline.daym, cline.month, cline.dayw, _aqconfig_.socket_port, cline.url, cline.value);
} else if ( inarray && inBuf[i] == '}') {
//inobj=false;
//objed=i;
}
}
fprintf(fp, "#***** AUTO GENERATED DO NOT EDIT *****\n");
fclose(fp);
// if we created file, change the permisions
// if we created file, change the permissions
if (!fileexists)
if ( chmod(CRON_FILE, S_IRUSR | S_IWUSR ) < 0 )
LOG(SCHD_LOG,LOG_ERR, "Could not change permitions on cron file %s, scheduling may not work\n",CRON_FILE);
LOG(SCHD_LOG,LOG_ERR, "Could not change permissions on cron file %s, scheduling may not work\n",CRON_FILE);
remount_root_ro(fs);
return length;
return sprintf(outBuf, "{\"message\":\"Saved Schedules\"}");
}
int build_schedules_js(char* buffer, int size)

View File

@ -24,7 +24,7 @@ typedef struct aqs_cron
} aqs_cron;
int build_schedules_js(char* buffer, int size);
int save_schedules_js(char* inBuf, int inSize, char* outBuf, int outSize);
int save_schedules_js(const char* inBuf, int inSize, char* outBuf, int outSize);
//void read_schedules();
//void write_schedules();

View File

@ -1417,22 +1417,25 @@ float pass_mg_body(struct mg_str *body) {
int i;
char buf[10];
int len = sizeof(buf);
if (body->len < len) {
len = body->len;
}
// NSF Really need to come back and clean this up
for (i=0; i < body->len; i++) {
for (i=0; i < len; i++) {
if ( body->p[i] == '=' || body->p[i] == ':' ) {
while (!isdigit((unsigned char) body->p[i]) && body->p[i] != '-' && i < body->len) {i++;}
if(i < body->len) {
while (!isdigit((unsigned char) body->p[i]) && body->p[i] != '-' && i < len) {i++;}
if(i < len) {
// Need to copy to buffer so we can terminate correctly.
strncpy(buf, &body->p[i], body->len - i);
buf[body->len - i] = '\0';
//printf ("RETURN\n");
//return atof(&body->p[i]);
strncpy(buf, &body->p[i], len - i);
buf[len - i] = '\0';
return atof(buf);
}
}
}
//printf ("RETURN UNKNOWN\n");
return TEMP_UNKNOWN;
}
@ -1554,8 +1557,7 @@ void action_web_request(struct mg_connection *nc, struct http_message *http_msg)
{
char message[JSON_BUFFER_SIZE];
DEBUG_TIMER_START(&tid2);
//int size = save_schedules_js(_aqualink_data, &http_msg->body, message, JSON_BUFFER_SIZE);
int size = save_schedules_js((char *)&http_msg->body, http_msg->body.len, message, JSON_BUFFER_SIZE);
int size = save_schedules_js(http_msg->body.p, http_msg->body.len, message, JSON_BUFFER_SIZE);
DEBUG_TIMER_STOP(tid2, NET_LOG, "action_web_request() save_schedules_js took");
mg_send_head(nc, 200, size, CONTENT_JS);
mg_send(nc, message, size);