static void dn_set_default_options(struct instance *nci) { int status; nci->ctx = NULL; nci->log_level = DN_LOG_DEFAULT; nci->log_filename = DN_LOG_PATH; nci->conf_filename = DN_CONF_PATH; nci->stats_port = DN_STATS_PORT; nci->stats_addr = DN_STATS_ADDR; nci->stats_interval = DN_STATS_INTERVAL; status = dn_gethostname(nci->hostname, DN_MAXHOSTNAMELEN); if (status < 0) { log_warn("gethostname failed, ignored: %s", strerror(errno)); dn_snprintf(nci->hostname, DN_MAXHOSTNAMELEN, "unknown"); } nci->hostname[DN_MAXHOSTNAMELEN - 1] = '\0'; nci->mbuf_chunk_size = DN_MBUF_SIZE; nci->pid = (pid_t)-1; nci->pid_filename = NULL; nci->pidfile = 0; }
static rstatus_t dn_create_pidfile(struct instance *nci) { char pid[DN_UINTMAX_MAXLEN]; int fd, pid_len; ssize_t n; fd = open(nci->pid_filename, O_WRONLY | O_CREAT | O_TRUNC, 0644); if (fd < 0) { log_error("opening pid file '%s' failed: %s", nci->pid_filename, strerror(errno)); return DN_ERROR; } nci->pidfile = 1; pid_len = dn_snprintf(pid, DN_UINTMAX_MAXLEN, "%d", nci->pid); n = dn_write(fd, pid, pid_len); if (n < 0) { log_error("write to pid file '%s' failed: %s", nci->pid_filename, strerror(errno)); return DN_ERROR; } close(fd); return DN_OK; }
static rstatus_t stats_http_rsp(int sd, uint8_t *content, size_t len) { ssize_t n; uint8_t http_header[MAX_HTTP_HEADER_SIZE]; memset( (void*)http_header, (int)'\0', MAX_HTTP_HEADER_SIZE ); n = dn_snprintf(http_header, MAX_HTTP_HEADER_SIZE, "%.*s %u \r\n\r\n", header_str.len, header_str.data, len); if (n < 0 || n >= MAX_HTTP_HEADER_SIZE) { return DN_ERROR; } n = dn_sendn(sd, http_header, n); if (n < 0) { log_error("send http headers on sd %d failed: %s", sd, strerror(errno)); close(sd); return DN_ERROR; } n = dn_sendn(sd, content, len); if (n < 0) { log_error("send stats on sd %d failed: %s", sd, strerror(errno)); close(sd); return DN_ERROR; } close(sd); return DN_OK; }
static rstatus_t stats_begin_nesting(struct stats *st, struct string *key) { struct stats_buffer *buf; uint8_t *pos; size_t room; int n; buf = &st->buf; pos = buf->data + buf->len; room = buf->size - buf->len - 1; n = dn_snprintf(pos, room, "\"%.*s\": {", key->len, key->data); if (n < 0 || n >= (int)room) { return DN_ERROR; } buf->len += (size_t)n; return DN_OK; }
static rstatus_t stats_add_num(struct stats *st, struct string *key, int64_t val) { struct stats_buffer *buf; uint8_t *pos; size_t room; int n; buf = &st->buf; pos = buf->data + buf->len; room = buf->size - buf->len - 1; n = dn_snprintf(pos, room, "\"%.*s\":%"PRId64", ", key->len, key->data, val); if (n < 0 || n >= (int)room) { return DN_ERROR; } buf->len += (size_t)n; return DN_OK; }