aboutsummaryrefslogtreecommitdiff
path: root/examples/redis-unstable/tests/modules/fork.c
diff options
context:
space:
mode:
Diffstat (limited to 'examples/redis-unstable/tests/modules/fork.c')
-rw-r--r--examples/redis-unstable/tests/modules/fork.c96
1 files changed, 0 insertions, 96 deletions
diff --git a/examples/redis-unstable/tests/modules/fork.c b/examples/redis-unstable/tests/modules/fork.c
deleted file mode 100644
index d7a0d15..0000000
--- a/examples/redis-unstable/tests/modules/fork.c
+++ /dev/null
@@ -1,96 +0,0 @@
1
2/* define macros for having usleep */
3#define _BSD_SOURCE
4#define _DEFAULT_SOURCE
5
6#include "redismodule.h"
7#include <string.h>
8#include <assert.h>
9#include <unistd.h>
10
11#define UNUSED(V) ((void) V)
12
13int child_pid = -1;
14int exitted_with_code = -1;
15
16void done_handler(int exitcode, int bysignal, void *user_data) {
17 child_pid = -1;
18 exitted_with_code = exitcode;
19 assert(user_data==(void*)0xdeadbeef);
20 UNUSED(bysignal);
21}
22
23int fork_create(RedisModuleCtx *ctx, RedisModuleString **argv, int argc)
24{
25 long long code_to_exit_with;
26 long long usleep_us;
27 if (argc != 3) {
28 RedisModule_WrongArity(ctx);
29 return REDISMODULE_OK;
30 }
31
32 if(!RMAPI_FUNC_SUPPORTED(RedisModule_Fork)){
33 RedisModule_ReplyWithError(ctx, "Fork api is not supported in the current redis version");
34 return REDISMODULE_OK;
35 }
36
37 RedisModule_StringToLongLong(argv[1], &code_to_exit_with);
38 RedisModule_StringToLongLong(argv[2], &usleep_us);
39 exitted_with_code = -1;
40 int fork_child_pid = RedisModule_Fork(done_handler, (void*)0xdeadbeef);
41 if (fork_child_pid < 0) {
42 RedisModule_ReplyWithError(ctx, "Fork failed");
43 return REDISMODULE_OK;
44 } else if (fork_child_pid > 0) {
45 /* parent */
46 child_pid = fork_child_pid;
47 RedisModule_ReplyWithLongLong(ctx, child_pid);
48 return REDISMODULE_OK;
49 }
50
51 /* child */
52 RedisModule_Log(ctx, "notice", "fork child started");
53 usleep(usleep_us);
54 RedisModule_Log(ctx, "notice", "fork child exiting");
55 RedisModule_ExitFromChild(code_to_exit_with);
56 /* unreachable */
57 return 0;
58}
59
60int fork_exitcode(RedisModuleCtx *ctx, RedisModuleString **argv, int argc)
61{
62 UNUSED(argv);
63 UNUSED(argc);
64 RedisModule_ReplyWithLongLong(ctx, exitted_with_code);
65 return REDISMODULE_OK;
66}
67
68int fork_kill(RedisModuleCtx *ctx, RedisModuleString **argv, int argc)
69{
70 UNUSED(argv);
71 UNUSED(argc);
72 if (RedisModule_KillForkChild(child_pid) != REDISMODULE_OK)
73 RedisModule_ReplyWithError(ctx, "KillForkChild failed");
74 else
75 RedisModule_ReplyWithLongLong(ctx, 1);
76 child_pid = -1;
77 return REDISMODULE_OK;
78}
79
80int RedisModule_OnLoad(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
81 UNUSED(argv);
82 UNUSED(argc);
83 if (RedisModule_Init(ctx,"fork",1,REDISMODULE_APIVER_1)== REDISMODULE_ERR)
84 return REDISMODULE_ERR;
85
86 if (RedisModule_CreateCommand(ctx,"fork.create", fork_create,"",0,0,0) == REDISMODULE_ERR)
87 return REDISMODULE_ERR;
88
89 if (RedisModule_CreateCommand(ctx,"fork.exitcode", fork_exitcode,"",0,0,0) == REDISMODULE_ERR)
90 return REDISMODULE_ERR;
91
92 if (RedisModule_CreateCommand(ctx,"fork.kill", fork_kill,"",0,0,0) == REDISMODULE_ERR)
93 return REDISMODULE_ERR;
94
95 return REDISMODULE_OK;
96}