// ************************************************************************************************* // @fn main // @brief Main routine // @param none // @return none // ************************************************************************************************* int main(void) { #ifdef EMU emu_init(); #endif // Init MCU init_application(); // Assign initial value to global variables init_global_variables(); #ifdef CONFIG_TEST // Branch to welcome screen test_mode(); #else display_all_off(); #endif // Main control loop: wait in low power mode until some event needs to be processed while(1) { // When idle go to LPM3 idle_loop(); // Process wake-up events if (button.all_flags || sys.all_flags) wakeup_event(); // Process actions requested by logic modules if (request.all_flags) process_requests(); // Before going to LPM3, update display if (display.all_flags) display_update(); } }
// ************************************************************************************************* // @fn main // @brief Main routine // @param none // @return none // ************************************************************************************************* int main(void) { LED_On(); // Init MCU init_application(); // Assign initial value to global variables init_global_variables(); //timestampInit(1396556875); // Main control loop: wait in low power mode until some event needs to be processed while (1) { // Do the SHM specific stuff here. shmApp(); // Now for the predetermined time we should sleep here... P1OUT ^= 0x01; uint8_t i = 3; while (i--) { Timer0_A4_Delay(CONV_MS_TO_TICKS(1000)); } } }
void sighup_handler(int sig) { GKeyFile *keyfile; GError *error = NULL; int rc; // TODO: handle also closing/reopening of log files and databases proxy_error("Received HUP signal: reloading config file...\n"); #ifdef DEBUG //g_mem_profile(); malloc_stats_print(NULL, NULL, ""); #endif char *config_file=glovars.proxy_configfile; rc=config_file_is_readable(config_file); if (rc==0) { proxy_error("Config file %s is not readable\n", config_file); return; } keyfile = g_key_file_new(); if (!g_key_file_load_from_file(keyfile, config_file, G_KEY_FILE_NONE, &error)) { proxy_error("Error loading configuration from config file %s\n", config_file); g_key_file_free(keyfile); return; } // initialize variables and process config file init_global_variables(keyfile,1); g_key_file_free(keyfile); }
static int configure_collectd(struct cmdline_config *config) { /* * Read options from the config file, the environment and the command * line (in that order, with later options overwriting previous ones in * general). * Also, this will automatically load modules. */ if (cf_read(config->configfile)) { fprintf(stderr, "Error: Reading the config file failed!\n" "Read the logs for details.\n"); return 1; } /* * Change directory. We do this _after_ reading the config and loading * modules to relative paths work as expected. */ const char *basedir; if ((basedir = global_option_get("BaseDir")) == NULL) { fprintf(stderr, "Don't have a basedir to use. This should not happen. Ever."); return 1; } else if (change_basedir(basedir, config->create_basedir)) { fprintf(stderr, "Error: Unable to change to directory `%s'.\n", basedir); return 1; } /* * Set global variables or, if that fails, exit. We cannot run with * them being uninitialized. If nothing is configured, then defaults * are being used. So this means that the user has actually done * something wrong. */ if (init_global_variables() != 0) return 1; return 0; }
int main(int argc, char *argv[]) { #else int gstat_main(int argc, char *argv[]) { #endif DATA **data = NULL, *valdata = NULL; /* * initialise some global variables: */ atexit(close_gstat_log_file); init_userio(1); init_global_variables(); argv0 = argv[0]; /* * register command line arguments on command_line: */ command_line = store_argv(argc, argv); parse_gstatrc(); /* * INPUT: command line options; */ parse_options(argc, argv); /* exits on -e options */ /* * start with program heading: */ printlog("%s: %s version %s\n", GSTAT_NAME, GSTAT_OS, VERSION); printlog("%s\n", GSTAT_CR); gstat_start(); /* * INPUT: Parse command files: */ if (optind == argc) { /* there's no command file name left */ if (get_method() != UIF) { /* -i or -m command line option */ /* no arguments */ printlog("Updates, manuals and source code: %s\n", GSTAT_HOME); printlog("%s\n", USAGE); ErrMsg(ER_NOCMD, ""); } else { start_ui(); exit(0); } } else { /* we have a command file to be read */ for ( ; optind < argc; optind++) { command_file_name = argv[optind]; parse_file(command_file_name); if (logfile_name != NULL) set_gstat_log_file(efopen(logfile_name, "w")); /* * get global variables locally: */ data = get_gstat_data(); valdata = get_dataval(); set_seed(gl_seed); /* * check variable settings and next * INPUT: read data values from file: */ read_all_data(data, valdata, get_n_vars()); if (get_method() == NSP) /* Still no answer to this: choose default */ set_method(get_default_method()); set_mode(); check_global_variables(); setup_meschach_error_handler(); if (DEBUG_DUMP) dump_all(); if (get_method() != NSP) printlog("[%s]\n", method_string(get_method())); if (check_only) set_method(NSP); /* * start calculations && OUTPUT routines: */ switch (get_method()) { case UIF: start_ui(); break; case NSP: break; case COV: case SEM: do_variogram(get_n_vars(), get_method()); break; case POLY: setup_poly_method(); /*FALLTHROUGH*/ default: if (gl_xvalid) /* validation/cross validation */ cross_valid(data); else predict_all(data); /* or prediction: */ break; } /* switch get_method() */ remove_all(); /* free all data etc. */ init_global_variables(); /* re-init for next round */ } } if (DEBUG_DUMP) atexit(print_file_record); if (get_method() != UIF) elapsed(); /* * file closing & data freeing time: */ if (plotfile != NULL) efclose(plotfile); exit(0); } /* end of main() */
bool start_client(void) { clear_console(); printf("\n\033[32m[WIFSS] Starting Client...\033[0m\n\n"); init_global_variables(); char address[BUFFER]; char *args[BUFFER]; int16_t nbArgs = -1; do { printf("-> Server IP: "); prompt_keyboard(address); free_args(args, &nbArgs); parse_command(address, args, &nbArgs); } while(nbArgs != 1); int32_t port; char portBuffer[BUFFER]; do { printf("-> Server Port: "); prompt_keyboard(portBuffer); free_args(args, &nbArgs); parse_command(portBuffer, args, &nbArgs); if(nbArgs == 1) { port = strtoul(args[0], NULL, 10); } else { port = -1; } } while(port < 0 || port > 65535); struct addrinfo *servinfo; struct addrinfo hints; memset(&hints, 0, sizeof hints); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; hints.ai_flags = AI_PASSIVE; int16_t status = getaddrinfo(address, args[0], &hints, &servinfo); if(status != 0) { fprintf(stderr, "\n\033[31m[WIFSS] An error occurred while trying to get some information about your host: %s.\033[0m\n\n", gai_strerror(status)); exit(EXIT_FAILURE); } char buffer[BUFFER]; // Some stuff to set a timeout on `connect` int16_t sock = -1; int8_t error = 0; socklen_t lenght = sizeof(error); int16_t nbFds = -1; fd_set readfds; do { for(struct addrinfo *tmp = servinfo; tmp != NULL; tmp = tmp->ai_next) { /* Only for previous iterations */ if(sock != -1 && close(sock) == -1) { fprintf(stderr, "\n\033[31m[WIFSS] An error occurred while closing a socket: %s.\033[0m\n\n", strerror(errno)); exit(EXIT_FAILURE); } /* ____________________________ */ sock = socket(tmp->ai_family, tmp->ai_socktype, tmp->ai_protocol); if(sock == -1) { printf("\n\033[31m[WIFSS] Error while creating an endpoint for communication with server: %s.\033[0m\n\n", strerror(errno)); return false; } // Before the connection procedure, we'll set the socket as NON-BLOCKING fcntl(sock, F_SETFL, fcntl(sock, F_GETFL, false) | O_NONBLOCK); // Let's launch the connection procedure connect(sock, tmp->ai_addr, tmp->ai_addrlen); if(errno != EINPROGRESS) { fprintf(stderr, "\n\033[31m[WIFSS] An error occurred while running the connection procedure to: "); print_endpoint(tmp); printf(" (%s).\033[0m\n\n", strerror(errno)); continue; } // Let's now set a watch dog of 3 seconds on it FD_ZERO(&readfds); FD_SET(sock, &readfds); nbFds = select(sock + 1, &readfds, NULL, NULL, &(struct timeval){3, 0}); if(nbFds == 0) { // The timeout has been elapsed... printf("\n\033[31m[WIFSS] Error while connecting to "); print_endpoint(tmp); printf(": timeout reached.\033[0m\n"); continue; } else if(nbFds == -1) { // Again ? An error occurred... fprintf(stderr, "\n\033[31m[WIFSS] An error occurred while waiting for the connection procedure ending: %s.\033[0m\n\n", strerror(errno)); exit(EXIT_FAILURE); } else { // Fetch the buffered SYNC data [see @server/commands.c] recv(sock, buffer, BUFFER, false); if(strcmp(buffer, CLIENT_SERVER_SYNC)) { printf("%s\n", buffer); exit(EXIT_FAILURE); } } // Two cases: The connection has been established OR a f*cking new error occurred (before the timeout !)... if(getsockopt(sock, SOL_SOCKET, SO_ERROR, &error, &lenght) != 0) { fprintf(stderr, "\n\033[31m[WIFSS] An error occurred while retrieving some information about the socket: %s.\033[0m\n\n", gai_strerror(status)); exit(EXIT_FAILURE); } if(error == 0) { // For the future, let's set again the socket as BLOCKING fcntl(sock, F_SETFL, fcntl(sock, F_GETFL, false) ^ O_NONBLOCK); // SSL stuffs SSL *ssl = SSL_new(core_variables.ctx); if(ssl == NULL) { fprintf(stderr, "\n\n\033[31mSSL Error: Couldn\'t enable the SSL layer on the socket.\n\n"); exit(EXIT_FAILURE); } if(SSL_set_cipher_list(ssl, (const char *const)"HIGH:!aNULL:!kRSA:!PSK:!SRP:!MD5:!RC4") != 1) { fprintf(stderr, "\n\n\033[31mSSL Error: Couldn\'t set the ciphers list.\n\n"); exit(EXIT_FAILURE); } SSL_set_fd(ssl, sock); if(SSL_connect(ssl) <= 0) { printf("\n\n\033[31m[WIFSS] Couldn\'t create a TLS tunnel with the host...\033[0m\n\n"); ERR_print_errors_fp(stderr); exit(EXIT_FAILURE); } // SSL Verification + Certificate information if(SSL_get_verify_result(ssl) != X509_V_OK) { fprintf(stderr, "\n\n\033[31mSSL Error: The result got from SSL is not valid (is your certificate correct ?).\n\n"); exit(EXIT_FAILURE); } X509 *cert = SSL_get_peer_certificate(ssl); if(cert == NULL) { fprintf(stderr, "\n\n\033[31mSSL Error: No certificate was sent by server.\n\n"); exit(EXIT_FAILURE); } printf("\n\033[34m\tTLS cipher used for this connection: %s\n", SSL_get_cipher(ssl)); char *str = NULL; printf("\tServer certificate:\n"); str = X509_NAME_oneline(X509_get_subject_name(cert), 0, 0); printf("\t\tSubject: %s\n", (str != NULL ? str : "None")); OPENSSL_free(str); str = X509_NAME_oneline(X509_get_issuer_name(cert), 0, 0); printf("\t\tIssuer : %s\033[0m\n", (str != NULL ? str : "None")); OPENSSL_free(str); X509_free(cert); // __________________________________________ core_variables.ssl = ssl; printf("\n\033[32m[WIFSS] Connected to "); print_endpoint(tmp); printf(".\033[0m\n"); strncpy(buffer, "", BUFFER); if(SSL_read(ssl, buffer, BUFFER) <= 0) { printf("\n\033[31m[WIFSS] Couldn\'t get your ID from server, exiting now.\033[0m\n\n"); exit(EXIT_FAILURE); } uint16_t idTemp = get_second_args_group_as_integer(buffer); printf("\n\033[32m[WIFSS] Your ID on the server is %d.\033[0m\n\n", idTemp); /* We save the ID of the client for the future */ core_variables.client_id = idTemp; break; } else { printf("\n\033[31m[WIFSS] Error while connecting to "); print_endpoint(tmp); printf(": %s.\033[0m\n", strerror(errno)); } } if(core_variables.client_id != -1) { // Yeah... We've to `break` the loop a second time break; } else { printf("\nWould you like to retry now ? (YES / no)\n\n"); if(!prompt_yes_no(buffer, args, &nbArgs)) { printf("\n"); return false; } } } while(true);