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