예제 #1
0
파일: db.c 프로젝트: dbrelay/ngx_dbrelay
static void
dbrelay_write_json_log(json_t *json, dbrelay_request_t *request, char *error_string)
{
   	json_add_key(json, "log");
   	json_new_object(json);
   	if (request->sql) json_add_string(json, "sql", request->sql);
    	json_add_string(json, "error", error_string);
        json_end_object(json);
        json_end_object(json);
}
예제 #2
0
파일: db.c 프로젝트: dbrelay/ngx_dbrelay
int dbrelay_db_fill_data(json_t *json, dbrelay_connection_t *conn)
{
   int numcols, colnum;
   char tmp[256];
   int maxcolname;

   json_add_key(json, "data");
   json_new_array(json);
   while (api->has_results(conn->db)) 
   {
        maxcolname = 0;
	json_new_object(json);
	json_add_key(json, "fields");
	json_new_array(json);

	numcols = api->numcols(conn->db);
	for (colnum=1; colnum<=numcols; colnum++) {
            dbrelay_write_json_colinfo(json, conn->db, colnum, &maxcolname);
        }
	json_end_array(json);
	json_add_key(json, "rows");

	if (json_get_mode(json)==DBRELAY_JSON_MODE_STD) json_new_array(json);
        else json_add_json(json, "\"");

        while (api->fetch_row(conn->db)) { 
           maxcolname = 0;
	   if (json_get_mode(json)==DBRELAY_JSON_MODE_STD) json_new_object(json);
	   for (colnum=1; colnum<=numcols; colnum++) {
              dbrelay_write_json_column(json, conn->db, colnum, &maxcolname);
	      if (json_get_mode(json)==DBRELAY_JSON_MODE_CSV && colnum!=numcols) json_add_json(json, ",");
           }
	   if (json_get_mode(json)==DBRELAY_JSON_MODE_STD) json_end_object(json);
           else json_add_json(json, "\\n");
        }

	if (json_get_mode(json)==DBRELAY_JSON_MODE_STD) json_end_array(json);
        else json_add_json(json, "\",");

        if (api->rowcount(conn->db)==-1) {
           json_add_null(json, "count");
        } else {
           sprintf(tmp, "%d", api->rowcount(conn->db));
           json_add_number(json, "count", tmp);
        }
        json_end_object(json);
   }
   /* sprintf(error_string, "rc = %d", rc); */
   json_end_array(json);

   return 0;
}
예제 #3
0
파일: vmstate.c 프로젝트: MaddTheSane/qemu
static void vmsd_desc_field_end(const VMStateDescription *vmsd, QJSON *vmdesc,
                                const VMStateField *field, size_t size, int i)
{
    if (!vmdesc) {
        return;
    }

    if (field->flags & VMS_STRUCT) {
        /* We printed a struct in between, close its child object */
        json_end_object(vmdesc);
    }

    json_prop_int(vmdesc, "size", size);
    json_end_object(vmdesc);
}
예제 #4
0
파일: db.c 프로젝트: dbrelay/ngx_dbrelay
static void dbrelay_append_log_json(json_t *json, dbrelay_request_t *request, char *error_string)
{
   int i;
   char tmp[20];

   json_add_key(json, "log");
   json_new_object(json);
   if (request->flags & DBRELAY_FLAG_ECHOSQL) json_add_string(json, "sql", request->sql);
   if (strlen(error_string)) {
      json_add_string(json, "error", error_string);
   }
   i = 0;
   while (request->params[i]) {
      sprintf(tmp, "param%d", i);
      json_add_string(json, tmp, request->params[i]);
      i++;
   }
   json_end_object(json);

   json_end_object(json);
}
예제 #5
0
파일: vmstate.c 프로젝트: MaddTheSane/qemu
static int vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
                                   void *opaque, QJSON *vmdesc)
{
    const VMStateDescription **sub = vmsd->subsections;
    bool subsection_found = false;
    int ret = 0;

    trace_vmstate_subsection_save_top(vmsd->name);
    while (sub && *sub) {
        if (vmstate_save_needed(*sub, opaque)) {
            const VMStateDescription *vmsdsub = *sub;
            uint8_t len;

            trace_vmstate_subsection_save_loop(vmsd->name, vmsdsub->name);
            if (vmdesc) {
                /* Only create subsection array when we have any */
                if (!subsection_found) {
                    json_start_array(vmdesc, "subsections");
                    subsection_found = true;
                }

                json_start_object(vmdesc, NULL);
            }

            qemu_put_byte(f, QEMU_VM_SUBSECTION);
            len = strlen(vmsdsub->name);
            qemu_put_byte(f, len);
            qemu_put_buffer(f, (uint8_t *)vmsdsub->name, len);
            qemu_put_be32(f, vmsdsub->version_id);
            ret = vmstate_save_state(f, vmsdsub, opaque, vmdesc);
            if (ret) {
                return ret;
            }

            if (vmdesc) {
                json_end_object(vmdesc);
            }
        }
        sub++;
    }

    if (vmdesc && subsection_found) {
        json_end_array(vmdesc);
    }

    return ret;
}
예제 #6
0
파일: db.c 프로젝트: dbrelay/ngx_dbrelay
void dbrelay_write_json_colinfo(json_t *json, void *db, int colnum, int *maxcolname)
{
   char tmp[256], *colname, tmpcolname[256];
   int l;

   json_new_object(json);
   colname = api->colname(db, colnum);
   if (dbrelay_is_unnamed_column(colname)) {
      sprintf(tmpcolname, "%d", ++(*maxcolname));
      json_add_string(json, "name", tmpcolname);
   } else {
      l = atoi(colname); 
      if (l>0 && l>*maxcolname) {
         *maxcolname=l;
      }
      json_add_string(json, "name", colname);
   }
   api->coltype(db, colnum, tmp);
   json_add_string(json, "sql_type", tmp);
   l = api->collen(db, colnum);
   if (l!=0) {
      sprintf(tmp, "%d", l);
      json_add_number(json, "length", tmp);
   }
   l = api->colprec(db, colnum);
   if (l!=0) {
      sprintf(tmp, "%d", l);
      json_add_number(json, "precision", tmp);
   }
   l = api->colscale(db, colnum);
   if (l!=0) {
      sprintf(tmp, "%d", l);
      json_add_number(json, "scale", tmp);
   }
   json_end_object(json);
}
예제 #7
0
파일: db.c 프로젝트: dbrelay/ngx_dbrelay
/*
 * echo request parameters in json output
 */
