/* * sigchld_handler - The kernel sends a SIGCHLD to the shell whenever * a child job terminates (becomes a zombie), or stops because it * received a SIGSTOP or SIGTSTP signal. The handler reaps all * available zombie children, but doesn't wait for any other * currently running children to terminate. */ void sigchld_handler(int sig) { int status; pid_t pid; if (!flag) { pid = waitpid(-1, NULL, WNOHANG); deletejob(jobs, pid); return; } while ((pid = waitpid(-1, &status, WUNTRACED|WNOHANG)) > 0) { if (WIFSTOPPED(status)) sigtstp_handler(SIGTSTP); else if (WIFSIGNALED(status)) { if (WTERMSIG(status) == SIGINT) sigint_handler(SIGINT); else unix_error("sigchld_handler error"); } else if (WIFEXITED(status)) deletejob(jobs, pid); } }
void * signal_waiter (void *arg) { int signal; arg = NULL; pthread_sigmask (SIG_UNBLOCK, &signal_set, NULL); while (1) { #ifdef SOLARIS sigwait (&signal_set); #else sigwait (&signal_set, &signal); #endif switch (signal) { case SIGINT: sigint_handler (); break; case SIGALRM: sigalrm_handler (); break; } } }
/* * sigchld_handler - The kernel sends a SIGCHLD to the shell whenever * a child job terminates (becomes a zombie), or stops because it * received a SIGSTOP or SIGTSTP signal. The handler reaps all * available zombie children, but doesn't wait for any other * currently running children to terminate. */ void sigchld_handler(int sig) { int info; pid_t pid; while ((pid = waitpid(fgpid(jobs), &info, WNOHANG|WUNTRACED)) > 0) { if (WIFSTOPPED(info)) // Job exited by external process { sigtstp_handler(20); } else if (WIFSIGNALED(info)) // job terminated due to faulty signal { sigint_handler(-2); } else if (WIFEXITED(info)) // Job exits normalls { deletejob(jobs, pid); } } if ((errno != ECHILD && pid == -1) || pid > 0) // After the loop pid is -1, or pid is 0 { unix_error("WAITPID ERROR 404"); } return; }
void Salida::_run(){ // trap de SIGINT SIG_Trap sigint_handler(SIGINT); SignalHandler::getInstance()->registrarHandler(SIGINT, &sigint_handler); Logger *l = Logger::getInstance(); Logger::log("SALIDA", "Creo canal de lectura a entrada", DEBUG); FifoLectura canal_escuchar_de_entrada ( nombre + C_SALIDA ); pid_t pid_leido; while(sigint_handler.signalWasReceived() == 0){ canal_escuchar_de_entrada.abrir(); while(canal_escuchar_de_entrada.leer(static_cast<void*>(&pid_leido),sizeof(pid_t)) > 0){ std::string intermedio = "Sale la persona " + std::to_string(pid_leido); Logger::log("SALIDA", intermedio, DEBUG); // enviar al pid senial para que se desbloquee y siga andando por el parque kill(pid_leido, SIGBAJARSE); } canal_escuchar_de_entrada.cerrar(); } Logger::log("SALIDA", "Se cierra la salida", DEBUG); canal_escuchar_de_entrada.cerrar(); canal_escuchar_de_entrada.eliminar(); }
static void signal_handler(gint signum) { switch (signum) { case SIGINT: sigint_handler(); break; } }
void enable_interrupts( void ) { if( --mutex <= 0 ) { mutex = 0; if( sighup_pending ) sighup_handler( SIGHUP ); if( sigint_pending ) sigint_handler( SIGINT ); } }
void Servidor::iniciar() { SIGINT_Handler sigint_handler(this->cola,&this->clientes, &this->cerrado); SignalHandler::getInstance()->registrarHandler( SIGINT, &sigint_handler ); while (sigint_handler.getGracefulQuit() == 0) { cout << "Esperando peticiones." << endl; recibirPeticion(); procesarPeticion(); responderPeticion(getPeticionRecibida().pid); } cout << "El servidor ha dejado de recibir peticiones." << endl; }
void Cola::_run(){ // trap de SIGINT SIG_Trap sigint_handler(SIGINT); SIG_Trap sig_pasar_a_entrada(SIGPASARAENTRADA); SignalHandler::getInstance()->registrarHandler(SIGINT, &sigint_handler); SignalHandler::getInstance()->registrarHandler(SIGPASARAENTRADA, &sig_pasar_a_entrada); Logger *l = Logger::getInstance(); Logger::log("COLA", "Creo canal de lectura a personas", DEBUG); FifoLectura canal_escuchar_de_personas ( nombre + C_PERSONAS_A_COLA ); Logger::log("COLA", "Creo canal de escritura a entrada", DEBUG); FifoEscritura canal_escribir_a_entrada( nombre + C_COLA_A_ENTRADA ); pid_t pid_leido; bool queda_gente_para_meter = false; while(sigint_handler.signalWasReceived() == 0){ // ver cuando frenar (para que cierre bien y se cierren los pipes) if(!queda_gente_para_meter) canal_escuchar_de_personas.abrir(); // basicamente leo mientras haya gente queriendo entrar y no necesito pasarle gente a la entrada // si la entrada necesita gente me ocupo de eso primero // esto es para que la "cola" se mantenga en memoria y no sea el fifo en si // porque si hay muchisima gente se podria llenar el fifo y ahi se pierde el orden de llegada while(!(personas.size() > 0 && sig_pasar_a_entrada.signalWasReceived()) && (queda_gente_para_meter = canal_escuchar_de_personas.leer(static_cast<void*>(&pid_leido),sizeof(pid_t)) > 0)){ personas.push(pid_leido); std::string intermedio = "Entra la persona " + std::to_string(pid_leido) + " a la cola"; Logger::log("COLA", intermedio, DEBUG); } if(!queda_gente_para_meter) canal_escuchar_de_personas.cerrar(); // si frene porque tenia que pasarle gente a la entrada, hago eso if(sig_pasar_a_entrada.signalWasReceived() && personas.size() > 0){ pid_t prox = personas.front(); canal_escribir_a_entrada.abrir(); canal_escribir_a_entrada.escribir(static_cast<const void*>(&prox),sizeof(prox)); canal_escribir_a_entrada.cerrar(); personas.pop(); sig_pasar_a_entrada.reset(); std::string intermedio = "Paso la persona " + std::to_string(prox) + " a la entrada"; Logger::log("COLA", intermedio, DEBUG); } } Logger::log("COLA", "Se cierra la cola", DEBUG); canal_escribir_a_entrada.cerrar(); canal_escuchar_de_personas.cerrar(); canal_escuchar_de_personas.eliminar(); }
/* #### sigcontext doesn't exist in Solaris. This should be updated to be correct for Solaris. */ static SIGTYPE sighandler (int sig) { if (audio_fd > 0) { reset_device (0); close (audio_fd); } if (sig == SIGHUP && sighup_handler) sighup_handler (sig); else if (sig == SIGINT && sigint_handler) sigint_handler (sig); else exit (1); }
void signal_handler(int signum) { t_data *data; data = NULL; data = stock_data(data, 0); if(signum == SIGINT) sigint_handler(data); if(signum == SIGTSTP) sigtstp_handler(data); if(signum == SIGWINCH) sigwinch_handler(data); if(signum == SIGCONT) sigcont_handler(data); }
/* Intercept SIGINT and SIGHUP in order to close the audio and mixer devices before terminating sound output; this requires reliable signals as provided by "syssignal.h" */ static SIGTYPE sighandler (int sig) { if (mix_fd > 0) { if (audio_vol >= 0) { ioctl(mix_fd,SOUND_MIXER_WRITE_PCM,&audio_vol); audio_vol = -1; } if (mix_fd != audio_fd) close(mix_fd); mix_fd = -1; } if (audio_fd > 0) { ioctl(audio_fd,SNDCTL_DSP_SYNC,NULL); ioctl(audio_fd,SNDCTL_DSP_RESET,NULL); close(audio_fd); audio_fd = -1; } if (sig == SIGHUP && sighup_handler) sighup_handler(sig); else if (sig == SIGINT && sigint_handler) sigint_handler(sig); else exit(1); }
/* * sigchld_handler - The kernel sends a SIGCHLD to the shell whenever * a child job terminates (becomes a zombie), or stops because it * received a SIGSTOP or SIGTSTP signal. The handler reaps all * available zombie children, but doesn't wait for any other * currently running children to terminate. */ void sigchld_handler(int sig) { /*need to send extra signal*/ if (trace16){ pid_t pid; int status; while ((pid = waitpid(-1, &status, WNOHANG|WUNTRACED)) > 0){ if (WIFSTOPPED(status)) sigtstp_handler(SIGTSTP); else if (WIFSIGNALED(status)) sigint_handler(SIGINT); else if (WIFEXITED(status)) deletejob(jobs,pid); } } /*normal ones*/ else{ pid_t pid; int status; pid = waitpid(-1, NULL, WNOHANG); deletejob(jobs,pid); return; } }
/* * sigchld_handler - The kernel sends a SIGCHLD to the shell whenever * a child job terminates (becomes a zombie), or stops because it * received a SIGSTOP or SIGTSTP signal. The handler reaps all * available zombie children, but doesn't wait for any other * currently running children to terminate. */ void sigchld_handler(int sig) { int status; pid_t pid; //reap child while((pid = waitpid(-1, &status, WNOHANG|WUNTRACED)) > 0){ //child is stopped if(WIFSTOPPED(status)) sigtstp_handler(SIGTSTP); //child is killed else if(WIFSIGNALED(status)) sigint_handler(SIGINT); //child exit, just delete the job else if(WIFEXITED(status)) deletejob(jobs, pid); } return; }
/* * sigchld_handler - The kernel sends a SIGCHLD to the shell whenever * a child job terminates (becomes a zombie), or stops because it * received a SIGSTOP or SIGTSTP signal. The handler reaps all * available zombie children, but doesn'the wait for any other * currently running children to terminate. */ void sigchld_handler(int sig) { int status; pid_t pid; while ((pid = waitpid(-fgpid(jobs), &status, WNOHANG|WUNTRACED)) > 0) { if(WIFEXITED(status)) deletejob(jobs, pid); /* Delete the child from the job list*/ if(WIFSIGNALED(status)) sigint_handler(SIGINT); //jump to sigint handler if ctrl-c is pressed from keyboard if(WIFSTOPPED(status)) sigtstp_handler(SIGTSTP); //jump to sigstp handler if ctrl-z is pressed from keyboard } /* Reap a zombie child */ if (errno != ECHILD) unix_error("waitpid error"); return; }
/* * sigchld_handler - The kernel sends a SIGCHLD to the shell whenever * a child job terminates (becomes a zombie), or stops because it * received a SIGSTOP or SIGTSTP signal. The handler reaps all * available zombie children, but doesn't wait for any other * currently running children to terminate. */ void sigchld_handler(int sig) { pid_t pid; int status; int saved_errno = errno; while ((pid = waitpid(-1, &status, WNOHANG | WUNTRACED)) > 0) { if (WIFSTOPPED(status)) { //check if it is a Stop signal (crl-z) sigtstp_handler(20); //send signal to signalhandler return; } if (WIFSIGNALED(status)) { //checks for terminate signal (crl-c) sigint_handler(-2); //send signal to signalhandler } deletejob(jobs,pid); } errno = saved_errno; return; }
/* * sigchld_handler - The kernel sends a SIGCHLD to the shell whenever * a child job terminates (becomes a zombie), or stops because it * received a SIGSTOP or SIGTSTP signal. The handler reaps all * available zombie children, but doesn't wait for any other * currently running children to terminate. */ void sigchld_handler(int sig) { pid_t pid; int status, child_sig; /* reap any zombies and handle status reports */ while ((pid = waitpid(-1, &status, WUNTRACED | WNOHANG)) > 0) { if (WIFSTOPPED(status)) { sigtstp_handler(WSTOPSIG(status)); } else if (WIFSIGNALED(status)) { child_sig = WTERMSIG(status); if (child_sig == SIGINT) sigint_handler(child_sig); else unix_error("sigchld_handler: uncaught signal\n"); } else { deletejob(jobs, pid); /* remove the job */ } } if (pid == -1 && errno != ECHILD) unix_error("sigchld_handler: waitpid error"); return; }
/* * sigchld_handler - The kernel sends a SIGCHLD to the shell whenever * a child job terminates (becomes a zombie), or stops because it * received a SIGSTOP or SIGTSTP signal. The handler reaps all * available zombie children, but doesn't wait for any other * currently running children to terminate. */ void sigchld_handler(int sig) { int status; pid_t pid; // Waiting for/ handling all of the child processes according to their status while ((pid = waitpid(fgpid(jobs), &status, WNOHANG|WUNTRACED)) > 0) { if (WIFSTOPPED(status)){ sigtstp_handler(20); } else if (WIFSIGNALED(status)){ sigint_handler(-2); } else if (WIFEXITED(status)){ deletejob(jobs, pid); } } if (errno != ECHILD) { unix_error("waitpid error"); } return; }}
void stop_cb(){ end_game = 1; sigint_handler(); // in this case end_game is useless }
int main(int argc, char *argv[]) { char line[INPUTLEN]; char buff[BUFFSIZE]; char *peersfile; struct stat st; int rc; sigset_t set; if (argc < 3) { fprintf(stderr, "Usage: %s <peers_file> <self_id>\n", argv[0]); exit(EXIT_FAILURE); } peersfile = argv[1]; rc = stat(peersfile, &st); if (rc == -1) { if (errno == ENOENT) { fprintf(stderr, "Can't open %s.\n", peersfile); exit(EXIT_FAILURE); } else { fprintf(stderr, "Error stating %s: %d\n", peersfile, errno); exit(EXIT_FAILURE); } } self_info = NULL; load_peers(peersfile, argv[2]); if (self_info == NULL) { fprintf(stderr, "Can't find id %s in %s.\n", argv[2], peersfile); exit(EXIT_FAILURE); } init_gui(); main_tid = pthread_self(); sigemptyset(&set); sigaddset(&set, SIGINT); pthread_sigmask(SIG_BLOCK, &set, NULL); pthread_create(&heartbeat_tid, NULL, heartbeat, NULL); pthread_create(&chatserver_tid, NULL, chatserver, NULL); create_peers_poller(); pthread_sigmask(SIG_UNBLOCK, &set, NULL); signal(SIGINT, sigint_handler); usleep(250000); chat_writeln(TRUE, LOG_INFO, "Client ready..."); while(TRUE) { werase(input_window); wgetnstr(input_window, line, INPUTLEN); if (strstr(line, "status") == line) { chat_writeln(TRUE, LOG_INFO, "STATUS"); cmd_status(); } else if (strstr(line, "leave") == line) { werase(input_window); chat_writeln(TRUE, LOG_INFO, "Leaving..."); sleep(1); break; } else if (strstr(line, "msg") == line) { cmd_message(line + 3); } else if (strstr(line, "bcast") == line) { cmd_broadcast(line + 5); } else if (strstr(line, "exec") == line) { cmd_exec(line + 4); } else { snprintf(buff, BUFFSIZE, "%s :unknown command", line); chat_writeln(TRUE, LOG_ERR, buff); } } sigint_handler(SIGINT); exit(EXIT_SUCCESS); }
int main( int argc, char *argv[] ) { int32_t rc; int32_t timeout; // Handle interrupt events to make sure files are closed before exiting (void) signal( SIGINT, sigint_handler ); // Make sure input is correct if( argc < 6 ) { printf("Usage: %s port baudrate C(0.0-1000.0) [graph (0,1)] timeout\n", argv[0]); return 0; } printf("Threaded Serial Terminal\r\n"); // Convert string serial number to integer and open port if( serial_open( atoi( argv[1] ), atoi( argv[2] ), &process_packet ) ) { printf("Error opening serial port.\r\n"); exit(-1); } timeout = atoi( argv[5] ); printf("Data collection will start in ~%d seconds...\n", timeout); sleep(timeout); #ifdef ENABLE_MUSIC system("banshee --play"); #endif // Initialize routing an power tables memset( (uint8_t*)power_table, 0xff, MAX_DEVICES ); memset( (uint8_t*)routing_table, ( MAX_DEVICES + 1 ), MAX_DEVICES ); if ( routing_initialize( (energy_t)strtod( argv[3], NULL ) ) ) { printf("Error initializing routes.\n"); exit(-1); } rc = pthread_create( &serial_thread, NULL, serial_read_thread, NULL ); if (rc) { printf("Error creating serial thread\n"); exit(-1); } rc = pthread_create( &routing_thread, NULL, compute_routes_thread, (void*) routing_table ); if (rc) { printf("Error creating routing thread\n"); exit(-1); } pthread_mutex_init( &mutex_graph, NULL ); // Start mutex locked pthread_mutex_lock ( &mutex_graph ); rc = pthread_create( &graphing_thread, NULL, graph_thread, NULL ); if (rc) { printf("Error creating serial thread\n"); exit(-1); } uint32_t round = 0; for(;;) { //uint8_t index; // Wait until routing is done pthread_mutex_lock ( &mutex_route_done ); // Send new routes to AP send_serial_message( (uint8_t *)rp_tables, sizeof(rp_tables) ); // Only graph when asked to if ( argv[4][0] == '1') { // Start generating the graph pthread_mutex_unlock ( &mutex_graph ); } // Print routes and powers /* for( index = 0; index < MAX_DEVICES; index++ ) { printf( "%d->%d ", index+1, routing_table[index] ); } printf("\n"); for( index = 0; index < MAX_DEVICES; index++ ) { printf( "%02X ", power_table[index] ); } printf("\n");*/ //printf("Round %d\n", round); // Stop collecting data after MAX_ROUNDS if(round == MAX_ROUNDS) { sigint_handler( 0 ); } round++; } return 0; }
void ServerSocketListener::_run() { SIG_Trap sigint_handler(SIGINT); SignalHandler::getInstance()->registrarHandler(SIGINT, &sigint_handler); colaDeEnvio.get(); Logger::log(nombre + "_L", "Creo cl sender" , DEBUG); ServerSocketClSender sender(clientSocket, nombre); clientSender = sender.run(); char buffer[BUFFSIZE]; std::string peticion; Logger::log(nombre + "_L", "Mando CONN START a sender" , DEBUG); mensaje recibido; recibido.mtype = CONNECTION_START; recibido.socket = this->clientSocket; peticion = nombre + SEPARATOR + std::to_string(getpid()); strcpy ( recibido.texto,peticion.c_str() ); recibido.msize = peticion.size(); colaDeEnvio.escribir(recibido); while(!sigint_handler.signalWasReceived()) { int bytesRecibidos = this->recibir ( static_cast<void*>(buffer),BUFFSIZE ); if (!sigint_handler.signalWasReceived()) { peticion = buffer; peticion.resize(bytesRecibidos); std::string nickname = peticion.substr(0, peticion.find_first_of(":")); std::string message = peticion.substr(peticion.find_first_of(":")+2, peticion.size()); //std::cout << "ServerListener: " << getpid() << ": dato recibido: '" << peticion << "'"; //std::cout << ", de : " << clientSocket << std::endl; Logger::log(nombre + "_L", "recibi '" + peticion.substr(0, std::min((int)peticion.size()-1, 10)) + "...' de " + std::to_string(clientSocket) , DEBUG); if (nickname == EXIT_MESSAGE) { recibido.mtype = CONNECTION_END; } else if (nickname == "_client") { recibido.mtype = NICKNAME_REQ; } else if (nickname == SEPARATOR){ recibido.mtype = GET_LOG; } else { recibido.mtype = TEXT; } recibido.socket = this->clientSocket; recibido.msize = message.size(); strcpy ( recibido.texto,message.c_str() ); recibido.nsize = nickname.size(); strcpy ( recibido.nickname,nickname.c_str() ); Logger::log(nombre + "_L", "Forwardeo a sender" , DEBUG); colaDeEnvio.escribir(recibido); if (nickname == EXIT_MESSAGE) { raise(SIGINT); kill(clientSender, SIGINT); } } } Logger::log(nombre + "_L", "Cierro todo" , DEBUG); kill(clientSender, SIGINT); cerrarConexion(); }
int main(void) { system(CLEAR_SC); /* Creation/Load of the database */ database = open ("database", O_RDWR | O_CREAT,S_IRWXU); if(database==ERROR) return ERROR; /* Initialize the flock structure. */ lock.l_type = F_WRLCK; /* A write lock also blocks readers */ lock.l_start = 0; lock.l_len = 0; lock.l_whence = SEEK_SET; /* Lock the database */ printf("Accesing the database\n"); if(fcntl (database, F_SETLKW, &lock)==ERROR) return ERROR; printf("Access granted\n"); /* Assign handler for SIGINT (AKA Ctl+C) */ struct sigaction sa; memset (&sa, 0, sizeof (sa)); sa.sa_handler=&sigint_handler; if(sigaction(SIGINT,&sa,NULL)==-1) return SIGNAL_ERROR; /*Binary semaphore creation - MUTEX*/ semaphore=binary_semaphore_allocation(IPC_PRIVATE,IPC_CREAT); binary_semaphore_initialize(semaphore); /*Shared Memory Allocation*/ users_shared_memory = shmget(IPC_PRIVATE,sizeof(data_person)*MAX_USERS,IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR ); newid_shared_memory = shmget(IPC_PRIVATE,sizeof(int),IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR ); /* Shared Memory Attach */ int * newid = (int *) shmat(newid_shared_memory,0,0); data_person * users = (data_person *) shmat(users_shared_memory,0,0); /* Shared Memory initialization */ *newid=0; /* Load data from the database */ load_database(users,newid); /* Shared Memory Detach */ shmdt(newid); shmdt(users); /*Client-Server comunication*/ if(initialize()==ERROR) { printf("The port 49500 was not available\n"); sigint_handler(0); } start_connection(&application_request,MAX_QUEUE); return 0; }