From dc4f4e67e4f7dc4d84416f3b5283cdb4491cbd53 Mon Sep 17 00:00:00 2001 From: avjarami Date: Tue, 6 Mar 2018 21:10:59 -0800 Subject: [PATCH] Addressing review Addressing comments from first code review and fixing travis-ci check_journal error in prune test. Signed-off-by: avjarami --- scripts/telemctl | 8 +- src/common.h | 2 +- src/journal/cli.c | 81 +++--- src/journal/journal.c | 458 ++++++++++++++++++---------------- src/journal/journal.h | 8 +- src/main.c | 7 +- src/probes/telem_record_gen.c | 2 +- src/telemdaemon.c | 54 +--- src/telemdaemon.h | 2 +- src/telemetry.c | 87 +++---- src/util.c | 64 ++++- src/util.h | 3 + tests/check_journal.c | 142 ++++++----- tests/check_libtelemetry.c | 22 +- 14 files changed, 494 insertions(+), 446 deletions(-) diff --git a/scripts/telemctl b/scripts/telemctl index 536e26b..560742d 100755 --- a/scripts/telemctl +++ b/scripts/telemctl @@ -92,7 +92,7 @@ telem_restart() { } telem_journal_cli() { - telem_journal $@ + telem_journal "$@" } usage() { @@ -112,10 +112,8 @@ usage() { exit 2 } -if [ $# -ne 1 ]; then - if [ "$1" != "journal" ]; then +if [ "$1" != "journal" ] && [ $# -ne 1 ]; then usage - fi fi if [ $EUID -ne 0 ]; then @@ -138,7 +136,7 @@ case $SUBCOMMAND in is-active) telem_is_active ;; journal) - telem_journal_cli ;; + telem_journal_cli "$@" ;; *) notice "Unknown command passed to $SCRIPT" usage ;; diff --git a/src/common.h b/src/common.h index e2f7a56..773130e 100644 --- a/src/common.h +++ b/src/common.h @@ -63,7 +63,7 @@ /* Journal */ #define JOURNAL_PATH "/var/log/telemetry/journal" -#define JOURNAL_TMP "/var/log/telemetry/.journal" +#define JOURNAL_TMPDIR "/var/log/telemetry/" /* For internal library usage. Bump the version whenever we change the record * structure (e.g. adding or removing a header field). Note that the value diff --git a/src/journal/cli.c b/src/journal/cli.c index d4d9461..f7d691f 100644 --- a/src/journal/cli.c +++ b/src/journal/cli.c @@ -1,4 +1,4 @@ -/** +/* * This program is part of the Clear Linux Project * * Copyright 2018 Intel Corporation @@ -12,8 +12,7 @@ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more * details. - * - * */ + */ #define _GNU_SOURCE @@ -22,6 +21,7 @@ #include #include +#include "common.h" #include "journal.h" void print_usage(void) @@ -35,10 +35,10 @@ void print_usage(void) printf(" -h, --help Display this help message\n"); } +int main(int argc, char **argv) +{ -int main(int argc, char **argv){ - - int rc = 0; + int rc = EXIT_SUCCESS; int count = 0; int verbose_output = 0; char *boot_id = NULL; @@ -51,46 +51,46 @@ int main(int argc, char **argv){ int c; int opt_index = 0; struct option opts[] = { - { "record_id", 1, NULL, 'r' }, - { "event_id", 1, NULL, 'e' }, - { "classification", 1, NULL, 'c' }, - { "boot_id", 1, NULL, 'b' }, - { "verbose", 0, NULL, 'V' }, - { "help", 0, NULL, 'h' }, - { NULL, 0, NULL, 0} + { "record_id", 1, NULL, 'r' }, + { "event_id", 1, NULL, 'e' }, + { "classification", 1, NULL, 'c' }, + { "boot_id", 1, NULL, 'b' }, + { "verbose", 0, NULL, 'V' }, + { "help", 0, NULL, 'h' }, + { NULL, 0, NULL, 0 } }; - while((c = getopt_long(argc, argv, "r:e:c:Vh", opts, &opt_index)) != -1) { - switch(c) { - case 'r': - record_id = strdup((char*)optarg); - printf("record_id: %s\n", record_id); - break; - case 'e': - event_id = strdup((char*)optarg); - printf("event_id: %s\n", event_id); - break; - case 'c': - classification = strdup((char*)optarg); - printf("classification: %s\n", classification); - break; - case 'b': - boot_id = strdup((char*)optarg); - printf("boot_id: %s\n", boot_id); - break; - case 'V': - verbose_output = 1; - break; - case 'h': - print_usage(); - exit(EXIT_SUCCESS); - } + while ((c = getopt_long(argc, argv, "r:e:c:b:Vh", opts, &opt_index)) != -1) { + switch (c) { + case 'r': + record_id = optarg; + break; + case 'e': + event_id = optarg; + break; + case 'c': + classification = optarg; + break; + case 'b': + boot_id = optarg; + break; + case 'V': + verbose_output = 1; + break; + case 'h': + print_usage(); + exit(EXIT_SUCCESS); + case '?': + /** default */ + print_usage(); + exit(EXIT_FAILURE); + } } - if ((telem_journal = open_journal(NULL))){ + if ((telem_journal = open_journal(JOURNAL_PATH))) { if (verbose_output) { fprintf(stdout, "%-30s %-27s %-32s %-32s %-36s\n", "Classification", "Time stamp", - "Record ID", "Event ID", "Boot ID"); + "Record ID", "Event ID", "Boot ID"); } count = print_journal(telem_journal, classification, record_id, event_id, boot_id); if (verbose_output) { @@ -99,6 +99,7 @@ int main(int argc, char **argv){ close_journal(telem_journal); } else { fprintf(stderr, "Unable to open journal\n"); + rc = EXIT_FAILURE; } free(classification); diff --git a/src/journal/journal.c b/src/journal/journal.c index 242a6e5..fc879b0 100644 --- a/src/journal/journal.c +++ b/src/journal/journal.c @@ -17,7 +17,7 @@ #define _GNU_SOURCE #define ID_LEN 32 -#define BOOTID_LEN 36 +#define BOOTID_LEN 37 // Includes the \n character at the end #define BOOTID_FILE "/proc/sys/kernel/random/boot_id" #include @@ -29,13 +29,13 @@ #include #include #include +#include #include "util.h" #include "common.h" #include "journal.h" - -/** +/* * Packs journal entry values into one line string separated * by '\036' RS. * @@ -50,7 +50,11 @@ static int serialize_journal_entry(struct JournalEntry *entry, char **buff) { int rc = 0; - if (asprintf(buff, "%s\036%lld\036%s\036%s\036%s", entry->record_id, (long long int) entry->timestamp, + if (entry == NULL) { + return -1; + } + + if (asprintf(buff, "%s\036%lld\036%s\036%s\036%s", entry->record_id, (long long int)entry->timestamp, entry->classification, entry->event_id, entry->boot_id) < 0) { #ifdef DEBUG fprintf(stderr, "Error: unable to serialize data in buff\n"); @@ -77,23 +81,6 @@ static void free_journal_entry(struct JournalEntry *entry) } } -/** - * Sets last character of a string to '\0' if the character - * is '\n'. - * - * @param l a pointer to string. - * - */ -static void strip_new_line(char *l) -{ - size_t len; - - len = strlen(l); - if (*(l+len-1) == '\n') { - *(l+len-1) = '\0'; - } -} - /** * Unpacks a record that was stored in file and initializes a * JournalEntry struct with parsed values. @@ -108,39 +95,53 @@ static int deserialize_journal_entry(char *line, struct JournalEntry **entry) { int rc = -1; size_t len = 0; + size_t llen = 0; size_t offset = 0; struct JournalEntry *e = NULL; + if (line == NULL) { + return -1; + } + e = malloc(sizeof(struct JournalEntry)); if (!e) { return -1; } + llen = strlen(line); + for (int i = 0; i < 5; i++) { char *out = NULL; - len = strcspn(line+offset, "\036"); - out = strndup((line+offset), len); + if (offset > llen) { + rc = -1; + break; + } + len = strcspn(line + offset, "\036\n"); + out = strndup((line + offset), len); offset += len + 1; if (out) { switch (i) { case 0: - e->record_id = out; - break; + e->record_id = out; + break; case 1: - e->timestamp = atoll(out); - free(out); - break; + e->timestamp = atoll(out); + free(out); + break; case 2: - e->classification = out; - break; + e->classification = out; + break; case 3: - e->event_id = out; - break; + e->event_id = out; + break; case 4: - strip_new_line(out); - e->boot_id = out; - rc = 0; - break; + e->boot_id = out; + rc = 0; + break; + default: + assert(i < 0 && i > 4); + free(out); + rc = 1; } } else { rc = -1; @@ -175,7 +176,7 @@ static int get_record_count(struct TelemJournal *telem_journal) rewind(telem_journal->fptr); // read until the end line by line - while((read = getline(&line, &len, telem_journal->fptr)) != -1) { + while ((read = getline(&line, &len, telem_journal->fptr)) != -1) { count++; } free(line); @@ -196,6 +197,10 @@ static int save_entry(FILE *fptr, struct JournalEntry *entry) int rc = -1; char *serialized_data = NULL; + if (fptr == NULL || entry == NULL) { + return rc; + } + if ((rc = serialize_journal_entry(entry, &serialized_data)) == 0) { #ifdef DEBUG printf("Saving: %s\n", serialized_data); @@ -211,28 +216,24 @@ static int save_entry(FILE *fptr, struct JournalEntry *entry) /** * Reads the boot unique identifier from BOOTID_FILE * - * @param buff pointer to be initialized and set with value - * read from boot_id file. + * @param buff pointer to a BOOTID_LEN allocated * - * @return 0 on sucess, -1 on failure + * @return 0 on success, -1 on failure */ -static int read_boot_id(char **buff) +static int read_boot_id(char buff[]) { int rc = -1; FILE *fs = NULL; - *buff = (char*)malloc((size_t)BOOTID_LEN + 1); - if (!buff) { - return rc; - } - fs = fopen(BOOTID_FILE, "r"); if (!fs) { +#ifdef DEBUG + fprintf(stderr, "Unable to open %s for reading: %d\n", BOOTID_FILE, errno); +#endif return rc; } - if (fgets(*buff, BOOTID_LEN + 1, fs)){ - *(char *)(*buff+BOOTID_LEN)='\0'; + if (fgets(buff, BOOTID_LEN, fs)) { rc = 0; } fclose(fs); @@ -246,8 +247,10 @@ static int read_boot_id(char **buff) * @param n Number of lines to skip. * @param fptr A file pointer. * - * @returns 0 on sucess, 1 if end was reached before - * line n + * @returns 0 on success, on failure -1 if n > existing + * lines in file or errno if failure happens + * during file operation. + * */ static int skip_n_lines(int n, FILE *fptr) { @@ -255,10 +258,21 @@ static int skip_n_lines(int n, FILE *fptr) size_t len = 0; char *line = NULL; - fseek(fptr, 0, 0); - for(int i = 0; i < n; i++) { + if (fptr == NULL) { +#ifdef DEBUG + fprintf(stderr, "fptr argument to skip_n_lines is invalid\n"); +#endif + // File descriptor in bad state + return EBADFD; + } + + if (fseek(fptr, 0, 0) != 0) { + return errno; + } + + for (int i = 0; i < n; i++) { if (getline(&line, &len, fptr) == -1) { - rc = 1; + rc = -1; break; } } @@ -272,35 +286,48 @@ static int skip_n_lines(int n, FILE *fptr) * * @param fptr A file pointer from source file. * - * @returns 0 on success, 1 on failure + * @returns 0 on success, errno on failure */ -static int copy_to_tmp(FILE *fptr) +static int copy_to_tmp(FILE *fptr, char *tmp_path) { + int rc = 0; size_t len; char *line = NULL; FILE *fptr_tmp = NULL; - fptr_tmp = fopen(JOURNAL_TMP, "w"); + if (fptr == NULL) { + return -1; + } + + fptr_tmp = fopen(tmp_path, "w"); if (!fptr_tmp) { - return 1; + return errno; } // Copy lines to new file while (getline(&line, &len, fptr) > -1) { - fprintf(fptr_tmp, "%s", line); + if (fprintf(fptr_tmp, "%s", line) < 0) { + rc = errno; + break; + } } - fclose(fptr_tmp); + if (fclose(fptr_tmp) != 0) { + rc = errno; +#ifdef DEBUG + perror("Error: "); +#endif + } free(line); - return 0; + return rc; } /* Exported function */ TelemJournal *open_journal(const char *journal_file) { FILE *fptr = NULL; - char *boot_id = NULL; + char boot_id[BOOTID_LEN] = { '\0' }; struct TelemJournal *telem_journal; // Use default location if journal_file parameter is NULL @@ -309,24 +336,29 @@ TelemJournal *open_journal(const char *journal_file) if (fptr == NULL) { #ifdef DEBUG - printf("Unable to open journal on disk\n"); + perror("Error while opening journal file: "); #endif return NULL; } - if (read_boot_id(&boot_id) != 0) { + if (read_boot_id(boot_id) != 0) { #ifdef DEBUG - printf("Failure reading boot_id\n"); + perror("Error while reading boot_id: "); #endif return NULL; } telem_journal = malloc(sizeof(struct TelemJournal)); + if (!telem_journal) { + fprintf(stderr, "Unable to allocate more memory\n"); + return NULL; + } telem_journal->fptr = fptr; telem_journal->journal_file = \ (journal_file == NULL) ? strdup(JOURNAL_PATH) : strdup(journal_file); - telem_journal->boot_id = boot_id; + /* boot_id includes \n at the end, strip RC during duplication */ + telem_journal->boot_id = strndup(boot_id, BOOTID_LEN - 1); telem_journal->record_count = get_record_count(telem_journal); telem_journal->record_count_limit = RECORD_LIMIT; @@ -340,7 +372,7 @@ TelemJournal *open_journal(const char *journal_file) /* Exported function */ void close_journal(TelemJournal *telem_journal) { - if (telem_journal) { + if (telem_journal) { free(telem_journal->boot_id); free(telem_journal->journal_file); fclose(telem_journal->fptr); @@ -348,13 +380,27 @@ void close_journal(TelemJournal *telem_journal) } } +/** + * Checks if class is a prefix i.e. t/\* or t/t/\* + * + * @param class A pointer to string with classification + * value to check. + * @return 1 if class is a prefix, 0 otherwise + */ +static int is_class_prefix(char *class) +{ + size_t class_len = strlen(class); + return (strcmp((char *)(class + class_len - 2), "/*") == 0) ? 1 : 0; +} + /* Exported function */ int print_journal(TelemJournal *telem_journal, char *classification, - char *record_id, char *event_id, char *boot_id) + char *record_id, char *event_id, char *boot_id) { int n = 0; + int rc = 0; int count = 0; - char str_time[80] = {0}; + char str_time[80] = { '\0' }; char *line = NULL; size_t len = 0; FILE *journal_fileptr = NULL; @@ -373,24 +419,37 @@ int print_journal(TelemJournal *telem_journal, char *classification, // Advance file pointer to line n n = telem_journal->record_count - telem_journal->record_count_limit; if (n > 0) { - skip_n_lines(n, journal_fileptr); + rc = skip_n_lines(n, journal_fileptr); + if (rc == -1) { + return rc; + } else if (rc != 0) { + fprintf(stderr, "An error occurred while advancing journal file: %s\n", strerror(rc)); + } } while (getline(&line, &len, journal_fileptr) != -1) { deserialize_journal_entry(line, &entry); if (entry) { /* filter entry out if one is provided */ - if (record_id != NULL && strncmp(entry->record_id, record_id, ID_LEN) != 0) { + if (record_id != NULL && strcmp(entry->record_id, record_id) != 0) { continue; } - if (classification != NULL && strncmp(entry->classification, classification, strlen(entry->classification)) != 0) { + if (boot_id != NULL && strcmp(entry->boot_id, boot_id) != 0) { continue; } - if (boot_id != NULL && strncmp(entry->boot_id, boot_id, BOOTID_LEN) != 0) { + if (event_id != NULL && strcmp(entry->event_id, event_id) != 0) { continue; } - if (event_id != NULL && strncmp(entry->event_id, event_id, strlen(entry->event_id)) != 0) { - continue; + // In the case of class checking prefixes is an option + if (classification != NULL) { + // Check prefixes when classification ends in /*, otherwise use strcomp + if (is_class_prefix(classification)) { + if (strncmp(entry->classification, classification, strlen(classification) - 1) != 0) { + continue; + } + } else if (strcmp(entry->classification, classification) != 0) { + continue; + } } /* end filters section */ ts = *localtime(&entry->timestamp); @@ -410,53 +469,8 @@ int print_journal(TelemJournal *telem_journal, char *classification, } /** - * Validate classification - * TODO: move to util or common and use it in telemetry.c - * - * @param classification A pointer with the value to validate. - * - * @returns 0 on success, 1 on failure - */ -static int validate_classification(char *classification) -{ - size_t i, j; - int slashes = 0; - int x = 0; - - if (classification == NULL) { - return 1; - } - - j = strlen(classification); - - if (j > MAX_CLASS_LENGTH) { - return 1; - } - - for (i = 0, x = 0; i <= (j - 1); i++, x++) { - if (classification[i] == '/') { - slashes++; - x = 0; - } else { - if (x > MAX_SUBCAT_LENGTH) { - return 1; - } - } - } - - if (slashes != 2) { -#ifdef DEBUG - fprintf(stderr, "ERR: Classification string should have two /s.\n"); -#endif - return 1; - } - - return 0; -} - - -/** - * Validation for event id. + * Validation for event id. Thic check makes sure + * that an event_id is a 32 hexadecimal chars long. * * @param event_id A pointer to id value. * @@ -485,128 +499,150 @@ static int validate_event_id(char *event_id) int new_journal_entry(TelemJournal *telem_journal, char *classification, time_t timestamp, char *event_id) { - int rc = 1; - char *boot_id = NULL; - char *record_id = NULL; - struct JournalEntry *entry = NULL; + int rc = 1; + char boot_id[BOOTID_LEN] = { '\0' }; + char *record_id = NULL; + struct JournalEntry *entry = NULL; - if (telem_journal == NULL) { + if (telem_journal == NULL) { #ifdef DEBUG - fprintf(stderr, "Error: telem_journal was not initialized\n"); + fprintf(stderr, "Error: telem_journal was not initialized\n"); #endif - return rc; - } + return rc; + } - if (validate_classification(classification) != 0) { - return rc; - } + if (validate_classification(classification) != 0) { + return rc; + } - if (validate_event_id(event_id) != 0) { - return rc; - } + if (validate_event_id(event_id) != 0) { + return rc; + } - entry = malloc(sizeof(struct JournalEntry)); - entry->classification = NULL; - entry->record_id = NULL; - entry->event_id = NULL; - entry->boot_id = NULL; + entry = malloc(sizeof(struct JournalEntry)); + if (!entry) { + fprintf(stderr, "Error: unable to allocate more memory\n"); + return rc; + } + entry->classification = NULL; + entry->record_id = NULL; + entry->event_id = NULL; + entry->boot_id = NULL; - if (get_random_id(&record_id) != 0) { + if (get_random_id(&record_id) != 0) { #ifdef DEBUG - fprintf(stderr, "Error: unable to generate random id\n"); + fprintf(stderr, "Error: unable to generate random id\n"); #endif - goto quit; - } + goto quit; + } - if (read_boot_id(&boot_id) != 0) { + if (read_boot_id(boot_id) != 0) { #ifdef DEBUG - fprintf(stderr, "Error: unable to read boot_id\n"); + fprintf(stderr, "Error: unable to read boot_id\n"); #endif - goto quit; - } + goto quit; + } - if ((entry->classification = strdup(classification)) == NULL) { - goto quit; - } - if ((entry->event_id = strdup(event_id)) == NULL) { - goto quit; - } + if ((entry->classification = strdup(classification)) == NULL) { + goto quit; + } + if ((entry->event_id = strdup(event_id)) == NULL) { + goto quit; + } - entry->record_id = record_id; - entry->boot_id = boot_id; - entry->timestamp = timestamp; + entry->record_id = record_id; + /* boot_id includes \n at the end, strip RC during duplication */ + entry->boot_id = strndup(boot_id, BOOTID_LEN - 1); + entry->timestamp = timestamp; - if ((rc = save_entry(telem_journal->fptr, entry)) == 0) { - telem_journal->record_count = telem_journal->record_count + 1; + if ((rc = save_entry(telem_journal->fptr, entry)) == 0) { + telem_journal->record_count = telem_journal->record_count + 1; #ifdef DEBUG - fprintf(stdout, "%d records in journal\n", telem_journal->record_count); + fprintf(stdout, "%d records in journal\n", telem_journal->record_count); #endif - } + } quit: - free_journal_entry(entry); + free_journal_entry(entry); - return rc; + return rc; } /* Exported function */ -int prune_journal(struct TelemJournal *telem_journal) +int prune_journal(struct TelemJournal *telem_journal, char *tmp_dir) { - int rc = 1; - int count = 0; - int deviation = DEVIATION; + int rc = 1; + int count = 0; + int deviation = DEVIATION; + char *tmp_file_path = NULL; - if ( telem_journal->record_count > (deviation + telem_journal->record_count_limit)) { - count = telem_journal->record_count - telem_journal->record_count_limit; + if (telem_journal == NULL) { + return rc; + } - // jump to line# count - if ( skip_n_lines(count, telem_journal->fptr) != 0) { + if (telem_journal->record_count > (deviation + telem_journal->record_count_limit)) { + count = telem_journal->record_count - telem_journal->record_count_limit; + + // jump to line# count + if ((rc = skip_n_lines(count, telem_journal->fptr)) != 0) { #ifdef DEBUG - fprintf(stderr, "Error: skipping %d journal lines\n", count); + fprintf(stderr, "Error: skipping %d journal lines\n", count); +#endif + // if rc == -1 (n > #lines in file, change to errno style) + return (rc == -1) ? EADDRNOTAVAIL : rc; + } + // Use default if not tmp_dir is specified + if (tmp_dir == NULL) { + if ((asprintf(&tmp_file_path, "%s/.journal", JOURNAL_TMPDIR) == -1)) { + return ENOMEM; + } + } else { + if ((asprintf(&tmp_file_path, "%s/.journal", tmp_dir) == -1)) { + return ENOMEM; + } + } + // create new file with rest of file + if ((rc = copy_to_tmp(telem_journal->fptr, tmp_file_path)) != 0) { +#ifdef DEBUG + fprintf(stderr, "Error: copying partial journal to temp journal file\n"); +#endif + goto quit; + } + // close file handler + if ((rc = fclose(telem_journal->fptr)) != 0) { +#ifdef DEBUG + fprintf(stderr, "Error: closing journal file handler\n"); +#endif + goto quit; + } + // overwrite file + if ((rc = rename(tmp_file_path, telem_journal->journal_file)) != 0) { +#ifdef DEBUG + fprintf(stderr, "Error: while overwriting journal file\n"); +#endif + goto quit; + } + // reopen file handler + telem_journal->fptr = fopen(telem_journal->journal_file, "a+"); + if (!telem_journal->fptr) { +#ifdef DEBUG + fprintf(stderr, "Error: re-opening journal file\n"); +#endif + return rc; + } + // update record count + telem_journal->record_count = telem_journal->record_count - count; +#ifdef DEBUG + fprintf(stdout, "record_count: %d\n", telem_journal->record_count); #endif - return rc; } - // create new file with rest of file - if ( copy_to_tmp(telem_journal->fptr) != 0) { -#ifdef DEBUG - fprintf(stderr, "Error: copying partial journal to temp journal file\n"); -#endif - return rc; - } - // close file handler - if ( fclose(telem_journal->fptr) != 0) { -#ifdef DEBUG - fprintf(stderr, "Error: clossing journal file handler\n"); -#endif - return rc; - } - // overwrite file - if ( rename(JOURNAL_TMP, JOURNAL_PATH) != 0) { -#ifdef DEBUG - fprintf(stderr, "Error: while overriding journal file\n"); -#endif - return rc; - } - // reopen file handler - telem_journal->fptr = fopen(JOURNAL_PATH, "a+"); - if ( !telem_journal->fptr ) { -#ifdef DEBUG - fprintf(stderr, "Error: re-opening journal file\n"); -#endif - return rc; - } - // update record count - telem_journal->record_count = (count >= telem_journal->record_count_limit) ? - telem_journal->record_count_limit : - telem_journal->record_count - count; -#ifdef DEBUG - fprintf(stdout, "record_count: %d\n", telem_journal->record_count); -#endif rc = 0; - } - return rc; +quit: + free(tmp_file_path); + + return rc; } /* vi: set ts=8 sw=8 sts=4 et tw=80 cino=(0: */ diff --git a/src/journal/journal.h b/src/journal/journal.h index fe515ec..e7741ec 100644 --- a/src/journal/journal.h +++ b/src/journal/journal.h @@ -102,11 +102,11 @@ int new_journal_entry(TelemJournal *telem_journal, char *classification, * * @param telem_journal A pointer to telemetry journal struct * returned by open_journal call. + * @param tmp_dir A pointer to a string with the path to use for + * temp file during pruning process. * - * @return 0 on success, 1 on failure + * @return 0 on success, errno on failure */ -int prune_journal(TelemJournal *telem_journal); - - +int prune_journal(TelemJournal *telem_journal, char *tmp_dir); /* vi: set ts=8 sw=8 sts=4 et tw=80 cino=(0: */ diff --git a/src/main.c b/src/main.c index fee6918..e19cbfc 100644 --- a/src/main.c +++ b/src/main.c @@ -252,7 +252,10 @@ int main(int argc, char **argv) while (1) { malloc_trim(0); if (initial_nfds == daemon.nfds) { - prune_journal(daemon.record_journal); + ret = prune_journal(daemon.record_journal, JOURNAL_TMPDIR); + if (ret != 0) { + telem_log(LOG_WARNING, "Unable to prune journal\n"); + } } ret = poll(daemon.pollfds, daemon.nfds, spool_process_time * 1000); if (ret == -1) { @@ -369,6 +372,8 @@ int main(int argc, char **argv) } clean_exit: + close_journal(daemon.record_journal); + /* Free memory before exiting */ while ((cl = LIST_FIRST(&(daemon.client_head))) != NULL) { remove_client(&(daemon.client_head), cl); diff --git a/src/probes/telem_record_gen.c b/src/probes/telem_record_gen.c index 8b1aef7..eda9d01 100644 --- a/src/probes/telem_record_gen.c +++ b/src/probes/telem_record_gen.c @@ -204,7 +204,7 @@ int validate_opts(void) fprintf(stderr, "Error: event_id length %zu it should have %d characters\n", result, EVENT_ID_LEN); return ret; } - if((result = strspn(opt_event_id, alphab)) != EVENT_ID_LEN) { + if ((result = strspn(opt_event_id, alphab)) != EVENT_ID_LEN) { fprintf(stderr, "Error: event_id contains invalid character '%c' at position %zu, valid characters are %s\n", opt_event_id[result], result, alphab); return ret; diff --git a/src/telemdaemon.c b/src/telemdaemon.c index b478b04..5e48bd5 100644 --- a/src/telemdaemon.c +++ b/src/telemdaemon.c @@ -44,7 +44,7 @@ void initialize_daemon(TelemDaemon *daemon) daemon->client_head = head; daemon->bypass_http_post_ts = 0; daemon->is_spool_valid = false; - daemon->record_journal = open_journal(NULL); + daemon->record_journal = open_journal(JOURNAL_PATH); initialize_rate_limit(daemon); daemon->current_spool_size = 0; } @@ -319,7 +319,9 @@ void process_record(TelemDaemon *daemon, client *cl) /* Save record in journal */ if (get_header_value(headers[TM_CLASSIFICATION], &classification_value) && get_header_value(headers[TM_EVENT_ID], &event_id_value)) { - new_journal_entry(daemon->record_journal, classification_value, temp, event_id_value); + if (new_journal_entry(daemon->record_journal, classification_value, temp, event_id_value) != 0) { + telem_log(LOG_INFO, "new_journal_entry in process_record: failed saving record entry\n"); + } } free(classification_value); free(event_id_value); @@ -692,7 +694,7 @@ bool get_machine_id(char *machine_id) return true; } -int machine_id_write(uint64_t upper, uint64_t lower) +int machine_id_write(char *new_id) { FILE *fp; int ret = 0; @@ -704,7 +706,7 @@ int machine_id_write(uint64_t upper, uint64_t lower) goto file_error; } - ret = fprintf(fp, "%.16" PRIx64 "%.16" PRIx64, upper, lower); + ret = fprintf(fp, "%s", new_id); if (ret < 0) { telem_perror("Unable to write to machine id file"); goto file_error; @@ -721,49 +723,17 @@ file_error: int generate_machine_id(void) { - int frandom = -1; - struct stat stat_buf; - int ret = 0; - struct iovec random_id[2]; - uint64_t random_id_upper; - uint64_t random_id_lower; - int result = -1; + int result = 0; + char *new_id = NULL; - ret = stat("/dev/urandom", &stat_buf); - if (ret == -1) { - telem_log(LOG_ERR, "Unable to stat urandom device\n"); + if ((result = get_random_id(&new_id)) != 0) { goto rand_err; } - if (!S_ISCHR(stat_buf.st_mode)) { - telem_log(LOG_ERR, "/dev/urandom is not a character device file\n"); - goto rand_err; - } - - /* TODO : check for major and minor numbers to be extra sure ?? - ((stat_buffer.st_rdev == makedev(1, 8)) || (stat_buffer.st_rdev == makedev(1, 9))*/ - - frandom = open("/dev/urandom", O_RDONLY); - if (frandom < 0) { - telem_perror("Error opening /dev/urandom"); - goto rand_err; - } - - random_id[0].iov_base = &random_id_upper; - random_id[0].iov_len = 8; - random_id[1].iov_base = &random_id_lower; - random_id[1].iov_len = 8; - - if (readv(frandom, random_id, 2) < 0) { - telem_perror("Error while reading /dev/urandom"); - goto rand_err; - } - - result = machine_id_write(random_id_upper, random_id_lower); + result = machine_id_write(new_id); rand_err: - if (frandom >= 0) { - close(frandom); - } + free(new_id); + return result; } diff --git a/src/telemdaemon.h b/src/telemdaemon.h index 442cb0f..c3a7e69 100644 --- a/src/telemdaemon.h +++ b/src/telemdaemon.h @@ -269,7 +269,7 @@ bool post_record_http(char *header_values[], char *body, bool spool); * @param body The payload of the record * */ -void spool_record(TelemDaemon *daemon, char *headers[], char *body); +void spool_record(TelemDaemon * daemon, char *headers[], char *body); /** * Get random machine id stored in file diff --git a/src/telemetry.c b/src/telemetry.c index f0d331e..966b6ce 100644 --- a/src/telemetry.c +++ b/src/telemetry.c @@ -31,6 +31,7 @@ #include #include +#include "util.h" #include "common.h" #include "configuration.h" #include "telemetry.h" @@ -138,38 +139,14 @@ static int set_severity_header(struct telem_ref *t_ref, uint32_t severity) */ static int set_classification_header(struct telem_ref *t_ref, char *classification) { - size_t i, j; - int slashes = 0; - int x = 0; - j = strlen(classification); - - if (j > MAX_CLASS_LENGTH) { - return -EINVAL; - } - - for (i = 0, x = 0; i <= (j - 1); i++, x++) { - if (classification[i] == '/') { - slashes++; - x = 0; - } else { - if (x > MAX_SUBCAT_LENGTH) { - return -EINVAL; - } - } - } - - if (slashes != 2) { -#ifdef DEBUG - fprintf(stderr, "ERR: Classification string should have two /s.\n"); -#endif + if (validate_classification(classification) == 1) { return -EINVAL; } return set_header(&(t_ref->record->headers[TM_CLASSIFICATION]), TM_CLASSIFICATION_STR, classification, &(t_ref->record->header_size)); - } /** @@ -638,7 +615,6 @@ static int set_bios_version_header(struct telem_ref *t_ref) return status; } - /** * Generates an alphanumeric id of length 32 * @@ -652,7 +628,7 @@ static int gen_event_id(char *buff) { int frandom = -1; int result = -1; - uint64_t random_id[2] = {0}; + uint64_t random_id[2] = { 0 }; frandom = open("/dev/urandom", O_RDONLY); if (frandom < 0) { @@ -670,7 +646,6 @@ static int gen_event_id(char *buff) return result; } - /** * Sets the event_id header, this id is an identifier that multiple * records can share. This means that one event can lead to multiple @@ -689,7 +664,7 @@ static int set_event_id_header(struct telem_ref *t_ref) rc = gen_event_id(buff); if (rc == 0) { - rc = set_header( + rc = set_header( &(t_ref->record->headers[TM_EVENT_ID]), TM_EVENT_ID_STR, buff, &(t_ref->record->header_size)); @@ -698,7 +673,6 @@ static int set_event_id_header(struct telem_ref *t_ref) return rc; } - /** * Sets the hosttype header, which is a tuple of three values looked for * in the dmi filesystem. System Vendor (sys_vendor), Product Name @@ -1084,7 +1058,6 @@ int tm_set_payload(struct telem_ref *t_ref, char *payload) return ret; } - /** * Checks for id to have length = 32 characters * and hexadecimal characters only. @@ -1096,49 +1069,47 @@ int tm_set_payload(struct telem_ref *t_ref, char *payload) */ static int validate_event_id(char *id) { - int rc = 0; - const char alphab[] = EVENT_ID_ALPHAB; + int rc = 0; + const char alphab[] = EVENT_ID_ALPHAB; - // make sure is not null - if (!id) { - return 1; - } + // make sure is not null + if (!id) { + return 1; + } - // 32 - 32 = 0 - if ((rc = (int)(strlen(id) - EVENT_ID_LEN)) != 0) { - return 1; - } + // 32 - 32 = 0 + if ((rc = (int)(strlen(id) - EVENT_ID_LEN)) != 0) { + return 1; + } - // verify alphabet - // strspn checks for characters in alphab - if (strspn(id, alphab) != EVENT_ID_LEN) { - return 1; - } + // verify alphabet + // strspn checks for characters in alphab + if (strspn(id, alphab) != EVENT_ID_LEN) { + return 1; + } - return rc; + return rc; } - int tm_set_event_id(struct telem_ref *t_ref, char *event_id) { int rc = -1; if (!validate_event_id(event_id)) { - if (t_ref && t_ref->record && t_ref->record->headers) { - // free default id before overriding - free(t_ref->record->headers[TM_EVENT_ID]); - // set new event_id - if (asprintf(&(t_ref->record->headers[TM_EVENT_ID]), - "%s: %s\n", TM_EVENT_ID_STR, event_id) > 0) { - rc = 0; - } - } + if (t_ref && t_ref->record && t_ref->record->headers) { + // free default id before overriding + free(t_ref->record->headers[TM_EVENT_ID]); + // set new event_id + if (asprintf(&(t_ref->record->headers[TM_EVENT_ID]), + "%s: %s\n", TM_EVENT_ID_STR, event_id) > 0) { + rc = 0; + } + } } return rc; } - /** * Write nbytes from buf to fd. Used to send records to telemd. * diff --git a/src/util.c b/src/util.c index 33cd3bd..4afcd56 100644 --- a/src/util.c +++ b/src/util.c @@ -49,10 +49,14 @@ bool get_header_value(const char *header, char **value) *value = NULL; - if((sep = strchr(header, ':')) != NULL) { + if (header == NULL) { + return false; + } + + if ((sep = strchr(header, ':')) != NULL) { sep++; // skip space after colon if there's one - if(*sep == ' ') { + if (*sep == ' ') { sep++; } *value = strdup(sep); @@ -141,7 +145,7 @@ int get_random_id(char **buff) { int result = -1; int frandom = -1; - uint64_t random_id[2] = {0}; + uint64_t random_id[2] = { '\0' }; frandom = open("/dev/urandom", O_RDONLY); if (frandom < 0) { @@ -149,9 +153,9 @@ int get_random_id(char **buff) } if (read(frandom, &random_id, sizeof(random_id)) == sizeof(random_id)) { - if (asprintf(buff, "%.16" PRIx64 "%.16" PRIx64, random_id[0], random_id[1]) == RANDOM_ID_LEN) { - result = 0; - } + if (asprintf(buff, "%.16" PRIx64 "%.16" PRIx64, random_id[0], random_id[1]) == RANDOM_ID_LEN) { + result = 0; + } } close(frandom); @@ -159,4 +163,52 @@ int get_random_id(char **buff) return result; } +/** + * Validate classification value. A valid classification + * is a string with 2 slashes, with max length of + * MAX_CLASS_LENGTH and strings betweeb slashes should + * have a max length of MAX_SUBCAT_LENGTH. + * + * @param classification A pointer to classification value + * to be validated. + * + * @return 0 on sucess and 1 on failure + */ +int validate_classification(char *classification) +{ + size_t i, j; + int slashes = 0; + int x = 0; + + if (classification == NULL) { + return 1; + } + + j = strlen(classification); + + if (j > MAX_CLASS_LENGTH) { + return 1; + } + + for (i = 0, x = 0; i <= (j - 1); i++, x++) { + if (classification[i] == '/') { + slashes++; + x = 0; + } else { + if (x > MAX_SUBCAT_LENGTH) { + return 1; + } + } + } + + if (slashes != 2) { +#ifdef DEBUG + fprintf(stderr, "ERR: Classification string should have two /s.\n"); +#endif + return 1; + } + + return 0; +} + /* vi: set ts=8 sw=8 sts=4 et tw=80 cino=(0: */ diff --git a/src/util.h b/src/util.h index 7ec8bb9..6a3810a 100644 --- a/src/util.h +++ b/src/util.h @@ -33,4 +33,7 @@ long get_directory_size(const char *sdir); /* Initialize buff and copy generated id */ int get_random_id(char **buff); +/* Validates classification value */ +int validate_classification(char *classification); + /* vi: set ts=8 sw=8 sts=4 et tw=80 cino=(0: */ diff --git a/tests/check_journal.c b/tests/check_journal.c index 72d0ab6..d9cf048 100644 --- a/tests/check_journal.c +++ b/tests/check_journal.c @@ -15,10 +15,15 @@ */ #include +#include +#include +#include "common.h" #include "journal/journal.h" static struct TelemJournal *journal = NULL; static char *journal_file = TESTOOPSDIR "/journal.txt"; +static char *eid = "00007766547776eb7fc478eb0eb43e43"; +static int K = 20; START_TEST(check_open_journal) { @@ -50,93 +55,90 @@ void new_entry_setup(void) START_TEST(check_new_entry) { - int ret; - ret = new_journal_entry(journal, "t/t/t", 1520054957, - "3bc17766547776eb7fc478eb0eb43e43"); - ck_assert_int_eq(ret, 0); - close_journal(journal); + int ret; + ret = new_journal_entry(journal, "t/t/t", 1520054957, + "3bc17766547776eb7fc478eb0eb43e43"); + ck_assert_int_eq(ret, 0); + close_journal(journal); } END_TEST START_TEST(check_new_entry_bad_class) { - int ret = 0; - /* has an invalid class */ - ret = new_journal_entry(journal, "t/t", 1520054957, - "3bc17766547776eb7fc478eb0eb43e43"); - ck_assert_int_eq(ret, 1); + int ret = 0; + /* has an invalid class */ + ret = new_journal_entry(journal, "t/t", 1520054957, + "3bc17766547776eb7fc478eb0eb43e43"); + ck_assert_int_eq(ret, 1); } END_TEST START_TEST(check_new_entry_bad_id) { - int ret = 0; - /* event_id is invalid */ - ret = new_journal_entry(journal, "t/t/t", 1520054957, - "Xbc17766547776eb7fc478eb0eb43e43"); - ck_assert_int_eq(ret, 1); + int ret = 0; + /* event_id is invalid */ + ret = new_journal_entry(journal, "t/t/t", 1520054957, + "Xbc17766547776eb7fc478eb0eb43e43"); + ck_assert_int_eq(ret, 1); } END_TEST START_TEST(check_new_entry_null_class) { - int ret = 0; - /* param is NULL */ - ret = new_journal_entry(journal, NULL, 1520054957, - "3bc17766547776eb7fc478eb0eb43e43"); - ck_assert_int_eq(ret, 1); + int ret = 0; + /* param is NULL */ + ret = new_journal_entry(journal, NULL, 1520054957, + "3bc17766547776eb7fc478eb0eb43e43"); + ck_assert_int_eq(ret, 1); } END_TEST START_TEST(check_new_entry_null_id) { - int ret = 0; - /* null id */ - ret = new_journal_entry(journal, "t/t/t", 1520054957, NULL); - ck_assert_int_eq(ret, 1); + int ret = 0; + /* null id */ + ret = new_journal_entry(journal, "t/t/t", 1520054957, NULL); + ck_assert_int_eq(ret, 1); } END_TEST void new_entry_teardown(void) { - if (journal) { - close_journal(journal); - journal = NULL; - } + if (journal) { + close_journal(journal); + journal = NULL; + } } void insert_n_records(int n, struct TelemJournal *j) { for (int i = 0; i < n; i++) { new_journal_entry(j, "t/t/t", 1520054957 + i, - "3bc17766547776eb7fc478eb0eb43e43"); + "3bc17766547776eb7fc478eb0eb43e43"); } } /* - -failing in travis-ci - + failing in travis-ci + */ START_TEST(check_journal_file_prune) { - int rc = 0; - struct TelemJournal *j = open_journal(journal_file); + int rc = 0; + struct TelemJournal *j = open_journal(journal_file); - insert_n_records(j->record_count_limit * 2, j); - ck_assert_int_gt(j->record_count, j->record_count_limit); - rc = prune_journal(j); - ck_assert_int_eq(rc, 0); - // Record count should always be below the limit + hysteresis - ck_assert_int_lt(j->record_count, j->record_count_limit + DEVIATION); - - - close_journal(j); + insert_n_records(j->record_count_limit * 2, j); + ck_assert_int_gt(j->record_count, j->record_count_limit); + rc = prune_journal(j, TESTOOPSDIR); + ck_assert(rc == 0); + // Record count should always be below the limit + hysteresis + ck_assert_int_lt(j->record_count, j->record_count_limit + DEVIATION); + close_journal(j); } END_TEST -*/ void journal_entry_setup(void) { + int result = 0; const char *journal_print = TESTOOPSDIR "/journal.print.txt"; if (journal) { @@ -144,34 +146,42 @@ void journal_entry_setup(void) } journal = open_journal(journal_print); + + insert_n_records(K, journal); + /* Insert recors with specific values */ + result = new_journal_entry(journal, "a/b/c", 1520054957, eid); + ck_assert(result == 0); + result = new_journal_entry(journal, "a/b/d", 1520054957, eid); + ck_assert(result == 0); + } START_TEST(check_journal_print) { - int count = 0; - const int k = 20; - - insert_n_records(k, journal); - count = print_journal(journal, NULL, NULL, NULL, NULL); - ck_assert(count == k); + int count = 0; + count = print_journal(journal, NULL, NULL, NULL, NULL); + ck_assert_int_eq(count, K + 2); } END_TEST -START_TEST(check_journal_filter) +START_TEST(check_journal_filter_by_class) { - int result = 0; - char *eid = "00007766547776eb7fc478eb0eb43e43"; + int result = print_journal(journal, "a/b/c", NULL, NULL, NULL); + ck_assert_int_eq(result, 1); +} +END_TEST - result = new_journal_entry(journal, "a/b/c", 1520054957, eid); - ck_assert(result == 0); - result = new_journal_entry(journal, "a/b/d", 1520054957, eid); - ck_assert(result == 0); +START_TEST(check_journal_filter_by_class_prefix) +{ + int result = print_journal(journal, "a/b/*", NULL, NULL, NULL); + ck_assert_int_eq(result, 2); +} +END_TEST - result = print_journal(journal, "a/b/c", NULL, NULL, NULL); - ck_assert(result == 1); - - result = print_journal(journal, NULL, NULL, eid, NULL); - ck_assert(result == 2); +START_TEST(check_journal_filter_by_event_id) +{ + int result = print_journal(journal, NULL, NULL, eid, NULL); + ck_assert(result == 2); } END_TEST @@ -200,15 +210,17 @@ Suite *config_suite(void) tcase_add_test(t, check_new_entry_null_id); suite_add_tcase(s, t); -/* t = tcase_create("prunning journal"); + t = tcase_create("prunning journal"); tcase_add_test(t, check_journal_file_prune); - suite_add_tcase(s, t);*/ + suite_add_tcase(s, t); t = tcase_create("print journal"); tcase_add_unchecked_fixture(t, journal_entry_setup, journal_entry_teardown); tcase_add_test(t, check_journal_print); - tcase_add_test(t, check_journal_filter); + tcase_add_test(t, check_journal_filter_by_class); + tcase_add_test(t, check_journal_filter_by_class_prefix); + tcase_add_test(t, check_journal_filter_by_event_id); suite_add_tcase(s, t); return s; diff --git a/tests/check_libtelemetry.c b/tests/check_libtelemetry.c index 7a1f1bc..04176a0 100644 --- a/tests/check_libtelemetry.c +++ b/tests/check_libtelemetry.c @@ -160,19 +160,19 @@ END_TEST void event_id_setup(void) { - int ret; + int ret; - if (ref) { - return; - } + if (ref) { + return; + } - ret = tm_create_record(&ref, 1, "t/t/t", 2000); - original_event_id = strdup(ref->record->headers[TM_EVENT_ID]); - if (!original_event_id) { - return; - } - ck_assert_msg(ret != -ECONNREFUSED, - "Opt-out enabled. Opt in to run this test"); + ret = tm_create_record(&ref, 1, "t/t/t", 2000); + original_event_id = strdup(ref->record->headers[TM_EVENT_ID]); + if (!original_event_id) { + return; + } + ck_assert_msg(ret != -ECONNREFUSED, + "Opt-out enabled. Opt in to run this test"); } START_TEST(record_set_event_id)