main.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // Copyright (c) 2020-2023 Cesanta Software Limited
  2. // All rights reserved
  3. #include <syslog.h>
  4. #include "mongoose.h"
  5. #include "net.h"
  6. #include "appl.h"
  7. static int s_sig_num;
  8. static void signal_handler(int sig_num) {
  9. signal(sig_num, signal_handler);
  10. s_sig_num = sig_num;
  11. }
  12. static void mylog(char ch, void *param) {
  13. static char buf[256];
  14. static size_t len;
  15. buf[len++] = ch;
  16. if (ch == '\n' || len >= sizeof(buf)) {
  17. syslog(LOG_INFO, "%.*s", (int) len, buf); // Send logs
  18. len = 0;
  19. }
  20. }
  21. /*
  22. * System function daemon() replacement based on FreeBSD implementation.
  23. * Original source file CVS tag:
  24. * $FreeBSD: src/lib/libc/gen/daemon.c,v 1.3 2000/01/27 23:06:14 jasone Exp $
  25. */
  26. static int become_daemon(nochdir, noclose)
  27. int nochdir, noclose;
  28. {
  29. int fd;
  30. switch (fork())
  31. {
  32. case -1:
  33. return (-1);
  34. case 0:
  35. break;
  36. default:
  37. _exit(0);
  38. }
  39. if (setsid() == -1)
  40. return (-1);
  41. if (!nochdir)
  42. (void)chdir("/");
  43. if (!noclose && (fd = open("/dev/null", O_RDWR, 0)) != -1)
  44. {
  45. (void)dup2(fd, STDIN_FILENO);
  46. (void)dup2(fd, STDOUT_FILENO);
  47. (void)dup2(fd, STDERR_FILENO);
  48. if (fd > 2)
  49. (void)close(fd);
  50. }
  51. return (0);
  52. }
  53. int main(void) {
  54. struct mg_mgr mgr;
  55. signal(SIGPIPE, SIG_IGN);
  56. signal(SIGINT, signal_handler);
  57. signal(SIGTERM, signal_handler);
  58. openlog(NULL, LOG_CONS | LOG_PID, 0);
  59. syslog(LOG_INFO, "%s ++ ", __func__);
  60. syslog(LOG_INFO, "Mac Size : char:%d short:%d int:%d int64_t:%d long:%d long long:%d float:%d double:%d",
  61. sizeof(char), sizeof(short), sizeof(int), sizeof(int64_t), sizeof(long), sizeof(long long), sizeof(float), sizeof(double));
  62. if( become_daemon(true, false) != 0){
  63. syslog(LOG_INFO, "%s, become_daemon Fail", __func__);
  64. return 0;
  65. }
  66. //mg_log_set(MG_LL_DEBUG); // Set debug log level
  67. mg_log_set(MG_LL_ERROR);
  68. //mg_log_set_fn(mylog, NULL);
  69. mg_mgr_init(&mgr);
  70. appl_start();
  71. web_init(&mgr);
  72. while (1) { // s_sig_num == 0
  73. mg_mgr_poll(&mgr, 50);
  74. }
  75. mg_mgr_free(&mgr);
  76. MG_INFO(("Exiting on signal %d", s_sig_num));
  77. return 0;
  78. }