/* -------------------------------------------------------- * PRIVATE: Given a binary filename, it will open a pipe to * `ldd', parse the output, and return a list of libraries */ bool get_libs(strlist *list, const char *prog) { FILE *ldd_in; char *buf, *start, *end; buf = sprintf_static("ldd %s", prog); ldd_in = popen(buf, "r"); while ( (buf = fgets_static(ldd_in)) != NULL) { if (strstr(buf, "needs:")) continue; start = strrchr(buf, '>'); if (!start++) start = buf; while (isspace(*start)) ++start; end = strrchr(buf, '('); if (!end) end = buf + strlen(buf); while (start < end && isspace(*(end - 1))) --end; *end = '\0'; strlist_insert(list, start); } pclose(ldd_in); return true; }
const char *reglist::getReason(char *s) { vector< regrule_t * >::iterator i; if (only_rules.size() > 0) { for (i = only_rules.begin(); i != only_rules.end(); ++i) { int retval = regexec(&((*i)->preg), s, 0, NULL, 0); if (retval == 0) { if (!(*i)->show_reason) return NULL; return sprintf_static("Including '%s' because it matched regular expression '%s'", s, (*i)->pattern); } if (retval == REG_ENOSYS) { dlog(ERR, "Error while executing regular expression in reglist::check(): The function is not supported.\n"); return NULL; } } return false; } if (skip_rules.size() > 0) { for (i = skip_rules.begin(); i != skip_rules.end(); ++i) { int retval = regexec(&((*i)->preg), s, 0, NULL, 0); if (retval == 0 && (*i)->show_reason) { if (!(*i)->show_reason) return NULL; return sprintf_static("Skipping '%s' because it matched regular expression '%s'", s, (*i)->pattern); } if (retval == REG_ENOSYS) { dlog(ERR, "Error while executing regular expression in reglist::check(): The function is not supported.\n"); return NULL; } } } return NULL; }
bool record_search(record_t *newRecord) { FILE *index_fd; char *index_filename; bool found = false, latest = true; index_filename = sprintf_static("%s/%s", config.record_dir, HISTORY_RECORD_INDEX_FILE); errno = 0; index_fd = fopen(index_filename, "r"); if (errno && errno == ENOENT) { index_fd = fopen(index_filename, "w"); if (!index_fd) { fprintf(config.outfd, "*\n* Error opening history record index %s. Disabling history record.\n*\n", index_filename); newRecord->enabled = false; return false; } fclose(index_fd); } else if (!index_fd) { fprintf(config.outfd, "*\n* Error opening history record index %s. Disabling history record.\n*\n", index_filename); newRecord->enabled = false; return false; } else { // We should use C++ string instead to avoid buffer overflows. char *buf; while ( (buf = fgets_static(index_fd))) { if (buf[0] == '\t' || buf[0] == '\n') continue; chomp(buf); strlist prog_line = char2strlist(buf); if (strlist_cmp(&prog_line, &newRecord->prog_line)) { strlist diff_list = STRLIST_INITIALIZER; while (!found && (buf = fgets_static(index_fd)) && buf[0] == '\t') { chomp(buf); strlist lib_line = char2strlist(buf + 1); strncpy(newRecord->filename, strlist_get(&lib_line, 0), sizeof(newRecord->filename)); strlist_pop_front(&lib_line); if (strlist_cmp(&lib_line, &newRecord->lib_line)) { found = true; } else { if (latest) diff_list = libs_diff(&lib_line, &newRecord->lib_line); latest = false; } strlist_clear(&lib_line); } if (!found) { fprintf(config.outfd, "* Warning: Current library set unknown. Differences from latest run include:\n"); fprintf(config.outfd, "*\n"); for (unsigned i = 0; i < diff_list.count; ++i) fprintf(config.outfd, "%s\n", strlist_get(&diff_list, i)); } else if (!latest) fprintf(config.outfd, "* Warning: Current library signature does not match latest run.\n"); strlist_clear(&diff_list); } strlist_clear(&prog_line); } fclose(index_fd); } if (!found) { // Fill storage file information const char *prog_file = strlist_get(&newRecord->prog_line, 0); if (strrchr(prog_file, '/')) prog_file = strrchr(prog_file, '/') + 1; snprintf(newRecord->filename, sizeof(newRecord->filename), "%s/%s-XXXXXX", config.record_dir, prog_file); int file_desc = mkstemp(newRecord->filename); newRecord->fd = fdopen(file_desc, "a"); } else { newRecord->fd = fopen(newRecord->filename, "a"); } char timestr[STRING_MAX]; time_t timestamp = time(NULL); strftime(timestr, sizeof(timestr), "%Y-%m-%d %H:%M:%S %Z", localtime(×tamp)); fprintf(newRecord->fd, "Log start: %s\n", timestr); fprintf(newRecord->fd, "--------------------------------------------------------------------------------\n"); char raw_filename[ PATH_MAX ]; strncpy(raw_filename, newRecord->filename, sizeof(raw_filename)); strncat(raw_filename, ".raw", sizeof(raw_filename) - strlen(raw_filename)); newRecord->raw_fd = fopen(raw_filename, "a"); fprintf(newRecord->raw_fd, "Raw transcript log start: %s\n", timestr); fprintf(newRecord->raw_fd, "--------------------------------------------------------------------------------\n"); return (found && latest); }