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