perror.c 8.56 KB
Newer Older
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
1 2 3 4 5 6 7 8
/* Copyright (C) 2000 MySQL AB

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
bk@work.mysql.com's avatar
bk@work.mysql.com committed
9
   but WITHOUT ANY WARRANTY; without even the implied warranty of
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
10 11 12 13 14 15
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
16 17 18

/* Return error-text for system error messages and nisam messages */

monty@mysql.com's avatar
monty@mysql.com committed
19
#define PERROR_VERSION "2.10"
bk@work.mysql.com's avatar
bk@work.mysql.com committed
20

21
#include <my_global.h>
bk@work.mysql.com's avatar
bk@work.mysql.com committed
22 23 24
#include <my_sys.h>
#include <m_string.h>
#include <errno.h>
25
#include <my_getopt.h>
26 27
#ifdef HAVE_NDBCLUSTER_DB
#include "../ndb/src/ndbapi/ndberror.c"
28
#include "../ndb/src/kernel/error/ndbd_exit_codes.c"
29
#endif
bk@work.mysql.com's avatar
bk@work.mysql.com committed
30

31
static my_bool verbose, print_all_codes;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
32

33 34 35 36 37
#ifdef HAVE_NDBCLUSTER_DB
static my_bool ndb_code;
static char ndb_string[1024];
#endif

