Commit b98fb201 authored by Juliusz Chroboczek's avatar Juliusz Chroboczek

Fix incorrect handling of multiple local commands in a single read.

parent 53518417
......@@ -316,7 +316,7 @@ local_notify_all_1(struct local_socket *s)
int
local_read(struct local_socket *s)
{
int rc;
int rc, n;
char *eol;
char reply[100] = "ok\n";
const char *message = NULL;
......@@ -336,47 +336,50 @@ local_read(struct local_socket *s)
return rc;
s->n += rc;
eol = memchr(s->buf, '\n', s->n);
if(eol == NULL)
return 1;
while(s->n > 0) {
eol = memchr(s->buf, '\n', s->n);
if(eol == NULL)
break;
n = eol + 1 - s->buf;
rc = parse_config_from_string(s->buf, eol + 1 - s->buf, &message);
switch(rc) {
case CONFIG_ACTION_DONE:
break;
case CONFIG_ACTION_QUIT:
shutdown(s->fd, 1);
reply[0] = '\0';
break;
case CONFIG_ACTION_DUMP:
local_notify_all_1(s);
break;
case CONFIG_ACTION_MONITOR:
local_notify_all_1(s);
s->monitor = 1;
break;
case CONFIG_ACTION_UNMONITOR:
s->monitor = 0;
break;
case CONFIG_ACTION_NO:
snprintf(reply, sizeof(reply), "no%s%s\n",
message ? " " : "", message ? message : "");
break;
default:
snprintf(reply, sizeof(reply), "bad\n");
}
rc = parse_config_from_string(s->buf, n, &message);
switch(rc) {
case CONFIG_ACTION_DONE:
break;
case CONFIG_ACTION_QUIT:
shutdown(s->fd, 1);
reply[0] = '\0';
break;
case CONFIG_ACTION_DUMP:
local_notify_all_1(s);
break;
case CONFIG_ACTION_MONITOR:
local_notify_all_1(s);
s->monitor = 1;
break;
case CONFIG_ACTION_UNMONITOR:
s->monitor = 0;
break;
case CONFIG_ACTION_NO:
snprintf(reply, sizeof(reply), "no%s%s\n",
message ? " " : "", message ? message : "");
break;
default:
snprintf(reply, sizeof(reply), "bad\n");
}
if(reply[0] != '\0') {
rc = write_timeout(s->fd, reply, strlen(reply));
if(rc < 0)
goto fail;
if(reply[0] != '\0') {
rc = write_timeout(s->fd, reply, strlen(reply));
if(rc < 0) {
goto fail;
}
}
if(s->n > n)
memmove(s->buf, s->buf + n, s->n - n);
s->n -= n;
}
if(s->n > eol + 1 - s->buf) {
memmove(s->buf, eol + 1, s->n - (eol + 1 - s->buf));
s->n -= (eol + 1 - s->buf);
} else {
s->n = 0;
if(s->n == 0) {
free(s->buf);
s->buf = NULL;
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment