~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to libdrizzle/client.c

  • Committer: brian
  • Date: 2008-08-01 09:15:55 UTC
  • mfrom: (261.1.4 drizzle)
  • Revision ID: brian@localhost.localdomain-20080801091555-axgut9h6uvtbatei
Merge

Show diffs side-by-side

added added

removed removed

Lines of Context:
253
253
static void read_user_name(char *name)
254
254
{
255
255
  if (geteuid() == 0)
256
 
    (void) strmov(name,"root");    /* allow use of surun */
 
256
    strcpy(name,"root");    /* allow use of surun */
257
257
  else
258
258
  {
259
259
#ifdef HAVE_GETPWUID
267
267
         !(str=getenv("LOGIN")))
268
268
  str="UNKNOWN_USER";
269
269
    }
270
 
    (void) strmake(name,str,USERNAME_LENGTH);
 
270
    strncpy(name,str,USERNAME_LENGTH);
271
271
#elif HAVE_CUSERID
272
272
    (void) cuserid(name);
273
273
#else
274
 
    strmov(name,"UNKNOWN_USER");
 
274
    strcpy(name,"UNKNOWN_USER");
275
275
#endif
276
276
  }
277
277
  return;
296
296
  {
297
297
    net= &drizzle->net;
298
298
    net->last_errno= errcode;
299
 
    strmov(net->last_error, ER(errcode));
300
 
    strmov(net->sqlstate, sqlstate);
 
299
    strcpy(net->last_error, ER(errcode));
 
300
    strcpy(net->sqlstate, sqlstate);
301
301
  }
302
302
  else
303
303
  {
304
304
    drizzle_server_last_errno= errcode;
305
 
    strmov(drizzle_server_last_error, ER(errcode));
 
305
    strcpy(drizzle_server_last_error, ER(errcode));
306
306
  }
307
307
  return;
308
308
}
317
317
{
318
318
  net->last_errno= 0;
319
319
  net->last_error[0]= '\0';
320
 
  strmov(net->sqlstate, not_error_sqlstate);
 
320
  strcpy(net->sqlstate, not_error_sqlstate);
321
321
}
322
322
 
323
323
/**
345
345
  vsnprintf(net->last_error, sizeof(net->last_error)-1,
346
346
               format, args);
347
347
  va_end(args);
348
 
  strmov(net->sqlstate, sqlstate);
 
348
  strcpy(net->sqlstate, sqlstate);
349
349
 
350
350
  return;
351
351
}
388
388
      len-=2;
389
389
      if (protocol_41(drizzle) && pos[0] == '#')
390
390
      {
391
 
  strmake(net->sqlstate, pos+1, SQLSTATE_LENGTH);
392
 
  pos+= SQLSTATE_LENGTH+1;
 
391
        strncpy(net->sqlstate, pos+1, SQLSTATE_LENGTH);
 
392
        pos+= SQLSTATE_LENGTH+1;
393
393
      }
394
394
      else
395
395
      {
398
398
          (unknown error sql state).
399
399
        */
400
400
 
401
 
        strmov(net->sqlstate, unknown_sqlstate);
 
401
        strcpy(net->sqlstate, unknown_sqlstate);
402
402
      }
403
403
 
404
 
      (void) strmake(net->last_error,(char*) pos,
405
 
         min((uint) len,(uint) sizeof(net->last_error)-1));
 
404
      strncpy(net->last_error,(char*) pos, min((uint) len,
 
405
              (uint32_t) sizeof(net->last_error)-1));
406
406
    }
407
407
    else
408
408
      set_drizzle_error(drizzle, CR_UNKNOWN_ERROR, unknown_sqlstate);
679
679
      options->password=my_strdup(opt_arg,MYF(MY_WME));
680
680
    }
681
681
    break;
682
 
        case 5:
683
 
          options->protocol = DRIZZLE_PROTOCOL_PIPE;
684
682
  case 20:      /* connect_timeout */
685
683
  case 6:        /* timeout */
686
684
    if (opt_arg)
751
749
            exit(1);
752
750
          }
753
751
          break;
754
 
        case 26: /* shared_memory_base_name */
755
 
#ifdef HAVE_SMEM
756
 
          if (options->shared_memory_base_name != def_shared_memory_base_name)
757
 
            my_free(options->shared_memory_base_name,MYF(MY_ALLOW_ZERO_PTR));
758
 
          options->shared_memory_base_name=my_strdup(opt_arg,MYF(MY_WME));
759
 
