goahead.c 11 KB

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