int main (int argc, char *argv[]) { conf_t conf; /* FIXME: Revamp signal handlers. */ if (posignal (SIGHUP, SIG_IGN) == SIG_ERR) { log_err (EMUNGE_SNAFU, LOG_ERR, "Failed to ignore signal=%d", SIGHUP); } if (posignal (SIGPIPE, SIG_IGN) == SIG_ERR) { log_err (EMUNGE_SNAFU, LOG_ERR, "Failed to ignore signal=%d", SIGPIPE); } /* Close stdin since it is not used. */ if (close (STDIN_FILENO) < 0) { log_errno (EMUNGE_SNAFU, LOG_ERR, "Failed to close standard input"); } /* Set stdout to be line buffered. */ if (setvbuf (stdout, NULL, _IOLBF, 0) < 0) { log_err (EMUNGE_SNAFU, LOG_ERR, "Failed to line-buffer standard output"); } log_open_file (stderr, argv[0], LOG_INFO, LOG_OPT_PRIORITY); conf = create_conf (); parse_cmdline (conf, argc, argv); start_threads (conf); process_creds (conf); stop_threads (conf); destroy_conf (conf); log_close_file (); exit (EMUNGE_SUCCESS); }
static void save_what_can_be_saved(int sig_num) { const char *logfile; char buf[32]; if (write(2,message,strlen(message))<=0) return; logfile = path_config_get_string(PATH_CONFIG_FILE_CARDPEEK_LOG); if (write(2,logfile,strlen(logfile))<=0) return; if (write(2,signature,strlen(signature))<=0) return; sprintf(buf,"Received signal %i\n",sig_num); if (write(2,buf,strlen(buf))<=0) return; log_printf(LOG_ERROR,"Received signal %i",sig_num); do_backtrace(); log_close_file(); exit(-2); }
int main ( int argc, char** argv ) { FILE* pipe; SHAREDBUFFER* shared_buffer; CONFIG* config; char main_process_lifetime[15]; int producer_count = 0; char producer_lifetime[15]; int consumer_count = 0; int child_exit_status = 0; int child_exit_pid; int child_pid; int i = 0; int logfd; char message[150]; char dir[255]; int available_process_count = 10000; strncpy(dir, argv[0], last_index_of(argv[0], '/')); chdir(dir); logfd = log_open_file( NULL ); if ( !logfd ) { printf("Unable to open log file.\n"); return EXIT_FAILURE; } log_event( "--------------------------------" ); log_event( "Main process started." ); // first of all load configuration file config = load_config_file(); if ( !config ) { log_event( "Unable to access configuration." ); return EXIT_FAILURE; } // read values from configuration producer_count = atoi(read_configuration(config, "producer_count", "5")); consumer_count = atoi(read_configuration(config, "consumer_count", "2")); strcpy(producer_lifetime, read_configuration(config, "producer_lifetime", "10")); strcpy(main_process_lifetime, read_configuration(config, "main_process_lifetime", "40")); destroy_config( config ); log_event( "Fetching available process count of system." ); pipe = popen(PROCESS_COUNT_SCRIPT, "r"); if ( pipe ) { fgets(message, 100, pipe); pclose(pipe); available_process_count = atoi(message); } if ( producer_count + consumer_count + 1 > available_process_count ) { log_event( "Requested total process count is more than available process count of system." ); goto SAFE_EXIT; } //initialize shared buffer shared_buffer = create_shared_buffer(); if ( !shared_buffer ) { log_event( "Unable to initialize shared buffer." ); destroy_config( config ); return EXIT_FAILURE; } // create and start producer processes // producer processes run for a specified lifetime ( in seconds ) // which is read from configuration for ( i=0; i<producer_count; ++i) { child_pid = vfork(); if ( child_pid == 0 ) // producer process { execl("producer", "producer", producer_lifetime, NULL); exit(0); } } // start consumer processes // consumer processes run until a SIGKILL signal // there is no specific entry in the requirements that // when a consumer process should end. for ( i=0; i<consumer_count; ++i) { child_pid = vfork(); if ( child_pid == 0 ) // consumer process { execl("consumer", "consumer", NULL); exit(0); } } // start controller process // controller process checks elapsed time // once in every 5 seconds // if total execution time is above a specified time (in seconds) // signals all child processes except itself, to force exit child_pid = vfork(); if ( child_pid == 0 ) //controller process { execl("controller", "controller", main_process_lifetime, NULL); exit(0); } // in order to catch all child processes exits // we need a common wait for all them // here exitting processes is cought with pid and exit status // wait childs to exit while( (child_exit_pid = wait(&child_exit_status)) > 0 ) { sprintf( message, "Process [PID:%d] exitid with status: %d", child_exit_pid, child_exit_status ); log_event( message ); } SAFE_EXIT: log_event( "Main process is being closed." ); // relase allocated data structures to OS destroy_shared_buffer( shared_buffer ); log_close_file( ); return EXIT_SUCCESS; }
int main(int argc, char **argv) { cardmanager_t* CTX; cardreader_t* READER; int opt; int opt_index = 0; int run_gui = 1; char* reader_name = NULL; char* exec_command = NULL; #ifndef _WIN32 SSL_load_error_strings(); #endif signal(SIGSEGV, save_what_can_be_saved); path_config_init(); log_open_file(); while ((opt = getopt_long(argc,argv,"r:e:vh",long_options,&opt_index))!=-1) { switch (opt) { case 'r': reader_name = g_strdup(optarg); break; case 'e': exec_command = optarg; break; case 'v': display_readers_and_version(); run_gui = 0; break; default: display_help(argv[0]); run_gui = 0; } } if (run_gui) { /* if we want threads: gdk_threads_init(); gdk_threads_enter(); */ gui_init(&argc,&argv); gui_create(); log_printf(LOG_INFO,"Running %s",system_string_info()); install_dot_file(); luax_init(); CTX = cardmanager_new(); if (reader_name == NULL) { reader_name = gui_select_reader(cardmanager_count_readers(CTX), cardmanager_reader_name_list(CTX)); } READER = cardreader_new(reader_name); cardmanager_free(CTX); if (READER) { luax_set_card_reader(READER); cardreader_set_callback(READER,gui_readerview_print,NULL); if (exec_command) g_idle_add(run_command_from_cli,exec_command); else update_cardpeek(); /*else g_idle_add(run_update_checks,NULL); */ gui_run(); cardreader_free(READER); } else { fprintf(stderr,"Failed to open smart card reader '%s'.\n",reader_name); log_printf(LOG_ERROR,"Failed to open smart card reader '%s'.", reader_name); } luax_config_table_save(); luax_release(); /* if we want threads: gdk_threads_leave(); */ } if (reader_name) g_free(reader_name); log_close_file(); path_config_release(); ERR_free_strings(); return 0; }