goahead.c 12 KB

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