Imported Upstream version 2.5.2p1
[debian/amanda] / client-src / selfcheck.c
index 2230bc5a54cfc3179aeab263e7c851289834ec45..fe6179c4ddf2ba6dfb96d43bc502cc473f4e6992 100644 (file)
@@ -25,7 +25,7 @@
  *                        University of Maryland at College Park
  */
 /* 
- * $Id: selfcheck.c,v 1.95.2.2 2007/01/19 01:03:34 martinea Exp $
+ * $Id: selfcheck.c,v 1.95 2006/08/29 11:21:00 martinea Exp $
  *
  * do self-check and send back any error messages
  */
 #include "pipespawn.h"
 #include "amfeatures.h"
 #include "client_util.h"
-#include "clientconf.h"
+#include "conffile.h"
 #include "amandad.h"
 
 #ifdef SAMBA_CLIENT
 #include "findpass.h"
 #endif
 
+#define selfcheck_debug(i,x) do {      \
+       if ((i) <= debug_selfcheck) {   \
+           dbprintf(x);                \
+       }                               \
+} while (0)
+
 int need_samba=0;
 int need_rundump=0;
 int need_dump=0;
@@ -61,7 +67,7 @@ int need_runtar=0;
 int need_gnutar=0;
 int need_compress_path=0;
 int need_calcsize=0;
-int program_is_wrapper=0;
+int program_is_backup_api=0;
 
 static char *amandad_auth = NULL;
 static am_feature_t *our_features = NULL;
@@ -72,7 +78,7 @@ static g_option_t *g_options = NULL;
 int main(int argc, char **argv);
 
 static void check_options(char *program, char *calcprog, char *disk, char *amdevice, option_t *options);
-static void check_disk(char *program, char *calcprog, char *disk, char *amdevice, int level, char *optstr);
+static void check_disk(char *program, char *calcprog, char *disk, char *amdevice, int level, option_t *options);
 static void check_overall(void);
 static void check_access(char *filename, int mode);
 static int check_file_exist(char *filename);
