1/*
2 * Copyright (c) 2009-2021, Redis Ltd.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * * Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * * Neither the name of Redis nor the names of its contributors may be used
14 * to endorse or promote products derived from this software without
15 * specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#ifndef __SCRIPT_H_
31#define __SCRIPT_H_
32
33/*
34 * Script.c unit provides an API for functions and eval
35 * to interact with Redis. Interaction includes mostly
36 * executing commands, but also functionalities like calling
37 * Redis back on long scripts or check if the script was killed.
38 *
39 * The interaction is done using a scriptRunCtx object that
40 * need to be created by the user and initialized using scriptPrepareForRun.
41 *
42 * Detailed list of functionalities expose by the unit:
43 * 1. Calling commands (including all the validation checks such as
44 * acl, cluster, read only run, ...)
45 * 2. Set Resp
46 * 3. Set Replication method (AOF/REPLICATION/NONE)
47 * 4. Call Redis back to on long running scripts to allow Redis reply
48 * to clients and perform script kill
49 */
50
51/*
52 * scriptInterrupt function will return one of those value,
53 *
54 * - SCRIPT_KILL - kill the current running script.
55 * - SCRIPT_CONTINUE - keep running the current script.
56 */
57#define SCRIPT_KILL 1
58#define SCRIPT_CONTINUE 2
59
60/* runCtx flags */
61#define SCRIPT_WRITE_DIRTY (1ULL<<0) /* indicate that the current script already performed a write command */
62#define SCRIPT_TIMEDOUT (1ULL<<3) /* indicate that the current script timedout */
63#define SCRIPT_KILLED (1ULL<<4) /* indicate that the current script was marked to be killed */
64#define SCRIPT_READ_ONLY (1ULL<<5) /* indicate that the current script should only perform read commands */
65#define SCRIPT_ALLOW_OOM (1ULL<<6) /* indicate to allow any command even if OOM reached */
66#define SCRIPT_EVAL_MODE (1ULL<<7) /* Indicate that the current script called from legacy Lua */
67#define SCRIPT_ALLOW_CROSS_SLOT (1ULL<<8) /* Indicate that the current script may access keys from multiple slots */
68typedef struct scriptRunCtx scriptRunCtx;
69
70struct scriptRunCtx {
71 const char *funcname;
72 client *c;
73 client *original_client;
74 int flags;
75 int repl_flags;
76 monotime start_time;
77 mstime_t snapshot_time;
78};
79
80/* Scripts flags */
81#define SCRIPT_FLAG_NO_WRITES (1ULL<<0)
82#define SCRIPT_FLAG_ALLOW_OOM (1ULL<<1)
83#define SCRIPT_FLAG_ALLOW_STALE (1ULL<<2)
84#define SCRIPT_FLAG_NO_CLUSTER (1ULL<<3)
85#define SCRIPT_FLAG_EVAL_COMPAT_MODE (1ULL<<4) /* EVAL Script backwards compatible behavior, no shebang provided */
86#define SCRIPT_FLAG_ALLOW_CROSS_SLOT (1ULL<<5)
87
88/* Defines a script flags */
89typedef struct scriptFlag {
90 uint64_t flag;
91 const char *str;
92} scriptFlag;
93
94extern scriptFlag scripts_flags_def[];
95
96uint64_t scriptFlagsToCmdFlags(uint64_t cmd_flags, uint64_t script_flags);
97int scriptPrepareForRun(scriptRunCtx *r_ctx, client *engine_client, client *caller, const char *funcname, uint64_t script_flags, int ro);
98void scriptResetRun(scriptRunCtx *r_ctx);
99int scriptSetResp(scriptRunCtx *r_ctx, int resp);
100int scriptSetRepl(scriptRunCtx *r_ctx, int repl);
101void scriptCall(scriptRunCtx *r_ctx, robj **argv, int argc, sds *err);
102int scriptInterrupt(scriptRunCtx *r_ctx);
103void scriptKill(client *c, int is_eval);
104int scriptIsRunning();
105const char* scriptCurrFunction();
106int scriptIsEval();
107int scriptIsTimedout();
108client* scriptGetClient();
109client* scriptGetCaller();
110mstime_t scriptTimeSnapshot();
111long long scriptRunDuration();
112
113#endif /* __SCRIPT_H_ */
114