/** * Avoid objects and go to the light or dark * @global _mode_light_ it must be set */ void go_light_detection() { int ir; start_process(go_light()); _go_light_detection_running_ = 1; while(_go_light_detection_running_) { /* Obstacle detected, I stop the run and I am escaping remotely */ if((ir = ir_detect()) != OBSTACLE_NONE) { stop_process(_go_light_running_); wait_process(_go_light_running_); escape(ir); /* Run after the correction*/ start_process(go_light()); } } stop_process(_go_light_running_); wait_process(_go_light_running_); _go_light_detection_running_ = -1; }
/** * Escape objects and go to the light or dark * @global _mode_light_ it must be set */ void go_light_touch() { int bmp; start_process(go_light()); _go_light_touch_running_ = 1; while(_go_light_touch_running_) { /* Obstacle touched, I stop the run and I am escaping */ bmp = bumper_detection(); if(bmp != 0 && bmp != BMP_BACK) { stop_process(_go_light_running_); wait_process(_go_light_running_); escape(bmp); /* Run after the correction*/ start_process(go_light()); } } stop_process(_go_light_running_); wait_process(_go_light_running_); _go_light_touch_running_ = -1; }
/** * Running to 20cm of ligth * but escape obstacle with bumpers */ void go_light_stop_touch() { int bmp; _mode_light_ = GO_LIGHT; _motor_initial_speed_ = 100; light_environnement(); start_process(go_light_stop(20)); while(_go_light_touch_running_) { bmp = bumper_detection(); if(bmp != 0 && (bmp != BMP_BACK)) { stop_process(_go_light_stop_running_); wait_process(_go_light_stop_running_); escape(bmp); start_process(go_light_stop(20)); } } stop_process(_go_light_stop_running_); wait_process(_go_light_stop_running_); _go_light_touch_running_ = -1; }
void test_multiuser (Options options) { DWORD result; int exit_code; char buffer[256]; HANDLE second_user_token; char *second_user_endpoint_name; /* Start two servers as different users */ get_test_user_token (&second_user_token); h_process[0] = exec (options.server, second_user_token, &h_pipe[0]); h_process[1] = exec (options.server, NULL, &h_pipe[1]); CloseHandle (second_user_token); assert_line (h_pipe[0], "server: listening"); assert_line (h_pipe[1], "server: listening"); read_line (h_pipe[0], buffer, 256); if (strncmp (buffer, "server: endpoint name: ", strlen ("server: endpoint name: "))) rpc_log_error ("Unexpected output from server: %s\n", buffer); buffer[strlen(buffer) - 2] = 0; second_user_endpoint_name = buffer + strlen ("server: endpoint name: "); /* Check normal client can contact server */ h_process[2] = exec (options.client, NULL, &h_pipe[2]); exit_code = wait_process (2, "client"); if (exit_code != 0) { dump (h_pipe[2]); rpc_log_error ("tester: Client enountered an error contacting own user's server\n"); } /* Check rogue client cannot contact other user's server */ if (! options.rogue_client) return; h_process[2] = exec_with_args (options.rogue_client, second_user_endpoint_name, NULL, &h_pipe[2]); exit_code = wait_process (2, "client"); if (exit_code != RPC_S_CALL_FAILED) { dump (h_pipe[2]); rpc_log_error ("tester: Rogue client encountered an error %i trying to contact " "other user's server (RPC_S_CALL_FAILED (%i) is expected " "when the security callback denies access).\n", exit_code, RPC_S_CALL_FAILED); } }
/* * This is a post- fix recursive function for starting every service * that s depends on before starting s. * @param s A Service_T object */ static void do_start(Service_T s) { ASSERT(s); if (s->visited) return; s->visited = TRUE; if (s->dependantlist) { Dependant_T d; for (d = s->dependantlist; d; d = d->next ) { Service_T parent = Util_getService(d->dependant); ASSERT(parent); do_start(parent); } } if (s->start) { if (s->type != TYPE_PROCESS || ! Util_isProcessRunning(s, FALSE)) { LogInfo("'%s' start: %s\n", s->name, s->start->arg[0]); spawn(s, s->start, NULL); /* We only wait for a process type, other service types does not have a pid file to watch */ if (s->type == TYPE_PROCESS) wait_process(s, Process_Started); } } else { LogDebug("'%s' start skipped -- method not defined\n", s->name); } Util_monitorSet(s); }
/* * This function simply restarts the service s. * @param s A Service_T object */ static void do_restart(Service_T s) { if (s->restart) { LogInfo("'%s' restart: %s\n", s->name, s->restart->arg[0]); spawn(s, s->restart, NULL); /* We only wait for a process type, other service types does not have a pid file to watch */ if (s->type == TYPE_PROCESS) wait_process(s, Process_Started); } else { LogDebug("'%s' restart skipped -- method not defined\n", s->name); } Util_monitorSet(s); }
static int test_initfree_env(void) { process_t child = process_FREE; process_result_t result; TEST(0 == init_process(&child, &childprocess_environment, 0, &(process_stdio_t)process_stdio_INIT_INHERIT)); TEST(0 == wait_process(&child, &result)); TEST(0 == free_process(&child)); TEST(process_state_TERMINATED == result.state); TEST(0 == result.returncode); return 0 ; ONERR: TEST(0 == free_process(&child)); return EINVAL ; }
static void signals_handler (int sig, siginfo_t *si, void *context) { UNUSED(context); switch (sig) { case SIGHUP: printf ("Handling Graceful Restart..\n"); cherokee_server_handle_HUP (srv); break; case SIGUSR2: printf ("Reopening log files..\n"); cherokee_server_log_reopen (srv); break; case SIGINT: case SIGTERM: if (srv->wanna_exit) { break; } printf ("Server is exiting..\n"); cherokee_server_handle_TERM (srv); break; case SIGCHLD: wait_process (si->si_pid); break; case SIGSEGV: #ifdef SIGBUS case SIGBUS: #endif cherokee_server_handle_panic (srv); break; default: PRINT_ERROR ("Unknown signal: %d\n", sig); } }
/* * This function simply stops the service p. * @param s A Service_T object * @param flag TRUE if the monitoring should be disabled or FALSE if monitoring should continue (when stop is part of restart) * @return TRUE if the service was stopped otherwise FALSE */ static int do_stop(Service_T s, int flag) { int rv = TRUE; ASSERT(s); if (s->depend_visited) return rv; s->depend_visited = TRUE; if (s->stop) { if (s->type != TYPE_PROCESS || Util_isProcessRunning(s, FALSE)) { LogInfo("'%s' stop: %s\n", s->name, s->stop->arg[0]); spawn(s, s->stop, NULL); if (s->type == TYPE_PROCESS && (wait_process(s, Process_Stopped) != Process_Stopped)) // Only wait for process service types stop rv = FALSE; } } else { LogDebug("'%s' stop skipped -- method not defined\n", s->name); } if (flag) Util_monitorUnset(s); else Util_resetInfo(s); return rv; }
void test_standard (Options options) { DWORD result; int exit_code; /* Start server */ h_process[0] = exec (options.server, NULL, &h_pipe[0]); assert_line (h_pipe[0], "server: listening"); /* Run client */ h_process[1] = exec (options.client, NULL, &h_pipe[1]); exit_code = wait_process (1, "client"); if (exit_code) { printf ("\nServer output:\n"); dump (h_pipe[0]); printf ("\nClient output:\n"); dump (h_pipe[1]); rpc_log_error ("Client exited with an error"); } assert_last_line (h_pipe[1], "client: success"); }
int main() { start_process(&a, 'a', delay_process[0]); start_process(&b, 'b', delay_process[1]); wait_process(a, 'a'); wait_process(b, 'b'); start_process(&c, 'c', delay_process[2]); start_process(&d, 'd', delay_process[3]); start_process(&e, 'e', delay_process[4]); wait_process(e, 'e'); start_process(&f, 'f', delay_process[5]); start_process(&g, 'g', delay_process[6]); wait_process(g, 'g'); start_process(&h, 'h', delay_process[7]); wait_process(c, 'c'); wait_process(h, 'h'); start_process(&i, 'i', delay_process[8]); wait_process(d, 'd'); wait_process(i, 'i'); start_process(&j, 'j', delay_process[9]); start_process(&k, 'k', delay_process[10]); wait_process(f, 'f'); wait_process(j, 'j'); wait_process(k, 'k'); return 0; }
/* * This is called once the connection has been negotiated. It is used * for rsyncd, remote-shell, and local connections. */ int client_run(int f_in, int f_out, pid_t pid, int argc, char *argv[]) { struct file_list *flist = NULL; int status = 0, status2 = 0; char *local_name = NULL; cleanup_child_pid = pid; if (!read_batch) { set_nonblocking(f_in); set_nonblocking(f_out); } io_set_sock_fds(f_in, f_out); setup_protocol(f_out,f_in); if (protocol_version >= 23 && !read_batch) io_start_multiplex_in(); /* We set our stderr file handle to blocking because ssh might have * set it to non-blocking. This can be particularly troublesome if * stderr is a clone of stdout, because ssh would have set our stdout * to non-blocking at the same time (which can easily cause us to lose * output from our print statements). This kluge shouldn't cause ssh * any problems for how we use it. Note also that we delayed setting * this until after the above protocol setup so that we know for sure * that ssh is done twiddling its file descriptors. */ set_blocking(STDERR_FILENO); if (am_sender) { keep_dirlinks = 0; /* Must be disabled on the sender. */ io_start_buffering_out(); if (!filesfrom_host) set_msg_fd_in(f_in); send_filter_list(f_out); if (filesfrom_host) filesfrom_fd = f_in; if (write_batch && !am_server) start_write_batch(f_out); flist = send_file_list(f_out, argc, argv); set_msg_fd_in(-1); if (verbose > 3) rprintf(FINFO,"file list sent\n"); the_file_list = flist; io_flush(NORMAL_FLUSH); send_files(flist,f_out,f_in); io_flush(FULL_FLUSH); handle_stats(-1); if (protocol_version >= 24) read_final_goodbye(f_in, f_out); if (pid != -1) { if (verbose > 3) rprintf(FINFO,"client_run waiting on %d\n", (int) pid); io_flush(FULL_FLUSH); wait_process(pid, &status); } output_summary(); io_flush(FULL_FLUSH); exit_cleanup(status); } if (need_messages_from_generator && !read_batch) io_start_multiplex_out(); if (argc == 0) list_only |= 1; send_filter_list(read_batch ? -1 : f_out); if (filesfrom_fd >= 0) { io_set_filesfrom_fds(filesfrom_fd, f_out); filesfrom_fd = -1; } if (write_batch && !am_server) start_write_batch(f_in); flist = recv_file_list(f_in); the_file_list = flist; if (flist && flist->count > 0) { local_name = get_local_name(flist, argv[0]); status2 = do_recv(f_in, f_out, flist, local_name); } else { handle_stats(-1); output_summary(); } if (pid != -1) { if (verbose > 3) rprintf(FINFO,"client_run2 waiting on %d\n", (int) pid); io_flush(FULL_FLUSH); wait_process(pid, &status); } return MAX(status, status2); }
/** * Move forward, and when a wall is detected, follow it * Last update: - * @version - */ void main() { int ir_current; /* Store, temporarily the ir values */ int ir_previous = 0; int light; /* Initialize all components needed to run the robot */ init_motors(); _motor_initial_speed_ = 100; start_process(running_forever()); while(1) { int enc_dif = _right_enc_counts_-_left_enc_counts_; /* * Depending on the ir_detect() value), * make it rotate, and move forward again */ ir_current = ir_detect(); /* Something is detected */ if(ir_current != 0) { if(ir_current == OBSTACLE_FRONT) { printf("FRONT\n"); /* Stop the robot */ stop_process(_running_process_running_); wait_process(_running_process_running_); /* Check the light difference */ light = light_diff(); rotate(C_MOTOR, ((light < 0) - (light >= 0))*90); /* The robot can go foward after the dodge */ start_process(running_forever()); ir_previous = 0; } else { printf("move out %d\n", 10 * ((ir_previous == OBSTACLE_LEFT) - (ir_previous == OBSTACLE_RIGHT))); /* Stop the robot */ stop_process(_running_process_running_); wait_process(_running_process_running_); rotate(C_MOTOR, 10 * ((ir_current == OBSTACLE_LEFT) - (ir_current == OBSTACLE_RIGHT))); /* The robot can go foward after the dodge */ start_process(running_forever()); ir_previous = ir_current; } } else if(ir_previous) { printf("move to %d\n", -10 * ((ir_previous == OBSTACLE_LEFT) - (ir_previous == OBSTACLE_RIGHT))); /* Stop the robot */ stop_process(_running_process_running_); wait_process(_running_process_running_); /* Check the light difference */ rotate(C_MOTOR, -10 * ((ir_previous == OBSTACLE_LEFT) - (ir_previous == OBSTACLE_RIGHT))); /* The robot can go foward after the dodge */ start_process(running_forever()); } sleep(0.1); } }
/** * Eventually calls exit(), passing @p code, therefore does not return. * * @param code one of the RERR_* codes from errcode.h. **/ NORETURN void _exit_cleanup(int code, const char *file, int line) { static int switch_step = 0; static int exit_code = 0, exit_line = 0; static const char *exit_file = NULL; static int unmodified_code = 0; SIGACTION(SIGUSR1, SIG_IGN); SIGACTION(SIGUSR2, SIG_IGN); if (exit_code) { /* Preserve first exit info when recursing. */ code = exit_code; file = exit_file; line = exit_line; } /* If this is the exit at the end of the run, the server side * should not attempt to output a message (see log_exit()). */ if (am_server && code == 0) am_server = 2; /* Some of our actions might cause a recursive call back here, so we * keep track of where we are in the cleanup and never repeat a step. */ switch (switch_step) { #include "case_N.h" /* case 0: */ switch_step++; exit_code = unmodified_code = code; exit_file = file; exit_line = line; if (verbose > 3) { rprintf(FINFO, "[%s] _exit_cleanup(code=%d, file=%s, line=%d): entered\n", who_am_i(), code, file, line); } /* FALLTHROUGH */ #include "case_N.h" switch_step++; if (cleanup_child_pid != -1) { int status; int pid = wait_process(cleanup_child_pid, &status, WNOHANG); if (pid == cleanup_child_pid) { status = WEXITSTATUS(status); if (status > code) code = exit_code = status; } } /* FALLTHROUGH */ #include "case_N.h" switch_step++; if (cleanup_got_literal && cleanup_fname && cleanup_new_fname && keep_partial && handle_partial_dir(cleanup_new_fname, PDIR_CREATE)) { const char *fname = cleanup_fname; cleanup_fname = NULL; if (cleanup_fd_r != -1) bpc_close(cleanup_fd_r); if (cleanup_fd_w != -1) { flush_write_file(cleanup_fd_w); bpc_close(cleanup_fd_w); } finish_transfer(cleanup_new_fname, fname, NULL, NULL, cleanup_file, 0, !partial_dir); } /* FALLTHROUGH */ #include "case_N.h" switch_step++; if (!code || am_server || am_receiver) io_flush(FULL_FLUSH); /* FALLTHROUGH */ #include "case_N.h" switch_step++; bpc_sysCall_cleanup(); if (cleanup_fname) do_unlink(cleanup_fname); if (code) kill_all(SIGUSR1); if (cleanup_pid && cleanup_pid == getpid()) { char *pidf = lp_pid_file(); if (pidf && *pidf) unlink(lp_pid_file()); } if (code == 0) { if (io_error & IOERR_DEL_LIMIT) code = exit_code = RERR_DEL_LIMIT; if (io_error & IOERR_VANISHED) code = exit_code = RERR_VANISHED; if (io_error & IOERR_GENERAL || got_xfer_error) code = exit_code = RERR_PARTIAL; } if (code || am_daemon || (logfile_name && (am_server || !verbose))) log_exit(code, file, line); /* FALLTHROUGH */ #include "case_N.h" switch_step++; if (verbose > 2) { rprintf(FINFO, "[%s] _exit_cleanup(code=%d, file=%s, line=%d): " "about to call exit(%d)\n", who_am_i(), unmodified_code, file, line, code); } /* FALLTHROUGH */ #include "case_N.h" switch_step++; if (am_server && code) msleep(100); close_all(); /* FALLTHROUGH */ default: break; } exit(code); }
int main( int argc, char ** argv) { GList *dlist; GList *dlist1; disk_t *diskp; disklist_t diskl; size_t i; char *conf_diskfile; char *conf_tapelist; char *conf_indexdir; find_result_t *output_find; time_t tmp_time; int amtrmidx_debug = 0; config_overrides_t *cfg_ovr = NULL; gboolean compress_index; gboolean sort_index; char *lock_file; file_lock *lock_index; if (argc > 1 && argv[1] && g_str_equal(argv[1], "--version")) { printf("amtrmidx-%s\n", VERSION); return (0); } /* * Configure program for internationalization: * 1) Only set the message locale for now. * 2) Set textdomain for all amanda related programs to "amanda" * We don't want to be forced to support dozens of message catalogs. */ setlocale(LC_MESSAGES, "C"); textdomain("amanda"); safe_fd(-1, 0); safe_cd(); set_pname("amtrmidx"); /* Don't die when child closes pipe */ signal(SIGPIPE, SIG_IGN); dbopen(DBG_SUBDIR_SERVER); dbprintf(_("%s: version %s\n"), argv[0], VERSION); cfg_ovr = extract_commandline_config_overrides(&argc, &argv); if (argc > 1 && g_str_equal(argv[1], "-t")) { amtrmidx_debug = 1; argc--; argv++; } if (argc < 2) { g_fprintf(stderr, _("Usage: %s [-t] <config> [-o configoption]*\n"), argv[0]); return 1; } set_config_overrides(cfg_ovr); config_init_with_global(CONFIG_INIT_EXPLICIT_NAME | CONFIG_INIT_USE_CWD, argv[1]); conf_diskfile = config_dir_relative(getconf_str(CNF_DISKFILE)); read_diskfile(conf_diskfile, &diskl); amfree(conf_diskfile); if (config_errors(NULL) >= CFGERR_WARNINGS) { config_print_errors(); if (config_errors(NULL) >= CFGERR_ERRORS) { g_critical(_("errors processing config file")); } } check_running_as(RUNNING_AS_DUMPUSER); dbrename(get_config_name(), DBG_SUBDIR_SERVER); conf_tapelist = config_dir_relative(getconf_str(CNF_TAPELIST)); if(read_tapelist(conf_tapelist)) { error(_("could not load tapelist \"%s\""), conf_tapelist); /*NOTREACHED*/ } amfree(conf_tapelist); compress_index = getconf_boolean(CNF_COMPRESS_INDEX); sort_index = getconf_boolean(CNF_SORT_INDEX); output_find = find_dump(&diskl); conf_indexdir = config_dir_relative(getconf_str(CNF_INDEXDIR)); /* take a lock file to prevent concurent trim */ lock_file = g_strdup_printf("%s/%s", conf_indexdir, "lock"); lock_index = file_lock_new(lock_file); if (file_lock_lock_wr(lock_index) != 0) goto lock_failed; /* now go through the list of disks and find which have indexes */ time(&tmp_time); tmp_time -= 7*24*60*60; /* back one week */ for (dlist = diskl.head; dlist != NULL; dlist = dlist->next) { diskp = dlist->data; if (diskp->index) { char *indexdir, *qindexdir; DIR *d; struct dirent *f; char **names; size_t name_length; size_t name_count; char *host; char *disk, *qdisk; size_t len_date; disk_t *dp; GSList *matching_dp = NULL; /* get listing of indices, newest first */ host = sanitise_filename(diskp->host->hostname); disk = sanitise_filename(diskp->name); qdisk = quote_string(diskp->name); indexdir = g_strjoin(NULL, conf_indexdir, "/", host, "/", disk, "/", NULL); qindexdir = quote_string(indexdir); /* find all dles that use the same indexdir */ for (dlist1 = diskl.head; dlist1 != NULL; dlist1 = dlist1->next) { char *dp_host, *dp_disk; dp = dlist1->data; dp_host = sanitise_filename(dp->host->hostname); dp_disk = sanitise_filename(dp->name); if (g_str_equal(host, dp_host) && g_str_equal(disk, dp_disk)) { matching_dp = g_slist_append(matching_dp, dp); } amfree(dp_host); amfree(dp_disk); } dbprintf("%s %s -> %s\n", diskp->host->hostname, qdisk, qindexdir); amfree(qdisk); if ((d = opendir(indexdir)) == NULL) { dbprintf(_("could not open index directory %s\n"), qindexdir); amfree(host); amfree(disk); amfree(indexdir); amfree(qindexdir); g_slist_free(matching_dp); continue; } name_length = 100; names = (char **)g_malloc(name_length * sizeof(char *)); name_count = 0; while ((f = readdir(d)) != NULL) { size_t l; if(is_dot_or_dotdot(f->d_name)) { continue; } for(i = 0; i < sizeof("YYYYMMDDHHMMSS")-1; i++) { if(! isdigit((int)(f->d_name[i]))) { break; } } len_date = i; /* len_date=8 for YYYYMMDD */ /* len_date=14 for YYYYMMDDHHMMSS */ if((len_date != 8 && len_date != 14) || f->d_name[len_date] != '_' || ! isdigit((int)(f->d_name[len_date+1]))) { continue; /* not an index file */ } /* * Clear out old index temp files. */ l = strlen(f->d_name) - (sizeof(".tmp")-1); if ((l > (len_date + 1)) && (g_str_equal(f->d_name + l, ".tmp"))) { struct stat sbuf; char *path, *qpath; path = g_strconcat(indexdir, f->d_name, NULL); qpath = quote_string(path); if(lstat(path, &sbuf) != -1 && ((sbuf.st_mode & S_IFMT) == S_IFREG) && ((time_t)sbuf.st_mtime < tmp_time)) { dbprintf("rm %s\n", qpath); if(amtrmidx_debug == 0 && unlink(path) == -1) { dbprintf(_("Error removing %s: %s\n"), qpath, strerror(errno)); } } amfree(qpath); amfree(path); continue; } if(name_count >= name_length) { char **new_names; new_names = g_malloc((name_length * 2) * sizeof(char *)); memcpy(new_names, names, name_length * sizeof(char *)); amfree(names); names = new_names; name_length *= 2; } names[name_count++] = g_strdup(f->d_name); } closedir(d); qsort(names, name_count, sizeof(char *), sort_by_name_reversed); /* * Search for the first full dump past the minimum number * of index files to keep. */ for(i = 0; i < name_count; i++) { char *datestamp; int level; size_t len_date; int matching = 0; GSList *mdp; for(len_date = 0; len_date < sizeof("YYYYMMDDHHMMSS")-1; len_date++) { if(! isdigit((int)(names[i][len_date]))) { break; } } datestamp = g_strdup(names[i]); datestamp[len_date] = '\0'; if (sscanf(&names[i][len_date+1], "%d", &level) != 1) level = 0; for (mdp = matching_dp; mdp != NULL; mdp = mdp->next) { dp = mdp->data; if (dump_exist(output_find, dp->host->hostname, dp->name, datestamp, level)) { matching = 1; } } if (!matching) { struct stat sbuf; char *path, *qpath; path = g_strconcat(indexdir, names[i], NULL); qpath = quote_string(path); if(lstat(path, &sbuf) != -1 && ((sbuf.st_mode & S_IFMT) == S_IFREG) && ((time_t)sbuf.st_mtime < tmp_time)) { dbprintf("rm %s\n", qpath); if(amtrmidx_debug == 0 && unlink(path) == -1) { dbprintf(_("Error removing %s: %s\n"), qpath, strerror(errno)); } } amfree(qpath); amfree(path); } /* Did it require un/compression and/or sorting */ { char *orig_name = getindexfname(host, disk, datestamp, level); char *sorted_name = getindex_sorted_fname(host, disk, datestamp, level); char *sorted_gz_name = getindex_sorted_gz_fname(host, disk, datestamp, level); char *unsorted_name = getindex_unsorted_fname(host, disk, datestamp, level); char *unsorted_gz_name = getindex_unsorted_gz_fname(host, disk, datestamp, level); gboolean orig_exist = FALSE; gboolean sorted_exist = FALSE; gboolean sorted_gz_exist = FALSE; gboolean unsorted_exist = FALSE; gboolean unsorted_gz_exist = FALSE; int fd; int uncompress_err_fd = -1; int sort_err_fd = -1; int compress_err_fd = -1; pid_t uncompress_pid = -1; pid_t sort_pid = -1; pid_t compress_pid = -1; orig_exist = file_exists(orig_name); sorted_exist = file_exists(sorted_name); sorted_gz_exist = file_exists(sorted_gz_name); unsorted_exist = file_exists(unsorted_name); unsorted_gz_exist = file_exists(unsorted_gz_name); if (sort_index && compress_index) { if (!sorted_gz_exist) { if (sorted_exist) { // COMPRESS compress_pid = run_compress(-1, NULL, &compress_err_fd, sorted_name, sorted_gz_name); unlink(sorted_name); } else if (unsorted_exist) { // SORT AND COMPRESS sort_pid = run_sort(-1, &fd, &sort_err_fd, unsorted_name, NULL); compress_pid = run_compress(fd, NULL, &compress_err_fd, NULL, sorted_gz_name); unlink(unsorted_name); } else if (unsorted_gz_exist) { // UNCOMPRESS SORT AND COMPRESS uncompress_pid = run_uncompress(-1, &fd, &uncompress_err_fd, unsorted_gz_name, NULL); sort_pid = run_sort(fd, &fd, &sort_err_fd, NULL, NULL); compress_pid = run_compress(fd, NULL, &compress_err_fd, NULL, sorted_gz_name); unlink(unsorted_gz_name); } else if (orig_exist) { // UNCOMPRESS SORT AND COMPRESS uncompress_pid = run_uncompress(-1, &fd, &uncompress_err_fd, orig_name, NULL); sort_pid = run_sort(fd, &fd, &sort_err_fd, NULL, NULL); compress_pid = run_compress(fd, NULL, &compress_err_fd, NULL, sorted_gz_name); unlink(orig_name); } } else { if (sorted_exist) { unlink(sorted_name); } if (unsorted_exist) { unlink(unsorted_name); } if (unsorted_gz_exist) { unlink(unsorted_gz_name); } } } else if (sort_index && !compress_index) { if (!sorted_exist) { if (sorted_gz_exist) { // UNCOMPRESS uncompress_pid = run_uncompress(-1, NULL, &uncompress_err_fd, sorted_gz_name, sorted_name); unlink(sorted_gz_name); } else if (unsorted_exist) { // SORT sort_pid = run_sort(-1, NULL, &sort_err_fd, unsorted_name, sorted_name); unlink(unsorted_name); } else if (unsorted_gz_exist) { // UNCOMPRESS AND SORT uncompress_pid = run_uncompress(-1, &fd, &uncompress_err_fd, unsorted_gz_name, NULL); sort_pid = run_sort(fd, NULL, &sort_err_fd, NULL, sorted_name); unlink(unsorted_gz_name); } else if (orig_exist) { // UNCOMPRESS AND SORT uncompress_pid = run_uncompress(-1, &fd, &uncompress_err_fd, orig_name, NULL); sort_pid = run_sort(fd, NULL, &sort_err_fd, NULL, sorted_name); unlink(orig_name); } } else { if (sorted_gz_exist) { unlink(sorted_gz_name); } if (unsorted_exist) { unlink(unsorted_name); } if (unsorted_gz_exist) { unlink(unsorted_gz_name); } } } else if (!sort_index && compress_index) { if (!sorted_gz_exist && !unsorted_gz_exist) { if (sorted_exist) { // COMPRESS sorted compress_pid = run_compress(-1, NULL, &compress_err_fd, sorted_name, sorted_gz_name); unlink(sorted_name); } else if (unsorted_exist) { // COMPRESS unsorted compress_pid = run_compress(-1, NULL, &compress_err_fd, unsorted_name, unsorted_gz_name); unlink(unsorted_name); } else if (orig_exist) { // RENAME orig rename(orig_name, unsorted_gz_name); } } else { if (sorted_exist) { unlink(sorted_name); } if (unsorted_exist) { unlink(unsorted_name); } if (sorted_gz_exist && unsorted_gz_exist) { unlink(unsorted_gz_name); } } } else if (!sort_index && !compress_index) { if (!sorted_exist && !unsorted_exist) { if (sorted_gz_exist) { // UNCOMPRESS sorted uncompress_pid = run_uncompress(-1, NULL, &uncompress_err_fd, sorted_gz_name, sorted_name); unlink(sorted_gz_name); } else if (unsorted_gz_exist) { // UNCOMPRESS unsorted uncompress_pid = run_uncompress(-1, NULL, &uncompress_err_fd, unsorted_gz_name, unsorted_name); unlink(unsorted_gz_name); } else if (orig_exist) { // UNCOMPRESS orig uncompress_pid = run_uncompress(-1, NULL, &uncompress_err_fd, orig_name, unsorted_name); unlink(orig_name); } } else { if (sorted_gz_exist) { unlink(sorted_gz_name); } if (unsorted_gz_exist) { unlink(unsorted_gz_name); } if (sorted_exist && unsorted_exist) { unlink(unsorted_name); } } } if (uncompress_pid != -1) wait_process(uncompress_pid, uncompress_err_fd, "uncompress"); if (sort_pid != -1) wait_process(sort_pid, sort_err_fd, "sort"); if (compress_pid != -1) wait_process(compress_pid, compress_err_fd, "compress"); g_free(orig_name); g_free(sorted_name); g_free(sorted_gz_name); g_free(unsorted_name); g_free(unsorted_gz_name); } amfree(datestamp); amfree(names[i]); } g_slist_free(matching_dp); amfree(names); amfree(host); amfree(disk); amfree(indexdir); amfree(qindexdir); } } file_lock_unlock(lock_index); lock_failed: file_lock_free(lock_index); amfree(conf_indexdir); amfree(lock_file); free_find_result(&output_find); clear_tapelist(); free_disklist(&diskl); unload_disklist(); dbclose(); return 0; }
/** * Eventually calls exit(), passing @p code, therefore does not return. * * @param code one of the RERR_* codes from errcode.h. **/ NORETURN void _exit_cleanup(int code, const char *file, int line) { static int switch_step = 0; static int exit_code = 0, exit_line = 0; static const char *exit_file = NULL; static int first_code = 0; SIGACTION(SIGUSR1, SIG_IGN); SIGACTION(SIGUSR2, SIG_IGN); if (!exit_code) { /* Preserve first error exit info when recursing. */ exit_code = code; exit_file = file; exit_line = line < 0 ? -line : line; } /* If this is the exit at the end of the run, the server side * should not attempt to output a message (see log_exit()). */ if (am_server && code == 0) am_server = 2; /* Some of our actions might cause a recursive call back here, so we * keep track of where we are in the cleanup and never repeat a step. */ switch (switch_step) { #include "case_N.h" /* case 0: */ switch_step++; first_code = code; if (output_needs_newline) { fputc('\n', stdout); output_needs_newline = 0; } if (DEBUG_GTE(EXIT, 2)) { rprintf(FINFO, "[%s] _exit_cleanup(code=%d, file=%s, line=%d): entered\n", who_am_i(), code, file, line); } /* FALLTHROUGH */ #include "case_N.h" switch_step++; if (cleanup_child_pid != -1) { int status; int pid = wait_process(cleanup_child_pid, &status, WNOHANG); if (pid == cleanup_child_pid) { status = WEXITSTATUS(status); if (status > exit_code) exit_code = status; } } /* FALLTHROUGH */ #include "case_N.h" switch_step++; if (cleanup_got_literal && (cleanup_fname || cleanup_fd_w != -1)) { if (cleanup_fd_r != -1) { close(cleanup_fd_r); cleanup_fd_r = -1; } if (cleanup_fd_w != -1) { flush_write_file(cleanup_fd_w); close(cleanup_fd_w); cleanup_fd_w = -1; } if (cleanup_fname && cleanup_new_fname && keep_partial && handle_partial_dir(cleanup_new_fname, PDIR_CREATE)) { int tweak_modtime = 0; const char *fname = cleanup_fname; cleanup_fname = NULL; if (!partial_dir) { /* We don't want to leave a partial file with a modern time or it * could be skipped via --update. Setting the time to something * really old also helps it to stand out as unfinished in an ls. */ tweak_modtime = 1; cleanup_file->modtime = 0; } finish_transfer(cleanup_new_fname, fname, NULL, NULL, cleanup_file, tweak_modtime, !partial_dir); } } /* FALLTHROUGH */ #include "case_N.h" switch_step++; if (flush_ok_after_signal) { flush_ok_after_signal = False; if (code == RERR_SIGNAL) io_flush(FULL_FLUSH); } if (!exit_code && !code) io_flush(FULL_FLUSH); /* FALLTHROUGH */ #include "case_N.h" switch_step++; if (cleanup_fname) do_unlink(cleanup_fname); if (exit_code) kill_all(SIGUSR1); if (cleanup_pid && cleanup_pid == getpid()) { char *pidf = lp_pid_file(); if (pidf && *pidf) unlink(lp_pid_file()); } if (exit_code == 0) { if (code) exit_code = code; if (io_error & IOERR_DEL_LIMIT) exit_code = RERR_DEL_LIMIT; if (io_error & IOERR_VANISHED) exit_code = RERR_VANISHED; if (io_error & IOERR_GENERAL || got_xfer_error) exit_code = RERR_PARTIAL; } /* If line < 0, this exit is after a MSG_ERROR_EXIT event, so * we don't want to output a duplicate error. */ if ((exit_code && line > 0) || am_daemon || (logfile_name && (am_server || !INFO_GTE(STATS, 1)))) log_exit(exit_code, exit_file, exit_line); /* FALLTHROUGH */ #include "case_N.h" switch_step++; if (DEBUG_GTE(EXIT, 1)) { rprintf(FINFO, "[%s] _exit_cleanup(code=%d, file=%s, line=%d): " "about to call exit(%d)\n", who_am_i(), first_code, exit_file, exit_line, exit_code); } /* FALLTHROUGH */ #include "case_N.h" switch_step++; if (exit_code && exit_code != RERR_SOCKETIO && exit_code != RERR_STREAMIO && exit_code != RERR_SIGNAL1 && exit_code != RERR_TIMEOUT && !shutting_down && (protocol_version >= 31 || am_receiver)) { if (line > 0) { if (DEBUG_GTE(EXIT, 3)) { rprintf(FINFO, "[%s] sending MSG_ERROR_EXIT with exit_code %d\n", who_am_i(), exit_code); } send_msg_int(MSG_ERROR_EXIT, exit_code); } noop_io_until_death(); } /* FALLTHROUGH */ #include "case_N.h" switch_step++; if (am_server && exit_code) msleep(100); close_all(); /* FALLTHROUGH */ default: break; } exit(exit_code); }
static int do_recv(int f_in,int f_out,struct file_list *flist,char *local_name) { int pid; int status = 0; int error_pipe[2]; /* The receiving side mustn't obey this, or an existing symlink that * points to an identical file won't be replaced by the referent. */ copy_links = 0; if (preserve_hard_links) init_hard_links(); if (fd_pair(error_pipe) < 0) { rsyserr(FERROR, errno, "pipe failed in do_recv"); exit_cleanup(RERR_IPC); } io_flush(NORMAL_FLUSH); if ((pid = do_fork()) == -1) { rsyserr(FERROR, errno, "fork failed in do_recv"); exit_cleanup(RERR_IPC); } if (pid == 0) { close(error_pipe[0]); if (f_in != f_out) close(f_out); /* we can't let two processes write to the socket at one time */ close_multiplexing_out(); /* set place to send errors */ set_msg_fd_out(error_pipe[1]); recv_files(f_in, flist, local_name); io_flush(FULL_FLUSH); handle_stats(f_in); send_msg(MSG_DONE, "", 0); io_flush(FULL_FLUSH); /* Handle any keep-alive packets from the post-processing work * that the generator does. */ if (protocol_version >= 29) { kluge_around_eof = -1; /* This should only get stopped via a USR2 signal. */ while (read_int(f_in) == flist->count && read_shortint(f_in) == ITEM_IS_NEW) {} rprintf(FERROR, "Invalid packet at end of run [%s]\n", who_am_i()); exit_cleanup(RERR_PROTOCOL); } /* Finally, we go to sleep until our parent kills us with a * USR2 signal. We sleep for a short time, as on some OSes * a signal won't interrupt a sleep! */ while (1) msleep(20); } am_generator = 1; close_multiplexing_in(); if (write_batch && !am_server) stop_write_batch(); close(error_pipe[1]); if (f_in != f_out) close(f_in); io_start_buffering_out(); set_msg_fd_in(error_pipe[0]); generate_files(f_out, flist, local_name); handle_stats(-1); io_flush(FULL_FLUSH); if (protocol_version >= 24) { /* send a final goodbye message */ write_int(f_out, -1); } io_flush(FULL_FLUSH); set_msg_fd_in(-1); kill(pid, SIGUSR2); wait_process(pid, &status); return status; }