Commit d0f03fd7 by Stéphane Graber Committed by GitHub

Merge pull request #1328 from brauner/2016-11-27/make_running_hooks_async_signal_safe

conf: remove thread-unsafe strsignal + improve log
parents 1e0a0046 062b72c6
...@@ -375,32 +375,31 @@ static int run_buffer(char *buffer) ...@@ -375,32 +375,31 @@ static int run_buffer(char *buffer)
f = lxc_popen(buffer); f = lxc_popen(buffer);
if (!f) { if (!f) {
SYSERROR("popen failed"); SYSERROR("Failed to popen() %s.", buffer);
return -1; return -1;
} }
output = malloc(LXC_LOG_BUFFER_SIZE); output = malloc(LXC_LOG_BUFFER_SIZE);
if (!output) { if (!output) {
ERROR("failed to allocate memory for script output"); ERROR("Failed to allocate memory for %s.", buffer);
lxc_pclose(f); lxc_pclose(f);
return -1; return -1;
} }
while(fgets(output, LXC_LOG_BUFFER_SIZE, f->f)) while (fgets(output, LXC_LOG_BUFFER_SIZE, f->f))
DEBUG("script output: %s", output); DEBUG("Script %s with output: %s.", buffer, output);
free(output); free(output);
ret = lxc_pclose(f); ret = lxc_pclose(f);
if (ret == -1) { if (ret == -1) {
SYSERROR("Script exited on error"); SYSERROR("Script exited with error.");
return -1; return -1;
} else if (WIFEXITED(ret) && WEXITSTATUS(ret) != 0) { } else if (WIFEXITED(ret) && WEXITSTATUS(ret) != 0) {
ERROR("Script exited with status %d", WEXITSTATUS(ret)); ERROR("Script exited with status %d.", WEXITSTATUS(ret));
return -1; return -1;
} else if (WIFSIGNALED(ret)) { } else if (WIFSIGNALED(ret)) {
ERROR("Script terminated by signal %d (%s)", WTERMSIG(ret), ERROR("Script terminated by signal %d.", WTERMSIG(ret));
strsignal(WTERMSIG(ret)));
return -1; return -1;
} }
...@@ -408,17 +407,17 @@ static int run_buffer(char *buffer) ...@@ -408,17 +407,17 @@ static int run_buffer(char *buffer)
} }
static int run_script_argv(const char *name, const char *section, static int run_script_argv(const char *name, const char *section,
const char *script, const char *hook, const char *lxcpath, const char *script, const char *hook,
char **argsin) const char *lxcpath, char **argsin)
{ {
int ret, i; int ret, i;
char *buffer; char *buffer;
size_t size = 0; size_t size = 0;
INFO("Executing script '%s' for container '%s', config section '%s'", INFO("Executing script \"%s\" for container \"%s\", config section \"%s\".",
script, name, section); script, name, section);
for (i=0; argsin && argsin[i]; i++) for (i = 0; argsin && argsin[i]; i++)
size += strlen(argsin[i]) + 1; size += strlen(argsin[i]) + 1;
size += strlen(hook) + 1; size += strlen(hook) + 1;
...@@ -433,22 +432,23 @@ static int run_script_argv(const char *name, const char *section, ...@@ -433,22 +432,23 @@ static int run_script_argv(const char *name, const char *section,
buffer = alloca(size); buffer = alloca(size);
if (!buffer) { if (!buffer) {
ERROR("failed to allocate memory"); ERROR("Failed to allocate memory.");
return -1; return -1;
} }
ret = snprintf(buffer, size, "%s %s %s %s", script, name, section, hook); ret =
if (ret < 0 || ret >= size) { snprintf(buffer, size, "%s %s %s %s", script, name, section, hook);
ERROR("Script name too long"); if (ret < 0 || (size_t)ret >= size) {
ERROR("Script name too long.");
return -1; return -1;
} }
for (i=0; argsin && argsin[i]; i++) { for (i = 0; argsin && argsin[i]; i++) {
int len = size-ret; int len = size - ret;
int rc; int rc;
rc = snprintf(buffer + ret, len, " %s", argsin[i]); rc = snprintf(buffer + ret, len, " %s", argsin[i]);
if (rc < 0 || rc >= len) { if (rc < 0 || rc >= len) {
ERROR("Script args too long"); ERROR("Script args too long.");
return -1; return -1;
} }
ret += rc; ret += rc;
...@@ -457,15 +457,15 @@ static int run_script_argv(const char *name, const char *section, ...@@ -457,15 +457,15 @@ static int run_script_argv(const char *name, const char *section,
return run_buffer(buffer); return run_buffer(buffer);
} }
static int run_script(const char *name, const char *section, static int run_script(const char *name, const char *section, const char *script,
const char *script, ...) ...)
{ {
int ret; int ret;
char *buffer, *p; char *buffer, *p;
size_t size = 0; size_t size = 0;
va_list ap; va_list ap;
INFO("Executing script '%s' for container '%s', config section '%s'", INFO("Executing script \"%s\" for container \"%s\", config section \"%s\".",
script, name, section); script, name, section);
va_start(ap, script); va_start(ap, script);
...@@ -483,23 +483,23 @@ static int run_script(const char *name, const char *section, ...@@ -483,23 +483,23 @@ static int run_script(const char *name, const char *section,
buffer = alloca(size); buffer = alloca(size);
if (!buffer) { if (!buffer) {
ERROR("failed to allocate memory"); ERROR("Failed to allocate memory.");
return -1; return -1;
} }
ret = snprintf(buffer, size, "%s %s %s", script, name, section); ret = snprintf(buffer, size, "%s %s %s", script, name, section);
if (ret < 0 || ret >= size) { if (ret < 0 || ret >= size) {
ERROR("Script name too long"); ERROR("Script name too long.");
return -1; return -1;
} }
va_start(ap, script); va_start(ap, script);
while ((p = va_arg(ap, char *))) { while ((p = va_arg(ap, char *))) {
int len = size-ret; int len = size - ret;
int rc; int rc;
rc = snprintf(buffer + ret, len, " %s", p); rc = snprintf(buffer + ret, len, " %s", p);
if (rc < 0 || rc >= len) { if (rc < 0 || rc >= len) {
ERROR("Script args too long"); ERROR("Script args too long.");
return -1; return -1;
} }
ret += rc; ret += rc;
......
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