main.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * Copyright (C) 2011-2016 Intel Corporation. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in
  12. * the documentation and/or other materials provided with the
  13. * distribution.
  14. * * Neither the name of Intel Corporation nor the names of its
  15. * contributors may be used to endorse or promote products derived
  16. * from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. */
  31. #include <signal.h>
  32. #include <unistd.h>
  33. #include <UnixServerSocket.h>
  34. #include <CAESMServer.h>
  35. #include <CSelector.h>
  36. #include <AESMLogicWrapper.h>
  37. #include <curl/curl.h>
  38. #include <error_report.h>
  39. #include <Config.h>
  40. #include <iostream>
  41. static bool curl_initialized;
  42. bool is_curl_initialized_succ()
  43. {
  44. return curl_initialized;
  45. }
  46. static CAESMServer* server = NULL;
  47. volatile bool reload = false;
  48. void signal_handler(int sig)
  49. {
  50. switch(sig)
  51. {
  52. case SIGTERM:
  53. if (server)
  54. {
  55. reload = false;
  56. server->shutDown();
  57. }
  58. break;
  59. case SIGHUP:
  60. if (server)
  61. {
  62. reload = true;
  63. server->shutDown();
  64. }
  65. break;
  66. default:
  67. break;
  68. }
  69. }
  70. int main() {
  71. if(daemon(0, 0) < 0)
  72. {
  73. AESM_LOG_INIT();
  74. AESM_LOG_FATAL("Fail to set daemon.");
  75. AESM_LOG_FINI();
  76. exit(1);
  77. }
  78. CURLcode curl_code = curl_global_init(CURL_GLOBAL_DEFAULT);
  79. if(curl_code!=CURLE_OK){
  80. curl_initialized = false;
  81. }else{
  82. curl_initialized = true;
  83. }
  84. signal(SIGCHLD, SIG_IGN);
  85. signal(SIGHUP, signal_handler);
  86. //ignore SIGPIPE, socket is unexpectedly closed by client
  87. signal(SIGPIPE, SIG_IGN);
  88. signal(SIGTERM, signal_handler);
  89. try{
  90. do{
  91. reload = false;
  92. AESMLogicWrapper* aesmLogic = new AESMLogicWrapper();
  93. if(aesmLogic->service_start()!=AE_SUCCESS){
  94. AESM_LOG_ERROR("Fail to start service.");
  95. delete aesmLogic;
  96. exit(1);
  97. }
  98. UnixServerSocket* serverSock = new UnixServerSocket(CONFIG_SOCKET_PATH);
  99. CSelector* selector = new CSelector(serverSock);
  100. server = new CAESMServer(serverSock, selector, aesmLogic);
  101. AESM_LOG_WARN("The server sock is %#lx" ,serverSock);
  102. server->init();
  103. server->doWork();
  104. CAESMServer* temp_server = server;
  105. server = NULL;
  106. delete temp_server;
  107. }while(reload == true);
  108. }
  109. catch(char const* error_msg)
  110. {
  111. AESM_LOG_FATAL("%s", error_msg);
  112. }
  113. return 0;
  114. }