goahead.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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. //remote control
  158. websDefineAction("chassisPwrCtrl", chassisPwrCtrl);
  159. websDefineAction("getChassisStatus", getChassisStatus);
  160. //Fru
  161. //websDefineAction("getFruInfo", getFruInfo);
  162. websDefineAction("getFruChassisInfo", getFruChassisInfo);
  163. websDefineAction("getFruBoardInfo", getFruBoardInfo);
  164. websDefineAction("getFruProductInfo", getFruProductInfo);
  165. //sel
  166. websDefineAction("Web_ClearSEL", Web_ClearSEL);
  167. websDefineAction("GetAllSELEntriesSorted", GetAllSELEntriesSorted);
  168. //user
  169. websDefineAction("getAllUserInfo", getAllUserInfo);
  170. websDefineAction("setUserPassword", setUserPassword);
  171. websDefineAction("addUser", addUser);
  172. websDefineAction("delUser", delUser);
  173. //update firmware
  174. // websDefineAction("uploadTest", uploadTest);
  175. websDefineAction("prepareDevice", prepareDevice);
  176. websDefineAction("uploadFirmware", uploadFirmware);
  177. websDefineAction("updateFlash", updateFlash);
  178. websDefineAction("getUpdateProgress", getUpdateProgress);
  179. websDefineAction("getVerifyStatus", getVerifyStatus);
  180. websDefineAction("resetBmc", resetBmc);
  181. /**************** user code after goahead end************************/
  182. #if ME_UNIX_LIKE && !MACOSX
  183. /*
  184. Service events till terminated
  185. */
  186. if (websGetBackground()) {
  187. printf("---> daemon\n");
  188. if (daemon(0, 0) < 0) {
  189. error("Cannot run as daemon");
  190. return -1;
  191. }
  192. }
  193. #endif
  194. printf("---> websServiceEvents\n");
  195. websServiceEvents(&finished);
  196. logmsg(1, "Instructed to exit");
  197. printf("---> websClose\n");
  198. websClose();
  199. #if WINDOWS
  200. windowsClose();
  201. #endif
  202. return 0;
  203. }
  204. static void logHeader()
  205. {
  206. char home[ME_GOAHEAD_LIMIT_STRING];
  207. getcwd(home, sizeof(home));
  208. logmsg(2, "Configuration for %s", ME_TITLE);
  209. logmsg(2, "---------------------------------------------");
  210. logmsg(2, "Version: %s", ME_VERSION);
  211. logmsg(2, "BuildType: %s", ME_DEBUG ? "Debug" : "Release");
  212. logmsg(2, "CPU: %s", ME_CPU);
  213. logmsg(2, "OS: %s", ME_OS);
  214. logmsg(2, "Host: %s", websGetServer());
  215. logmsg(2, "Directory: %s", home);
  216. logmsg(2, "Documents: %s", websGetDocuments());
  217. logmsg(2, "Configure: %s", ME_CONFIG_CMD);
  218. logmsg(2, "---------------------------------------------");
  219. }
  220. static void usage() {
  221. fprintf(stderr, "\n%s Usage:\n\n"
  222. " %s [options] [documents] [[IPaddress][:port] ...]\n\n"
  223. " Options:\n"
  224. #if ME_GOAHEAD_AUTH
  225. " --auth authFile # User and role configuration\n"
  226. #endif
  227. #if ME_UNIX_LIKE && !MACOSX
  228. " --background # Run as a Unix daemon\n"
  229. #endif
  230. " --debugger # Run in debug mode\n"
  231. " --home directory # Change to directory to run\n"
  232. " --log logFile:level # Log to file file at verbosity level\n"
  233. " --route routeFile # Route configuration file\n"
  234. " --verbose # Same as --log stdout:2\n"
  235. " --version # Output version information\n\n",
  236. ME_TITLE, ME_NAME);
  237. exit(-1);
  238. }
  239. static void initPlatform()
  240. {
  241. #if ME_UNIX_LIKE
  242. signal(SIGTERM, sigHandler);
  243. #ifdef SIGPIPE
  244. signal(SIGPIPE, SIG_IGN);
  245. #endif
  246. #elif ME_WIN_LIKE
  247. _fmode=_O_BINARY;
  248. #endif
  249. }
  250. #if ME_UNIX_LIKE
  251. static void sigHandler(int signo)
  252. {
  253. finished = 1;
  254. }
  255. #endif
  256. #if WINDOWS
  257. /*
  258. Create a taskbar entry. Register the window class and create a window
  259. */
  260. static int windowsInit()
  261. {
  262. HINSTANCE inst;
  263. WNDCLASS wc; /* Window class */
  264. HMENU hSysMenu;
  265. HWND hwnd;
  266. inst = websGetInst();
  267. wc.style = CS_HREDRAW | CS_VREDRAW;
  268. wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  269. wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  270. wc.cbClsExtra = 0;
  271. wc.cbWndExtra = 0;
  272. wc.hInstance = inst;
  273. wc.hIcon = NULL;
  274. wc.lpfnWndProc = (WNDPROC) websWindProc;
  275. wc.lpszMenuName = wc.lpszClassName = ME_NAME;
  276. if (! RegisterClass(&wc)) {
  277. return -1;
  278. }
  279. /*
  280. Create a window just so we can have a taskbar to close this web server
  281. */
  282. hwnd = CreateWindow(ME_NAME, ME_TITLE, WS_MINIMIZE | WS_POPUPWINDOW, CW_USEDEFAULT,
  283. 0, 0, 0, NULL, NULL, inst, NULL);
  284. if (hwnd == NULL) {
  285. return -1;
  286. }
  287. /*
  288. Add the about box menu item to the system menu
  289. */
  290. hSysMenu = GetSystemMenu(hwnd, FALSE);
  291. if (hSysMenu != NULL) {
  292. AppendMenu(hSysMenu, MF_SEPARATOR, 0, NULL);
  293. }
  294. ShowWindow(hwnd, SW_SHOWNORMAL);
  295. UpdateWindow(hwnd);
  296. return 0;
  297. }
  298. static void windowsClose()
  299. {
  300. HINSTANCE inst;
  301. inst = websGetInst();
  302. UnregisterClass(ME_NAME, inst);
  303. }
  304. /*
  305. Main menu window message handler.
  306. */
  307. static LRESULT CALLBACK websWindProc(HWND hwnd, UINT msg, UINT wp, LPARAM lp)
  308. {
  309. switch (msg) {
  310. case WM_DESTROY:
  311. PostQuitMessage(0);
  312. finished++;
  313. return 0;
  314. case WM_SYSCOMMAND:
  315. break;
  316. }
  317. return DefWindowProc(hwnd, msg, wp, lp);
  318. }
  319. /*
  320. Check for Windows Messages
  321. */
  322. WPARAM checkWindowsMsgLoop()
  323. {
  324. MSG msg;
  325. if (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)) {
  326. if (!GetMessage(&msg, NULL, 0, 0) || msg.message == WM_QUIT) {
  327. return msg.wParam;
  328. }
  329. TranslateMessage(&msg);
  330. DispatchMessage(&msg);
  331. }
  332. return 0;
  333. }
  334. /*
  335. Windows message handler
  336. */
  337. static LRESULT CALLBACK websAboutProc(HWND hwndDlg, uint msg, uint wp, long lp)
  338. {
  339. LRESULT lResult;
  340. lResult = DefWindowProc(hwndDlg, msg, wp, lp);
  341. switch (msg) {
  342. case WM_CREATE:
  343. break;
  344. case WM_DESTROY:
  345. break;
  346. case WM_COMMAND:
  347. break;
  348. }
  349. return lResult;
  350. }
  351. #endif