svc.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. #include "precompile.h"
  2. #include "sp_mod.h"
  3. #include "svc.h"
  4. #include "app.h"
  5. #include "def.h"
  6. #include "SpBase.h"
  7. #include "sp_btr.h"
  8. #include "sp_gui.h"
  9. #include "sp_dbg_export.h"
  10. #include "sp_iom.h"
  11. #include "sp_svc.h"
  12. #include "sp_def.h"
  13. #include "sp_cfg.h"
  14. #include "sp_rpc.h"
  15. void on_bluesceen_display(sp_rpc_server_t *server, int epid, int svc_id, int call_type, iobuffer_t **info_pkt)
  16. {
  17. app_t *app = get_app_instance();
  18. char *msg = NULL;
  19. //int iBlueScreen=0;
  20. //iobuffer_format_read(*info_pkt, "s4", &msg, &iBlueScreen);
  21. iobuffer_format_read(*info_pkt, "s", &msg);
  22. sp_gui_show_running_info(app->bsc_gui, msg, 1);
  23. toolkit_free(msg);
  24. }
  25. void on_fatal_error_display(sp_rpc_server_t *server, int epid, int svc_id, int call_type, iobuffer_t **info_pkt)
  26. {
  27. app_t *app = get_app_instance();
  28. char *msg = NULL;
  29. //int iBlueScreen=0;
  30. //iobuffer_format_read(*info_pkt, "s4", &msg, &iBlueScreen);
  31. iobuffer_format_read(*info_pkt, "s", &msg);
  32. sp_gui_show_running_info(app->bsc_gui, msg, 2);
  33. toolkit_free(msg);
  34. }
  35. void on_startup_info_display(sp_rpc_server_t *server, int epid, int svc_id, int call_type, iobuffer_t **info_pkt)
  36. {
  37. app_t *app = get_app_instance();
  38. char *msg = NULL;
  39. iobuffer_format_read(*info_pkt, "s", &msg);
  40. sp_gui_show_running_info(app->bsc_gui, msg, 3);
  41. toolkit_free(msg);
  42. }
  43. void on_bluesceen_undisplay(sp_rpc_server_t *server, int epid, int svc_id, int call_type, iobuffer_t **info_pkt)
  44. {
  45. app_t *app = get_app_instance();
  46. sp_gui_undisplay(app->bsc_gui);
  47. }
  48. static BOOL RestartWindows()
  49. {
  50. HANDLE hToken; // handle to process token
  51. TOKEN_PRIVILEGES tkp; // pointer to token structure
  52. BOOL fResult; // system shutdown flag
  53. // Get the current process token handle so we can get shutdown privilege.
  54. if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
  55. {
  56. Dbg("get proc token fail");
  57. return FALSE;
  58. }
  59. // Get the LUID for shutdown privilege.
  60. LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid);
  61. tkp.PrivilegeCount = 1; // one privilege to set
  62. tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  63. // Get shutdown privilege for this process.
  64. AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0);
  65. // Cannot test the return value of AdjustTokenPrivileges.
  66. if (GetLastError() != ERROR_SUCCESS)
  67. {
  68. Dbg("adjust proc token privilege fail");
  69. return FALSE;
  70. }
  71. // Display the shutdown dialog box and start the countdown.
  72. fResult = InitiateSystemShutdown(
  73. NULL, // shut down local computer
  74. NULL, // message for user
  75. 0, // time-out period, in seconds
  76. FALSE, // ask user to close apps
  77. TRUE); // reboot after shutdown
  78. if (!fResult)
  79. {
  80. Dbg("request windows reboot fail");
  81. return FALSE;
  82. }
  83. // Disable shutdown privilege.
  84. tkp.Privileges[0].Attributes = 0;
  85. AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0);
  86. return TRUE;
  87. }
  88. DWORD getExePath(char *exePath)
  89. {
  90. char pathbuf[MAX_PATH] = "";
  91. int pathlen = ::GetModuleFileName(NULL, pathbuf, MAX_PATH);
  92. // 替换掉单杠
  93. while (pathlen > 0)
  94. {
  95. if (pathbuf[pathlen--] == '\\')
  96. break;
  97. }
  98. pathbuf[++pathlen] = '\0';
  99. sprintf(exePath, "%s", pathbuf);
  100. return strlen(exePath);
  101. }
  102. int KickoffSpRestart(bool bRestart)
  103. {
  104. STARTUPINFO si;
  105. PROCESS_INFORMATION pi;
  106. ZeroMemory(&si, sizeof(si));
  107. si.cb = sizeof(si);
  108. ZeroMemory(&pi, sizeof(pi));
  109. // LPTSTR szCmdline[] = _tcsdup(TEXT("\"C:\\Program Files\\MyApp\" -L -S"));
  110. //CreateProcess(NULL, szCmdline, /*...*/);
  111. // Start the child process.
  112. char exepath[MAX_PATH] = "";
  113. getExePath(exepath);
  114. CSimpleStringA csRestart, csVerPath, csAll, csSep("\""), csBlank(" "), csScript("wscript.exe"), csReFlag("r");
  115. csRestart = CSimpleStringA(exepath) + "\\sprestart.exe";
  116. sp_env_t *env = sp_get_env();
  117. csVerPath = env->dir->root_ver_path;
  118. csVerPath += "\\spexplorerfast.vbs";
  119. if (!bRestart)
  120. csReFlag = "n";
  121. csAll = csRestart + csBlank + csSep + csScript + csBlank + csVerPath + csSep
  122. + csBlank + csSep + csReFlag + csSep;
  123. Dbg("allpath[%s]", (LPCTSTR)csAll);
  124. LPTSTR szCmdline = _strdup(csAll);
  125. if (!CreateProcess(NULL, szCmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))
  126. {
  127. Dbg("CreateProcess failed (%d).\n", GetLastError());
  128. return -1;
  129. }
  130. Dbg("CreateProcess Success PID:%d.\n", pi.dwProcessId);
  131. DWORD dwErr = GetLastError();
  132. // Close process and thread handles.
  133. CloseHandle(pi.hProcess);
  134. CloseHandle(pi.hThread);
  135. return 0;
  136. }
  137. void on_machine_reboot(sp_rpc_server_t *server, int epid, int svc_id, int call_type, iobuffer_t **info_pkt)
  138. {
  139. int reason(0), way(0);
  140. sp_env_t *env = sp_get_env();
  141. sp_cfg_t *cfg = env->cfg;
  142. iobuffer_read(*info_pkt, IOBUF_T_I4, &reason, 0);
  143. iobuffer_read(*info_pkt, IOBUF_T_I4, &way, 0);
  144. Dbg("now reboot: reason = %d, way = %d", reason, way);
  145. CSimpleStringA strPath = env->dir->root_runinfo_path;
  146. strPath += "\\BootLog";
  147. sp_btr_write_on_shutdown(strPath, env->btr_ctx, reason, way);
  148. RebootWayEnum eRebootWay = (RebootWayEnum)way;
  149. // 只处理框架重启及操作系统重启,电源重启由健康实体处理
  150. if (eRebootWay == RebootWay_Framework)
  151. {
  152. Dbg("reboot framework now");
  153. KickoffSpRestart(true);
  154. }
  155. else if (eRebootWay == RebootWay_OS)
  156. {
  157. Dbg("reboot windows now");
  158. RestartWindows();
  159. }
  160. app_t *app = get_app_instance();
  161. sp_iom_post_quit(app->iom);
  162. }
  163. void on_entity_quit(sp_rpc_server_t *server, int epid, int svc_id, int call_type, iobuffer_t **info_pkt)
  164. {
  165. int entity_id;
  166. sp_env_t *env = sp_get_env();
  167. iobuffer_read(*info_pkt, IOBUF_T_I4, &entity_id, NULL);
  168. sp_mod_mgr_stop_entity(env->mod_mgr, entity_id, svc_id, CloseCause_Self);
  169. }
  170. void on_output_console_on(sp_rpc_server_t *server, int epid, int svc_id, int call_type, iobuffer_t **info_pkt)
  171. {
  172. Dbg("set output console on");
  173. //sp_dbg_start_console("可视柜台终端运行监控");
  174. auto app = get_app_instance();
  175. sp_gui_display(app->bsc_gui);
  176. }
  177. void on_output_console_off(sp_rpc_server_t *server, int epid, int svc_id, int call_type, iobuffer_t **info_pkt)
  178. {
  179. Dbg("set output console off");
  180. //sp_dbg_close_console();
  181. auto app = get_app_instance();
  182. sp_gui_undisplay(app->bsc_gui);
  183. }
  184. iobuffer_t *on_entity_start(sp_rpc_server_t *server, int epid, int svc_id, int rpc_id, int call_type, iobuffer_t **req_pkt)
  185. {
  186. sp_env_t *env = sp_get_env();
  187. int entity_id;
  188. int result;
  189. char *cmdline = NULL;
  190. iobuffer_t *ans_pkt = NULL;
  191. iobuffer_read(*req_pkt, IOBUF_T_I4, &entity_id, NULL);
  192. sp_dbg_debug("handle start entity req: %d", entity_id);
  193. iobuffer_format_read(*req_pkt, "s", &cmdline);
  194. result = sp_mod_mgr_start_entity(env->mod_mgr, entity_id, cmdline, svc_id);
  195. toolkit_free(cmdline);
  196. ans_pkt = iobuffer_create(-1, -1);
  197. iobuffer_write(ans_pkt, IOBUF_T_I4, &result, 0);
  198. return ans_pkt;
  199. }
  200. iobuffer_t *on_entity_stop(sp_rpc_server_t *server, int epid, int svc_id, int rpc_id, int call_type, iobuffer_t **req_pkt)
  201. {
  202. sp_env_t *env = sp_get_env();
  203. int entity_id;
  204. int result;
  205. int cause_code;
  206. iobuffer_t *ans_pkt = NULL;
  207. iobuffer_read(*req_pkt, IOBUF_T_I4, &entity_id, NULL);
  208. if (svc_id == SP_SHELL_SVC_ID) {
  209. cause_code = CloseCause_ShellQuit;
  210. } else if (svc_id == SP_INVALID_SVC_ID) {
  211. cause_code = CloseCause_Lost;
  212. } else if (svc_id == entity_id) {
  213. cause_code = CloseCause_Self;
  214. } else {
  215. cause_code = CloseCause_Other;
  216. }
  217. sp_dbg_debug("handle stop entity req: %d", entity_id);
  218. result = sp_mod_mgr_stop_entity(env->mod_mgr, entity_id, svc_id, cause_code);
  219. ans_pkt = iobuffer_create(-1, -1);
  220. iobuffer_write(ans_pkt, IOBUF_T_I4, &result, 0);
  221. return ans_pkt;
  222. }
  223. iobuffer_t *on_entity_continue(sp_rpc_server_t *server, int epid, int svc_id, int rpc_id, int call_type, iobuffer_t **req_pkt)
  224. {
  225. sp_env_t *env = sp_get_env();
  226. int entity_id;
  227. int result;
  228. iobuffer_t *ans_pkt = NULL;
  229. iobuffer_read(*req_pkt, IOBUF_T_I4, &entity_id, NULL);
  230. sp_dbg_debug("handle continue entity req: %d", entity_id);
  231. result = sp_mod_mgr_continue_entity(env->mod_mgr, entity_id, svc_id);
  232. ans_pkt = iobuffer_create(-1, -1);
  233. iobuffer_write(ans_pkt, IOBUF_T_I4, &result, 0);
  234. return ans_pkt;
  235. }
  236. iobuffer_t *on_entity_pause(sp_rpc_server_t *server, int epid, int svc_id, int rpc_id, int call_type, iobuffer_t **req_pkt)
  237. {
  238. sp_env_t *env = sp_get_env();
  239. int entity_id;
  240. int result;
  241. iobuffer_t *ans_pkt = NULL;
  242. iobuffer_read(*req_pkt, IOBUF_T_I4, &entity_id, NULL);
  243. sp_dbg_debug("handle pause entity req: %d", entity_id);
  244. result = sp_mod_mgr_pause_entity(env->mod_mgr, entity_id, svc_id);
  245. ans_pkt = iobuffer_create(-1, -1);
  246. iobuffer_write(ans_pkt, IOBUF_T_I4, &result, 0);
  247. return ans_pkt;
  248. }
  249. iobuffer_t *on_entity_test(sp_rpc_server_t *server, int epid, int svc_id, int rpc_id, int call_type, iobuffer_t **req_pkt)
  250. {
  251. sp_env_t *env = sp_get_env();
  252. int entity_id;
  253. int test_type;
  254. int result;
  255. iobuffer_t *ans_pkt = NULL;
  256. iobuffer_read(*req_pkt, IOBUF_T_I4, &entity_id, NULL);
  257. iobuffer_read(*req_pkt, IOBUF_T_I4, &test_type, NULL);
  258. //sp_dbg_debug("handle test entity req: %d", entity_id);
  259. result = sp_mod_mgr_test_entity(env->mod_mgr, entity_id, svc_id, test_type);
  260. ans_pkt = iobuffer_create(-1, -1);
  261. iobuffer_write(ans_pkt, IOBUF_T_I4, &result, 0);
  262. return ans_pkt;
  263. }
  264. iobuffer_t *on_entity_termination(sp_rpc_server_t *server, int epid, int svc_id, int rpc_id, int call_type, iobuffer_t **req_pkt)
  265. {
  266. sp_env_t *env = sp_get_env();
  267. int entity_id;
  268. int result;
  269. iobuffer_t *ans_pkt = NULL;
  270. iobuffer_read(*req_pkt, IOBUF_T_I4, &entity_id, NULL);
  271. sp_dbg_debug("handle terminate entity req: %d", entity_id);
  272. result = sp_mod_mgr_terminate_entity(env->mod_mgr, entity_id, svc_id);
  273. ans_pkt = iobuffer_create(-1, -1);
  274. iobuffer_write(ans_pkt, IOBUF_T_I4, &result, 0);
  275. return ans_pkt;
  276. }
  277. iobuffer_t *on_get_bcast_receiver(sp_rpc_server_t *server, int epid, int svc_id, int rpc_id, int call_type, iobuffer_t **req_pkt)
  278. {
  279. sp_env_t *env = sp_get_env();
  280. iobuffer_t *ans_pkt = NULL;
  281. int result = 0;
  282. int i;
  283. array_header_t *arr = sp_bcm_daemon_get_receiver(get_app_instance()->bcm_daemon, svc_id);
  284. ans_pkt = iobuffer_create(-1, -1);
  285. iobuffer_write(ans_pkt, IOBUF_T_I4, &result, 0);
  286. iobuffer_write(ans_pkt, IOBUF_T_I4, &arr->nelts, 0);
  287. for (i = 0; i < arr->nelts; ++i) {
  288. sp_bcm_receiver_t *recver = ARRAY_IDX(arr, i, sp_bcm_receiver_t*);
  289. iobuffer_write(ans_pkt, IOBUF_T_I8, &recver->id, 0);
  290. iobuffer_write(ans_pkt, IOBUF_T_I4, &recver->receiver_id, 0);
  291. iobuffer_write(ans_pkt, IOBUF_T_STR, &recver->param, -1);
  292. }
  293. sp_bcm_daemon_free_receiver_array(arr);
  294. return ans_pkt;
  295. }