/**
  Check if the server version matches with the server version mysql_upgrade
  was compiled with.

  @return 0 match successful
          1 failed
*/
static int check_version_match(void)
{
  DYNAMIC_STRING ds_version;
  char version_str[NAME_CHAR_LEN + 1];

  if (init_dynamic_string(&ds_version, NULL, NAME_CHAR_LEN, NAME_CHAR_LEN))
    die("Out of memory");

  if (run_query("show variables like 'version'",
                &ds_version, FALSE) ||
      extract_variable_from_show(&ds_version, version_str))
  {
    print_error("Version check failed. Got the following error when calling "
                "the 'mysql' command line client", &ds_version);
    dynstr_free(&ds_version);
    return 1;                                   /* Query failed */
  }

  dynstr_free(&ds_version);

  if (calc_server_version((char *) version_str) != MYSQL_VERSION_ID)
  {
    fprintf(stderr, "Error: Server version (%s) does not match with the "
            "version of\nthe server (%s) with which this program was built/"
            "distributed. You can\nuse --skip-version-check to skip this "
            "check.\n", version_str, MYSQL_SERVER_VERSION);
    return 1;
  }
  else
    return 0;
}
Beispiel #2
0
static int run_sql_fix_privilege_tables(void)
{
  int found_real_errors= 0;
  const char **query_ptr;
  DYNAMIC_STRING ds_script;
  DYNAMIC_STRING ds_result;
  DBUG_ENTER("run_sql_fix_privilege_tables");

  if (init_dynamic_string(&ds_script, "", 65536, 1024))
    die("Out of memory");

  if (init_dynamic_string(&ds_result, "", 512, 512))
    die("Out of memory");

  verbose("Running 'mysql_fix_privilege_tables'...");

  /*
    Individual queries can not be executed independently by invoking
    a forked mysql client, because the script uses session variables
    and prepared statements.
  */
  for ( query_ptr= &mysql_fix_privilege_tables[0];
        *query_ptr != NULL;
        query_ptr++
      )
  {
    dynstr_append(&ds_script, *query_ptr);
  }

  run_query(ds_script.str,
            &ds_result, /* Collect result */
            TRUE);
  {
    /*
      Scan each line of the result for real errors
      and ignore the expected one(s) like "Duplicate column name",
      "Unknown column" and "Duplicate key name" since they just
      indicate the system tables are already up to date
    */
    char *line= ds_result.str;
    do
    {
      if (!is_expected_error(line))
      {
        /* Something unexpected failed, dump error line to screen */
        found_real_errors++;
        print_line(line);
      }
      else if ((strncmp(line, "WARNING", 7) == 0) ||
               (strncmp(line, "Warning", 7) == 0))
      {
        print_line(line);
      }
    } while ((line= get_line(line)) && *line);
  }

  dynstr_free(&ds_result);
  dynstr_free(&ds_script);
  DBUG_RETURN(found_real_errors);
}
static void free_used_memory(void)
{
  /* Free memory allocated by 'load_defaults' */
  free_defaults(defaults_argv);

  dynstr_free(&ds_args);
  dynstr_free(&conn_args);
}
Beispiel #4
0
/*
 * Contact the hypervisor metadata agent and request the value for the provided
 * key name.  Returns a C string if a value is found, NULL if no value is
 * found, or aborts the program on any other condition.  The caller is expected
 * to call free(3C) on the returned string.
 */