#endif
760
 
          break;
761
752
  case 27: /* multi-results */
762
753
    options->client_flag|= CLIENT_MULTI_RESULTS;
763
754
    break;
1112
1103
          drizzle_port =(uint) atoi(env);
1113
1104
      }
1114
1105
    }
1115
 
    if (!drizzle_unix_port)
1116
 
    {
1117
 
      char *env;
1118
 
      drizzle_unix_port = (char*) MYSQL_UNIX_ADDR;
1119
 
      if ((env = getenv("DRIZZLE_UNIX_PORT")))
1120
 
        drizzle_unix_port = env;
1121
 
    }
1122
1106
#if defined(SIGPIPE)
1123
1107
    (void) signal(SIGPIPE, SIG_IGN);
1124
1108
#endif
1158
1142
  ptr->options.client_flag|= CLIENT_LOCAL_FILES;
1159
1143
#endif
1160
1144
 
1161
 
#ifdef HAVE_SMEM
1162
 
  ptr->options.shared_memory_base_name= (char*) def_shared_memory_base_name;
1163
 
#endif
1164
 
 
1165
1145
  ptr->options.methods_to_use= DRIZZLE_OPT_GUESS_CONNECTION;
1166
1146
  ptr->options.report_data_truncation= true;  /* default */
1167
1147
 
1341
1321
  char          *end,*host_info=NULL;
1342
1322
  uint32_t         pkt_length;
1343
1323
  NET           *net= &drizzle->net;
1344
 
  struct        sockaddr_un UNIXaddr;
1345
1324
  init_sigpipe_variables
1346
1325
 
1347
1326
  /* Don't give sigpipe errors if the client doesn't want them */
1389
1368
  /*
1390
1369
    Part 0: Grab a socket and connect it to the server
1391
1370
  */
1392
 
#if defined(HAVE_SMEM)
1393
 
  if ((!drizzle->options.protocol ||
1394
 
       drizzle->options.protocol == DRIZZLE_PROTOCOL_MEMORY) &&
1395
 
      (!host || !strcmp(host,LOCAL_HOST)))
1396
 
  {
1397
 
    if ((create_shared_memory(drizzle,net, drizzle->options.connect_timeout)) ==
1398
 
  INVALID_HANDLE_VALUE)
1399
 
    {
1400
 
      if (drizzle->options.protocol == DRIZZLE_PROTOCOL_MEMORY)
1401
 
  goto error;
1402
 
 
1403
 
      /*
1404
 
        Try also with PIPE or TCP/IP. Clear the error from
1405
 
        create_shared_memory().
1406
 
      */
1407
 
 
1408
 
      net_clear_error(net);
1409
 
    }
1410
 
    else
1411
 
    {
1412
 
      drizzle->options.protocol=DRIZZLE_PROTOCOL_MEMORY;
1413
 
      unix_socket = 0;
1414
 
      host=drizzle->options.shared_memory_base_name;
1415
 
      snprintf(host_info=buff, sizeof(buff)-1,
1416
 
               ER(CR_SHARED_MEMORY_CONNECTION), host);
1417
 
    }
1418
 
  }
1419
 
#endif /* HAVE_SMEM */
1420
 
  if (!net->vio &&
1421
 
      (!drizzle->options.protocol ||
1422
 
       drizzle->options.protocol == DRIZZLE_PROTOCOL_SOCKET) &&
1423
 
      (unix_socket || drizzle_unix_port) &&
1424
 
      (!host || !strcmp(host,LOCAL_HOST)))
