goahead.c 10 KB

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