@@ -147,9 +153,7 @@ main(
        if (line[0] == '\0')
            continue;
 
-#define sc "OPTIONS "
-       if(strncmp(line, sc, SIZEOF(sc)-1) == 0) {
-#undef sc
+       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);
@@ -194,9 +198,9 @@ main(
        skip_non_whitespace(s, ch);
        s[-1] = '\0';                           /* terminate the program name */
 
-       program_is_wrapper = 0;
-       if(strcmp(program,"DUMPER")==0) {
-           program_is_wrapper = 1;
+       program_is_backup_api = 0;
+       if(strcmp(program,"BACKUP")==0) {
+           program_is_backup_api = 1;
            skip_whitespace(s, ch);             /* find dumper name */
            if (ch == '\0') {
                goto err;                       /* no program */
@@ -206,7 +210,7 @@ main(
            s[-1] = '\0';                       /* terminate the program name */
        }
 
-       if(strncmp(program, "CALCSIZE", 8) == 0) {
+       if(strncmp_const(program, "CALCSIZE") == 0) {
            skip_whitespace(s, ch);             /* find program name */
            if (ch == '\0') {
                goto err;                       /* no program */
@@ -251,11 +255,7 @@ main(
        skip_integer(s, ch);
 
        skip_whitespace(s, ch);
-#define sc "OPTIONS "
-       if (ch && strncmp (s - 1, sc, SIZEOF(sc)-1) == 0) {
-           s += SIZEOF(sc)-1;
-           ch = s[-1];
-#undef sc
+       if (ch && strncmp_const_skip(s - 1, "OPTIONS ", s, ch) == 0) {
            skip_whitespace(s, ch);             /* find the option string */
            if(ch == '\0') {
                goto err;                       /* bad options string */
@@ -266,7 +266,7 @@ main(
            options = parse_options(optstr, disk, amdevice, g_options->features, 1);
            /*@ignore@*/
            check_options(program, calcprog, disk, amdevice, options);
-           check_disk(program, calcprog, disk, amdevice, level, &optstr[2]);
+           check_disk(program, calcprog, disk, amdevice, level, options);
            /*@end@*/
            free_sl(options->exclude_file);
            free_sl(options->exclude_list);
@@ -292,7 +292,7 @@ main(
            need_compress_path=1;
            need_calcsize=1;
            /*@ignore@*/
-           check_disk(program, calcprog, disk, amdevice, level, "");
+           check_disk(program, calcprog, disk, amdevice, level, NULL);
            /*@end@*/
        } else {
            goto err;                           /* bad syntax */
@@ -482,8 +482,8 @@ check_options(
            need_restore=1;
 #endif
     }
-    if ((options->compress == COMPR_BEST) || (options->compress == COMPR_FAST) 
-               || (options->compress == COMPR_CUST)) {
+    if ((options->compress == COMP_BEST) || (options->compress == COMP_FAST) 
+               || (options->compress == COMP_CUST)) {
        need_compress_path=1;
     }
     if(options->auth && amandad_auth) {
@@ -501,7 +501,7 @@ check_disk(
     char *     disk,
     char *     amdevice,
     int                level,
-    char *     optstr)
+    option_t    *options)
 {
     char *device = stralloc("nodevice");
     char *err = NULL;
@@ -517,6 +517,8 @@ check_disk(
     char *qdisk = quote_string(disk);
     char *qamdevice = quote_string(amdevice);
     char *qdevice = NULL;
+    FILE *toolin;
+    char number[NUM_STR_SIZE];
 
     (void)level;       /* Quiet unused parameter warning */
 
@@ -712,35 +714,95 @@ check_disk(
 #endif
        }
     }
-    else { /* program_is_wrapper==1 */
-       pid_t pid_wrapper;
-       fflush(stdout);fflush(stdin);
-       switch (pid_wrapper = fork()) {
+    else { /* program_is_backup_api==1 */
+       pid_t  backup_api_pid;
+       int    property_pipe[2];
+       backup_support_option_t *bsu;
+
+       bsu = backup_support_option(program, g_options, disk, amdevice);
+
+       if (pipe(property_pipe) < 0) {
+           err = vstralloc("pipe failed: ", strerror(errno), NULL);
+           goto common_exit;
+       }
+       fflush(stdout);fflush(stderr);
+       
+       switch (backup_api_pid = fork()) {
        case -1:
-           printf("ERROR [fork: %s]\n", strerror(errno));
-           error("fork: %s", strerror(errno));
-           /*NOTREACHED*/
+           err = vstralloc("fork failed: ", strerror(errno), NULL);
+           goto common_exit;
 
        case 0: /* child */
            {
-               char *argvchild[6];
+               char *argvchild[14];
                char *cmd = vstralloc(DUMPER_DIR, "/", program, NULL);
-               argvchild[0] = program;
-               argvchild[1] = "selfcheck";
-               argvchild[2] = disk;
-               argvchild[3] = amdevice;
-               argvchild[4] = optstr;
-               argvchild[5] = NULL;
+               int j=0;
+               argvchild[j++] = program;
+               argvchild[j++] = "selfcheck";
+               if (bsu->message_line == 1) {
+                   argvchild[j++] = "--message";
+                   argvchild[j++] = "line";
+               }
+               if (g_options->config != NULL && bsu->config == 1) {
+                   argvchild[j++] = "--config";
+                   argvchild[j++] = g_options->config;
+               }
+               if (g_options->hostname != NULL && bsu->host == 1) {
+                   argvchild[j++] = "--host";
+                   argvchild[j++] = g_options->hostname;
+               }
+               if (disk != NULL && bsu->disk == 1) {
+                   argvchild[j++] = "--disk";
+                   argvchild[j++] = disk;
+               }
+               argvchild[j++] = "--device";
+               argvchild[j++] = amdevice;
+               if(options && options->createindex && bsu->index_line == 1) {
+                   argvchild[j++] = "--index";
+                   argvchild[j++] = "line";
+               }
+               if (!options->no_record && bsu->record == 1) {
+                   argvchild[j++] = "--record";
+               }
+               argvchild[j++] = NULL;
+               dup2(property_pipe[0], 0);
+               aclose(property_pipe[1]);
                execve(cmd,argvchild,safe_env());
+               printf("ERROR [Can't execute %s: %s]\n", cmd, strerror(errno));
                exit(127);
            }
        default: /* parent */
            {
                int status;
-               waitpid(pid_wrapper, &status, 0);
+               aclose(property_pipe[0]);
+               toolin = fdopen(property_pipe[1],"w");
+               if (!toolin) {
+                   err = vstralloc("Can't fdopen: ", strerror(errno), NULL);
+                   goto common_exit;
+               }
+               output_tool_property(toolin, options);
+               fflush(toolin);
+               fclose(toolin);
+               if (waitpid(backup_api_pid, &status, 0) < 0) {
+                   if (!WIFEXITED(status)) {
+                       snprintf(number, SIZEOF(number), "%d",
+                                (int)WTERMSIG(status));
+                       err = vstralloc("Tool exited with signal ", number,
+                                       NULL);
+                   } else if (WEXITSTATUS(status) != 0) {
+                       snprintf(number, SIZEOF(number), "%d",
+                                (int)WEXITSTATUS(status));
+                       err = vstralloc("Tool exited with status ", number,
+                                       NULL);
+                   } else {
+                       err = stralloc("waitpid returned negative value");
+                   }
+                   goto common_exit;
+               }
            }
        }
-       fflush(stdout);fflush(stdin);
+       amfree(bsu);
+       fflush(stdout);fflush(stderr);
        amfree(device);
        amfree(qamdevice);
        amfree(qdisk);
@@ -751,7 +813,7 @@ check_disk(
     dbprintf(("%s: device %s\n", debug_prefix_time(NULL), qdevice));
 
     /* skip accessability test if this is an AFS entry */
-    if(strncmp(device, "afs:", 4) != 0) {
+    if(strncmp_const(device, "afs:") != 0) {
 #ifdef CHECK_FOR_ACCESS_WITH_OPEN
        access_result = open(device, O_RDONLY);
        access_type = "open";
@@ -899,7 +961,7 @@ check_overall(void)
        printf("ERROR [GNUTAR program not available]\n");
 #endif
        need_amandates = 1;
-       gnutar_list_dir = client_getconf_str(CLN_GNUTAR_LIST_DIR);
+       gnutar_list_dir = getconf_str(CNF_GNUTAR_LIST_DIR);
        if (strlen(gnutar_list_dir) == 0)
            gnutar_list_dir = NULL;
        if (gnutar_list_dir) 
@@ -908,7 +970,7 @@ check_overall(void)
 
     if (need_amandates) {
        char *amandates_file;
-       amandates_file = client_getconf_str(CLN_AMANDATES);
+       amandates_file = getconf_str(CNF_AMANDATES);
        check_file(amandates_file, R_OK|W_OK);
     }
     if( need_calcsize ) {