/** * Misc. tools for config parsing */ int GetStringParam(config_item_t block, const char *block_name, const char *var_name, param_flags_t flags, char *target, unsigned int target_size, char ***extra_args_tab, unsigned int *nb_extra_args, char *err_msg) { config_item_t curr_item; int rc; int extra = 0; char *name; char *value; gsize sz; err_msg[0] = '\0'; if ( nb_extra_args ) *nb_extra_args = 0; if ( extra_args_tab ) *extra_args_tab = NULL; rc = get_cfg_param(block, block_name, var_name, flags, &name, &value, &extra, &curr_item, err_msg); if (rc) return rc; sz = g_strlcpy(target, value, target_size); if (sz >= target_size) { sprintf(err_msg, "Option too long for parameter '%s::%s', line %d", block_name, var_name, rh_config_GetItemLine(curr_item)); return EINVAL; } if ( extra ) { if ( !extra_args_tab || !nb_extra_args ) { sprintf( err_msg, "Unexpected options for parameter '%s::%s', line %d", block_name, var_name, rh_config_GetItemLine( curr_item ) ); return EINVAL; } else { *nb_extra_args = rh_config_GetExtraArgs( curr_item, extra_args_tab ); } } /* checks */ /* empty string? */ if ((flags & PFLG_NOT_EMPTY) && EMPTY_STRING(target)) { sprintf(err_msg, "Unexpected empty parameter '%s::%s', line %d", block_name, var_name, rh_config_GetItemLine(curr_item)); return EINVAL; } /* are stdio names allowed ? */ if ((flags & PFLG_STDIO_ALLOWED) && is_stdname(target)) return 0; if ((flags & PFLG_ABSOLUTE_PATH) && !IS_ABSOLUTE_PATH(target)) { sprintf( err_msg, "Absolute path expected for parameter '%s::%s', line %d", block_name, var_name, rh_config_GetItemLine( curr_item ) ); return EINVAL; } if ((flags & PFLG_NO_WILDCARDS) && WILDCARDS_IN(target)) { sprintf( err_msg, "Wildcards are not allowed in '%s::%s', line %d", block_name, var_name, rh_config_GetItemLine( curr_item ) ); return EINVAL; } if (flags & PFLG_MAIL) { char *arob = strchr( target, '@' ); /* check there is an arobase, and this arobase has text before and after */ if ( ( arob == NULL ) || ( arob == target ) || ( *( arob + 1 ) == '\0' ) ) { sprintf( err_msg, "Invalid mail address in '%s::%s', line %d", block_name, var_name, rh_config_GetItemLine( curr_item ) ); return EINVAL; } } if ((flags & PFLG_REMOVE_FINAL_SLASH) && FINAL_SLASH(target)) REMOVE_FINAL_SLASH(target); return 0; }
/** * List contents of the given id/path list */ static int list_contents(char ** id_list, int id_count) { wagon_t *ids; int i, rc; attr_set_t root_attrs; entry_id_t root_id; int is_id; rc = retrieve_root_id(&root_id); if (rc) return rc; ids = MemCalloc(id_count, sizeof(wagon_t)); if (!ids) return -ENOMEM; for (i = 0; i < id_count; i++) { is_id = TRUE; /* is it a path or fid? */ if (sscanf(id_list[i], SFID, RFID(&ids[i].id)) != FID_SCAN_CNT) { is_id = FALSE; /* take it as a path */ rc = Path2Id(id_list[i], &ids[i].id); if (!rc) { ids[i].fullname = id_list[i]; if (FINAL_SLASH(ids[i].fullname)) REMOVE_FINAL_SLASH(ids[i].fullname); } } else { #if _HAVE_FID /* Take it as an FID. */ char path[RBH_PATH_MAX]; rc = Lustre_GetFullPath( &ids[i].id, path, sizeof(path)); if (!rc) ids[i].fullname = strdup(path); #endif } if (rc) { DisplayLog(LVL_MAJOR, FIND_TAG, "Invalid parameter: %s: %s", id_list[i], strerror(-rc)); goto out; } if ((prog_options.bulk != force_nobulk) && (id_count == 1) && entry_id_equal(&ids[i].id, &root_id)) { /* the ID is FS root: use list_bulk instead */ DisplayLog(LVL_DEBUG, FIND_TAG, "Optimization: switching to bulk DB request mode"); mkfilters(FALSE); /* keep dirs */ MemFree(ids); return list_bulk(); } /* get root attrs to print it (if it matches program options) */ root_attrs.attr_mask = disp_mask | query_mask; rc = ListMgr_Get(&lmgr, &ids[i].id, &root_attrs); if (rc == 0) dircb(&ids[i], &root_attrs, 1, NULL); else { DisplayLog(LVL_VERB, FIND_TAG, "Notice: no attrs in DB for %s", id_list[i]); if (!is_id) { struct stat st; ATTR_MASK_SET(&root_attrs, fullpath); strcpy(ATTR(&root_attrs, fullpath), id_list[i]); if (lstat(ATTR(&root_attrs, fullpath ), &st) == 0) { PosixStat2EntryAttr(&st, &root_attrs, TRUE); ListMgr_GenerateFields( &root_attrs, disp_mask | query_mask); } } else if (entry_id_equal(&ids[i].id, &root_id)) { /* this is root id */ struct stat st; ATTR_MASK_SET(&root_attrs, fullpath); strcpy(ATTR(&root_attrs, fullpath), config.global_config.fs_path); if (lstat(ATTR(&root_attrs, fullpath ), &st) == 0) { PosixStat2EntryAttr(&st, &root_attrs, TRUE); ListMgr_GenerateFields( &root_attrs, disp_mask | query_mask); } } dircb(&ids[i], &root_attrs, 1, NULL); } rc = rbh_scrub(&lmgr, &ids[i], 1, disp_mask | query_mask, dircb, NULL); } out: /* ids have been processed, free them */ MemFree(ids); return rc; }
/** * Main daemon routine */ int main(int argc, char **argv) { int c, i, option_index = 0; const char *bin; int rc; char err_msg[4096]; bool chgd = false; char badcfg[RBH_PATH_MAX]; char tag_name[256] = ""; bin = rh_basename(argv[0]); start_time = time(NULL); zero_options(&options); /* parse command line options */ while ((c = getopt_long(argc, argv, SHORT_OPT_STRING, option_tab, &option_index)) != -1) { switch (c) { case 's': options.partial_scan = 1; rh_strncpy(options.partial_scan_path, optarg, RBH_PATH_MAX); /* clean final slash */ if (FINAL_SLASH(options.partial_scan_path)) REMOVE_FINAL_SLASH(options.partial_scan_path); break; case 'd': if (parse_diff_mask(optarg, &options.diff_arg.diff_mask, err_msg)) { fprintf(stderr, "Invalid argument for --diff: %s\n", err_msg); exit(1); } break; case 'a': if (optarg) { if (!strcasecmp(optarg, "fs")) options.diff_arg.apply = APPLY_FS; else if (!strcasecmp(optarg, "db")) options.diff_arg.apply = APPLY_DB; else { fprintf(stderr, "Invalid argument for --apply: '%s' (fs or db expected)\n", optarg); exit(1); } } else options.diff_arg.apply = APPLY_DB; break; case 'D': options.flags |= RUNFLG_DRY_RUN; break; case 'f': rh_strncpy(options.config_file, optarg, MAX_OPT_LEN); break; #ifdef _HSM_LITE case 'b': options.diff_arg.recov_from_backend = 1; break; #endif #ifdef _HAVE_FID /* only for lustre 2.x */ case 'o': rh_strncpy(options.output_dir, optarg, MAX_OPT_LEN); break; #endif case 'l': { int log_level = str2debuglevel(optarg); if (log_level == -1) { fprintf(stderr, "Unsupported log level '%s'. CRIT, MAJOR, EVENT, VERB, DEBUG or FULL expected.\n", optarg); exit(1); } force_debug_level(log_level); break; } case 'h': display_help(bin); exit(0); break; case 'V': display_version(bin); exit(0); break; case ':': case '?': default: fprintf(stderr, "Run '%s --help' for more details.\n", bin); exit(1); break; } } /* check there is no extra arguments */ if (optind != argc) { fprintf(stderr, "Error: unexpected argument on command line: %s\n", argv[optind]); exit(1); } /* initialize internal resources (glib, llapi, internal resources...) */ rc = rbh_init_internals(); if (rc != 0) exit(rc); /* get default config file, if not specified */ if (SearchConfig(options.config_file, options.config_file, &chgd, badcfg, MAX_OPT_LEN) != 0) { fprintf(stderr, "No config file (or too many) found matching %s\n", badcfg); exit(2); } else if (chgd) { fprintf(stderr, "Using config file '%s'.\n", options.config_file); } if (rbh_cfg_load(MODULE_MASK_FS_SCAN | MODULE_MASK_ENTRY_PROCESSOR, options.config_file, err_msg)) { fprintf(stderr, "Error reading configuration file '%s': %s\n", options.config_file, err_msg); exit(1); } if (!log_config.force_debug_level) log_config.debug_level = LVL_CRIT; /* least messages as possible */ /* Set logging to stderr */ strcpy(log_config.log_file, "stderr"); strcpy(log_config.report_file, "stderr"); strcpy(log_config.alert_file, "stderr"); /* Initialize logging */ rc = InitializeLogs(bin); if (rc) { fprintf(stderr, "Error opening log files: rc=%d, errno=%d: %s\n", rc, errno, strerror(errno)); exit(rc); } /* Initialize filesystem access */ rc = InitFS(); if (rc) exit(rc); /* Initialize status managers */ rc = smi_init_all(options.flags); if (rc) exit(rc); /* Initialize list manager */ rc = ListMgr_Init(0); if (rc) { DisplayLog(LVL_CRIT, DIFF_TAG, "Error initializing list manager: %s (%d)", lmgr_err2str(rc), rc); exit(rc); } else DisplayLog(LVL_VERB, DIFF_TAG, "ListManager successfully initialized"); if (CheckLastFS() != 0) exit(1); if (attr_mask_is_null(options.diff_arg.diff_mask)) { /* parse "all" */ char tmpstr[] = "all"; rc = parse_diff_mask(tmpstr, &options.diff_arg.diff_mask, err_msg); if (rc) { DisplayLog(LVL_CRIT, DIFF_TAG, "unexpected error parsing diff mask: %s", err_msg); exit(1); } } options.diff_arg.diff_mask = translate_all_status_mask(options.diff_arg.diff_mask); #ifdef LUSTRE_DUMP_FILES if (options.diff_arg.apply == APPLY_FS && !(options.flags & RUNFLG_DRY_RUN)) { /* open the file to write LOV EA and FID remapping */ if (!EMPTY_STRING(options.output_dir)) { char fname[RBH_PATH_MAX]; if (mkdir(options.output_dir, 0700) && (errno != EEXIST)) { DisplayLog(LVL_CRIT, DIFF_TAG, "Failed to create directory %s: %s", options.output_dir, strerror(errno)); exit(1); } snprintf(fname, RBH_PATH_MAX - 1, "%s/" LOVEA_FNAME, options.output_dir); options.diff_arg.lovea_file = fopen(fname, "w"); if (options.diff_arg.lovea_file == NULL) { DisplayLog(LVL_CRIT, DIFF_TAG, "Failed to open %s for writing: %s", fname, strerror(errno)); exit(1); } snprintf(fname, RBH_PATH_MAX - 1, "%s/" FIDREMAP_FNAME, options.output_dir); options.diff_arg.fid_remap_file = fopen(fname, "w"); if (options.diff_arg.fid_remap_file == NULL) { DisplayLog(LVL_CRIT, DIFF_TAG, "Failed to open %s for writing: %s", fname, strerror(errno)); exit(1); } } } #endif /* if no DB apply action is specified, can't use md_update field for * checking removed entries. So, create a special tag for that. */ if ((options.diff_arg.apply != APPLY_DB) || (options.flags & RUNFLG_DRY_RUN)) { fprintf(stderr, "Preparing diff table...\n"); /* create a connexion to the DB. this is safe to use the global lmgr var * as statistics thread is not running */ if (!ensure_db_access()) exit(1); /* create a tag to clear entries after the scan */ /* There could be several diff running in parallel, * so set a suffix to avoid conflicts */ sprintf(tag_name, "DIFF_%u", (unsigned int)getpid()); options.diff_arg.db_tag = tag_name; /* add filter for partial scan */ if (options.partial_scan) { lmgr_filter_t filter; filter_value_t val; lmgr_simple_filter_init(&filter); char tmp[RBH_PATH_MAX]; strcpy(tmp, options.partial_scan_path); strcat(tmp, "/*"); val.value.val_str = tmp; lmgr_simple_filter_add(&filter, ATTR_INDEX_fullpath, LIKE, val, 0); rc = ListMgr_CreateTag(&lmgr, tag_name, &filter, false); lmgr_simple_filter_free(&filter); } else rc = ListMgr_CreateTag(&lmgr, tag_name, NULL, false); if (rc) exit(rc); } /* Initialise Pipeline */ rc = EntryProcessor_Init(DIFF_PIPELINE, options.flags, &options.diff_arg); if (rc) { DisplayLog(LVL_CRIT, DIFF_TAG, "Error %d initializing EntryProcessor pipeline", rc); goto clean_tag; } else DisplayLog(LVL_VERB, DIFF_TAG, "EntryProcessor successfully initialized"); fprintf(stderr, "Starting scan\n"); /* print header to indicate the content of diff * #<diff cmd> * ---fs[=/subdir] * +++db */ for (i = 0; i < argc; i++) printf("%s%s", i == 0 ? "# " : " ", argv[i]); printf("\n"); if (options.diff_arg.apply == APPLY_FS) { if (options.partial_scan) printf("---fs=%s\n", options.partial_scan_path); else printf("---fs\n"); printf("+++db\n"); } else { printf("---db\n"); if (options.partial_scan) printf("+++fs=%s\n", options.partial_scan_path); else printf("+++fs\n"); } /* Start FS scan */ if (options.partial_scan) rc = FSScan_Start(options.flags, options.partial_scan_path); else rc = FSScan_Start(options.flags, NULL); if (rc) { DisplayLog(LVL_CRIT, DIFF_TAG, "Error %d initializing FS Scan module", rc); goto clean_tag; } else DisplayLog(LVL_VERB, DIFF_TAG, "FS Scan module successfully initialized"); /* Flush logs now, to have a trace in the logs */ FlushLogs(); /* both pipeline and scan are now running, can now trap events and * display stats */ /* create signal handling thread */ rc = pthread_create(&sig_thr, NULL, signal_handler_thr, NULL); if (rc) { DisplayLog(LVL_CRIT, DIFF_TAG, "Error starting signal handler thread: %s", strerror(errno)); goto clean_tag; } else DisplayLog(LVL_VERB, DIFF_TAG, "Signal handler thread started successfully"); pthread_create(&stat_thread, NULL, stats_thr, NULL); /* wait for FS scan to end */ FSScan_Wait(); DisplayLog(LVL_MAJOR, DIFF_TAG, "FS Scan finished"); /* Pipeline must be flushed */ EntryProcessor_Terminate(true); #ifdef LUSTRE_DUMP_FILES /* flush the lovea file */ if (options.diff_arg.lovea_file) { fprintf(stderr, " > LOV EA information written to %s/" LOVEA_FNAME "\n", options.output_dir); fclose(options.diff_arg.lovea_file); } if (options.diff_arg.fid_remap_file) { fprintf(stderr, " > FID remapping written to %s/" FIDREMAP_FNAME "\n", options.output_dir); fclose(options.diff_arg.fid_remap_file); } #endif fprintf(stderr, "End of scan\n"); DisplayLog(LVL_MAJOR, DIFF_TAG, "All tasks done! Exiting."); rc = 0; clean_tag: /* destroy the tag before exit */ if (options.diff_arg.db_tag != NULL && ensure_db_access()) { fprintf(stderr, "Cleaning diff table...\n"); ListMgr_DestroyTag(&lmgr, options.diff_arg.db_tag); } exit(rc); return rc; /* for compiler */ }