/** * Shut down the system. * * Terminates all running processes, flushes and unmounts all filesystems, and * then performs the specified action. The CAP_SHUTDOWN capability is required * to use this function. * * @param action Action to perform once the system has been shut down. * * @return Status code describing result of the operation. Will * always succeed unless the calling process does not have * the CAP_SHUTDOWN capability. */ status_t kern_shutdown(int action) { if(!cap_check(NULL, CAP_SHUTDOWN)) return STATUS_PERM_DENIED; system_shutdown(action); fatal("Shouldn't get here"); }
result_t handle_system_event(const char *request, size_t req_len) { // tokenize request message size_t num_tokens = 0; char **tokens = str_split(request, ": ", &num_tokens); if(num_tokens < 2) { fprintf(stderr, "Could not find correct number of tokens in rpc system request."); return FAILURE; } if(strncmp(tokens[1], ACTION_VOLUME_MUTE, sizeof(ACTION_VOLUME_MUTE)-1) == 0) { audio_output_mute(!audio_input_is_muted()); } else if(strncmp(tokens[1], ACTION_VOLUME_LEVEL, sizeof(ACTION_VOLUME_LEVEL)-1) == 0) { if(num_tokens < 3) { fprintf(stderr, "Could not find correct number of tokens for rpc system request's action volume level"); return FAILURE; } float volume_level = atoi(tokens[2])/100.0; volume_level = (volume_level < 0.0f) ? 0.0f : (volume_level > 1.0f) ? 1.0f : volume_level; audio_output_set_volume(volume_level); } else if(strncmp(tokens[1], ACTION_SHUT_DOWN, sizeof(ACTION_SHUT_DOWN)-1) == 0) { system_shutdown(); } else if(strncmp(tokens[1], ACTION_RESTART, sizeof(ACTION_RESTART)-1) == 0) { system_restart(); } else if(strncmp(tokens[1], ACTION_SLEEP, sizeof(ACTION_SLEEP)-1) == 0) { system_sleep(); } else if(strncmp(tokens[1], ACTION_LOG_OUT, sizeof(ACTION_LOG_OUT)-1) == 0) { system_logout(); } // free tokens free_tokens(tokens, num_tokens); }
status_t _user_shutdown(bool reboot) { if (geteuid() != 0) return B_NOT_ALLOWED; return system_shutdown(reboot); }
static void exit_normaly() { system_shutdown(); #ifdef MULTICHILD child_data = NULL; dettach_childs_queue(childs_queue); #endif }
static void signal_handler(int sig) { switch(sig) { case SIGHUP: case SIGTERM: system_shutdown(); break; } }
/************************************************** Shutdown everything properly ***************************************************/ void shutdown(void) { /* system shutdown */ memfile_autosave(-1,config.state_auto); system_shutdown(); audio_shutdown(); free(cart.rom); gx_audio_Shutdown(); gx_video_Shutdown(); #ifdef HW_RVL DI_Close(); #endif }
void Application::destroy() { LOGD("Start Application.destroy()"); // save sram saveSRam(_sramDir); // shutdown genplus system_shutdown(); // set to false _romLoaded = false; LOGD("Finished Application.destroy()"); }
static void genesis_leave() { if (gensettings.saves & SAVES_SRAM) { /* save SRAM */ save_sram(sramname); } else{ if(WindowPrompt("Save", "Save SRAM?", "Save", "Don't Save")){ save_sram(sramname); } } if (gensettings.saves & SAVES_STATES) { save_state(statename); } else{ if(WindowPrompt("Save", "Save Save State?", "Save", "Don't Save")){ save_state(statename); } } system_shutdown(); // audio_shutdown(); error_shutdown(); free(cart.rom); }
gint intercept_shutdown(gint fd, gint how) { return system_shutdown(fd, how); }
int start_server() { int child_indx, pid, i, ctl_socket; int childs, freeservers, used, maxrequests, ret; char command_buffer[COMMANDS_BUFFER_SIZE]; int user_informed = 0; ctl_socket = ci_named_pipe_create(CONF.COMMANDS_SOCKET); if (ctl_socket < 0) { ci_debug_printf(1, "Error opening control socket %s. Fatal error, exiting!\n", CONF.COMMANDS_SOCKET); exit(0); } if (!ci_proc_mutex_init(&accept_mutex)) { ci_debug_printf(1, "Can't init mutex for accepting conenctions. Fatal error, exiting!\n"); exit(0); } childs_queue = malloc(sizeof(struct childs_queue)); if (!create_childs_queue(childs_queue, 2 * CONF.MAX_SERVERS)) { ci_proc_mutex_destroy(&accept_mutex); ci_debug_printf(1, "Can't init shared memory. Fatal error, exiting!\n"); exit(0); } init_commands(); pid = 1; #ifdef MULTICHILD if (CONF.START_SERVERS > CONF.MAX_SERVERS) CONF.START_SERVERS = CONF.MAX_SERVERS; for (i = 0; i < CONF.START_SERVERS; i++) { if (pid) pid = start_child(LISTEN_SOCKET); } if (pid != 0) { main_signals(); while (1) { if ((ret = wait_for_commands(ctl_socket, command_buffer, 1)) > 0) { ci_debug_printf(5, "I received the command: %s\n", command_buffer); handle_monitor_process_commands(command_buffer); } if (ret < 0) { /*Eof received on pipe. Going to reopen ... */ ci_named_pipe_close(ctl_socket); ctl_socket = ci_named_pipe_open(CONF.COMMANDS_SOCKET); if (ctl_socket < 0) { ci_debug_printf(1, "Error opening control socket. We are unstable and going down!"); c_icap_going_to_term = 1; } } if (c_icap_going_to_term) break; childs_queue_stats(childs_queue, &childs, &freeservers, &used, &maxrequests); ci_debug_printf(10, "Server stats: \n\t Children: %d\n\t Free servers: %d\n" "\tUsed servers:%d\n\tRequests served:%d\n", childs, freeservers, used, maxrequests); if (MAX_REQUESTS_PER_CHILD > 0 && (child_indx = find_a_child_nrequests (childs_queue, MAX_REQUESTS_PER_CHILD)) >= 0) { ci_debug_printf(8, "Max requests reached for child :%d of pid :%d\n", child_indx, childs_queue->childs[child_indx].pid); pid = start_child(LISTEN_SOCKET); // usleep(500); childs_queue->childs[child_indx].father_said = GRACEFULLY; /*kill a server ... */ kill(childs_queue->childs[child_indx].pid, SIGTERM); } else if ((freeservers <= CONF.MIN_SPARE_THREADS && childs < CONF.MAX_SERVERS) || childs < CONF.START_SERVERS) { ci_debug_printf(8, "Free Servers: %d, children: %d. Going to start a child .....\n", freeservers, childs); pid = start_child(LISTEN_SOCKET); } else if (freeservers >= CONF.MAX_SPARE_THREADS && childs > CONF.START_SERVERS && (freeservers - CONF.THREADS_PER_CHILD) > CONF.MIN_SPARE_THREADS) { if ((child_indx = find_an_idle_child(childs_queue)) >= 0) { childs_queue->childs[child_indx].father_said = GRACEFULLY; ci_debug_printf(8, "Free Servers: %d, children: %d. Going to stop child %d(index: %d)\n", freeservers, childs, childs_queue->childs[child_indx].pid, child_indx); /*kill a server ... */ kill(childs_queue->childs[child_indx].pid, SIGTERM); user_informed = 0; } } else if (childs == CONF.MAX_SERVERS && freeservers < CONF.MIN_SPARE_THREADS) { if(! user_informed) { ci_debug_printf(1, "ATTENTION!!!! Not enough available servers (children %d, free servers %d, used servers %d)!!!!! " "Maybe you should increase the MaxServers and the " "ThreadsPerChild values in c-icap.conf file!!!!!!!!!",childs , freeservers, used); user_informed = 1; } } if (c_icap_going_to_term) break; check_for_exited_childs(); if (c_icap_reconfigure) { c_icap_reconfigure = 0; if (!server_reconfigure()) { ci_debug_printf(1, "Error while reconfiguring, exiting!\n"); break; } } } /*Main process exit point */ ci_debug_printf(1, "Possibly a term signal received. Monitor process going to term all children\n"); kill_all_childs(); system_shutdown(); ci_debug_printf(1, "Exiting....\n"); return 1; } #else child_data = (child_shared_data_t *) malloc(sizeof(child_shared_data_t)); child_data->pid = 0; child_data->freeservers = CONF.THREADS_PER_CHILD; child_data->usedservers = 0; child_data->requests = 0; child_data->connections = 0; child_data->to_be_killed = 0; child_data->father_said = 0; child_data->idle = 1; child_data->stats_size = ci_stat_memblock_size(); child_data->stats = malloc(child_data->stats_size); child_data->stats->sig = MEMBLOCK_SIG; ci_stat_attach_mem(child_data->stats, child_data->stats_size, NULL); child_main(LISTEN_SOCKET, 0); ci_proc_mutex_destroy(&accept_mutex); destroy_childs_queue(childs_queue); #endif return 1; }
/*--------------------------------------------------------------------------*/ int main(int argc, char **argv) { /* * Initalize hardware. */ msp430_cpu_init(); clock_init(); uart_init(9600); /* Must come before first printf */ /* xmem_init(); */ PRINTF("iWatch 0.10 build at " __TIME__ " " __DATE__ "\n"); UCSCTL8 &= ~BIT2; /* * Hardware initialization done! */ /* * Initialize Contiki and our processes. */ process_init(); process_start(&etimer_process, NULL); rtimer_init(); ctimer_init(); energest_init(); ENERGEST_ON(ENERGEST_TYPE_CPU); backlight_init(); battery_init(); SPI_FLASH_Init(); if (system_testing()) { clock_time_t t; backlight_on(200, 0); t = clock_seconds(); // sleep 1 while(clock_seconds() - t <= 3); printf("$$OK BACKLIGHT\n"); t = clock_seconds(); while(clock_seconds() - t <= 3); backlight_on(0, 0); motor_on(200, 0); // sleep 1s t = clock_seconds(); while(clock_seconds() - t <= 3); printf("$$OK MOTOR\n"); t = clock_seconds(); while(clock_seconds() - t <= 3); motor_on(0, 0); #if PRODUCT_W001 I2C_Init(); codec_init(); codec_bypass(1); // sleep 1s t = clock_seconds(); while(clock_seconds() - t <= 3); printf("$$OK MIC\n"); // sleep 1s t = clock_seconds(); while(clock_seconds() - t <= 3); codec_bypass(0); codec_shutdown(); #endif } int reason = CheckUpgrade(); window_init(reason); button_init(); rtc_init(); CFSFontWrapperLoad(); system_init(); // check system status and do factor reset if needed I2C_Init(); //codec_init(); //ant_init(); bluetooth_init(); #ifdef PRODUCT_W004 //bmx_init(); #else mpu6050_init(); #endif // check the button status if (button_snapshot() & (1 << BUTTON_UP)) { clock_time_t t; // delay 1 second // button up is pressed, we will set emerging flag motor_on(200, CLOCK_SECOND * 2); t = clock_seconds(); while(clock_seconds() - t <= 1); if (button_snapshot() & (1 << BUTTON_UP)) system_setemerging(); motor_on(0, 0); } if (!system_retail()) { bluetooth_discoverable(1); } #if PRODUCT_W001 if (system_testing()) ant_init(MODE_HRM); #endif system_restore(); // protocol_init(); // protocol_start(1); process_start(&system_process, NULL); /* * This is the scheduler loop. */ msp430_dco_required = 0; /* check firmware update */ if (reason == 0xff) { printf("Start Upgrade\n"); Upgrade(); // never return if sucessfully upgrade } watchdog_start(); while(1) { int r; do { /* Reset watchdog. */ watchdog_periodic(); r = process_run(); } while(r > 0); /* * Idle processing. */ int s = splhigh(); /* Disable interrupts. */ /* uart1_active is for avoiding LPM3 when still sending or receiving */ if(process_nevents() != 0) { splx(s); /* Re-enable interrupts. */ } else { static unsigned long irq_energest = 0; /* Re-enable interrupts and go to sleep atomically. */ ENERGEST_OFF(ENERGEST_TYPE_CPU); ENERGEST_ON(ENERGEST_TYPE_LPM); /* We only want to measure the processing done in IRQs when we are asleep, so we discard the processing time done when we were awake. */ energest_type_set(ENERGEST_TYPE_IRQ, irq_energest); watchdog_stop(); if (shutdown_mode) { system_shutdown(1); // never return LPM4; } if (msp430_dco_required) { __low_power_mode_0(); } else { __low_power_mode_3(); } /* We get the current processing time for interrupts that was done during the LPM and store it for next time around. */ __disable_interrupt(); irq_energest = energest_type_time(ENERGEST_TYPE_IRQ); __enable_interrupt(); watchdog_start(); ENERGEST_OFF(ENERGEST_TYPE_LPM); ENERGEST_ON(ENERGEST_TYPE_CPU); } } }