diff --git a/common/src/Reboot/setup_systemd.sh b/Runner/suites/Kernel/FunctionalArea/baseport/Reboot_health_check/setup_systemd.sh similarity index 100% rename from common/src/Reboot/setup_systemd.sh rename to Runner/suites/Kernel/FunctionalArea/baseport/Reboot_health_check/setup_systemd.sh diff --git a/common/src/Reboot/Simple_reboot.c b/common/src/Reboot/Simple_reboot.c deleted file mode 100755 index ad1ebeab..00000000 --- a/common/src/Reboot/Simple_reboot.c +++ /dev/null @@ -1,127 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include - -#define HEALTH_DIR "/var/reboot_health" -#define LOG_FILE "/var/reboot_health/reboot_health.log" -#define SERIAL_PORT "/dev/ttyS0" - -// Function to get the current timestamp -void get_timestamp(char *buffer, size_t size) { - time_t now = time(NULL); - struct tm *t = localtime(&now); - strftime(buffer, size, "%Y-%m-%d %H:%M:%S", t); -} - -// Function to log messages with a specified level -void log_message(const char *level, const char *message) { - char timestamp[64]; - get_timestamp(timestamp, sizeof(timestamp)); - - // Log to file - FILE *log = fopen(LOG_FILE, "a"); - if (log) { - fprintf(log, "[%s] [%s] %s\n", timestamp, level, message); - fclose(log); - } - - // Log to serial port (for visibility) - FILE *serial = fopen(SERIAL_PORT, "w"); - if (serial) { - fprintf(serial, "[%s] [%s] %s\n", timestamp, level, message); - fclose(serial); - } -} - -// Helper functions for logging -void log_info(const char *message) { log_message("INFO", message); } -void log_pass(const char *message) { log_message("PASS", message); } -void log_fail(const char *message) { log_message("FAIL", message); } -void log_error(const char *message) { log_message("ERROR", message); } - -// Check if the system has recently rebooted by reading uptime -void check_system_reboot() { - FILE *fp; - char buffer[128]; - unsigned long uptime = 0; - - fp = fopen("/proc/uptime", "r"); - if (fp == NULL) { - log_error("Failed to read /proc/uptime."); - exit(1); - } - fscanf(fp, "%s", buffer); - fclose(fp); - - uptime = strtol(buffer, NULL, 10); // Uptime in seconds - - if (uptime < 300) { // If uptime is less than 5 minutes, assume the system has rebooted recently - log_info("System has rebooted recently (uptime < 300 seconds)."); - } else { - log_info("System uptime is normal (no recent reboot detected)."); - } -} - -// Check if the system is running a valid shell (PID 1) -void check_shell_alive() { - FILE *fp = fopen("/proc/1/comm", "r"); - if (!fp) { - log_fail("Cannot open /proc/1/comm. System critical error."); - exit(1); - } - - char buf[64] = {0}; - if (!fgets(buf, sizeof(buf), fp)) { - fclose(fp); - log_fail("Failed to read PID 1 comm."); - exit(1); - } - fclose(fp); - - buf[strcspn(buf, "\n")] = 0; // Remove trailing newline - - if (strstr(buf, "init") || strstr(buf, "systemd") || strstr(buf, "busybox")) { - char msg[128]; - snprintf(msg, sizeof(msg), "Booted successfully with PID1 -> %s", buf); - log_pass(msg); // Log PASS - } else { - char msg[128]; - snprintf(msg, sizeof(msg), "Boot failed. Unexpected PID1: %s", buf); - log_fail(msg); // Log FAIL - - log_info("Attempting reboot now..."); - sync(); - reboot(RB_AUTOBOOT); - } -} - -// Create the directory if it doesn't exist -int create_directory_if_not_exists(const char *dir_path) { - struct stat st = {0}; - if (stat(dir_path, &st) == -1) { - if (mkdir(dir_path, 0755) == -1) { - log_error("Failed to create directory /var/reboot_health."); - return -1; - } - log_info("Created directory /var/reboot_health."); - } - return 0; -} - -int main() { - // Ensure the directory exists before proceeding - if (create_directory_if_not_exists(HEALTH_DIR) != 0) { - log_error("Exiting due to failure in creating directory."); - return 1; // Directory creation failed - } - - check_system_reboot(); // Check if the system has recently rebooted - check_shell_alive(); // Check if the system shell (PID 1) is alive and healthy - - log_info("Reboot health check completed."); - return 0; -} diff --git a/common/src/Reboot/reboot_health_check.c b/common/src/Reboot/reboot_health_check.c deleted file mode 100755 index a264c438..00000000 --- a/common/src/Reboot/reboot_health_check.c +++ /dev/null @@ -1,124 +0,0 @@ -#include -#include -#include -#include -#include -#include - -#define LOG_DIR "/var/reboot_health" -#define LOG_FILE "/var/reboot_health/reboot_health.log" -#define RESULT_FILE "/var/reboot_health/test_result.txt" // Separate result file for CI/CD - -// Function to check if the system rebooted (log file exists) -int check_system_rebooted() { - FILE *file = fopen(LOG_FILE, "r"); - if (file == NULL) { - return 0; // No log file means no reboot has occurred - } - fclose(file); - return 1; // Log file exists, system has rebooted -} - -// Function to log the result of the health check -void log_health_check(int status) { - FILE *log_file = fopen(LOG_FILE, "a"); - if (log_file == NULL) { - perror("Failed to open log file"); - exit(1); - } - - time_t now = time(NULL); - char *time_str = ctime(&now); - time_str[strlen(time_str) - 1] = '\0'; // Remove the newline character from ctime's output - - if (status) { - fprintf(log_file, "[%s] PASS: System rebooted successfully.\n", time_str); - } else { - fprintf(log_file, "[%s] FAIL: System did not reboot successfully.\n", time_str); - } - - fclose(log_file); -} - -// Function to write a result (PASS/FAIL) for CI/CD into a separate file -void write_result_for_cicd(int status) { - FILE *result_file = fopen(RESULT_FILE, "a"); - if (result_file == NULL) { - perror("Failed to open result file"); - exit(1); - } - - time_t now = time(NULL); - char *time_str = ctime(&now); - time_str[strlen(time_str) - 1] = '\0'; // Remove the newline character - - if (status) { - fprintf(result_file, "[%s] PASS\n", time_str); - } else { - fprintf(result_file, "[%s] FAIL\n", time_str); - } - - fclose(result_file); -} - -// Function to ensure the log directory exists -void create_log_directory() { - if (access(LOG_DIR, F_OK) == -1) { - if (mkdir(LOG_DIR, 0755) != 0) { - perror("Failed to create log directory"); - exit(1); - } - } -} - -// Function to check if the log and result files exist, create them if not -void ensure_files_exist() { - // Create log file if not exists - FILE *log_file = fopen(LOG_FILE, "a"); - if (log_file == NULL) { - perror("Failed to open log file"); - exit(1); - } - fclose(log_file); // Close after checking/creating the file - - // Create result file if not exists - FILE *result_file = fopen(RESULT_FILE, "w"); - if (result_file == NULL) { - perror("Failed to open result file"); - exit(1); - } - fclose(result_file); // Close after checking/creating the file -} - -// Watchdog timer simulation -void watchdog_timer() { - int counter = 0; - while (1) { - sleep(5); // Check every 5 seconds - - counter++; - - if (check_system_rebooted()) { - log_health_check(1); // Log success if system rebooted - write_result_for_cicd(1); // Write pass to CI/CD result file - break; // Exit the loop after success - } - - if (counter >= 12) { // Timeout after 1 minute - log_health_check(0); // Log failure if no reboot detected - write_result_for_cicd(0); // Write fail to CI/CD result file - break; // Exit the loop after failure - } - } -} - -int main() { - // Ensure the necessary directories and files exist - create_log_directory(); - ensure_files_exist(); - - // Start the watchdog timer - watchdog_timer(); - - return 0; -}