I wish to make interactive code learning system, it allows users, (young programmers normally) to write contents of one function in c++ language, send it to server and there it will be compiled into dynamic library and called from main program.
Program expects function to return correct answer depending on given parameters.
Of course, there will be some kids, that will cause errors like segmentation fault. (server is Linux powered).
So, can I make signal handler that would exit function?
What I wish to accomplish:
for (int i = 0; i < PLAYER_NUM; i++) {
snprintf(buf, sizeof(buf), "players/%s.so", player[i]);
handle = dlopen(buf, RTLD_LAZY);
add[i] = (int (*)(int, int))dlsym(handle, "sum");
} // that was simply loading of functions from libraries.
for (int x = 0; x < 10; x++)
for (int i = 0; i < PLAYER_NUM; i++) {
if(failed[i]) continue;
ret = add[i](x, 5);
if(sigfault_received() || res != (x + 5)) {
failed[i] = true;
}
}
Faulty code can cause all kinds of issues which might not be recoverable. So handling SIGSEGV won't really help.
The solution is to run that code in a separate process and use IPC, pipes or sockets to communicate with the main process.