xref: /redis-3.2.3/src/scripting.c (revision fdc8e0bd)
1 /*
2  * Copyright (c) 2009-2012, Salvatore Sanfilippo <antirez at gmail dot com>
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 #include "server.h"
31 #include "sha1.h"
32 #include "rand.h"
33 #include "cluster.h"
34 
35 #include <lua.h>
36 #include <lauxlib.h>
37 #include <lualib.h>
38 #include <ctype.h>
39 #include <math.h>
40 
41 char *redisProtocolToLuaType_Int(lua_State *lua, char *reply);
42 char *redisProtocolToLuaType_Bulk(lua_State *lua, char *reply);
43 char *redisProtocolToLuaType_Status(lua_State *lua, char *reply);
44 char *redisProtocolToLuaType_Error(lua_State *lua, char *reply);
45 char *redisProtocolToLuaType_MultiBulk(lua_State *lua, char *reply);
46 int redis_math_random (lua_State *L);
47 int redis_math_randomseed (lua_State *L);
48 void sha1hex(char *digest, char *script, size_t len);
49 
50 /* Take a Redis reply in the Redis protocol format and convert it into a
51  * Lua type. Thanks to this function, and the introduction of not connected
52  * clients, it is trivial to implement the redis() lua function.
53  *
54  * Basically we take the arguments, execute the Redis command in the context
55  * of a non connected client, then take the generated reply and convert it
56  * into a suitable Lua type. With this trick the scripting feature does not
57  * need the introduction of a full Redis internals API. Basically the script
58  * is like a normal client that bypasses all the slow I/O paths.
59  *
60  * Note: in this function we do not do any sanity check as the reply is
61  * generated by Redis directly. This allows us to go faster.
62  * The reply string can be altered during the parsing as it is discarded
63  * after the conversion is completed.
64  *
65  * Errors are returned as a table with a single 'err' field set to the
66  * error string.
67  */
68 
69 char *redisProtocolToLuaType(lua_State *lua, char* reply) {
70     char *p = reply;
71 
72     switch(*p) {
73     case ':':
74         p = redisProtocolToLuaType_Int(lua,reply);
75         break;
76     case '$':
77         p = redisProtocolToLuaType_Bulk(lua,reply);
78         break;
79     case '+':
80         p = redisProtocolToLuaType_Status(lua,reply);
81         break;
82     case '-':
83         p = redisProtocolToLuaType_Error(lua,reply);
84         break;
85     case '*':
86         p = redisProtocolToLuaType_MultiBulk(lua,reply);
87         break;
88     }
89     return p;
90 }
91 
92 char *redisProtocolToLuaType_Int(lua_State *lua, char *reply) {
93     char *p = strchr(reply+1,'\r');
94     long long value;
95 
96     string2ll(reply+1,p-reply-1,&value);
97     lua_pushnumber(lua,(lua_Number)value);
98     return p+2;
99 }
100 
101 char *redisProtocolToLuaType_Bulk(lua_State *lua, char *reply) {
102     char *p = strchr(reply+1,'\r');
103     long long bulklen;
104 
105     string2ll(reply+1,p-reply-1,&bulklen);
106     if (bulklen == -1) {
107         lua_pushboolean(lua,0);
108         return p+2;
109     } else {
110         lua_pushlstring(lua,p+2,bulklen);
111         return p+2+bulklen+2;
112     }
113 }
114 
115 char *redisProtocolToLuaType_Status(lua_State *lua, char *reply) {
116     char *p = strchr(reply+1,'\r');
117 
118     lua_newtable(lua);
119     lua_pushstring(lua,"ok");
120     lua_pushlstring(lua,reply+1,p-reply-1);
121     lua_settable(lua,-3);
122     return p+2;
123 }
124 
125 char *redisProtocolToLuaType_Error(lua_State *lua, char *reply) {
126     char *p = strchr(reply+1,'\r');
127 
128     lua_newtable(lua);
129     lua_pushstring(lua,"err");
130     lua_pushlstring(lua,reply+1,p-reply-1);
131     lua_settable(lua,-3);
132     return p+2;
133 }
134 
135 char *redisProtocolToLuaType_MultiBulk(lua_State *lua, char *reply) {
136     char *p = strchr(reply+1,'\r');
137     long long mbulklen;
138     int j = 0;
139 
140     string2ll(reply+1,p-reply-1,&mbulklen);
141     p += 2;
142     if (mbulklen == -1) {
143         lua_pushboolean(lua,0);
144         return p;
145     }
146     lua_newtable(lua);
147     for (j = 0; j < mbulklen; j++) {
148         lua_pushnumber(lua,j+1);
149         p = redisProtocolToLuaType(lua,p);
150         lua_settable(lua,-3);
151     }
152     return p;
153 }
154 
155 void luaPushError(lua_State *lua, char *error) {
156     lua_Debug dbg;
157 
158     lua_newtable(lua);
159     lua_pushstring(lua,"err");
160 
161     /* Attempt to figure out where this function was called, if possible */
162     if(lua_getstack(lua, 1, &dbg) && lua_getinfo(lua, "nSl", &dbg)) {
163         sds msg = sdscatprintf(sdsempty(), "%s: %d: %s",
164             dbg.source, dbg.currentline, error);
165         lua_pushstring(lua, msg);
166         sdsfree(msg);
167     } else {
168         lua_pushstring(lua, error);
169     }
170     lua_settable(lua,-3);
171 }
172 
173 /* Sort the array currently in the stack. We do this to make the output
174  * of commands like KEYS or SMEMBERS something deterministic when called
175  * from Lua (to play well with AOf/replication).
176  *
177  * The array is sorted using table.sort itself, and assuming all the
178  * list elements are strings. */
179 void luaSortArray(lua_State *lua) {
180     /* Initial Stack: array */
181     lua_getglobal(lua,"table");
182     lua_pushstring(lua,"sort");
183     lua_gettable(lua,-2);       /* Stack: array, table, table.sort */
184     lua_pushvalue(lua,-3);      /* Stack: array, table, table.sort, array */
185     if (lua_pcall(lua,1,0,0)) {
186         /* Stack: array, table, error */
187 
188         /* We are not interested in the error, we assume that the problem is
189          * that there are 'false' elements inside the array, so we try
190          * again with a slower function but able to handle this case, that
191          * is: table.sort(table, __redis__compare_helper) */
192         lua_pop(lua,1);             /* Stack: array, table */
193         lua_pushstring(lua,"sort"); /* Stack: array, table, sort */
194         lua_gettable(lua,-2);       /* Stack: array, table, table.sort */
195         lua_pushvalue(lua,-3);      /* Stack: array, table, table.sort, array */
196         lua_getglobal(lua,"__redis__compare_helper");
197         /* Stack: array, table, table.sort, array, __redis__compare_helper */
198         lua_call(lua,2,0);
199     }
200     /* Stack: array (sorted), table */
201     lua_pop(lua,1);             /* Stack: array (sorted) */
202 }
203 
204 #define LUA_CMD_OBJCACHE_SIZE 32
205 #define LUA_CMD_OBJCACHE_MAX_LEN 64
206 int luaRedisGenericCommand(lua_State *lua, int raise_error) {
207     int j, argc = lua_gettop(lua);
208     struct redisCommand *cmd;
209     client *c = server.lua_client;
210     sds reply;
211 
212     /* Cached across calls. */
213     static robj **argv = NULL;
214     static int argv_size = 0;
215     static robj *cached_objects[LUA_CMD_OBJCACHE_SIZE];
216     static size_t cached_objects_len[LUA_CMD_OBJCACHE_SIZE];
217     static int inuse = 0;   /* Recursive calls detection. */
218 
219     /* By using Lua debug hooks it is possible to trigger a recursive call
220      * to luaRedisGenericCommand(), which normally should never happen.
221      * To make this function reentrant is futile and makes it slower, but
222      * we should at least detect such a misuse, and abort. */
223     if (inuse) {
224         char *recursion_warning =
225             "luaRedisGenericCommand() recursive call detected. "
226             "Are you doing funny stuff with Lua debug hooks?";
227         serverLog(LL_WARNING,"%s",recursion_warning);
228         luaPushError(lua,recursion_warning);
229         return 1;
230     }
231     inuse++;
232 
233     /* Require at least one argument */
234     if (argc == 0) {
235         luaPushError(lua,
236             "Please specify at least one argument for redis.call()");
237         inuse--;
238         return 1;
239     }
240 
241     /* Build the arguments vector */
242     if (argv_size < argc) {
243         argv = zrealloc(argv,sizeof(robj*)*argc);
244         argv_size = argc;
245     }
246 
247     for (j = 0; j < argc; j++) {
248         char *obj_s;
249         size_t obj_len;
250         char dbuf[64];
251 
252         if (lua_type(lua,j+1) == LUA_TNUMBER) {
253             /* We can't use lua_tolstring() for number -> string conversion
254              * since Lua uses a format specifier that loses precision. */
255             lua_Number num = lua_tonumber(lua,j+1);
256 
257             obj_len = snprintf(dbuf,sizeof(dbuf),"%.17g",(double)num);
258             obj_s = dbuf;
259         } else {
260             obj_s = (char*)lua_tolstring(lua,j+1,&obj_len);
261             if (obj_s == NULL) break; /* Not a string. */
262         }
263 
264         /* Try to use a cached object. */
265         if (j < LUA_CMD_OBJCACHE_SIZE && cached_objects[j] &&
266             cached_objects_len[j] >= obj_len)
267         {
268             sds s = cached_objects[j]->ptr;
269             argv[j] = cached_objects[j];
270             cached_objects[j] = NULL;
271             memcpy(s,obj_s,obj_len+1);
272             sdssetlen(s, obj_len);
273         } else {
274             argv[j] = createStringObject(obj_s, obj_len);
275         }
276     }
277 
278     /* Check if one of the arguments passed by the Lua script
279      * is not a string or an integer (lua_isstring() return true for
280      * integers as well). */
281     if (j != argc) {
282         j--;
283         while (j >= 0) {
284             decrRefCount(argv[j]);
285             j--;
286         }
287         luaPushError(lua,
288             "Lua redis() command arguments must be strings or integers");
289         inuse--;
290         return 1;
291     }
292 
293     /* Setup our fake client for command execution */
294     c->argv = argv;
295     c->argc = argc;
296 
297     /* Command lookup */
298     cmd = lookupCommand(argv[0]->ptr);
299     if (!cmd || ((cmd->arity > 0 && cmd->arity != argc) ||
300                    (argc < -cmd->arity)))
301     {
302         if (cmd)
303             luaPushError(lua,
304                 "Wrong number of args calling Redis command From Lua script");
305         else
306             luaPushError(lua,"Unknown Redis command called from Lua script");
307         goto cleanup;
308     }
309     c->cmd = cmd;
310 
311     /* There are commands that are not allowed inside scripts. */
312     if (cmd->flags & CMD_NOSCRIPT) {
313         luaPushError(lua, "This Redis command is not allowed from scripts");
314         goto cleanup;
315     }
316 
317     /* Write commands are forbidden against read-only slaves, or if a
318      * command marked as non-deterministic was already called in the context
319      * of this script. */
320     if (cmd->flags & CMD_WRITE) {
321         if (server.lua_random_dirty && !server.lua_replicate_commands) {
322             luaPushError(lua,
323                 "Write commands not allowed after non deterministic commands. Call redis.replicate_commands() at the start of your script in order to switch to single commands replication mode.");
324             goto cleanup;
325         } else if (server.masterhost && server.repl_slave_ro &&
326                    !server.loading &&
327                    !(server.lua_caller->flags & CLIENT_MASTER))
328         {
329             luaPushError(lua, shared.roslaveerr->ptr);
330             goto cleanup;
331         } else if (server.stop_writes_on_bgsave_err &&
332                    server.saveparamslen > 0 &&
333                    server.lastbgsave_status == C_ERR)
334         {
335             luaPushError(lua, shared.bgsaveerr->ptr);
336             goto cleanup;
337         }
338     }
339 
340     /* If we reached the memory limit configured via maxmemory, commands that
341      * could enlarge the memory usage are not allowed, but only if this is the
342      * first write in the context of this script, otherwise we can't stop
343      * in the middle. */
344     if (server.maxmemory && server.lua_write_dirty == 0 &&
345         (cmd->flags & CMD_DENYOOM))
346     {
347         if (freeMemoryIfNeeded() == C_ERR) {
348             luaPushError(lua, shared.oomerr->ptr);
349             goto cleanup;
350         }
351     }
352 
353     if (cmd->flags & CMD_RANDOM) server.lua_random_dirty = 1;
354     if (cmd->flags & CMD_WRITE) server.lua_write_dirty = 1;
355 
356     /* If this is a Redis Cluster node, we need to make sure Lua is not
357      * trying to access non-local keys, with the exception of commands
358      * received from our master. */
359     if (server.cluster_enabled && !(server.lua_caller->flags & CLIENT_MASTER)) {
360         /* Duplicate relevant flags in the lua client. */
361         c->flags &= ~(CLIENT_READONLY|CLIENT_ASKING);
362         c->flags |= server.lua_caller->flags & (CLIENT_READONLY|CLIENT_ASKING);
363         if (getNodeByQuery(c,c->cmd,c->argv,c->argc,NULL,NULL) !=
364                            server.cluster->myself)
365         {
366             luaPushError(lua,
367                 "Lua script attempted to access a non local key in a "
368                 "cluster node");
369             goto cleanup;
370         }
371     }
372 
373     /* If we are using single commands replication, we need to wrap what
374      * we propagate into a MULTI/EXEC block, so that it will be atomic like
375      * a Lua script in the context of AOF and slaves. */
376     if (server.lua_replicate_commands &&
377         !server.lua_multi_emitted &&
378         server.lua_write_dirty &&
379         server.lua_repl != PROPAGATE_NONE)
380     {
381         execCommandPropagateMulti(server.lua_caller);
382         server.lua_multi_emitted = 1;
383     }
384 
385     /* Run the command */
386     int call_flags = CMD_CALL_SLOWLOG | CMD_CALL_STATS;
387     if (server.lua_replicate_commands) {
388         /* Set flags according to redis.set_repl() settings. */
389         if (server.lua_repl & PROPAGATE_AOF)
390             call_flags |= CMD_CALL_PROPAGATE_AOF;
391         if (server.lua_repl & PROPAGATE_REPL)
392             call_flags |= CMD_CALL_PROPAGATE_REPL;
393     }
394     call(c,call_flags);
395 
396     /* Convert the result of the Redis command into a suitable Lua type.
397      * The first thing we need is to create a single string from the client
398      * output buffers. */
399     if (listLength(c->reply) == 0 && c->bufpos < PROTO_REPLY_CHUNK_BYTES) {
400         /* This is a fast path for the common case of a reply inside the
401          * client static buffer. Don't create an SDS string but just use
402          * the client buffer directly. */
403         c->buf[c->bufpos] = '\0';
404         reply = c->buf;
405         c->bufpos = 0;
406     } else {
407         reply = sdsnewlen(c->buf,c->bufpos);
408         c->bufpos = 0;
409         while(listLength(c->reply)) {
410             robj *o = listNodeValue(listFirst(c->reply));
411 
412             reply = sdscatlen(reply,o->ptr,sdslen(o->ptr));
413             listDelNode(c->reply,listFirst(c->reply));
414         }
415     }
416     if (raise_error && reply[0] != '-') raise_error = 0;
417     redisProtocolToLuaType(lua,reply);
418     /* Sort the output array if needed, assuming it is a non-null multi bulk
419      * reply as expected. */
420     if ((cmd->flags & CMD_SORT_FOR_SCRIPT) &&
421         (server.lua_replicate_commands == 0) &&
422         (reply[0] == '*' && reply[1] != '-')) {
423             luaSortArray(lua);
424     }
425     if (reply != c->buf) sdsfree(reply);
426     c->reply_bytes = 0;
427 
428 cleanup:
429     /* Clean up. Command code may have changed argv/argc so we use the
430      * argv/argc of the client instead of the local variables. */
431     for (j = 0; j < c->argc; j++) {
432         robj *o = c->argv[j];
433 
434         /* Try to cache the object in the cached_objects array.
435          * The object must be small, SDS-encoded, and with refcount = 1
436          * (we must be the only owner) for us to cache it. */
437         if (j < LUA_CMD_OBJCACHE_SIZE &&
438             o->refcount == 1 &&
439             (o->encoding == OBJ_ENCODING_RAW ||
440              o->encoding == OBJ_ENCODING_EMBSTR) &&
441             sdslen(o->ptr) <= LUA_CMD_OBJCACHE_MAX_LEN)
442         {
443             sds s = o->ptr;
444             if (cached_objects[j]) decrRefCount(cached_objects[j]);
445             cached_objects[j] = o;
446             cached_objects_len[j] = sdsalloc(s);
447         } else {
448             decrRefCount(o);
449         }
450     }
451 
452     if (c->argv != argv) {
453         zfree(c->argv);
454         argv = NULL;
455         argv_size = 0;
456     }
457 
458     if (raise_error) {
459         /* If we are here we should have an error in the stack, in the
460          * form of a table with an "err" field. Extract the string to
461          * return the plain error. */
462         lua_pushstring(lua,"err");
463         lua_gettable(lua,-2);
464         inuse--;
465         return lua_error(lua);
466     }
467     inuse--;
468     return 1;
469 }
470 
471 /* redis.call() */
472 int luaRedisCallCommand(lua_State *lua) {
473     return luaRedisGenericCommand(lua,1);
474 }
475 
476 /* redis.pcall() */
477 int luaRedisPCallCommand(lua_State *lua) {
478     return luaRedisGenericCommand(lua,0);
479 }
480 
481 /* This adds redis.sha1hex(string) to Lua scripts using the same hashing
482  * function used for sha1ing lua scripts. */
483 int luaRedisSha1hexCommand(lua_State *lua) {
484     int argc = lua_gettop(lua);
485     char digest[41];
486     size_t len;
487     char *s;
488 
489     if (argc != 1) {
490         lua_pushstring(lua, "wrong number of arguments");
491         return lua_error(lua);
492     }
493 
494     s = (char*)lua_tolstring(lua,1,&len);
495     sha1hex(digest,s,len);
496     lua_pushstring(lua,digest);
497     return 1;
498 }
499 
500 /* Returns a table with a single field 'field' set to the string value
501  * passed as argument. This helper function is handy when returning
502  * a Redis Protocol error or status reply from Lua:
503  *
504  * return redis.error_reply("ERR Some Error")
505  * return redis.status_reply("ERR Some Error")
506  */
507 int luaRedisReturnSingleFieldTable(lua_State *lua, char *field) {
508     if (lua_gettop(lua) != 1 || lua_type(lua,-1) != LUA_TSTRING) {
509         luaPushError(lua, "wrong number or type of arguments");
510         return 1;
511     }
512 
513     lua_newtable(lua);
514     lua_pushstring(lua, field);
515     lua_pushvalue(lua, -3);
516     lua_settable(lua, -3);
517     return 1;
518 }
519 
520 /* redis.error_reply() */
521 int luaRedisErrorReplyCommand(lua_State *lua) {
522     return luaRedisReturnSingleFieldTable(lua,"err");
523 }
524 
525 /* redis.status_reply() */
526 int luaRedisStatusReplyCommand(lua_State *lua) {
527     return luaRedisReturnSingleFieldTable(lua,"ok");
528 }
529 
530 /* redis.replicate_commands()
531  *
532  * Turn on single commands replication if the script never called
533  * a write command so far, and returns true. Otherwise if the script
534  * already started to write, returns false and stick to whole scripts
535  * replication, which is our default. */
536 int luaRedisReplicateCommandsCommand(lua_State *lua) {
537     if (server.lua_write_dirty) {
538         lua_pushboolean(lua,0);
539     } else {
540         server.lua_replicate_commands = 1;
541         /* When we switch to single commands replication, we can provide
542          * different math.random() sequences at every call, which is what
543          * the user normally expects. */
544         redisSrand48(rand());
545         lua_pushboolean(lua,1);
546     }
547     return 1;
548 }
549 
550 /* redis.set_repl()
551  *
552  * Set the propagation of write commands executed in the context of the
553  * script to on/off for AOF and slaves. */
554 int luaRedisSetReplCommand(lua_State *lua) {
555     int argc = lua_gettop(lua);
556     int flags;
557 
558     if (server.lua_replicate_commands == 0) {
559         lua_pushstring(lua, "You can set the replication behavior only after turning on single commands replication with redis.replicate_commands().");
560         return lua_error(lua);
561     } else if (argc != 1) {
562         lua_pushstring(lua, "redis.set_repl() requires two arguments.");
563         return lua_error(lua);
564     }
565 
566     flags = lua_tonumber(lua,-1);
567     if ((flags & ~(PROPAGATE_AOF|PROPAGATE_REPL)) != 0) {
568         lua_pushstring(lua, "Invalid replication flags. Use REPL_AOF, REPL_SLAVE, REPL_ALL or REPL_NONE.");
569         return lua_error(lua);
570     }
571     server.lua_repl = flags;
572     return 0;
573 }
574 
575 /* redis.log() */
576 int luaLogCommand(lua_State *lua) {
577     int j, argc = lua_gettop(lua);
578     int level;
579     sds log;
580 
581     if (argc < 2) {
582         lua_pushstring(lua, "redis.log() requires two arguments or more.");
583         return lua_error(lua);
584     } else if (!lua_isnumber(lua,-argc)) {
585         lua_pushstring(lua, "First argument must be a number (log level).");
586         return lua_error(lua);
587     }
588     level = lua_tonumber(lua,-argc);
589     if (level < LL_DEBUG || level > LL_WARNING) {
590         lua_pushstring(lua, "Invalid debug level.");
591         return lua_error(lua);
592     }
593 
594     /* Glue together all the arguments */
595     log = sdsempty();
596     for (j = 1; j < argc; j++) {
597         size_t len;
598         char *s;
599 
600         s = (char*)lua_tolstring(lua,(-argc)+j,&len);
601         if (s) {
602             if (j != 1) log = sdscatlen(log," ",1);
603             log = sdscatlen(log,s,len);
604         }
605     }
606     serverLogRaw(level,log);
607     sdsfree(log);
608     return 0;
609 }
610 
611 void luaMaskCountHook(lua_State *lua, lua_Debug *ar) {
612     long long elapsed;
613     UNUSED(ar);
614     UNUSED(lua);
615 
616     elapsed = mstime() - server.lua_time_start;
617     if (elapsed >= server.lua_time_limit && server.lua_timedout == 0) {
618         serverLog(LL_WARNING,"Lua slow script detected: still in execution after %lld milliseconds. You can try killing the script using the SCRIPT KILL command.",elapsed);
619         server.lua_timedout = 1;
620         /* Once the script timeouts we reenter the event loop to permit others
621          * to call SCRIPT KILL or SHUTDOWN NOSAVE if needed. For this reason
622          * we need to mask the client executing the script from the event loop.
623          * If we don't do that the client may disconnect and could no longer be
624          * here when the EVAL command will return. */
625          aeDeleteFileEvent(server.el, server.lua_caller->fd, AE_READABLE);
626     }
627     if (server.lua_timedout) processEventsWhileBlocked();
628     if (server.lua_kill) {
629         serverLog(LL_WARNING,"Lua script killed by user with SCRIPT KILL.");
630         lua_pushstring(lua,"Script killed by user with SCRIPT KILL...");
631         lua_error(lua);
632     }
633 }
634 
635 void luaLoadLib(lua_State *lua, const char *libname, lua_CFunction luafunc) {
636   lua_pushcfunction(lua, luafunc);
637   lua_pushstring(lua, libname);
638   lua_call(lua, 1, 0);
639 }
640 
641 LUALIB_API int (luaopen_cjson) (lua_State *L);
642 LUALIB_API int (luaopen_struct) (lua_State *L);
643 LUALIB_API int (luaopen_cmsgpack) (lua_State *L);
644 LUALIB_API int (luaopen_bit) (lua_State *L);
645 
646 void luaLoadLibraries(lua_State *lua) {
647     luaLoadLib(lua, "", luaopen_base);
648     luaLoadLib(lua, LUA_TABLIBNAME, luaopen_table);
649     luaLoadLib(lua, LUA_STRLIBNAME, luaopen_string);
650     luaLoadLib(lua, LUA_MATHLIBNAME, luaopen_math);
651     luaLoadLib(lua, LUA_DBLIBNAME, luaopen_debug);
652     luaLoadLib(lua, "cjson", luaopen_cjson);
653     luaLoadLib(lua, "struct", luaopen_struct);
654     luaLoadLib(lua, "cmsgpack", luaopen_cmsgpack);
655     luaLoadLib(lua, "bit", luaopen_bit);
656 
657 #if 0 /* Stuff that we don't load currently, for sandboxing concerns. */
658     luaLoadLib(lua, LUA_LOADLIBNAME, luaopen_package);
659     luaLoadLib(lua, LUA_OSLIBNAME, luaopen_os);
660 #endif
661 }
662 
663 /* Remove a functions that we don't want to expose to the Redis scripting
664  * environment. */
665 void luaRemoveUnsupportedFunctions(lua_State *lua) {
666     lua_pushnil(lua);
667     lua_setglobal(lua,"loadfile");
668 }
669 
670 /* This function installs metamethods in the global table _G that prevent
671  * the creation of globals accidentally.
672  *
673  * It should be the last to be called in the scripting engine initialization
674  * sequence, because it may interact with creation of globals. */
675 void scriptingEnableGlobalsProtection(lua_State *lua) {
676     char *s[32];
677     sds code = sdsempty();
678     int j = 0;
679 
680     /* strict.lua from: http://metalua.luaforge.net/src/lib/strict.lua.html.
681      * Modified to be adapted to Redis. */
682     s[j++]="local dbg=debug\n";
683     s[j++]="local mt = {}\n";
684     s[j++]="setmetatable(_G, mt)\n";
685     s[j++]="mt.__newindex = function (t, n, v)\n";
686     s[j++]="  if dbg.getinfo(2) then\n";
687     s[j++]="    local w = dbg.getinfo(2, \"S\").what\n";
688     s[j++]="    if w ~= \"main\" and w ~= \"C\" then\n";
689     s[j++]="      error(\"Script attempted to create global variable '\"..tostring(n)..\"'\", 2)\n";
690     s[j++]="    end\n";
691     s[j++]="  end\n";
692     s[j++]="  rawset(t, n, v)\n";
693     s[j++]="end\n";
694     s[j++]="mt.__index = function (t, n)\n";
695     s[j++]="  if dbg.getinfo(2) and dbg.getinfo(2, \"S\").what ~= \"C\" then\n";
696     s[j++]="    error(\"Script attempted to access unexisting global variable '\"..tostring(n)..\"'\", 2)\n";
697     s[j++]="  end\n";
698     s[j++]="  return rawget(t, n)\n";
699     s[j++]="end\n";
700     s[j++]="debug = nil\n";
701     s[j++]=NULL;
702 
703     for (j = 0; s[j] != NULL; j++) code = sdscatlen(code,s[j],strlen(s[j]));
704     luaL_loadbuffer(lua,code,sdslen(code),"@enable_strict_lua");
705     lua_pcall(lua,0,0,0);
706     sdsfree(code);
707 }
708 
709 /* Initialize the scripting environment.
710  * It is possible to call this function to reset the scripting environment
711  * assuming that we call scriptingRelease() before.
712  * See scriptingReset() for more information. */
713 void scriptingInit(void) {
714     lua_State *lua = lua_open();
715 
716     luaLoadLibraries(lua);
717     luaRemoveUnsupportedFunctions(lua);
718 
719     /* Initialize a dictionary we use to map SHAs to scripts.
720      * This is useful for replication, as we need to replicate EVALSHA
721      * as EVAL, so we need to remember the associated script. */
722     server.lua_scripts = dictCreate(&shaScriptObjectDictType,NULL);
723 
724     /* Register the redis commands table and fields */
725     lua_newtable(lua);
726 
727     /* redis.call */
728     lua_pushstring(lua,"call");
729     lua_pushcfunction(lua,luaRedisCallCommand);
730     lua_settable(lua,-3);
731 
732     /* redis.pcall */
733     lua_pushstring(lua,"pcall");
734     lua_pushcfunction(lua,luaRedisPCallCommand);
735     lua_settable(lua,-3);
736 
737     /* redis.log and log levels. */
738     lua_pushstring(lua,"log");
739     lua_pushcfunction(lua,luaLogCommand);
740     lua_settable(lua,-3);
741 
742     lua_pushstring(lua,"LL_DEBUG");
743     lua_pushnumber(lua,LL_DEBUG);
744     lua_settable(lua,-3);
745 
746     lua_pushstring(lua,"LL_VERBOSE");
747     lua_pushnumber(lua,LL_VERBOSE);
748     lua_settable(lua,-3);
749 
750     lua_pushstring(lua,"LL_NOTICE");
751     lua_pushnumber(lua,LL_NOTICE);
752     lua_settable(lua,-3);
753 
754     lua_pushstring(lua,"LL_WARNING");
755     lua_pushnumber(lua,LL_WARNING);
756     lua_settable(lua,-3);
757 
758     /* redis.sha1hex */
759     lua_pushstring(lua, "sha1hex");
760     lua_pushcfunction(lua, luaRedisSha1hexCommand);
761     lua_settable(lua, -3);
762 
763     /* redis.error_reply and redis.status_reply */
764     lua_pushstring(lua, "error_reply");
765     lua_pushcfunction(lua, luaRedisErrorReplyCommand);
766     lua_settable(lua, -3);
767     lua_pushstring(lua, "status_reply");
768     lua_pushcfunction(lua, luaRedisStatusReplyCommand);
769     lua_settable(lua, -3);
770 
771     /* redis.replicate_commands */
772     lua_pushstring(lua, "replicate_commands");
773     lua_pushcfunction(lua, luaRedisReplicateCommandsCommand);
774     lua_settable(lua, -3);
775 
776     /* redis.set_repl and associated flags. */
777     lua_pushstring(lua,"set_repl");
778     lua_pushcfunction(lua,luaRedisSetReplCommand);
779     lua_settable(lua,-3);
780 
781     lua_pushstring(lua,"REPL_NONE");
782     lua_pushnumber(lua,PROPAGATE_NONE);
783     lua_settable(lua,-3);
784 
785     lua_pushstring(lua,"REPL_AOF");
786     lua_pushnumber(lua,PROPAGATE_AOF);
787     lua_settable(lua,-3);
788 
789     lua_pushstring(lua,"REPL_SLAVE");
790     lua_pushnumber(lua,PROPAGATE_REPL);
791     lua_settable(lua,-3);
792 
793     lua_pushstring(lua,"REPL_ALL");
794     lua_pushnumber(lua,PROPAGATE_AOF|PROPAGATE_REPL);
795     lua_settable(lua,-3);
796 
797     /* Finally set the table as 'redis' global var. */
798     lua_setglobal(lua,"redis");
799 
800     /* Replace math.random and math.randomseed with our implementations. */
801     lua_getglobal(lua,"math");
802 
803     lua_pushstring(lua,"random");
804     lua_pushcfunction(lua,redis_math_random);
805     lua_settable(lua,-3);
806 
807     lua_pushstring(lua,"randomseed");
808     lua_pushcfunction(lua,redis_math_randomseed);
809     lua_settable(lua,-3);
810 
811     lua_setglobal(lua,"math");
812 
813     /* Add a helper function that we use to sort the multi bulk output of non
814      * deterministic commands, when containing 'false' elements. */
815     {
816         char *compare_func =    "function __redis__compare_helper(a,b)\n"
817                                 "  if a == false then a = '' end\n"
818                                 "  if b == false then b = '' end\n"
819                                 "  return a<b\n"
820                                 "end\n";
821         luaL_loadbuffer(lua,compare_func,strlen(compare_func),"@cmp_func_def");
822         lua_pcall(lua,0,0,0);
823     }
824 
825     /* Add a helper function we use for pcall error reporting.
826      * Note that when the error is in the C function we want to report the
827      * information about the caller, that's what makes sense from the point
828      * of view of the user debugging a script. */
829     {
830         char *errh_func =       "local dbg = debug\n"
831                                 "function __redis__err__handler(err)\n"
832                                 "  local i = dbg.getinfo(2,'nSl')\n"
833                                 "  if i and i.what == 'C' then\n"
834                                 "    i = dbg.getinfo(3,'nSl')\n"
835                                 "  end\n"
836                                 "  if i then\n"
837                                 "    return i.source .. ':' .. i.currentline .. ': ' .. err\n"
838                                 "  else\n"
839                                 "    return err\n"
840                                 "  end\n"
841                                 "end\n";
842         luaL_loadbuffer(lua,errh_func,strlen(errh_func),"@err_handler_def");
843         lua_pcall(lua,0,0,0);
844     }
845 
846     /* Create the (non connected) client that we use to execute Redis commands
847      * inside the Lua interpreter.
848      * Note: there is no need to create it again when this function is called
849      * by scriptingReset(). */
850     if (server.lua_client == NULL) {
851         server.lua_client = createClient(-1);
852         server.lua_client->flags |= CLIENT_LUA;
853     }
854 
855     /* Lua beginners often don't use "local", this is likely to introduce
856      * subtle bugs in their code. To prevent problems we protect accesses
857      * to global variables. */
858     scriptingEnableGlobalsProtection(lua);
859 
860     server.lua = lua;
861 }
862 
863 /* Release resources related to Lua scripting.
864  * This function is used in order to reset the scripting environment. */
865 void scriptingRelease(void) {
866     dictRelease(server.lua_scripts);
867     lua_close(server.lua);
868 }
869 
870 void scriptingReset(void) {
871     scriptingRelease();
872     scriptingInit();
873 }
874 
875 /* Perform the SHA1 of the input string. We use this both for hashing script
876  * bodies in order to obtain the Lua function name, and in the implementation
877  * of redis.sha1().
878  *
879  * 'digest' should point to a 41 bytes buffer: 40 for SHA1 converted into an
880  * hexadecimal number, plus 1 byte for null term. */
881 void sha1hex(char *digest, char *script, size_t len) {
882     SHA1_CTX ctx;
883     unsigned char hash[20];
884     char *cset = "0123456789abcdef";
885     int j;
886 
887     SHA1Init(&ctx);
888     SHA1Update(&ctx,(unsigned char*)script,len);
889     SHA1Final(hash,&ctx);
890 
891     for (j = 0; j < 20; j++) {
892         digest[j*2] = cset[((hash[j]&0xF0)>>4)];
893         digest[j*2+1] = cset[(hash[j]&0xF)];
894     }
895     digest[40] = '\0';
896 }
897 
898 void luaReplyToRedisReply(client *c, lua_State *lua) {
899     int t = lua_type(lua,-1);
900 
901     switch(t) {
902     case LUA_TSTRING:
903         addReplyBulkCBuffer(c,(char*)lua_tostring(lua,-1),lua_strlen(lua,-1));
904         break;
905     case LUA_TBOOLEAN:
906         addReply(c,lua_toboolean(lua,-1) ? shared.cone : shared.nullbulk);
907         break;
908     case LUA_TNUMBER:
909         addReplyLongLong(c,(long long)lua_tonumber(lua,-1));
910         break;
911     case LUA_TTABLE:
912         /* We need to check if it is an array, an error, or a status reply.
913          * Error are returned as a single element table with 'err' field.
914          * Status replies are returned as single element table with 'ok'
915          * field. */
916         lua_pushstring(lua,"err");
917         lua_gettable(lua,-2);
918         t = lua_type(lua,-1);
919         if (t == LUA_TSTRING) {
920             sds err = sdsnew(lua_tostring(lua,-1));
921             sdsmapchars(err,"\r\n","  ",2);
922             addReplySds(c,sdscatprintf(sdsempty(),"-%s\r\n",err));
923             sdsfree(err);
924             lua_pop(lua,2);
925             return;
926         }
927 
928         lua_pop(lua,1);
929         lua_pushstring(lua,"ok");
930         lua_gettable(lua,-2);
931         t = lua_type(lua,-1);
932         if (t == LUA_TSTRING) {
933             sds ok = sdsnew(lua_tostring(lua,-1));
934             sdsmapchars(ok,"\r\n","  ",2);
935             addReplySds(c,sdscatprintf(sdsempty(),"+%s\r\n",ok));
936             sdsfree(ok);
937             lua_pop(lua,1);
938         } else {
939             void *replylen = addDeferredMultiBulkLength(c);
940             int j = 1, mbulklen = 0;
941 
942             lua_pop(lua,1); /* Discard the 'ok' field value we popped */
943             while(1) {
944                 lua_pushnumber(lua,j++);
945                 lua_gettable(lua,-2);
946                 t = lua_type(lua,-1);
947                 if (t == LUA_TNIL) {
948                     lua_pop(lua,1);
949                     break;
950                 }
951                 luaReplyToRedisReply(c, lua);
952                 mbulklen++;
953             }
954             setDeferredMultiBulkLength(c,replylen,mbulklen);
955         }
956         break;
957     default:
958         addReply(c,shared.nullbulk);
959     }
960     lua_pop(lua,1);
961 }
962 
963 /* Set an array of Redis String Objects as a Lua array (table) stored into a
964  * global variable. */
965 void luaSetGlobalArray(lua_State *lua, char *var, robj **elev, int elec) {
966     int j;
967 
968     lua_newtable(lua);
969     for (j = 0; j < elec; j++) {
970         lua_pushlstring(lua,(char*)elev[j]->ptr,sdslen(elev[j]->ptr));
971         lua_rawseti(lua,-2,j+1);
972     }
973     lua_setglobal(lua,var);
974 }
975 
976 /* Define a lua function with the specified function name and body.
977  * The function name musts be a 42 characters long string, since all the
978  * functions we defined in the Lua context are in the form:
979  *
980  *   f_<hex sha1 sum>
981  *
982  * On success C_OK is returned, and nothing is left on the Lua stack.
983  * On error C_ERR is returned and an appropriate error is set in the
984  * client context. */
985 int luaCreateFunction(client *c, lua_State *lua, char *funcname, robj *body) {
986     sds funcdef = sdsempty();
987 
988     funcdef = sdscat(funcdef,"function ");
989     funcdef = sdscatlen(funcdef,funcname,42);
990     funcdef = sdscatlen(funcdef,"() ",3);
991     funcdef = sdscatlen(funcdef,body->ptr,sdslen(body->ptr));
992     funcdef = sdscatlen(funcdef," end",4);
993 
994     if (luaL_loadbuffer(lua,funcdef,sdslen(funcdef),"@user_script")) {
995         addReplyErrorFormat(c,"Error compiling script (new function): %s\n",
996             lua_tostring(lua,-1));
997         lua_pop(lua,1);
998         sdsfree(funcdef);
999         return C_ERR;
1000     }
1001     sdsfree(funcdef);
1002     if (lua_pcall(lua,0,0,0)) {
1003         addReplyErrorFormat(c,"Error running script (new function): %s\n",
1004             lua_tostring(lua,-1));
1005         lua_pop(lua,1);
1006         return C_ERR;
1007     }
1008 
1009     /* We also save a SHA1 -> Original script map in a dictionary
1010      * so that we can replicate / write in the AOF all the
1011      * EVALSHA commands as EVAL using the original script. */
1012     {
1013         int retval = dictAdd(server.lua_scripts,
1014                              sdsnewlen(funcname+2,40),body);
1015         serverAssertWithInfo(c,NULL,retval == DICT_OK);
1016         incrRefCount(body);
1017     }
1018     return C_OK;
1019 }
1020 
1021 void evalGenericCommand(client *c, int evalsha) {
1022     lua_State *lua = server.lua;
1023     char funcname[43];
1024     long long numkeys;
1025     int delhook = 0, err;
1026 
1027     /* When we replicate whole scripts, we want the same PRNG sequence at
1028      * every call so that our PRNG is not affected by external state. */
1029     redisSrand48(0);
1030 
1031     /* We set this flag to zero to remember that so far no random command
1032      * was called. This way we can allow the user to call commands like
1033      * SRANDMEMBER or RANDOMKEY from Lua scripts as far as no write command
1034      * is called (otherwise the replication and AOF would end with non
1035      * deterministic sequences).
1036      *
1037      * Thanks to this flag we'll raise an error every time a write command
1038      * is called after a random command was used. */
1039     server.lua_random_dirty = 0;
1040     server.lua_write_dirty = 0;
1041     server.lua_replicate_commands = server.lua_always_replicate_commands;
1042     server.lua_multi_emitted = 0;
1043     server.lua_repl = PROPAGATE_AOF|PROPAGATE_REPL;
1044 
1045     /* Get the number of arguments that are keys */
1046     if (getLongLongFromObjectOrReply(c,c->argv[2],&numkeys,NULL) != C_OK)
1047         return;
1048     if (numkeys > (c->argc - 3)) {
1049         addReplyError(c,"Number of keys can't be greater than number of args");
1050         return;
1051     } else if (numkeys < 0) {
1052         addReplyError(c,"Number of keys can't be negative");
1053         return;
1054     }
1055 
1056     /* We obtain the script SHA1, then check if this function is already
1057      * defined into the Lua state */
1058     funcname[0] = 'f';
1059     funcname[1] = '_';
1060     if (!evalsha) {
1061         /* Hash the code if this is an EVAL call */
1062         sha1hex(funcname+2,c->argv[1]->ptr,sdslen(c->argv[1]->ptr));
1063     } else {
1064         /* We already have the SHA if it is a EVALSHA */
1065         int j;
1066         char *sha = c->argv[1]->ptr;
1067 
1068         /* Convert to lowercase. We don't use tolower since the function
1069          * managed to always show up in the profiler output consuming
1070          * a non trivial amount of time. */
1071         for (j = 0; j < 40; j++)
1072             funcname[j+2] = (sha[j] >= 'A' && sha[j] <= 'Z') ?
1073                 sha[j]+('a'-'A') : sha[j];
1074         funcname[42] = '\0';
1075     }
1076 
1077     /* Push the pcall error handler function on the stack. */
1078     lua_getglobal(lua, "__redis__err__handler");
1079 
1080     /* Try to lookup the Lua function */
1081     lua_getglobal(lua, funcname);
1082     if (lua_isnil(lua,-1)) {
1083         lua_pop(lua,1); /* remove the nil from the stack */
1084         /* Function not defined... let's define it if we have the
1085          * body of the function. If this is an EVALSHA call we can just
1086          * return an error. */
1087         if (evalsha) {
1088             lua_pop(lua,1); /* remove the error handler from the stack. */
1089             addReply(c, shared.noscripterr);
1090             return;
1091         }
1092         if (luaCreateFunction(c,lua,funcname,c->argv[1]) == C_ERR) {
1093             lua_pop(lua,1); /* remove the error handler from the stack. */
1094             /* The error is sent to the client by luaCreateFunction()
1095              * itself when it returns C_ERR. */
1096             return;
1097         }
1098         /* Now the following is guaranteed to return non nil */
1099         lua_getglobal(lua, funcname);
1100         serverAssert(!lua_isnil(lua,-1));
1101     }
1102 
1103     /* Populate the argv and keys table accordingly to the arguments that
1104      * EVAL received. */
1105     luaSetGlobalArray(lua,"KEYS",c->argv+3,numkeys);
1106     luaSetGlobalArray(lua,"ARGV",c->argv+3+numkeys,c->argc-3-numkeys);
1107 
1108     /* Select the right DB in the context of the Lua client */
1109     selectDb(server.lua_client,c->db->id);
1110 
1111     /* Set a hook in order to be able to stop the script execution if it
1112      * is running for too much time.
1113      * We set the hook only if the time limit is enabled as the hook will
1114      * make the Lua script execution slower. */
1115     server.lua_caller = c;
1116     server.lua_time_start = mstime();
1117     server.lua_kill = 0;
1118     if (server.lua_time_limit > 0 && server.masterhost == NULL) {
1119         lua_sethook(lua,luaMaskCountHook,LUA_MASKCOUNT,100000);
1120         delhook = 1;
1121     }
1122 
1123     /* At this point whether this script was never seen before or if it was
1124      * already defined, we can call it. We have zero arguments and expect
1125      * a single return value. */
1126     err = lua_pcall(lua,0,1,-2);
1127 
1128     /* Perform some cleanup that we need to do both on error and success. */
1129     if (delhook) lua_sethook(lua,luaMaskCountHook,0,0); /* Disable hook */
1130     if (server.lua_timedout) {
1131         server.lua_timedout = 0;
1132         /* Restore the readable handler that was unregistered when the
1133          * script timeout was detected. */
1134         aeCreateFileEvent(server.el,c->fd,AE_READABLE,
1135                           readQueryFromClient,c);
1136     }
1137     server.lua_caller = NULL;
1138 
1139     /* Call the Lua garbage collector from time to time to avoid a
1140      * full cycle performed by Lua, which adds too latency.
1141      *
1142      * The call is performed every LUA_GC_CYCLE_PERIOD executed commands
1143      * (and for LUA_GC_CYCLE_PERIOD collection steps) because calling it
1144      * for every command uses too much CPU. */
1145     #define LUA_GC_CYCLE_PERIOD 50
1146     {
1147         static long gc_count = 0;
1148 
1149         gc_count++;
1150         if (gc_count == LUA_GC_CYCLE_PERIOD) {
1151             lua_gc(lua,LUA_GCSTEP,LUA_GC_CYCLE_PERIOD);
1152             gc_count = 0;
1153         }
1154     }
1155 
1156     if (err) {
1157         addReplyErrorFormat(c,"Error running script (call to %s): %s\n",
1158             funcname, lua_tostring(lua,-1));
1159         lua_pop(lua,2); /* Consume the Lua reply and remove error handler. */
1160     } else {
1161         /* On success convert the Lua return value into Redis protocol, and
1162          * send it to * the client. */
1163         luaReplyToRedisReply(c,lua); /* Convert and consume the reply. */
1164         lua_pop(lua,1); /* Remove the error handler. */
1165     }
1166 
1167     /* If we are using single commands replication, emit EXEC if there
1168      * was at least a write. */
1169     if (server.lua_replicate_commands) {
1170         preventCommandPropagation(c);
1171         if (server.lua_multi_emitted) {
1172             robj *propargv[1];
1173             propargv[0] = createStringObject("EXEC",4);
1174             alsoPropagate(server.execCommand,c->db->id,propargv,1,
1175                 PROPAGATE_AOF|PROPAGATE_REPL);
1176             decrRefCount(propargv[0]);
1177         }
1178     }
1179 
1180     /* EVALSHA should be propagated to Slave and AOF file as full EVAL, unless
1181      * we are sure that the script was already in the context of all the
1182      * attached slaves *and* the current AOF file if enabled.
1183      *
1184      * To do so we use a cache of SHA1s of scripts that we already propagated
1185      * as full EVAL, that's called the Replication Script Cache.
1186      *
1187      * For repliation, everytime a new slave attaches to the master, we need to
1188      * flush our cache of scripts that can be replicated as EVALSHA, while
1189      * for AOF we need to do so every time we rewrite the AOF file. */
1190     if (evalsha && !server.lua_replicate_commands) {
1191         if (!replicationScriptCacheExists(c->argv[1]->ptr)) {
1192             /* This script is not in our script cache, replicate it as
1193              * EVAL, then add it into the script cache, as from now on
1194              * slaves and AOF know about it. */
1195             robj *script = dictFetchValue(server.lua_scripts,c->argv[1]->ptr);
1196 
1197             replicationScriptCacheAdd(c->argv[1]->ptr);
1198             serverAssertWithInfo(c,NULL,script != NULL);
1199             rewriteClientCommandArgument(c,0,
1200                 resetRefCount(createStringObject("EVAL",4)));
1201             rewriteClientCommandArgument(c,1,script);
1202             printf("forceCommandPropagation\n");
1203             forceCommandPropagation(c,PROPAGATE_REPL|PROPAGATE_AOF);
1204         }
1205     }
1206 }
1207 
1208 void evalCommand(client *c) {
1209     evalGenericCommand(c,0);
1210 }
1211 
1212 void evalShaCommand(client *c) {
1213     if (sdslen(c->argv[1]->ptr) != 40) {
1214         /* We know that a match is not possible if the provided SHA is
1215          * not the right length. So we return an error ASAP, this way
1216          * evalGenericCommand() can be implemented without string length
1217          * sanity check */
1218         addReply(c, shared.noscripterr);
1219         return;
1220     }
1221     evalGenericCommand(c,1);
1222 }
1223 
1224 /* We replace math.random() with our implementation that is not affected
1225  * by specific libc random() implementations and will output the same sequence
1226  * (for the same seed) in every arch. */
1227 
1228 /* The following implementation is the one shipped with Lua itself but with
1229  * rand() replaced by redisLrand48(). */
1230 int redis_math_random (lua_State *L) {
1231   /* the `%' avoids the (rare) case of r==1, and is needed also because on
1232      some systems (SunOS!) `rand()' may return a value larger than RAND_MAX */
1233   lua_Number r = (lua_Number)(redisLrand48()%REDIS_LRAND48_MAX) /
1234                                 (lua_Number)REDIS_LRAND48_MAX;
1235   switch (lua_gettop(L)) {  /* check number of arguments */
1236     case 0: {  /* no arguments */
1237       lua_pushnumber(L, r);  /* Number between 0 and 1 */
1238       break;
1239     }
1240     case 1: {  /* only upper limit */
1241       int u = luaL_checkint(L, 1);
1242       luaL_argcheck(L, 1<=u, 1, "interval is empty");
1243       lua_pushnumber(L, floor(r*u)+1);  /* int between 1 and `u' */
1244       break;
1245     }
1246     case 2: {  /* lower and upper limits */
1247       int l = luaL_checkint(L, 1);
1248       int u = luaL_checkint(L, 2);
1249       luaL_argcheck(L, l<=u, 2, "interval is empty");
1250       lua_pushnumber(L, floor(r*(u-l+1))+l);  /* int between `l' and `u' */
1251       break;
1252     }
1253     default: return luaL_error(L, "wrong number of arguments");
1254   }
1255   return 1;
1256 }
1257 
1258 int redis_math_randomseed (lua_State *L) {
1259   redisSrand48(luaL_checkint(L, 1));
1260   return 0;
1261 }
1262 
1263 /* ---------------------------------------------------------------------------
1264  * SCRIPT command for script environment introspection and control
1265  * ------------------------------------------------------------------------- */
1266 
1267 void scriptCommand(client *c) {
1268     if (c->argc == 2 && !strcasecmp(c->argv[1]->ptr,"flush")) {
1269         scriptingReset();
1270         addReply(c,shared.ok);
1271         replicationScriptCacheFlush();
1272         server.dirty++; /* Propagating this command is a good idea. */
1273     } else if (c->argc >= 2 && !strcasecmp(c->argv[1]->ptr,"exists")) {
1274         int j;
1275 
1276         addReplyMultiBulkLen(c, c->argc-2);
1277         for (j = 2; j < c->argc; j++) {
1278             if (dictFind(server.lua_scripts,c->argv[j]->ptr))
1279                 addReply(c,shared.cone);
1280             else
1281                 addReply(c,shared.czero);
1282         }
1283     } else if (c->argc == 3 && !strcasecmp(c->argv[1]->ptr,"load")) {
1284         char funcname[43];
1285         sds sha;
1286 
1287         funcname[0] = 'f';
1288         funcname[1] = '_';
1289         sha1hex(funcname+2,c->argv[2]->ptr,sdslen(c->argv[2]->ptr));
1290         sha = sdsnewlen(funcname+2,40);
1291         if (dictFind(server.lua_scripts,sha) == NULL) {
1292             if (luaCreateFunction(c,server.lua,funcname,c->argv[2])
1293                     == C_ERR) {
1294                 sdsfree(sha);
1295                 return;
1296             }
1297         }
1298         addReplyBulkCBuffer(c,funcname+2,40);
1299         sdsfree(sha);
1300         forceCommandPropagation(c,PROPAGATE_REPL|PROPAGATE_AOF);
1301     } else if (c->argc == 2 && !strcasecmp(c->argv[1]->ptr,"kill")) {
1302         if (server.lua_caller == NULL) {
1303             addReplySds(c,sdsnew("-NOTBUSY No scripts in execution right now.\r\n"));
1304         } else if (server.lua_write_dirty) {
1305             addReplySds(c,sdsnew("-UNKILLABLE Sorry the script already executed write commands against the dataset. You can either wait the script termination or kill the server in a hard way using the SHUTDOWN NOSAVE command.\r\n"));
1306         } else {
1307             server.lua_kill = 1;
1308             addReply(c,shared.ok);
1309         }
1310     } else {
1311         addReplyError(c, "Unknown SCRIPT subcommand or wrong # of args.");
1312     }
1313 }
1314