Line data Source code
1 : #include <minipot_config.h> 2 : #include <stdlib.h> 3 : #include <unistd.h> 4 : #include "log.h" 5 : 6 : void minipot_config_init(struct minipot_config *cfg) { 7 0 : cfg->config_file = NULL; 8 0 : cfg->zmq_endopint = NULL; 9 0 : cfg->topic = NULL; 10 0 : cfg->user = NULL; 11 0 : cfg->ports_size = 4; 12 0 : cfg->ports_cnt = 0; 13 0 : cfg->ports = malloc(sizeof(*cfg->ports) * cfg->ports_size); 14 0 : } 15 : 16 : void minipot_config_destroy(struct minipot_config *cfg) { 17 0 : free(cfg->config_file); 18 0 : free(cfg->zmq_endopint); 19 0 : free(cfg->topic); 20 0 : free(cfg->user); 21 0 : free(cfg->ports); 22 0 : } 23 : 24 : void minipot_config_check(struct minipot_config *cfg) { 25 0 : if (!cfg->zmq_endopint) 26 0 : critical("ZMQ endopint must be defined!"); 27 0 : if (!cfg->topic) 28 0 : critical("Topic must be defined!"); 29 0 : if (cfg->ports_cnt < 1) 30 0 : critical("At least one port must be defined"); 31 : // Check for user only if we run as a super-user 32 0 : if (!geteuid() && !cfg->user) 33 0 : critical("User must be defined"); 34 0 : } 35 : 36 : char *minipot_config_2str(struct minipot_config *cfg) { 37 0 : char *str; 38 0 : size_t str_len; 39 0 : FILE *stream = open_memstream (&str, &str_len); 40 0 : if (cfg->config_file) 41 0 : fprintf(stream, "\tconfig file: %s\n", cfg->config_file); 42 0 : if (cfg->zmq_endopint) 43 0 : fprintf(stream, "\tzmq-endpoint: %s\n", cfg->zmq_endopint); 44 0 : if (cfg->topic) 45 0 : fprintf(stream, "\ttopic: %s\n", cfg->topic); 46 0 : if (cfg->ports_cnt > 0) { 47 0 : fprintf(stream, "\tports: "); 48 0 : for (size_t i = 0; i < cfg->ports_cnt - 1; i++) 49 0 : fprintf(stream, "%d, ", cfg->ports[i]); 50 0 : fprintf(stream, "%d\n", cfg->ports[cfg->ports_cnt - 1]); 51 : } 52 0 : if (cfg->user) 53 0 : fprintf(stream, "\tuser: %s\n", cfg->user); 54 0 : fclose(stream); 55 0 : return str; 56 : }