Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 44 additions & 6 deletions c_sourcecode/src/utils/prerequisites.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,38 @@ static int has_required_dumps(const config_t *config)
return 0;
}


/**
* True when path contains anything other than "." and "..", matching `ls -A`.
* Returns:
* 1 -> Directory exists and has at least one entry
* 0 -> Missing, empty, or path is NULL
*/
static int dir_is_nonempty(const char *path)
{
DIR *dp;
struct dirent *entry;

if (!path || path[0] == '\0')
return 0;

dp = opendir(path);
if (!dp)
return 0;

while ((entry = readdir(dp)) != NULL)
{
if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0)
{
closedir(dp);
return 1;
}
}

closedir(dp);
return 0;
}

int prerequisites_wait(config_t *config, int timeout_sec)
{
int dump_file_found = 0;
Expand All @@ -160,18 +192,24 @@ int prerequisites_wait(config_t *config, int timeout_sec)
return ERR_INVALID_ARGUMENT;
}
CRASHUPLOAD_INFO("Inside prerequisites_wait: device type=%d\n", config->device_type);

/* runDumpUpload.sh: an empty WORKING_DIR exits before this wait.
* A non-empty coredump directory waits until the generator creates
* /tmp/coredump_mutex_release, then the dump count runs. */
if (config->dump_type == DUMP_TYPE_COREDUMP &&
dir_is_nonempty(config->core_path) &&
filePresentCheck("/tmp/coredump_mutex_release") != 0)
{
CRASHUPLOAD_INFO("Waiting for Coredump Completion\n");
sleep(21);
}

dump_file_found = has_required_dumps(config);
if (1 != dump_file_found)
{
CRASHUPLOAD_INFO("dump file or core file not found. Exiting\n");
return NO_DUMPS_FOUND;
}
defer_upload_if_needed(config->device_type);
// TODO: Below mutex_release file create by core dump generation script.So using same
if ((config->dump_type == DUMP_TYPE_COREDUMP) && (0 != (filePresentCheck("/tmp/coredump_mutex_release"))))
{
CRASHUPLOAD_INFO("Waiting for Coredump Completion\n");
sleep(21); // TODO: How this number arive??
}
return PREREQUISITES_SUCCESS;
}
10 changes: 10 additions & 0 deletions unittest/prerequisites_gtest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,20 @@ class PrerequisitesTest : public ::testing::Test {

// Clean up flag files
unlink("/tmp/set_crash_reboot_flag");
unlink("/tmp/coredump_mutex_release");
}

void TearDown() override {
// Clean up test directories and files
system("rm -rf /tmp/test_prereq_dir /tmp/test_minidumps /tmp/test_cores");
unlink("/tmp/set_crash_reboot_flag");
unlink("/tmp/coredump_mutex_release");
}

void TouchCoredumpMutexRelease() {
FILE* fp = fopen("/tmp/coredump_mutex_release", "w");
if (fp)
fclose(fp);
}

void CreateDumpFile(const char* dir, const char* filename) {
Expand Down Expand Up @@ -262,6 +270,7 @@ TEST_F(PrerequisitesTest, PrerequisitesWait_MediaClient_CoredumpFound_Success) {
test_config.device_type = DEVICE_TYPE_MEDIACLIENT;
test_config.dump_type = DUMP_TYPE_COREDUMP;
CreateDumpFile(test_core_dir, "app_core.12345");
TouchCoredumpMutexRelease();

int ret = prerequisites_wait(&test_config, 30);

Expand Down Expand Up @@ -322,6 +331,7 @@ TEST_F(PrerequisitesTest, Integration_FullWorkflow_MediaClientCoredump) {
test_config.device_type = DEVICE_TYPE_MEDIACLIENT;
test_config.dump_type = DUMP_TYPE_COREDUMP;
CreateDumpFile(test_core_dir, "receiver_core.12345");
TouchCoredumpMutexRelease();

int ret = prerequisites_wait(&test_config, 30);

Expand Down
Loading