2024-03-21 09:30:47 -04:00
|
|
|
// SPDX-FileCopyrightText: 2024 Redict Contributors
|
|
|
|
// SPDX-FileCopyrightText: 2024 Salvatore Sanfilippo <antirez at gmail dot com>
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2024-03-21 15:11:44 -04:00
|
|
|
// SPDX-License-Identifier: LGPL-3.0-only
|
2019-07-17 01:51:02 -04:00
|
|
|
|
2019-10-24 02:38:52 -04:00
|
|
|
/* define macros for having usleep */
|
|
|
|
#define _BSD_SOURCE
|
|
|
|
#define _DEFAULT_SOURCE
|
|
|
|
|
2024-03-21 05:49:18 -04:00
|
|
|
#include "redictmodule.h"
|
2019-07-17 01:51:02 -04:00
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#define UNUSED(V) ((void) V)
|
|
|
|
|
|
|
|
int child_pid = -1;
|
|
|
|
int exitted_with_code = -1;
|
|
|
|
|
|
|
|
void done_handler(int exitcode, int bysignal, void *user_data) {
|
|
|
|
child_pid = -1;
|
|
|
|
exitted_with_code = exitcode;
|
|
|
|
assert(user_data==(void*)0xdeadbeef);
|
|
|
|
UNUSED(bysignal);
|
|
|
|
}
|
|
|
|
|
2024-03-25 07:41:50 -04:00
|
|
|
int fork_create(RedictModuleCtx *ctx, RedictModuleString **argv, int argc)
|
2019-07-17 01:51:02 -04:00
|
|
|
{
|
|
|
|
long long code_to_exit_with;
|
2022-05-12 13:10:38 -04:00
|
|
|
long long usleep_us;
|
|
|
|
if (argc != 3) {
|
2024-03-25 07:41:50 -04:00
|
|
|
RedictModule_WrongArity(ctx);
|
|
|
|
return REDICTMODULE_OK;
|
2019-07-17 01:51:02 -04:00
|
|
|
}
|
2020-11-09 03:46:23 -05:00
|
|
|
|
2024-03-25 07:41:50 -04:00
|
|
|
if(!RMAPI_FUNC_SUPPORTED(RedictModule_Fork)){
|
2024-03-25 07:45:47 -04:00
|
|
|
RedictModule_ReplyWithError(ctx, "Fork api is not supported in the current redict version");
|
2024-03-25 07:41:50 -04:00
|
|
|
return REDICTMODULE_OK;
|
2020-11-09 03:46:23 -05:00
|
|
|
}
|
|
|
|
|
2024-03-25 07:41:50 -04:00
|
|
|
RedictModule_StringToLongLong(argv[1], &code_to_exit_with);
|
|
|
|
RedictModule_StringToLongLong(argv[2], &usleep_us);
|
2019-07-17 01:51:02 -04:00
|
|
|
exitted_with_code = -1;
|
2024-03-25 07:41:50 -04:00
|
|
|
int fork_child_pid = RedictModule_Fork(done_handler, (void*)0xdeadbeef);
|
2022-05-12 13:10:38 -04:00
|
|
|
if (fork_child_pid < 0) {
|
2024-03-25 07:41:50 -04:00
|
|
|
RedictModule_ReplyWithError(ctx, "Fork failed");
|
|
|
|
return REDICTMODULE_OK;
|
2022-05-12 13:10:38 -04:00
|
|
|
} else if (fork_child_pid > 0) {
|
2019-07-17 01:51:02 -04:00
|
|
|
/* parent */
|
2022-05-12 13:10:38 -04:00
|
|
|
child_pid = fork_child_pid;
|
2024-03-25 07:41:50 -04:00
|
|
|
RedictModule_ReplyWithLongLong(ctx, child_pid);
|
|
|
|
return REDICTMODULE_OK;
|
2019-07-17 01:51:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* child */
|
2024-03-25 07:41:50 -04:00
|
|
|
RedictModule_Log(ctx, "notice", "fork child started");
|
2022-05-12 13:10:38 -04:00
|
|
|
usleep(usleep_us);
|
2024-03-25 07:41:50 -04:00
|
|
|
RedictModule_Log(ctx, "notice", "fork child exiting");
|
|
|
|
RedictModule_ExitFromChild(code_to_exit_with);
|
2019-07-17 01:51:02 -04:00
|
|
|
/* unreachable */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-03-25 07:41:50 -04:00
|
|
|
int fork_exitcode(RedictModuleCtx *ctx, RedictModuleString **argv, int argc)
|
2019-07-17 01:51:02 -04:00
|
|
|
{
|
|
|
|
UNUSED(argv);
|
|
|
|
UNUSED(argc);
|
2024-03-25 07:41:50 -04:00
|
|
|
RedictModule_ReplyWithLongLong(ctx, exitted_with_code);
|
|
|
|
return REDICTMODULE_OK;
|
2019-07-17 01:51:02 -04:00
|
|
|
}
|
|
|
|
|
2024-03-25 07:41:50 -04:00
|
|
|
int fork_kill(RedictModuleCtx *ctx, RedictModuleString **argv, int argc)
|
2019-07-17 01:51:02 -04:00
|
|
|
{
|
|
|
|
UNUSED(argv);
|
|
|
|
UNUSED(argc);
|
2024-03-25 07:41:50 -04:00
|
|
|
if (RedictModule_KillForkChild(child_pid) != REDICTMODULE_OK)
|
|
|
|
RedictModule_ReplyWithError(ctx, "KillForkChild failed");
|
2019-07-17 01:51:02 -04:00
|
|
|
else
|
2024-03-25 07:41:50 -04:00
|
|
|
RedictModule_ReplyWithLongLong(ctx, 1);
|
2019-07-17 01:51:02 -04:00
|
|
|
child_pid = -1;
|
2024-03-25 07:41:50 -04:00
|
|
|
return REDICTMODULE_OK;
|
2019-07-17 01:51:02 -04:00
|
|
|
}
|
|
|
|
|
2024-03-25 07:41:50 -04:00
|
|
|
int RedictModule_OnLoad(RedictModuleCtx *ctx, RedictModuleString **argv, int argc) {
|
2019-07-17 01:51:02 -04:00
|
|
|
UNUSED(argv);
|
|
|
|
UNUSED(argc);
|
2024-03-25 07:41:50 -04:00
|
|
|
if (RedictModule_Init(ctx,"fork",1,REDICTMODULE_APIVER_1)== REDICTMODULE_ERR)
|
|
|
|
return REDICTMODULE_ERR;
|
2019-07-17 01:51:02 -04:00
|
|
|
|
2024-03-25 07:41:50 -04:00
|
|
|
if (RedictModule_CreateCommand(ctx,"fork.create", fork_create,"",0,0,0) == REDICTMODULE_ERR)
|
|
|
|
return REDICTMODULE_ERR;
|
2019-07-17 01:51:02 -04:00
|
|
|
|
2024-03-25 07:41:50 -04:00
|
|
|
if (RedictModule_CreateCommand(ctx,"fork.exitcode", fork_exitcode,"",0,0,0) == REDICTMODULE_ERR)
|
|
|
|
return REDICTMODULE_ERR;
|
2019-07-17 01:51:02 -04:00
|
|
|
|
2024-03-25 07:41:50 -04:00
|
|
|
if (RedictModule_CreateCommand(ctx,"fork.kill", fork_kill,"",0,0,0) == REDICTMODULE_ERR)
|
|
|
|
return REDICTMODULE_ERR;
|
2019-07-17 01:51:02 -04:00
|
|
|
|
2024-03-25 07:41:50 -04:00
|
|
|
return REDICTMODULE_OK;
|
2019-07-17 01:51:02 -04:00
|
|
|
}
|