+ g_snprintf(number, SIZEOF(number), "%d", level);
+ g_ptr_array_add(argv_ptr, stralloc(number));
+ g_snprintf(number, SIZEOF(number), "%d", dumpsince);
+ g_ptr_array_add(argv_ptr, stralloc(number));
+ }
+
+ g_ptr_array_add(argv_ptr, NULL);
+ command = (char *)g_ptr_array_index(argv_ptr, 0);
+ cmdline = stralloc(command);
+ for(i = 1; i < argv_ptr->len - 1; i++)
+ cmdline = vstrextend(&cmdline, " ",
+ (char *)g_ptr_array_index(argv_ptr,i), NULL);
+ dbprintf(_("running: \"%s\"\n"), cmdline);
+ amfree(cmdline);
+
+ start_time = curclock();
+
+ fflush(stderr); fflush(stdout);
+
+ if ((nullfd = open("/dev/null", O_RDWR)) == -1) {
+ errmsg = vstrallocf(_("Cannot access /dev/null : %s"),
+ strerror(errno));
+ dbprintf("%s\n", errmsg);
+ goto common_exit;
+ }
+
+ calcpid = pipespawnv(cmd, STDERR_PIPE, 0,
+ &nullfd, &nullfd, &pipefd, (char **)argv_ptr->pdata);
+ amfree(cmd);
+
+ dumpout = fdopen(pipefd,"r");
+ if (!dumpout) {
+ error(_("Can't fdopen: %s"), strerror(errno));
+ /*NOTREACHED*/
+ }
+
+ match_expr = vstralloc(" %d SIZE %lld", NULL);
+ len = strlen(qdisk);
+ for(size = (off_t)-1; (line = agets(dumpout)) != NULL; free(line)) {
+ long long size_ = (long long)0;
+ if (line[0] == '\0' || (int)strlen(line) <= len)
+ continue;
+ /* Don't use sscanf for qdisk because it can have a '%'. */
+ if (strncmp(line, qdisk, len) == 0 &&
+ sscanf(line+len, match_expr, &level, &size_) == 2) {
+ g_printf("%d %lld %d\n", level, size_, 1); /* write to sendsize */
+ dbprintf(_("estimate size for %s level %d: %lld KB\n"),
+ qdisk, level, size_);
+ }
+ size = (off_t)size_;
+ }
+ amfree(match_expr);
+
+ dbprintf(_("waiting for %s %s child (pid=%d)\n"),
+ command, qdisk, (int)calcpid);
+ waitpid(calcpid, &wait_status, 0);
+ if (WIFSIGNALED(wait_status)) {
+ errmsg = vstrallocf(_("%s terminated with signal %d: see %s"),
+ "calcsize", WTERMSIG(wait_status),
+ dbfn());
+ } else if (WIFEXITED(wait_status)) {
+ if (WEXITSTATUS(wait_status) != 0) {
+ errmsg = vstrallocf(_("%s exited with status %d: see %s"),
+ "calcsize", WEXITSTATUS(wait_status),
+ dbfn());
+ } else {
+ /* Normal exit */
+ }
+ } else {
+ errmsg = vstrallocf(_("%s got bad exit: see %s"),
+ "calcsize", dbfn());
+ }
+
+ dbprintf(_("after %s %s wait: child pid=%d status=%d\n"),
+ command, qdisk,
+ (int)calcpid, WEXITSTATUS(wait_status));
+
+ dbprintf(_(".....\n"));
+ dbprintf(_("estimate time for %s: %s\n"),
+ qdisk,
+ walltime_str(timessub(curclock(), start_time)));
+
+common_exit:
+ if (errmsg && errmsg[0] != '\0') {
+ char *qerrmsg = quote_string(errmsg);
+ dbprintf(_("errmsg is %s\n"), errmsg);
+ g_printf("ERROR %s\n", qerrmsg);
+ amfree(qerrmsg);
+ }
+ amfree(qdisk);
+ amfree(errmsg);
+ g_ptr_array_free_full(argv_ptr);
+ amfree(cmd);
+
+}
+
+
+void
+check_access(
+ char * filename,
+ int mode)
+{
+ char *noun, *adjective;
+ char *quoted = quote_string(filename);
+
+ if(mode == F_OK)
+ noun = "find", adjective = "exists";
+ else if((mode & X_OK) == X_OK)
+ noun = "execute", adjective = "executable";
+ else if((mode & (W_OK|R_OK)) == (W_OK|R_OK))
+ noun = "read/write", adjective = "read/writable";
+ else
+ noun = "access", adjective = "accessible";
+
+ if(access(filename, mode) == -1)
+ g_printf(_("ERROR [can not %s %s: %s]\n"), noun, quoted, strerror(errno));
+ else
+ g_printf(_("OK %s %s\n"), quoted, adjective);
+ amfree(quoted);
+}
+
+void
+check_file(
+ char * filename,
+ int mode)
+{
+ struct stat stat_buf;
+ char *quoted;
+
+ if(!stat(filename, &stat_buf)) {
+ if(!S_ISREG(stat_buf.st_mode)) {
+ quoted = quote_string(filename);
+ g_printf(_("ERROR [%s is not a file]\n"), quoted);
+ amfree(quoted);
+ }
+ } else {
+ int save_errno = errno;
+ quoted = quote_string(filename);
+ g_printf(_("ERROR [can not stat %s: %s]\n"), quoted,
+ strerror(save_errno));
+ amfree(quoted);
+ }
+ if (getuid() == geteuid()) {
+ check_access(filename, mode);
+ }
+}
+
+void
+check_dir(
+ char * dirname,
+ int mode)
+{
+ struct stat stat_buf;
+ char *quoted;
+ char *dir;
+
+ if(!stat(dirname, &stat_buf)) {
+ if(!S_ISDIR(stat_buf.st_mode)) {
+ quoted = quote_string(dirname);
+ g_printf(_("ERROR [%s is not a directory]\n"), quoted);
+ amfree(quoted);
+ }
+ } else {
+ int save_errno = errno;
+ quoted = quote_string(dirname);
+ g_printf(_("ERROR [can not stat %s: %s]\n"), quoted,
+ strerror(save_errno));
+ amfree(quoted);
+ }
+ if (getuid() == geteuid()) {
+ dir = stralloc2(dirname, "/.");
+ check_access(dir, mode);
+ amfree(dir);
+ }
+}
+
+void
+check_suid(
+ char * filename)
+{
+#ifndef SINGLE_USERID
+ struct stat stat_buf;
+ char *quoted = quote_string(filename);
+
+ if(!stat(filename, &stat_buf)) {
+ if(stat_buf.st_uid != 0 ) {
+ g_printf(_("ERROR [%s is not owned by root]\n"), quoted);
+ }
+ if((stat_buf.st_mode & S_ISUID) != S_ISUID) {
+ g_printf(_("ERROR [%s is not SUID root]\n"), quoted);
+ }
+ }
+ else {
+ g_printf(_("ERROR [can not stat %s: %s]\n"), quoted, strerror(errno));
+ }
+ amfree(quoted);
+#else
+ (void)filename; /* Quiet unused parameter warning */
+#endif
+}
+
+/*
+ * Returns the value of the first integer in a string.
+ */
+
+double
+the_num(
+ char * str,
+ int pos)
+{
+ char *num;
+ int ch;
+ double d;
+
+ do {
+ ch = *str++;
+ while(ch && !isdigit(ch)) ch = *str++;
+ if (pos == 1) break;
+ pos--;
+ while(ch && (isdigit(ch) || ch == '.')) ch = *str++;
+ } while (ch);
+ num = str - 1;
+ while(isdigit(ch) || ch == '.') ch = *str++;
+ str[-1] = '\0';
+ d = atof(num);
+ str[-1] = (char)ch;
+ return d;
+}
+
+
+char *
+config_errors_to_error_string(
+ GSList *errlist)
+{
+ char *errmsg;
+ gboolean multiple_errors = FALSE;
+
+ if (errlist) {
+ errmsg = (char *)errlist->data;
+ if (errlist->next)
+ multiple_errors = TRUE;
+ } else {
+ errmsg = _("(no error message)");
+ }
+
+ return vstrallocf("ERROR %s%s", errmsg,
+ multiple_errors? _(" (additional errors not displayed)"):"");
+}
+
+
+void
+add_type_table(
+ dmpline_t typ,
+ amregex_t **re_table,
+ amregex_t *orig_re_table,
+ GSList *normal_message,
+ GSList *ignore_message,
+ GSList *strange_message)
+{
+ amregex_t *rp;
+
+ for(rp = orig_re_table; rp->regex != NULL; rp++) {
+ if (rp->typ == typ) {
+ int found = 0;
+ GSList *mes;
+
+ for (mes = normal_message; mes != NULL; mes = mes->next) {
+ if (strcmp(rp->regex, (char *)mes->data) == 0)
+ found = 1;