goahead.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. /*
  2. goahead.c -- Main program for GoAhead
  3. Usage: goahead [options] [documents] [IP][:port] ...
  4. Options:
  5. --auth authFile # User and role configuration
  6. --background # Run as a Linux daemon
  7. --home directory # Change to directory to run
  8. --log logFile:level # Log to file file at verbosity level
  9. --route routeFile # Route configuration file
  10. --verbose # Same as --log stdout:2
  11. --version # Output version information
  12. Copyright (c) All Rights Reserved. See details at the end of the file.
  13. */
  14. /********************************* Includes ***********************************/
  15. #include "goahead.h"
  16. #include "dashboard.h"
  17. #include "cJSON.h"
  18. #include "com_BmcType.h"
  19. #include "remote_control.h"
  20. #include "fan.h"
  21. #include "fru.h"
  22. #include "sel.h"
  23. #include "server_health.h"
  24. #include "fw_update.h"
  25. #include "config.h"
  26. #include "user.h"
  27. #include <stdio.h>
  28. #include <string.h>
  29. /********************************* Defines ************************************/
  30. static int finished = 0;
  31. UserInfo_T UserInfoTbl[MAX_USER_NUM];
  32. /********************************* Forwards ***********************************/
  33. static void initPlatform();
  34. static void logHeader();
  35. static void usage();
  36. #if WINDOWS
  37. static void windowsClose();
  38. static int windowsInit();
  39. static LRESULT CALLBACK websWindProc(HWND hwnd, UINT msg, UINT wp, LPARAM lp);
  40. #endif
  41. #if ME_UNIX_LIKE
  42. static void sigHandler(int signo);
  43. #endif
  44. /*********************************** Code *************************************/
  45. MAIN(goahead, int argc, char **argv, char **envp)
  46. {
  47. char *argp, *home, *documents, *endpoints, *endpoint, *route, *auth, *tok, *lspec;
  48. int argind;
  49. #if WINDOWS
  50. if (windowsInit() < 0) {
  51. return 0;
  52. }
  53. #endif
  54. route = "/etc/goahead/route.txt";
  55. auth = "/etc/goahead/auth.txt";
  56. /**************** user code before goahead ************************/
  57. cJSON_Hooks hooks;
  58. hooks.malloc_fn = (void *(*)(size_t))walloc;
  59. hooks.free_fn = (void (*)(void *))wfree;
  60. cJSON_InitHooks(&hooks);
  61. /**************** user code before goahead end************************/
  62. for (argind = 1; argind < argc; argind++) {
  63. argp = argv[argind];
  64. if (*argp != '-') {
  65. break;
  66. } else if (smatch(argp, "--auth") || smatch(argp, "-a")) {
  67. if (argind >= argc) usage();
  68. auth = argv[++argind];
  69. #if ME_UNIX_LIKE && !MACOSX
  70. } else if (smatch(argp, "--background") || smatch(argp, "-b")) {
  71. websSetBackground(1);
  72. #endif
  73. } else if (smatch(argp, "--debugger") || smatch(argp, "-d") || smatch(argp, "-D")) {
  74. websSetDebug(1);
  75. } else if (smatch(argp, "--home")) {
  76. if (argind >= argc) usage();
  77. home = argv[++argind];
  78. if (chdir(home) < 0) {
  79. error("Cannot change directory to %s", home);
  80. exit(-1);
  81. }
  82. } else if (smatch(argp, "--log") || smatch(argp, "-l")) {
  83. if (argind >= argc) usage();
  84. logSetPath(argv[++argind]);
  85. } else if (smatch(argp, "--verbose") || smatch(argp, "-v")) {
  86. logSetPath("stdout:2"); //2
  87. } else if (smatch(argp, "--route") || smatch(argp, "-r")) {
  88. route = argv[++argind];
  89. } else if (smatch(argp, "--version") || smatch(argp, "-V")) {
  90. printf("%s\n", ME_VERSION);
  91. exit(0);
  92. } else if (*argp == '-' && isdigit((uchar) argp[1])) {
  93. lspec = sfmt("stdout:%s", &argp[1]);
  94. logSetPath(lspec);
  95. wfree(lspec);
  96. } else {
  97. usage();
  98. }
  99. }
  100. documents = ME_GOAHEAD_DOCUMENTS;
  101. if (argc > argind) {
  102. documents = argv[argind++];
  103. }
  104. // printf("---> initPlatform\n");
  105. initPlatform(); //注册信号
  106. // printf("---> websOpen\n");
  107. // printf(">>>>>>>>>>>>>>>>>>>>>>>>>>2>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
  108. if (websOpen(documents, route) < 0) {
  109. error("Cannot initialize server. Exiting.");
  110. return -1;
  111. }
  112. #if ME_GOAHEAD_AUTH
  113. // printf("---> websLoad\n");
  114. if (websLoad(auth) < 0) {
  115. error("Cannot load %s", auth);
  116. return -1;
  117. }
  118. #endif
  119. logHeader();
  120. if (argind < argc) {
  121. while (argind < argc) {
  122. endpoint = argv[argind++];
  123. // printf("---> websListen log1\n");
  124. if (websListen(endpoint) < 0) {
  125. return -1;
  126. }
  127. }
  128. } else {
  129. endpoints = sclone(ME_GOAHEAD_LISTEN);
  130. for (endpoint = stok(endpoints, ", \t", &tok); endpoint; endpoint = stok(NULL, ", \t,", &tok)) {
  131. #if !ME_COM_SSL
  132. if (strstr(endpoint, "https")) continue;
  133. #endif
  134. // printf("---> websListen log2\n");
  135. if (websListen(endpoint) < 0) {
  136. wfree(endpoints);
  137. return -1;
  138. }
  139. }
  140. wfree(endpoints);
  141. }
  142. #if ME_ROM && KEEP
  143. /*
  144. If not using a route/auth config files, then manually create the routes like this:
  145. If custom matching is required, use websSetRouteMatch. If authentication is required, use websSetRouteAuth.
  146. */
  147. websAddRoute("/", "file", 0);
  148. #endif
  149. /**************** user code after goahead ************************/
  150. websDefineAction("test", actionTest);
  151. websDefineAction("buy", buy);
  152. //dashboard
  153. websDefineAction("getDeviceInfo", getDeviceInfo);
  154. websDefineAction("getSysInfo", getSysInfo);
  155. websDefineAction("getCardInfo", getCardInfo);
  156. websDefineAction("getSensorInfo", getSensorInfo);
  157. websDefineAction("getChassisInfo", getChassisInfo);
  158. websDefineAction("getBladeManage", getBladeManage);
  159. websDefineAction("setBladeManage", setBladeManage);
  160. websDefineAction("getAllBladeInfo", getAllBladeInfo);
  161. //remote control
  162. websDefineAction("chassisPwrCtrl", chassisPwrCtrl);
  163. websDefineAction("getChassisStatus", getChassisStatus);
  164. //Fru
  165. //websDefineAction("getFruInfo", getFruInfo);
  166. websDefineAction("getFruChassisInfo", getFruChassisInfo);
  167. websDefineAction("getFruBoardInfo", getFruBoardInfo);
  168. websDefineAction("getFruProductInfo", getFruProductInfo);
  169. //sel
  170. websDefineAction("Web_ClearSEL", Web_ClearSEL);
  171. websDefineAction("GetAllSELEntriesSorted", GetAllSELEntriesSorted);
  172. websDefineAction("SetTime", SetTime);
  173. websDefineAction("GetTime", GetTime);
  174. //websDefineAction("SetTimeUTCOffset", SetTimeUTCOffset);
  175. //websDefineAction("GetTimeUTCOffset", GetTimeUTCOffset);
  176. //user
  177. websDefineAction("getAllUserInfo", getAllUserInfo);
  178. websDefineAction("setUserPassword", setUserPassword);
  179. websDefineAction("addUser", addUser);
  180. websDefineAction("delUser", delUser);
  181. //update firmware
  182. // websDefineAction("uploadTest", uploadTest);
  183. websDefineAction("prepareDevice", prepareDevice);
  184. websDefineAction("uploadFirmware", uploadFirmware);
  185. websDefineAction("updateFlash", updateFlash);
  186. websDefineAction("getUpdateProgress", getUpdateProgress);
  187. websDefineAction("getVerifyStatus", getVerifyStatus);
  188. websDefineAction("resetBmc", resetBmc);
  189. //server_health
  190. websDefineAction("setThreshold", setThreshold);
  191. websDefineAction("webGetSensorHistory", webGetSensorHistory);
  192. //fan
  193. websDefineAction("getAllFanInfo", getAllFanInfo);
  194. websDefineAction("setFanInfo", setFanInfo);
  195. //config
  196. websDefineAction("restoreFactorySettings", restoreFactorySettings);
  197. websDefineAction("web_ResetBMC", web_ResetBMC);
  198. websDefineAction("web_GetLanInfo", web_GetLanInfo);
  199. websDefineAction("web_SetLanInfo", web_SetLanInfo);
  200. websDefineAction("checkLoginStatus", checkLoginStatus);
  201. websDefineAction("logout", logout);
  202. websDefineAction("web_GetRunTime", web_GetRunTime);
  203. /**************** user code after goahead end************************/
  204. #if ME_UNIX_LIKE && !MACOSX
  205. /*
  206. Service events till terminated
  207. */
  208. if (websGetBackground()) {
  209. printf("---> daemon\n");
  210. if (daemon(0, 0) < 0) {
  211. error("Cannot run as daemon");
  212. return -1;
  213. }
  214. }
  215. #endif
  216. printf("---> websServiceEvents\n");
  217. websServiceEvents(&finished);
  218. logmsg(1, "Instructed to exit");
  219. printf("---> websClose\n");
  220. websClose();
  221. #if WINDOWS
  222. windowsClose();
  223. #endif
  224. return 0;
  225. }
  226. static void logHeader()
  227. {
  228. char home[ME_GOAHEAD_LIMIT_STRING];
  229. getcwd(home, sizeof(home));
  230. logmsg(2, "Configuration for %s", ME_TITLE);
  231. logmsg(2, "---------------------------------------------");
  232. logmsg(2, "Version: %s", ME_VERSION);
  233. logmsg(2, "BuildType: %s", ME_DEBUG ? "Debug" : "Release");
  234. logmsg(2, "CPU: %s", ME_CPU);
  235. logmsg(2, "OS: %s", ME_OS);
  236. logmsg(2, "Host: %s", websGetServer());
  237. logmsg(2, "Directory: %s", home);
  238. logmsg(2, "Documents: %s", websGetDocuments());
  239. logmsg(2, "Configure: %s", ME_CONFIG_CMD);
  240. logmsg(2, "---------------------------------------------");
  241. }
  242. static void usage() {
  243. fprintf(stderr, "\n%s Usage:\n\n"
  244. " %s [options] [documents] [[IPaddress][:port] ...]\n\n"
  245. " Options:\n"
  246. #if ME_GOAHEAD_AUTH
  247. " --auth authFile # User and role configuration\n"
  248. #endif
  249. #if ME_UNIX_LIKE && !MACOSX
  250. " --background # Run as a Unix daemon\n"
  251. #endif
  252. " --debugger # Run in debug mode\n"
  253. " --home directory # Change to directory to run\n"
  254. " --log logFile:level # Log to file file at verbosity level\n"
  255. " --route routeFile # Route configuration file\n"
  256. " --verbose # Same as --log stdout:2\n"
  257. " --version # Output version information\n\n",
  258. ME_TITLE, ME_NAME);
  259. exit(-1);
  260. }
  261. static void initPlatform()
  262. {
  263. #if ME_UNIX_LIKE
  264. signal(SIGTERM, sigHandler);
  265. #ifdef SIGPIPE
  266. signal(SIGPIPE, SIG_IGN);
  267. #endif
  268. #elif ME_WIN_LIKE
  269. _fmode=_O_BINARY;
  270. #endif
  271. }
  272. #if ME_UNIX_LIKE
  273. static void sigHandler(int signo)
  274. {
  275. finished = 1;
  276. }
  277. #endif
  278. #if WINDOWS
  279. /*
  280. Create a taskbar entry. Register the window class and create a window
  281. */
  282. static int windowsInit()
  283. {
  284. HINSTANCE inst;
  285. WNDCLASS wc; /* Window class */
  286. HMENU hSysMenu;
  287. HWND hwnd;
  288. inst = websGetInst();
  289. wc.style = CS_HREDRAW | CS_VREDRAW;
  290. wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  291. wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  292. wc.cbClsExtra = 0;
  293. wc.cbWndExtra = 0;
  294. wc.hInstance = inst;
  295. wc.hIcon = NULL;
  296. wc.lpfnWndProc = (WNDPROC) websWindProc;
  297. wc.lpszMenuName = wc.lpszClassName = ME_NAME;
  298. if (! RegisterClass(&wc)) {
  299. return -1;
  300. }
  301. /*
  302. Create a window just so we can have a taskbar to close this web server
  303. */
  304. hwnd = CreateWindow(ME_NAME, ME_TITLE, WS_MINIMIZE | WS_POPUPWINDOW, CW_USEDEFAULT,
  305. 0, 0, 0, NULL, NULL, inst, NULL);
  306. if (hwnd == NULL) {
  307. return -1;
  308. }
  309. /*
  310. Add the about box menu item to the system menu
  311. */
  312. hSysMenu = GetSystemMenu(hwnd, FALSE);
  313. if (hSysMenu != NULL) {
  314. AppendMenu(hSysMenu, MF_SEPARATOR, 0, NULL);
  315. }
  316. ShowWindow(hwnd, SW_SHOWNORMAL);
  317. UpdateWindow(hwnd);
  318. return 0;
  319. }
  320. static void windowsClose()
  321. {
  322. HINSTANCE inst;
  323. inst = websGetInst();
  324. UnregisterClass(ME_NAME, inst);
  325. }
  326. /*
  327. Main menu window message handler.
  328. */
  329. static LRESULT CALLBACK websWindProc(HWND hwnd, UINT msg, UINT wp, LPARAM lp)
  330. {
  331. switch (msg) {
  332. case WM_DESTROY:
  333. PostQuitMessage(0);
  334. finished++;
  335. return 0;
  336. case WM_SYSCOMMAND:
  337. break;
  338. }
  339. return DefWindowProc(hwnd, msg, wp, lp);
  340. }
  341. /*
  342. Check for Windows Messages
  343. */
  344. WPARAM checkWindowsMsgLoop()
  345. {
  346. MSG msg;
  347. if (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)) {
  348. if (!GetMessage(&msg, NULL, 0, 0) || msg.message == WM_QUIT) {
  349. return msg.wParam;
  350. }
  351. TranslateMessage(&msg);
  352. DispatchMessage(&msg);
  353. }
  354. return 0;
  355. }
  356. /*
  357. Windows message handler
  358. */
  359. static LRESULT CALLBACK websAboutProc(HWND hwndDlg, uint msg, uint wp, long lp)
  360. {
  361. LRESULT lResult;
  362. lResult = DefWindowProc(hwndDlg, msg, wp, lp);
  363. switch (msg) {
  364. case WM_CREATE:
  365. break;
  366. case WM_DESTROY:
  367. break;
  368. case WM_COMMAND:
  369. break;
  370. }
  371. return lResult;
  372. }
  373. #endif