Example #1
0
static qltimer_t*
new_timer(qdict_t *args, const char *mod, const char *func) {
  qltimer_t *timer;

  timer = (qltimer_t*)qalloc(sizeof(qltimer_t));
  if (timer == NULL) {
    return NULL;
  }
  timer->args = args;
  timer->mod  = qstring_new(mod);
  timer->func = qstring_new(func);
  if (timer->mod == NULL || timer->func == NULL) {
    goto error;
  }

  return timer;

error:
  if (timer->mod) {
    qstring_destroy(timer->mod);
  }
  if (timer->func) {
    qstring_destroy(timer->func);
  }
  qfree(timer);

  return NULL;
}
Example #2
0
void json_lexer_init(JSONLexer *lexer, JSONLexerEmitter func)
{
    lexer->emit = func;
    lexer->state = IN_START;
    lexer->token = qstring_new();
    lexer->x = lexer->y = 0;
}
Example #3
0
static int send_response(GAState *s, QObject *payload)
{
    const char *buf;
    QString *payload_qstr, *response_qstr;
    GIOStatus status;

    g_assert(payload && s->channel);

    payload_qstr = qobject_to_json(payload);
    if (!payload_qstr) {
        return -EINVAL;
    }

    if (s->delimit_response) {
        s->delimit_response = false;
        response_qstr = qstring_new();
        qstring_append_chr(response_qstr, QGA_SENTINEL_BYTE);
        qstring_append(response_qstr, qstring_get_str(payload_qstr));
        QDECREF(payload_qstr);
    } else {
        response_qstr = payload_qstr;
    }

    qstring_append_chr(response_qstr, '\n');
    buf = qstring_get_str(response_qstr);
    status = ga_channel_write_all(s->channel, buf, strlen(buf));
    QDECREF(response_qstr);
    if (status != G_IO_STATUS_NORMAL) {
        return -EIO;
    }

    return 0;
}
Example #4
0
void
qlogger_open_file() {
  qstring_t   file;
  char        buff[30] = {'\0'};
  time_t      t;  
  struct tm   tm; 

  t = time(NULL);
  memset(&tm, 0, sizeof(struct tm));
  localtime_r(&t, &tm);
  strftime((char*)(&buff[0]), sizeof(buff), "%Y%m%d-%H%M%S", &tm);

  logger->log_size = 0;
  if (logger->fd != -1) {
    fsync(logger->fd);
    close(logger->fd);
  }
  file = qstring_new(config.log_path);
  file = qstring_catvprintf(file, "/qserver_%s.log", buff);

  logger->fd = open(file, O_CREAT | O_TRUNC | O_RDWR,
                    S_IWUSR | S_IRUSR | S_IWOTH | S_IROTH | S_IRGRP | S_IWGRP);
  if (logger->fd == -1) {
    qstdout("open log file %s error: %s\n", file, strerror(errno));
    exit(-1);
  }
  qstring_destroy(file);
}
QString *qobject_to_json(const QObject *obj)
{
    QString *str = qstring_new();

    to_json(obj, str);

    return str;
}
Example #6
0
static void qobject_is_equal_conversion_test(void)
{
    QNum *u0, *i0, *d0;
    QString *s0, *s_empty;
    QBool *bfalse;

    u0 = qnum_from_uint(0u);
    i0 = qnum_from_int(0);
    d0 = qnum_from_double(0.0);
    s0 = qstring_from_str("0");
    s_empty = qstring_new();
    bfalse = qbool_from_bool(false);

    /* No automatic type conversion */
    check_unequal(u0, s0, s_empty, bfalse, qnull(), NULL);
    check_unequal(i0, s0, s_empty, bfalse, qnull(), NULL);
    check_unequal(d0, s0, s_empty, bfalse, qnull(), NULL);

    free_all(u0, i0, d0, s0, s_empty, bfalse);
}
Example #7
0
static int json_lexer_feed_char(JSONLexer *lexer, char ch)
{
    int char_consumed, new_state;

    lexer->x++;
    if (ch == '\n') {
        lexer->x = 0;
        lexer->y++;
    }

    do {
        new_state = json_lexer[lexer->state][(uint8_t)ch];
        char_consumed = !TERMINAL_NEEDED_LOOKAHEAD(lexer->state, new_state);
        if (char_consumed) {
            qstring_append_chr(lexer->token, ch);
        }

        switch (new_state) {
        case JSON_OPERATOR:
        case JSON_ESCAPE:
        case JSON_INTEGER:
        case JSON_FLOAT:
        case JSON_KEYWORD:
        case JSON_STRING:
            lexer->emit(lexer, lexer->token, new_state, lexer->x, lexer->y);
        case JSON_SKIP:
            QDECREF(lexer->token);
            lexer->token = qstring_new();
            new_state = IN_START;
            break;
        case IN_ERROR:
            return -EINVAL;
        default:
            break;
        }
        lexer->state = new_state;
    } while (!char_consumed);
    return 0;
}
Example #8
0
/**
 * qerror_human(): Format QError data into human-readable string.
 *
 * Formats according to member 'desc' of the specified QError object.
 */
