/*** Wait for child process to terminate. @function wait @int[opt=-1] pid child process id to wait for, or -1 for any child process @int[opt] options bitwise OR of `WNOHANG` and `WUNTRACED` @treturn[1] int pid of running child, if not exited yet and called with `WNOHANG` @treturn[1] string "running" @treturn[2] int pid of terminated child, if successful @treturn[2] string "exited", "killed" or "stopped" @treturn[2] int exit status, or signal number responsible for "killed" or "stopped" @return[3] nil @treturn[3] string error message @treturn[3] int errnum @see waitpid(2) @see posix.unistd.fork */ static int Pwait(lua_State *L) { int status = 0; pid_t pid = optint(L, 1, -1); int options = optint(L, 2, 0); checknargs(L, 2); pid = waitpid(pid, &status, options); if (pid == -1) return pusherror(L, NULL); lua_pushinteger(L, pid); if (pid == 0) { lua_pushliteral(L,"running"); return 2; } else if (WIFEXITED(status)) { lua_pushliteral(L,"exited"); lua_pushinteger(L, WEXITSTATUS(status)); return 3; } else if (WIFSIGNALED(status)) { lua_pushliteral(L,"killed"); lua_pushinteger(L, WTERMSIG(status)); return 3; } else if (WIFSTOPPED(status)) { lua_pushliteral(L,"stopped"); lua_pushinteger(L, WSTOPSIG(status)); return 3; } return 1; }
/*** Manipulate file descriptor. @function fcntl @int fd file descriptor to act on @int cmd operation to perform @tparam[opt=0] int|flock arg when *cmd* is `F_GETLK`, `F_SETLK` or `F_SETLKW`, then *arg* is a @{flock} table, otherwise an integer with meaning dependent upon the value of *cmd*. @return[1] integer return value depending on *cmd*, if successful @return[2] nil @treturn[2] string error message @treturn[2] int errnum @see fcntl(2) @see lock.lua @usage local flag = P.fcntl (fd, P.F_GETFL) */ static int Pfcntl(lua_State *L) { int fd = checkint(L, 1); int cmd = checkint(L, 2); int arg; struct flock lockinfo; int r; checknargs(L, 3); switch (cmd) { case F_SETLK: case F_SETLKW: case F_GETLK: luaL_checktype(L, 3, LUA_TTABLE); /* Copy fields to flock struct */ lua_getfield(L, 3, "l_type"); lockinfo.l_type = (short)lua_tointeger(L, -1); lua_getfield(L, 3, "l_whence"); lockinfo.l_whence = (short)lua_tointeger(L, -1); lua_getfield(L, 3, "l_start"); lockinfo.l_start = (off_t)lua_tointeger(L, -1); lua_getfield(L, 3, "l_len"); lockinfo.l_len = (off_t)lua_tointeger(L, -1); /* Lock */ r = fcntl(fd, cmd, &lockinfo); /* Copy fields from flock struct */ lua_pushinteger(L, lockinfo.l_type); lua_setfield(L, 3, "l_type"); lua_pushinteger(L, lockinfo.l_whence); lua_setfield(L, 3, "l_whence"); lua_pushinteger(L, lockinfo.l_start); lua_setfield(L, 3, "l_start"); lua_pushinteger(L, lockinfo.l_len); lua_setfield(L, 3, "l_len"); lua_pushinteger(L, lockinfo.l_pid); lua_setfield(L, 3, "l_pid"); break; default: arg = optint(L, 3, 0); r = fcntl(fd, cmd, arg); break; } return pushresult(L, r, "fcntl"); }
/*** Set log priority mask. @function setlogmask @int mask bitwise OR of @{LOG_MASK} bits. @treturn[1] int previous mask, if successful @return[2] nil @treturn[2] string error message @treturn[2] int errnum @see setlogmask(3) */ static int Psetlogmask(lua_State *L) { checknargs(L, 1); return pushresult(L, setlogmask(optint(L, 1, 0)), "setlogmask"); }
int main(int argc, char *argv[]) { #if defined(__WIN32__) WSADATA WSAData; if(WSAStartup(MAKEWORD(2, 2), &WSAData))//初始化 { printf("initializationing error!\n"); WSACleanup(); exit(0); } #endif const char * config_file = "config"; if (argc > 1) { config_file = argv[1]; } skynet_globalinit(); skynet_env_init(); #if !defined(__WIN32__) sigign(); #endif struct skynet_config config; struct lua_State *L = lua_newstate(skynet_lalloc, NULL); luaL_openlibs(L); // link lua lib lua_close(L); L = luaL_newstate(); int err = luaL_dofile(L, config_file); if (err) { fprintf(stderr,"%s\n",lua_tostring(L,-1)); lua_close(L); return 1; } _init_env(L); #ifdef LUA_CACHELIB printf("Skynet lua code cache enable\n"); #endif config.thread = optint("thread",8); config.module_path = optstring("cpath","./cservice/?.so"); config.logger = optstring("logger",NULL); config.harbor = optint("harbor", 1); config.master = optstring("master","127.0.0.1:2012"); config.bootstrap = optstring("bootstrap","snlua bootstrap"); config.local = optstring("address","127.0.0.1:2525"); config.standalone = optstring("standalone",NULL); lua_close(L); skynet_start(&config); skynet_globalexit(); printf("skynet exit\n"); return 0; }
/*** Install a signal handler for this signal number. Although this is the same API as signal(2), it uses sigaction for guaranteed semantics. @function signal @see signal.lua @int signum @tparam[opt=SIG_DFL] function handler function, or `SIG_IGN` or `SIG_DFL` constants @param[opt] flags the `sa_flags` element of `struct sigaction` @treturn function previous handler function @see sigaction(2) */ static int Psignal (lua_State *L) { struct sigaction sa, oldsa; int sig = checkint(L, 1), ret; void (*handler)(int) = sig_postpone; checknargs(L, 3); /* Check handler is OK */ switch (lua_type(L, 2)) { case LUA_TNIL: case LUA_TSTRING: handler = Fsigmacros[luaL_checkoption(L, 2, "SIG_DFL", Ssigmacros)]; break; case LUA_TFUNCTION: if (lua_tocfunction(L, 2) == sig_handler_wrap) { lua_getupvalue(L, 2, 1); handler = lua_touserdata(L, -1); lua_pop(L, 1); } break; default: argtypeerror(L, 2, "function, string or nil"); break; } /* Set up C signal handler, getting old handler */ sa.sa_handler = handler; sa.sa_flags = optint(L, 3, 0); sigfillset(&sa.sa_mask); ret = sigaction(sig, &sa, &oldsa); if (ret == -1) return 0; /* Set Lua handler if necessary */ if (handler == sig_postpone) { lua_pushlightuserdata(L, &signalL); /* We could use an upvalue, but we need this for sig_handle anyway. */ lua_rawget(L, LUA_REGISTRYINDEX); lua_pushvalue(L, 1); lua_pushvalue(L, 2); lua_rawset(L, -3); lua_pop(L, 1); } /* Push old handler as result */ if (oldsa.sa_handler == sig_postpone) { lua_pushlightuserdata(L, &signalL); lua_rawget(L, LUA_REGISTRYINDEX); lua_pushvalue(L, 1); lua_rawget(L, -2); } else if (oldsa.sa_handler == SIG_DFL) lua_pushstring(L, "SIG_DFL"); else if (oldsa.sa_handler == SIG_IGN) lua_pushstring(L, "SIG_IGN"); else { lua_pushinteger(L, sig); lua_pushlightuserdata(L, oldsa.sa_handler); lua_pushcclosure(L, sig_handler_wrap, 2); } return 1; }
/*** Get a message queue identifier @function msgget @int key message queue id, or `IPC_PRIVATE` for a new queue @int[opt=0] flags bitwise OR of zero or more from `IPC_CREAT` and `IPC_EXCL`, and access permissions `S_IRUSR`, `S_IWUSR`, `S_IRGRP`, `S_IWGRP`, `S_IROTH` and `S_IWOTH` (from @{posix.sys.stat}) @treturn[1] int message queue identifier, if successful @return[2] nil @treturn[2] string error message @treturn[2] int errnum @see msgget(2) */ static int Pmsgget(lua_State *L) { checknargs (L, 2); return pushresult(L, msgget(checkint(L, 1), optint(L, 2, 0)), "msgget"); }