goahead.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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. initPlatform(); //注册信号
  105. if (websOpen(documents, route) < 0) {
  106. error("Cannot initialize server. Exiting.");
  107. return -1;
  108. }
  109. #if ME_GOAHEAD_AUTH
  110. if (websLoad(auth) < 0) {
  111. error("Cannot load %s", auth);
  112. return -1;
  113. }
  114. #endif
  115. logHeader();
  116. if (argind < argc) {
  117. while (argind < argc) {
  118. endpoint = argv[argind++];
  119. if (websListen(endpoint) < 0) {
  120. return -1;
  121. }
  122. }
  123. } else {
  124. endpoints = sclone(ME_GOAHEAD_LISTEN);
  125. for (endpoint = stok(endpoints, ", \t", &tok); endpoint; endpoint = stok(NULL, ", \t,", &tok)) {
  126. #if !ME_COM_SSL
  127. if (strstr(endpoint, "https")) continue;
  128. #endif
  129. if (websListen(endpoint) < 0) {
  130. wfree(endpoints);
  131. return -1;
  132. }
  133. }
  134. wfree(endpoints);
  135. }
  136. #if ME_ROM && KEEP
  137. /*
  138. If not using a route/auth config files, then manually create the routes like this:
  139. If custom matching is required, use websSetRouteMatch. If authentication is required, use websSetRouteAuth.
  140. */
  141. websAddRoute("/", "file", 0);
  142. #endif
  143. /**************** user code after goahead ************************/
  144. websDefineAction("test", actionTest);
  145. websDefineAction("buy", buy);
  146. //dashboard
  147. websDefineAction("getDeviceInfo", getDeviceInfo);
  148. websDefineAction("getSysInfo", getSysInfo);
  149. websDefineAction("getSensorInfo", getSensorInfo);
  150. websDefineAction("getAllBladeInfo", getAllBladeInfo);
  151. //remote control
  152. websDefineAction("chassisPwrCtrl", chassisPwrCtrl);
  153. websDefineAction("getChassisStatus", getChassisStatus);
  154. //Fru
  155. //websDefineAction("getFruInfo", getFruInfo);
  156. websDefineAction("getFruChassisInfo", getFruChassisInfo);
  157. websDefineAction("getFruBoardInfo", getFruBoardInfo);
  158. websDefineAction("getFruProductInfo", getFruProductInfo);
  159. //sel
  160. websDefineAction("Web_ClearSEL", Web_ClearSEL);
  161. websDefineAction("GetAllSELEntriesSorted", GetAllSELEntriesSorted);
  162. websDefineAction("SetTime", SetTime);
  163. websDefineAction("GetTime", GetTime);
  164. //websDefineAction("SetTimeUTCOffset", SetTimeUTCOffset);
  165. //websDefineAction("GetTimeUTCOffset", GetTimeUTCOffset);
  166. //user
  167. websDefineAction("getAllUserInfo", getAllUserInfo);
  168. websDefineAction("setUserPassword", setUserPassword);
  169. websDefineAction("addUser", addUser);
  170. websDefineAction("delUser", delUser);
  171. //update firmware
  172. // websDefineAction("uploadTest", uploadTest);
  173. websDefineAction("prepareDevice", prepareDevice);
  174. websDefineAction("uploadFirmware", uploadFirmware);
  175. websDefineAction("updateFlash", updateFlash);
  176. websDefineAction("getUpdateProgress", getUpdateProgress);
  177. websDefineAction("getVerifyStatus", getVerifyStatus);
  178. websDefineAction("resetBmc", resetBmc);
  179. websDefineAction("getEraseStatus", getEraseStatus);
  180. //server_health
  181. websDefineAction("setThreshold", setThreshold);
  182. websDefineAction("webGetSensorHistory", webGetSensorHistory);
  183. //fan
  184. websDefineAction("getAllFanInfo", getAllFanInfo);
  185. websDefineAction("setFanInfo", setFanInfo);
  186. //config
  187. websDefineAction("restoreFactorySettings", restoreFactorySettings);
  188. websDefineAction("web_ResetBMC", web_ResetBMC);
  189. websDefineAction("web_GetLanInfo", web_GetLanInfo);
  190. websDefineAction("web_SetLanInfo", web_SetLanInfo);
  191. websDefineAction("checkLoginStatus", checkLoginStatus);
  192. websDefineAction("logout", logout);
  193. websDefineAction("web_GetRunTime", web_GetRunTime);
  194. websDefineAction("web_SetModIdentifyOn", web_SetModIdentifyOn);
  195. websDefineAction("web_SetModIdentifyOff", web_SetModIdentifyOff);
  196. websDefineAction("web_SaveConfig", web_SaveConfig);
  197. websDefineAction("web_RestoreConfig", web_RestoreConfig);
  198. /**************** user code after goahead end************************/
  199. #if ME_UNIX_LIKE && !MACOSX
  200. /*
  201. Service events till terminated
  202. */
  203. if (websGetBackground()) {
  204. printf("---> daemon\n");
  205. if (daemon(0, 0) < 0) {
  206. error("Cannot run as daemon");
  207. return -1;
  208. }
  209. }
  210. #endif
  211. websServiceEvents(&finished);
  212. printf("Instructed to exit");
  213. websClose();
  214. #if WINDOWS
  215. windowsClose();
  216. #endif
  217. return 0;
  218. }
  219. static void logHeader()
  220. {
  221. char home[ME_GOAHEAD_LIMIT_STRING];
  222. getcwd(home, sizeof(home));
  223. //printf( "Configuration for %s\n", ME_TITLE);
  224. printf( "\ngoahead web server start\n");
  225. printf( "---------------------------------------------\n");
  226. printf( " Version: %s\n", ME_VERSION);
  227. printf( " BuildType: %s\n", ME_DEBUG ? "Debug" : "Release");
  228. printf( " CPU: %s\n", ME_CPU);
  229. printf( " OS: %s\n", ME_OS);
  230. printf( " Host: %s\n", websGetServer());
  231. printf( " Directory: %s\n", home);
  232. printf( " Documents: %s\n", websGetDocuments());
  233. printf( " Configure: %s\n", ME_CONFIG_CMD);
  234. printf( "---------------------------------------------\n");
  235. }
  236. static void usage() {
  237. fprintf(stderr, "\n%s Usage:\n\n"
  238. " %s [options] [documents] [[IPaddress][:port] ...]\n\n"
  239. " Options:\n"
  240. #if ME_GOAHEAD_AUTH
  241. " --auth authFile # User and role configuration\n"
  242. #endif
  243. #if ME_UNIX_LIKE && !MACOSX
  244. " --background # Run as a Unix daemon\n"
  245. #endif
  246. " --debugger # Run in debug mode\n"
  247. " --home directory # Change to directory to run\n"
  248. " --log logFile:level # Log to file file at verbosity level\n"
  249. " --route routeFile # Route configuration file\n"
  250. " --verbose # Same as --log stdout:2\n"
  251. " --version # Output version information\n\n",
  252. ME_TITLE, ME_NAME);
  253. exit(-1);
  254. }
  255. static void initPlatform()
  256. {
  257. #if ME_UNIX_LIKE
  258. signal(SIGTERM, sigHandler);
  259. #ifdef SIGPIPE
  260. signal(SIGPIPE, SIG_IGN);
  261. #endif
  262. #elif ME_WIN_LIKE
  263. _fmode=_O_BINARY;
  264. #endif
  265. }
  266. #if ME_UNIX_LIKE
  267. static void sigHandler(int signo)
  268. {
  269. finished = 1;
  270. }
  271. #endif
  272. #if WINDOWS
  273. /*
  274. Create a taskbar entry. Register the window class and create a window
  275. */
  276. static int windowsInit()
  277. {
  278. HINSTANCE inst;
  279. WNDCLASS wc; /* Window class */
  280. HMENU hSysMenu;
  281. HWND hwnd;
  282. inst = websGetInst();
  283. wc.style = CS_HREDRAW | CS_VREDRAW;
  284. wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  285. wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  286. wc.cbClsExtra = 0;
  287. wc.cbWndExtra = 0;
  288. wc.hInstance = inst;
  289. wc.hIcon = NULL;
  290. wc.lpfnWndProc = (WNDPROC) websWindProc;
  291. wc.lpszMenuName = wc.lpszClassName = ME_NAME;
  292. if (! RegisterClass(&wc)) {
  293. return -1;
  294. }
  295. /*
  296. Create a window just so we can have a taskbar to close this web server
  297. */
  298. hwnd = CreateWindow(ME_NAME, ME_TITLE, WS_MINIMIZE | WS_POPUPWINDOW, CW_USEDEFAULT,
  299. 0, 0, 0, NULL, NULL, inst, NULL);
  300. if (hwnd == NULL) {
  301. return -1;
  302. }
  303. /*
  304. Add the about box menu item to the system menu
  305. */
  306. hSysMenu = GetSystemMenu(hwnd, FALSE);
  307. if (hSysMenu != NULL) {
  308. AppendMenu(hSysMenu, MF_SEPARATOR, 0, NULL);
  309. }
  310. ShowWindow(hwnd, SW_SHOWNORMAL);
  311. UpdateWindow(hwnd);
  312. return 0;
  313. }
  314. static void windowsClose()
  315. {
  316. HINSTANCE inst;
  317. inst = websGetInst();
  318. UnregisterClass(ME_NAME, inst);
  319. }
  320. /*
  321. Main menu window message handler.
  322. */
  323. static LRESULT CALLBACK websWindProc(HWND hwnd, UINT msg, UINT wp, LPARAM lp)
  324. {
  325. switch (msg) {
  326. case WM_DESTROY:
  327. PostQuitMessage(0);
  328. finished++;
  329. return 0;
  330. case WM_SYSCOMMAND:
  331. break;
  332. }
  333. return DefWindowProc(hwnd, msg, wp, lp);
  334. }
  335. /*
  336. Check for Windows Messages
  337. */
  338. WPARAM checkWindowsMsgLoop()
  339. {
  340. MSG msg;
  341. if (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)) {
  342. if (!GetMessage(&msg, NULL, 0, 0) || msg.message == WM_QUIT) {
  343. return msg.wParam;
  344. }
  345. TranslateMessage(&msg);
  346. DispatchMessage(&msg);
  347. }
  348. return 0;
  349. }
  350. /*
  351. Windows message handler
  352. */
  353. static LRESULT CALLBACK websAboutProc(HWND hwndDlg, uint msg, uint wp, long lp)
  354. {
  355. LRESULT lResult;
  356. lResult = DefWindowProc(hwndDlg, msg, wp, lp);
  357. switch (msg) {
  358. case WM_CREATE:
  359. break;
  360. case WM_DESTROY:
  361. break;
  362. case WM_COMMAND:
  363. break;
  364. }
  365. return lResult;
  366. }
  367. #endif