void emotions_Analyze(char* path, char* link){ //getting full path to .exe directory //requesting data from Emotion API using curl, saving JSON response to html char cmd[400]; char exportFileName[200]; sprintf(exportFileName, "%s\\emotions.json", path); char resultFileName[200]; sprintf(resultFileName, "%s\\emotions.txt", path); sprintf(cmd, "%s\\curl.exe -o %s -XPOST https://api.projectoxford.ai/emotion/v1.0/recognize?subscription-key=349f1c7ff1e642498d333cb2fe5fab25 -k --data-binary @%s -H \"Content-Type:application/octet-stream\"", path, exportFileName, link); system(cmd);// WARNING! NO UPDATE IF COMMENTED system("cls"); //parsing and structurizing JSON response, writing to stream FILE* fJSON = fopen(exportFileName, "r"); FILE* output = fopen(resultFileName, "w"); json_error_t error; json_t* root = json_loadf(fJSON, 0, &error); json_t* wrongURL = json_object_get(root, "error"); if (json_is_object(wrongURL)){ //failed to download photo by API fprintf(output, "%s", "@"); fprintf(stdout, "%s", "Download fail"); } else if (json_array_size(root) == 0){//no face detected fprintf(output, "%s", "#"); fprintf(stdout, "%s", "No faces on photo. Come back!"); } for (int i = 0; i < json_array_size(root); i++){ json_t* data = json_array_get(root, i); json_t* scores = json_object_get(data, "scores"); json_t* anger = json_object_get(scores, "anger"); json_t* contempt = json_object_get(scores, "contempt"); json_t* disgust = json_object_get(scores, "disgust"); json_t* fear = json_object_get(scores, "fear"); json_t* happiness = json_object_get(scores, "happiness"); json_t* neutral = json_object_get(scores, "neutral"); json_t* sadness = json_object_get(scores, "sadness"); json_t* surprise = json_object_get(scores, "surprise"); puts("Your emotions:\n"); fprintf(stdout, "Anger: %.3lf %%\n", json_real_value(anger)*100); fprintf(stdout, "Contempt: %.3lf %%\n", json_real_value(contempt) * 100); fprintf(stdout, "Disgust: %.3lf %%\n", json_real_value(disgust) * 100); fprintf(stdout, "Fear: %.3lf %%\n", json_real_value(fear) * 100); fprintf(stdout, "Happiness: %.3lf %%\n", json_real_value(happiness) * 100); fprintf(stdout, "Neutral: %.3lf %%\n", json_real_value(neutral) * 100); fprintf(stdout, "Sadness: %.3lf %%\n", json_real_value(sadness) * 100); fprintf(stdout, "Surprise: %.3lf %%\n", json_real_value(surprise)*100); fprintf(output, "%.15lf\n", json_real_value(anger)); fprintf(output, "%.15lf\n", json_real_value(contempt)); fprintf(output, "%.15lf\n", json_real_value(disgust)); fprintf(output, "%.15lf\n", json_real_value(fear)); fprintf(output, "%.15lf\n", json_real_value(happiness)); fprintf(output, "%.15lf\n", (json_real_value(neutral)>0.95) ? json_real_value(neutral):0.0); fprintf(output, "%.15lf\n", json_real_value(sadness)); fprintf(output, "%.15lf\n", json_real_value(surprise)); puts(""); } fclose(fJSON); fclose(output); }
char *test_function_with_expression() { spec_describe("function one expression: -> {\n1 + 1\n}"); FxP_ParserContext *context = parse_string("-> {\n1 + 1\n}\n"); setup_json(); element = json_object_get( json_object_get(expression, "function_definition"), "expressions"); assert_ints_equal(json_array_size(element), 1, "expressions size correct"); element = json_object_get( json_object_get( json_object_get( json_object_get( json_object_get( json_array_get( json_object_get( json_object_get(expression, "function_definition"), "expressions"), 0), "method_call"), "receiver"), "literal"), "bit"), "INTEGER"); assert_ints_equal(json_integer_value(element), 1, "first expression receiver correct"); element = json_object_get( json_object_get( json_object_get( json_object_get( json_object_get( json_array_get( json_object_get( json_object_get(expression, "function_definition"), "expressions"), 0), "method_call"), "message"), "lookup"), "bit"), "STRING"); assert_strings_equal(json_string_value(element), "+", "first expression message correct"); element = json_object_get( json_object_get( json_object_get( json_array_get( json_object_get( json_object_get( json_array_get( json_object_get( json_object_get(expression, "function_definition"), "expressions"), 0), "method_call"), "arguments"), 0), "literal"), "bit"), "INTEGER"); assert_ints_equal(json_integer_value(element), 1, "first expression arguments correct"); cleanup(); return NULL; }
/* * parse an RSA key from a JSON object in to a cjose JWK object */ static apr_byte_t oidc_jwk_parse_rsa_x5c(apr_pool_t *pool, json_t *json, cjose_jwk_t **jwk, oidc_jose_error_t *err) { apr_byte_t rv = FALSE; /* get the "x5c" array element from the JSON object */ json_t *v = json_object_get(json, "x5c"); if (v == NULL) { oidc_jose_error(err, "JSON key \"%s\" could not be found", "x5c"); return FALSE; } if (!json_is_array(v)) { oidc_jose_error(err, "JSON key \"%s\" was found but its value is not a JSON array", "x5c"); return FALSE; } /* take the first element of the array */ v = json_array_get(v, 0); if (v == NULL) { oidc_jose_error(err, "first element in JSON array is \"null\""); return FALSE; } if (!json_is_string(v)) { oidc_jose_error(err, "first element in array is not a JSON string"); return FALSE; } const char *s_x5c = json_string_value(v); /* PEM-format it */ const int len = 75; int i = 0; char *s = apr_psprintf(pool, "-----BEGIN CERTIFICATE-----\n"); while (i < strlen(s_x5c)) { s = apr_psprintf(pool, "%s%s\n", s, apr_pstrndup(pool, s_x5c + i, len)); i += len; } s = apr_psprintf(pool, "%s-----END CERTIFICATE-----\n", s); BIO *input = NULL; /* put it in BIO memory */ if ((input = BIO_new(BIO_s_mem())) == NULL) { oidc_jose_error_openssl(err, "memory allocation BIO_new/BIO_s_mem"); return FALSE; } if (BIO_puts(input, s) <= 0) { BIO_free(input); oidc_jose_error_openssl(err, "BIO_puts"); return FALSE; } /* do the actual parsing */ rv = oidc_jwk_rsa_bio_to_jwk(pool, input, jwk, FALSE, err); BIO_free(input); return rv; }
/* * based on an account name, perform OpenID Connect Provider Issuer Discovery to find out the issuer and obtain and store its metadata */ apr_byte_t oidc_proto_account_based_discovery(request_rec *r, oidc_cfg *cfg, const char *acct, char **issuer) { // TODO: maybe show intermediate/progress screen "discovering..." ap_log_rerror(APLOG_MARK, OIDC_DEBUG, 0, r, "oidc_proto_account_based_discovery: entering, acct=%s", acct); const char *resource = apr_psprintf(r->pool, "acct:%s", acct); const char *domain = strrchr(acct, '@'); if (domain == NULL) { ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "oidc_proto_account_based_discovery: invalid account name"); return FALSE; } domain++; const char *url = apr_psprintf(r->pool, "https://%s/.well-known/webfinger", domain); apr_table_t *params = apr_table_make(r->pool, 1); apr_table_addn(params, "resource", resource); apr_table_addn(params, "rel", "http://openid.net/specs/connect/1.0/issuer"); const char *response = NULL; if (oidc_util_http_call(r, url, OIDC_HTTP_GET, params, NULL, NULL, cfg->provider.ssl_validate_server, &response, cfg->http_timeout_short, cfg->outgoing_proxy) == FALSE) { /* errors will have been logged by now */ return FALSE; } /* decode and see if it is not an error response somehow */ json_t *j_response = NULL; if (oidc_util_decode_json_and_check_error(r, response, &j_response) == FALSE) return FALSE; /* get the links parameter */ json_t *j_links = json_object_get(j_response, "links"); if ((j_links == NULL) || (!json_is_array(j_links))) { ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "oidc_proto_account_based_discovery: response JSON object did not contain a \"links\" array"); json_decref(j_response); return FALSE; } /* get the one-and-only object in the "links" array */ json_t *j_object = json_array_get(j_links, 0); if ((j_object == NULL) || (!json_is_object(j_object))) { ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "oidc_proto_account_based_discovery: response JSON object did not contain a JSON object as the first element in the \"links\" array"); json_decref(j_response); return FALSE; } /* get the href from that object, which is the issuer value */ json_t *j_href = json_object_get(j_object, "href"); if ((j_href == NULL) || (!json_is_string(j_href))) { ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "oidc_proto_account_based_discovery: response JSON object did not contain a \"href\" element in the first \"links\" array object"); json_decref(j_response); return FALSE; } *issuer = apr_pstrdup(r->pool, json_string_value(j_href)); ap_log_rerror(APLOG_MARK, OIDC_DEBUG, 0, r, "oidc_proto_account_based_discovery: returning issuer \"%s\" for account \"%s\" after doing successful webfinger-based discovery", *issuer, acct); json_decref(j_response); return TRUE; }
static json_t *bunser_template(const char *buf, const char *end, json_int_t *used, json_error_t *jerr) { json_int_t needed = 0; json_int_t total = 0; json_int_t i, nelems; json_int_t ip, np; json_t *templ = NULL, *arrval, *ret = NULL; buf++; total++; if (*buf != BSER_ARRAY) { snprintf(jerr->text, sizeof(jerr->text), "Expected array encoding, but found 0x%02x", *buf); *used = total; return NULL; } // Load in the property names template templ = bunser_array(buf, end, &needed, jerr); if (!templ) { *used = needed + total; goto bail; } total += needed; buf += needed; // And the number of objects needed = 0; if (!bunser_int(buf, end - buf, &needed, &nelems)) { *used = needed + total; snprintf(jerr->text, sizeof(jerr->text), "invalid object number encoding (needed %d but have %d)", (int)needed, (int)(end - buf)); goto bail; } total += needed; buf += needed; np = json_array_size(templ); // Now load up the array with object values arrval = json_array_of_size((size_t)nelems); for (i = 0; i < nelems; i++) { json_t *item, *val; item = json_object_of_size((size_t)np); for (ip = 0; ip < np; ip++) { if (*buf == BSER_SKIP) { buf++; total++; continue; } needed = 0; val = bunser(buf, end, &needed, jerr); if (!val) { *used = needed + total; goto bail; } buf += needed; total += needed; json_object_set_new_nocheck(item, json_string_value(json_array_get(templ, (size_t)ip)), val); } json_array_append_new(arrval, item); } *used = total; ret = arrval; bail: json_decref(templ); return ret; }
void BookingApi::GetMinPrice(string const & hotelId, string const & currency, function<void(string const &, string const &)> const & fn) { char dateArrival[12]{}; char dateDeparture[12]{}; system_clock::time_point p = system_clock::from_time_t(time(nullptr)); tm arrival = my::GmTime(system_clock::to_time_t(p)); tm departure = my::GmTime(system_clock::to_time_t(p + hours(24))); strftime(dateArrival, sizeof(dateArrival), "%Y-%m-%d", &arrival); strftime(dateDeparture, sizeof(dateDeparture), "%Y-%m-%d", &departure); string url = MakeApiUrl("getHotelAvailability", {{"hotel_ids", hotelId}, {"currency_code", currency}, {"arrival_date", dateArrival}, {"departure_date", dateDeparture}}); auto const callback = [this, fn, currency](downloader::HttpRequest & answer) { string minPrice; string priceCurrency; try { my::Json root(answer.Data().c_str()); if (!json_is_array(root.get())) MYTHROW(my::Json::Exception, ("The answer must contain a json array.")); size_t const sz = json_array_size(root.get()); if (sz > 0) { // Read default hotel price and currency. auto obj = json_array_get(root.get(), 0); my::FromJSONObject(obj, "min_price", minPrice); my::FromJSONObject(obj, "currency_code", priceCurrency); // Try to get price in requested currency. if (!currency.empty() && priceCurrency != currency) { json_t * arr = json_object_get(obj, "other_currency"); if (arr && json_is_array(arr)) { size_t sz = json_array_size(arr); for (size_t i = 0; i < sz; ++i) { auto el = json_array_get(arr, i); string code; my::FromJSONObject(el, "currency_code", code); if (code == currency) { priceCurrency = code; my::FromJSONObject(el, "min_price", minPrice); break; } } } } } } catch (my::Json::Exception const & e) { LOG(LERROR, (e.Msg())); minPrice.clear(); priceCurrency.clear(); } fn(minPrice, priceCurrency); m_request.reset(); }; m_request.reset(downloader::HttpRequest::Get(url, callback)); }
Release * release_get_latest(void) { Release *release = NULL; const char *SERVER_URL = "https://api.github.com/repos/analogdevicesinc/iio-oscilloscope/releases"; json_t *j_root, *j_release; bool release_abort = false; j_root = decode_url_feedback(SERVER_URL); if (!j_root) { printf("Could not decode data about git releases\n"); goto cleanup_and_fail; } j_release = get_latest_release(j_root); if (!j_release) { printf("No release found\n"); goto cleanup_and_fail; } release = release_new(); if (!release) { printf("%s\n", strerror(errno)); goto cleanup_and_fail; } release->name = strdup(json_string_value(json_object_get(j_release, "name"))); release->build_date = strdup(json_string_value(json_object_get(j_release, "created_at"))); release->url = strdup(json_string_value(json_object_get(j_release, "html_url"))); /* Get the release SHA commit */ json_t *j_tags, *tag, *name, *commit; const char *tag_name; size_t i; tag_name = json_string_value(json_object_get(j_release, "tag_name")); j_tags = decode_url_feedback("https://api.github.com/repos/analogdevicesinc/iio-oscilloscope/tags"); if (!j_tags) { printf("Could not decode data about git tags\n"); release_abort = true; goto cleanup_and_fail; } for (i = 0; i < json_array_size(j_tags); i++) { tag = json_array_get(j_tags, i); if (!json_is_object(tag)) break; name = json_object_get(tag, "name"); if (!json_is_string(name)) break; if (tag_name && !strcmp(json_string_value(name), tag_name)) { commit = json_object_get(tag, "commit"); if (!json_is_object(commit)) break; release->commit = strdup(json_string_value( json_object_get(commit, "sha"))); } } if (!release->commit) { printf("Could not find release SHA commit\n"); release_abort = true; goto cleanup_and_fail; } /* Get download link for the windows build */ json_t *assets, *w_build; assets = json_object_get(j_release, "assets"); if (!assets) { release_abort = true; goto cleanup_and_fail; } w_build = json_array_get(assets, 0); if (!w_build) { release_abort = true; goto cleanup_and_fail; } release->windows_dld_url = strdup(json_string_value(json_object_get(w_build, "browser_download_url"))); cleanup_and_fail: if (release_abort) { release_dispose(release); release = NULL; } json_decref(j_root); return release; }
janus_turnrest_response *janus_turnrest_request(void) { janus_mutex_lock(&api_mutex); if(api_server == NULL) { janus_mutex_unlock(&api_mutex); return NULL; } /* Prepare the request URI */ char query_string[512]; g_snprintf(query_string, 512, "service=turn"); if(api_key != NULL) { char buffer[256]; g_snprintf(buffer, 256, "&api=%s", api_key); g_strlcat(query_string, buffer, 512); } char request_uri[1024]; g_snprintf(request_uri, 1024, "%s?%s", api_server, query_string); JANUS_LOG(LOG_VERB, "Sending request: %s\n", request_uri); janus_mutex_unlock(&api_mutex); /* Prepare the libcurl context */ CURLcode res; CURL *curl = curl_easy_init(); if(curl == NULL) { JANUS_LOG(LOG_ERR, "libcurl error\n"); return NULL; } curl_easy_setopt(curl, CURLOPT_URL, request_uri); curl_easy_setopt(curl, CURLOPT_POST, 1); /* FIXME Some servers don't like a POST with no data */ curl_easy_setopt(curl, CURLOPT_POSTFIELDS, query_string); curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10L); /* FIXME Max 10 seconds */ /* For getting data, we use an helper struct and the libcurl callback */ janus_turnrest_buffer data; data.buffer = malloc(1); data.size = 0; curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, janus_turnrest_callback); curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&data); curl_easy_setopt(curl, CURLOPT_USERAGENT, "Janus/1.0"); /* Send the request */ res = curl_easy_perform(curl); if(res != CURLE_OK) { JANUS_LOG(LOG_ERR, "Couldn't send the request: %s\n", curl_easy_strerror(res)); free(data.buffer); curl_easy_cleanup(curl); return NULL; } /* Cleanup the libcurl context */ curl_easy_cleanup(curl); /* Process the response */ JANUS_LOG(LOG_VERB, "Got %zu bytes from the TURN REST API server\n", data.size); JANUS_LOG(LOG_VERB, "%s\n", data.buffer); json_error_t error; json_t *root = json_loads(data.buffer, 0, &error); if(!root) { JANUS_LOG(LOG_ERR, "Couldn't parse response: error on line %d: %s", error.line, error.text); free(data.buffer); return NULL; } free(data.buffer); json_t *username = json_object_get(root, "username"); if(!username) { JANUS_LOG(LOG_ERR, "Invalid response: missing username\n"); return NULL; } if(!json_is_string(username)) { JANUS_LOG(LOG_ERR, "Invalid response: username should be a string\n"); return NULL; } json_t *password = json_object_get(root, "password"); if(!password) { JANUS_LOG(LOG_ERR, "Invalid response: missing password\n"); return NULL; } if(!json_is_string(password)) { JANUS_LOG(LOG_ERR, "Invalid response: password should be a string\n"); return NULL; } json_t *ttl = json_object_get(root, "ttl"); if(ttl && !json_is_integer(ttl)) { JANUS_LOG(LOG_ERR, "Invalid response: ttl should be an integer\n"); return NULL; } json_t *uris = json_object_get(root, "uris"); if(!uris) { JANUS_LOG(LOG_ERR, "Invalid response: missing uris\n"); return NULL; } if(!json_is_array(uris) || json_array_size(uris) == 0) { JANUS_LOG(LOG_ERR, "Invalid response: uris should be a non-empty array\n"); return NULL; } /* Turn the response into a janus_turnrest_response object we can use */ janus_turnrest_response *response = calloc(1, sizeof(janus_turnrest_response)); response->username = g_strdup(json_string_value(username)); response->password = g_strdup(json_string_value(password)); response->ttl = ttl ? json_integer_value(ttl) : 0; response->servers = NULL; size_t i = 0; for(i=0; i<json_array_size(uris); i++) { json_t *uri = json_array_get(uris, i); if(uri == NULL || !json_is_string(uri)) { JANUS_LOG(LOG_WARN, "Skipping invalid TURN URI (not a string)...\n"); continue; } const char *turn_uri = json_string_value(uri); if(strstr(turn_uri, "turn:") != turn_uri && strstr(turn_uri, "turns:") != turn_uri) { JANUS_LOG(LOG_WARN, "Skipping invalid TURN URI '%s' (not a TURN URI)...\n", turn_uri); continue; } janus_turnrest_instance *instance = calloc(1, sizeof(janus_turnrest_instance)); instance->transport = NICE_RELAY_TYPE_TURN_UDP; if(strstr(turn_uri, "turns:") == turn_uri) instance->transport = NICE_RELAY_TYPE_TURN_TLS; else if(strstr(turn_uri, "transport=tcp") == turn_uri) instance->transport = NICE_RELAY_TYPE_TURN_TCP; gchar **parts = NULL; if(strstr(turn_uri, "?") != NULL) { parts = g_strsplit(turn_uri, "?", -1); turn_uri = parts[0]; } gchar **uri_parts = g_strsplit(turn_uri, ":", -1); /* Resolve the TURN URI address */ struct hostent *he = gethostbyname(uri_parts[1]); if(he == NULL) { JANUS_LOG(LOG_WARN, "Skipping invalid TURN URI '%s' (could not resolve the address)...\n", uri_parts[1]); g_strfreev(uri_parts); continue; } struct in_addr **addr_list = (struct in_addr **)he->h_addr_list; if(addr_list[0] == NULL) { JANUS_LOG(LOG_WARN, "Skipping invalid TURN URI '%s' (could not resolve the address)...\n", uri_parts[1]); g_strfreev(uri_parts); continue; } instance->server = g_strdup(inet_ntoa(*addr_list[0])); if(uri_parts[2] == NULL) { /* No port? USe 3478 by default */ instance->port = 3478; } else { instance->port = atoi(uri_parts[2]); } g_strfreev(uri_parts); g_strfreev(parts); /* Add the server to the list */ response->servers = g_list_append(response->servers, instance); } if(response->servers == NULL) { JANUS_LOG(LOG_ERR, "Couldn't find any valid TURN URI in the response...\n"); janus_turnrest_response_destroy(response); return NULL; } /* Done */ return response; }
// Given a json array, concat the elements using a delimiter w_string_t *w_string_implode(json_t *arr, const char *delim) { uint32_t delim_len = u32_strlen(delim); uint32_t len = 0; uint32_t i; w_string_t *s; char *buf; if (json_array_size(arr) == 0) { return w_string_new(""); } if (json_array_size(arr) == 1) { return w_string_new(json_string_value(json_array_get(arr, 0))); } len = ((uint32_t)json_array_size(arr) - 1) * delim_len; for (i = 0; i < json_array_size(arr); i++) { const char *str; str = json_string_value(json_array_get(arr, i)); len += u32_strlen(str); } s = malloc(sizeof(*s) + len + 1); if (!s) { perror("no memory available"); abort(); } s->refcnt = 1; s->slice = NULL; buf = (char*)(s + 1); s->buf = buf; for (i = 0; i < json_array_size(arr); i++) { const char *str; uint32_t l; str = json_string_value(json_array_get(arr, i)); l = u32_strlen(str); memcpy(buf, str, l); // Final string doesn't want delimiter after it if (i == json_array_size(arr) - 1) { buf += l; break; } memcpy(buf + l, delim, delim_len); buf += l + delim_len; } *buf = '\0'; s->len = (uint32_t)(buf - s->buf); s->hval = w_hash_bytes(s->buf, s->len, 0); return s; }
void PerfLogThread::loop() { json_ref samples; char **envp; json_ref perf_cmd; int64_t sample_batch; w_set_thread_name("perflog"); // Prep some things that we'll need each time we run a command { uint32_t env_size; auto envpht = w_envp_make_ht(); char *statedir = dirname(strdup(watchman_state_file)); w_envp_set_cstring(envpht, "WATCHMAN_STATE_DIR", statedir); w_envp_set_cstring(envpht, "WATCHMAN_SOCK", get_sock_name()); envp = w_envp_make_from_ht(envpht, &env_size); } perf_cmd = cfg_get_json("perf_logger_command"); if (json_is_string(perf_cmd)) { perf_cmd = json_array({perf_cmd}); } if (!json_is_array(perf_cmd)) { w_log( W_LOG_FATAL, "perf_logger_command must be either a string or an array of strings\n"); } sample_batch = cfg_get_int("perf_logger_command_max_samples_per_call", 4); while (!w_is_stopping()) { { auto wlock = samples_.wlock(); if (!*wlock) { cond_.wait(wlock.getUniqueLock()); } samples = nullptr; std::swap(samples, *wlock); } if (samples) { while (json_array_size(samples) > 0) { int i = 0; auto cmd = json_array(); posix_spawnattr_t attr; posix_spawn_file_actions_t actions; pid_t pid; char **argv = NULL; json_array_extend(cmd, perf_cmd); while (i < sample_batch && json_array_size(samples) > 0) { char *stringy = json_dumps(json_array_get(samples, 0), 0); if (stringy) { json_array_append_new( cmd, typed_string_to_json(stringy, W_STRING_MIXED)); free(stringy); } json_array_remove(samples, 0); i++; } argv = w_argv_copy_from_json(cmd, 0); if (!argv) { char *dumped = json_dumps(cmd, 0); w_log(W_LOG_FATAL, "error converting %s to an argv array\n", dumped); } posix_spawnattr_init(&attr); #ifdef POSIX_SPAWN_CLOEXEC_DEFAULT posix_spawnattr_setflags(&attr, POSIX_SPAWN_CLOEXEC_DEFAULT); #endif posix_spawn_file_actions_init(&actions); posix_spawn_file_actions_addopen(&actions, STDIN_FILENO, "/dev/null", O_RDONLY, 0666); posix_spawn_file_actions_addopen(&actions, STDOUT_FILENO, "/dev/null", O_WRONLY, 0666); posix_spawn_file_actions_addopen(&actions, STDERR_FILENO, "/dev/null", O_WRONLY, 0666); if (posix_spawnp(&pid, argv[0], &actions, &attr, argv, envp) == 0) { int status; while (waitpid(pid, &status, 0) != pid) { if (errno != EINTR) { break; } } } else { int err = errno; w_log(W_LOG_ERR, "failed to spawn %s: %s\n", argv[0], strerror(err)); } posix_spawnattr_destroy(&attr); posix_spawn_file_actions_destroy(&actions); free(argv); } } } }
static void spawn_command(w_root_t *root, struct watchman_trigger_command *cmd, w_query_res *res, struct w_clockspec *since_spec) { char **envp = NULL; uint32_t i = 0; int ret; w_stm_t stdin_file = NULL; json_t *args; char **argv = NULL; uint32_t env_size; posix_spawn_file_actions_t actions; posix_spawnattr_t attr; #ifndef _WIN32 sigset_t mask; #endif long arg_max; size_t argspace_remaining; bool file_overflow = false; int result_log_level; char clockbuf[128]; w_string_t *working_dir = NULL; #ifdef _WIN32 arg_max = 32*1024; #else arg_max = sysconf(_SC_ARG_MAX); #endif if (arg_max <= 0) { argspace_remaining = UINT_MAX; } else { argspace_remaining = (uint32_t)arg_max; } // Allow some misc working overhead argspace_remaining -= 32; stdin_file = prepare_stdin(cmd, res); if (!stdin_file) { w_log(W_LOG_ERR, "trigger %.*s:%s %s\n", (int)root->root_path->len, root->root_path->buf, cmd->triggername->buf, strerror(errno)); return; } // Assumption: that only one thread will be executing on a given // cmd instance so that mutation of cmd->envht is safe. // This is guaranteed in the current architecture. if (cmd->max_files_stdin > 0 && res->num_results > cmd->max_files_stdin) { file_overflow = true; } // It is way too much of a hassle to try to recreate the clock value if it's // not a relative clock spec, and it's only going to happen on the first run // anyway, so just skip doing that entirely. if (since_spec && since_spec->tag == w_cs_clock && clock_id_string(since_spec->clock.root_number, since_spec->clock.ticks, clockbuf, sizeof(clockbuf))) { w_envp_set_cstring(cmd->envht, "WATCHMAN_SINCE", clockbuf); } else { w_envp_unset(cmd->envht, "WATCHMAN_SINCE"); } if (clock_id_string(res->root_number, res->ticks, clockbuf, sizeof(clockbuf))) { w_envp_set_cstring(cmd->envht, "WATCHMAN_CLOCK", clockbuf); } else { w_envp_unset(cmd->envht, "WATCHMAN_CLOCK"); } if (cmd->query->relative_root) { w_envp_set(cmd->envht, "WATCHMAN_RELATIVE_ROOT", cmd->query->relative_root); } else { w_envp_unset(cmd->envht, "WATCHMAN_RELATIVE_ROOT"); } // Compute args args = json_deep_copy(cmd->command); if (cmd->append_files) { // Measure how much space the base args take up for (i = 0; i < json_array_size(args); i++) { const char *ele = json_string_value(json_array_get(args, i)); argspace_remaining -= strlen(ele) + 1 + sizeof(char*); } // Dry run with env to compute space envp = w_envp_make_from_ht(cmd->envht, &env_size); free(envp); envp = NULL; argspace_remaining -= env_size; for (i = 0; i < res->num_results; i++) { // also: NUL terminator and entry in argv uint32_t size = res->results[i].relname->len + 1 + sizeof(char*); if (argspace_remaining < size) { file_overflow = true; break; } argspace_remaining -= size; json_array_append_new( args, json_string_nocheck(res->results[i].relname->buf) ); } } argv = w_argv_copy_from_json(args, 0); json_decref(args); args = NULL; w_envp_set_bool(cmd->envht, "WATCHMAN_FILES_OVERFLOW", file_overflow); envp = w_envp_make_from_ht(cmd->envht, &env_size); posix_spawnattr_init(&attr); #ifndef _WIN32 sigemptyset(&mask); posix_spawnattr_setsigmask(&attr, &mask); #endif posix_spawnattr_setflags(&attr, POSIX_SPAWN_SETSIGMASK| #ifdef POSIX_SPAWN_CLOEXEC_DEFAULT // Darwin: close everything except what we put in file actions POSIX_SPAWN_CLOEXEC_DEFAULT| #endif POSIX_SPAWN_SETPGROUP); posix_spawn_file_actions_init(&actions); #ifndef _WIN32 posix_spawn_file_actions_adddup2(&actions, w_stm_fileno(stdin_file), STDIN_FILENO); #else posix_spawn_file_actions_adddup2_handle_np(&actions, w_stm_handle(stdin_file), STDIN_FILENO); #endif if (cmd->stdout_name) { posix_spawn_file_actions_addopen(&actions, STDOUT_FILENO, cmd->stdout_name, cmd->stdout_flags, 0666); } else { posix_spawn_file_actions_adddup2(&actions, STDOUT_FILENO, STDOUT_FILENO); } if (cmd->stderr_name) { posix_spawn_file_actions_addopen(&actions, STDERR_FILENO, cmd->stderr_name, cmd->stderr_flags, 0666); } else { posix_spawn_file_actions_adddup2(&actions, STDERR_FILENO, STDERR_FILENO); } // Figure out the appropriate cwd { const char *cwd = NULL; working_dir = NULL; if (cmd->query->relative_root) { working_dir = cmd->query->relative_root; } else { working_dir = root->root_path; } w_string_addref(working_dir); json_unpack(cmd->definition, "{s:s}", "chdir", &cwd); if (cwd) { w_string_t *cwd_str = w_string_new(cwd); if (w_is_path_absolute(cwd)) { w_string_delref(working_dir); working_dir = cwd_str; } else { w_string_t *joined; joined = w_string_path_cat(working_dir, cwd_str); w_string_delref(cwd_str); w_string_delref(working_dir); working_dir = joined; } } w_log(W_LOG_DBG, "using %.*s for working dir\n", working_dir->len, working_dir->buf); } pthread_mutex_lock(&spawn_lock); #ifndef _WIN32 ignore_result(chdir(working_dir->buf)); #else posix_spawnattr_setcwd_np(&attr, working_dir->buf); #endif w_string_delref(working_dir); working_dir = NULL; ret = posix_spawnp(&cmd->current_proc, argv[0], &actions, &attr, argv, envp); if (ret == 0) { w_root_addref(root); insert_running_pid(cmd->current_proc, root); } else { // On Darwin (at least), posix_spawn can fail but will still populate the // pid. Since we use the pid to gate future spawns, we need to ensure // that we clear out the pid on failure, otherwise the trigger would be // effectively disabled for the rest of the watch lifetime cmd->current_proc = 0; } #ifndef _WIN32 ignore_result(chdir("/")); #endif pthread_mutex_unlock(&spawn_lock); // If failed, we want to make sure we log enough info to figure out why result_log_level = res == 0 ? W_LOG_DBG : W_LOG_ERR; w_log(result_log_level, "posix_spawnp:\n"); for (i = 0; argv[i]; i++) { w_log(result_log_level, "argv[%d] %s\n", i, argv[i]); } for (i = 0; envp[i]; i++) { w_log(result_log_level, "envp[%d] %s\n", i, envp[i]); } w_log(result_log_level, "trigger %.*s:%s pid=%d ret=%d %s\n", (int)root->root_path->len, root->root_path->buf, cmd->triggername->buf, (int)cmd->current_proc, ret, strerror(ret)); free(argv); free(envp); posix_spawnattr_destroy(&attr); posix_spawn_file_actions_destroy(&actions); if (stdin_file) { w_stm_close(stdin_file); } }
/* since /root <timestamp> [patterns] */ static void cmd_since(struct watchman_client *client, json_t *args) { const char *clockspec; w_root_t *root; w_query *query; char *errmsg = NULL; struct w_query_field_list field_list; w_query_res res; json_t *response, *clock_ele; json_t *file_list; char clockbuf[128]; /* resolve the root */ if (json_array_size(args) < 3) { send_error_response(client, "not enough arguments for 'since'"); return; } root = resolve_root_or_err(client, args, 1, false); if (!root) { return; } clock_ele = json_array_get(args, 2); clockspec = json_string_value(clock_ele); if (!clockspec) { send_error_response(client, "expected argument 2 to be a valid clockspec"); w_root_delref(root); return; } query = w_query_parse_legacy(args, &errmsg, 3, NULL, clockspec, NULL); if (errmsg) { send_error_response(client, "%s", errmsg); free(errmsg); w_root_delref(root); return; } w_query_legacy_field_list(&field_list); if (!w_query_execute(query, root, &res, NULL, NULL)) { send_error_response(client, "query failed: %s", res.errmsg); w_query_result_free(&res); w_root_delref(root); w_query_delref(query); return; } w_query_delref(query); file_list = w_query_results_to_json(&field_list, res.num_results, res.results); w_query_result_free(&res); response = make_response(); if (clock_id_string(res.root_number, res.ticks, clockbuf, sizeof(clockbuf))) { set_prop(response, "clock", json_string_nocheck(clockbuf)); } set_prop(response, "is_fresh_instance", json_pack("b", res.is_fresh_instance)); set_prop(response, "files", file_list); send_and_dispose_response(client, response); w_root_delref(root); }
static int unpack_array(scanner_t *s, json_t *root, va_list *ap) { size_t i = 0; int strict = 0; if(root && !json_is_array(root)) { set_error(s, "<validation>", "Expected array, got %s", type_name(root)); return -1; } next_token(s); while(s->token != ']') { json_t *value; if(strict != 0) { set_error(s, "<format>", "Expected ']' after '%c', got '%c'", (strict == 1 ? '!' : '*'), s->token); return -1; } if(!s->token) { set_error(s, "<format>", "Unexpected end of format string"); return -1; } if(s->token == '!' || s->token == '*') { strict = (s->token == '!' ? 1 : -1); next_token(s); continue; } if(!strchr(unpack_value_starters, s->token)) { set_error(s, "<format>", "Unexpected format character '%c'", s->token); return -1; } if(!root) { /* skipping */ value = NULL; } else { value = json_array_get(root, i); if(!value) { set_error(s, "<validation>", "Array index %lu out of range", (unsigned long)i); return -1; } } if(unpack(s, value, ap)) return -1; next_token(s); i++; } if(strict == 0 && (s->flags & JSON_STRICT)) strict = 1; if(root && strict == 1 && i != json_array_size(root)) { long diff = (long)json_array_size(root) - (long)i; set_error(s, "<validation>", "%li array item(s) left unpacked", diff); return -1; } return 0; }
qeo_mgmt_client_retcode_t qeo_mgmt_json_util_parseGetFWDMessage(const char* data, ssize_t length, qeo_mgmt_client_forwarder_cb callback, void* cookie) { json_error_t json_error = {0}; qeo_mgmt_client_retcode_t result = QMGMTCLIENT_EBADREPLY; json_t* message = json_loadb(data, length, JSON_REJECT_DUPLICATES, &json_error); qeo_mgmt_client_forwarder_t* fwd = NULL; do { json_t* fwdArray; ssize_t fwdSize; ssize_t i; if (message == NULL) { qeo_log_w("Failed to parse json message %s (%s:%d:%d)", json_error.text, json_error.source, json_error.line, json_error.column); qeo_log_w("Data = (%s)", data); //JSON parsing error break; } if (!json_is_object(message)) { qeo_log_w("invalid message received - top level is not a JSON object"); break; } fwdArray = json_object_get(message, "forwarders"); if (fwdArray == NULL || !json_is_array(fwdArray)) { qeo_log_w("root object did not contain a field 'forwarders' of type array (%p)", fwdArray); } fwdSize = json_array_size(fwdArray); qeo_log_i("Found an array of %d forwarder(s) in message\n", fwdSize); for (i = 0; i < fwdSize; i++) { qeo_mgmt_client_retcode_t cb_result; json_t* fwdObject = json_array_get(fwdArray, i); json_t* idString; json_t* locatorArray; ssize_t nrOfLocators; ssize_t j; if (!json_is_object(fwdObject)) { qeo_log_w("unexpected content in fwdArray - object expected"); break; } idString = json_object_get(fwdObject, "id"); if (idString == NULL || !json_is_string(idString)) { qeo_log_w("forwarder object did not contain a string field called 'id' (%p)", idString); break; } locatorArray = json_object_get(fwdObject, "locators"); if (locatorArray == NULL || !json_is_array(locatorArray)) { qeo_log_w("forwarder object did not contain an array field called 'nrOfLocators' (%p)", locatorArray); break; } nrOfLocators = json_array_size(locatorArray); qeo_log_i("found forwarder with id='%s' and %d locator(s)", json_string_value(idString), nrOfLocators); fwd = malloc(sizeof(qeo_mgmt_client_forwarder_t)); if (fwd == NULL) { qeo_log_w("fwd == NULL"); result = QMGMTCLIENT_EMEM; break; } fwd->nrOfLocators = nrOfLocators; fwd->locators = calloc(nrOfLocators, sizeof(qeo_mgmt_client_locator_t)); fwd->deviceID = qeo_mgmt_util_hex_to_int(json_string_value(idString)); if (fwd->deviceID == -1){ qeo_log_w("Invalid device id inside json message"); break; } if (fwd->locators == NULL) { qeo_log_w("fwd->locators == NULL"); result = QMGMTCLIENT_EMEM; break; } for (j = 0; j < nrOfLocators; j++) { json_t* endpointObj = json_array_get(locatorArray, j); json_t* typeString = json_object_get(endpointObj,"type"); json_t* addrString = json_object_get(endpointObj,"address"); json_t* portInt = json_object_get(endpointObj,"port"); if (portInt == NULL || !json_is_integer(portInt)) { qeo_log_w("locator object did not contain a integer field called 'port' (%p)", portInt); break; } if (addrString == NULL || !json_is_string(addrString)) { qeo_log_w("locator object did not contain a string field called 'address' (%p)", addrString); break; } if (typeString == NULL || !json_is_string(typeString)) { qeo_log_w("locator object did not contain a string field called 'type' (%p)", typeString); break; } qeo_log_i("locator object %d = {type = '%s', address = '%s', port = %d}", j, json_string_value(typeString), json_string_value(addrString), (int) json_integer_value(portInt)); //valid locator fwd->locators[j].port = (int) json_integer_value(portInt); if (fwd->locators[j].port < -1 || fwd->locators[j].port > 0xffff){ qeo_log_w("Invalid port inside locator"); break; } fwd->locators[j].type = _get_locator_type(json_string_value(typeString)); fwd->locators[j].address = strdup(json_string_value(addrString)); //check value; don't forget to free! if (fwd->locators[j].address == NULL) { qeo_log_w("locator->address == NULL"); break; } } if (j != nrOfLocators){ break; } cb_result = callback(fwd, cookie); fwd = NULL; //pointer is handed over; set it to NULL so we wont free it. if (cb_result != QMGMTCLIENT_OK) {//the callback reports an error abort. result = cb_result; break; } } if (i != fwdSize){ break; } qeo_log_i("Successfully walked JSON object tree..."); result = QMGMTCLIENT_OK; } while(0); if (message) { json_decref(message); } if (fwd) { //if an error occurred, then the 'fwd' is not freed. qeo_mgmt_client_free_forwarder(fwd); } return result; }
/** * in case of simulation extend data or reset n_obs to take into * account opts->end ISO 8601 date */ void ssm_data_adapt_to_simul(ssm_data_t *data, json_t *jdata, ssm_nav_t *nav, ssm_options_t *opts) { int i, n; if(strcmp("", opts->end)==0){ return; } struct tm tm_start_date_t0; memset(&tm_start_date_t0, 0, sizeof(struct tm)); strptime(data->date_t0, "%Y-%m-%d", &tm_start_date_t0); time_t t_start_date_t0 = timegm(&tm_start_date_t0); struct tm tm_end; memset(&tm_end, 0, sizeof(struct tm)); strptime(opts->end, "%Y-%m-%d", &tm_end); time_t t_end = timegm(&tm_end); double delta_date_t0 = difftime(t_end, t_start_date_t0)/(24.0*60.0*60.0); if(delta_date_t0 < 0.0){ ssm_print_err("end date is before t0"); exit(EXIT_FAILURE); } unsigned int time_start; struct tm tm_start; memset(&tm_start, 0, sizeof(struct tm)); time_t t_start; if(data->length){ strptime(data->rows[data->length-1]->date, "%Y-%m-%d", &tm_start); time_start = data->rows[data->length-1]->time; t_start = timegm(&tm_start); } else { t_start = t_start_date_t0; } double delta = difftime(t_end, t_start)/(24.0*60.0*60.0); if(delta < 0.0){ //there are data but t_end before the last data point. In this case we just adjuts n_obs and n_obs_nonan if(data->length){ data->n_obs = 0; data->n_obs_nonan = 0; while(data->rows[data->n_obs]->time <= delta_date_t0){ if(data->rows[data->n_obs]->ts_nonan_length){ data->n_obs_nonan += 1; } data->n_obs += 1; } //safety data->n_obs = GSL_MIN(data->n_obs, data->length); data->n_obs_nonan = GSL_MIN(data->n_obs, data->n_obs_nonan); return; } ssm_print_err("end date is before t0"); exit(EXIT_FAILURE); } int n_extra = (int) ceil(delta / (double) opts->freq); if(n_extra){ int offset = data->length; data->length += n_extra; data->n_obs += n_extra; ssm_row_t **rows = realloc(data->rows, data->length * sizeof (ssm_row_t *)); if (rows!=NULL) { data->rows = rows; } else { ssm_print_err("could not re-allocate memory for ssm_data_t rows"); exit(EXIT_FAILURE); } time_t t = t_start; char iso_8601[] = "YYYY-MM-DD"; double one_day_in_sec = 24.0*60.0*60.0; int inc = opts->freq * 24*60*60; json_t *jreset_all = json_object_get(jdata, "reset_all"); int states_reset_length = json_array_size(jreset_all); for(n=0; n<n_extra; n++){ ssm_row_t *row = malloc(sizeof (ssm_row_t)); if (row == NULL) { ssm_print_err("Allocation impossible for ssm_data_row_t *"); exit(EXIT_FAILURE); } t += inc; struct tm *tm; tm = gmtime(&t); strftime(iso_8601, sizeof(iso_8601), "%Y-%m-%d", tm); row->date = strdup(iso_8601); row->time = time_start + (unsigned int) difftime(t, t_start)/one_day_in_sec; row->ts_nonan_length = 0; row->states_reset_length = states_reset_length; row->states_reset = malloc(states_reset_length * sizeof (ssm_state_t *)); if (row->states_reset == NULL) { ssm_print_err("Allocation impossible for ssm_data_row_t.states_reset"); exit(EXIT_FAILURE); } for(i=0; i<row->states_reset_length; i++){ json_t *jreset_all_i = json_array_get(jreset_all, i); if(json_is_number(jreset_all_i)) { int id = json_integer_value(jreset_all_i); row->states_reset[i] = nav->states[id]; } else { char str[SSM_STR_BUFFSIZE]; snprintf(str, SSM_STR_BUFFSIZE, "error: reset_all[%d] is not an integer\n", i); ssm_print_err(str); exit(EXIT_FAILURE); } } row->values = NULL; data->rows[offset+n] = row; } } }
void pcm_json_rule_to_seccomp(json_t *rule) { json_t *syscall, *action, *restrictions; int rc; int syscall_num; int arg_count; int i; int default_action; struct scmp_arg_cmp arg_cmp[ARG_COUNT_MAX]; syscall = json_object_get(rule, "syscall"); if(! json_is_string(syscall)) { errx(EXIT_FAILURE, "Expected string for system call"); } action = json_object_get(rule, "action"); if(! json_is_string(action)) { errx(EXIT_FAILURE, "Expected string for action value"); } default_action = pcm_string_to_policy(json_string_value(action)); if(default_action == -1) { errx(EXIT_FAILURE, "Rule action invalid (expecting ALLOW, KILL, or ERRNO)"); } syscall_num = seccomp_syscall_resolve_name(json_string_value(syscall)); if(syscall_num == __NR_SCMP_ERROR) { errx(EXIT_FAILURE, "System call number is negative?"); } arg_count = 0; restrictions = json_object_get(rule, "restrictions"); if(restrictions) { if(! json_is_array(restrictions)) { errx(EXIT_FAILURE, "restrictions is not an array"); } if(json_array_size(restrictions) > ARG_COUNT_MAX) { errx(EXIT_FAILURE, "too many restrictions :/"); } for(i = 0; i < json_array_size(restrictions); i++) { json_t *arg_restriction; arg_restriction = json_array_get(restrictions, i); if(! arg_restriction) { errx(EXIT_FAILURE, "fetching entry from array failed?"); } if(! json_is_object(arg_restriction)) { errx(EXIT_FAILURE, "argument restriction is not an object"); } pcm_json_restriction_to_arg_cmp(arg_restriction, &arg_cmp[i]); } arg_count = i; } rc = seccomp_rule_add_array(PCM_GLOBAL.seccomp, default_action, syscall_num, arg_count, arg_cmp); if(rc < 0) { errx(EXIT_FAILURE, "Adding rule failed?"); } }
ssm_calc_t *ssm_calc_new(json_t *jdata, ssm_nav_t *nav, ssm_data_t *data, ssm_fitness_t *fitness, ssm_options_t *opts, int thread_id) { ssm_calc_t *calc = malloc(sizeof (ssm_calc_t)); if (calc==NULL) { char str[SSM_STR_BUFFSIZE]; snprintf(str, SSM_STR_BUFFSIZE, "Allocation impossible for ssm_calc_t (thread_id: %d)", thread_id); ssm_print_err(str); exit(EXIT_FAILURE); } /***********/ /* threads */ /***********/ calc->threads_length = ssm_sanitize_n_threads(opts->n_thread, fitness); calc->thread_id = thread_id; /******************/ /* random numbers */ /******************/ // random number generator and parallel MC simulations: // // idea using one different seed per thread but is it realy uncorelated ??? // Should I go through the trouble of changing from GSL to SPRNG???? // answer: // I would recommend using ranlxd. The seeds should give 2^31 // effectively independent streams of length 10^171. A discussion of the // seeding procedure can be found in the file notes.ps at // http://www.briangough.ukfsn.org/ranlux_2.2/ // -- // Brian Gough // // => we create as many rng as parallel threads *but* note that for // the operations not prarallelized, we always use // cacl[0].randgsl const gsl_rng_type *Type; if (calc->threads_length == 1){ //we don't need a rng supporting parallel computing, we use mt19937 that is way faster than ranlxs0 (1754 k ints/sec vs 565 k ints/sec) Type = gsl_rng_mt19937; /*MT19937 generator of Makoto Matsumoto and Takuji Nishimura*/ } else { Type = gsl_rng_ranlxs0; //gsl_rng_ranlxs2 is better than gsl_rng_ranlxs0 but 2 times slower } unsigned long int seed; if(opts->flag_seed_time){ seed = (unsigned) time(NULL); } else{ seed=2; } calc->seed = seed + opts->id; /*we ensure uniqueness of seed in case of parrallel runs*/ calc->randgsl = gsl_rng_alloc(Type); gsl_rng_set(calc->randgsl, calc->seed + thread_id); /*******************/ /* implementations */ /*******************/ int dim = _ssm_dim_X(nav); if (nav->implementation == SSM_ODE || nav->implementation == SSM_EKF){ calc->T = gsl_odeiv2_step_rkf45; calc->control = gsl_odeiv2_control_y_new(opts->eps_abs, opts->eps_rel); calc->step = gsl_odeiv2_step_alloc(calc->T, dim); calc->evolve = gsl_odeiv2_evolve_alloc(dim); (calc->sys).function = (nav->implementation == SSM_ODE) ? &ssm_step_ode: &ssm_step_ekf; (calc->sys).jacobian = NULL; (calc->sys).dimension= dim; (calc->sys).params= calc; if(nav->implementation == SSM_EKF){ int can_run; if ( (nav->noises_off & (SSM_NO_DEM_STO)) && (nav->noises_off & (SSM_NO_WHITE_NOISE)) ) { calc->eval_Q = &ssm_eval_Q_no_dem_sto_no_env_sto; can_run = 0; } else if ((nav->noises_off & SSM_NO_DEM_STO) && !(nav->noises_off & SSM_NO_WHITE_NOISE)) { calc->eval_Q = &ssm_eval_Q_no_dem_sto; can_run = nav->par_noise->length; } else if (!(nav->noises_off & SSM_NO_DEM_STO) && (nav->noises_off & SSM_NO_WHITE_NOISE)) { calc->eval_Q = &ssm_eval_Q_no_env_sto; can_run = 1; } else { calc->eval_Q = &ssm_eval_Q_full; can_run = 1; } if(!(nav->noises_off & SSM_NO_DIFF)){ can_run += nav->states_diff->length; } if(!can_run){ ssm_print_err("Kalman methods must be used with at least one source of stochasticity in the process."); exit(EXIT_FAILURE); } int n_s = nav->states_sv_inc->length + nav->states_diff->length; int n_o = nav->observed_length; calc->_pred_error = gsl_vector_calloc(n_o); calc->_zero = gsl_vector_calloc(n_o); calc->_St = gsl_matrix_calloc(n_o, n_o); calc->_Stm1 = gsl_matrix_calloc(n_o, n_o); calc->_Rt = gsl_matrix_calloc(n_o, n_o); calc->_Ht = gsl_matrix_calloc(n_s, n_o); calc->_Kt = gsl_matrix_calloc(n_s, n_o); calc->_Tmp_N_SV_N_TS = gsl_matrix_calloc(n_s, n_o); calc->_Tmp_N_TS_N_SV = gsl_matrix_calloc(n_o, n_s); calc->_Q = gsl_matrix_calloc(n_s, n_s); calc->_FtCt = gsl_matrix_calloc(n_s, n_s); calc->_Ft = gsl_matrix_calloc(n_s, n_s); calc->_eval = gsl_vector_calloc(n_s); calc->_evec = gsl_matrix_calloc(n_s, n_s); calc->_w_eigen_vv = gsl_eigen_symmv_alloc(n_s); } } else if (nav->implementation == SSM_SDE){ calc->y_pred = ssm_d1_new(dim); } else if (nav->implementation == SSM_PSR){ ssm_psr_new(calc); } /**************************/ /* multi-threaded sorting */ /**************************/ calc->J = fitness->J; calc->to_be_sorted = ssm_d1_new(fitness->J); calc->index_sorted = ssm_st1_new(fitness->J); /**************/ /* references */ /**************/ calc->_par = NULL; calc->_nav = nav; /**************/ /* covariates */ /**************/ json_t *jcovariates = json_object_get(jdata, "covariates"); calc->covariates_length = json_array_size(jcovariates); if(calc->covariates_length){ calc->acc = malloc(calc->covariates_length * sizeof(gsl_interp_accel *)); if (calc->acc == NULL) { char str[SSM_STR_BUFFSIZE]; snprintf(str, SSM_STR_BUFFSIZE, "Allocation impossible in file :%s line : %d",__FILE__,__LINE__); ssm_print_err(str); exit(EXIT_FAILURE); } calc->spline = malloc(calc->covariates_length * sizeof(gsl_spline *)); if (calc->spline == NULL) { char str[SSM_STR_BUFFSIZE]; snprintf(str, SSM_STR_BUFFSIZE, "Allocation impossible in file :%s line : %d",__FILE__,__LINE__); ssm_print_err(str); exit(EXIT_FAILURE); } const gsl_interp_type *my_gsl_interp_type = ssm_str_to_interp_type(opts->interpolator); int k, z; double freeze_forcing; // the time (in days) to freeze (i.e only take metadata from this time) (ignored if freeze_forcing < 0.0) double t_max; //t_max the highest possible time in days when interpolated metadata will be requested (negative values default to last point of metadata). //assess freeze_forcing and t_max... struct tm tm_start; memset(&tm_start, 0, sizeof(struct tm)); strptime(data->date_t0, "%Y-%m-%d", &tm_start); time_t t_start = timegm(&tm_start); if(strcmp("", opts->end)!=0){ struct tm tm_freeze; memset(&tm_freeze, 0, sizeof(struct tm)); strptime(opts->freeze_forcing, "%Y-%m-%d", &tm_freeze); time_t t_freeze = timegm(&tm_freeze); freeze_forcing = difftime(t_freeze, t_start)/(24.0*60.0*60.0); } else { freeze_forcing = -1.0; } if(strcmp("", opts->end)!=0){ struct tm tm_end; memset(&tm_end, 0, sizeof(struct tm)); strptime(opts->end, "%Y-%m-%d", &tm_end); time_t t_end = timegm(&tm_end); t_max = difftime(t_end, t_start)/(24.0*60.0*60.0); } else { t_max = -1.0; } for (k=0; k< calc->covariates_length; k++) { json_t *jcovariate = json_array_get(jcovariates, k); double *x = ssm_load_jd1_new(jcovariate, "x"); double *y = ssm_load_jd1_new(jcovariate, "y"); int size = json_array_size(json_object_get(jcovariate, "x")); if((freeze_forcing < 0.0) && (t_max > x[size-1])){ //no freeze but t_max > x[size-1] repeat last value int prev_size = size ; size += ((int) t_max - x[prev_size-1]) ; double *tmp_x = realloc(x, size * sizeof (double) ); if ( tmp_x == NULL ) { ssm_print_err("Reallocation impossible"); free(x); exit(EXIT_FAILURE); } else { x = tmp_x; } double *tmp_y = realloc(y, size * sizeof (double) ); if ( tmp_y == NULL ) { ssm_print_err("Reallocation impossible"); free(y); exit(EXIT_FAILURE); } else { y = tmp_y; } //repeat last value double xlast = x[prev_size-1]; for(z = prev_size; z < size ; z++ ){ x[z] = xlast + z; y[z] = y[prev_size - 1]; } } if( (freeze_forcing>=0.0) || (size == 1) ){ //only 1 value: make it 2 double x_all[2]; x_all[0] = x[0]; x_all[1] = GSL_MAX( GSL_MAX( t_max, ((data->n_obs>=1) ? (double) data->rows[data->n_obs-1]->time: 0.0) ), x[size-1]); double y_all[2]; y_all[0] = (size == 1) ? y[0]: gsl_spline_eval(calc->spline[k], GSL_MIN(freeze_forcing, x[size-1]), calc->acc[k]); //interpolate y for time freeze_forcing requested (if possible) y_all[1] = y_all[0]; calc->acc[k] = gsl_interp_accel_alloc (); calc->spline[k] = gsl_spline_alloc (gsl_interp_linear, 2); gsl_spline_init (calc->spline[k], x_all, y_all, 2); } else if (size >= gsl_interp_type_min_size(my_gsl_interp_type)) { calc->acc[k] = gsl_interp_accel_alloc (); calc->spline[k] = gsl_spline_alloc(my_gsl_interp_type, size); gsl_spline_init (calc->spline[k], x, y, size); } else { ssm_print_warning("insufficient data points for required metadata interpolator, switching to linear"); calc->acc[k] = gsl_interp_accel_alloc (); calc->spline[k] = gsl_spline_alloc (gsl_interp_linear, size); gsl_spline_init(calc->spline[k], x, y, size); } free(x); free(y); } } return calc; }
int pcm_json_to_seccomp(char **hook) { json_t *str, *rules, *hook_obj; u_int32_t default_action; int i, rc; if(hook) *hook = NULL; // initialize to a sane value /* Verify it's what we expect */ if(! json_is_object(PCM_GLOBAL.policy)) { errx(EXIT_FAILURE, "root is not an object"); } /* Get the default action */ str = json_object_get(PCM_GLOBAL.policy, "default"); if(! json_is_string(str)) { errx(EXIT_FAILURE, "expected string, didn't get it :/"); } default_action = pcm_string_to_policy(json_string_value(str)); if(default_action == -1) { errx(EXIT_FAILURE, "converting default policy string failed, expecting ALLOW, KILL, or ERRNO"); } /* Initialize seccomp with the default action */ PCM_GLOBAL.seccomp = seccomp_init(default_action); if(PCM_GLOBAL.seccomp == NULL) { errx(EXIT_FAILURE, "Initializing seccomp context failed"); } str = json_object_get(PCM_GLOBAL.policy, "no_new_privs"); if(str) { // for another day, default version of jansson on this system doesn't have it. // int status; // status = json_boolean(str); // we'll assume they want to disable it. rc = seccomp_attr_set(PCM_GLOBAL.seccomp, SCMP_FLTATR_CTL_NNP, 0); if(! rc) { errx(EXIT_FAILURE, "Unable to set no_new_privs attribute to off (%m) and rc = %d, -rc = %d\n", rc, -rc); } } /* And loop over the rules */ rules = json_object_get(PCM_GLOBAL.policy, "rules"); if(! json_is_array(rules)) { errx(EXIT_FAILURE, "Getting policy rules from json failed"); } if(hook) { // XXX, should the file hook value overwrite env var etc? // should we just pcm_hook_set() here? hook_obj = json_object_get(PCM_GLOBAL.policy, "hook"); if(hook_obj) { if(! json_is_string(hook_obj)) { errx(EXIT_FAILURE, "expected hook string, didn't get it :/"); } *hook = strdup(json_string_value(hook_obj)); } } for(i = 0; i < json_array_size(rules); i++) { json_t *rule; rule = json_array_get(rules, i); if(! json_is_object(rule)) { errx(EXIT_FAILURE, "Expected rule object, got something else instead"); } pcm_json_rule_to_seccomp(rule); } pcm_policy_free(); return 0; }
/* subscribe /root subname {query} * Subscribes the client connection to the specified root. */ static void cmd_subscribe(struct watchman_client *client, json_t *args) { w_root_t *root; struct watchman_client_subscription *sub; json_t *resp; const char *name; json_t *jfield_list; w_query *query; json_t *query_spec; struct w_query_field_list field_list; char *errmsg; int defer = true; /* can't use bool because json_unpack requires int */ json_t *defer_list = NULL; json_t *drop_list = NULL; if (json_array_size(args) != 4) { send_error_response(client, "wrong number of arguments for subscribe"); return; } root = resolve_root_or_err(client, args, 1, true); if (!root) { return; } name = json_string_value(json_array_get(args, 2)); if (!name) { send_error_response(client, "expected 2nd parameter to be subscription name"); goto done; } query_spec = json_array_get(args, 3); jfield_list = json_object_get(query_spec, "fields"); if (!parse_field_list(jfield_list, &field_list, &errmsg)) { send_error_response(client, "invalid field list: %s", errmsg); free(errmsg); goto done; } query = w_query_parse(root, query_spec, &errmsg); if (!query) { send_error_response(client, "failed to parse query: %s", errmsg); free(errmsg); goto done; } json_unpack(query_spec, "{s?:o}", "defer", &defer_list); if (defer_list && !json_is_array(defer_list)) { send_error_response(client, "defer field must be an array of strings"); goto done; } json_unpack(query_spec, "{s?:o}", "drop", &drop_list); if (drop_list && !json_is_array(drop_list)) { send_error_response(client, "drop field must be an array of strings"); goto done; } sub = calloc(1, sizeof(*sub)); if (!sub) { send_error_response(client, "no memory!"); goto done; } sub->name = w_string_new(name); sub->query = query; json_unpack(query_spec, "{s?:b}", "defer_vcs", &defer); sub->vcs_defer = defer; if (drop_list || defer_list) { size_t i; sub->drop_or_defer = w_ht_new(2, &w_ht_string_funcs); if (defer_list) { for (i = 0; i < json_array_size(defer_list); i++) { w_ht_replace(sub->drop_or_defer, w_ht_ptr_val(w_string_new(json_string_value( json_array_get(defer_list, i)))), false); } } if (drop_list) { for (i = 0; i < json_array_size(drop_list); i++) { w_ht_replace(sub->drop_or_defer, w_ht_ptr_val(w_string_new(json_string_value( json_array_get(drop_list, i)))), true); } } } memcpy(&sub->field_list, &field_list, sizeof(field_list)); sub->root = root; pthread_mutex_lock(&w_client_lock); w_ht_replace(client->subscriptions, w_ht_ptr_val(sub->name), w_ht_ptr_val(sub)); pthread_mutex_unlock(&w_client_lock); resp = make_response(); annotate_with_clock(root, resp); set_prop(resp, "subscribe", json_string(name)); add_root_warnings_to_response(resp, root); send_and_dispose_response(client, resp); resp = build_subscription_results(sub, root); if (resp) { send_and_dispose_response(client, resp); } done: w_root_delref(root); }
int main(int argc, char **argv) { if (argc < 4) { fprintf(stderr, "error: not enough arguments\n" "usage: aucc eu|us realm a|h|n|e\n"); } else { int proceed = 0; if (!strncmp(argv[1], "eu", 2) || !strncmp(argv[1], "us", 2)) { proceed = 1; } if (!strncmp(argv[3], "a", 1) || !strncmp(argv[3], "h", 1) || !strncmp(argv[3], "n", 1) || !strncmp(argv[3], "e", 1)) { proceed = 1; } if (proceed) { char url[URL_SIZE]; char *data; snprintf(url, URL_SIZE, "http://%s.battle.net/api/wow/auction/data/%s", argv[1], argv[2]); data = request(url); if (data) { json_t *root; json_error_t error; root = json_loads(data, 0, &error); free(data); if (root) { json_t *files, *file, *urld; const char *auction_data_url; files = json_object_get(root, "files"); file = json_array_get(files, 0); urld = json_object_get(file, "url"); auction_data_url = json_string_value(urld); int result = handle(auction_data_url); if (result == -1) { return 1; } else { int count = 0; switch (argv[3][0]) { case 'a': count = a; break; case 'h': count = h; break; case 'n': count = n; break; case 'e': count = a + h + n; break; } printf("%d auctions found.\n", count); } } else { fprintf(stderr, "error: on line %d: %s\n", error.line, error.text); return 1; } json_decref(root); } else { fprintf(stderr, "error: unable to get data, probably wrong realm name\n"); return 1; } } } return 0; }
/* * get the key from the JWKs that corresponds with the key specified in the header */ static apr_byte_t oidc_proto_get_key_from_jwks(request_rec *r, apr_jwt_header_t *jwt_hdr, json_t *j_jwks, const char *type, apr_jwk_t **result) { char *x5t = NULL; apr_jwt_get_string(r->pool, &jwt_hdr->value, "x5t", &x5t); ap_log_rerror(APLOG_MARK, OIDC_DEBUG, 0, r, "oidc_proto_get_key_from_jwks: search for kid \"%s\" or thumbprint x5t \"%s\"", jwt_hdr->kid, x5t); /* get the "keys" JSON array from the JWKs object */ json_t *keys = json_object_get(j_jwks, "keys"); if ( (keys == NULL) || !(json_is_array(keys)) ) { ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "oidc_proto_get_key_from_jwks: \"keys\" array element is not a JSON array"); return FALSE; } int i; for (i = 0; i < json_array_size(keys); i++) { /* get the next element in the array */ json_t *elem = json_array_get(keys, i); /* check that it is a JSON object */ if (!json_is_object(elem)) { ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, "oidc_proto_get_key_from_jwks: \"keys\" array element is not a JSON object, skipping"); continue; } /* get the key type and see if it is the RSA type that we are looking for */ json_t *kty = json_object_get(elem, "kty"); if ( (!json_is_string(kty)) || (strcmp(json_string_value(kty), type) != 0)) continue; /* see if we were looking for a specific kid, if not we'll return the first one found */ if ( (jwt_hdr->kid == NULL) && (x5t == NULL) ) { ap_log_rerror(APLOG_MARK, OIDC_DEBUG, 0, r, "oidc_proto_get_key_from_jwks: no kid/x5t to match, return first key found"); apr_jwk_parse_json(r->pool, elem, NULL, result); break; } /* we are looking for a specific kid, get the kid from the current element */ json_t *ekid = json_object_get(elem, "kid"); if ( (ekid != NULL) && json_is_string(ekid) && (jwt_hdr->kid != NULL) ) { /* compare the requested kid against the current element */ if (apr_strnatcmp(jwt_hdr->kid, json_string_value(ekid)) == 0) { ap_log_rerror(APLOG_MARK, OIDC_DEBUG, 0, r, "oidc_proto_get_key_from_jwks: found matching kid: \"%s\"", jwt_hdr->kid); apr_jwk_parse_json(r->pool, elem, NULL, result); break; } } /* we are looking for a specific x5t, get the x5t from the current element */ json_t *ex5t = json_object_get(elem, "kid"); if ( (ex5t != NULL) && json_is_string(ex5t) && (x5t != NULL) ) { /* compare the requested kid against the current element */ if (apr_strnatcmp(x5t, json_string_value(ex5t)) == 0) { ap_log_rerror(APLOG_MARK, OIDC_DEBUG, 0, r, "oidc_proto_get_key_from_jwks: found matching x5t: \"%s\"", x5t); apr_jwk_parse_json(r->pool, elem, NULL, result); break; } } } return TRUE; }
static int dump(json_t *json, char **buf, int *size) { int i; int len; if (json == NULL) return 1; switch (json_typeof(json)) { case JSON_STRING: { char *tmp; char *value = json_string_get(json); tmp = (char *)malloc(strlen(value) + 3); if ( ! tmp) return 1; len = sprintf(tmp, "\"%s\"", value); append_string(tmp, len, buf, size); free(tmp); } break; case JSON_NUMBER: { char tmp[32] = {0, }; long double value = json_number_get(json); len = sprintf(tmp, "%.17Lg", value); append_string(tmp, len, buf, size); } break; case JSON_OBJECT: { int obj_size; char *key; obj_size = json_object_sizeof(json); append_string("{", 1, buf, size); for (i = 0; i < obj_size; i++) { key = json_object_get_key(json, i); if ( ! key) continue; json_t *j = json_object_get(json, key); if ( ! j) continue; append_string(key, strlen(key), buf, size); append_string(":", 1, buf, size); dump(j, buf, size); if (i + 1 < obj_size) append_string(", ", 2, buf, size); } append_string("}", 1, buf, size); } break; case JSON_ARRAY: { int array_size; array_size = json_array_sizeof(json); append_string("[", 1, buf, size); for (i = 0; i < array_size; i++) { json_t *j = json_array_get(json, i); if ( ! j) continue; dump(j, buf, size); if (i + 1 < array_size) append_string(", ", 2, buf, size); } append_string("]", 1, buf, size); } break; case JSON_TRUE: append_string("true", 4, buf, size); break; case JSON_FALSE: append_string("false", 5, buf, size); break; case JSON_NULL: append_string("null", 4, buf, size); break; } return 0; }
static int do_connect(const char *host, int port, const char *user, const char *pass, const char *email, int reg_user) { int fd = -1, authresult; char ipv6_error[120], ipv4_error[120], errmsg[256]; json_t *jmsg, *jarr; /* try ipv6 */ if (fd == -1) fd = connect_server(host, port, FALSE, ipv6_error, 120); if (fd == -1) /* no ipv6 connection, try ipv4 */ fd = connect_server(host, port, TRUE, ipv4_error, 120); if (fd == -1) { /* both connection attempts failed: error message */ if (!ipv6_error[0] && !ipv4_error[0]) { snprintf(errmsg, 256, "No address for \"%s\" found!", host); print_error(errmsg); } if (ipv6_error[0]) { snprintf(errmsg, 256, "IPv6: %s", ipv6_error); print_error(errmsg); } if (ipv4_error[0]) { snprintf(errmsg, 256, "IPv4: %s", ipv4_error); print_error(errmsg); } sockfd = -1; net_active = FALSE; return NO_CONNECTION; } in_connect_disconnect = TRUE; sockfd = fd; jmsg = json_pack("{ss,ss}", "username", user, "password", pass); if (reg_user) { if (email) json_object_set_new(jmsg, "email", json_string(email)); jmsg = send_receive_msg("register", jmsg); } else { jmsg = send_receive_msg("auth", jmsg); } in_connect_disconnect = FALSE; if (!jmsg || json_unpack(jmsg, "{si*}", "return", &authresult) == -1) { if (jmsg) json_decref(jmsg); close(fd); net_active = FALSE; sockfd = -1; return NO_CONNECTION; } /* the "version" field in the response is optional */ if (json_unpack(jmsg, "{so*}", "version", &jarr) != -1 && json_is_array(jarr) && json_array_size(jarr) >= 3) { nhnet_server_ver.major = json_integer_value(json_array_get(jarr, 0)); nhnet_server_ver.minor = json_integer_value(json_array_get(jarr, 1)); nhnet_server_ver.patchlevel = json_integer_value(json_array_get(jarr, 2)); } json_decref(jmsg); if (host != saved_hostname) strncpy(saved_hostname, host, sizeof (saved_hostname)); if (user != saved_username) strncpy(saved_username, user, sizeof (saved_username)); if (pass != saved_password) strncpy(saved_password, pass, sizeof (saved_password)); saved_port = port; if (authresult == AUTH_SUCCESS_NEW) { conn_err = FALSE; net_active = TRUE; } else { net_active = FALSE; sockfd = -1; } return authresult; }
static char *request(const char *url) { CURL *curl; CURLcode status; char *data; long code; curl = curl_easy_init(); data = malloc(BUFFER_SIZE); if(!curl || !data) return NULL; struct write_result write_result = { .data = data, .pos = 0 }; curl_easy_setopt(curl, CURLOPT_URL, url); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_response); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &write_result); status = curl_easy_perform(curl); if(status != 0) { fprintf(stderr, "error: unable to request data from %s:\n", url); fprintf(stderr, "%s\n", curl_easy_strerror(status)); return NULL; } curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code); if(code != 200) { fprintf(stderr, "error: server responded with code %ld\n", code); return NULL; } curl_easy_cleanup(curl); curl_global_cleanup(); /* zero-terminate the result */ data[write_result.pos] = '\0'; return data; } int main(int argc, char *argv[]) { size_t i; char *text; char url[URL_SIZE]; json_t *root; json_error_t error; json_t *commits; if(argc != 3) { fprintf(stderr, "usage: %s USER REPOSITORY\n\n", argv[0]); fprintf(stderr, "List commits at USER's REPOSITORY.\n\n"); return 2; } snprintf(url, URL_SIZE, URL_FORMAT, argv[1], argv[2]); text = request(url); if(!text) return 1; root = json_loads(text, 0, &error); free(text); if(!root) { fprintf(stderr, "error: on line %d: %s\n", error.line, error.text); return 1; } commits = json_object_get(root, "commits"); if(!json_is_array(commits)) { fprintf(stderr, "error: commits is not an array\n"); return 1; } for(i = 0; i < json_array_size(commits); i++) { json_t *commit, *id, *message; const char *message_text; commit = json_array_get(commits, i); if(!json_is_object(commit)) { fprintf(stderr, "error: commit %d is not an object\n", i + 1); return 1; } id = json_object_get(commit, "id"); if(!json_is_string(id)) { fprintf(stderr, "error: commit %d: id is not a string\n", i + 1); return 1; } message = json_object_get(commit, "message"); if(!json_is_string(message)) { fprintf(stderr, "error: commit %d: message is not a string\n", i + 1); return 1; } message_text = json_string_value(message); printf("%.8s %.*s\n", json_string_value(id), newline_offset(message_text), message_text); } json_decref(root); return 0; }
char *test_two_expressions() { spec_describe("test order of two expressions: 1 + 1\nprint 'word'"); FxP_ParserContext *context = parse_string("1 + 1\nprint 'word'"); setup_json(); element = json_object_get( json_object_get( json_object_get( json_object_get( json_object_get(expression, "method_call"), "receiver"), "literal"), "bit"), "INTEGER"); assert_ints_equal(json_integer_value(element), 1, "first expression receiver correct"); element = json_object_get( json_object_get( json_object_get( json_object_get( json_object_get(expression, "method_call"), "message"), "lookup"), "bit"), "STRING"); assert_strings_equal(json_string_value(element), "+", "second expression message correct"); element = json_object_get( json_object_get( json_object_get( json_array_get( json_object_get( json_object_get(expression, "method_call"), "arguments"), 0), "literal"), "bit"), "INTEGER"); assert_ints_equal(json_integer_value(element), 1, "second expression arguments correct"); expression = json_array_get(expressions, 1); \ element = json_object_get( json_object_get( json_object_get( json_object_get( json_object_get(expression, "method_call"), "message"), "lookup"), "bit"), "STRING"); assert_strings_equal(json_string_value(element), "print", "first expression message correct"); element = json_object_get( json_object_get( json_object_get( json_array_get( json_object_get( json_object_get(expression, "method_call"), "arguments"), 0), "literal"), "bit"), "STRING"); assert_strings_equal(json_string_value(element), "word", "first expression arguments correct"); cleanup(); return NULL; }
ssm_nav_t *ssm_nav_new(json_t *jparameters, ssm_options_t *opts) { char str[SSM_STR_BUFFSIZE]; ssm_nav_t *nav = malloc(sizeof (ssm_nav_t)); if (nav == NULL) { ssm_print_err("Allocation impossible for ssm_nav_t *"); exit(EXIT_FAILURE); } nav->implementation = opts->implementation; nav->noises_off = opts->noises_off; nav->print = opts->print; nav->parameters = _ssm_parameters_new(&nav->parameters_length); nav->states = _ssm_states_new(&nav->states_length, nav->parameters); nav->observed = _ssm_observed_new(&nav->observed_length); nav->states_sv = ssm_it_states_sv_new(nav->states); nav->states_remainders = ssm_it_states_remainders_new(nav->states); nav->states_inc = ssm_it_states_inc_new(nav->states); nav->states_sv_inc = ssm_it_states_sv_inc_new(nav->states); nav->states_diff = ssm_it_states_diff_new(nav->states); nav->par_all = ssm_it_parameters_all_new(nav->parameters); nav->par_noise = ssm_it_parameters_noise_new(nav->parameters); nav->par_disp = ssm_it_parameters_disp_new(nav->parameters); nav->par_icsv = ssm_it_parameters_icsv_new(nav->parameters); nav->par_icdiff = ssm_it_parameters_icdiff_new(nav->parameters); //theta: we over-allocate the iterators nav->theta_all = _ssm_it_parameters_new(nav->par_all->length); nav->theta_no_icsv_no_icdiff = _ssm_it_parameters_new(nav->par_all->length); nav->theta_icsv_icdiff = _ssm_it_parameters_new(nav->par_all->length); //json_t jparameters with diagonal covariance term to 0.0 won't be infered: re-compute length and content nav->theta_all->length = 0; nav->theta_no_icsv_no_icdiff->length = 0; nav->theta_icsv_icdiff->length = 0; int index, i; json_t *jresource = json_object_get(jparameters, "resources"); for(index=0; index< json_array_size(jresource); index++){ json_t *el = json_array_get(jresource, index); const char *name = json_string_value(json_object_get(el, "name")); if (strcmp(name, "covariance") == 0) { json_t *values = json_object_get(el, "data"); //for all the parameters: if covariance term and covariance term >0.0, fill theta_* for(i=0; i<nav->par_all->length; i++){ json_t *jcov_i = json_object_get(values, nav->par_all->p[i]->name); if(jcov_i){ json_t *jcov_ii = json_object_get(jcov_i, nav->par_all->p[i]->name); if(jcov_ii){ if(!json_is_number(jcov_ii)) { snprintf(str, SSM_STR_BUFFSIZE, "error: parameters.covariance.%s.%s is not a number\n", nav->par_all->p[i]->name, nav->par_all->p[i]->name); ssm_print_err(str); exit(EXIT_FAILURE); } if(json_number_value(jcov_ii) > 0.0){ if( ssm_in_par(nav->par_noise, nav->par_all->p[i]->name) ) { if(!(nav->noises_off & SSM_NO_WHITE_NOISE)){ nav->theta_all->p[nav->theta_all->length] = nav->par_all->p[i]; nav->theta_all->p[nav->theta_all->length]->offset_theta = nav->theta_all->length; nav->theta_all->length += 1; } } else if( ssm_in_par(nav->par_disp, nav->par_all->p[i]->name) ) { if(!(nav->noises_off & SSM_NO_DIFF)){ nav->theta_all->p[nav->theta_all->length] = nav->par_all->p[i]; nav->theta_all->p[nav->theta_all->length]->offset_theta = nav->theta_all->length; nav->theta_all->length += 1; } } else { nav->theta_all->p[nav->theta_all->length] = nav->par_all->p[i]; nav->theta_all->p[nav->theta_all->length]->offset_theta = nav->theta_all->length; nav->theta_all->length += 1; } int in_icsv = ssm_in_par(nav->par_icsv, nav->par_all->p[i]->name); int in_icdiff =ssm_in_par(nav->par_icdiff, nav->par_all->p[i]->name); if(!in_icsv && !in_icdiff){ nav->theta_no_icsv_no_icdiff->p[nav->theta_no_icsv_no_icdiff->length] = nav->par_all->p[i]; nav->theta_no_icsv_no_icdiff->length += 1; } else if (in_icsv || in_icdiff){ if(in_icdiff){ if(!(nav->noises_off & SSM_NO_DIFF)){ //diffusion is allowed nav->theta_icsv_icdiff->p[nav->theta_icsv_icdiff->length] = nav->par_all->p[i]; nav->theta_icsv_icdiff->length += 1; } else { nav->theta_no_icsv_no_icdiff->p[nav->theta_no_icsv_no_icdiff->length] = nav->par_all->p[i]; nav->theta_no_icsv_no_icdiff->length += 1; } } else { nav->theta_icsv_icdiff->p[nav->theta_icsv_icdiff->length] = nav->par_all->p[i]; nav->theta_icsv_icdiff->length += 1; } } } } } } break; } } //files (CSV open and print headers) if(opts->print & SSM_PRINT_TRACE){ #if SSM_JSON nav->trace = stdout; #else snprintf(str, SSM_STR_BUFFSIZE, "%s/trace_%d.csv", opts->root, opts->id); nav->trace = fopen(str, "w"); ssm_print_header_trace(nav->trace, nav); #endif } else { nav->trace = NULL; } if(opts->print & SSM_PRINT_X){ #if SSM_JSON nav->X = stdout; #else snprintf(str, SSM_STR_BUFFSIZE, "%s/X_%d.csv", opts->root, opts->id); nav->X = fopen(str, "w"); ssm_print_header_X(nav->X, nav); #endif } else { nav->X = NULL; } if(opts->print & SSM_PRINT_HAT){ #if SSM_JSON nav->hat = stdout; #else snprintf(str, SSM_STR_BUFFSIZE, "%s/hat_%d.csv", opts->root, opts->id); nav->hat = fopen(str, "w"); ssm_print_header_hat(nav->hat, nav); #endif } else { nav->hat = NULL; } if(opts->print & SSM_PRINT_DIAG){ #if SSM_JSON nav->diag = stdout; #else snprintf(str, SSM_STR_BUFFSIZE, "%s/diag_%d.csv", opts->root, opts->id); nav->diag = fopen(str, "w"); if(opts->algo & (SSM_SMC | SSM_KALMAN)){ ssm_print_header_pred_res(nav->diag, nav); } else if (opts->algo & (SSM_PMCMC | SSM_KMCMC)){ ssm_print_header_ar(nav->diag); } else if (opts->algo & SSM_MIF){ ssm_mif_print_header_mean_var_theoretical_ess(nav->diag, nav); } #endif } else { nav->diag = NULL; } return nav; }
char *test_function_with_two_expressions() { // TODO: parser can't handle no terminating \n at end of expressions set /*spec_describe("function two expressions: -> {\n 1 + 1\n print 'word'}");*/ spec_describe("function two expressions: -> {\n 1 + 1\n print 'word'\n}\n"); FxP_ParserContext *context = parse_string("-> {\n 1 + 1\n print 'word'\n}\n"); setup_json(); element = json_object_get( json_object_get(expression, "function_definition"), "expressions"); assert_ints_equal(json_array_size(element), 2, "expressions size correct"); expression = json_object_get( json_object_get(expression, "function_definition"), "expressions"); element = json_object_get( json_object_get( json_object_get( json_object_get( json_object_get( json_array_get( expression, 0), "method_call"), "receiver"), "literal"), "bit"), "INTEGER"); assert_ints_equal(json_integer_value(element), 1, "first expression receiver correct"); element = json_object_get( json_object_get( json_object_get( json_object_get( json_object_get( json_array_get( expression, 0), "method_call"), "message"), "lookup"), "bit"), "STRING"); assert_strings_equal(json_string_value(element), "+", "first expression message correct"); element = json_object_get( json_object_get( json_object_get( json_array_get( json_object_get( json_object_get( json_array_get( expression, 0), "method_call"), "arguments"), 0), "literal"), "bit"), "INTEGER"); assert_ints_equal(json_integer_value(element), 1, "first expression arguments correct"); cleanup(); return NULL; }
ssm_data_t *ssm_data_new(json_t *jdata, ssm_nav_t *nav, ssm_options_t *opts) { char str[SSM_STR_BUFFSIZE]; int i, j; ssm_data_t *data = malloc(sizeof (ssm_data_t)); if (data==NULL) { ssm_print_err("Allocation impossible for ssm_data_t"); exit(EXIT_FAILURE); } json_t *jstart = json_object_get(jdata, "start"); if(json_is_string(jstart)) { data->date_t0 = strdup(json_string_value(jstart)); } else { ssm_print_err("data start is not a string"); exit(EXIT_FAILURE); } json_t *jdata_data = json_object_get(jdata, "data"); data->length = json_array_size(jdata_data); data->ts_length = nav->observed_length; //n_obs if(opts->n_obs >= 0){ data->n_obs = (opts->n_obs < data->length) ? opts->n_obs : data->length; } else { data->n_obs = data->length; } ssm_row_t **rows = malloc(data->length * sizeof (ssm_row_t *)); if (rows==NULL) { ssm_print_err("Allocation impossible for ssm_data_row_t **"); exit(EXIT_FAILURE); } data->length_nonan = 0; data->n_obs_nonan = 0; data->ind_nonan = ssm_u1_new(data->length); for (i=0; i< data->length; i++){ rows[i] = malloc(sizeof (ssm_row_t)); if (rows[i] == NULL) { ssm_print_err("Allocation impossible for ssm_data_row_t *"); exit(EXIT_FAILURE); } json_t *jrow = json_array_get(jdata_data, i); json_t *jdate = json_object_get(jrow, "date"); if(json_is_string(jdate)) { rows[i]->date = strdup(json_string_value(jdate)); } else { snprintf(str, SSM_STR_BUFFSIZE, "error: data[%d].date is not a string\n", i); ssm_print_err(str); exit(EXIT_FAILURE); } json_t *jtime = json_object_get(jrow, "time"); if(json_is_number(jtime)) { rows[i]->time = (unsigned int) json_integer_value(jtime); } else { snprintf(str, SSM_STR_BUFFSIZE, "error: data[%d].time is not an integer\n", i); ssm_print_err(str); exit(EXIT_FAILURE); } json_t *jobserved = json_object_get(jrow, "observed"); rows[i]->ts_nonan_length = json_array_size(jobserved); if(rows[i]->ts_nonan_length){ rows[i]->observed = malloc(rows[i]->ts_nonan_length * sizeof (ssm_observed_t *)); if (rows[i]->observed == NULL) { ssm_print_err("Allocation impossible for ssm_data_row_t.observed"); exit(EXIT_FAILURE); } for(j=0; j<rows[i]->ts_nonan_length; j++){ json_t *jobserved_j = json_array_get(jobserved, j); if(json_is_number(jobserved_j)) { int id = json_integer_value(jobserved_j); rows[i]->observed[j] = nav->observed[id]; } else { snprintf(str, SSM_STR_BUFFSIZE, "error: data[%d].observed[%d] is not an integer\n", i, j); ssm_print_err(str); exit(EXIT_FAILURE); } } } rows[i]->values = ssm_load_jd1_new(jrow, "values"); json_t *jreset = json_object_get(jrow, "reset"); rows[i]->states_reset_length = json_array_size(jreset); if(rows[i]->states_reset_length){ rows[i]->states_reset = malloc(rows[i]->states_reset_length * sizeof (ssm_state_t *)); if (rows[i]->states_reset == NULL) { ssm_print_err("Allocation impossible for ssm_data_row_t.states_reset"); exit(EXIT_FAILURE); } } for(j=0; j<rows[i]->states_reset_length; j++){ json_t *jreset_j = json_array_get(jreset, j); if(json_is_number(jreset_j)) { int id = json_integer_value(jreset_j); rows[i]->states_reset[j] = nav->states[id]; } else { snprintf(str, SSM_STR_BUFFSIZE, "error: data[%d].reset[%d] is not an integer\n", i, j); ssm_print_err(str); exit(EXIT_FAILURE); } } if(rows[i]->ts_nonan_length){ data->ind_nonan[data->length_nonan] = i; data->length_nonan += 1; if(i< data->n_obs){ data->n_obs_nonan += 1; } } } data->rows = rows; ssm_data_adapt_to_simul(data, jdata, nav, opts); return data; }
/* subscribe /root subname {query} * Subscribes the client connection to the specified root. */ static void cmd_subscribe( struct watchman_client* clientbase, const json_ref& args) { std::shared_ptr<watchman_client_subscription> sub; json_ref resp, initial_subscription_results; json_ref jfield_list; json_ref jname; std::shared_ptr<w_query> query; json_ref query_spec; int defer = true; /* can't use bool because json_unpack requires int */ json_ref defer_list; json_ref drop_list; struct watchman_user_client *client = (struct watchman_user_client *)clientbase; if (json_array_size(args) != 4) { send_error_response(client, "wrong number of arguments for subscribe"); return; } auto root = resolve_root_or_err(client, args, 1, true); if (!root) { return; } jname = args.at(2); if (!json_is_string(jname)) { send_error_response( client, "expected 2nd parameter to be subscription name"); return; } query_spec = args.at(3); query = w_query_parse(root, query_spec); defer_list = query_spec.get_default("defer"); if (defer_list && !json_is_array(defer_list)) { send_error_response(client, "defer field must be an array of strings"); return; } drop_list = query_spec.get_default("drop"); if (drop_list && !json_is_array(drop_list)) { send_error_response(client, "drop field must be an array of strings"); return; } sub = std::make_shared<watchman_client_subscription>( root, client->shared_from_this()); sub->name = json_to_w_string(jname); sub->query = query; json_unpack(query_spec, "{s?:b}", "defer_vcs", &defer); sub->vcs_defer = defer; if (drop_list || defer_list) { size_t i; if (defer_list) { for (i = 0; i < json_array_size(defer_list); i++) { sub->drop_or_defer[json_to_w_string(json_array_get(defer_list, i))] = false; } } if (drop_list) { for (i = 0; i < json_array_size(drop_list); i++) { sub->drop_or_defer[json_to_w_string(json_array_get(drop_list, i))] = true; } } } // If they want SCM aware results we should wait for SCM events to finish // before dispatching subscriptions if (query->since_spec && query->since_spec->hasScmParams()) { sub->vcs_defer = true; // If they didn't specify any drop/defer behavior, default to a reasonable // setting that works together with the fsmonitor extension for hg. if (sub->drop_or_defer.find("hg.update") == sub->drop_or_defer.end()) { sub->drop_or_defer["hg.update"] = false; // defer } } // Connect the root to our subscription { std::weak_ptr<watchman_client> clientRef(client->shared_from_this()); client->unilateralSub.insert(std::make_pair( sub, root->unilateralResponses->subscribe( [clientRef, sub]() { auto client = clientRef.lock(); if (client) { client->ping->notify(); } }, sub->name))); } client->subscriptions[sub->name] = sub; resp = make_response(); resp.set("subscribe", json_ref(jname)); add_root_warnings_to_response(resp, root); ClockSpec position; initial_subscription_results = sub->buildSubscriptionResults(root, position); resp.set("clock", position.toJson()); send_and_dispose_response(client, std::move(resp)); if (initial_subscription_results) { send_and_dispose_response(client, std::move(initial_subscription_results)); } }
status_t windows_system_map_lookup( const char *rekall_profile, const char *symbol, const char *subsymbol, addr_t *address, addr_t *size) { status_t ret = VMI_FAILURE; json_error_t error; json_t *root = json_load_file(rekall_profile, 0, &error); if (!root) { PRINT_DEBUG("Rekall profile error on line %d: %s\n", error.line, error.text); goto exit; } if (!json_is_object(root)) { PRINT_DEBUG("Rekall profile: root is not an objet\n"); goto err_exit; } if (!subsymbol && !size) { json_t *constants = json_object_get(root, "$CONSTANTS"); json_t *jsymbol = json_object_get(constants, symbol); if (!jsymbol) { PRINT_DEBUG("Rekall profile: constant '%s' not found\n", symbol); goto err_exit; } *address = json_integer_value(jsymbol); ret = VMI_SUCCESS; } else { json_t *structs = json_object_get(root, "$STRUCTS"); json_t *jstruct = json_object_get(structs, symbol); if (!jstruct) { PRINT_DEBUG("Rekall profile: structure '%s' not found\n", symbol); goto err_exit; } if (size) { json_t *jsize = json_array_get(jstruct, 0); *size = json_integer_value(jsize); } if (address) { json_t *jstruct2 = json_array_get(jstruct, 1); json_t *jmember = json_object_get(jstruct2, subsymbol); if (!jmember) { PRINT_DEBUG("Rekall profile: structure member '%s' not found\n", subsymbol); goto err_exit; } json_t *jvalue = json_array_get(jmember, 0); *address = json_integer_value(jvalue); } ret = VMI_SUCCESS; } err_exit: json_decref(root); exit: return ret; }