Prechádzať zdrojové kódy

* Detect corrupted binaries during upgrade

Bryan Drewery 16 rokov pred
rodič
commit
829784c1d0
3 zmenil súbory, kde vykonal 21 pridanie a 9 odobranie
  1. 12 5
      src/binary.c
  2. 1 1
      src/binary.h
  3. 8 3
      src/misc.c

+ 12 - 5
src/binary.c

@@ -554,14 +554,21 @@ check_sum(const char *fname, const char *cfgfile, bool read_stdin)
   }
 }
 
-bool check_bin_initialized(const char *fname)
+/*
+ * @returns 0 = initialized, 1 = not initialized, 2 = corrupted
+ */
+int check_bin_initialized(const char *fname)
 {
   const char* argv[] = {fname, "-p", 0};
   int i = simple_exec(argv);
-  if (i != -1 && WEXITSTATUS(i) == 4)
-    return 1;
+  if (i != -1) {
+    if (WEXITSTATUS(i) == 4)
+      return 0;
+    else if (WEXITSTATUS(i) == 5)
+      return 1;
+  }
 
-  return 0;
+  return 2;
 }
 
 bool check_bin_compat(const char *fname)
@@ -594,7 +601,7 @@ void write_settings(const char *fname, int die, bool doconf, int initialized)
   int bits = WRITE_CHECKSUM;
   /* see if the binary is already initialized or not */
   if (initialized == -1)
-    initialized = check_bin_initialized(fname);
+    initialized = check_bin_initialized(fname) ? 0 : 1;
 
   /* only write pack data if the binary is uninitialized
    * otherwise, assume it has similar/correct/updated pack data

+ 1 - 1
src/binary.h

@@ -13,7 +13,7 @@ extern int checked_bin_buf;
 
 void check_sum(const char *, const char *, bool);
 void write_settings(const char *, int, bool, int initialized = -1);
-bool check_bin_initialized(const char *fname);
+int check_bin_initialized(const char *fname);
 bool check_bin_compat(const char *fname);
 void conf_to_bin(conf_t *, bool, int);
 void reload_bin_data();

+ 8 - 3
src/misc.c

@@ -930,8 +930,13 @@ int updatebin(int idx, char *par, int secs)
   }
 
   /* Check if the new binary is compatible */
-  int initialized = (int)check_bin_initialized(path);
-  if (!initialized && !check_bin_compat(path)) {
+  int initialized_code = check_bin_initialized(path);
+  printf("%d\n", initialized_code);
+  if (initialized_code == 2) {
+    logidx(idx, STR("New binary is corrupted or the wrong architecture/operating system."));
+    free(path);
+    return 1;
+  } else if (initialized_code == 1 && !check_bin_compat(path)) {
     logidx(idx, STR("New binary must be initialized as pack structure has been changed in new version."));
     free(path);
     return 1;
@@ -943,7 +948,7 @@ int updatebin(int idx, char *par, int secs)
   simple_snprintf(buf, sizeof(buf), STR("%s/.bin.old"), conf.datadir);
   copyfile(binname, buf);
 
-  write_settings(path, -1, 0, initialized);	/* re-write the binary with our packdata */
+  write_settings(path, -1, 0, initialized_code ? 0 : 1);	/* re-write the binary with our packdata */
 
   Tempfile *conffile = new Tempfile("conf");