|
| 1 | +/* |
| 2 | +* Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries. |
| 3 | +* SPDX-License-Identifier: BSD-3-Clause-Clear |
| 4 | +*/ |
| 5 | +#include <stdio.h> |
| 6 | +#include <stdlib.h> |
| 7 | +#include <unistd.h> |
| 8 | +#include <sys/stat.h> |
| 9 | +#include <string.h> |
| 10 | +#include <time.h> |
| 11 | +#include <sys/reboot.h> |
| 12 | + |
| 13 | +#define HEALTH_DIR "/var/reboot_health" |
| 14 | +#define LOG_FILE "/var/reboot_health/reboot_health.log" |
| 15 | +#define SERIAL_PORT "/dev/ttyS0" |
| 16 | + |
| 17 | +// Function to get the current timestamp |
| 18 | +void get_timestamp(char *buffer, size_t size) { |
| 19 | + time_t now = time(NULL); |
| 20 | + struct tm *t = localtime(&now); |
| 21 | + strftime(buffer, size, "%Y-%m-%d %H:%M:%S", t); |
| 22 | +} |
| 23 | + |
| 24 | +// Function to log messages with a specified level |
| 25 | +void log_message(const char *level, const char *message) { |
| 26 | + char timestamp[64]; |
| 27 | + get_timestamp(timestamp, sizeof(timestamp)); |
| 28 | + |
| 29 | + // Log to file |
| 30 | + FILE *log = fopen(LOG_FILE, "a"); |
| 31 | + if (log) { |
| 32 | + fprintf(log, "[%s] [%s] %s\n", timestamp, level, message); |
| 33 | + fclose(log); |
| 34 | + } |
| 35 | + |
| 36 | + // Log to serial port (for visibility) |
| 37 | + FILE *serial = fopen(SERIAL_PORT, "w"); |
| 38 | + if (serial) { |
| 39 | + fprintf(serial, "[%s] [%s] %s\n", timestamp, level, message); |
| 40 | + fclose(serial); |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +// Helper functions for logging |
| 45 | +void log_info(const char *message) { log_message("INFO", message); } |
| 46 | +void log_pass(const char *message) { log_message("PASS", message); } |
| 47 | +void log_fail(const char *message) { log_message("FAIL", message); } |
| 48 | +void log_error(const char *message) { log_message("ERROR", message); } |
| 49 | + |
| 50 | +// Check if the system has recently rebooted by reading uptime |
| 51 | +void check_system_reboot() { |
| 52 | + FILE *fp; |
| 53 | + char buffer[128]; |
| 54 | + unsigned long uptime = 0; |
| 55 | + |
| 56 | + fp = fopen("/proc/uptime", "r"); |
| 57 | + if (fp == NULL) { |
| 58 | + log_error("Failed to read /proc/uptime."); |
| 59 | + exit(1); |
| 60 | + } |
| 61 | + fscanf(fp, "%s", buffer); |
| 62 | + fclose(fp); |
| 63 | + |
| 64 | + uptime = strtol(buffer, NULL, 10); // Uptime in seconds |
| 65 | + |
| 66 | + if (uptime < 300) { // If uptime is less than 5 minutes, assume the system has rebooted recently |
| 67 | + log_info("System has rebooted recently (uptime < 300 seconds)."); |
| 68 | + } else { |
| 69 | + log_info("System uptime is normal (no recent reboot detected)."); |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +// Check if the system is running a valid shell (PID 1) |
| 74 | +void check_shell_alive() { |
| 75 | + FILE *fp = fopen("/proc/1/comm", "r"); |
| 76 | + if (!fp) { |
| 77 | + log_fail("Cannot open /proc/1/comm. System critical error."); |
| 78 | + exit(1); |
| 79 | + } |
| 80 | + |
| 81 | + char buf[64] = {0}; |
| 82 | + if (!fgets(buf, sizeof(buf), fp)) { |
| 83 | + fclose(fp); |
| 84 | + log_fail("Failed to read PID 1 comm."); |
| 85 | + exit(1); |
| 86 | + } |
| 87 | + fclose(fp); |
| 88 | + |
| 89 | + buf[strcspn(buf, "\n")] = 0; // Remove trailing newline |
| 90 | + |
| 91 | + if (strstr(buf, "init") || strstr(buf, "systemd") || strstr(buf, "busybox")) { |
| 92 | + char msg[128]; |
| 93 | + snprintf(msg, sizeof(msg), "Booted successfully with PID1 -> %s", buf); |
| 94 | + log_pass(msg); // Log PASS |
| 95 | + } else { |
| 96 | + char msg[128]; |
| 97 | + snprintf(msg, sizeof(msg), "Boot failed. Unexpected PID1: %s", buf); |
| 98 | + log_fail(msg); // Log FAIL |
| 99 | + |
| 100 | + log_info("Attempting reboot now..."); |
| 101 | + sync(); |
| 102 | + reboot(RB_AUTOBOOT); |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +// Create the directory if it doesn't exist |
| 107 | +int create_directory_if_not_exists(const char *dir_path) { |
| 108 | + struct stat st = {0}; |
| 109 | + if (stat(dir_path, &st) == -1) { |
| 110 | + if (mkdir(dir_path, 0755) == -1) { |
| 111 | + log_error("Failed to create directory /var/reboot_health."); |
| 112 | + return -1; |
| 113 | + } |
| 114 | + log_info("Created directory /var/reboot_health."); |
| 115 | + } |
| 116 | + return 0; |
| 117 | +} |
| 118 | + |
| 119 | +int main() { |
| 120 | + // Ensure the directory exists before proceeding |
| 121 | + if (create_directory_if_not_exists(HEALTH_DIR) != 0) { |
| 122 | + log_error("Exiting due to failure in creating directory."); |
| 123 | + return 1; // Directory creation failed |
| 124 | + } |
| 125 | + |
| 126 | + check_system_reboot(); // Check if the system has recently rebooted |
| 127 | + check_shell_alive(); // Check if the system shell (PID 1) is alive and healthy |
| 128 | + |
| 129 | + log_info("Reboot health check completed."); |
| 130 | + return 0; |
| 131 | +} |
0 commit comments