int main(int argc, const char * argv[]) { // Setup listening to SIGTERM setup_signal_handling(SIGTERM, handle_signal); // Get the patient name if (argc <= 1) { // Name not given in argv, so prompt user for it printf("Patient name: "); size_t buff_size; char* buffer; getline(&buffer, &buff_size, stdin); strncpy(patient_name, buffer, NAME_MAX_LENGTH); } else { // Get name from argv strncpy(patient_name, argv[1], NAME_MAX_LENGTH); } if (false == init()) { dump("Finished"); return 1; } dump("Starting monitor for patient: %s", patient_name); fork_process(parent_main, child_main); return 0; }
void child_main() { // Ignore control-C in child setup_signal_handling(SIGINT, SIG_IGN); // Create the message queue msg_queue_id = msgget(MESSAGE_QUEUE_KEY, 0666 | IPC_CREAT); if (msg_queue_id == -1) { // Invalid message queue dump("Failed to open/create controller queue."); send_signal(SIGTERM, true); return; } while(running) { child_loop(); } // Delete the message queue dump("Removing message queue"); if (-1 == msgctl(msg_queue_id, IPC_RMID, 0)) { dump("Failed to delete message queue"); } dump("Finished"); }
int main(int args, char** argv) { Message message = {0}; setup_signal_handling(); create_queues(); receive(&message); exit_server(); return 0; }
bool init() { // Setup inital signal handling setup_signal_handling(SIGINT, handle_signal); setup_signal_handling(SIGTERM, handle_signal); // Create controller FIFO if (-1 == mkfifo(CONTROLLER_FIFO_NAME, 0777)) { dump("Failed to create controller FIFO with error: %d", errno); return false; } // Create message queue msg_queue_id = msgget(MESSAGE_QUEUE_KEY, 0666 | IPC_CREAT); if (-1 == msg_queue_id) { dump("Failed to setup mesage queue with error: %d", errno); unlink(CONTROLLER_FIFO_NAME); return false; } return true; }
void parent_main() { // Ignore control-C in child setup_signal_handling(SIGINT, SIG_IGN); while(running) { parent_loop(); } // Delete the monitor fifo dump("Removing fifo file: %s", monitor_fifo_name); unlink(monitor_fifo_name); dump("Parent finished"); }
int pup_heap_init(struct PupHeap *heap) { fprintf(stderr, "pup_heap_init() pid=%d\n", getpid()); int res; res = pthread_key_create(&heap->this_thread_info, NULL /* no dtor */); if (res) return res; heap->region_list = NULL; heap->thread_list = 0; // initialisation for the main thread, res = pup_heap_thread_init(heap); if (res) { // ignore return value, since we're cleaning up anyway, pthread_key_delete(heap->this_thread_info); return res; } res = setup_signal_handling(heap); if (res) { pthread_key_delete(heap->this_thread_info); return res; } res = heap_thread_start(heap); if (res) { pup_heap_thread_destroy(heap); pthread_key_delete(heap->this_thread_info); return res; } res = pthread_barrier_init(&heap->safepoint_barrier, NULL, 2); // FIXME: proper error handling, ABORTF_ON(res, "pthread_barrier_init() failed: %s", strerror(errno)); ANNOTATE_BARRIER_INIT(&heap->safepoint_barrier, 2, false); struct PupGCState *gc_state = pup_gc_state_create(); // FIXME: proper error handling, ABORT_ON(!gc_state, "pup_gc_state_create() failed"); set_gc_state(heap, gc_state); return 0; }
void child_main() { // Ignore control-C in child setup_signal_handling(SIGINT, SIG_IGN); // Child setup srand(RAND_SEED); // Intializes random number generator // Get message queue msg_queue_id = msgget(MESSAGE_QUEUE_KEY, 0666); if (msg_queue_id == -1) { // Invalid message queue dump("Cannot open controller queue. Is controller running?"); send_signal(SIGTERM, true); // Tell parent to stop return; } // Start loop while(running) { child_loop(); } dump("Child finished"); }
void become_daemon() { int fork_pid = fork(); ensure_perror(fork_pid != -1, "Couldn't fork"); if (fork_pid == 0) { set_new_sid(); setup_signal_handling(); int pid = fork(); ensure_perror(pid != -1, "Couldn't fork"); if (pid == 0) { change_working_dir(); close_std_io(); umask(0); } else { write_daemon_pid(pid); exit(0); } } else { exit(0); } }
int main( int argc, char *argv[]) { while( 1) { int c = getopt( argc, argv, "vfmic:p:"); if( c == 'v') VFLAG++; else if( c == 'f') background = 0; else if( c == 'c') config_file = optarg; else if( c == 'p') pid_file = optarg; else if( c == -1) break; else bailout( "unknown option [%c]", c); } setup_signal_handling(); load_config(); if( CF_output_policy != OP_SPECTRUM) { int i; struct BAND *b; for( i=0, b=bands; i<nbands; i++, b++) report( 1, "band %s %d %d %s", b->ident, b->start, b->end, b->side->name); } if( background && !logfile) report( -1, "warning: no logfile specified for daemon"); if( background) make_daemon(); setup_input_stream(); DF = (double) CF_sample_rate/(double) FFTWID; report( 1, "resolution: bins=%d fftwid=%d df=%f", CF_bins, FFTWID, DF); if( CF_uspec_file) { // Convert CF_uspec_secs seconds to uspec_max frames uspec_max = rint( CF_uspec_secs * CF_sample_rate / FFTWID); report( 2, "utility spectrum interval: %d frames", uspec_max); report( 2, "utility spectrum file: %s", CF_uspec_file); } // Convert CF_output_interval seconds to output_int frames output_int = rint( CF_output_interval * CF_sample_rate / FFTWID); if( output_int == 0) output_int = 1; report( 2, "output interval: %d frames", output_int); if( CF_output_policy == OP_SPECTRUM) { // Convert range variables Hertz to bins cuton = CF_range1 / DF; cutoff = CF_range2 / DF; report( 2, "output bins: %d to %d", cuton, cutoff); } // Both sets of channel data structures are initialised, even if mono initialise_channel( &left); initialise_channel( &right); setup_hamming_window(); if( CF_card_delay) sleep( CF_card_delay); report( 0, "sidc version %s %s: starting work", PACKAGE_VERSION, soundsystem); alert_on = 1; if( CF_priority) set_scheduling(); // Setup real time scheduling process_signal(); if( CF_output_policy != OP_SPECTRUM) { int i; struct BAND *b; for( i=0, b=bands; i<nbands; i++, b++) free( b->ident); } if( CF_uspec_file) free( CF_uspec_file); if( CF_mailaddr) free( CF_mailaddr); if( out_prefix) free( out_prefix); return 0; }
int main(int argc, char *argv[]) { int i, rc; if (argc < 3) { usage(); rc = 1; goto out; } rc = setup_signal_handling(); if (rc != 0) { fprintf(stderr, "FAIL: Couldn't set up signal handler\n"); rc = 1; goto out; } for (i = 1; i < argc && interface == NULL; i++) { char *arg = argv[i]; if (strcmp(arg, "--system") == 0) { type = DBUS_BUS_SYSTEM; session_or_system = TRUE; } else if (strcmp(arg, "--session") == 0) { type = DBUS_BUS_SESSION; session_or_system = TRUE; } else if (strstr(arg, "--address") == arg) { address = strchr(arg, '='); if (address == NULL) { fprintf(stderr, "FAIL: \"--address=\" requires an ADDRESS\n"); usage(); rc = 1; goto out; } else { address = address + 1; } } else if (strstr(arg, "--name=") == arg) name = strchr(arg, '=') + 1; else if (strstr(arg, "--log=") == arg) { char *path = strchr(arg, '=') + 1; log_fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); if (log_fd < 0) { fprintf(stderr, "FAIL: Couldn't open log file \"%s\"\n", path); exit(1); } } else if (strstr(arg, "--lock-fd=") == arg) { char *fd = strchr(arg, '=') + 1; lock_fd = atoi(fd); } else if (!strcmp(arg, "--help")) { usage(); rc = 0; goto out; } else if (arg[0] == '-') { usage(); rc = 1; goto out; } else if (path == NULL) path = arg; else /* interface == NULL guaranteed by the 'while' loop */ interface = arg; } if (name == NULL || path == NULL || interface == NULL || i < argc) { usage(); rc = 1; goto out; } if (session_or_system && (address != NULL)) { fprintf(stderr, "FAIL: \"--address\" may not be used with \"--system\" or \"--session\"\n"); usage(); rc = 1; goto out; } dbus_error_init(&error); if (address != NULL) connection = dbus_connection_open(address, &error); else connection = dbus_bus_get(type, &error); if (connection == NULL) { fprintf(stderr, "FAIL: Failed to open connection to \"%s\" message bus: %s\n", address ? address : ((type == DBUS_BUS_SYSTEM) ? "system" : "session"), error.message); dbus_error_free(&error); rc = 1; goto out; } else if (address != NULL) dbus_bus_register(connection, &error); rc = do_service(); out: if (connection) dbus_connection_unref(connection); unlock_fd(); if (rc == 0) printf("PASS\n"); exit(rc); }
int main (int argc, char *argv[]) { GMainContext *ctx; GMainLoop *loop; /* * The mate-keyring startup is not as simple as I wish it could be. * * It's often started in the primordial stages of a session, where * there's no DBus, and no proper X display. This is the strange world * of PAM. * * When started with the --login option, we do as little initialization * as possible. We expect a login password on the stdin, and unlock * or create the login keyring. * * Then later we expect mate-keyring-dameon to be run again with the * --start option. This second mate-keyring-daemon will hook the * original daemon up with environment variables necessary to initialize * itself and bring it into the session. This second daemon usually exits. * * Without either of these options, we follow a more boring and * predictable startup. */ /* * Before we do ANYTHING, we drop privileges so we don't become * a security issue ourselves. */ gkd_capability_obtain_capability_and_drop_privileges (); #ifdef WITH_TESTS g_setenv ("DBUS_FATAL_WARNINGS", "1", FALSE); if (!g_getenv ("G_DEBUG")) g_log_set_always_fatal (G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING); #endif g_type_init (); g_thread_init (NULL); #ifdef HAVE_LOCALE_H /* internationalisation */ setlocale (LC_ALL, ""); #endif #ifdef HAVE_GETTEXT bindtextdomain (GETTEXT_PACKAGE, MATELOCALEDIR); textdomain (GETTEXT_PACKAGE); bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8"); #endif egg_libgcrypt_initialize (); /* Send all warning or error messages to syslog */ prepare_logging (); parse_arguments (&argc, &argv); /* The --version option. This is machine parseable output */ if (run_version) { g_print ("mate-keyring-daemon: %s\n", VERSION); g_print ("testing: %s\n", #ifdef WITH_TESTS "enabled"); #else "disabled"); #endif exit (0); } /* The --start option */ if (run_for_start) { if (discover_other_daemon (initialize_daemon_at, TRUE)) { /* * Another daemon was initialized, print out environment * for any callers, and quit or go comatose. */ print_environment (0); if (run_foreground) while (sleep(0x08000000) == 0); cleanup_and_exit (0); } /* The --replace option */ } else if (run_for_replace) { discover_other_daemon (replace_daemon_at, FALSE); if (control_directory) g_message ("replacing daemon at: %s", control_directory); } /* Initialize the main directory */ gkd_util_init_master_directory (control_directory); /* Initialize our daemon main loop and threading */ loop = g_main_loop_new (NULL, FALSE); ctx = g_main_loop_get_context (loop); /* Initialize our control socket */ if (!gkd_control_listen ()) return FALSE; /* The --login option. Delayed initialization */ if (run_for_login) { login_password = read_login_password (STDIN); atexit (clear_login_password); timeout_id = g_timeout_add_seconds (LOGIN_TIMEOUT, (GSourceFunc) on_login_timeout, NULL); /* Not a login daemon. Startup stuff now.*/ } else { /* These are things that can run before forking */ if (!gkr_daemon_startup_steps (run_components)) cleanup_and_exit (1); } /* The whole forking and daemonizing dance starts here. */ fork_and_print_environment(); setup_signal_handling (loop); /* Prepare logging a second time, since we may be in a different process */ prepare_logging(); /* Remainder initialization after forking, if initialization not delayed */ if (!run_for_login) { gkr_daemon_initialize_steps (run_components); } g_main_loop_run (loop); /* This wraps everything up in order */ egg_cleanup_perform (); /* Wrap up signal handling here */ cleanup_signal_handling (); g_free (control_directory); return 0; }