static void dbrelay_append_request_json(json_t *json, dbrelay_request_t *request)
{
   json_add_key(json, "request");
   json_new_object(json);

   if (IS_SET(request->query_tag)) 
      json_add_string(json, "query_tag", request->query_tag);
   json_add_string(json, "sql_server", request->sql_server);
   json_add_string(json, "sql_user", request->sql_user);

   if (IS_SET(request->sql_port)) 
      json_add_string(json, "sql_port", request->sql_port);

   json_add_string(json, "sql_database", request->sql_database);

/*
 * do not return password back to client
   if (IS_SET(request->sql_password)) 
      json_add_string(json, "sql_password", request->sql_password);
*/

   json_end_object(json);

}
예제 #8
0
파일: qjson.c 프로젝트: AdrianHuang/qemu
void qjson_finish(QJSON *json)
{
    json_end_object(json);
}
예제 #9
0
int
getprop_main(const struct cmd_getprop_info* info)
{
    const char* format = info->getprop.format;
    const char* format_not_found = info->getprop.format_not_found;
    bool null = info->getprop.null;
    if (null && format == NULL && format_not_found == NULL)
        usage_error("must supply --format or "
                    "--format-not-found or both if using -0");

    find_symbol_in_libc("__system_property_foreach",
                        &property_foreach);
    if (property_foreach == NULL)
        property_foreach = compat_property_foreach;

    dbg("using %s for property enumeration",
        property_foreach == compat_property_foreach
        ? "compat_property_foreach"
        : "__system_property_foreach");

    int exit_status = 0;

    struct json_writer* writer = NULL;
    if (format == NULL && format_not_found == NULL) {
        writer = json_writer_create(xstdout);
        json_begin_object(writer);
    }

    const char** properties = ARGV_CONCAT(info->properties);
    bool first = true;
    char sep = null ? '\0' : '\n';
    if (*properties == NULL) {
        struct property_vector* pv = find_all_properties();
        char prev_name[PROP_NAME_MAX];
        for (size_t i = 0; i < pv->size; ++i) {
            char name[PROP_NAME_MAX];
            char value[PROP_VALUE_MAX];
            (void) __system_property_read(pv->props[i], name, value);
            if (i > 0 && strcmp(name, prev_name) == 0)
                continue;
            if (writer != NULL) {
                json_begin_field(writer, name);
                json_emit_string(writer, value);
            } else {
                output_property(&first, sep, format, name, value);
            }
            strcpy(prev_name, name);
        }
    } else {
        size_t nproperties = argv_count(properties);
        qsort(properties,
              nproperties,
              sizeof (properties[0]),
              property_argv_compare);
        const char* property;
        const char* prev_property = NULL;
        while ((property = *properties++)) {
            if (prev_property != NULL && !strcmp(prev_property, property))
                continue;
            if (writer != NULL)
                json_begin_field(writer, property);
            const prop_info* pi = __system_property_find(property);
            if (pi) {
                char value[PROP_VALUE_MAX];
                __system_property_read(pi, NULL, value);
                if (writer != NULL)
                    json_emit_string(writer, value);
                else if (format != NULL)
                    output_property(&first, sep, format, property, value);
            } else {
                if (writer != NULL)
                    json_emit_null(writer);
                else if (format_not_found != NULL)
                    output_property(&first, sep,
                                    format_not_found,
                                    property, NULL);
                exit_status = 4;
            }
            prev_property = property;
        }
    }

    if (writer == NULL && !first && !null)
        xputc(sep, xstdout);

    if (writer != NULL)
        json_end_object(writer);

    xflush(xstdout);
    return exit_status;
}
예제 #10
0
파일: db.c 프로젝트: dbrelay/ngx_dbrelay
u_char *dbrelay_db_status(dbrelay_request_t *request)
{
   dbrelay_connection_t *connections;
   dbrelay_connection_t *conn;
   json_t *json = json_new();
   int i;
   char tmpstr[100];
   u_char *json_output;
   struct tm *ts;


   json_new_object(json);
   json_add_key(json, "status");
   json_new_object(json);

   json_add_key(json, "info");
   json_new_object(json);
   json_add_string(json, "build", DBRELAY_BUILD);
   sprintf(tmpstr, "0x%08x", dbrelay_get_ipc_key());
   json_add_string(json, "ipckey", tmpstr);
   json_end_object(json);

   json_add_key(json, "connections");
   json_new_array(json);

   connections = dbrelay_time_get_shmem(request);

   for (i=0; i<DBRELAY_MAX_CONN; i++) {
     conn = &connections[i];
     if (connections[i].pid!=0) {
        json_new_object(json);
        sprintf(tmpstr, "%u", conn->slot);
        json_add_number(json, "slot", tmpstr);
        sprintf(tmpstr, "%u", conn->pid);
        json_add_number(json, "pid", tmpstr);
        json_add_string(json, "name", conn->connection_name ? conn->connection_name : "");
        ts = localtime(&conn->tm_create);
        strftime(tmpstr, sizeof(tmpstr), "%Y-%m-%d %H:%M:%S", ts);
        json_add_string(json, "tm_created", tmpstr);
        ts = localtime(&conn->tm_accessed);
        strftime(tmpstr, sizeof(tmpstr), "%Y-%m-%d %H:%M:%S", ts);
        json_add_string(json, "tm_accessed", tmpstr);
        json_add_string(json, "sql_server", conn->sql_server ? conn->sql_server : "");
        json_add_string(json, "sql_port", conn->sql_port ? conn->sql_port : "");
        json_add_string(json, "sql_database", conn->sql_database ? conn->sql_database : "");
        json_add_string(json, "sql_user", conn->sql_user ? conn->sql_user : "");
        sprintf(tmpstr, "%ld", conn->connection_timeout);
        json_add_number(json, "connection_timeout", tmpstr);
        sprintf(tmpstr, "%u", conn->in_use);
        json_add_number(json, "in_use", tmpstr);
        json_add_string(json, "sock_path", conn->sock_path);
        sprintf(tmpstr, "%u", conn->helper_pid);
        json_add_number(json, "helper_pid", tmpstr);
        json_end_object(json);
     }
   }

   dbrelay_time_release_shmem(request, connections);

   json_end_array(json);
   json_end_object(json);
   json_end_object(json);

   json_output = (u_char *) json_to_string(json);
   json_free(json);

   return json_output;
}