Skip to content

Commit

Permalink
fix: Make signal handler code asynchronous-safe.
Browse files Browse the repository at this point in the history
- Add a SipiGlobal class to handle global initialisation and cleanup using RAII.
- Replace exit() in main() with “return EXIT_SUCCESS” or “return EXIT_FAILURE”.
  • Loading branch information
Benjamin Geer committed Dec 16, 2016
1 parent 3a1cd65 commit 8cc43b3
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 15 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ In the main directory, call:
local/bin/sipi -config config/sipi.config.lua
```

All operations are written to the log file `sipi.log.file`.
All operations are written to the log file `sipi.log`.

## Serving an Image

Expand Down
5 changes: 4 additions & 1 deletion shttps/Server.h
Original file line number Diff line number Diff line change
Expand Up @@ -385,9 +385,12 @@ namespace shttps {

/*!
* Stop the server gracefully (all destructors are called etc.) and the
* cache file is updated.
* cache file is updated. This function is asynchronous-safe, so it may be called
* from within a signal handler.
*/
inline void stop(void) {
// POSIX declares write() to be asynchronous-safe.
// See https://www.securecoding.cert.org/confluence/display/c/SIG30-C.+Call+only+asynchronous-safe+functions+within+signal+handlers
write(stoppipe[1], "@", 1);
}

Expand Down
43 changes: 30 additions & 13 deletions src/sipi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
#include <csignal>
#include <utility>

#include <stdlib.h>
#include <curl/curl.h>

#include "shttps/Logger.h"
#include "shttps/Global.h"
#include "shttps/LuaServer.h"
Expand Down Expand Up @@ -104,16 +107,13 @@ static std::string fileType_string(FileType f_type) {
};

static void sighandler(int sig) {
std::cerr << std::endl << "Got SIGINT, stopping server gracefully...." << std::endl;
auto logger = Logger::getLogger(shttps::loggername);
// Any functions called in a signal handler must be asynchronous-safe.
// See https://www.securecoding.cert.org/confluence/display/c/SIG30-C.+Call+only+asynchronous-safe+functions+within+signal+handlers

if (serverptr != NULL) {
*logger << Logger::LogLevel::INFORMATIONAL << "Got SIGINT, stopping server" << Logger::LogAction::FLUSH;
// Server::stop() is asynchronous-safe.
serverptr->stop();
}
else {
*logger << Logger::LogLevel::INFORMATIONAL << "Got SIGINT, exiting server" << Logger::LogAction::FLUSH;
exit(0);
}
}
//=========================================================================

Expand Down Expand Up @@ -245,8 +245,24 @@ static void sipiConfGlobals(lua_State *L, shttps::Connection &conn, void *user_d
lua_setglobal(L, "config");
}

namespace Sipi {
/*!
* Handles global initialisation and cleanup.
*/
class SipiGlobal {
public:
SipiGlobal() {
curl_global_init(CURL_GLOBAL_ALL);
}

~SipiGlobal() {
curl_global_cleanup();
}
};
}

int main (int argc, char *argv[]) {
Sipi::SipiGlobal sipiGlobal;

//
// register namespace sipi in xmp. Since this part of the XMP library is
Expand Down Expand Up @@ -289,7 +305,7 @@ int main (int argc, char *argv[]) {
}
catch (Sipi::SipiError &err) {
std::cerr << err;
exit (-1);
return EXIT_FAILURE;
}

//
Expand All @@ -301,7 +317,7 @@ int main (int argc, char *argv[]) {
}
catch (Sipi::SipiError &err) {
std::cerr << err;
exit (-1);
return EXIT_FAILURE;
}
Sipi::SipiImage img1, img2;
img1.read(infname1);
Expand Down Expand Up @@ -439,7 +455,7 @@ int main (int argc, char *argv[]) {
}
catch (Sipi::SipiError &err) {
std::cerr << err;
exit (-1);
return EXIT_FAILURE;
}

//
Expand All @@ -451,7 +467,7 @@ int main (int argc, char *argv[]) {
}
catch (Sipi::SipiError &err) {
std::cerr << err;
exit (-1);
return EXIT_FAILURE;
}

//
Expand All @@ -463,7 +479,7 @@ int main (int argc, char *argv[]) {
}
catch (Sipi::SipiError &err) {
std::cerr << err;
exit (-1);
return EXIT_FAILURE;
}


Expand Down Expand Up @@ -590,5 +606,6 @@ int main (int argc, char *argv[]) {
}

}
return 0;

return EXIT_SUCCESS;
}

0 comments on commit 8cc43b3

Please sign in to comment.