void read_ui_config(void) { fnchar uiconfname[BUFSZ]; /* If running in connection-only mode, we won't know the file to look at the first time we call get_config_name. So instead, we put this off until after we're already logged in. */ if (get_config_name(uiconfname, TRUE)) read_config_file(uiconfname); }
void write_nh_config(void) { FILE *fp; fnchar filename[BUFSZ]; if (get_config_name(filename, FALSE) && (fp = open_config_file(filename))) { write_config_options(fp, nh_options); fclose(fp); } }
void write_ui_config(void) { FILE *fp; fnchar filename[BUFSZ]; if (get_config_name(filename, TRUE) && (fp = open_config_file(filename))) { write_config_options(fp, curses_options); fclose(fp); } }
int reusable_tape( tape_t *tp) { if (tp == NULL) return 0; if (tp->reuse == 0) return 0; if (g_str_equal(tp->datestamp, "0")) return 1; if (tp->config && !g_str_equal(tp->config, get_config_name())) return 0; compute_retention(); return (!tp->retention && !tp->retention_nb); }
tape_t * lookup_last_reusable_tape( const char *l_template, const char *tapepool, const char *storage, int retention_tapes, int retention_days G_GNUC_UNUSED, int retention_recover G_GNUC_UNUSED, int retention_full G_GNUC_UNUSED, int skip) { tape_t *tp, **tpsave; int count=0; int s; /* * The idea here is we keep the last "several" reusable tapes we * find in a stack and then return the n-th oldest one to the * caller. If skip is zero, the oldest is returned, if it is * one, the next oldest, two, the next to next oldest and so on. */ compute_retention(); tpsave = g_malloc((skip + 1) * sizeof(*tpsave)); for (s = 0; s <= skip; s++) { tpsave[s] = NULL; } for (tp = tape_list; tp != NULL; tp = tp->next) { if (tp->reuse == 1 && !tp->retention && !g_str_equal(tp->datestamp, "0") && (!tp->config || g_str_equal(tp->config, get_config_name())) && (!tp->storage || g_str_equal(tp->storage, storage)) && (!tp->pool || g_str_equal(tp->pool, tapepool)) && (match_labelstr_template(l_template, tp->label, tp->barcode, tp->meta, tp->storage))) { count++; for(s = skip; s > 0; s--) { tpsave[s] = tpsave[s - 1]; } tpsave[0] = tp; } } s = retention_tapes + 1 - count; if (s < 0) s = 0; if (skip < s) tp = NULL; else tp = tpsave[skip - s]; amfree(tpsave); return tp; }
static char * tape_hash_key( const char *pool, const char *label) { char *tape_key; if (pool) { tape_key = g_strdup_printf("P:%s-L:%s", pool, label); } else { tape_key = g_strdup_printf("P:%s-L:%s", get_config_name(), label); } return tape_key; }
void write_config(void) { FILE *fp; fnchar filename[BUFSZ]; fnchar uiconfname[BUFSZ]; get_config_name(filename, FALSE); get_config_name(uiconfname, TRUE); fp = open_config_file(filename); if (fp && should_write_config()) { write_config_options(fp, nh_get_options(GAME_OPTIONS)); write_config_options(fp, nh_get_options(CURRENT_BIRTH_OPTIONS)); fclose(fp); } fp = open_config_file(uiconfname); if (fp) { write_config_options(fp, curses_options); fclose(fp); } }
bool get_config(xmlrpc_env *env, ServerConfig &config) { const char *config_name = get_config_name(env); if (env->fault_occurred) return false; if (!config.read(config_name)) { xmlrpc_env_set_fault_formatted(env, ARCH_DAT_NO_INDEX, "Cannot open config '%s'", config_name); return false; } return true; }
nh_bool read_nh_config(void) { fnchar filename[BUFSZ]; struct nh_option_desc *opts = nh_get_options(); if (!opts) return FALSE; nhlib_free_optlist(nh_options); nh_options = opts; get_config_name(filename, FALSE); read_config_file(filename); return TRUE; }
void startup_tape_process( char *taper_program) { int fd[2]; char **config_options; if(socketpair(AF_UNIX, SOCK_STREAM, 0, fd) == -1) { error(_("taper pipe: %s"), strerror(errno)); /*NOTREACHED*/ } if(fd[0] < 0 || fd[0] >= (int)FD_SETSIZE) { error(_("taper socketpair 0: descriptor %d out of range (0 .. %d)\n"), fd[0], (int)FD_SETSIZE-1); /*NOTREACHED*/ } if(fd[1] < 0 || fd[1] >= (int)FD_SETSIZE) { error(_("taper socketpair 1: descriptor %d out of range (0 .. %d)\n"), fd[1], (int)FD_SETSIZE-1); /*NOTREACHED*/ } switch(taper_pid = fork()) { case -1: error(_("fork taper: %s"), strerror(errno)); /*NOTREACHED*/ case 0: /* child process */ aclose(fd[0]); if(dup2(fd[1], 0) == -1 || dup2(fd[1], 1) == -1) error(_("taper dup2: %s"), strerror(errno)); config_options = get_config_options(2); config_options[0] = "taper"; config_options[1] = get_config_name(); safe_fd(-1, 0); execve(taper_program, config_options, safe_env()); error("exec %s: %s", taper_program, strerror(errno)); /*NOTREACHED*/ default: /* parent process */ aclose(fd[1]); taper = fd[0]; taper_ev_read = NULL; } }
void startup_chunk_process( chunker_t *chunker, char *chunker_program) { int fd[2]; char **config_options; if(socketpair(AF_UNIX, SOCK_STREAM, 0, fd) == -1) { error(_("%s pipe: %s"), chunker->name, strerror(errno)); /*NOTREACHED*/ } switch(chunker->pid = fork()) { case -1: error(_("fork %s: %s"), chunker->name, strerror(errno)); /*NOTREACHED*/ case 0: /* child process */ aclose(fd[0]); if(dup2(fd[1], 0) == -1 || dup2(fd[1], 1) == -1) { error(_("%s dup2: %s"), chunker->name, strerror(errno)); /*NOTREACHED*/ } config_options = get_config_options(2); config_options[0] = chunker->name ? chunker->name : "chunker", config_options[1] = get_config_name(); safe_fd(-1, 0); execve(chunker_program, config_options, safe_env()); error(_("exec %s (%s): %s"), chunker_program, chunker->name, strerror(errno)); /*NOTREACHED*/ default: /* parent process */ aclose(fd[1]); chunker->down = 0; chunker->fd = fd[0]; chunker->ev_read = NULL; g_fprintf(stderr,_("driver: started %s pid %u\n"), chunker->name, (unsigned)chunker->pid); fflush(stderr); } }
static void load_config(struct brubeck_server *server, const char *path) { json_error_t error; /* required */ int capacity; json_t *backends, *samplers; /* optional */ int expire = 0; char *http = NULL; server->name = "brubeck"; server->config_name = get_config_name(path); server->dump_path = NULL; server->config = json_load_file(path, 0, &error); if (!server->config) { die("failed to load config file, %s (%s:%d:%d)", error.text, error.source, error.line, error.column); } json_unpack_or_die(server->config, "{s?:s, s:s, s:i, s:o, s:o, s?:s, s?:i}", "server_name", &server->name, "dumpfile", &server->dump_path, "capacity", &capacity, "backends", &backends, "samplers", &samplers, "http", &http, "expire", &expire); gh_log_set_instance(server->name); server->metrics = brubeck_hashtable_new(1 << capacity); if (!server->metrics) die("failed to initialize hash table (size: %lu)", 1ul << capacity); load_backends(server, backends); load_samplers(server, samplers); if (http) brubeck_http_endpoint_init(server, http); if (expire) server->fd_expire = load_timerfd(expire); }
int main( int argc, char ** argv) { int c; char *command; application_argument_t argument; int i; #ifdef GNUTAR gnutar_path = GNUTAR; #else gnutar_path = NULL; #endif gnutar_directory = NULL; gnutar_onefilesystem = 1; gnutar_atimepreserve = 1; gnutar_checkdevice = 1; gnutar_sparse = 1; gnutar_no_unquote = 0; exit_handling = NULL; /* initialize */ /* * 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"); if (argc < 2) { printf("ERROR no command given to amgtar\n"); error(_("No command given to amgtar")); } /* drop root privileges */ if (!set_root_privs(0)) { if (strcmp(argv[1], "selfcheck") == 0) { printf("ERROR amgtar must be run setuid root\n"); } error(_("amgtar must be run setuid root")); } safe_fd(3, 2); set_pname("amgtar"); /* Don't die when child closes pipe */ signal(SIGPIPE, SIG_IGN); #if defined(USE_DBMALLOC) malloc_size_1 = malloc_inuse(&malloc_hist_1); #endif add_amanda_log_handler(amanda_log_stderr); add_amanda_log_handler(amanda_log_syslog); dbopen(DBG_SUBDIR_CLIENT); startclock(); dbprintf(_("version %s\n"), version()); config_init(CONFIG_INIT_CLIENT, NULL); //check_running_as(RUNNING_AS_DUMPUSER_PREFERRED); //root for amrecover //RUNNING_AS_CLIENT_LOGIN from selfcheck, sendsize, sendbackup /* parse argument */ command = argv[1]; argument.config = NULL; argument.host = NULL; argument.message = 0; argument.collection = 0; argument.calcsize = 0; argument.tar_blocksize = NULL; argument.level = NULL; init_dle(&argument.dle); while (1) { int option_index = 0; c = getopt_long (argc, argv, "", long_options, &option_index); if (c == -1) { break; } switch (c) { case 1: argument.config = stralloc(optarg); break; case 2: argument.host = stralloc(optarg); break; case 3: argument.dle.disk = stralloc(optarg); break; case 4: argument.dle.device = stralloc(optarg); break; case 5: argument.level = g_slist_append(argument.level, GINT_TO_POINTER(atoi(optarg))); break; case 6: argument.dle.create_index = 1; break; case 7: argument.message = 1; break; case 8: argument.collection = 1; break; case 9: argument.dle.record = 1; break; case 10: gnutar_path = stralloc(optarg); break; case 11: gnutar_listdir = stralloc(optarg); break; case 12: if (optarg && strcasecmp(optarg, "NO") == 0) gnutar_onefilesystem = 0; else if (optarg && strcasecmp(optarg, "YES") == 0) gnutar_onefilesystem = 1; else if (strcasecmp(command, "selfcheck") == 0) printf(_("ERROR [%s: bad ONE-FILE-SYSTEM property value (%s)]\n"), get_pname(), optarg); break; case 13: if (optarg && strcasecmp(optarg, "NO") == 0) gnutar_sparse = 0; else if (optarg && strcasecmp(optarg, "YES") == 0) gnutar_sparse = 1; else if (strcasecmp(command, "selfcheck") == 0) printf(_("ERROR [%s: bad SPARSE property value (%s)]\n"), get_pname(), optarg); break; case 14: if (optarg && strcasecmp(optarg, "NO") == 0) gnutar_atimepreserve = 0; else if (optarg && strcasecmp(optarg, "YES") == 0) gnutar_atimepreserve = 1; else if (strcasecmp(command, "selfcheck") == 0) printf(_("ERROR [%s: bad ATIME-PRESERVE property value (%s)]\n"), get_pname(), optarg); break; case 15: if (optarg && strcasecmp(optarg, "NO") == 0) gnutar_checkdevice = 0; else if (optarg && strcasecmp(optarg, "YES") == 0) gnutar_checkdevice = 1; else if (strcasecmp(command, "selfcheck") == 0) printf(_("ERROR [%s: bad CHECK-DEVICE property value (%s)]\n"), get_pname(), optarg); break; case 16: if (optarg) argument.dle.include_file = append_sl(argument.dle.include_file, optarg); break; case 17: if (optarg) argument.dle.include_list = append_sl(argument.dle.include_list, optarg); break; case 18: argument.dle.include_optional = 1; break; case 19: if (optarg) argument.dle.exclude_file = append_sl(argument.dle.exclude_file, optarg); break; case 20: if (optarg) argument.dle.exclude_list = append_sl(argument.dle.exclude_list, optarg); break; case 21: argument.dle.exclude_optional = 1; break; case 22: gnutar_directory = stralloc(optarg); break; case 23: if (optarg) normal_message = g_slist_append(normal_message, optarg); break; case 24: if (optarg) ignore_message = g_slist_append(ignore_message, optarg); break; case 25: if (optarg) strange_message = g_slist_append(strange_message, optarg); break; case 26: if (optarg) exit_handling = stralloc(optarg); break; case 27: argument.calcsize = 1; break; case 28: argument.tar_blocksize = stralloc(optarg); break; case 29: if (optarg && strcasecmp(optarg, "NO") == 0) gnutar_no_unquote = 0; else if (optarg && strcasecmp(optarg, "YES") == 0) gnutar_no_unquote = 1; else if (strcasecmp(command, "selfcheck") == 0) printf(_("ERROR [%s: bad No_UNQUOTE property value (%s)]\n"), get_pname(), optarg); break; case ':': case '?': break; } } argument.argc = argc - optind; argument.argv = argv + optind; if (argument.config) { config_init(CONFIG_INIT_CLIENT | CONFIG_INIT_EXPLICIT_NAME | CONFIG_INIT_OVERLAY, argument.config); dbrename(get_config_name(), DBG_SUBDIR_CLIENT); } if (config_errors(NULL) >= CFGERR_ERRORS) { g_critical(_("errors processing config file")); } re_table = build_re_table(init_re_table, normal_message, ignore_message, strange_message); for(i=0;i<256;i++) exit_value[i] = 1; /* BAD */ exit_value[0] = 0; /* GOOD */ exit_value[1] = 0; /* GOOD */ if (exit_handling) { char *s = exit_handling; while (s) { char *r = strchr(s, '='); if (r) { int j = atoi(s); if (j >= 0 && j < 256) { r++; if (strncasecmp(r, "GOOD", 4) == 0) { exit_value[j] = 0; } } } s = strchr(s+1, ' '); } } gnutar_listdir = getconf_str(CNF_GNUTAR_LIST_DIR); if (strlen(gnutar_listdir) == 0) gnutar_listdir = NULL; if (gnutar_path) { dbprintf("GNUTAR-PATH %s\n", gnutar_path); } else { dbprintf("GNUTAR-PATH is not set\n"); } if (gnutar_listdir) { dbprintf("GNUTAR-LISTDIR %s\n", gnutar_listdir); } else { dbprintf("GNUTAR-LISTDIR is not set\n"); } if (gnutar_directory) { dbprintf("DIRECTORY %s\n", gnutar_directory); } dbprintf("ONE-FILE-SYSTEM %s\n", gnutar_onefilesystem? "yes":"no"); dbprintf("SPARSE %s\n", gnutar_sparse? "yes":"no"); dbprintf("NO-UNQUOTE %s\n", gnutar_no_unquote? "yes":"no"); dbprintf("ATIME-PRESERVE %s\n", gnutar_atimepreserve? "yes":"no"); dbprintf("CHECK-DEVICE %s\n", gnutar_checkdevice? "yes":"no"); { amregex_t *rp; for (rp = re_table; rp->regex != NULL; rp++) { switch (rp->typ) { case DMP_NORMAL : dbprintf("NORMAL %s\n", rp->regex); break; case DMP_IGNORE : dbprintf("IGNORE %s\n", rp->regex); break; case DMP_STRANGE: dbprintf("STRANGE %s\n", rp->regex); break; case DMP_SIZE : dbprintf("SIZE %s\n", rp->regex); break; case DMP_ERROR : dbprintf("ERROR %s\n", rp->regex); break; } } } if (strcmp(command, "support") == 0) { amgtar_support(&argument); } else if (strcmp(command, "selfcheck") == 0) { amgtar_selfcheck(&argument); } else if (strcmp(command, "estimate") == 0) { amgtar_estimate(&argument); } else if (strcmp(command, "backup") == 0) { amgtar_backup(&argument); } else if (strcmp(command, "restore") == 0) { amgtar_restore(&argument); } else if (strcmp(command, "validate") == 0) { amgtar_validate(&argument); } else { dbprintf("Unknown command `%s'.\n", command); fprintf(stderr, "Unknown command `%s'.\n", command); exit (1); } return 0; }
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; }
int main(int argc, char ** argv) { char * tapelist_name; taper_state_t state; config_overwrites_t *cfg_ovr = NULL; char *cfg_opt = NULL; /* * 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); set_pname("taper"); dbopen("server"); device_api_init(); init_taper_state(&state); /* Don't die when child closes pipe */ signal(SIGPIPE, SIG_IGN); g_fprintf(stderr, _("%s: pid %ld executable %s version %s\n"), get_pname(), (long) getpid(), argv[0], version()); dbprintf(_("%s: pid %ld executable %s version %s\n"), get_pname(), (long) getpid(), argv[0], version()); /* Process options */ cfg_ovr = extract_commandline_config_overwrites(&argc, &argv); if(argc > 2) { error("Too many arguments!\n"); g_assert_not_reached(); } if (argc > 1) cfg_opt = argv[1]; config_init(CONFIG_INIT_EXPLICIT_NAME | CONFIG_INIT_USE_CWD, cfg_opt); apply_config_overwrites(cfg_ovr); if (config_errors(NULL) >= CFGERR_ERRORS) { g_critical(_("errors processing config file")); } safe_cd(); add_amanda_log_handler(amanda_log_stderr); add_amanda_log_handler(amanda_log_trace_log); check_running_as(RUNNING_AS_DUMPUSER); dbrename(get_config_name(), DBG_SUBDIR_SERVER); log_add(L_INFO, "%s pid %ld", get_pname(), (long)getpid()); tapelist_name = config_dir_relative(getconf_str(CNF_TAPELIST)); if (read_tapelist(tapelist_name) != 0) { log_add(L_INFO, "pid-done %ld", (long)getpid()); error("could not load tapelist \"%s\"", tapelist_name); g_assert_not_reached(); } amfree(tapelist_name); state.have_changer = changer_init(); if (state.have_changer < 0) { log_add(L_INFO, "pid-done %ld", (long)getpid()); error("changer initialization failed: %s", strerror(errno)); g_assert_not_reached(); } state.next_tape_label = NULL; state.next_tape_device = NULL; state.cur_tape = 0; if (!find_first_tape(&state)) { log_add(L_INFO, "pid-done %ld", (long)getpid()); return EXIT_SUCCESS; } while (process_driver_command(&state)); log_add(L_INFO, "pid-done %ld", (long)getpid()); return EXIT_SUCCESS; }
int main( int argc, char ** argv) { int i; time_t timer; char *lineread = NULL; struct sigaction act, oact; extern char *optarg; extern int optind; char *line = NULL; const security_driver_t *secdrv; char *req = NULL; int response_error; struct tm *tm; config_overrides_t *cfg_ovr; char *starting_hostname = NULL; /* * 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); set_pname("amrecover"); /* Don't die when child closes pipe */ signal(SIGPIPE, SIG_IGN); dbopen(DBG_SUBDIR_CLIENT); /* treat amrecover-specific command line options as the equivalent * -o command-line options to set configuration values */ cfg_ovr = new_config_overrides(argc/2); /* If the first argument is not an option flag, then we assume * it is a configuration name to match the syntax of the other * Amanda utilities. */ if (argc > 1 && argv[1][0] != '-') { add_config_override(cfg_ovr, "conf", argv[1]); /* remove that option from the command line */ argv[1] = argv[0]; argv++; argc--; } /* now parse regular command-line '-' options */ while ((i = getopt(argc, argv, "o:C:s:t:d:Uh:")) != EOF) { switch (i) { case 'C': add_config_override(cfg_ovr, "conf", optarg); break; case 's': add_config_override(cfg_ovr, "index_server", optarg); break; case 't': add_config_override(cfg_ovr, "tape_server", optarg); break; case 'd': add_config_override(cfg_ovr, "tapedev", optarg); break; case 'o': add_config_override_opt(cfg_ovr, optarg); break; case 'h': starting_hostname = g_strdup(optarg); break; case 'U': case '?': (void)g_printf(USAGE); return 0; } } if (optind != argc) { (void)g_fprintf(stderr, USAGE); exit(1); } /* load the base client configuration */ set_config_overrides(cfg_ovr); config_init(CONFIG_INIT_CLIENT, NULL); if (config_errors(NULL) >= CFGERR_WARNINGS) { config_print_errors(); if (config_errors(NULL) >= CFGERR_ERRORS) { g_critical(_("errors processing config file")); } } /* and now try to load the configuration named in that file */ config_init(CONFIG_INIT_CLIENT | CONFIG_INIT_EXPLICIT_NAME | CONFIG_INIT_OVERLAY, getconf_str(CNF_CONF)); check_running_as(RUNNING_AS_ROOT); dbrename(get_config_name(), DBG_SUBDIR_CLIENT); our_features = am_init_feature_set(); our_features_string = am_feature_to_string(our_features); if (!starting_hostname) { starting_hostname = g_malloc(MAX_HOSTNAME_LENGTH+1); if (gethostname(starting_hostname, MAX_HOSTNAME_LENGTH) != 0) { error(_("cannot determine local host name\n")); /*NOTREACHED*/ } starting_hostname[MAX_HOSTNAME_LENGTH] = '\0'; } server_name = NULL; if (getconf_seen(CNF_INDEX_SERVER) == -2) { /* command line argument */ server_name = getconf_str(CNF_INDEX_SERVER); } if (!server_name) { server_name = getenv("AMANDA_SERVER"); if (server_name) { g_printf(_("Using index server from environment AMANDA_SERVER (%s)\n"), server_name); } } if (!server_name) { server_name = getconf_str(CNF_INDEX_SERVER); } if (!server_name) { error(_("No index server set")); /*NOTREACHED*/ } server_name = g_strdup(server_name); tape_server_name = NULL; if (getconf_seen(CNF_TAPE_SERVER) == -2) { /* command line argument */ tape_server_name = getconf_str(CNF_TAPE_SERVER); } if (!tape_server_name) { tape_server_name = getenv("AMANDA_TAPE_SERVER"); if (!tape_server_name) { tape_server_name = getenv("AMANDA_TAPESERVER"); if (tape_server_name) { g_printf(_("Using tape server from environment AMANDA_TAPESERVER (%s)\n"), tape_server_name); } } else { g_printf(_("Using tape server from environment AMANDA_TAPE_SERVER (%s)\n"), tape_server_name); } } if (!tape_server_name) { tape_server_name = getconf_str(CNF_TAPE_SERVER); } if (!tape_server_name) { error(_("No tape server set")); /*NOTREACHED*/ } tape_server_name = g_strdup(tape_server_name); amfree(tape_device_name); tape_device_name = getconf_str(CNF_TAPEDEV); if (!tape_device_name || strlen(tape_device_name) == 0 || !getconf_seen(CNF_TAPEDEV)) { tape_device_name = NULL; } else { tape_device_name = g_strdup(tape_device_name); } authopt = g_strdup(getconf_str(CNF_AUTH)); amfree(disk_name); amfree(mount_point); amfree(disk_path); dump_date[0] = '\0'; /* Don't die when child closes pipe */ signal(SIGPIPE, SIG_IGN); /* set up signal handler */ act.sa_handler = sigint_handler; sigemptyset(&act.sa_mask); act.sa_flags = 0; if (sigaction(SIGINT, &act, &oact) != 0) { error(_("error setting signal handler: %s"), strerror(errno)); /*NOTREACHED*/ } proplist = g_hash_table_new_full(g_str_hash, g_str_equal, &g_free, &free_property_t); protocol_init(); /* We assume that amindexd support fe_amindexd_options_features */ /* and fe_amindexd_options_auth */ /* We should send a noop to really know */ req = g_strdup_printf("SERVICE amindexd\n" "OPTIONS features=%s;auth=%s;\n", our_features_string, authopt); secdrv = security_getdriver(authopt); if (secdrv == NULL) { error(_("no '%s' security driver available for host '%s'"), authopt, server_name); /*NOTREACHED*/ } protocol_sendreq(server_name, secdrv, generic_client_get_security_conf, req, STARTUP_TIMEOUT, amindexd_response, &response_error); amfree(req); protocol_run(); g_printf(_("AMRECOVER Version %s. Contacting server on %s ...\n"), VERSION, server_name); if(response_error != 0) { g_fprintf(stderr,"%s\n",errstr); exit(1); } /* get server's banner */ if (grab_reply(1) == -1) { aclose(server_socket); exit(1); } if (!server_happy()) { dbclose(); aclose(server_socket); exit(1); } /* try to get the features from the server */ { char *their_feature_string = NULL; indexsrv_features = NULL; line = g_strdup_printf("FEATURES %s", our_features_string); if(exchange(line) == 0) { their_feature_string = g_strdup(server_line+13); indexsrv_features = am_string_to_feature(their_feature_string); if (!indexsrv_features) g_printf(_("Bad feature string from server: %s"), their_feature_string); } if (!indexsrv_features) indexsrv_features = am_set_default_feature_set(); amfree(their_feature_string); amfree(line); } /* set the date of extraction to be today */ (void)time(&timer); tm = localtime(&timer); if (tm) strftime(dump_date, sizeof(dump_date), "%Y-%m-%d", tm); else error(_("BAD DATE")); g_printf(_("Setting restore date to today (%s)\n"), dump_date); line = g_strdup_printf("DATE %s", dump_date); if (converse(line) == -1) { aclose(server_socket); exit(1); } amfree(line); line = g_strdup_printf("SCNF %s", get_config_name()); if (converse(line) == -1) { aclose(server_socket); exit(1); } amfree(line); if (server_happy()) { /* set host we are restoring to this host by default */ amfree(dump_hostname); set_host(starting_hostname); if (dump_hostname) g_printf(_("Use the setdisk command to choose dump disk to recover\n")); else g_printf(_("Use the sethost command to choose a host to recover\n")); } quit_prog = 0; do { if ((lineread = readline("amrecover> ")) == NULL) { clearerr(stdin); putchar('\n'); break; } if (lineread[0] != '\0') { add_history(lineread); dbprintf(_("user command: '%s'\n"), lineread); process_line(lineread); /* act on line's content */ } amfree(lineread); } while (!quit_prog); dbclose(); aclose(server_socket); return 0; }
int main( int argc, char ** argv) { int foreground; int batch; int redirect; char **datearg = NULL; int nb_datearg = 0; char *conf_diskfile; char *conf_tapelist; char *conf_logfile; int conf_usetimestamps; disklist_t diskq; disk_t *dp; pid_t pid; pid_t driver_pid, reporter_pid; amwait_t exitcode; int opt; GSList *holding_list=NULL, *holding_file; int driver_pipe[2]; char date_string[100]; char date_string_standard[100]; time_t today; char *errstr; struct tm *tm; char *tapedev; char *tpchanger; char *qdisk, *qhname; GSList *datestamp_list = NULL; config_overrides_t *cfg_ovr; char **config_options; find_result_t *holding_files; disklist_t holding_disklist = { NULL, NULL }; /* * 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("amflush"); /* Don't die when child closes pipe */ signal(SIGPIPE, SIG_IGN); dbopen(DBG_SUBDIR_SERVER); add_amanda_log_handler(amanda_log_stderr); foreground = 0; batch = 0; redirect = 1; /* process arguments */ cfg_ovr = new_config_overrides(argc/2); while((opt = getopt(argc, argv, "bfso:D:")) != EOF) { switch(opt) { case 'b': batch = 1; break; case 'f': foreground = 1; break; case 's': redirect = 0; break; case 'o': add_config_override_opt(cfg_ovr, optarg); break; case 'D': if (datearg == NULL) datearg = g_malloc(21*sizeof(char *)); if(nb_datearg == 20) { g_fprintf(stderr,_("maximum of 20 -D arguments.\n")); exit(1); } datearg[nb_datearg++] = g_strdup(optarg); datearg[nb_datearg] = NULL; break; } } argc -= optind, argv += optind; if(!foreground && !redirect) { g_fprintf(stderr,_("Can't redirect to stdout/stderr if not in forground.\n")); exit(1); } if(argc < 1) { error(_("Usage: amflush [-b] [-f] [-s] [-D date]* [-o configoption]* <confdir> [host [disk]* ]*")); /*NOTREACHED*/ } set_config_overrides(cfg_ovr); config_init(CONFIG_INIT_EXPLICIT_NAME, argv[0]); conf_diskfile = config_dir_relative(getconf_str(CNF_DISKFILE)); read_diskfile(conf_diskfile, &diskq); 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); /* load DLEs from the holding disk, in case there's anything to flush there */ search_holding_disk(&holding_files, &holding_disklist); /* note that the dumps are added to the global disklist, so we need not * consult holding_files or holding_disklist after this. The holding-only * dumps will be filtered properly by match_disklist, setting the dp->todo * flag appropriately. */ errstr = match_disklist(&diskq, argc-1, argv+1); if (errstr) { g_printf(_("%s"),errstr); amfree(errstr); } 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); conf_usetimestamps = getconf_boolean(CNF_USETIMESTAMPS); amflush_datestamp = get_datestamp_from_time(0); if(conf_usetimestamps == 0) { amflush_timestamp = g_strdup(amflush_datestamp); } else { amflush_timestamp = get_timestamp_from_time(0); } conf_logdir = config_dir_relative(getconf_str(CNF_LOGDIR)); conf_logfile = g_strjoin(NULL, conf_logdir, "/log", NULL); if (access(conf_logfile, F_OK) == 0) { run_amcleanup(get_config_name()); } if (access(conf_logfile, F_OK) == 0) { char *process_name = get_master_process(conf_logfile); error(_("%s exists: %s is already running, or you must run amcleanup"), conf_logfile, process_name); /*NOTREACHED*/ } driver_program = g_strjoin(NULL, amlibexecdir, "/", "driver", NULL); reporter_program = g_strjoin(NULL, sbindir, "/", "amreport", NULL); logroll_program = g_strjoin(NULL, amlibexecdir, "/", "amlogroll", NULL); tapedev = getconf_str(CNF_TAPEDEV); tpchanger = getconf_str(CNF_TPCHANGER); if (tapedev == NULL && tpchanger == NULL) { error(_("No tapedev or tpchanger specified")); } /* if dates were specified (-D), then use match_datestamp * against the list of all datestamps to turn that list * into a set of existing datestamps (basically, evaluate the * expressions into actual datestamps) */ if(datearg) { GSList *all_datestamps; GSList *datestamp; int i, ok; all_datestamps = holding_get_all_datestamps(); for(datestamp = all_datestamps; datestamp != NULL; datestamp = datestamp->next) { ok = 0; for(i=0; i<nb_datearg && ok==0; i++) { ok = match_datestamp(datearg[i], (char *)datestamp->data); } if (ok) datestamp_list = g_slist_insert_sorted(datestamp_list, g_strdup((char *)datestamp->data), g_compare_strings); } slist_free_full(all_datestamps, g_free); } else { /* otherwise, in batch mode, use all datestamps */ if(batch) { datestamp_list = holding_get_all_datestamps(); } /* or allow the user to pick datestamps */ else { datestamp_list = pick_datestamp(); } } if(!datestamp_list) { g_printf(_("Could not find any Amanda directories to flush.\n")); exit(1); } holding_list = holding_get_files_for_flush(datestamp_list); if (holding_list == NULL) { g_printf(_("Could not find any valid dump image, check directory.\n")); exit(1); } if (access(conf_logfile, F_OK) == 0) { char *process_name = get_master_process(conf_logfile); error(_("%s exists: someone started %s"), conf_logfile, process_name); /*NOTREACHED*/ } log_add(L_INFO, "%s pid %ld", get_pname(), (long)getpid()); if(!batch) confirm(datestamp_list); for(dp = diskq.head; dp != NULL; dp = dp->next) { if(dp->todo) { char *qname; qname = quote_string(dp->name); log_add(L_DISK, "%s %s", dp->host->hostname, qname); amfree(qname); } } if(!foreground) { /* write it before redirecting stdout */ puts(_("Running in background, you can log off now.")); puts(_("You'll get mail when amflush is finished.")); } if(redirect) redirect_stderr(); if(!foreground) detach(); add_amanda_log_handler(amanda_log_stderr); add_amanda_log_handler(amanda_log_trace_log); today = time(NULL); tm = localtime(&today); if (tm) { strftime(date_string, 100, "%a %b %e %H:%M:%S %Z %Y", tm); strftime(date_string_standard, 100, "%Y-%m-%d %H:%M:%S %Z", tm); } else { error(_("BAD DATE")); /* should never happen */ } g_fprintf(stderr, _("amflush: start at %s\n"), date_string); g_fprintf(stderr, _("amflush: datestamp %s\n"), amflush_timestamp); g_fprintf(stderr, _("amflush: starttime %s\n"), amflush_timestamp); g_fprintf(stderr, _("amflush: starttime-locale-independent %s\n"), date_string_standard); log_add(L_START, _("date %s"), amflush_timestamp); /* START DRIVER */ if(pipe(driver_pipe) == -1) { error(_("error [opening pipe to driver: %s]"), strerror(errno)); /*NOTREACHED*/ } if((driver_pid = fork()) == 0) { /* * This is the child process. */ dup2(driver_pipe[0], 0); close(driver_pipe[1]); config_options = get_config_options(3); config_options[0] = "driver"; config_options[1] = get_config_name(); config_options[2] = "nodump"; safe_fd(-1, 0); execve(driver_program, config_options, safe_env()); error(_("cannot exec %s: %s"), driver_program, strerror(errno)); /*NOTREACHED*/ } else if(driver_pid == -1) { error(_("cannot fork for %s: %s"), driver_program, strerror(errno)); /*NOTREACHED*/ } driver_stream = fdopen(driver_pipe[1], "w"); if (!driver_stream) { error(_("Can't fdopen: %s"), strerror(errno)); /*NOTREACHED*/ } g_fprintf(driver_stream, "DATE %s\n", amflush_timestamp); for(holding_file=holding_list; holding_file != NULL; holding_file = holding_file->next) { dumpfile_t file; holding_file_get_dumpfile((char *)holding_file->data, &file); if (holding_file_size((char *)holding_file->data, 1) <= 0) { g_debug("%s is empty - ignoring", (char *)holding_file->data); log_add(L_INFO, "%s: removing file with no data.", (char *)holding_file->data); holding_file_unlink((char *)holding_file->data); dumpfile_free_data(&file); continue; } /* search_holding_disk should have already ensured that every * holding dumpfile has an entry in the dynamic disklist */ dp = lookup_disk(file.name, file.disk); assert(dp != NULL); /* but match_disklist may have indicated we should not flush it */ if (dp->todo == 0) continue; qdisk = quote_string(file.disk); qhname = quote_string((char *)holding_file->data); g_fprintf(stderr, "FLUSH %s %s %s %d %s\n", file.name, qdisk, file.datestamp, file.dumplevel, qhname); g_debug("flushing '%s'", (char *)holding_file->data); g_fprintf(driver_stream, "FLUSH %s %s %s %d %s\n", file.name, qdisk, file.datestamp, file.dumplevel, qhname); amfree(qdisk); amfree(qhname); dumpfile_free_data(&file); } g_fprintf(stderr, "ENDFLUSH\n"); fflush(stderr); g_fprintf(driver_stream, "ENDFLUSH\n"); fflush(driver_stream); fclose(driver_stream); /* WAIT DRIVER */ while(1) { if((pid = wait(&exitcode)) == -1) { if(errno == EINTR) { continue; } else { error(_("wait for %s: %s"), driver_program, strerror(errno)); /*NOTREACHED*/ } } else if (pid == driver_pid) { break; } } slist_free_full(datestamp_list, g_free); datestamp_list = NULL; slist_free_full(holding_list, g_free); holding_list = NULL; if(redirect) { /* rename errfile */ char *errfile, *errfilex, *nerrfilex, number[100]; int tapecycle; int maxdays, days; struct stat stat_buf; errfile = g_strjoin(NULL, conf_logdir, "/amflush", NULL); errfilex = NULL; nerrfilex = NULL; tapecycle = getconf_int(CNF_TAPECYCLE); maxdays = tapecycle + 2; days = 1; /* First, find out the last existing errfile, */ /* to avoid ``infinite'' loops if tapecycle is infinite */ g_snprintf(number,100,"%d",days); errfilex = newvstralloc(errfilex, errfile, ".", number, NULL); while ( days < maxdays && stat(errfilex,&stat_buf)==0) { days++; g_snprintf(number,100,"%d",days); errfilex = newvstralloc(errfilex, errfile, ".", number, NULL); } g_snprintf(number,100,"%d",days); errfilex = newvstralloc(errfilex, errfile, ".", number, NULL); nerrfilex = NULL; while (days > 1) { amfree(nerrfilex); nerrfilex = errfilex; days--; g_snprintf(number,100,"%d",days); errfilex = g_strjoin(NULL, errfile, ".", number, NULL); if (rename(errfilex, nerrfilex) != 0) { error(_("cannot rename \"%s\" to \"%s\": %s"), errfilex, nerrfilex, strerror(errno)); /*NOTREACHED*/ } } errfilex = newvstralloc(errfilex, errfile, ".1", NULL); if (rename(errfile,errfilex) != 0) { error(_("cannot rename \"%s\" to \"%s\": %s"), errfilex, nerrfilex, strerror(errno)); /*NOTREACHED*/ } amfree(errfile); amfree(errfilex); amfree(nerrfilex); } /* * Have amreport generate report and send mail. Note that we do * not bother checking the exit status. If it does not work, it * can be rerun. */ if((reporter_pid = fork()) == 0) { /* * This is the child process. */ config_options = get_config_options(3); config_options[0] = "amreport"; config_options[1] = get_config_name(); config_options[2] = "--from-amdump"; safe_fd(-1, 0); execve(reporter_program, config_options, safe_env()); error(_("cannot exec %s: %s"), reporter_program, strerror(errno)); /*NOTREACHED*/ } else if(reporter_pid == -1) { error(_("cannot fork for %s: %s"), reporter_program, strerror(errno)); /*NOTREACHED*/ } while(1) { if((pid = wait(&exitcode)) == -1) { if(errno == EINTR) { continue; } else { error(_("wait for %s: %s"), reporter_program, strerror(errno)); /*NOTREACHED*/ } } else if (pid == reporter_pid) { break; } } log_add(L_INFO, "pid-done %ld", (long)getpid()); /* * Call amlogroll to rename the log file to its datestamped version. * Since we exec at this point, our exit code will be that of amlogroll. */ config_options = get_config_options(2); config_options[0] = "amlogroll"; config_options[1] = get_config_name(); safe_fd(-1, 0); execve(logroll_program, config_options, safe_env()); error(_("cannot exec %s: %s"), logroll_program, strerror(errno)); /*NOTREACHED*/ return 0; /* keep the compiler happy */ }
void read_ui_config(void) { fnchar uiconfname[BUFSZ]; get_config_name(uiconfname, TRUE); read_config_file(uiconfname); }
int main( int argc, char ** argv) { config_overrides_t *cfg_ovr; char *hostname; char *auth; char *service; int opt; extern int optind; extern char *optarg; FILE *input_file; int use_connect = 0; int got_input_file = 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("amservice"); /* drop root privileges */ if (!set_root_privs(0)) { error(_("amservice must be run setuid root")); } /* Don't die when child closes pipe */ signal(SIGPIPE, SIG_IGN); dbopen(DBG_SUBDIR_SERVER); add_amanda_log_handler(amanda_log_stderr); our_features = am_init_feature_set(); our_feature_string = am_feature_to_string(our_features); /* process arguments */ cfg_ovr = new_config_overrides(argc/2); input_file = stdin; while((opt = getopt_long(argc, argv, "o:f:s", long_options, NULL)) != EOF) { switch(opt) { case 1: printf("amservice-%s\n", VERSION); return(0); break; case 'o': add_config_override_opt(cfg_ovr, optarg); break; case 'f': if (got_input_file == 1) { g_critical("Invalid two -f argument"); exit(1); } got_input_file = 1; if (*optarg == '/') { input_file = fopen(optarg, "r"); } else { char *name = g_strjoin(NULL, get_original_cwd(), "/", optarg, NULL); input_file = fopen(name, "r"); amfree(name); } if (!input_file) { g_critical("Cannot open output file '%s': %s", optarg, strerror(errno)); exit(1); } break; case 's': use_connect = 1; break; } } if (use_connect && !got_input_file) { g_critical("The -s option require -f"); exit(1); } argc -= optind, argv += optind; if(argc < 3) usage(); /* set a default config */ set_config_overrides(cfg_ovr); config_init(CONFIG_INIT_CLIENT, NULL); dbrename(get_config_name(), DBG_SUBDIR_SERVER); if (config_errors(NULL) >= CFGERR_WARNINGS) { config_print_errors(); if (config_errors(NULL) >= CFGERR_ERRORS) { g_critical(_("errors processing config file")); } } conf_ctimeout = (time_t)getconf_int(CNF_CTIMEOUT); hostname = argv[0]; auth = argv[1]; service = argv[2]; /* start client side checks */ copy_stream = use_connect && got_input_file; client_protocol(hostname, auth, service, input_file); amfree(our_feature_string); am_release_feature_set(our_features); our_features = NULL; if (got_input_file) fclose(input_file); dbclose(); return(remote_errors != 0); }
/* return == NULL: correct * != NULL: error message * * The ndmp-proxy is assumed to take a configuration name and a port number * on its command line, and to immediately attempt to bind to that port. If * the bind operation succeeds, it should print "OK\n" to stdout and close the * file descriptor. If the operation fails because the address is already * in use (and thus, most likely, there's another proxy running already), then * it should print "INUSE\n", close the file descriptor, and exit. For any other * failure, it should print a suitable error message and exit. */ static char * start_ndmp_proxy(void) { char *ndmp_proxy; GPtrArray *proxy_argv; char buffer[32769]; int proxy_in, proxy_out, proxy_err; int rc; pid_t pid; char *errmsg; amwait_t wait_status; proxy_argv = g_ptr_array_new(); g_ptr_array_add(proxy_argv, g_strdup("ndmp-proxy")); g_ptr_array_add(proxy_argv, g_strdup(get_config_name())); g_ptr_array_add(proxy_argv, g_strdup_printf("%d", getconf_int(CNF_NDMP_PROXY_PORT))); g_ptr_array_add(proxy_argv, NULL); ndmp_proxy = g_strdup_printf("%s/ndmp-proxy", amlibexecdir); proxy_in = open("/dev/null", O_RDONLY); proxy_err = debug_fd(); pid = pipespawnv(ndmp_proxy, STDOUT_PIPE, 0, &proxy_in, &proxy_out, &proxy_err, (char **)proxy_argv->pdata); close(proxy_in); g_ptr_array_free_full(proxy_argv); g_debug("started ndmp-proxy with pid %d", pid); /* wait for the proxy to say "OK" */ rc = full_read(proxy_out, buffer, sizeof(buffer)-1); if (rc == -1) { errmsg = g_strdup_printf("Error reading from ndmp-proxy: %s", strerror(errno)); /* clean up the PID if possible */ waitpid(pid, NULL, WNOHANG); return errmsg; } else if (rc == 0) { if (waitpid(pid, &wait_status, WNOHANG)) { errmsg = str_exit_status("ndmp-proxy", wait_status); } else { errmsg = g_strdup_printf("unexpected EOF from ndmp-proxy"); } return errmsg; } aclose(proxy_out); /* process the output */ buffer[rc] = '\0'; if (0 == strcmp(buffer, "OK\n")) { return NULL; } else if (0 == strcmp(buffer, "INUSE\n")) { g_warning("overlapping attempts to start ndmp-proxy; ignoring this attempt"); /* clean up the pid */ waitpid(pid, NULL, 0); return NULL; } else { errmsg = g_strdup_printf("ndmp-proxy failed: %s", buffer); return errmsg; } }
int main( int argc, char ** argv) { int interactive = 0; int level = 0; int mesgpipe[2]; dle_t *dle = NULL; char *dumpdate, *stroptions; char *qdisk = NULL; char *qamdevice = NULL; char *line = NULL; char *err_extra = NULL; char *s; int i; int ch; GSList *errlist; FILE *mesgstream; level_t *alevel; /* initialize */ /* * 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(DATA_FD_OFFSET, DATA_FD_COUNT*2); safe_cd(); set_pname("sendbackup"); /* Don't die when child closes pipe */ signal(SIGPIPE, SIG_IGN); /* Don't die when interrupt received */ signal(SIGINT, SIG_IGN); if(argc > 1 && strcmp(argv[1],"-t") == 0) { interactive = 1; argc--; argv++; } else { interactive = 0; } erroutput_type = (ERR_INTERACTIVE|ERR_SYSLOG); dbopen(DBG_SUBDIR_CLIENT); startclock(); dbprintf(_("Version %s\n"), version()); if(argc > 2 && strcmp(argv[1], "amandad") == 0) { amandad_auth = stralloc(argv[2]); } our_features = am_init_feature_set(); our_feature_string = am_feature_to_string(our_features); config_init(CONFIG_INIT_CLIENT, NULL); /* (check for config errors comes later) */ check_running_as(RUNNING_AS_CLIENT_LOGIN); if(interactive) { /* * In interactive (debug) mode, the backup data is sent to * /dev/null and none of the network connections back to driver * programs on the tape host are set up. The index service is * run and goes to stdout. */ g_fprintf(stderr, _("%s: running in interactive test mode\n"), get_pname()); fflush(stderr); } qdisk = NULL; dumpdate = NULL; stroptions = NULL; for(; (line = agets(stdin)) != NULL; free(line)) { if (line[0] == '\0') continue; if(interactive) { g_fprintf(stderr, "%s> ", get_pname()); fflush(stderr); } if(strncmp_const(line, "OPTIONS ") == 0) { g_options = parse_g_options(line+8, 1); if(!g_options->hostname) { g_options->hostname = alloc(MAX_HOSTNAME_LENGTH+1); gethostname(g_options->hostname, MAX_HOSTNAME_LENGTH); g_options->hostname[MAX_HOSTNAME_LENGTH] = '\0'; } if (g_options->config) { /* overlay this configuration on the existing (nameless) configuration */ config_init(CONFIG_INIT_CLIENT | CONFIG_INIT_EXPLICIT_NAME | CONFIG_INIT_OVERLAY, g_options->config); dbrename(get_config_name(), DBG_SUBDIR_CLIENT); } /* check for any config errors now */ if (config_errors(&errlist) >= CFGERR_ERRORS) { char *errstr = config_errors_to_error_string(errlist); g_printf("%s\n", errstr); dbclose(); return 1; } if (am_has_feature(g_options->features, fe_req_xml)) { break; } continue; } if (dle && dle->program != NULL) { err_extra = _("multiple requests"); goto err; } dbprintf(_(" sendbackup req: <%s>\n"), line); dle = alloc_dle(); s = line; ch = *s++; skip_whitespace(s, ch); /* find the program name */ if(ch == '\0') { err_extra = _("no program name"); goto err; /* no program name */ } dle->program = s - 1; skip_non_whitespace(s, ch); s[-1] = '\0'; if (strcmp(dle->program, "APPLICATION")==0) { dle->program_is_application_api=1; skip_whitespace(s, ch); /* find dumper name */ if (ch == '\0') { goto err; /* no program */ } dle->program = s - 1; skip_non_whitespace(s, ch); s[-1] = '\0'; } dle->program = stralloc(dle->program); skip_whitespace(s, ch); /* find the disk name */ if(ch == '\0') { err_extra = _("no disk name"); goto err; /* no disk name */ } amfree(qdisk); qdisk = s - 1; ch = *qdisk; skip_quoted_string(s, ch); s[-1] = '\0'; qdisk = stralloc(qdisk); dle->disk = unquote_string(qdisk); skip_whitespace(s, ch); /* find the device or level */ if (ch == '\0') { err_extra = _("bad level"); goto err; } if(!isdigit((int)s[-1])) { amfree(qamdevice); qamdevice = s - 1; ch = *qamdevice; skip_quoted_string(s, ch); s[-1] = '\0'; qamdevice = stralloc(qamdevice); dle->device = unquote_string(qamdevice); skip_whitespace(s, ch); /* find level number */ } else { dle->device = stralloc(dle->disk); qamdevice = stralloc(qdisk); } /* find the level number */ if(ch == '\0' || sscanf(s - 1, "%d", &level) != 1) { err_extra = _("bad level"); goto err; /* bad level */ } skip_integer(s, ch); alevel = g_new0(level_t, 1); alevel->level = level; dle->levellist = g_slist_append(dle->levellist, alevel); skip_whitespace(s, ch); /* find the dump date */ if(ch == '\0') { err_extra = _("no dumpdate"); goto err; /* no dumpdate */ } amfree(dumpdate); dumpdate = s - 1; skip_non_whitespace(s, ch); s[-1] = '\0'; dumpdate = stralloc(dumpdate); skip_whitespace(s, ch); /* find the options keyword */ if(ch == '\0') { err_extra = _("no options"); goto err; /* no options */ } if(strncmp_const_skip(s - 1, "OPTIONS ", s, ch) != 0) { err_extra = _("no OPTIONS keyword"); goto err; /* no options */ } skip_whitespace(s, ch); /* find the options string */ if(ch == '\0') { err_extra = _("bad options string"); goto err; /* no options */ } amfree(stroptions); stroptions = stralloc(s - 1); } amfree(line); if (g_options == NULL) { g_printf(_("ERROR [Missing OPTIONS line in sendbackup input]\n")); error(_("Missing OPTIONS line in sendbackup input\n")); /*NOTREACHED*/ } if (am_has_feature(g_options->features, fe_req_xml)) { char *errmsg = NULL; dle = amxml_parse_node_FILE(stdin, &errmsg); if (errmsg) { err_extra = errmsg; goto err; } if (!dle) { err_extra = _("One DLE required"); goto err; } else if (dle->next) { err_extra = _("Only one DLE allowed"); goto err; } qdisk = quote_string(dle->disk); if (dle->device == NULL) dle->device = stralloc(dle->disk); qamdevice = quote_string(dle->device); dumpdate = stralloc("NODATE"); stroptions = stralloc(""); } else { parse_options(stroptions, dle, g_options->features, 0); } gdle = dle; if (dle->program == NULL || dle->disk == NULL || dle->device == NULL || dle->levellist == NULL || dumpdate == NULL) { err_extra = _("no valid sendbackup request"); goto err; } if (g_slist_length(dle->levellist) != 1) { err_extra = _("Too many level"); goto err; } alevel = (level_t *)dle->levellist->data; level = alevel->level; dbprintf(_(" Parsed request as: program `%s'\n"), dle->program); dbprintf(_(" disk `%s'\n"), qdisk); dbprintf(_(" device `%s'\n"), qamdevice); dbprintf(_(" level %d\n"), level); dbprintf(_(" since %s\n"), dumpdate); dbprintf(_(" options `%s'\n"), stroptions); if (dle->program_is_application_api==1) { /* check that the application_api exist */ } else { for(i = 0; programs[i]; i++) { if (strcmp(programs[i]->name, dle->program) == 0) { break; } } if (programs[i] == NULL) { dbprintf(_("ERROR [%s: unknown program %s]\n"), get_pname(), dle->program); error(_("ERROR [%s: unknown program %s]"), get_pname(), dle->program); /*NOTREACHED*/ } program = programs[i]; } if(!interactive) { datafd = DATA_FD_OFFSET + 0; mesgfd = DATA_FD_OFFSET + 2; indexfd = DATA_FD_OFFSET + 4; } if (!dle->create_index) indexfd = -1; if (dle->auth && amandad_auth) { if(strcasecmp(dle->auth, amandad_auth) != 0) { g_printf(_("ERROR [client configured for auth=%s while server requested '%s']\n"), amandad_auth, dle->auth); exit(-1); } } if (dle->kencrypt) { g_printf("KENCRYPT\n"); } g_printf(_("CONNECT DATA %d MESG %d INDEX %d\n"), DATA_FD_OFFSET, DATA_FD_OFFSET+1, indexfd == -1 ? -1 : DATA_FD_OFFSET+2); g_printf(_("OPTIONS ")); if(am_has_feature(g_options->features, fe_rep_options_features)) { g_printf("features=%s;", our_feature_string); } if(am_has_feature(g_options->features, fe_rep_options_hostname)) { g_printf("hostname=%s;", g_options->hostname); } g_printf("\n"); fflush(stdout); if (freopen("/dev/null", "w", stdout) == NULL) { dbprintf(_("Error redirecting stdout to /dev/null: %s\n"), strerror(errno)); exit(1); } if(interactive) { if((datafd = open("/dev/null", O_RDWR)) < 0) { error(_("ERROR [open of /dev/null for debug data stream: %s]\n"), strerror(errno)); /*NOTREACHED*/ } mesgfd = 2; indexfd = 1; } if(!interactive) { if(datafd == -1 || mesgfd == -1 || (dle->create_index && indexfd == -1)) { dbclose(); exit(1); } } mesgstream = fdopen(mesgfd,"w"); run_client_scripts(EXECUTE_ON_PRE_DLE_BACKUP, g_options, dle, mesgstream); fflush(mesgstream); if (dle->program_is_application_api==1) { guint j; char *cmd=NULL; GPtrArray *argv_ptr; char levelstr[20]; backup_support_option_t *bsu; char *compopt = NULL; char *encryptopt = skip_argument; int compout, dumpout; GSList *scriptlist; script_t *script; time_t cur_dumptime; int result; GPtrArray *errarray; int errfd[2]; FILE *dumperr; /* apply client-side encryption here */ if ( dle->encrypt == ENCRYPT_CUST ) { encpid = pipespawn(dle->clnt_encrypt, STDIN_PIPE, 0, &compout, &datafd, &mesgfd, dle->clnt_encrypt, encryptopt, NULL); dbprintf(_("encrypt: pid %ld: %s\n"), (long)encpid, dle->clnt_encrypt); } else { compout = datafd; encpid = -1; } /* now do the client-side compression */ if(dle->compress == COMP_FAST || dle->compress == COMP_BEST) { compopt = skip_argument; #if defined(COMPRESS_BEST_OPT) && defined(COMPRESS_FAST_OPT) if(dle->compress == COMP_BEST) { compopt = COMPRESS_BEST_OPT; } else { compopt = COMPRESS_FAST_OPT; } #endif comppid = pipespawn(COMPRESS_PATH, STDIN_PIPE, 0, &dumpout, &compout, &mesgfd, COMPRESS_PATH, compopt, NULL); dbprintf(_("gnutar: pid %ld: %s"), (long)comppid, COMPRESS_PATH); if(compopt != skip_argument) { dbprintf(_("pid %ld: %s %s\n"), (long)comppid, COMPRESS_PATH, compopt); } else { dbprintf(_("pid %ld: %s\n"), (long)comppid, COMPRESS_PATH); } } else if (dle->compress == COMP_CUST) { compopt = skip_argument; comppid = pipespawn(dle->compprog, STDIN_PIPE, 0, &dumpout, &compout, &mesgfd, dle->compprog, compopt, NULL); if(compopt != skip_argument) { dbprintf(_("pid %ld: %s %s\n"), (long)comppid, dle->compprog, compopt); } else { dbprintf(_("pid %ld: %s\n"), (long)comppid, dle->compprog); } } else { dumpout = compout; comppid = -1; } cur_dumptime = time(0); bsu = backup_support_option(dle->program, g_options, dle->disk, dle->device, &errarray); if (!bsu) { char *errmsg; char *qerrmsg; guint i; for (i=0; i < errarray->len; i++) { errmsg = g_ptr_array_index(errarray, i); qerrmsg = quote_string(errmsg); fdprintf(mesgfd, _("sendbackup: error [Application '%s': %s]\n"), dle->program, errmsg); dbprintf("aa: %s\n",qerrmsg); amfree(qerrmsg); } if (i == 0) { /* no errarray */ errmsg = vstrallocf(_("Can't execute application '%s'"), dle->program); qerrmsg = quote_string(errmsg); fdprintf(mesgfd, _("sendbackup: error [%s]\n"), errmsg); dbprintf(_("ERROR %s\n"), qerrmsg); amfree(qerrmsg); amfree(errmsg); } return 0; } if (pipe(errfd) < 0) { char *errmsg; char *qerrmsg; errmsg = vstrallocf(_("Application '%s': can't create pipe"), dle->program); qerrmsg = quote_string(errmsg); fdprintf(mesgfd, _("sendbackup: error [%s]\n"), errmsg); dbprintf(_("ERROR %s\n"), qerrmsg); amfree(qerrmsg); amfree(errmsg); return 0; } switch(application_api_pid=fork()) { case 0: argv_ptr = g_ptr_array_new(); cmd = vstralloc(APPLICATION_DIR, "/", dle->program, NULL); g_ptr_array_add(argv_ptr, stralloc(dle->program)); g_ptr_array_add(argv_ptr, stralloc("backup")); if (bsu->message_line == 1) { g_ptr_array_add(argv_ptr, stralloc("--message")); g_ptr_array_add(argv_ptr, stralloc("line")); } if (g_options->config && bsu->config == 1) { g_ptr_array_add(argv_ptr, stralloc("--config")); g_ptr_array_add(argv_ptr, stralloc(g_options->config)); } if (g_options->hostname && bsu->host == 1) { g_ptr_array_add(argv_ptr, stralloc("--host")); g_ptr_array_add(argv_ptr, stralloc(g_options->hostname)); } if (dle->disk && bsu->disk == 1) { g_ptr_array_add(argv_ptr, stralloc("--disk")); g_ptr_array_add(argv_ptr, stralloc(dle->disk)); } g_ptr_array_add(argv_ptr, stralloc("--device")); g_ptr_array_add(argv_ptr, stralloc(dle->device)); if (level <= bsu->max_level) { g_ptr_array_add(argv_ptr, stralloc("--level")); g_snprintf(levelstr,19,"%d",level); g_ptr_array_add(argv_ptr, stralloc(levelstr)); } if (indexfd != -1 && bsu->index_line == 1) { g_ptr_array_add(argv_ptr, stralloc("--index")); g_ptr_array_add(argv_ptr, stralloc("line")); } if (dle->record && bsu->record == 1) { g_ptr_array_add(argv_ptr, stralloc("--record")); } application_property_add_to_argv(argv_ptr, dle, bsu); for (scriptlist = dle->scriptlist; scriptlist != NULL; scriptlist = scriptlist->next) { script = (script_t *)scriptlist->data; if (script->result && script->result->proplist) { property_add_to_argv(argv_ptr, script->result->proplist); } } g_ptr_array_add(argv_ptr, NULL); dbprintf(_("%s: running \"%s\n"), get_pname(), cmd); for (j = 1; j < argv_ptr->len - 1; j++) dbprintf(" %s\n", (char *)g_ptr_array_index(argv_ptr,j)); dbprintf(_("\"\n")); if(dup2(dumpout, 1) == -1) { error(_("Can't dup2: %s"),strerror(errno)); /*NOTREACHED*/ } if (dup2(errfd[1], 2) == -1) { error(_("Can't dup2: %s"),strerror(errno)); /*NOTREACHED*/ } if(dup2(mesgfd, 3) == -1) { error(_("Can't dup2: %s"),strerror(errno)); /*NOTREACHED*/ } if(indexfd > 0) { if(dup2(indexfd, 4) == -1) { error(_("Can't dup2: %s"),strerror(errno)); /*NOTREACHED*/ } fcntl(indexfd, F_SETFD, 0); } application_api_info_tapeheader(mesgfd, dle->program, dle); if (indexfd != 0) { safe_fd(3, 2); } else { safe_fd(3, 1); } execve(cmd, (char **)argv_ptr->pdata, safe_env()); exit(1); break; default: break; case -1: error(_("%s: fork returned: %s"), get_pname(), strerror(errno)); } close(errfd[1]); dumperr = fdopen(errfd[0],"r"); if (!dumperr) { error(_("Can't fdopen: %s"), strerror(errno)); /*NOTREACHED*/ } result = 0; while ((line = agets(dumperr)) != NULL) { if (strlen(line) > 0) { fdprintf(mesgfd, "sendbackup: error [%s]\n", line); dbprintf("error: %s\n", line); result = 1; } amfree(line); } result |= check_result(mesgfd); if (result == 0) { char *amandates_file; amandates_file = getconf_str(CNF_AMANDATES); if(start_amandates(amandates_file, 1)) { amandates_updateone(dle->disk, level, cur_dumptime); finish_amandates(); free_amandates(); } else { if (GPOINTER_TO_INT(dle->estimatelist->data) == ES_CALCSIZE && bsu->calcsize) { error(_("error [opening %s for writing: %s]"), amandates_file, strerror(errno)); } else { g_debug(_("non-fatal error opening '%s' for writing: %s]"), amandates_file, strerror(errno)); } } } amfree(bsu); } else { if(!interactive) { /* redirect stderr */ if(dup2(mesgfd, 2) == -1) { dbprintf(_("Error redirecting stderr to fd %d: %s\n"), mesgfd, strerror(errno)); dbclose(); exit(1); } } if(pipe(mesgpipe) == -1) { s = strerror(errno); dbprintf(_("error [opening mesg pipe: %s]\n"), s); error(_("error [opening mesg pipe: %s]"), s); } program->start_backup(dle, g_options->hostname, datafd, mesgpipe[1], indexfd); dbprintf(_("Started backup\n")); parse_backup_messages(dle, mesgpipe[0]); dbprintf(_("Parsed backup messages\n")); } run_client_scripts(EXECUTE_ON_POST_DLE_BACKUP, g_options, dle, mesgstream); fflush(mesgstream); amfree(qdisk); amfree(qamdevice); amfree(dumpdate); amfree(stroptions); amfree(our_feature_string); am_release_feature_set(our_features); our_features = NULL; free_g_options(g_options); dbclose(); return 0; err: if (err_extra) { g_printf(_("ERROR FORMAT ERROR IN REQUEST PACKET '%s'\n"), err_extra); dbprintf(_("REQ packet is bogus: %s\n"), err_extra); } else { g_printf(_("ERROR FORMAT ERROR IN REQUEST PACKET\n")); dbprintf(_("REQ packet is bogus\n")); } amfree(qdisk); amfree(qamdevice); amfree(dumpdate); amfree(stroptions); amfree(our_feature_string); dbclose(); return 1; }
int main( int argc, char ** argv) { char *logfname; char *conf_logdir; FILE *logfile; config_overrides_t *cfg_ovr = NULL; char *cfg_opt = NULL; /* * 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); set_pname("amlogroll"); dbopen(DBG_SUBDIR_SERVER); add_amanda_log_handler(amanda_log_stderr); /* Process options */ cfg_ovr = extract_commandline_config_overrides(&argc, &argv); if (argc >= 2) { cfg_opt = argv[1]; } /* read configuration files */ set_config_overrides(cfg_ovr); config_init(CONFIG_INIT_EXPLICIT_NAME | CONFIG_INIT_USE_CWD, cfg_opt); if (config_errors(NULL) >= CFGERR_WARNINGS) { config_print_errors(); if (config_errors(NULL) >= CFGERR_ERRORS) { g_critical(_("errors processing config file")); } } safe_cd(); /* must happen after config_init */ check_running_as(RUNNING_AS_DUMPUSER); dbrename(get_config_name(), DBG_SUBDIR_SERVER); conf_logdir = config_dir_relative(getconf_str(CNF_LOGDIR)); logfname = vstralloc(conf_logdir, "/", "log", NULL); amfree(conf_logdir); if((logfile = fopen(logfname, "r")) == NULL) { error(_("could not open log %s: %s"), logfname, strerror(errno)); /*NOTREACHED*/ } amfree(logfname); add_amanda_log_handler(amanda_log_trace_log); while(get_logline(logfile)) { if(curlog == L_START) { handle_start(); if(datestamp != NULL) { break; } } } afclose(logfile); log_rename(datestamp); amfree(datestamp); dbclose(); return 0; }
void read_nh_config(void) { fnchar filename[BUFSZ]; get_config_name(filename, FALSE); read_config_file(filename); }
int main( int argc, char ** argv) { disklist_t diskl; int no_keep; /* files per system to keep */ char **output_find_log; DIR *dir; struct dirent *adir; char **name; int useful; char *olddir; char *oldfile = NULL, *newfile = NULL; time_t today, date_keep; char *logname = NULL; struct stat stat_log; struct stat stat_old; char *conf_diskfile; char *conf_tapelist; char *conf_logdir; int dumpcycle; config_overwrites_t *cfg_ovr = NULL; /* * 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("amtrmlog"); /* Don't die when child closes pipe */ signal(SIGPIPE, SIG_IGN); cfg_ovr = extract_commandline_config_overwrites(&argc, &argv); if (argc > 1 && strcmp(argv[1], "-t") == 0) { amtrmidx_debug = 1; argc--; argv++; } if (argc < 2) { g_fprintf(stderr, _("Usage: %s [-t] <config> [-o configoption]*\n"), argv[0]); return 1; } dbopen(DBG_SUBDIR_SERVER); dbprintf(_("%s: version %s\n"), argv[0], version()); config_init(CONFIG_INIT_EXPLICIT_NAME, argv[1]); apply_config_overwrites(cfg_ovr); 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); today = time((time_t *)NULL); dumpcycle = getconf_int(CNF_DUMPCYCLE); if(dumpcycle > 5000) dumpcycle = 5000; date_keep = today - (dumpcycle * 86400); output_find_log = find_log(); /* determine how many log to keep */ no_keep = getconf_int(CNF_TAPECYCLE) * 2; dbprintf(plural(_("Keeping %d log file\n"), _("Keeping %d log files\n"), no_keep), no_keep); conf_logdir = config_dir_relative(getconf_str(CNF_LOGDIR)); olddir = vstralloc(conf_logdir, "/oldlog", NULL); if (mkpdir(olddir, 0700, (uid_t)-1, (gid_t)-1) != 0) { error(_("could not create parents of %s: %s"), olddir, strerror(errno)); /*NOTREACHED*/ } if (mkdir(olddir, 0700) != 0 && errno != EEXIST) { error(_("could not create %s: %s"), olddir, strerror(errno)); /*NOTREACHED*/ } if (stat(olddir,&stat_old) == -1) { error(_("can't stat oldlog directory \"%s\": %s"), olddir, strerror(errno)); /*NOTREACHED*/ } if (!S_ISDIR(stat_old.st_mode)) { error(_("Oldlog directory \"%s\" is not a directory"), olddir); /*NOTREACHED*/ } if ((dir = opendir(conf_logdir)) == NULL) { error(_("could not open log directory \"%s\": %s"), conf_logdir,strerror(errno)); /*NOTREACHED*/ } while ((adir=readdir(dir)) != NULL) { if(strncmp(adir->d_name,"log.",4)==0) { useful=0; for (name=output_find_log;*name !=NULL; name++) { if((strlen(adir->d_name) >= 13 && strlen(*name) >= 13 && adir->d_name[12] == '.' && (*name)[12] == '.' && strncmp(adir->d_name,*name,12)==0) || strncmp(adir->d_name,*name,18)==0) { useful=1; break; } } logname=newvstralloc(logname, conf_logdir, "/" ,adir->d_name, NULL); if(stat(logname,&stat_log)==0) { if((time_t)stat_log.st_mtime > date_keep) { useful = 1; } } if(useful == 0) { oldfile = newvstralloc(oldfile, conf_logdir, "/", adir->d_name, NULL); newfile = newvstralloc(newfile, olddir, "/", adir->d_name, NULL); if (rename(oldfile,newfile) != 0) { error(_("could not rename \"%s\" to \"%s\": %s"), oldfile, newfile, strerror(errno)); /*NOTREACHED*/ } } } } closedir(dir); for (name = output_find_log; *name != NULL; name++) { amfree(*name); } amfree(output_find_log); amfree(logname); amfree(oldfile); amfree(newfile); amfree(olddir); amfree(conf_logdir); clear_tapelist(); free_disklist(&diskl); dbclose(); return 0; }
int main( int argc, char ** argv) { int c; char *command; application_argument_t argument; #ifdef STAR star_path = STAR; #else star_path = NULL; #endif star_tardumps = "/etc/tardumps"; star_dle_tardumps = 0; star_onefilesystem = 1; star_sparse = 1; star_directory = NULL; /* initialize */ /* * 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"); if (argc < 2) { printf("ERROR no command given to amstar\n"); error(_("No command given to amstar")); } /* drop root privileges */ if (!set_root_privs(0)) { if (strcmp(argv[1], "selfcheck") == 0) { printf("ERROR amstar must be run setuid root\n"); } error(_("amstar must be run setuid root")); } safe_fd(3, 2); set_pname("amstar"); /* Don't die when child closes pipe */ signal(SIGPIPE, SIG_IGN); #if defined(USE_DBMALLOC) malloc_size_1 = malloc_inuse(&malloc_hist_1); #endif add_amanda_log_handler(amanda_log_stderr); add_amanda_log_handler(amanda_log_syslog); dbopen(DBG_SUBDIR_CLIENT); startclock(); dbprintf(_("version %s\n"), VERSION); config_init(CONFIG_INIT_CLIENT, NULL); //check_running_as(RUNNING_AS_DUMPUSER_PREFERRED); //root for amrecover //RUNNING_AS_CLIENT_LOGIN from selfcheck, sendsize, sendbackup /* parse argument */ command = argv[1]; argument.config = NULL; argument.host = NULL; argument.message = 0; argument.collection = 0; argument.calcsize = 0; argument.level = NULL; argument.command_options = NULL; init_dle(&argument.dle); opterr = 0; while (1) { int option_index = 0; c = getopt_long (argc, argv, "", long_options, &option_index); if (c == -1) break; switch (c) { case 1: argument.config = stralloc(optarg); break; case 2: argument.host = stralloc(optarg); break; case 3: argument.dle.disk = stralloc(optarg); break; case 4: argument.dle.device = stralloc(optarg); break; case 5: argument.level = g_slist_append(argument.level, GINT_TO_POINTER(atoi(optarg))); break; case 6: argument.dle.create_index = 1; break; case 7: argument.message = 1; break; case 8: argument.collection = 1; break; case 9: argument.dle.record = 1; break; case 10: star_path = stralloc(optarg); break; case 11: star_tardumps = stralloc(optarg); break; case 12: if (optarg && strcasecmp(optarg, "NO") == 0) star_dle_tardumps = 0; else if (optarg && strcasecmp(optarg, "YES") == 0) star_dle_tardumps = 1; else if (strcasecmp(command, "selfcheck") == 0) printf(_("ERROR [%s: bad STAR-DLE-TARDUMP property value (%s)]\n"), get_pname(), optarg); break; case 13: if (optarg && strcasecmp(optarg, "YES") != 0) { /* This option is required to be YES */ /* star_onefilesystem = 0; */ } break; case 14: if (optarg && strcasecmp(optarg, "NO") == 0) star_sparse = 0; else if (optarg && strcasecmp(optarg, "YES") == 0) star_sparse = 1; else if (strcasecmp(command, "selfcheck") == 0) printf(_("ERROR [%s: bad SPARSE property value (%s)]\n"), get_pname(), optarg); break; case 15: argument.calcsize = 1; break; case 16: if (optarg) normal_message = g_slist_append(normal_message, optarg); break; case 17: if (optarg) ignore_message = g_slist_append(ignore_message, optarg); break; case 18: if (optarg) strange_message = g_slist_append(strange_message, optarg); break; case 19: if (optarg) argument.dle.include_list = append_sl(argument.dle.include_list, optarg); break; case 20: if (optarg) argument.dle.exclude_list = append_sl(argument.dle.exclude_list, optarg); break; case 21: if (optarg) star_directory = stralloc(optarg); break; case 22: argument.command_options = g_slist_append(argument.command_options, stralloc(optarg)); case 23: if (optarg) argument.dle.exclude_file = append_sl(argument.dle.exclude_file, optarg); break; case ':': case '?': break; } } if (!argument.dle.disk && argument.dle.device) argument.dle.disk = stralloc(argument.dle.device); if (!argument.dle.device && argument.dle.disk) argument.dle.device = stralloc(argument.dle.disk); argument.argc = argc - optind; argument.argv = argv + optind; if (argument.config) { /* overlay this configuration on the existing (nameless) configuration */ config_init(CONFIG_INIT_CLIENT | CONFIG_INIT_EXPLICIT_NAME | CONFIG_INIT_OVERLAY, argument.config); dbrename(get_config_name(), DBG_SUBDIR_CLIENT); } if (config_errors(NULL) >= CFGERR_ERRORS) { g_critical(_("errors processing config file")); } re_table = build_re_table(init_re_table, normal_message, ignore_message, strange_message); if (strcmp(command, "support") == 0) { amstar_support(&argument); } else if (strcmp(command, "selfcheck") == 0) { amstar_selfcheck(&argument); } else if (strcmp(command, "estimate") == 0) { amstar_estimate(&argument); } else if (strcmp(command, "backup") == 0) { amstar_backup(&argument); } else if (strcmp(command, "restore") == 0) { amstar_restore(&argument); } else if (strcmp(command, "validate") == 0) { amstar_validate(&argument); } else { fprintf(stderr, "Unknown command `%s'.\n", command); exit (1); } return 0; }
int main( int argc, char ** argv) { extern int optind; int opt; GSList *dumpspecs = NULL; int fd; tapelist_t *needed_tapes = NULL; char *e; rst_flags_t *rst_flags; int minimum_arguments; config_overrides_t *cfg_ovr = NULL; disklist_t diskq; char * conf_diskfile = NULL; am_feature_t *our_features = am_init_feature_set(); /* * 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"); set_pname("amfetchdump"); /* Don't die when child closes pipe */ signal(SIGPIPE, SIG_IGN); dbopen(DBG_SUBDIR_SERVER); add_amanda_log_handler(amanda_log_stderr); error_exit_status = 2; rst_flags = new_rst_flags(); rst_flags->wait_tape_prompt = 1; /* handle options */ cfg_ovr = new_config_overrides(argc/2); while( (opt = getopt_long(argc, argv, "alht:scCpb:nwi:d:O:o:", long_options, NULL)) != -1) { switch(opt) { case 0: switch (loptions) { case 1: rst_flags->headers = 1; if (strcmp(optarg, "-") == 0) rst_flags->header_to_fd = STDOUT_FILENO; else rst_flags->header_to_fd = atoi(optarg); if (fcntl(rst_flags->header_to_fd, F_GETFL, NULL) == -1) { error(_("fd %d: %s\n"), rst_flags->header_to_fd, strerror(errno)); } break; case 2: rst_flags->headers = 1; rst_flags->header_to_fd = open(optarg, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR); if (rst_flags->header_to_fd == -1) { error(_("Can't create '%s': %s\n"), optarg, strerror(errno)); } break; } break; case 'b': rst_flags->blocksize = (ssize_t)strtol(optarg, &e, 10); if(*e == 'k' || *e == 'K') { rst_flags->blocksize *= 1024; } else if(*e == 'm' || *e == 'M') { rst_flags->blocksize *= 1024 * 1024; } else if(*e != '\0') { error(_("invalid blocksize value \"%s\""), optarg); /*NOTREACHED*/ } if(rst_flags->blocksize < DISK_BLOCK_BYTES) { error(_("minimum block size is %dk"), DISK_BLOCK_BYTES / 1024); /*NOTREACHED*/ } break; case 'c': rst_flags->compress = 1; break; case 'O': rst_flags->restore_dir = stralloc(optarg) ; break; case 'd': rst_flags->alt_tapedev = stralloc(optarg) ; break; case 'C': rst_flags->compress = 1; rst_flags->comp_type = COMPRESS_BEST_OPT; break; case 'p': rst_flags->pipe_to_fd = STDOUT_FILENO; break; case 's': rst_flags->fsf = (off_t)0; break; case 'l': rst_flags->leave_comp = 1; break; case 'i': rst_flags->inventory_log = stralloc(optarg); break; case 'n': rst_flags->inline_assemble = 0; break; case 'w': rst_flags->delay_assemble = 1; break; case 'a': rst_flags->wait_tape_prompt = 0; break; case 'h': rst_flags->headers = 1; break; case 'o': add_config_override_opt(cfg_ovr, optarg); break; default: usage(); /*NOTREACHED*/ } } for(fd = 3; fd < (int)FD_SETSIZE; fd++) { if (fd != debug_fd() && fd != rst_flags->pipe_to_fd && fd != rst_flags->header_to_fd) { /* * Make sure nobody spoofs us with a lot of extra open files * that would cause a successful open to get a very high file * descriptor, which in turn might be used as an index into * an array (e.g. an fd_set). */ close(fd); } } /* Check some flags that affect inventorying */ if(rst_flags->inventory_log){ if(rst_flags->inline_assemble) rst_flags->delay_assemble = 1; rst_flags->inline_assemble = 0; rst_flags->leave_comp = 1; if(rst_flags->compress){ error(_("Cannot force compression when doing inventory/search")); /*NOTREACHED*/ } g_fprintf(stderr, _("Doing inventory/search, dumps will not be uncompressed or assembled on-the-fly.\n")); } else{ if(rst_flags->delay_assemble){ g_fprintf(stderr, _("Using -w, split dumpfiles will *not* be automatically uncompressed.\n")); } } /* make sure our options all make sense otherwise */ if(check_rst_flags(rst_flags) == -1) { usage(); /*NOTREACHED*/ } if (rst_flags->inventory_log) { minimum_arguments = 1; } else { minimum_arguments = 2; } if(argc - optind < minimum_arguments) { usage(); /*NOTREACHED*/ } config_init(CONFIG_INIT_EXPLICIT_NAME, argv[optind++]); apply_config_overrides(cfg_ovr); conf_diskfile = config_dir_relative(getconf_str(CNF_DISKFILE)); read_diskfile(conf_diskfile, &diskq); 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); dumpspecs = cmdline_parse_dumpspecs(argc - optind, argv + optind, CMDLINE_PARSE_DATESTAMP | CMDLINE_PARSE_LEVEL | CMDLINE_EMPTY_TO_WILDCARD); /* * We've been told explicitly to go and search through the tapes the hard * way. */ if(rst_flags->inventory_log){ g_fprintf(stderr, _("Beginning tape-by-tape search.\n")); search_tapes(stderr, stdin, rst_flags->alt_tapedev == NULL, NULL, dumpspecs, rst_flags, our_features); dbclose(); exit(0); } /* Decide what tapes we'll need */ needed_tapes = list_needed_tapes(dumpspecs, rst_flags->pipe_to_fd == STDOUT_FILENO, &diskq); parent_pid = getpid(); atexit(cleanup); get_lock = lock_logfile(); /* config is loaded, should be ok here */ if(get_lock == 0) { char *process_name = get_master_process(rst_conf_logfile); error(_("%s exists: %s is already running, or you must run amcleanup"), rst_conf_logfile, process_name); } log_add(L_INFO, "%s pid %ld", get_pname(), (long)getpid()); search_tapes(NULL, stdin, rst_flags->alt_tapedev == NULL, needed_tapes, dumpspecs, rst_flags, our_features); cleanup(); dumpspec_list_free(dumpspecs); if(rst_flags->inline_assemble || rst_flags->delay_assemble) flush_open_outputs(1, NULL); else flush_open_outputs(0, NULL); free_disklist(&diskq); free_rst_flags(rst_flags); dbclose(); return(0); }
void startup_tape_process( char *taper_program, int taper_parallel_write, gboolean no_taper) { int fd[2]; int i; char **config_options; taper_t *taper; /* always allocate the tapetable */ tapetable = calloc(sizeof(taper_t), taper_parallel_write+1); if (!tapetable) { error(_("could not g_malloc tapetable")); /*NOTREACHED*/ } for (taper = tapetable, i = 0; i < taper_parallel_write; taper++, i++) { taper->name = g_strdup_printf("worker%d", i); taper->sendresult = 0; taper->input_error = NULL; taper->tape_error = NULL; taper->result = 0; taper->dumper = NULL; taper->disk = NULL; taper->first_label = NULL; taper->first_fileno = 0; taper->state = TAPER_STATE_DEFAULT; taper->left = 0; taper->written = 0; /* jump right to degraded mode if there's no taper */ if (no_taper) { taper->tape_error = g_strdup("no taper started (--no-taper)"); taper->result = BOGUS; } } /* don't start the taper if we're not supposed to */ if (no_taper) return; if(socketpair(AF_UNIX, SOCK_STREAM, 0, fd) == -1) { error(_("taper pipe: %s"), strerror(errno)); /*NOTREACHED*/ } if(fd[0] < 0 || fd[0] >= (int)FD_SETSIZE) { error(_("taper socketpair 0: descriptor %d out of range (0 .. %d)\n"), fd[0], (int)FD_SETSIZE-1); /*NOTREACHED*/ } if(fd[1] < 0 || fd[1] >= (int)FD_SETSIZE) { error(_("taper socketpair 1: descriptor %d out of range (0 .. %d)\n"), fd[1], (int)FD_SETSIZE-1); /*NOTREACHED*/ } switch(taper_pid = fork()) { case -1: error(_("fork taper: %s"), strerror(errno)); /*NOTREACHED*/ case 0: /* child process */ aclose(fd[0]); if(dup2(fd[1], 0) == -1 || dup2(fd[1], 1) == -1) error(_("taper dup2: %s"), strerror(errno)); config_options = get_config_options(2); config_options[0] = "taper"; config_options[1] = get_config_name(); safe_fd(-1, 0); execve(taper_program, config_options, safe_env()); error("exec %s: %s", taper_program, strerror(errno)); /*NOTREACHED*/ default: /* parent process */ aclose(fd[1]); taper_fd = fd[0]; taper_ev_read = NULL; } }
int main(int argc, char **argv) { char *socketname = get_config_name("control"); /* check if japlay is already running */ if (socketname && file_exists(socketname)) { int fd = unix_socket_connect(socketname); if (fd >= 0) { int i; for (i = 1; i < argc; ++i) { char *path = absolute_path(argv[i]); if (path) { sendto(fd, path, strlen(path), 0, NULL, 0); free(path); } } close(fd); return 0; } /* remove leftover socket */ unlink(socketname); } g_thread_init(NULL); gdk_threads_init(); gdk_threads_enter(); gtk_init(&argc, &argv); main_thread = g_thread_self(); if (japlay_init(&argc, argv)) { error("Can not initialize japlay\n"); return -1; } main_window = gtk_window_new(GTK_WINDOW_TOPLEVEL); gtk_window_set_title(GTK_WINDOW(main_window), APP_NAME); g_signal_connect(G_OBJECT(main_window), "destroy", G_CALLBACK(destroy_cb), NULL); g_signal_connect(G_OBJECT(main_window), "key-press-event", G_CALLBACK(key_pressed_cb), NULL); GtkWidget *menubar = gtk_menu_bar_new(); GtkWidget *file_menu = gtk_menu_new(); GtkWidget *item = gtk_menu_item_new_with_label("New playlist"); g_signal_connect(G_OBJECT(item), "activate", G_CALLBACK(new_playlist_cb), NULL); gtk_menu_append(GTK_MENU(file_menu), item); item = gtk_menu_item_new_with_label("Add directory to the playlist..."); g_signal_connect(G_OBJECT(item), "activate", G_CALLBACK(add_dir_cb), NULL); gtk_menu_append(GTK_MENU(file_menu), item); item = gtk_menu_item_new_with_label("Clear playlist"); g_signal_connect(G_OBJECT(item), "activate", G_CALLBACK(clear_playlist_cb), NULL); gtk_menu_append(GTK_MENU(file_menu), item); item = gtk_separator_menu_item_new(); gtk_menu_append(GTK_MENU(file_menu), item); item = gtk_check_menu_item_new_with_label("Enable shuffle"); g_signal_connect(G_OBJECT(item), "toggled", G_CALLBACK(enable_shuffle_cb), NULL); gtk_menu_append(GTK_MENU(file_menu), item); item = gtk_check_menu_item_new_with_label("Enable automatic volume"); g_signal_connect(G_OBJECT(item), "toggled", G_CALLBACK(enable_autovol_cb), NULL); gtk_menu_append(GTK_MENU(file_menu), item); /*item = gtk_menu_item_new_with_label("Scan playlist"); g_signal_connect(G_OBJECT(item), "activate", G_CALLBACK(scan_playlist_cb), NULL); gtk_menu_append(GTK_MENU(file_menu), item);*/ item = gtk_separator_menu_item_new(); gtk_menu_append(GTK_MENU(file_menu), item); item = gtk_menu_item_new_with_label("Quit"); g_signal_connect(G_OBJECT(item), "activate", G_CALLBACK(destroy_cb), NULL); gtk_menu_append(GTK_MENU(file_menu), item); item = gtk_menu_item_new_with_label("File"); gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), file_menu); gtk_menu_bar_append(GTK_MENU_BAR(menubar), item); static const struct button { const char *stockid; const char *help; void (*const cb)(GtkButton *button, gpointer ptr); } buttons[] = { {GTK_STOCK_MEDIA_PLAY, "Play", play_cb}, {GTK_STOCK_MEDIA_STOP, "stop", stop_cb}, {GTK_STOCK_MEDIA_PAUSE, "Pause", pause_cb}, {GTK_STOCK_MEDIA_NEXT, "Skip to the next song in the queue", next_cb}, {GTK_STOCK_OPEN, "Add files to the playlist", add_cb}, {GTK_STOCK_OK, "Add selected songs to the queue", enqueue_cb}, {GTK_STOCK_DELETE, "Remove selected songs", remove_cb}, {NULL, NULL, NULL} }; GtkWidget *toolbar = gtk_hbox_new(false, 0); int i; for (i = 0; buttons[i].stockid; ++i) { GtkWidget *button = gtk_button_new(); GtkWidget *image = gtk_image_new_from_stock(buttons[i].stockid, GTK_ICON_SIZE_SMALL_TOOLBAR); gtk_button_set_image(GTK_BUTTON(button), image); g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(buttons[i].cb), NULL); gtk_box_pack_start(GTK_BOX(toolbar), button, false, true, 0); gtk_widget_set_tooltip_text(button, buttons[i].help); } scope_area = gtk_drawing_area_new(); gtk_widget_set_size_request(scope_area, SCOPE_WIDTH, -1); gtk_box_pack_start(GTK_BOX(toolbar), scope_area, false, true, 0); g_signal_connect(G_OBJECT(scope_area), "expose_event", G_CALLBACK(expose_event_cb), NULL); seekbar = gtk_hscale_new_with_range(0, 1, 1); gtk_range_set_update_policy(GTK_RANGE(seekbar), GTK_UPDATE_DELAYED); g_signal_connect(G_OBJECT(seekbar), "change-value", G_CALLBACK(seek_cb), NULL); g_signal_connect(G_OBJECT(seekbar), "format-value", G_CALLBACK(format_seek_cb), NULL); notebook = gtk_notebook_new(); GtkWidget *vbox = gtk_vbox_new(false, 0); gtk_box_pack_start(GTK_BOX(vbox), menubar, false, true, 0); gtk_box_pack_start(GTK_BOX(vbox), toolbar, false, true, 0); gtk_box_pack_start(GTK_BOX(vbox), seekbar, false, true, 0); gtk_box_pack_start(GTK_BOX(vbox), notebook, true, true, 0); gtk_container_add(GTK_CONTAINER(main_window), vbox); gtk_widget_show_all(main_window); /* TODO: load all playlists */ main_playlist = new_playlist(); add_playlist_page(main_playlist, "Main"); add_playlist_page(japlay_queue, "Play queue"); add_playlist_page(japlay_history, "History"); char *playlistpath = get_config_name("main_playlist.m3u"); if (playlistpath) load_playlist(main_playlist, playlistpath); char *queuepath = get_config_name("queue.m3u"); if (queuepath) load_playlist(japlay_queue, queuepath); char *settingspath = get_config_name("settings.cfg"); if (settingspath) load_settings(settingspath); for (i = 1; i < argc; ++i) add_file_playlist(main_playlist, argv[i]); signal(SIGINT, handle_sigint); int fd = -1; if (socketname) { fd = unix_socket_create(socketname); if (fd >= 0) { GIOChannel *io = g_io_channel_unix_new(fd); g_io_add_watch(io, G_IO_IN, incoming_client, NULL); } } gtk_main(); if (fd >= 0) unlink(socketname); if (playlistpath) save_playlist_m3u(main_playlist, playlistpath); if (queuepath) save_playlist_m3u(japlay_queue, queuepath); if (settingspath) save_settings(settingspath); japlay_exit(); return 0; }
// { int32 ver, string desc } = archiver.info() xmlrpc_value *get_info(xmlrpc_env *env, xmlrpc_value *args, void *user) { #ifdef LOGFILE LOG_MSG("archiver.info\n"); #endif int i; const char *config = get_config_name(env); if (!config) return 0; #ifdef LOGFILE LOG_MSG("config: '%s'\n", config); #endif char description[500]; sprintf(description, "Channel Archiver Data Server V%d,\n" "for " EPICS_VERSION_STRING ",\n" "built " __DATE__ ", " __TIME__ "\n" "from sources for version " ARCH_VERSION_TXT "\n" "Config: '%s'\n", ARCH_VER, config); AutoXmlRpcValue how(xmlrpc_build_value(env, "(sssss)", "raw", "spreadsheet", "average", "plot-binning", "linear")); if (env->fault_occurred) return 0; // 'status': array of all status string. AutoXmlRpcValue element; AutoXmlRpcValue status(xmlrpc_build_value(env, "()")); if (env->fault_occurred) return 0; for (i=0; i<=lastEpicsAlarmCond; ++i) { element = xmlrpc_build_value(env, "s", epicsAlarmConditionStrings[i]); xmlrpc_array_append_item(env, status, element); if (env->fault_occurred) return 0; } // 'severity': array of all severity strings. AutoXmlRpcValue severity(xmlrpc_build_value(env, "()")); for (i=0; i<=lastEpicsAlarmSev; ++i) { element = xmlrpc_build_value(env, "{s:i,s:s,s:b,s:b}", "num", (xmlrpc_int32)i, "sevr", epicsAlarmSeverityStrings[i], "has_value", (xmlrpc_bool) 1, "txt_stat", (xmlrpc_bool) 1); xmlrpc_array_append_item(env, severity, element); if (env->fault_occurred) return 0; } // ... including the archive-specific ones. element = xmlrpc_build_value(env, "{s:i,s:s,s:b,s:b}", "num", (xmlrpc_int32)ARCH_EST_REPEAT, "sevr", "Est_Repeat", "has_value", (xmlrpc_bool) 1, "txt_stat", (xmlrpc_bool) 0); xmlrpc_array_append_item(env, severity, element); element = xmlrpc_build_value(env, "{s:i,s:s,s:b,s:b}", "num", (xmlrpc_int32)ARCH_REPEAT, "sevr", "Repeat", "has_value", (xmlrpc_bool) 1, "txt_stat", (xmlrpc_bool) 0); xmlrpc_array_append_item(env, severity, element); element = xmlrpc_build_value(env, "{s:i,s:s,s:b,s:b}", "num", (xmlrpc_int32)ARCH_DISCONNECT, "sevr", "Disconnected", "has_value", (xmlrpc_bool) 0, "txt_stat", (xmlrpc_bool) 1); xmlrpc_array_append_item(env, severity, element); element = xmlrpc_build_value(env, "{s:i,s:s,s:b,s:b}", "num", (xmlrpc_int32)ARCH_STOPPED, "sevr", "Archive_Off", "has_value", (xmlrpc_bool) 0, "txt_stat", (xmlrpc_bool) 1); xmlrpc_array_append_item(env, severity, element); element = xmlrpc_build_value(env, "{s:i,s:s,s:b,s:b}", "num", (xmlrpc_int32)ARCH_DISABLED, "sevr", "Archive_Disabled", "has_value", (xmlrpc_bool) 0, "txt_stat", (xmlrpc_bool) 1); xmlrpc_array_append_item(env, severity, element); // Overall result. return xmlrpc_build_value(env, "{s:i,s:s,s:V,s:V,s:V}", "ver", ARCH_VER, "desc", description, "how", how.get(), "stat", status.get(), "sevr", severity.get()); }
int main( int argc, char ** argv) { config_overrides_t *cfg_ovr; char *hostname; char *auth; char *service; char *config = NULL; int opt; extern int optind; extern char *optarg; FILE *input_file; int use_connect = 0; int got_input_file = 0; int i; unsigned char gfd[32768]; glib_init(); /* * 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("amservice"); /* drop root privileges */ if (!set_root_privs(0)) { error(_("amservice must be run setuid root")); } /* Don't die when child closes pipe */ signal(SIGPIPE, SIG_IGN); dbopen(DBG_SUBDIR_SERVER); add_amanda_log_handler(amanda_log_stderr); our_features = am_init_feature_set(); our_feature_string = am_feature_to_string(our_features); /* process arguments */ for (i=0; i<argc; i++) { g_debug("argv[%d] = %s", i, argv[i]); } for (i = 0;i < 32768; i++) { gfd[i] = 0; } cfg_ovr = new_config_overrides(argc/2); input_file = stdin; while((opt = getopt_long(argc, argv, "o:f:s", long_options, NULL)) != EOF) { switch(opt) { case 1: printf("amservice-%s\n", VERSION); return(0); break; case 2: g_free(our_feature_string); g_free(our_features); our_feature_string = g_strdup(optarg); our_features = am_string_to_feature(our_feature_string); break; case 3: { gchar *copy_optarg = g_strdup(optarg); gchar *coma = strchr(copy_optarg, ','); gchar *stream_in; if (nb_lstream == DATA_FD_COUNT) { g_critical("Too many --stream, maximum is %d", DATA_FD_COUNT); exit(1); } else if (coma) { *coma++ = '\0'; stream_in = coma; coma = strchr(coma, ','); if (coma) { *coma++ = '\0'; lstreams[nb_lstream].name = g_strdup(copy_optarg); lstreams[nb_lstream].fd_in = atoi(stream_in); lstreams[nb_lstream].fd_out = atoi(coma); gfd[lstreams[nb_lstream].fd_in] = 1; gfd[lstreams[nb_lstream].fd_out] = 1; nb_lstream++; } } if (!coma) { g_critical("Invalid --stream option (%s)", optarg); exit(1); } g_free(copy_optarg); break; } case 4: g_free(config); config = g_strdup(optarg); break; case 'o': add_config_override_opt(cfg_ovr, optarg); break; case 'f': if (got_input_file == 1) { g_critical("Invalid two -f argument"); exit(1); } got_input_file = 1; if (*optarg == '/') { input_file = fopen(optarg, "r"); } else { char *name = g_strjoin(NULL, get_original_cwd(), "/", optarg, NULL); input_file = fopen(name, "r"); amfree(name); } if (!input_file) { g_critical("Cannot open input file '%s': %s", optarg, strerror(errno)); exit(1); } break; case 's': use_connect = 1; break; } } if (use_connect && !got_input_file) { g_critical("The -s option require -f"); exit(1); } /* close all unused fd */ for (i = 3;i < 32768; i++) { if (gfd[i] == 0 && i != dbfd() && (!got_input_file || i != fileno(input_file))) { close(i); } } argc -= optind, argv += optind; if(argc < 3) usage(); /* set a default config */ set_config_overrides(cfg_ovr); config_init(CONFIG_INIT_CLIENT|CONFIG_INIT_GLOBAL, NULL); if (config) { config_init(CONFIG_INIT_CLIENT | CONFIG_INIT_EXPLICIT_NAME | CONFIG_INIT_OVERLAY, config); } dbrename(get_config_name(), DBG_SUBDIR_SERVER); if (config_errors(NULL) >= CFGERR_WARNINGS) { config_print_errors(); if (config_errors(NULL) >= CFGERR_ERRORS) { g_critical(_("errors processing config file")); } } conf_ctimeout = (time_t)getconf_int(CNF_CTIMEOUT); hostname = argv[0]; auth = argv[1]; if (g_str_equal(auth,"NULL")) { auth = getconf_str(CNF_AUTH); } service = argv[2]; /* start client side checks */ copy_stream = use_connect && got_input_file; client_protocol(hostname, auth, service, config, input_file); amfree(our_feature_string); am_release_feature_set(our_features); our_features = NULL; if (got_input_file) fclose(input_file); dbclose(); return(remote_errors != 0); }