Skip to content

Commit 4458452

Browse files
authored
Add files via upload
1 parent 33c7127 commit 4458452

File tree

1 file changed

+72
-48
lines changed

1 file changed

+72
-48
lines changed

iptablespush.c

+72-48
Original file line numberDiff line numberDiff line change
@@ -8,45 +8,65 @@
88
#include <ctype.h>
99
#include <string.h>
1010
#include <unistd.h>
11+
#include <signal.h>
12+
#include <sys/types.h>
13+
#include <sys/wait.h>
14+
15+
int redis_waitpid(pid_t pid)
16+
{
17+
int rc, status;
18+
do
19+
{
20+
if (-1 == (rc = waitpid(pid, &status, WUNTRACED)))
21+
{
22+
goto exit;
23+
}
24+
}
25+
while (!WIFEXITED(status) && !WIFSIGNALED(status));
26+
exit:
27+
28+
return rc;
29+
}
30+
1131
int execute_fork()
1232
{
13-
fflush(stdout);
14-
fflush(stderr);
15-
return fork();
33+
fflush(stdout);
34+
fflush(stderr);
35+
return fork();
1636
}
1737

1838
int execute_popen(pid_t *pid, const char *command)
1939
{
20-
int fd[2];
21-
22-
if (-1 == pipe(fd))
23-
return -1;
24-
25-
if (-1 == (*pid = execute_fork()))
26-
{
27-
close(fd[0]);
28-
close(fd[1]);
29-
return -1;
30-
}
31-
32-
if (0 != *pid) /* parent process */
33-
{
34-
close(fd[1]);
35-
return fd[0];
36-
}
37-
38-
close(fd[0]);
39-
dup2(fd[1], STDOUT_FILENO);
40-
dup2(fd[1], STDERR_FILENO);
41-
close(fd[1]);
42-
if (-1 == setpgid(0, 0))
43-
{
44-
exit(EXIT_SUCCESS);
45-
}
46-
47-
execl("/bin/sh", "sh", "-c", command, NULL);
48-
49-
exit(EXIT_SUCCESS);
40+
int fd[2];
41+
42+
if (-1 == pipe(fd))
43+
return -1;
44+
45+
if (-1 == (*pid = execute_fork()))
46+
{
47+
close(fd[0]);
48+
close(fd[1]);
49+
return -1;
50+
}
51+
52+
if (0 != *pid) /* parent process */
53+
{
54+
close(fd[1]);
55+
return fd[0];
56+
}
57+
58+
close(fd[0]);
59+
dup2(fd[1], STDOUT_FILENO);
60+
dup2(fd[1], STDERR_FILENO);
61+
close(fd[1]);
62+
if (-1 == setpgid(0, 0))
63+
{
64+
exit(EXIT_SUCCESS);
65+
}
66+
67+
execl("/bin/sh", "sh", "-c", command, NULL);
68+
69+
exit(EXIT_SUCCESS);
5070
}
5171

5272
int IptablesPush_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc)
@@ -55,7 +75,7 @@ int IptablesPush_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int
5575
return RedisModule_WrongArity(ctx);
5676

5777
RedisModuleKey *key = RedisModule_OpenKey(ctx, argv[1],
58-
REDISMODULE_READ | REDISMODULE_WRITE);
78+
REDISMODULE_READ | REDISMODULE_WRITE);
5979
pid_t pid;
6080
int fd;
6181
char tmp_buf[4096];
@@ -68,9 +88,13 @@ int IptablesPush_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int
6888
printf("%s || %s\n", RedisModule_StringPtrLen(argv[1], NULL),
6989
RedisModule_StringPtrLen(argv[2], NULL));
7090
fd = execute_popen(&pid, check_command);
71-
if (0 < read(fd, tmp_buf, sizeof(tmp_buf) - 1)) {
72-
execute_popen(&pid, insert_command);
73-
}
91+
redis_waitpid(pid);
92+
if (0 < read(fd, tmp_buf, sizeof(tmp_buf) - 1)) {
93+
close(fd);
94+
execute_popen(&pid, insert_command);
95+
redis_waitpid(pid);
96+
}
97+
close(fd);
7498

7599
RedisModule_StringSet(key, argv[2]);
76100
size_t newlen = RedisModule_ValueLength(key);
@@ -83,19 +107,19 @@ int IptablesPush_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int
83107
/* This function must be present on each Redis module. It is used in order to
84108
* register the commands into the Redis server. */
85109
int RedisModule_OnLoad(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
86-
if (RedisModule_Init(ctx,"iptables-insert",1,REDISMODULE_APIVER_1)
87-
== REDISMODULE_ERR) return REDISMODULE_ERR;
110+
if (RedisModule_Init(ctx,"iptables-insert",1,REDISMODULE_APIVER_1)
111+
== REDISMODULE_ERR) return REDISMODULE_ERR;
88112

89-
/* Log the list of parameters passing loading the module. */
90-
for (int j = 0; j < argc; j++) {
91-
const char *s = RedisModule_StringPtrLen(argv[j],NULL);
92-
printf("Module loaded with ARGV[%d] = %s\n", j, s);
93-
}
113+
/* Log the list of parameters passing loading the module. */
114+
for (int j = 0; j < argc; j++) {
115+
const char *s = RedisModule_StringPtrLen(argv[j],NULL);
116+
printf("Module loaded with ARGV[%d] = %s\n", j, s);
117+
}
94118

95-
if (RedisModule_CreateCommand(ctx,"iptables.push",
96-
IptablesPush_RedisCommand,"write deny-oom",1,1,1) == REDISMODULE_ERR)
97-
return REDISMODULE_ERR;
119+
if (RedisModule_CreateCommand(ctx,"iptables.push",
120+
IptablesPush_RedisCommand,"write deny-oom",1,1,1) == REDISMODULE_ERR)
121+
return REDISMODULE_ERR;
98122

99123

100-
return REDISMODULE_OK;
124+
return REDISMODULE_OK;
101125
}

0 commit comments

Comments
 (0)