void destroy_supervisor(supervisor_thread_t *thiz) { if(thiz) { int w = 0; for(w = 0; w < thiz->config->num_workers; w++) { destroy_worker(thiz->worker_threads[w]); } thiz->worker_threads = NULL; destroy_rate_limiter(thiz->rate_limiter); destroy_queue(thiz->queue); destroy_config(thiz->config); destroy_worker_array(); TESR_LOG_FREE(thiz, supervisor_thread_t); free(thiz); thiz = NULL; } else { LOG_ERROR("can not free supervisor_thread_t* as it is NULL"); } }
static int run_worker(struct worker_data *data) { char buf[PIPE_BUF+1] = {}; char outbuf[PIPE_BUF+1] = {}; char *urls[MAX_URLS]; size_t urls_c; char *p; int res, i; ssize_t len; double bytes; long header_bytes; if(init_worker(data)) return -1; while(1) { if((len = msg_read(data->pipe_r, buf, sizeof(buf))) == -1) { return EXIT_FAILURE; } buf[len] = '\0'; if(strncmp(buf, "STOP", 4) == 0 || len == 0) return destroy_worker(data); if(strncmp(buf, "RESET", 5) == 0) { if((res = reset_worker(data)) != 0) { len = sprintf(outbuf, "ERR %d", res); msg_write(data->pipe_w, outbuf, len); } else { msg_write(data->pipe_w, "OK", sizeof("OK")); } continue; } if(strncmp(buf, "URLLIST ", 8) == 0) { p = buf + 8; data->chunk.enabled = 1; } else if(strncmp(buf, "URL ", 4) == 0) { p = buf + 4; } else { fprintf(stderr, "Unrecognised command '%s'!\n", buf); break; } if(data->debug) { fprintf(stderr, "Getting URL '%s'.\n", p); } curl_easy_setopt(data->curl, CURLOPT_URL, p); data->chunk.size = 0; if((res = curl_easy_perform(data->curl)) != CURLE_OK) { len = sprintf(outbuf, "ERR %d", res); msg_write(data->pipe_w, outbuf, len); } else { if((res = curl_easy_getinfo(data->curl, CURLINFO_SIZE_DOWNLOAD, &bytes)) != CURLE_OK || (res = curl_easy_getinfo(data->curl, CURLINFO_HEADER_SIZE, &header_bytes)) != CURLE_OK ) { fprintf(stderr, "cURL error: %s\n", curl_easy_strerror(res)); } if(data->chunk.enabled == 0) { len = sprintf(outbuf, "OK %lu bytes", (long)bytes + header_bytes); msg_write(data->pipe_w, outbuf, len); } else { urls_c = parse_urls(data->chunk.memory, data->chunk.size, urls, MAX_URLS); len = sprintf(outbuf, "OK %lu bytes %lu urls", (long)bytes + header_bytes, (long)urls_c); msg_write(data->pipe_w, outbuf, len); for(i = 0; i < urls_c; i++) { len = sprintf(outbuf, "%s", urls[i]); msg_write(data->pipe_w, outbuf, len); free(urls[i]); } data->chunk.enabled = 0; } } } return 0; }
static int reset_worker(struct worker_data *data) { destroy_worker(data); return init_worker(data); }