char parameters[MAX_PARAMETERS][20];
SwBlock blocks[MAX_SW_BLOCKS];
FILE *log = fopen("log.txt", "a");
fprintf(log, "SW Block Name | Restart cnt | Start Time | Reason\n");
fprintf(log, "========================================================================================\n");
printf("SW Block Name | Restart cnt | Start Time | Reason\n");
printf("========================================================================================\n");
void printLog(SwBlock* block) {
FILE *log = fopen("log.txt", "a");
struct tm* timeInfo = localtime(&block->lastRestartTime);
strftime(timeString, sizeof(timeString), "%Y-%m-%d %H:%M:%S", timeInfo);
fprintf(log, "%s %d %s %s\n", block->name, block->restartCount, timeString, block->reason);
printf("%s %d %s %s\n", block->name, block->restartCount, timeString, block->reason);
void runSwBlock(SwBlock *block) {
void signalHandler(int signum) {
while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
for (int i = 0; i < swBlockCount; i++) {
if (blocks[i].pid == pid) {
block->lastRestartTime = time(NULL);
snprintf(block->reason, sizeof(block->reason), "Exit(%d)", WEXITSTATUS(status));
} else if (WIFSIGNALED(status)) {
int chstatus = WTERMSIG(status);
snprintf(block->reason, sizeof(block->reason), "Signal(%s)", strsignal(chstatus));
snprintf(block->reason, sizeof(block->reason), "Unknown");
printf("자식 프로세스 %d 종료됨.\n", pid);
sa.sa_handler = signalHandler;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART | SA_NOCLDSTOP;
if (sigaction(SIGCHLD, &sa, 0) == -1) {
void trimStr(char *str) {
char *end = str + (strlen(str) - 1);
while (isspace(*start)) start++;
while (isspace(*end)) end--;
memmove(str, start, end - start + 1);
str[end - start + 1] = '\0';
int readSwBlocks(FILE *file) {
while (index < MAX_SW_BLOCKS && fgets(buf, sizeof(buf), file)) {
char* token = strtok(buf, ";");
strcpy(blocks[index].name, token);
strcpy(blocks[index].reason, "Init");
blocks[index].lastRestartTime = time(NULL);
for (int paramIndex = 0; paramIndex < MAX_PARAMETERS; paramIndex++) {
token = strtok(NULL, ";");
strcpy(blocks[index].parameters[paramIndex], token);
FILE *fileList = fopen("swblocks.txt", "r");
swBlockCount = readSwBlocks(fileList);
for (int i = 0; i < swBlockCount; i++) {