QString *qerror_human(const QError *qerror)
{
    const char *p;
    QString *qstring;

    assert(qerror->entry != NULL);

    qstring = qstring_new();

    for (p = qerror->entry->desc; *p != '\0';) {
        if (*p != '%') {
            qstring_append_chr(qstring, *p++);
        } else if (*(p + 1) == '%') {
            qstring_append_chr(qstring, '%');
            p += 2;
        } else {
            p = append_field(qstring, qerror, p);
        }
    }

    return qstring;
}
Example #9
0
static QString *qerror_format_desc(QDict *error,
                                   const QErrorStringTable *entry)
{
    QString *qstring;
    const char *p;

    assert(entry != NULL);

    qstring = qstring_new();

    for (p = entry->desc; *p != '\0';) {
        if (*p != '%') {
            qstring_append_chr(qstring, *p++);
        } else if (*(p + 1) == '%') {
            qstring_append_chr(qstring, '%');
            p += 2;
        } else {
            p = append_field(error, qstring, entry, p);
        }
    }

    return qstring;
}
Example #10
0
static int json_lexer_feed_char(JSONLexer *lexer, char ch, bool flush)
{
    int char_consumed, new_state;

    lexer->x++;
    if (ch == '\n') {
        lexer->x = 0;
        lexer->y++;
    }

    do {
        new_state = json_lexer[lexer->state][(uint8_t)ch];
        char_consumed = !TERMINAL_NEEDED_LOOKAHEAD(lexer->state, new_state);
        if (char_consumed) {
            qstring_append_chr(lexer->token, ch);
        }

        switch (new_state) {
        case JSON_OPERATOR:
        case JSON_ESCAPE:
        case JSON_INTEGER:
        case JSON_FLOAT:
        case JSON_KEYWORD:
        case JSON_STRING:
            lexer->emit(lexer, lexer->token, new_state, lexer->x, lexer->y);
            /* fall through */
        case JSON_SKIP:
            QDECREF(lexer->token);
            lexer->token = qstring_new();
            new_state = IN_START;
            break;
        case IN_ERROR:
            /* XXX: To avoid having previous bad input leaving the parser in an
             * unresponsive state where we consume unpredictable amounts of
             * subsequent "good" input, percolate this error state up to the
             * tokenizer/parser by forcing a NULL object to be emitted, then
             * reset state.
             *
             * Also note that this handling is required for reliable channel
             * negotiation between QMP and the guest agent, since chr(0xFF)
             * is placed at the beginning of certain events to ensure proper
             * delivery when the channel is in an unknown state. chr(0xFF) is
             * never a valid ASCII/UTF-8 sequence, so this should reliably
             * induce an error/flush state.
             */
            lexer->emit(lexer, lexer->token, JSON_ERROR, lexer->x, lexer->y);
            QDECREF(lexer->token);
            lexer->token = qstring_new();
            new_state = IN_START;
            lexer->state = new_state;
            return 0;
        default:
            break;
        }
        lexer->state = new_state;
    } while (!char_consumed && !flush);

    /* Do not let a single token grow to an arbitrarily large size,
     * this is a security consideration.
     */
    if (lexer->token->length > MAX_TOKEN_SIZE) {
        lexer->emit(lexer, lexer->token, lexer->state, lexer->x, lexer->y);
        QDECREF(lexer->token);
        lexer->token = qstring_new();
        lexer->state = IN_START;
    }

    return 0;
}