38
static struct my_option my_long_options[] =
bk@work.mysql.com's avatar
bk@work.mysql.com committed
39
{
40 41
  {"help", '?', "Displays this help and exits.", 0, 0, 0, GET_NO_ARG,
   NO_ARG, 0, 0, 0, 0, 0, 0},
42
  {"info", 'I', "Synonym for --help.",  0, 0, 0, GET_NO_ARG,
43
   NO_ARG, 0, 0, 0, 0, 0, 0},
44
#ifdef HAVE_NDBCLUSTER_DB
tulin@dl145b.mysql.com's avatar
tulin@dl145b.mysql.com committed
45
  {"ndb", 257, "Ndbcluster storage engine specific error codes.",  (gptr*) &ndb_code,
tomas@poseidon.(none)'s avatar
tomas@poseidon.(none) committed
46
   (gptr*) &ndb_code, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
47
#endif
48 49 50 51 52
#ifdef HAVE_SYS_ERRLIST
  {"all", 'a', "Print all the error messages and the number.",
   (gptr*) &print_all_codes, (gptr*) &print_all_codes, 0, GET_BOOL, NO_ARG,
   0, 0, 0, 0, 0, 0},
#endif
53
  {"silent", 's', "Only print the error message.", 0, 0, 0, GET_NO_ARG, NO_ARG,
54 55 56 57 58 59
   0, 0, 0, 0, 0, 0},
  {"verbose", 'v', "Print error code and message (default).", (gptr*) &verbose,
   (gptr*) &verbose, 0, GET_BOOL, NO_ARG, 1, 0, 0, 0, 0, 0},
  {"version", 'V', "Displays version information and exits.",
   0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
60 61
};

62

bk@work.mysql.com's avatar
bk@work.mysql.com committed
63 64 65 66 67 68 69 70 71 72
typedef struct ha_errors {
  int errcode;
  const char *msg;
} HA_ERRORS;


static HA_ERRORS ha_errlist[]=
{
  { 120,"Didn't find key on read or update" },
  { 121,"Duplicate key on write or update" },
73
  { 123,"Someone has changed the row since it was read (while the table was locked to prevent it)" },
bk@work.mysql.com's avatar
bk@work.mysql.com committed
74
  { 124,"Wrong index given to function" },
75
  { 126,"Index file is crashed" },
bk@work.mysql.com's avatar
bk@work.mysql.com committed
76
  { 127,"Record-file is crashed" },
77 78
  { 128,"Out of memory" },
  { 130,"Incorrect file format" },
bk@work.mysql.com's avatar
bk@work.mysql.com committed
79 80 81 82 83 84 85 86
  { 131,"Command not supported by database" },
  { 132,"Old database file" },
  { 133,"No record read before update" },
  { 134,"Record was already deleted (or record file crashed)" },
  { 135,"No more room in record file" },
  { 136,"No more room in index file" },
  { 137,"No more records (read after end of file)" },
  { 138,"Unsupported extension used for table" },
87
  { 139,"Too big row"},
bk@work.mysql.com's avatar
bk@work.mysql.com committed
88
  { 140,"Wrong create options"},
89
  { 141,"Duplicate unique key or constraint on write or update"},
90
  { 142,"Unknown character set used"},
91
  { 143,"Conflicting table definitions in sub-tables of MERGE table"},
92
  { 144,"Table is crashed and last repair failed"},
93
  { 145,"Table was marked as crashed and should be repaired"},
94 95 96 97
  { 146,"Lock timed out; Retry transaction"},
  { 147,"Lock table is full;  Restart program with a larger locktable"},
  { 148,"Updates are not allowed under a read only transactions"},
  { 149,"Lock deadlock; Retry transaction"},
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
98
  { 150,"Foreign key constraint is incorrectly formed"},
99 100
  { 151,"Cannot add a child row"},
  { 152,"Cannot delete a parent row"},
101 102 103 104 105 106 107 108 109 110 111 112
  { -30999, "DB_INCOMPLETE: Sync didn't finish"},
  { -30998, "DB_KEYEMPTY: Key/data deleted or never created"},
  { -30997, "DB_KEYEXIST: The key/data pair already exists"},
  { -30996, "DB_LOCK_DEADLOCK: Deadlock"},
  { -30995, "DB_LOCK_NOTGRANTED: Lock unavailable"},
  { -30994, "DB_NOSERVER: Server panic return"},
  { -30993, "DB_NOSERVER_HOME: Bad home sent to server"},
  { -30992, "DB_NOSERVER_ID: Bad ID sent to server"},
  { -30991, "DB_NOTFOUND: Key/data pair not found (EOF)"},
  { -30990, "DB_OLD_VERSION: Out-of-date version"},
  { -30989, "DB_RUNRECOVERY: Panic return"},
  { -30988, "DB_VERIFY_BAD: Verify failed; bad format"},
bk@work.mysql.com's avatar
bk@work.mysql.com committed
113 114 115 116
  { 0,NullS },
};


117 118
#include <help_start.h>

bk@work.mysql.com's avatar
bk@work.mysql.com committed
119 120 121 122 123 124
static void print_version(void)
{
  printf("%s Ver %s, for %s (%s)\n",my_progname,PERROR_VERSION,
	 SYSTEM_TYPE,MACHINE_TYPE);
}

125

bk@work.mysql.com's avatar
bk@work.mysql.com committed
126 127 128 129
static void usage(void)
{
  print_version();
  puts("This software comes with ABSOLUTELY NO WARRANTY. This is free software,\nand you are welcome to modify and redistribute it under the GPL license\n");
paul@ice.local's avatar
paul@ice.local committed
130
  printf("Print a description for a system error code or an error code from\na MyISAM/ISAM/BDB table handler.\n");
131
  printf("If you want to get the error for a negative error code, you should use\n-- before the first error code to tell perror that there was no more options.\n\n");
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
132
  printf("Usage: %s [OPTIONS] [ERRORCODE [ERRORCODE...]]\n",my_progname);
133 134 135 136
  my_print_help(my_long_options);
  my_print_variables(my_long_options);
}

137 138
#include <help_end.h>

139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158

static my_bool
get_one_option(int optid, const struct my_option *opt __attribute__((unused)),
	       char *argument __attribute__((unused)))
{
  switch (optid) {
  case 's':
    verbose=0;
    break;
  case 'V':
    print_version();
    exit(0);
    break;
  case 'I':
  case '?':
    usage();
    exit(0);
    break;
  }
  return 0;
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
159
}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
160 161 162 163


static int get_options(int *argc,char ***argv)
{
164
  int ho_error;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
165

166
  if ((ho_error=handle_options(argc, argv, my_long_options, get_one_option)))
167 168
    exit(ho_error);

bk@work.mysql.com's avatar
bk@work.mysql.com committed
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
  if (!*argc && !print_all_codes)
  {
    usage();
    return 1;
  }
  return 0;
} /* get_options */


static const char *get_ha_error_msg(int code)
{
  HA_ERRORS *ha_err_ptr;

  for (ha_err_ptr=ha_errlist ; ha_err_ptr->errcode ;ha_err_ptr++)
    if (ha_err_ptr->errcode == code)
      return ha_err_ptr->msg;
  return NullS;
}


int main(int argc,char *argv[])
{
  int error,code,found;
  const char *msg;
193
  char *unknown_error = 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
194 195 196 197 198 199 200 201 202 203 204 205 206
  MY_INIT(argv[0]);

  if (get_options(&argc,&argv))
    exit(1);

  error=0;
#ifdef HAVE_SYS_ERRLIST
  if (print_all_codes)
  {
    HA_ERRORS *ha_err_ptr;
    for (code=1 ; code < sys_nerr ; code++)
    {
      if (sys_errlist[code][0])
207
      {						/* Skip if no error-text */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
208 209 210 211 212 213 214 215 216
	printf("%3d = %s\n",code,sys_errlist[code]);
      }
    }
    for (ha_err_ptr=ha_errlist ; ha_err_ptr->errcode ;ha_err_ptr++)
      printf("%3d = %s\n",ha_err_ptr->errcode,ha_err_ptr->msg);
  }
  else
#endif
  {
monty@mysql.com's avatar
monty@mysql.com committed
217 218 219 220 221
    /*
      On some system, like NETWARE, strerror(unknown_error) returns a
      string 'Unknown Error'.  To avoid printing it we try to find the
      error string by asking for an impossible big error message.
    */
222
    msg= strerror(10000);
223

224 225 226 227 228 229
    /*
      Allocate a buffer for unknown_error since strerror always returns
      the same pointer on some platforms such as Windows
    */
    unknown_error= malloc(strlen(msg)+1);
    strmov(unknown_error, msg);
monty@mysql.com's avatar
monty@mysql.com committed
230

bk@work.mysql.com's avatar
bk@work.mysql.com committed
231 232
    for ( ; argc-- > 0 ; argv++)
    {
233

bk@work.mysql.com's avatar
bk@work.mysql.com committed
234 235
      found=0;
      code=atoi(*argv);
236 237 238
#ifdef HAVE_NDBCLUSTER_DB
      if (ndb_code)
      {
239 240 241
	if ((ndb_error_string(code, ndb_string, sizeof(ndb_string)) < 0) &&
	    (ndbd_exit_string(code, ndb_string, sizeof(ndb_string)) < 0))
	{
242
          msg= 0;
243
	}
244 245
	else
	  msg= ndb_string;
246 247 248 249 250 251 252 253 254 255 256 257 258 259
        if (msg)
        {
          if (verbose)
            printf("NDB error code %3d: %s\n",code,msg);
          else
            puts(msg);
        }
        else
        {
	  fprintf(stderr,"Illegal ndb error code: %d\n",code);
          error= 1;
        }
        found= 1;
        msg= 0;
260 261 262 263
      }
      else 
#endif
	msg = strerror(code);
monty@mysql.com's avatar
monty@mysql.com committed
264 265

      /*
266 267 268
        We don't print the OS error message if it is the same as the
        unknown_error message we retrieved above, or it starts with
        'Unknown Error' (without regard to case).
monty@mysql.com's avatar
monty@mysql.com committed
269
      */
270
      if (msg &&
271 272
          my_strnncoll(&my_charset_latin1, (const uchar*) msg, 13,
                       (const uchar*) "Unknown Error", 13) &&
monty@mysql.com's avatar
monty@mysql.com committed
273
          (!unknown_error || strcmp(msg, unknown_error)))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
274 275 276
      {
	found=1;
	if (verbose)
277
	  printf("OS error code %3d:  %s\n",code,msg);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
278 279 280 281 282 283 284 285 286 287 288 289 290 291
	else
	  puts(msg);
      }
      if (!(msg=get_ha_error_msg(code)))
      {
	if (!found)
	{
	  fprintf(stderr,"Illegal error code: %d\n",code);
	  error=1;
	}
      }
      else
      {
	if (verbose)
292
	  printf("MySQL error code %3d: %s\n",code,msg);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
293 294 295 296 297
	else
	  puts(msg);
      }
    }
  }
298 299 300

  /* if we allocated a buffer for unknown_error, free it now */
  if (unknown_error)
301
    free(unknown_error);
302

bk@work.mysql.com's avatar
bk@work.mysql.com committed
303 304 305
  exit(error);
  return error;
}