goahead.c 10 KB

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