goahead.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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("web_getSensorHistory", web_getSensorHistory);
  192. //fan
  193. websDefineAction("getAllFanInfo", getAllFanInfo);
  194. websDefineAction("setFanInfo", setFanInfo);
  195. //config
  196. websDefineAction("restoreFactorySettings", restoreFactorySettings);
  197. websDefineAction("web_ResetBMC", web_ResetBMC);
  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. printf("---> websServiceEvents\n");
  212. websServiceEvents(&finished);
  213. logmsg(1, "Instructed to exit");
  214. printf("---> websClose\n");
  215. websClose();
  216. #if WINDOWS
  217. windowsClose();
  218. #endif
  219. return 0;
  220. }
  221. static void logHeader()
  222. {
  223. char home[ME_GOAHEAD_LIMIT_STRING];
  224. getcwd(home, sizeof(home));
  225. logmsg(2, "Configuration for %s", ME_TITLE);
  226. logmsg(2, "---------------------------------------------");
  227. logmsg(2, "Version: %s", ME_VERSION);
  228. logmsg(2, "BuildType: %s", ME_DEBUG ? "Debug" : "Release");
  229. logmsg(2, "CPU: %s", ME_CPU);
  230. logmsg(2, "OS: %s", ME_OS);
  231. logmsg(2, "Host: %s", websGetServer());
  232. logmsg(2, "Directory: %s", home);
  233. logmsg(2, "Documents: %s", websGetDocuments());
  234. logmsg(2, "Configure: %s", ME_CONFIG_CMD);
  235. logmsg(2, "---------------------------------------------");
  236. }
  237. static void usage() {
  238. fprintf(stderr, "\n%s Usage:\n\n"
  239. " %s [options] [documents] [[IPaddress][:port] ...]\n\n"
  240. " Options:\n"
  241. #if ME_GOAHEAD_AUTH
  242. " --auth authFile # User and role configuration\n"
  243. #endif
  244. #if ME_UNIX_LIKE && !MACOSX
  245. " --background # Run as a Unix daemon\n"
  246. #endif
  247. " --debugger # Run in debug mode\n"
  248. " --home directory # Change to directory to run\n"
  249. " --log logFile:level # Log to file file at verbosity level\n"
  250. " --route routeFile # Route configuration file\n"
  251. " --verbose # Same as --log stdout:2\n"
  252. " --version # Output version information\n\n",
  253. ME_TITLE, ME_NAME);
  254. exit(-1);
  255. }
  256. static void initPlatform()
  257. {
  258. #if ME_UNIX_LIKE
  259. signal(SIGTERM, sigHandler);
  260. #ifdef SIGPIPE
  261. signal(SIGPIPE, SIG_IGN);
  262. #endif
  263. #elif ME_WIN_LIKE
  264. _fmode=_O_BINARY;
  265. #endif
  266. }
  267. #if ME_UNIX_LIKE
  268. static void sigHandler(int signo)
  269. {
  270. finished = 1;
  271. }
  272. #endif
  273. #if WINDOWS
  274. /*
  275. Create a taskbar entry. Register the window class and create a window
  276. */
  277. static int windowsInit()
  278. {
  279. HINSTANCE inst;
  280. WNDCLASS wc; /* Window class */
  281. HMENU hSysMenu;
  282. HWND hwnd;
  283. inst = websGetInst();
  284. wc.style = CS_HREDRAW | CS_VREDRAW;
  285. wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  286. wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  287. wc.cbClsExtra = 0;
  288. wc.cbWndExtra = 0;
  289. wc.hInstance = inst;
  290. wc.hIcon = NULL;
  291. wc.lpfnWndProc = (WNDPROC) websWindProc;
  292. wc.lpszMenuName = wc.lpszClassName = ME_NAME;
  293. if (! RegisterClass(&wc)) {
  294. return -1;
  295. }
  296. /*
  297. Create a window just so we can have a taskbar to close this web server
  298. */
  299. hwnd = CreateWindow(ME_NAME, ME_TITLE, WS_MINIMIZE | WS_POPUPWINDOW, CW_USEDEFAULT,
  300. 0, 0, 0, NULL, NULL, inst, NULL);
  301. if (hwnd == NULL) {
  302. return -1;
  303. }
  304. /*
  305. Add the about box menu item to the system menu
  306. */
  307. hSysMenu = GetSystemMenu(hwnd, FALSE);
  308. if (hSysMenu != NULL) {
  309. AppendMenu(hSysMenu, MF_SEPARATOR, 0, NULL);
  310. }
  311. ShowWindow(hwnd, SW_SHOWNORMAL);
  312. UpdateWindow(hwnd);
  313. return 0;
  314. }
  315. static void windowsClose()
  316. {
  317. HINSTANCE inst;
  318. inst = websGetInst();
  319. UnregisterClass(ME_NAME, inst);
  320. }
  321. /*
  322. Main menu window message handler.
  323. */
  324. static LRESULT CALLBACK websWindProc(HWND hwnd, UINT msg, UINT wp, LPARAM lp)
  325. {
  326. switch (msg) {
  327. case WM_DESTROY:
  328. PostQuitMessage(0);
  329. finished++;
  330. return 0;
  331. case WM_SYSCOMMAND:
  332. break;
  333. }
  334. return DefWindowProc(hwnd, msg, wp, lp);
  335. }
  336. /*
  337. Check for Windows Messages
  338. */
  339. WPARAM checkWindowsMsgLoop()
  340. {
  341. MSG msg;
  342. if (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)) {
  343. if (!GetMessage(&msg, NULL, 0, 0) || msg.message == WM_QUIT) {
  344. return msg.wParam;
  345. }
  346. TranslateMessage(&msg);
  347. DispatchMessage(&msg);
  348. }
  349. return 0;
  350. }
  351. /*
  352. Windows message handler
  353. */
  354. static LRESULT CALLBACK websAboutProc(HWND hwndDlg, uint msg, uint wp, long lp)
  355. {
  356. LRESULT lResult;
  357. lResult = DefWindowProc(hwndDlg, msg, wp, lp);
  358. switch (msg) {
  359. case WM_CREATE:
  360. break;
  361. case WM_DESTROY:
  362. break;
  363. case WM_COMMAND:
  364. break;
  365. }
  366. return lResult;
  367. }
  368. #endif