Browse Source

* src/backends/exec.c : add timeout support

master
Alex 'AdUser' Z 9 years ago
parent
commit
8cc6d34ce5
  1. 20
      src/backends/exec.c

20
src/backends/exec.c

@ -19,6 +19,7 @@ typedef struct cmd_t {
struct _config { struct _config {
char name[ID_MAX + 1]; char name[ID_MAX + 1];
time_t timeout;
cmd_t *start; cmd_t *start;
cmd_t *stop; cmd_t *stop;
cmd_t *ban; cmd_t *ban;
@ -87,7 +88,7 @@ cmd_list_destroy(cmd_t *list) {
} }
static bool static bool
cmd_list_exec(cmd_t *list, const char *ip) { cmd_list_exec(cmd_t *list, const char *ip, time_t timeout) {
int status = 0; int status = 0;
pid_t pid; pid_t pid;
@ -97,6 +98,8 @@ cmd_list_exec(cmd_t *list, const char *ip) {
/* child */ /* child */
if (ip && cmd->pos) if (ip && cmd->pos)
cmd->argv[cmd->pos - 1] = ip; cmd->argv[cmd->pos - 1] = ip;
if (timeout)
alarm(timeout);
execv(cmd->args, cmd->argv); execv(cmd->args, cmd->argv);
} else if (pid > 0) { } else if (pid > 0) {
/* parent */ /* parent */
@ -146,6 +149,11 @@ config(cfg_t *cfg, const char *key, const char *value) {
assert(key != NULL); assert(key != NULL);
assert(value != NULL); assert(value != NULL);
if (strcmp(key, "timeout") == 0) {
cfg->timeout = atoi(value);
return true;
}
CREATE_CMD(start) CREATE_CMD(start)
CREATE_CMD(stop) CREATE_CMD(stop)
CREATE_CMD(ban) CREATE_CMD(ban)
@ -172,7 +180,7 @@ start(cfg_t *cfg) {
if (!cfg->start) if (!cfg->start)
return true; return true;
return cmd_list_exec(cfg->start, NULL); return cmd_list_exec(cfg->start, NULL, cfg->timeout);
} }
bool bool
@ -182,21 +190,21 @@ stop(cfg_t *cfg) {
if (!cfg->stop) if (!cfg->stop)
return true; return true;
return cmd_list_exec(cfg->stop, NULL); return cmd_list_exec(cfg->stop, NULL, cfg->timeout);
} }
bool bool
ban(cfg_t *cfg, const char *ip) { ban(cfg_t *cfg, const char *ip) {
assert(cfg != NULL && ip != NULL); assert(cfg != NULL && ip != NULL);
return cmd_list_exec(cfg->ban, ip); return cmd_list_exec(cfg->ban, ip, cfg->timeout);
} }
bool bool
unban(cfg_t *cfg, const char *ip) { unban(cfg_t *cfg, const char *ip) {
assert(cfg != NULL && ip != NULL); assert(cfg != NULL && ip != NULL);
return cmd_list_exec(cfg->unban, ip); return cmd_list_exec(cfg->unban, ip, cfg->timeout);
} }
bool bool
@ -206,7 +214,7 @@ exists(cfg_t *cfg, const char *ip) {
if (!cfg->exists) if (!cfg->exists)
return true; return true;
return cmd_list_exec(cfg->exists, ip); return cmd_list_exec(cfg->exists, ip, cfg->timeout);
} }

Loading…
Cancel
Save