// like uwsgi_pipe but with fixed size ssize_t uwsgi_pipe_sized(int src, int dst, size_t required, int timeout) { char buf[8192]; size_t written = 0; ssize_t len; while (written < required) { int ret = uwsgi_waitfd(src, timeout); if (ret > 0) { len = read(src, buf, UMIN(8192, required - written)); if (len == 0) { return written; } else if (len < 0) { uwsgi_error("read()"); return -1; } size_t remains = len; while (remains > 0) { int ret = uwsgi_waitfd_write(dst, timeout); if (ret > 0) { len = write(dst, buf, remains); if (len > 0) { remains -= len; written += len; } else if (len == 0) { return written; } else { uwsgi_error("write()"); return -1; } } else if (ret == 0) { goto timeout; } else { return -1; } } } else if (ret == 0) { goto timeout; } else { return -1; } } return written; timeout: uwsgi_log("timeout while piping from %d to %d !!!\n", src, dst); return -1; }
void uwsgi_send_stats(int fd, struct uwsgi_stats *(*func) (void)) { struct sockaddr_un client_src; socklen_t client_src_len = 0; int client_fd = accept(fd, (struct sockaddr *) &client_src, &client_src_len); if (client_fd < 0) { uwsgi_error("accept()"); return; } if (uwsgi.stats_http) { if (uwsgi_send_http_stats(client_fd)) { close(client_fd); return; } } struct uwsgi_stats *us = func(); if (!us) goto end; size_t remains = us->pos; off_t pos = 0; while (remains > 0) { int ret = uwsgi_waitfd_write(client_fd, uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT]); if (ret <= 0) { goto end0; } ssize_t res = write(client_fd, us->base + pos, remains); if (res <= 0) { if (res < 0) { uwsgi_error("write()"); } goto end0; } pos += res; remains -= res; } end0: free(us->base); free(us); end: close(client_fd); }
/* even if it is marked as non-blocking, so not use in request plugins as it uses poll() and not the hooks */ int uwsgi_write_nb(int fd, char *buf, size_t remains, int timeout) { char *ptr = buf; while(remains > 0) { int ret = uwsgi_waitfd_write(fd, timeout); if (ret > 0) { ssize_t len = write(fd, ptr, remains); if (len <= 0) { return -1; } ptr += len; remains -= len; continue; } return -1; } return 0; }
void emperor_send_stats(int fd) { struct sockaddr_un client_src; socklen_t client_src_len = 0; int client_fd = accept(fd, (struct sockaddr *) &client_src, &client_src_len); if (client_fd < 0) { uwsgi_error("accept()"); return; } if (uwsgi.stats_http) { if (uwsgi_send_http_stats(client_fd)) { close(client_fd); return; } } struct uwsgi_stats *us = uwsgi_stats_new(8192); if (uwsgi_stats_keyval_comma(us, "version", UWSGI_VERSION)) goto end; if (uwsgi_stats_keylong_comma(us, "pid", (unsigned long long) getpid())) goto end; if (uwsgi_stats_keylong_comma(us, "uid", (unsigned long long) getuid())) goto end; if (uwsgi_stats_keylong_comma(us, "gid", (unsigned long long) getgid())) goto end; char *cwd = uwsgi_get_cwd(); if (uwsgi_stats_keyval_comma(us, "cwd", cwd)) goto end0; if (uwsgi_stats_key(us, "emperor")) goto end0; if (uwsgi_stats_list_open(us)) goto end0; struct uwsgi_emperor_scanner *ues = emperor_scanners; while (ues) { uwsgi_stats_str(us, ues->arg); ues = ues->next; if (ues) { if (uwsgi_stats_comma(us)) goto end0; } } if (uwsgi_stats_list_close(us)) goto end0; if (uwsgi_stats_comma(us)) goto end0; if (uwsgi_stats_keylong_comma(us, "emperor_tyrant", (unsigned long long) uwsgi.emperor_tyrant)) goto end0; if (uwsgi_stats_keylong_comma(us, "throttle_level", (unsigned long long) emperor_throttle_level / 1000)) goto end0; if (uwsgi_stats_key(us, "vassals")) goto end0; if (uwsgi_stats_list_open(us)) goto end0; struct uwsgi_instance *c_ui = ui->ui_next; while (c_ui) { if (uwsgi_stats_object_open(us)) goto end0; if (uwsgi_stats_keyval_comma(us, "id", c_ui->name)) goto end0; if (uwsgi_stats_keylong_comma(us, "pid", (unsigned long long) c_ui->pid)) goto end0; if (uwsgi_stats_keylong_comma(us, "born", (unsigned long long) c_ui->born)) goto end0; if (uwsgi_stats_keylong_comma(us, "last_mod", (unsigned long long) c_ui->last_mod)) goto end0; if (uwsgi_stats_keylong_comma(us, "last_heartbeat", (unsigned long long) c_ui->last_heartbeat)) goto end0; if (uwsgi_stats_keylong_comma(us, "loyal", (unsigned long long) c_ui->loyal)) goto end0; if (uwsgi_stats_keylong_comma(us, "last_loyal", (unsigned long long) c_ui->last_loyal)) goto end0; if (uwsgi_stats_keylong_comma(us, "first_run", (unsigned long long) c_ui->first_run)) goto end0; if (uwsgi_stats_keylong_comma(us, "last_run", (unsigned long long) c_ui->last_run)) goto end0; if (uwsgi_stats_keylong_comma(us, "zerg", (unsigned long long) c_ui->zerg)) goto end0; if (uwsgi_stats_keylong_comma(us, "uid", (unsigned long long) c_ui->uid)) goto end0; if (uwsgi_stats_keylong_comma(us, "gid", (unsigned long long) c_ui->gid)) goto end0; if (uwsgi_stats_keyval_comma(us, "monitor", c_ui->scanner->arg)) goto end0; if (uwsgi_stats_keylong(us, "respawns", (unsigned long long) c_ui->respawns)) goto end0; if (uwsgi_stats_object_close(us)) goto end0; c_ui = c_ui->ui_next; if (c_ui) { if (uwsgi_stats_comma(us)) goto end0; } } if (uwsgi_stats_list_close(us)) goto end0; if (uwsgi_stats_comma(us)) goto end0; if (uwsgi_stats_key(us, "blacklist")) goto end0; if (uwsgi_stats_list_open(us)) goto end0; struct uwsgi_emperor_blacklist_item *uebi = emperor_blacklist; while (uebi) { if (uwsgi_stats_object_open(us)) goto end0; if (uwsgi_stats_keyval_comma(us, "id", uebi->id)) goto end0; if (uwsgi_stats_keylong_comma(us, "throttle_level", uebi->throttle_level / 1000)) goto end0; if (uwsgi_stats_keylong_comma(us, "attempt", (unsigned long long) uebi->attempt)) goto end0; if (uwsgi_stats_keylong_comma(us, "first_attempt", (unsigned long long) uebi->first_attempt.tv_sec)) goto end0; if (uwsgi_stats_keylong(us, "last_attempt", (unsigned long long) uebi->last_attempt.tv_sec)) goto end0; if (uwsgi_stats_object_close(us)) goto end0; uebi = uebi->next; if (uebi) { if (uwsgi_stats_comma(us)) goto end0; } } if (uwsgi_stats_list_close(us)) goto end0; if (uwsgi_stats_object_close(us)) goto end0; size_t remains = us->pos; off_t pos = 0; while (remains > 0) { int ret = uwsgi_waitfd_write(client_fd, uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT]); if (ret <= 0) { goto end0; } ssize_t res = write(client_fd, us->base + pos, remains); if (res <= 0) { if (res < 0) { uwsgi_error("write()"); } goto end0; } pos += res; remains -= res; } end0: free(cwd); end: free(us->base); free(us); close(client_fd); }
void corerouter_send_stats(struct uwsgi_corerouter *ucr) { struct sockaddr_un client_src; socklen_t client_src_len = 0; int client_fd = accept(ucr->cr_stats_server, (struct sockaddr *) &client_src, &client_src_len); if (client_fd < 0) { uwsgi_error("corerouter_send_stats()/accept()"); return; } if (uwsgi.stats_http) { if (uwsgi_send_http_stats(client_fd)) { close(client_fd); return; } } struct uwsgi_stats *us = uwsgi_stats_new(8192); if (uwsgi_stats_keyval_comma(us, "version", UWSGI_VERSION)) goto end; if (uwsgi_stats_keylong_comma(us, "pid", (unsigned long long) getpid())) goto end; if (uwsgi_stats_keylong_comma(us, "uid", (unsigned long long) getuid())) goto end; if (uwsgi_stats_keylong_comma(us, "gid", (unsigned long long) getgid())) goto end; char *cwd = uwsgi_get_cwd(); if (uwsgi_stats_keyval_comma(us, "cwd", cwd)) goto end0; if (uwsgi_stats_keylong_comma(us, "active_sessions", (unsigned long long) ucr->active_sessions)) goto end0; if (uwsgi_stats_key(us , ucr->short_name)) goto end0; if (uwsgi_stats_list_open(us)) goto end0; struct uwsgi_gateway_socket *ugs = uwsgi.gateway_sockets; while (ugs) { if (!strcmp(ugs->owner, ucr->name)) { if (uwsgi_stats_str(us, ugs->name)) goto end0; if (ugs->next) { if (uwsgi_stats_comma(us)) goto end0; } } ugs = ugs->next; } if (uwsgi_stats_list_close(us)) goto end0; if (uwsgi_stats_comma(us)) goto end0; if (ucr->static_nodes) { if (uwsgi_stats_key(us , "static_nodes")) goto end0; if (uwsgi_stats_list_open(us)) goto end0; struct uwsgi_string_list *usl = ucr->static_nodes; while(usl) { if (uwsgi_stats_object_open(us)) goto end0; if (uwsgi_stats_keyvaln_comma(us, "name", usl->value, usl->len)) goto end0; if (uwsgi_stats_keylong_comma(us, "hits", (unsigned long long) usl->custom2)) goto end0; if (uwsgi_stats_keylong(us, "grace", (unsigned long long) usl->custom)) goto end0; if (uwsgi_stats_object_close(us)) goto end0; usl = usl->next; if (usl) { if (uwsgi_stats_comma(us)) goto end0; } } if (uwsgi_stats_list_close(us)) goto end0; if (uwsgi_stats_comma(us)) goto end0; } if (ucr->has_subscription_sockets) { if (uwsgi_stats_key(us , "subscriptions")) goto end0; if (uwsgi_stats_list_open(us)) goto end0; int i; int first_processed = 0; for(i=0;i<UMAX16;i++) { struct uwsgi_subscribe_slot *s_slot = ucr->subscriptions[i]; if (s_slot && first_processed) { if (uwsgi_stats_comma(us)) goto end0; } while (s_slot) { first_processed = 1; if (uwsgi_stats_object_open(us)) goto end0; if (uwsgi_stats_keyvaln_comma(us, "key", s_slot->key, s_slot->keylen)) goto end0; if (uwsgi_stats_keylong_comma(us, "hash", (unsigned long long) s_slot->hash)) goto end0; if (uwsgi_stats_keylong_comma(us, "hits", (unsigned long long) s_slot->hits)) goto end0; #ifdef UWSGI_SSL if (uwsgi_stats_keylong_comma(us, "sni_enabled", (unsigned long long) s_slot->sni_enabled)) goto end0; #endif if (uwsgi_stats_key(us , "nodes")) goto end0; if (uwsgi_stats_list_open(us)) goto end0; struct uwsgi_subscribe_node *s_node = s_slot->nodes; while (s_node) { if (uwsgi_stats_object_open(us)) goto end0; if (uwsgi_stats_keyvaln_comma(us, "name", s_node->name, s_node->len)) goto end0; if (uwsgi_stats_keylong_comma(us, "modifier1", (unsigned long long) s_node->modifier1)) goto end0; if (uwsgi_stats_keylong_comma(us, "modifier2", (unsigned long long) s_node->modifier2)) goto end0; if (uwsgi_stats_keylong_comma(us, "last_check", (unsigned long long) s_node->last_check)) goto end0; if (uwsgi_stats_keylong_comma(us, "pid", (unsigned long long) s_node->pid)) goto end0; if (uwsgi_stats_keylong_comma(us, "uid", (unsigned long long) s_node->uid)) goto end0; if (uwsgi_stats_keylong_comma(us, "gid", (unsigned long long) s_node->gid)) goto end0; if (uwsgi_stats_keylong_comma(us, "requests", (unsigned long long) s_node->requests)) goto end0; if (uwsgi_stats_keylong_comma(us, "last_requests", (unsigned long long) s_node->last_requests)) goto end0; if (uwsgi_stats_keylong_comma(us, "tx", (unsigned long long) s_node->tx)) goto end0; if (uwsgi_stats_keylong_comma(us, "rx", (unsigned long long) s_node->rx)) goto end0; if (uwsgi_stats_keylong_comma(us, "cores", (unsigned long long) s_node->cores)) goto end0; if (uwsgi_stats_keylong_comma(us, "load", (unsigned long long) s_node->load)) goto end0; if (uwsgi_stats_keylong_comma(us, "weight", (unsigned long long) s_node->weight)) goto end0; if (uwsgi_stats_keylong_comma(us, "wrr", (unsigned long long) s_node->wrr)) goto end0; if (uwsgi_stats_keylong_comma(us, "ref", (unsigned long long) s_node->reference)) goto end0; if (uwsgi_stats_keylong_comma(us, "failcnt", (unsigned long long) s_node->failcnt)) goto end0; if (uwsgi_stats_keylong(us, "death_mark", (unsigned long long) s_node->death_mark)) goto end0; if (uwsgi_stats_object_close(us)) goto end0; if (s_node->next) { if (uwsgi_stats_comma(us)) goto end0; } s_node = s_node->next; } if (uwsgi_stats_list_close(us)) goto end0; if (uwsgi_stats_object_close(us)) goto end0; if (s_slot->next) { if (uwsgi_stats_comma(us)) goto end0; } s_slot = s_slot->next; // check for loopy optimization if (s_slot == ucr->subscriptions[i]) break; } } if (uwsgi_stats_list_close(us)) goto end0; if (uwsgi_stats_comma(us)) goto end0; } if (uwsgi_stats_keylong(us, "cheap", (unsigned long long) ucr->i_am_cheap)) goto end0; if (uwsgi_stats_object_close(us)) goto end0; size_t remains = us->pos; off_t pos = 0; while(remains > 0) { int ret = uwsgi_waitfd_write(client_fd, uwsgi.socket_timeout); if (ret <= 0) { goto end0; } ssize_t res = write(client_fd, us->base + pos, remains); if (res <= 0) { if (res < 0) { uwsgi_error("write()"); } goto end0; } pos += res; remains -= res; } end0: free(cwd); end: free(us->base); free(us); close(client_fd); }