goahead.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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. static void uploadTest(Webs *wp);
  45. /*********************************** Code *************************************/
  46. MAIN(goahead, int argc, char **argv, char **envp)
  47. {
  48. char *argp, *home, *documents, *endpoints, *endpoint, *route, *auth, *tok, *lspec;
  49. int argind;
  50. #if WINDOWS
  51. if (windowsInit() < 0) {
  52. return 0;
  53. }
  54. #endif
  55. route = "/etc/goahead/route.txt";
  56. auth = "/etc/goahead/auth.txt";
  57. /**************** user code before goahead ************************/
  58. cJSON_Hooks hooks;
  59. hooks.malloc_fn = (void *(*)(size_t))walloc;
  60. hooks.free_fn = (void (*)(void *))wfree;
  61. cJSON_InitHooks(&hooks);
  62. /**************** user code before goahead end************************/
  63. for (argind = 1; argind < argc; argind++) {
  64. argp = argv[argind];
  65. if (*argp != '-') {
  66. break;
  67. } else if (smatch(argp, "--auth") || smatch(argp, "-a")) {
  68. if (argind >= argc) usage();
  69. auth = argv[++argind];
  70. #if ME_UNIX_LIKE && !MACOSX
  71. } else if (smatch(argp, "--background") || smatch(argp, "-b")) {
  72. websSetBackground(1);
  73. #endif
  74. } else if (smatch(argp, "--debugger") || smatch(argp, "-d") || smatch(argp, "-D")) {
  75. websSetDebug(1);
  76. } else if (smatch(argp, "--home")) {
  77. if (argind >= argc) usage();
  78. home = argv[++argind];
  79. if (chdir(home) < 0) {
  80. error("Cannot change directory to %s", home);
  81. exit(-1);
  82. }
  83. } else if (smatch(argp, "--log") || smatch(argp, "-l")) {
  84. if (argind >= argc) usage();
  85. logSetPath(argv[++argind]);
  86. } else if (smatch(argp, "--verbose") || smatch(argp, "-v")) {
  87. logSetPath("stdout:2"); //2
  88. } else if (smatch(argp, "--route") || smatch(argp, "-r")) {
  89. route = argv[++argind];
  90. } else if (smatch(argp, "--version") || smatch(argp, "-V")) {
  91. printf("%s\n", ME_VERSION);
  92. exit(0);
  93. } else if (*argp == '-' && isdigit((uchar) argp[1])) {
  94. lspec = sfmt("stdout:%s", &argp[1]);
  95. logSetPath(lspec);
  96. wfree(lspec);
  97. } else {
  98. usage();
  99. }
  100. }
  101. documents = ME_GOAHEAD_DOCUMENTS;
  102. if (argc > argind) {
  103. documents = argv[argind++];
  104. }
  105. printf("---> initPlatform\n");
  106. initPlatform();
  107. printf("---> websOpen\n");
  108. // printf(">>>>>>>>>>>>>>>>>>>>>>>>>>2>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
  109. if (websOpen(documents, route) < 0) {
  110. error("Cannot initialize server. Exiting.");
  111. return -1;
  112. }
  113. #if ME_GOAHEAD_AUTH
  114. printf("---> websLoad\n");
  115. if (websLoad(auth) < 0) {
  116. error("Cannot load %s", auth);
  117. return -1;
  118. }
  119. #endif
  120. logHeader();
  121. if (argind < argc) {
  122. while (argind < argc) {
  123. endpoint = argv[argind++];
  124. printf("---> websListen log1\n");
  125. if (websListen(endpoint) < 0) {
  126. return -1;
  127. }
  128. }
  129. } else {
  130. endpoints = sclone(ME_GOAHEAD_LISTEN);
  131. for (endpoint = stok(endpoints, ", \t", &tok); endpoint; endpoint = stok(NULL, ", \t,", &tok)) {
  132. #if !ME_COM_SSL
  133. if (strstr(endpoint, "https")) continue;
  134. #endif
  135. printf("---> websListen log2\n");
  136. if (websListen(endpoint) < 0) {
  137. wfree(endpoints);
  138. return -1;
  139. }
  140. }
  141. wfree(endpoints);
  142. }
  143. #if ME_ROM && KEEP
  144. /*
  145. If not using a route/auth config files, then manually create the routes like this:
  146. If custom matching is required, use websSetRouteMatch. If authentication is required, use websSetRouteAuth.
  147. */
  148. websAddRoute("/", "file", 0);
  149. #endif
  150. /**************** user code after goahead ************************/
  151. websDefineAction("test", actionTest);
  152. websDefineAction("buy", buy);
  153. //dashboard
  154. websDefineAction("getDeviceInfo", getDeviceInfo);
  155. websDefineAction("getSysInfo", getSysInfo);
  156. websDefineAction("getCardInfo", getCardInfo);
  157. websDefineAction("getSensorInfo", getSensorInfo);
  158. //remote control
  159. websDefineAction("chassisPwrCtrl", chassisPwrCtrl);
  160. websDefineAction("getChassisStatus", getChassisStatus);
  161. //Fru
  162. //websDefineAction("getFruInfo", getFruInfo);
  163. websDefineAction("getFruChassisInfo", getFruChassisInfo);
  164. websDefineAction("getFruBoardInfo", getFruBoardInfo);
  165. websDefineAction("getFruProductInfo", getFruProductInfo);
  166. //sel
  167. websDefineAction("Web_ClearSEL", Web_ClearSEL);
  168. websDefineAction("GetAllSELEntriesSorted", GetAllSELEntriesSorted);
  169. //user
  170. websDefineAction("getAllUserInfo", getAllUserInfo);
  171. websDefineAction("setUserPassword", setUserPassword);
  172. websDefineAction("addUser", addUser);
  173. websDefineAction("delUser", delUser);
  174. //update firmware
  175. websDefineAction("uploadTest", uploadTest);
  176. websDefineAction("prepareDevice", prepareDevice);
  177. websDefineAction("uploadFirmware", uploadFirmware);
  178. websDefineAction("updateFlash", updateFlash);
  179. websDefineAction("getUpdateProgress", getUpdateProgress);
  180. websDefineAction("getVerifyStatus", getVerifyStatus);
  181. websDefineAction("resetBmc", resetBmc);
  182. /**************** user code after goahead end************************/
  183. #if ME_UNIX_LIKE && !MACOSX
  184. /*
  185. Service events till terminated
  186. */
  187. if (websGetBackground()) {
  188. printf("---> daemon\n");
  189. if (daemon(0, 0) < 0) {
  190. error("Cannot run as daemon");
  191. return -1;
  192. }
  193. }
  194. #endif
  195. printf("---> websServiceEvents\n");
  196. websServiceEvents(&finished);
  197. logmsg(1, "Instructed to exit");
  198. printf("---> websClose\n");
  199. websClose();
  200. #if WINDOWS
  201. windowsClose();
  202. #endif
  203. return 0;
  204. }
  205. // typedef struct{
  206. // char buf[1024*1000];
  207. // char filename[32];
  208. // int fileSize;
  209. // } filestruct;
  210. // //这个函数是把16进制字符串转换成16进制
  211. // int StringToHex(char *str, unsigned char *out, unsigned int *outlen)
  212. // {
  213. // char *p = str;
  214. // char high = 0, low = 0;
  215. // int tmplen = strlen(p), cnt = 0;
  216. // tmplen = strlen(p);
  217. // while(cnt < (tmplen / 2))
  218. // {
  219. // high = ((*p > '9') && ((*p <= 'F') || (*p <= 'f'))) ? *p - 48 - 7 : *p - 48;
  220. // low = (*(++ p) > '9' && ((*p <= 'F') || (*p <= 'f'))) ? *(p) - 48 - 7 : *(p) - 48;
  221. // out[cnt] = ((high & 0x0f) << 4 | (low & 0x0f));
  222. // p ++;
  223. // cnt ++;
  224. // }
  225. // if(tmplen % 2 != 0) out[cnt] = ((*p > '9') && ((*p <= 'F') || (*p <= 'f'))) ? *p - 48 - 7 : *p - 48;
  226. // if(outlen != NULL) *outlen = tmplen / 2 + tmplen % 2;
  227. // return tmplen / 2 + tmplen % 2;
  228. // }
  229. static void uploadTest(Webs *wp)
  230. {
  231. WebsKey *s;
  232. WebsUpload *up;
  233. char *upfile;
  234. websSetStatus(wp, 200);
  235. websWriteHeaders(wp, -1, 0);
  236. websWriteHeader(wp, "Content-Type", "text/plain");
  237. websWriteEndHeaders(wp);
  238. if (scaselessmatch(wp->method, "POST")) {
  239. for (s = hashFirst(wp->files); s; s = hashNext(wp->files, s)) {
  240. up = s->content.value.symbol;
  241. websWrite(wp, "FILE: %s\r\n", s->name.value.string);
  242. websWrite(wp, "FILENAME=%s\r\n", up->filename);
  243. websWrite(wp, "CLIENT=%s\r\n", up->clientFilename);
  244. websWrite(wp, "TYPE=%s\r\n", up->contentType);
  245. websWrite(wp, "SIZE=%d\r\n", up->size);
  246. upfile = sfmt("%s/tmp/%s", websGetDocuments(), up->clientFilename);
  247. printf("<<<<<<<<<<<%s>>>>>>>>>>\n", upfile);
  248. if (rename(up->filename, upfile) < 0) {
  249. error("Cannot rename uploaded file: %s to %s, errno %d", up->filename, upfile, errno);
  250. }
  251. wfree(upfile);
  252. }
  253. websWrite(wp, "\r\nVARS:\r\n");
  254. for (s = hashFirst(wp->vars); s; s = hashNext(wp->vars, s)) {
  255. websWrite(wp, "%s=%s\r\n", s->name.value.string, s->content.value.string);
  256. }
  257. }
  258. // char *pStr;
  259. // cJSON * root = cJSON_CreateObject();
  260. // cJSON * data = cJSON_CreateObject();
  261. // cJSON_AddItemToObject(root, "data", data);//根节点下添加
  262. // cJSON_AddStringToObject(root, "msg", "");
  263. // cJSON_AddNumberToObject(root, "code", 200);
  264. // pStr = cJSON_PrintUnformatted(root);
  265. // printf("---> cJSON Str:\n%s\n", pStr);
  266. // websSetStatus(wp, 200);
  267. // websWriteHeaders(wp, -1, 0);
  268. // websWriteEndHeaders(wp);
  269. // websWrite(wp,"%s", pStr);
  270. // websFlush(wp, 0);
  271. // // websDone(wp);
  272. // if(pStr)
  273. // wfree(pStr);
  274. // if(root)
  275. // cJSON_Delete(root);
  276. websDone(wp);
  277. }
  278. static void logHeader()
  279. {
  280. char home[ME_GOAHEAD_LIMIT_STRING];
  281. getcwd(home, sizeof(home));
  282. logmsg(2, "Configuration for %s", ME_TITLE);
  283. logmsg(2, "---------------------------------------------");
  284. logmsg(2, "Version: %s", ME_VERSION);
  285. logmsg(2, "BuildType: %s", ME_DEBUG ? "Debug" : "Release");
  286. logmsg(2, "CPU: %s", ME_CPU);
  287. logmsg(2, "OS: %s", ME_OS);
  288. logmsg(2, "Host: %s", websGetServer());
  289. logmsg(2, "Directory: %s", home);
  290. logmsg(2, "Documents: %s", websGetDocuments());
  291. logmsg(2, "Configure: %s", ME_CONFIG_CMD);
  292. logmsg(2, "---------------------------------------------");
  293. }
  294. static void usage() {
  295. fprintf(stderr, "\n%s Usage:\n\n"
  296. " %s [options] [documents] [[IPaddress][:port] ...]\n\n"
  297. " Options:\n"
  298. #if ME_GOAHEAD_AUTH
  299. " --auth authFile # User and role configuration\n"
  300. #endif
  301. #if ME_UNIX_LIKE && !MACOSX
  302. " --background # Run as a Unix daemon\n"
  303. #endif
  304. " --debugger # Run in debug mode\n"
  305. " --home directory # Change to directory to run\n"
  306. " --log logFile:level # Log to file file at verbosity level\n"
  307. " --route routeFile # Route configuration file\n"
  308. " --verbose # Same as --log stdout:2\n"
  309. " --version # Output version information\n\n",
  310. ME_TITLE, ME_NAME);
  311. exit(-1);
  312. }
  313. static void initPlatform()
  314. {
  315. #if ME_UNIX_LIKE
  316. signal(SIGTERM, sigHandler);
  317. #ifdef SIGPIPE
  318. signal(SIGPIPE, SIG_IGN);
  319. #endif
  320. #elif ME_WIN_LIKE
  321. _fmode=_O_BINARY;
  322. #endif
  323. }
  324. #if ME_UNIX_LIKE
  325. static void sigHandler(int signo)
  326. {
  327. finished = 1;
  328. }
  329. #endif
  330. #if WINDOWS
  331. /*
  332. Create a taskbar entry. Register the window class and create a window
  333. */
  334. static int windowsInit()
  335. {
  336. HINSTANCE inst;
  337. WNDCLASS wc; /* Window class */
  338. HMENU hSysMenu;
  339. HWND hwnd;
  340. inst = websGetInst();
  341. wc.style = CS_HREDRAW | CS_VREDRAW;
  342. wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  343. wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  344. wc.cbClsExtra = 0;
  345. wc.cbWndExtra = 0;
  346. wc.hInstance = inst;
  347. wc.hIcon = NULL;
  348. wc.lpfnWndProc = (WNDPROC) websWindProc;
  349. wc.lpszMenuName = wc.lpszClassName = ME_NAME;
  350. if (! RegisterClass(&wc)) {
  351. return -1;
  352. }
  353. /*
  354. Create a window just so we can have a taskbar to close this web server
  355. */
  356. hwnd = CreateWindow(ME_NAME, ME_TITLE, WS_MINIMIZE | WS_POPUPWINDOW, CW_USEDEFAULT,
  357. 0, 0, 0, NULL, NULL, inst, NULL);
  358. if (hwnd == NULL) {
  359. return -1;
  360. }
  361. /*
  362. Add the about box menu item to the system menu
  363. */
  364. hSysMenu = GetSystemMenu(hwnd, FALSE);
  365. if (hSysMenu != NULL) {
  366. AppendMenu(hSysMenu, MF_SEPARATOR, 0, NULL);
  367. }
  368. ShowWindow(hwnd, SW_SHOWNORMAL);
  369. UpdateWindow(hwnd);
  370. return 0;
  371. }
  372. static void windowsClose()
  373. {
  374. HINSTANCE inst;
  375. inst = websGetInst();
  376. UnregisterClass(ME_NAME, inst);
  377. }
  378. /*
  379. Main menu window message handler.
  380. */
  381. static LRESULT CALLBACK websWindProc(HWND hwnd, UINT msg, UINT wp, LPARAM lp)
  382. {
  383. switch (msg) {
  384. case WM_DESTROY:
  385. PostQuitMessage(0);
  386. finished++;
  387. return 0;
  388. case WM_SYSCOMMAND:
  389. break;
  390. }
  391. return DefWindowProc(hwnd, msg, wp, lp);
  392. }
  393. /*
  394. Check for Windows Messages
  395. */
  396. WPARAM checkWindowsMsgLoop()
  397. {
  398. MSG msg;
  399. if (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)) {
  400. if (!GetMessage(&msg, NULL, 0, 0) || msg.message == WM_QUIT) {
  401. return msg.wParam;
  402. }
  403. TranslateMessage(&msg);
  404. DispatchMessage(&msg);
  405. }
  406. return 0;
  407. }
  408. /*
  409. Windows message handler
  410. */
  411. static LRESULT CALLBACK websAboutProc(HWND hwndDlg, uint msg, uint wp, long lp)
  412. {
  413. LRESULT lResult;
  414. lResult = DefWindowProc(hwndDlg, msg, wp, lp);
  415. switch (msg) {
  416. case WM_CREATE:
  417. break;
  418. case WM_DESTROY:
  419. break;
  420. case WM_COMMAND:
  421. break;
  422. }
  423. return lResult;
  424. }
  425. #endif