8
8
#include <ctype.h>
9
9
#include <string.h>
10
10
#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
+
11
31
int execute_fork ()
12
32
{
13
- fflush (stdout );
14
- fflush (stderr );
15
- return fork ();
33
+ fflush (stdout );
34
+ fflush (stderr );
35
+ return fork ();
16
36
}
17
37
18
38
int execute_popen (pid_t * pid , const char * command )
19
39
{
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 );
50
70
}
51
71
52
72
int IptablesPush_RedisCommand (RedisModuleCtx * ctx , RedisModuleString * * argv , int argc )
@@ -55,7 +75,7 @@ int IptablesPush_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int
55
75
return RedisModule_WrongArity (ctx );
56
76
57
77
RedisModuleKey * key = RedisModule_OpenKey (ctx , argv [1 ],
58
- REDISMODULE_READ | REDISMODULE_WRITE );
78
+ REDISMODULE_READ | REDISMODULE_WRITE );
59
79
pid_t pid ;
60
80
int fd ;
61
81
char tmp_buf [4096 ];
@@ -68,9 +88,13 @@ int IptablesPush_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int
68
88
printf ("%s || %s\n" , RedisModule_StringPtrLen (argv [1 ], NULL ),
69
89
RedisModule_StringPtrLen (argv [2 ], NULL ));
70
90
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 );
74
98
75
99
RedisModule_StringSet (key , argv [2 ]);
76
100
size_t newlen = RedisModule_ValueLength (key );
@@ -83,19 +107,19 @@ int IptablesPush_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int
83
107
/* This function must be present on each Redis module. It is used in order to
84
108
* register the commands into the Redis server. */
85
109
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 ;
88
112
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
+ }
94
118
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 ;
98
122
99
123
100
- return REDISMODULE_OK ;
124
+ return REDISMODULE_OK ;
101
125
}
0 commit comments