Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 653
  • Last Modified:

urgent: SEGMENTATION FAULT WHEN RUNNING C API ON LINUX SERVER

I am experiencing the error" segmentation fault (core dumped)"  on a linux server when compile using
gcc -I/usr/lib/mysql -L/usr/include/mysql mysql_client.c -o mysql_client -lm -lmysqlclient -lz

my code is as follows

#include <stdio.h>
#include <stdlib.h>
#include <mysql/mysql.h>
#include<string.h>
#include<unistd.h>

#define USE_OLD_FUNCTIONS 1
#define MAXLEN 200
#define NUMBER_OF_COLS 6
MYSQL mysql;
MYSQL_RES *res;
MYSQL_ROW row;

void exiterr(int exitcode)
{
    fprintf( stderr, "%s\n", mysql_error(&mysql) );
    exit( exitcode );
}
int mysql_exec_sql(MYSQL *mysql,const char *create_definition)
{
   return mysql_real_query(mysql,create_definition,strlen(create_definition));
}

int main()
{
/********************************************************************
VARIABLE DEFINITIONS AND INITIALIZAITONS
**********************************************************************/      
          uint i = 0;
      char buf[]="name,sex,disease";
      char record[MAXLEN],prev_record[NUMBER_OF_COLS][MAXLEN],diagnosis[MAXLEN];
      char *name,*sex,*disease;
      int ipat_id,ipat_status;
      char cpat_id[10], cpat_status[3];
      //sprintf(cpat_id, "%d", ipat_id);
      mysql_init(&mysql);
      strcpy(diagnosis,"");
/********************************************************************
CONNECTING TO TELEMEDICINE DATABASE ON MYSQL SERVER
**********************************************************************/      
    if (!(mysql_real_connect(&mysql,HOSTNAME,USERNAME,PASSWORD,DATABASENAME,0,NULL,0)) )
        {
            printf("there is some error in connection\n");
            exiterr(1);
      }

    if (mysql_select_db(&mysql,DATABASENAME))
        exiterr(2);
 /********************************************************************
FINDING LAST RECORD IN THE DATABASE
**********************************************************************/      
   if (mysql_query(&mysql,"SELECT * FROM patient ORDER BY pat_id ASC"))
        exiterr(3);

    if (!(res = mysql_store_result(&mysql)))
        exiterr(4);
   
    while((row = mysql_fetch_row(res))) {
        for (i=0 ; i < mysql_num_fields(res); i++)
       {
                 strcpy(prev_record[i],row[i]);
      }
    }      
/********************************************************************
THE INPUT TEXT FROM CLIENT IS IDNETIFIED IN VARIABLES
**********************************************************************/      
      name=strtok(buf,",");
      sex=strtok(NULL,",");
      disease=strtok(NULL,",");
      
/********************************************************************
SETTING UP CLIENT ID PROPERLY IN VARIABLES
**********************************************************************/      
      ipat_id=atoi(prev_record[0]);      //converting char to iteger
      ipat_id++;                  //setting up new id for the new record
      sprintf(cpat_id, "%d", ipat_id);//Again conv to char to be used in mysql query
      ipat_status = 1;
      sprintf(cpat_status, "%d", ipat_status);//Again conv to char to be used in mysql query
      
/********************************************************************
PRODUCE MYSQL QUERY IN REQ FORMAT IN 'RECORD' ARRAY
**********************************************************************/            
strcpy(record,"INSERT INTO `patient` ( `pat_id` , `pat_status`  , `pat_name` , `pat_sex` , `pat_disease` ) VALUES ('");
      strcat(record,cpat_id);
      strcat(record," ',' ");
      strcat(record,cpat_status);
      strcat(record," ',' ");
      strcat(record,name);
      strcat(record," ',' ");
      strcat(record,sex);
      strcat(record," ',' ");
      strcat(record,disease);
      strcat(record," ' ) ");
      
      printf("record = %s\n",record );

    if(mysql_exec_sql(&mysql,record)==0)/*success*/
    {
      printf( "Record Added\n");      
    }
else
    printf( "Failed to add records: Error: %s\n", mysql_error(&mysql));
/********************************************************************
WAITING FOR DOCTOR RESOPONSE
**********************************************************************/            

strcpy(record,"SELECT pat_diagnose FROM patient WHERE pat_id = '");
      strcat(record,cpat_id);
      strcat(record," ' ");

while(strlen(diagnosis) == 0)
{
      printf("Sleeping for 15 seconds\n");
      sleep(30);

      if (mysql_query(&mysql,record))
              exiterr(3);

      if (!(res = mysql_store_result(&mysql)))
              exiterr(4);
        //printf("NUMBER OF FIELDS = %d\n", mysql_num_fields(res));
       row = mysql_fetch_row(res);
          strcpy(diagnosis,row[0]);
          //printf("DIAGNOSIS = %s length = %d\n" , diagnosis, strlen(diagnosis));
 }               
/********************************************************************
DISPLAYING ALL THE RECORDS PRESENT IN THE TABLE
**********************************************************************/            
      if (mysql_query(&mysql,"SELECT * FROM patient"))
        exiterr(3);

    if (!(res = mysql_store_result(&mysql)))
        exiterr(4);
    while((row = mysql_fetch_row(res))) {
        for (i=0 ; i < mysql_num_fields(res); i++)
       {
                 printf("%s\n", row[i]);
      }
    }
    if (!mysql_eof(res))
        exiterr(5);

strcpy(record,"UPDATE patient SET pat_status = 0 WHERE pat_id = '");
      strcat(record,cpat_id);
      strcat(record," ' ");
      
//      printf("record = %s\n",record );

    if(mysql_exec_sql(&mysql,record)==0)/*success*/
    {
      printf( "Record Added\n");      
    }
else
    printf( "Failed to add records: Error: %s\n", mysql_error(&mysql));



/********************************************************************
CLOSING THE CONNECTION TO DATABASE
**********************************************************************/            
mysql_free_result(res);
mysql_close(&mysql);
    return 0;

}
      
0
ibm979
Asked:
ibm979
  • 3
1 Solution
 
Joe JenkinsCommented:
Hmm.  Have you backtraced your core file to see where the segfault occured?  That will be a good start to fixing this problem.  You should have gdb.


1. gdb <executable> core

2. Use bt to see what happened.

3. Type select-frame <number>, where <number> is the number that appeared to the left of the name in the last level called.

4. Type list <source file>:<line> to get a listing of that line of code and a number of lines around it.

5. Type print <variable name> to see the value of a variable.  Does this look like what it should be?  Are you passing a null pointer or something weird?

6. Type q when done.

That's a basic outline of how you can get the values for what's going on here.  

Joe Jenkins
0
 
Joe JenkinsCommented:
Any follow-up on this?

Joe Jenkins
0
 
ibm979Author Commented:
hi

What i found that the server where i was trying the compile the program has very old version of MYSQL. I assumed that problem was with that old software. Bcz i compiled the same code on my local machine with latest version of MYSQL and it come with out any error and working properly.

regards
0
 
Joe JenkinsCommented:
ibm979

Ah, I'm glad you found the problem.  I looked through your code and didn't see anything that stuck out to me.  I do most of my programming in mysql 5 or MSSQL so it all made sense to me.

Grats!
Joe
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now