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