int initialize_director_api(void) { printf("Initializing director\n"); if ( initialize_netlink_family() ) return -EINVAL; // Registers self into the kernel if ( register_pid(getpid()) ) { return -EBUSY; } printf("Generic netlink channel for director number is: %d\n", state.gnl_fid); handlers[DIRECTOR_CHECK_NPM] = handle_npm_check; handlers[DIRECTOR_CHECK_FULL_NPM] = handle_npm_check_full; handlers[DIRECTOR_NODE_CONNECTED] = handle_node_connected; handlers[DIRECTOR_NODE_DISCONNECTED] = handle_node_disconnected; handlers[DIRECTOR_IMMIGRATION_REQUEST] = handle_immigration_request; handlers[DIRECTOR_IMMIGRATION_CONFIRMED] = handle_immigration_confirmed; handlers[DIRECTOR_TASK_EXIT] = handle_task_exitted; handlers[DIRECTOR_TASK_FORK] = handle_task_forked; handlers[DIRECTOR_MIGRATED_HOME] = handle_migrated_home; handlers[DIRECTOR_EMIGRATION_FAILED] = handle_emigration_failed; handlers[DIRECTOR_GENERIC_USER_MESSAGE] = handle_generic_user_message; printf("Comm channel initialized\n"); return 0; }
void finish (int eval) { register_pid (0, "mondo"); chdir ("/"); run_program_and_log_output (static_cast <char*> ("umount /mnt/cdrom"), true); printf ("See %s for details of backup run.", MONDO_LOGFILE); exit (eval); /*NOTREACHED*/ }
int main(int argc, char *argv[]) { struct sockaddr_storage their_addr; socklen_t addr_size; int socket_fd, time_stamp, num_bytes = 1; char output_buffer[100000]; struct timespec initial_time, current_time; int new_fd; int recv_len = 1; double current_minus_initial = 0; pid_t my_pid; FILE *input_csv = NULL; int frame_size = 0; my_pid = syscall(__NR_gettid); if(register_pid(my_pid) != 1) { perror("register pid failed"); exit(1); } socket_fd = initialize_connection(); input_csv = fopen(MSG_SIZES_FILENAME, "r"); if(input_csv == NULL) { perror("fopen error"); exit(1); } addr_size = sizeof their_addr; new_fd = accept(socket_fd, (struct sockaddr *)&their_addr, &addr_size); yield_pid(my_pid); clock_gettime(CLOCK_REALTIME, &initial_time); while(fscanf(input_csv, "%d\n", &frame_size) != EOL) { recv_len = recv(new_fd, &output_buffer, frame_size, MSG_WAITALL); clock_gettime(CLOCK_REALTIME, ¤t_time); current_minus_initial = (current_time.tv_sec - initial_time.tv_sec) * 1000.0; current_minus_initial += (current_time.tv_nsec - initial_time.tv_nsec) / 1000000.0; printf("%d at %f\n", recv_len, current_minus_initial); yield_pid(my_pid); } unregister_pid(my_pid); fclose(proc_file); close(socket_fd); return 0; }
int main(int argc, char*argv[]) /* Purpose: main subroutine Parameters: none Return: result (0=success, nonzero=failure) */ { pthread_t server_status_thread; parse_options(argc, argv); log_it(info, "---------- Monitas (server) by Hugo Rabson ----------"); register_pid(getpid(), "server"); set_signals(true); start_threads_to_watch_ports_for_requests(); pthread_create(&server_status_thread, NULL, generate_server_status_file_regularly, (void*)g->server_status_file); create_and_watch_fifo_for_commands(g->server_comdev); log_it(warn, "Execution should never reach this point"); exit(0); }
/** * Exit Mondo normally. * @param signal The exit code (0 indicates a successful backup; 1 for Mondo means the * user aborted; 254 means a fatal error occured). * @note This function never returns. */ void finish(int signal) { char *command = NULL; /* if (signal==0) { popup_and_OK("Please press <enter> to quit."); } */ /* newtPopHelpLine(); */ register_pid(0, "mondo"); chdir("/"); run_program_and_log_output("umount " MNT_CDROM, FALSE); run_program_and_log_output("rm -Rf /mondo.scratch.* /mondo.tmp.*", FALSE); if (g_erase_tmpdir_and_scratchdir) { run_program_and_log_output(g_erase_tmpdir_and_scratchdir, 1); } if (g_selfmounted_isodir) { asprintf(&command, "umount %s", g_selfmounted_isodir); run_program_and_log_output(command, 1); asprintf(&command, "rmdir %s", g_selfmounted_isodir); run_program_and_log_output(command, 1); } // iamhere("foo"); /* system("clear"); */ // iamhere("About to call newtFinished"); if (!g_text_mode) { if (does_file_exist("/THIS-IS-A-RAMDISK")) { log_msg(1, "Calling newtFinished()"); newtFinished(); } else { log_msg(1, "Calling newtSuspend()"); newtSuspend(); } } // system("clear"); // iamhere("Finished calling newtFinished"); printf(_("Execution run ended; result=%d\n"), signal); printf(_("Type 'less %s' to see the output log\n"), MONDO_LOGFILE); free_libmondo_global_strings(); exit(signal); }
void terminate_daemon(int sig) /* Purpose: Shut down the server in response to interrupt. Params: Signal received. Returns: None */ { char command[MAX_STR_LEN+1]; static bool logged_out_everyone=false; set_signals(false); // termination in progress log_it(info, "Abort signal caught by interrupt handler"); if (!logged_out_everyone) { // FIXME - lock the var w/mutex logged_out_everyone=true; forcibly_logout_all_clients(); } /* for(i=0; i<NOOF_THREADS; i--) { sprintf(tmp, "Terminating thread #%d", i); log_it(debug, tmp); pthread_join(g_threadinfo[i], NULL); } */ forcibly_logout_all_clients(); sprintf(command, "rm -f %s", g_command_fifo); call_program_and_log_output(command); // chmod(g_command_fifo, 0); unlink(g_command_fifo); unlink(g_server_status_file); register_pid(0, "server"); log_it(info, "---------- Monitas (server) has terminated ----------"); exit(0); }
/* main loop of the daemon*/ int init_start () { int ssock; struct sockaddr_in saddr; GIOChannel* sch; /* register pid */ if (cl_lock_pidfile(PID_FILE) < 0) { quorum_log(LOG_ERR, "already running: [pid %d]." , cl_read_pidfile(PID_FILE)); quorum_log(LOG_ERR, "Startup aborted (already running)." "Shutting down."); exit(100); } register_pid(FALSE, sigterm_action); /* enable coredumps */ quorum_log(LOG_DEBUG, "Enabling coredumps"); cl_cdtocoredir(); cl_enable_coredumps(TRUE); cl_set_all_coredump_signal_handlers(); /* initialize gnutls */ initialize_tls_global(); /* enable dynamic up/down debug level */ G_main_add_SignalHandler(G_PRIORITY_HIGH, SIGUSR1, sig_handler, NULL, NULL); G_main_add_SignalHandler(G_PRIORITY_HIGH, SIGUSR2, sig_handler, NULL, NULL); G_main_add_SignalHandler(G_PRIORITY_HIGH, SIGHUP, sig_handler, NULL, NULL); /* create the mainloop */ mainloop = g_main_new(FALSE); /* create the protocal table */ protocols = g_hash_table_new(g_str_hash, g_str_equal); /* create server socket */ ssock = socket(AF_INET, SOCK_STREAM, 0); if (ssock == -1) { quorum_log(LOG_ERR, "Can not create server socket." "Shutting down."); exit(100); } /* bind server socket*/ memset(&saddr, '\0', sizeof(saddr)); saddr.sin_family = AF_INET; saddr.sin_addr.s_addr = INADDR_ANY; saddr.sin_port = htons(PORT); if (bind(ssock, (struct sockaddr*)&saddr, sizeof(saddr)) == -1) { quorum_log(LOG_ERR, "Can not bind server socket." "Shutting down."); exit(100); } if (listen(ssock, 10) == -1) { quorum_log(LOG_ERR, "Can not start listen." "Shutting down."); exit(100); } /* create source for server socket and add to the mainloop */ sch = g_io_channel_unix_new(ssock); g_io_add_watch(sch, G_IO_IN|G_IO_ERR|G_IO_HUP, on_listen, NULL); /* run the mainloop */ quorum_log(LOG_DEBUG, "main: run the loop..."); quorum_log(LOG_INFO, "Started."); g_main_run(mainloop); /* exit, clean the pid file */ if (cl_unlock_pidfile(PID_FILE) == 0) { quorum_log(LOG_DEBUG, "[%s] stopped", QUORUMD); } return 0; }
/** * Exit Mondo with a fatal error. * @param error_string The error message to present to the user before exiting. * @note This function never returns. */ void fatal_error(char *error_string) { /*@ buffers ***************************************************** */ char *fatalstr; char *tmp; char *command; static bool already_exiting = FALSE; int i; /*@ end vars **************************************************** */ asprintf(&fatalstr, "-------FATAL ERROR---------"); set_signals(FALSE); // link to external func g_exiting = TRUE; log_msg(1, "%s - '%s'", fatalstr, error_string); printf("%s - %s\n", fatalstr, error_string); if (getpid() == g_mastermind_pid) { log_msg(2, "mastermind %d is exiting", (int) getpid()); kill(g_main_pid, SIGTERM); finish(1); } if (getpid() != g_main_pid) { if (g_mastermind_pid != 0 && getpid() != g_mastermind_pid) { log_msg(2, "non-m/m %d is exiting", (int) getpid()); kill(g_main_pid, SIGTERM); finish(1); } } log_msg(3, "OK, I think I'm the main PID."); if (already_exiting) { log_msg(3, "...I'm already exiting. Give me time, Julian!"); finish(1); } already_exiting = TRUE; log_msg(2, "I'm going to do some cleaning up now."); paranoid_system("killall mindi 2> /dev/null"); kill_anything_like_this("/mondo/do-not"); kill_anything_like_this("mondo.tmp"); kill_anything_like_this("ntfsclone"); sync(); asprintf(&tmp, "umount %s", g_tmpfs_mountpt); chdir("/"); for (i = 0; i < 10 && run_program_and_log_output(tmp, 5); i++) { log_msg(2, "Waiting for child processes to terminate"); sleep(1); run_program_and_log_output(tmp, 5); } paranoid_free(tmp); if (g_erase_tmpdir_and_scratchdir) { run_program_and_log_output(g_erase_tmpdir_and_scratchdir, 5); } if (g_selfmounted_isodir) { asprintf(&command, "umount %s", g_selfmounted_isodir); run_program_and_log_output(command, 5); asprintf(&command, "rmdir %s", g_selfmounted_isodir); run_program_and_log_output(command, 5); } if (!g_text_mode) { log_msg(0, fatalstr); log_msg(0, error_string); // popup_and_OK (error_string); newtFinished(); } system ("gzip -9c "MONDO_LOGFILE" > /tmp/MA.log.gz 2> /dev/null"); printf (_("If you require technical support, please contact the mailing list.\n")); printf(_("See http://www.mondorescue.org for details.\n")); printf (_("The list's members can help you, if you attach that file to your e-mail.\n")); printf(_("Log file: %s\n"), MONDO_LOGFILE); if (does_file_exist("/tmp/MA.log.gz")) { printf (_("FYI, I have gzipped the log and saved it to /tmp/MA.log.gz\n")); } printf(_("Mondo has aborted.\n")); register_pid(0, "mondo"); // finish() does this too, FYI if (!g_main_pid) { log_msg(3, "FYI - g_main_pid is blank"); } finish(254); }
void fatal_error_sub (char *error) { static bool already_exiting = false; char tmp[MAX_STR_LEN]; g_exiting = TRUE; log_it ("Fatal error received - '%s'", error); printf ("Fatal error... %s\n", error); if (getpid() == g_mastermind_pid) { log_it ("(FE) mastermind %d is exiting", (int)getpid()); kill (g_main_pid, SIGTERM); finish (1); } if (getpid() != g_main_pid) { if (g_mastermind_pid != 0 && getpid() != g_mastermind_pid) { log_it ("(FE) non-m/m %d is exiting", (int)getpid()); kill (g_main_pid, SIGTERM); finish (1); } if (getpid() != g_main_pid) { log_it ("(FE) aux pid %d is exiting", (int)getpid()); kill (g_main_pid, SIGTERM); finish (1); } } log_it ("OK, I think I'm the main PID."); if (already_exiting) { log_it ("...I'm already exiting. Give me time, Julian!"); finish (1); } already_exiting = TRUE; log_it ("I'm going to do some cleaning up now."); kill_anything_like_this ("mondoarchive"); kill_anything_like_this ("/mondo/do-not"); kill_anything_like_this ("tmp.mondo"); sync(); sprintf (tmp, "umount %s", g_tmpfs_mountpt); chdir ("/"); for(int i=0; i<10 && run_program_and_log_output (tmp, TRUE); i++) { log_it ("Waiting for child processes to terminate"); sleep (1); run_program_and_log_output (tmp, TRUE); } if (g_erase_tmpdir_and_scratchdir[0]) { run_program_and_log_output (g_erase_tmpdir_and_scratchdir, TRUE); } QMessageBox::critical (0, "XMondo", QString ("<font color=\"red\"><b>FATAL ERROR</b></font><br>%1").arg (error), "Quit", 0, 0, 0, 0); kapp->quit(); printf ("---FATAL ERROR--- %s\n", error); system ("cat /var/log/mondo-archive.log | gzip -9 > /tmp/MA.log.gz 2> /dev/null"); printf ("If you require technical support, please contact the mailing list.\n"); printf ("See http://www.mondorescue.org for details.\n"); printf ("Log file: %s\n", MONDO_LOGFILE); if (does_file_exist ("/tmp/MA.log.gz")) { printf ("FYI, I have gzipped the log and saved it to /tmp/MA.log.gz\n"); printf ("The list's members can help you, if you attach that file to your e-mail.\n"); } printf ("Mondo has aborted.\n"); register_pid (0, "mondo"); if (!g_main_pid) { log_it ("FYI - g_main_pid is blank"); } finish (254); /*NOTREACHED*/ }
int waitclient(const char* hostnames, struct clientinfo* clntinfo) { int chldpid =0; const char* option = 0; int ahandle, i; struct sigaction sa; struct descriptor_set d_set; unsigned int peer_ip; struct sockaddr_in c_in; time_t now; if (srvinfo.multithread) { daemonize(); } if (changeid(UNPRIV, EUID, "Changing id back (socket(), bind())") < 0) { return -1; } d_set = listen_on_ifaces(hostnames, clntinfo); if (d_set.maxfd < 0) { jlog(8, "d_set.maxfd was negative: %d", d_set.maxfd); return -1; } /* we have successfully bound */ /* become root again - use our function instead of plain * setuid() for the logging */ if (changeid(PRIV, UID, "Changing ID to root (pidfile)") < 0) { return -1; } option = config_get_option("pidfile"); if (option) { FILE* pidf; umask(022); pidf = fopen(option, "w"); if (pidf) { fprintf(pidf, "%ld\n", (long) getpid()); fclose(pidf); /* if successful register function to remove the * pidfile */ atexit(removepidfile); } else { jlog(2, "Error creating pidfile %s", option); } } /* this has to be done for the daemonization. We do it now after * the pidfile has been created */ umask(0); srvinfo.ready_to_serve = SVR_LAUNCH_READY; if (stage_action("startsetup") < 0) { return -1; } sa.sa_handler = childterm; chlds_exited = 0; sigemptyset (&sa.sa_mask); #ifndef WINDOWS sa.sa_flags = SA_RESTART; #endif sigaction (SIGCHLD, &sa, 0); /* Close stdin,stdout,stderr */ for(i = 0; i <= 2 && srvinfo.multithread; i++) { close(i); } srvinfo.main_server_pid = getpid(); atexit(sayterminating); while(1) { ahandle = get_connecting_socket(d_set); if (ahandle == -1) { /* either select() or accept() failed */ /* I don't try resume here because we are in an * endless loop. The danger of the programm falling * into an infinite loop consuming all cpu time is * too big... */ jlog(8, "get_connecting_socket() returned error code"); return -1; } c_in = socketinfo_get_local_sin(ahandle); peer_ip = get_uint_peer_ip(ahandle); now = time(NULL); config_counter_increase(peer_ip, /* from ip */ c_in.sin_addr.s_addr, /* proxy_ip */ ntohs(c_in.sin_port), /* proxy_port */ now); /* specific_time */ if (config_check_limit_violation()) { say(ahandle, "500 Too many connections, sorry\r\n"); close(ahandle); config_counter_decrease(peer_ip, /* from ip */ c_in.sin_addr.s_addr, /* proxy_ip */ ntohs(c_in.sin_port), /* proxy_port */ now); /* specific_time */ continue; } if (srvinfo.multithread) { if ((chldpid = fork()) < 0) { jlog(1, "Error forking: %s", strerror(errno)); close(ahandle); return -1; } if (chldpid > 0) { /* parent process */ /* register the PID */ register_pid(chldpid, peer_ip, c_in.sin_addr.s_addr, /* proxy_ip */ ntohs(c_in.sin_port), /* proxy_port */ now); /* specific_time */ close(ahandle); } if (chldpid == 0) { /* child process */ jlog(8, "forked to pid %d", getpid()); } } if (!srvinfo.multithread || chldpid == 0) { return child_setup(ahandle, clntinfo); } } }