1425
 
  {
1426
 
    my_socket sock= socket(AF_UNIX, SOCK_STREAM, 0);
1427
 
    if (sock == SOCKET_ERROR)
1428
 
    {
1429
 
      set_drizzle_extended_error(drizzle, CR_SOCKET_CREATE_ERROR,
1430
 
                               unknown_sqlstate,
1431
 
                               ER(CR_SOCKET_CREATE_ERROR),
1432
 
                               socket_errno);
1433
 
      goto error;
1434
 
    }
1435
 
 
1436
 
    net->vio= vio_new(sock, VIO_TYPE_SOCKET,
1437
 
                      VIO_LOCALHOST | VIO_BUFFERED_READ);
1438
 
    if (!net->vio)
1439
 
    {
1440
 
      set_drizzle_error(drizzle, CR_CONN_UNKNOW_PROTOCOL, unknown_sqlstate);
1441
 
      closesocket(sock);
1442
 
      goto error;
1443
 
    }
1444
 
 
1445
 
    host= LOCAL_HOST;
1446
 
    if (!unix_socket)
1447
 
      unix_socket= drizzle_unix_port;
1448
 
    host_info= (char*) ER(CR_LOCALHOST_CONNECTION);
1449
 
 
1450
 
    memset((char*) &UNIXaddr, 0, sizeof(UNIXaddr));
1451
 
    UNIXaddr.sun_family= AF_UNIX;
1452
 
    strmake(UNIXaddr.sun_path, unix_socket, sizeof(UNIXaddr.sun_path)-1);
1453
 
 
1454
 
    if (my_connect(sock, (struct sockaddr *) &UNIXaddr, sizeof(UNIXaddr),
1455
 
       drizzle->options.connect_timeout))
1456
 
    {
1457
 
      set_drizzle_extended_error(drizzle, CR_CONNECTION_ERROR,
1458
 
                               unknown_sqlstate,
1459
 
                               ER(CR_CONNECTION_ERROR),
1460
 
                               unix_socket, socket_errno);
1461
 
      vio_delete(net->vio);
1462
 
      net->vio= 0;
1463
 
      goto error;
1464
 
    }
1465
 
    drizzle->options.protocol=DRIZZLE_PROTOCOL_SOCKET;
1466
 
  }
1467
1371
  if (!net->vio &&
1468
1372
      (!drizzle->options.protocol ||
1469
1373
       drizzle->options.protocol == DRIZZLE_PROTOCOL_TCP))
1603
1507
    Scramble is split into two parts because old clients does not understand
1604
1508
    long scrambles; here goes the first part.
1605
1509
  */
1606
 
  strmake(drizzle->scramble, end, SCRAMBLE_LENGTH_323);
 
1510
  strncpy(drizzle->scramble, end, SCRAMBLE_LENGTH_323);
1607
1511
  end+= SCRAMBLE_LENGTH_323+1;
1608
1512
 
1609
1513
  if (pkt_length >= (uint) (end+1 - (char*) net->read_pos))
1617
1521
  end+= 18;
1618
1522
  if (pkt_length >= (uint) (end + SCRAMBLE_LENGTH - SCRAMBLE_LENGTH_323 + 1 -
1619
1523
                           (char *) net->read_pos))
1620
 
    strmake(drizzle->scramble+SCRAMBLE_LENGTH_323, end,
 
1524
    strncpy(drizzle->scramble+SCRAMBLE_LENGTH_323, end,
1621
1525
            SCRAMBLE_LENGTH-SCRAMBLE_LENGTH_323);
1622
1526
  else
1623
1527
    drizzle->server_capabilities&= ~CLIENT_SECURE_CONNECTION;
1647
1551
    set_drizzle_error(drizzle, CR_OUT_OF_MEMORY, unknown_sqlstate);
1648
1552
    goto error;
1649
1553
  }
1650
 
  strmov(drizzle->host_info,host_info);
1651
 
  strmov(drizzle->host,host);
 
1554
  strcpy(drizzle->host_info,host_info);
 
1555
  strcpy(drizzle->host,host);
1652
1556
  if (unix_socket)
1653
 
    strmov(drizzle->unix_socket,unix_socket);
 
1557
    strcpy(drizzle->unix_socket,unix_socket);
1654
1558
  else
1655
1559
    drizzle->unix_socket=0;
1656
 
  strmov(drizzle->server_version,(char*) net->read_pos+1);
 
1560
  strcpy(drizzle->server_version,(char*) net->read_pos+1);
1657
1561
  drizzle->port=port;
1658
1562
 
1659
1563
  /*
1693
1597
 
1694
1598
  /* This needs to be changed as it's not useful with big packets */
1695
1599
  if (user && user[0])
1696
 
    strmake(end,user,USERNAME_LENGTH);          /* Max user name */
 
1600
    strncpy(end,user,USERNAME_LENGTH);          /* Max user name */
1697
1601
  else
1698
1602
    read_user_name((char*) end);
1699
1603
 
1700
1604
  /* We have to handle different version of handshake here */
1701
 
#ifdef _CUSTOMCONFIG_
1702
 
#include "_cust_libdrizzle.h"
1703
 
#endif
1704
1605
  end= strend(end) + 1;
1705
1606
  if (passwd[0])
1706
1607
  {
1707
1608
    {
1708
1609
      *end++= SCRAMBLE_LENGTH;
1709
 
      scramble(end, drizzle->scramble, passwd);
1710
1610
      end+= SCRAMBLE_LENGTH;
1711
1611
    }
1712
1612
  }
