Unni
Unni

Reputation: 5794

symbol lookup error: undefined symbol: fclose

I have the following program which basically implements a DNS server. Shared memory is used to implement the server cache. I am trying to populate some initial entries to the cache by reading them from a file. But when the file entries go beyond a point, I am getting a segmentation fault. I am unable to debug the program on my own.

typedef struct rs{
char domainName[256];
char ip[36];
time_t timeStamp;
struct rs *cacheEnd;
} ResourceRecord;
ResourceRecord *cache, *tempCachePtr, *cacheEnd;
void create_Shared_Memory()         //Creates shared memory 
{
    int stateMemory;
    stateMemory=shmget(IPC_PRIVATE, (MAX_CACHE_SIZE)*sizeof(ResourceRecord *),IPC_CREAT|0660);
    if (stateMemory == -1)
    {
            perror("Shared memory creation");
                exit(EXIT_FAILURE);
        }
    cache=(ResourceRecord *)shmat(stateMemory,NULL,0);
    tempCachePtr = cache;
    if(shmctl(stateMemory,IPC_RMID,NULL)==-1)
    printf("ERROR CREATING CARMODEMEMORY!!!");

    if(cache==NULL)
    {
        perror("Shared memory attach ");
        shmctl(stateMemory,IPC_RMID,NULL);
        exit(EXIT_FAILURE);

    }
}

void populateCache(){
   tempCachePtr = cache+1;
   struct hostent* domainAddress;
   int i;
   ResourceRecord record;
   char * line = NULL;
   size_t fileInputLength = 0;
   ssize_t read;
   puts("Populating cache");
   FILE *fp = fopen("hosts.txt", "r");
   for(i=1;i<=40; i++){
      if((read = getline(&line, &fileInputLength, fp)) == -1){
        break;
      }
      printf("%d From file %s\n",i, line);
      strcpy(record.domainName, strtok(line, " "));
      strcpy(record.ip, strtok(NULL, "\n"));
      time(&record.timeStamp);
      *tempCachePtr = record;
      tempCachePtr++;
   }
   cacheEnd = tempCachePtr-1;
   *(cache->cacheEnd) = cacheEnd;
   fclose(fp);
}

I tried to debug the code using gdb and here is what I have

symbol lookup error: /home/path/dnsserver: undefined symbol: fclose, version GLIBC_2.2.5 [Inferior 1 (process 7178) exited with code 0177]

and also when I tried to change the number of file entries and debugged again:

SIGSEGV from /lib64/ld-linux-x86-64.so.2 shared memory

Upvotes: 0

Views: 1223

Answers (1)

Unni
Unni

Reputation: 5794

As pointed out by Anto Jurković, the mistake was that I was creating shared memory using 50*sizeof(ResourceRecord*) where sizeof(ResourceRecord*) is only a few bytes because it was a pointer but sizeof(ResourceRecord) was much bigger (8 and 312 in my system). So 50*sizeof(ResourceRecord*) was only allocating 400 bytes of memory which clearly was not sufficient for 40 ResourceRecord type records.

Upvotes: 1

Related Questions