mkind
mkind

Reputation: 2043

looping mysql_real_connect or what

consider following c code:

int main (int argc, char *argv[]) {
  MYSQL *sql_handle;

  fprintf(stdout,"initializing handle..\n");
  sql_handle = mysql_init(sql_handle);

  fprintf(stdout,"connecting to database..\n");
  mysql_real_connect(sql_handle,NULL,NULL,
                      NULL,"test",0,NULL,0);

  fprintf(stdout,"connection established\n");
  mysql_close(sql_handle);
}

this produces the following output

...
initializing handle..
connecting to database..
initializing handle..
connecting to database..
initializing handle..
connecting to database..
initializing handle..
connecting to database..
connection established
Error: Can't create UNIX socket (24)

the real_connect function seems to have problems. the daemon is running for sure. it's long time ago since i was used to c so this might be a stupid question.

[update] here's the complete code

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 
  4 #if defined __WIN32__ || _MSC_VER
  5    #include "my_global.h"
  6    #include "mysql.h"
  7 #else
  8    #include <mysql.h>
  9 #endif
 10 
 11 /* prototypes */
 12 void connect(void);
 13 
 14 /* sql handle */
 15 MYSQL *sql_handle;
 16 
 17 int main (int argc, char *argv[]) {
 18   fprintf(stdout,"main..\n");
 19   connect();
 20   return EXIT_SUCCESS;
 21   
 22 } 
 23 void connect(void){
 24   fprintf(stdout,"initializing database handle..\n");
 25   sql_handle = mysql_init(NULL);
 26   
 27   fprintf(stdout,"connecting to database..\n");
 28   mysql_real_connect(sql_handle,NULL,NULL,NULL,NULL,0,NULL,0);
 29   
 30   fprintf(stdout,"closing connection..\n");
 31   mysql_close(sql_handle);
 32 } 

this code produces that output:

...
connecting to database..
initializing database handle..
connecting to database..
initializing database handle..
connecting to database..
closing connection..
[1]    12914 segmentation fault (core dumped)  ./db

copying the body of the connect-function into main and removing the connect-functions resolves the issue. but this is not a solution.

Upvotes: 1

Views: 1290

Answers (3)

Kalki70
Kalki70

Reputation: 559

I think the problem was in the function connect(), which has the same name as connect() for sockets :

int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen);

When linking, the function mysql_real_connect() needs to call connect(), to connect the underlying socket, but the redefined connect() function is called instead, and so the loop is produced.

That is why changing the name of connect() to connect_to_database() solved the issue.

Upvotes: 1

mkind
mkind

Reputation: 2043

renaming the method

void connect(void)

to

void connect_to_database(void)

solved that issue for me

Upvotes: 1

pmg
pmg

Reputation: 108978

I think you need a real object for the sql handle

int main (int argc, char *argv[]) {
  MYSQL *sql_handle;

  /* create object */
  sql_handle = malloc(sizeof *sql_handle);

  /* ... */

  /* release object */
  free(sql_handle);
}

or

int main (int argc, char *argv[]) {
  MYSQL sql_handle;        /* create object, not pointer */

  fprintf(stdout,"initializing handle..\n");
  mysql_init(&sql_handle); /* use address */

  fprintf(stdout,"connecting to database..\n");
  mysql_real_connect(&sql_handle,NULL,NULL, /* use address */
                      NULL,"test",0,NULL,0);

  fprintf(stdout,"connection established\n");
  mysql_close(&sql_handle); /* use address */

}

Upvotes: 0

Related Questions