1716
1616
  /* Add database if needed */
1717
1617
  if (db && (drizzle->server_capabilities & CLIENT_CONNECT_WITH_DB))
1718
1618
  {
1719
 
    end= strmake(end, db, NAME_LEN) + 1;
 
1619
    end= strncpy(end, db, NAME_LEN) + NAME_LEN + 1;
1720
1620
    drizzle->db= my_strdup(db,MYF(MY_WME));
1721
1621
    db= 0;
1722
1622
  }
1821
1721
        drizzle->client_flag | CLIENT_REMEMBER_OPTIONS))
1822
1722
  {
1823
1723
    drizzle->net.last_errno= tmp_drizzle.net.last_errno;
1824
 
    strmov(drizzle->net.last_error, tmp_drizzle.net.last_error);
1825
 
    strmov(drizzle->net.sqlstate, tmp_drizzle.net.sqlstate);
 
1724
    strcpy(drizzle->net.last_error, tmp_drizzle.net.last_error);
 
1725
    strcpy(drizzle->net.sqlstate, tmp_drizzle.net.sqlstate);
1826
1726
    return(1);
1827
1727
  }
1828
1728
  if (drizzle_set_character_set(&tmp_drizzle, drizzle->charset->csname))
1830
1730
    memset((char*) &tmp_drizzle.options, 0, sizeof(tmp_drizzle.options));
1831
1731
    drizzle_close(&tmp_drizzle);
1832
1732
    drizzle->net.last_errno= tmp_drizzle.net.last_errno;
1833
 
    strmov(drizzle->net.last_error, tmp_drizzle.net.last_error);
1834
 
    strmov(drizzle->net.sqlstate, tmp_drizzle.net.sqlstate);
 
1733
    strcpy(drizzle->net.last_error, tmp_drizzle.net.last_error);
 
1734
    strcpy(drizzle->net.sqlstate, tmp_drizzle.net.sqlstate);
1835
1735
    return(1);
1836
1736
  }
1837
1737
 
1894
1794
    delete_dynamic(init_commands);
1895
1795
    my_free((char*)init_commands,MYF(MY_WME));
1896
1796
  }
1897
 
#ifdef HAVE_SMEM
1898
 
  if (drizzle->options.shared_memory_base_name != def_shared_memory_base_name)
1899
 
    my_free(drizzle->options.shared_memory_base_name,MYF(MY_ALLOW_ZERO_PTR));
1900
 
#endif /* HAVE_SMEM */
1901
1797
  memset((char*) &drizzle->options, 0, sizeof(drizzle->options));
1902
1798
  return;
1903
1799
}
2207
2103
    drizzle->options.compress= 1;      /* Remember for connect */
2208
2104
    drizzle->options.client_flag|= CLIENT_COMPRESS;
2209
2105
    break;
2210
 
  case DRIZZLE_OPT_NAMED_PIPE:      /* This option is depricated */
2211
 
    drizzle->options.protocol=DRIZZLE_PROTOCOL_PIPE; /* Force named pipe */
2212
 
    break;
2213
2106
  case DRIZZLE_OPT_LOCAL_INFILE:      /* Allow LOAD DATA LOCAL ?*/
2214
2107
    if (!arg || test(*(uint*) arg))
2215
2108
      drizzle->options.client_flag|= CLIENT_LOCAL_FILES;
2238
2131
  case DRIZZLE_OPT_PROTOCOL:
2239
2132
    drizzle->options.protocol= *(uint*) arg;
2240
2133
    break;
2241
 
  case DRIZZLE_SHARED_MEMORY_BASE_NAME:
2242
 
#ifdef HAVE_SMEM
2243
 
    if (drizzle->options.shared_memory_base_name != def_shared_memory_base_name)
2244
 
      my_free(drizzle->options.shared_memory_base_name,MYF(MY_ALLOW_ZERO_PTR));
2245
 
    drizzle->options.shared_memory_base_name=my_strdup(arg,MYF(MY_WME));
2246
 
#endif
2247
 
    break;
2248
2134
  case DRIZZLE_OPT_USE_REMOTE_CONNECTION:
2249
 
  case DRIZZLE_OPT_USE_EMBEDDED_CONNECTION:
2250
2135
  case DRIZZLE_OPT_GUESS_CONNECTION:
2251
2136
    drizzle->options.methods_to_use= option;
2252
2137
    break;