char *
mdataGet(const char *keyname)
{
    char *errmsg = NULL;
    string_t *mdata = NULL;
    mdata_response_t mdr;
    char *out;

    if (initialized_proto == 0) {
        if (proto_init(&mdp, &errmsg) != 0) {
            fatal(ERR_MDATA_INIT, "could not initialize metadata: %s\n",
                errmsg);
        }
        initialized_proto = 1;
    }

    if (proto_execute(mdp, "GET", keyname, &mdr, &mdata) != 0) {
        fatal(ERR_UNEXPECTED, "failed to get metadata for '%s': unknown "
          "error\n", keyname);
    }

    switch (mdr) {
    case MDR_SUCCESS:
        if ((out = strdup(dynstr_cstr(mdata))) == NULL) {
            fatal(ERR_STRDUP, "strdup failure\n");
        }
        dynstr_free(mdata);
        dlog("MDATA %s=%s\n", keyname, out);
        return (out);

    case MDR_NOTFOUND:
        dlog("INFO no metadata for '%s'\n", keyname);
        dynstr_free(mdata);
        return (NULL);

    case MDR_UNKNOWN:
        fatal(ERR_MDATA_FAIL, "failed to get metadata for '%s': %s\n",
            keyname, dynstr_cstr(mdata));
        break;

    case MDR_INVALID_COMMAND:
        fatal(ERR_MDATA_FAIL, "failed to get metadata for '%s': %s\n",
            keyname, "host does not support GET");
        break;

    default:
        fatal(ERR_UNEXPECTED, "GET[%s]: unknown response\n", keyname);
        break;
    }

    /* NOTREACHED */
    abort();
    return (NULL);
}
MYSQL_RES *MADB_GetDefaultColumnValues(MADB_Stmt *Stmt, MYSQL_FIELD *fields)
{
  DYNAMIC_STRING DynStr;
  unsigned int i;
  MYSQL_RES *result= NULL;
  
  init_dynamic_string(&DynStr, "SELECT COLUMN_NAME, COLUMN_DEFAULT FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA='", 512, 512);
  if (dynstr_append(&DynStr, fields[0].db) ||
      dynstr_append(&DynStr, "' AND TABLE_NAME='") ||
      dynstr_append(&DynStr, fields[0].org_table) ||
      dynstr_append(&DynStr, "' AND COLUMN_NAME IN ("))
    goto error;

  for (i=0; i < mysql_stmt_field_count(Stmt->stmt); i++)
  {
    if (dynstr_append(&DynStr, i > 0 ? ",'" : "'") ||
        dynstr_append(&DynStr, fields[i].org_name) ||
        dynstr_append(&DynStr, "'"))
      goto error;
  }
  if (dynstr_append(&DynStr, ") AND COLUMN_DEFAULT IS NOT NULL"))
    goto error;

  LOCK_MARIADB(Stmt->Connection);
  if (mysql_query(Stmt->Connection->mariadb, DynStr.str))
    goto error;
  result= mysql_store_result(Stmt->Connection->mariadb);
  
error:
    UNLOCK_MARIADB(Stmt->Connection);
    dynstr_free(&DynStr);
    return result;
}
Beispiel #6
0
void
mdataPut(const char *keyname, const char *value)
{
    string_t *data;
    char *errmsg = NULL;
    mdata_response_t mdr;
    string_t *req = dynstr_new();

    if (initialized_proto == 0) {
        if (proto_init(&mdp, &errmsg) != 0) {
            fatal(ERR_MDATA_INIT, "could not initialize metadata: %s\n",
                errmsg);
        }
        initialized_proto = 1;
    }

    base64_encode(keyname, strlen(keyname), req);
    dynstr_appendc(req, ' ');
    base64_encode(value, strlen(value), req);

    if (proto_version(mdp) < 2) {
        fatal(ERR_MDATA_TOO_OLD, "mdata protocol must be >= 2 for PUT");
    }

    if (proto_execute(mdp, "PUT", dynstr_cstr(req), &mdr, &data) != 0) {
        fatal(ERR_MDATA_FAIL, "failed to PUT");
    }

    dynstr_free(req);

    dlog("MDATA PUT %s=%s\n", keyname, value);
}
static int get_upgrade_info_file_name(char* name)
{
  DYNAMIC_STRING ds_datadir;
  DBUG_ENTER("get_upgrade_info_file_name");

  if (init_dynamic_string(&ds_datadir, NULL, 32, 32))
    die("Out of memory");

  if (run_query("show variables like 'datadir'",
                &ds_datadir, FALSE) ||
      extract_variable_from_show(&ds_datadir, name))
  {
    dynstr_free(&ds_datadir);
    DBUG_RETURN(1); /* Query failed */
  }

  dynstr_free(&ds_datadir);

  fn_format(name, "mysql_upgrade_info", name, "", MYF(0));
  DBUG_PRINT("exit", ("name: %s", name));
  DBUG_RETURN(0);
}
Beispiel #8
0
int main(void)
{
  plan(23);

  IF_WIN(skip_all("Test of POSIX shell escaping rules, not for CMD.EXE\n"), );

  ok(init_dynamic_string(&str1, NULL, 0, 32) == 0, "init");

  ok(dynstr_append_os_quoted(&str1, "test1", NULL) == 0, "append");
  check("'test1'");

  ok(dynstr_append_os_quoted(&str1, "con", "cat", NULL) == 0, "append");
  check("'concat'");

  ok(dynstr_append_os_quoted(&str1, "", NULL) == 0, "append");
  check("''");

  ok(dynstr_append_os_quoted(&str1, "space inside", NULL) == 0, "append");
  check("'space inside'");

  ok(dynstr_append_os_quoted(&str1, "single'quote", NULL) == 0, "append");
  check("'single'\"'\"'quote'");

  ok(dynstr_append_os_quoted(&str1, "many'single'quotes", NULL) == 0, "append");
  check("'many'\"'\"'single'\"'\"'quotes'");

  ok(dynstr_append_os_quoted(&str1, "'single quoted'", NULL) == 0, "append");
  check("''\"'\"'single quoted'\"'\"''");

  ok(dynstr_append_os_quoted(&str1, "double\"quote", NULL) == 0, "append");
  check("'double\"quote'");

  ok(dynstr_append_os_quoted(&str1, "mixed\"single'and\"double'quotes", NULL) == 0, "append");
  check("'mixed\"single'\"'\"'and\"double'\"'\"'quotes'");

  ok(dynstr_append_os_quoted(&str1, "back\\space", NULL) == 0, "append");
  check("'back\\space'");

  ok(dynstr_append_os_quoted(&str1, "backspace\\'and\\\"quote", NULL) == 0, "append");
  check("'backspace\\'\"'\"'and\\\"quote'");

  dynstr_free(&str1);

  return exit_status();
}
static my_bool is_mysql()
{
  my_bool ret= TRUE;
  DYNAMIC_STRING ds_events_struct;

  if (init_dynamic_string(&ds_events_struct, NULL,
                          EVENTS_STRUCT_LEN, EVENTS_STRUCT_LEN))
    die("Out of memory");

  if (run_query("show create table mysql.event",
                &ds_events_struct, FALSE) ||
      strstr(ds_events_struct.str, "IGNORE_BAD_TABLE_OPTIONS") != NULL)
    ret= FALSE;
  else
    verbose("MySQL upgrade detected");

  dynstr_free(&ds_events_struct);
  return(ret);
}
Beispiel #10
0
my_bool free_dynamic_string_array(DYNAMIC_STRING_ARRAY *array)
{
    DBUG_ENTER("free_dynamic_string_array");
    if (array) {
        delete_dynamic(array->pos_info_arr);
        array->pos_info_arr = NULL;
        
        dynstr_free(array->dynstr);
        array->dynstr = NULL;
    
        my_free(array->pos_info_arr);
        my_free(array->dynstr);
        array->pos_info_arr = NULL;
        array->dynstr = NULL;
        array->cur_idx = 0;
        DBUG_RETURN(FALSE);
    }
    
    DBUG_RETURN(TRUE);
}
static int run_tool(char *tool_path, DYNAMIC_STRING *ds_res, ...)
{
  int ret;
  const char* arg;
  va_list args;
  DYNAMIC_STRING ds_cmdline;

  DBUG_ENTER("run_tool");
  DBUG_PRINT("enter", ("tool_path: %s", tool_path));

  if (init_dynamic_string(&ds_cmdline, IF_WIN("\"", ""), FN_REFLEN, FN_REFLEN))
    die("Out of memory");

  dynstr_append_os_quoted(&ds_cmdline, tool_path, NullS);
  dynstr_append(&ds_cmdline, " ");

  va_start(args, ds_res);

  while ((arg= va_arg(args, char *)))
  {
    /* Options should be os quoted */
    if (strncmp(arg, "--", 2) == 0)
      dynstr_append_os_quoted(&ds_cmdline, arg, NullS);
    else
      dynstr_append(&ds_cmdline, arg);
    dynstr_append(&ds_cmdline, " ");
  }

  va_end(args);

#ifdef __WIN__
  dynstr_append(&ds_cmdline, "\"");
#endif

  DBUG_PRINT("info", ("Running: %s", ds_cmdline.str));
  ret= run_command(ds_cmdline.str, ds_res);
  DBUG_PRINT("exit", ("ret: %d", ret));
  dynstr_free(&ds_cmdline);
  DBUG_RETURN(ret);
}
Beispiel #12
0
static int run_sql_fix_privilege_tables(void)
{
  int found_real_errors= 0;
  DYNAMIC_STRING ds_result;
  DBUG_ENTER("run_sql_fix_privilege_tables");

  if (init_dynamic_string(&ds_result, "", 512, 512))
    die("Out of memory");

  verbose("Phase 3/3: Running 'mysql_fix_privilege_tables'...");
  run_query(mysql_fix_privilege_tables,
            &ds_result, /* Collect result */
            TRUE);

  {
    /*
      Scan each line of the result for real errors
      and ignore the expected one(s) like "Duplicate column name",
      "Unknown column" and "Duplicate key name" since they just
      indicate the system tables are already up to date
    */
    char *line= ds_result.str;
    do
    {
      if (!is_expected_error(line))
      {
        /* Something unexpected failed, dump error line to screen */
        found_real_errors++;
        print_line(line);
      }
      else if (strncmp(line, "WARNING", 7) == 0)
      {
        print_line(line);
      }
    } while ((line= get_line(line)) && *line);
  }

  dynstr_free(&ds_result);
  DBUG_RETURN(found_real_errors);
}
Beispiel #13
0
static int
plat_send_reset(mdata_plat_t *mpl)
{
	int ret = -1;
	string_t *str = dynstr_new();

	dynstr_append(str, "\n");
	if (plat_send(mpl, str) != 0)
		goto bail;
	dynstr_reset(str);

	if (plat_recv(mpl, str, 2000) != 0)
		goto bail;

	if (strcmp(dynstr_cstr(str), "invalid command") != 0)
		goto bail;

	ret = 0;

bail:
	dynstr_free(str);
	return (ret);
}
/**
  Look for the filename of given tool, with the presumption that it is in the
  same directory as mysql_upgrade and that the same executable-searching 
  mechanism will be used when we run our sub-shells with popen() later.
*/
static void find_tool(char *tool_executable_name, const char *tool_name, 
                      const char *self_name)
{
  char *last_fn_libchar;
  DYNAMIC_STRING ds_tmp;
  DBUG_ENTER("find_tool");
  DBUG_PRINT("enter", ("progname: %s", my_progname));

  if (init_dynamic_string(&ds_tmp, "", 32, 32))
    die("Out of memory");

  last_fn_libchar= strrchr(self_name, FN_LIBCHAR);

  if (last_fn_libchar == NULL)
  {
    /*
      mysql_upgrade was found by the shell searching the path.  A sibling
      next to us should be found the same way.
    */
    strncpy(tool_executable_name, tool_name, FN_REFLEN);
  }
  else
  {
    int len;

    /*
      mysql_upgrade was run absolutely or relatively.  We can find a sibling
      by replacing our name after the LIBCHAR with the new tool name.
    */

    /*
      When running in a not yet installed build and using libtool,
      the program(mysql_upgrade) will be in .libs/ and executed
      through a libtool wrapper in order to use the dynamic libraries
      from this build. The same must be done for the tools(mysql and
      mysqlcheck). Thus if path ends in .libs/, step up one directory
      and execute the tools from there
    */
    if (((last_fn_libchar - 6) >= self_name) &&
        (strncmp(last_fn_libchar - 5, ".libs", 5) == 0) &&
        (*(last_fn_libchar - 6) == FN_LIBCHAR))
    {
      DBUG_PRINT("info", ("Chopping off \".libs\" from end of path"));
      last_fn_libchar -= 6;
    }

    len= last_fn_libchar - self_name;

    my_snprintf(tool_executable_name, FN_REFLEN, "%.*s%c%s",
                len, self_name, FN_LIBCHAR, tool_name);
  }

  if (opt_verbose)
    verbose("Looking for '%s' as: %s", tool_name, tool_executable_name);

  /*
    Make sure it can be executed
  */
  if (run_tool(tool_executable_name,
               &ds_tmp, /* Get output from command, discard*/
               "--no-defaults",
               "--help",
               "2>&1",
               IF_WIN("> NUL", "> /dev/null"),
               NULL))
    die("Can't execute '%s'", tool_executable_name);

  dynstr_free(&ds_tmp);

  DBUG_VOID_RETURN;
}
SQLRETURN MADB_DaeStmt(MADB_Stmt *Stmt, SQLUSMALLINT Operation)
{
  char *TableName= MADB_GetTableName(Stmt);
  char *CatalogName= MADB_GetCatalogName(Stmt);
  DYNAMIC_STRING DynStmt;

  MADB_CLEAR_ERROR(&Stmt->Error);
  memset(&DynStmt, 0, sizeof(DYNAMIC_STRING));

  if (Stmt->DaeStmt)
    Stmt->Methods->StmtFree(Stmt->DaeStmt, SQL_DROP);
  Stmt->DaeStmt= NULL;
  if (!SQL_SUCCEEDED(SQLAllocStmt(Stmt->Connection, (SQLHANDLE *)&Stmt->DaeStmt)))
  {
    MADB_CopyError(&Stmt->Error, &Stmt->Connection->Error);
    goto end;
  }

  switch(Operation)
  {
  case SQL_ADD:
    if (init_dynamic_string(&DynStmt, "INSERT INTO ", 1024, 1024) ||
        MADB_DynStrAppendQuoted(&DynStmt, CatalogName) ||
        dynstr_append(&DynStmt, ".") ||
        MADB_DynStrAppendQuoted(&DynStmt, TableName)||
        MADB_DynStrUpdateSet(Stmt, &DynStmt))
    {
      dynstr_free(&DynStmt);
      return Stmt->Error.ReturnValue;
    }
    Stmt->DataExecutionType= MADB_DAE_ADD;
    break;
  case SQL_DELETE:
    if (init_dynamic_string(&DynStmt, "DELETE FROM ", 1024, 1024) ||
        MADB_DynStrAppendQuoted(&DynStmt, CatalogName) ||
        dynstr_append(&DynStmt, ".") ||
        MADB_DynStrAppendQuoted(&DynStmt, TableName) ||
        MADB_DynStrGetWhere(Stmt, &DynStmt, TableName, FALSE))
    {
      dynstr_free(&DynStmt);
      return Stmt->Error.ReturnValue;
    }
    Stmt->DataExecutionType= MADB_DAE_DELETE;
    break;
  case SQL_UPDATE:
    Stmt->Methods->RefreshRowPtrs(Stmt);
    if (init_dynamic_string(&DynStmt, "UPDATE ", 1024, 1024) ||
        MADB_DynStrAppendQuoted(&DynStmt, CatalogName) ||
        dynstr_append(&DynStmt, ".") ||
        MADB_DynStrAppendQuoted(&DynStmt, TableName)||
        MADB_DynStrUpdateSet(Stmt, &DynStmt)||
        MADB_DynStrGetWhere(Stmt, &DynStmt, TableName, FALSE))
    {
      dynstr_free(&DynStmt);
      return Stmt->Error.ReturnValue;
    }
    Stmt->DataExecutionType= MADB_DAE_UPDATE;
    break;
  }
  
  if (!SQL_SUCCEEDED(SQLPrepare(Stmt->DaeStmt, (SQLCHAR *)DynStmt.str, SQL_NTS)))
  {
    MADB_CopyError(&Stmt->Error, &Stmt->DaeStmt->Error);
    Stmt->Methods->StmtFree(Stmt->DaeStmt, SQL_DROP);
  }
   
end:
  dynstr_free(&DynStmt);
  return Stmt->Error.ReturnValue;

}