adding get_crashdump_log()
authorVolker Birk <vb@pep-project.org>
Sat, 20 Feb 2016 14:43:28 +0100
changeset 450ae04b9c42932
parent 449 c57293f2975b
child 451 495e53a4b441
adding get_crashdump_log()
Makefile.conf
src/pEpEngine.c
src/pEpEngine.h
src/pEp_internal.h
     1.1 --- a/Makefile.conf	Tue Feb 16 18:34:57 2016 +0100
     1.2 +++ b/Makefile.conf	Sat Feb 20 14:43:28 2016 +0100
     1.3 @@ -8,3 +8,9 @@
     1.4  # the next two lines are ignored on Windoze
     1.5  SYSTEM_DB=/usr/local/share/pEp/system.db
     1.6  PREFIX=$(HOME)
     1.7 +
     1.8 +# C makros (not environment variables) to overwrite:
     1.9 +#
    1.10 +# DEFAULT_KEYSERVER - string with default keyserver
    1.11 +# CRASHDUMP_DEFAULT_LINES - number of log lines to deliver for crashdumps
    1.12 +
     2.1 --- a/src/pEpEngine.c	Tue Feb 16 18:34:57 2016 +0100
     2.2 +++ b/src/pEpEngine.c	Sat Feb 20 14:43:28 2016 +0100
     2.3 @@ -19,6 +19,8 @@
     2.4      static const char *sql_get_trust;
     2.5      static const char *sql_least_trust;
     2.6      static const char *sql_mark_as_compromized;
     2.7 +    static const char *sql_crashdump;
     2.8 +
     2.9      bool in_first = false;
    2.10  
    2.11      assert(sqlite3_threadsafe());
    2.12 @@ -199,6 +201,8 @@
    2.13          sql_least_trust = "select min(comm_type) from trust where pgp_keypair_fpr = ?1 ;";
    2.14  
    2.15          sql_mark_as_compromized = "update trust not indexed set comm_type = 15 where pgp_keypair_fpr = ?1 ;";
    2.16 +
    2.17 +        sql_crashdump = "select title, entity, description, comment from log order by timestamp desc limit ?1 ;";
    2.18      }
    2.19  
    2.20      int_result = sqlite3_prepare_v2(_session->db, sql_log, (int)strlen(sql_log),
    2.21 @@ -315,9 +319,24 @@
    2.22      }
    2.23  }
    2.24  
    2.25 +static void _clean_log_value(char *text)
    2.26 +{
    2.27 +    if (text) {
    2.28 +        for (char *c = text; *c; c++) {
    2.29 +            if (*c < 32)
    2.30 +                *c = 32;
    2.31 +            else if (*c == '"')
    2.32 +                *c = '\'';
    2.33 +        }
    2.34 +    }
    2.35 +}
    2.36 +
    2.37  DYNAMIC_API PEP_STATUS log_event(
    2.38 -        PEP_SESSION session, const char *title, const char *entity,
    2.39 -        const char *description, const char *comment
    2.40 +        PEP_SESSION session,
    2.41 +        char *title,
    2.42 +        char *entity,
    2.43 +        char *description,
    2.44 +        char *comment
    2.45      )
    2.46  {
    2.47  	PEP_STATUS status = PEP_STATUS_OK;
    2.48 @@ -330,6 +349,11 @@
    2.49      if (!(session && title && entity))
    2.50          return PEP_ILLEGAL_VALUE;
    2.51  
    2.52 +    _clean_log_value(title);
    2.53 +    _clean_log_value(entity);
    2.54 +    _clean_log_value(description);
    2.55 +    _clean_log_value(comment);
    2.56 +
    2.57  	sqlite3_reset(session->log);
    2.58  	sqlite3_bind_text(session->log, 1, title, -1, SQLITE_STATIC);
    2.59  	sqlite3_bind_text(session->log, 2, entity, -1, SQLITE_STATIC);
    2.60 @@ -1023,3 +1047,104 @@
    2.61              expired);
    2.62  }
    2.63  
    2.64 +static char *_concat_string(char *str1, const char *str2, char delim)
    2.65 +{
    2.66 +    assert(str2);
    2.67 +
    2.68 +    size_t len1 = str1 ? strlen(str1) : 0;
    2.69 +    size_t len2 = strlen(str2);
    2.70 +    size_t len = len1 + len2 + 3;
    2.71 +    char * result = realloc(str1, len + 1);
    2.72 +
    2.73 +    if (result) {
    2.74 +        result[len1] = '"';
    2.75 +        strcpy(result + len1 + 1, str2);
    2.76 +        _clean_log_value(result + len1 + 1);
    2.77 +        result[len - 2] = '"';
    2.78 +        result[len - 1] = delim;
    2.79 +        result[len] = 0;
    2.80 +    }
    2.81 +    else {
    2.82 +        free(str1);
    2.83 +    }
    2.84 +
    2.85 +    return result;
    2.86 +}
    2.87 +
    2.88 +DYNAMIC_API PEP_STATUS get_crashdump_log(
    2.89 +        PEP_SESSION session,
    2.90 +        int maxlines,
    2.91 +        char **logdata
    2.92 +    )
    2.93 +{
    2.94 +    PEP_STATUS status = PEP_STATUS_OK;
    2.95 +    char *_logdata= NULL;
    2.96 +
    2.97 +    assert(session);
    2.98 +    assert(maxlines >= 0 && maxlines <= CRASHDUMP_MAX_LINES);
    2.99 +    assert(logdata);
   2.100 +
   2.101 +    if (!(session && logdata && maxlines >= 0 && maxlines <=
   2.102 +            CRASHDUMP_MAX_LINES))
   2.103 +        return PEP_ILLEGAL_VALUE;
   2.104 +
   2.105 +    int limit = maxlines ? maxlines : CRASHDUMP_DEFAULT_LINES;
   2.106 +    const char *title;
   2.107 +    const char *entity;
   2.108 +    const char *desc;
   2.109 +    const char *comment;
   2.110 +
   2.111 +    sqlite3_reset(session->crashdump);
   2.112 +	sqlite3_bind_int(session->crashdump, 1, limit);
   2.113 +
   2.114 +    int result;
   2.115 +
   2.116 +    do {
   2.117 +        result = sqlite3_step(session->crashdump);
   2.118 +        switch (result) {
   2.119 +        case SQLITE_ROW:
   2.120 +            title   = (const char *) sqlite3_column_text(session->crashdump, 0);
   2.121 +            entity  = (const char *) sqlite3_column_text(session->crashdump, 1);
   2.122 +            desc    = (const char *) sqlite3_column_text(session->crashdump, 2);
   2.123 +            comment = (const char *) sqlite3_column_text(session->crashdump, 3);
   2.124 +
   2.125 +            _logdata = _concat_string(_logdata, title, ',');
   2.126 +            if (_logdata == NULL)
   2.127 +                goto enomem;
   2.128 +
   2.129 +            _logdata = _concat_string(_logdata, entity, ',');
   2.130 +            if (_logdata == NULL)
   2.131 +                goto enomem;
   2.132 +
   2.133 +            _logdata = _concat_string(_logdata, desc, ',');
   2.134 +            if (_logdata == NULL)
   2.135 +                goto enomem;
   2.136 +
   2.137 +            _logdata = _concat_string(_logdata, comment, '\n');
   2.138 +            if (_logdata == NULL)
   2.139 +                goto enomem;
   2.140 +
   2.141 +            break;
   2.142 +
   2.143 +        case SQLITE_DONE:
   2.144 +            break;
   2.145 +
   2.146 +        default:
   2.147 +            status = PEP_UNKNOWN_ERROR;
   2.148 +            result = SQLITE_DONE;
   2.149 +        }
   2.150 +    } while (result != SQLITE_DONE);
   2.151 +
   2.152 +    sqlite3_reset(session->crashdump);
   2.153 +    if (status == PEP_STATUS_OK)
   2.154 +        *logdata = _logdata;
   2.155 +
   2.156 +    goto the_end;
   2.157 +
   2.158 +enomem:
   2.159 +    status = PEP_OUT_OF_MEMORY;
   2.160 +
   2.161 +the_end:
   2.162 +    return status;
   2.163 +}
   2.164 +
     3.1 --- a/src/pEpEngine.h	Tue Feb 16 18:34:57 2016 +0100
     3.2 +++ b/src/pEpEngine.h	Sat Feb 20 14:43:28 2016 +0100
     3.3 @@ -213,8 +213,11 @@
     3.4  //	    PEP_STATUS_OK       log entry created
     3.5  
     3.6  DYNAMIC_API PEP_STATUS log_event(
     3.7 -        PEP_SESSION session, const char *title, const char *entity,
     3.8 -        const char *description, const char *comment
     3.9 +        PEP_SESSION session,
    3.10 +        char *title,
    3.11 +        char *entity,
    3.12 +        char *description,
    3.13 +        char *comment
    3.14      );
    3.15  
    3.16  
    3.17 @@ -686,6 +689,20 @@
    3.18      );
    3.19  
    3.20  
    3.21 +// get_crashdump_log() - get the last log messages out
    3.22 +//
    3.23 +//  parameters:
    3.24 +//      session (in)            session handle
    3.25 +//      maxlines (in)           maximum number of lines (0 for default)
    3.26 +//      logdata (out)           logdata
    3.27 +
    3.28 +DYNAMIC_API PEP_STATUS get_crashdump_log(
    3.29 +        PEP_SESSION session,
    3.30 +        int maxlines,
    3.31 +        char **logdata
    3.32 +    );
    3.33 +
    3.34 +
    3.35  #ifdef __cplusplus
    3.36  }
    3.37  #endif
     4.1 --- a/src/pEp_internal.h	Tue Feb 16 18:34:57 2016 +0100
     4.2 +++ b/src/pEp_internal.h	Sat Feb 20 14:43:28 2016 +0100
     4.3 @@ -1,4 +1,4 @@
     4.4 -#define PEP_ENGINE_VERSION "0.5.0"
     4.5 +#define PEP_ENGINE_VERSION "0.6.0"
     4.6  
     4.7  // this is 20 trustwords with 79 chars max
     4.8  #define MAX_TRUSTWORDS_SPACE (20 * 80)
     4.9 @@ -13,7 +13,15 @@
    4.10  #define MAX_LINELENGTH 1024
    4.11  
    4.12  // default keyserver
    4.13 +#ifndef DEFAULT_KEYSERVER
    4.14  #define DEFAULT_KEYSERVER "hkp://keys.gnupg.net"
    4.15 +#endif
    4.16 +
    4.17 +// crashdump constants
    4.18 +#ifndef CRASHDUMP_DEFAULT_LINES
    4.19 +#define CRASHDUMP_DEFAULT_LINES 100
    4.20 +#endif
    4.21 +#define CRASHDUMP_MAX_LINES 32767
    4.22  
    4.23  #include "platform.h"
    4.24  
    4.25 @@ -88,6 +96,7 @@
    4.26      sqlite3_stmt *least_trust;
    4.27      sqlite3_stmt *mark_compromized;
    4.28      sqlite3_stmt *reset_trust;
    4.29 +    sqlite3_stmt *crashdump;
    4.30  
    4.31      examine_identity_t examine_identity;
    4.32      void *examine_management;