|
| 1 | +#include "./client.hpp" |
| 2 | + |
| 3 | +/**Points to know when handling signals - |
| 4 | + * |
| 5 | + * 1. |
| 6 | + * Prefer `sigaction` over `signal` |
| 7 | + * link - |
| 8 | + * https://stackoverflow.com/a/232711 |
| 9 | + * |
| 10 | + * 2. |
| 11 | + * When you install a custom signal handler, it overrides the default handler. |
| 12 | + * So be extremely careful about what you are handling, |
| 13 | + * and do you need to perform the actions (eg - freeing allocated memory ) |
| 14 | + * what the default handler would have performed. |
| 15 | + * There are ways to call default signal handler after our custom handler, |
| 16 | + * but most of them are not pleasant. |
| 17 | + * The general idea of to reset the handler and raise the exception again. |
| 18 | + * Only do this when you know what you are doing. |
| 19 | + * link - |
| 20 | + * https://stackoverflow.com/questions/6015498/executing-default-signal-handler |
| 21 | + * |
| 22 | + * 3. |
| 23 | + * printf and other printing statements must be avoiding inside the handlers. |
| 24 | + * A workaround is to use a global volatile flag and set it inside the handler, |
| 25 | + * then in the main function, check if flag is marked, then use print statements there. |
| 26 | + * links - |
| 27 | + * https://stackoverflow.com/a/16891799 |
| 28 | + * https://stackoverflow.com/a/16891065 |
| 29 | + * https://stackoverflow.com/a/21712525 |
| 30 | + * |
| 31 | +*/ |
| 32 | + |
| 33 | +// general functions |
| 34 | +void InstallSignalHandlers(); |
| 35 | +void installSignalHandler(int signalType, void (*singalHandlerFunction)(int)); |
| 36 | + |
| 37 | +// signal handler functions |
| 38 | +void segmentationFaultHandler(int sig); |
| 39 | +void zombieProcessesHandler(int sig); |
| 40 | + |
| 41 | +/**Wrapper function : Install Signal Handlers |
| 42 | + * |
| 43 | + * @usage |
| 44 | + * Cleanly install required signal handlers |
| 45 | + * |
| 46 | +*/ |
| 47 | +void InstallSignalHandlers() { |
| 48 | + /**Handling segmentation fault |
| 49 | + * |
| 50 | + * In most cases, you should avoid handling this. |
| 51 | + * let the core get dumped, and try debugging from the dump. |
| 52 | + * But in some cases, we need more control, for debugging etc |
| 53 | + * this is useful in those cases. |
| 54 | + */ |
| 55 | + // installSignalHandler(SIGSEGV, segmentationFaultHandler); |
| 56 | + |
| 57 | + // reap off zombie processes |
| 58 | + installSignalHandler(SIGCHLD, zombieProcessesHandler); |
| 59 | +} |
| 60 | + |
| 61 | + |
| 62 | +/**install signal handler |
| 63 | + * |
| 64 | + * @usage |
| 65 | + * install a single signal handler cleanly |
| 66 | + * |
| 67 | + * Note that, |
| 68 | + * sigaction is used instead of signal because, |
| 69 | + * sigaction is portable, predictable and thread safe. |
| 70 | + * |
| 71 | +*/ |
| 72 | +void installSignalHandler(int signalType, void (*singalHandlerFunction)(int)) { |
| 73 | + struct sigaction sa; |
| 74 | + sa.sa_handler = singalHandlerFunction; |
| 75 | + sigemptyset(&sa.sa_mask); |
| 76 | + sa.sa_flags = SA_RESTART; |
| 77 | + if (sigaction(signalType, &sa, NULL) == -1) { |
| 78 | + printError(); |
| 79 | + exit(1); |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +/**Segmentation Fault Handler |
| 84 | + * |
| 85 | + * Whenever the SIGSEGV signal is raised, |
| 86 | + * it is redirected here. |
| 87 | + * |
| 88 | +*/ |
| 89 | +void segmentationFaultHandler(int sig[[gnu::unused]]) { |
| 90 | + // @logging |
| 91 | + // logs("A Segmentation fault occured."); |
| 92 | + // fprintf(stderr, "Error: signal %d:\n", sig); |
| 93 | + |
| 94 | + // @logging : Print stack trace |
| 95 | + |
| 96 | + void* array[10]; |
| 97 | + size_t size; |
| 98 | + |
| 99 | + // get void*'s for all entries on the stack |
| 100 | + size = backtrace(array, 10); |
| 101 | + |
| 102 | + // print out all the frames to stderr |
| 103 | + backtrace_symbols_fd(array, size, STDERR_FILENO); |
| 104 | + |
| 105 | + exit(1); |
| 106 | +} |
| 107 | + |
| 108 | + |
| 109 | +/**Reaper for zombie processes |
| 110 | + * When the parent doesn't collect the child's status, it becomes a zombie. |
| 111 | + * |
| 112 | + * @usage |
| 113 | + * The server main process (the one which always listens), |
| 114 | + * does not collect the status of the forked processes created |
| 115 | + * to handle the incoming clients. |
| 116 | + * |
| 117 | + * When the server runs indefinitely, a lot of clients come and go. |
| 118 | + * A lot of processes are created for these clients. |
| 119 | + * Therefore, a lot of zombies will be lying around if we don't clean them |
| 120 | + * |
| 121 | + * @exact-use-case on client side |
| 122 | + * 1. when the named pipe ( a child process ) in executeShellCommand is done. |
| 123 | + * 2. the job of data connection connection manager process is done. |
| 124 | + * |
| 125 | +*/ |
| 126 | +void zombieProcessesHandler(int sig[[gnu::unused]]) { |
| 127 | + // @logging |
| 128 | + // logs("REAPING OFF ZOMBIE PROCESS"); |
| 129 | + // fprintf(stderr, "Error: signal %d:\n", sig); |
| 130 | + |
| 131 | + /** |
| 132 | + * The usual function of waitpid is to wait for child process to exit. |
| 133 | + * |
| 134 | + * However, WNOHANG in waitpid |
| 135 | + * tells it to return immediately if no child processes have exited yet. |
| 136 | + * It does not go into blocking state. |
| 137 | + * |
| 138 | + * waitpid() might overwrite errno, so we save and restore it |
| 139 | + */ |
| 140 | + int saved_errno = errno; |
| 141 | + while (waitpid(-1, NULL, WNOHANG) > 0) |
| 142 | + ; |
| 143 | + errno = saved_errno; |
| 144 | +} |
0 commit comments