-1

I made myself a webserver in C that connects to older hardware when users request a page. Here's pseudo code of what I did.

  1. Allocate only one large memory chunk for all variables via Malloc.
  2. run an endless loop to wait for new connections.

The reason I run an endless loop is because if I didn't then only one page would be served and the program would exit. Instead, I used sigaction to trap CTRL+C and some segmentation faults, that way my program can free memory before exiting.

When I read the linux manual for sigaction, it states it can't trap SIGKILL and SIGSTOP, which means currently if my program receives one of those two signals then memory will likely not be freed. I free memory using free(memoryhandle).

Is there a catch-all like function that I can use to trap all the signals possible so when an unknown signal enters my program I can force memory to be free?

Here's a template of my code so far (minus the includes):

char* memory;

void processconnections(){
  //connection processing here
}

void startserver(){
   //server startup sequence
}

void myhandler(int signal){
    free(memory);
}

int main(){
    int size=1000000; //about 1MB to allocate
    memory=malloc(size);
    sigaction act={0};
    sigemptyset(&act.sa_mask);
    act.sa_handler=myhandler;
    act.sa_flags=SA_RESTART;
    sigaction(constant_for_remaining_signals,act,NULL);     
    startserver();
    while(1){
      processconnections();
    }
}

I'm trying to think of what to replace the 1st parameter of sigaction with to catch all remaining signals that aren't being taken care of by earlier sigaction calls.

any idea?

Mike
  • 9
  • 2
  • 8
    You don't need to free memory before exiting. When a process exits, all its memory is reclaimed automatically. – Barmar May 18 '22 at 18:17
  • 8
    when your program exits, the system will reclaim all of its memory. You don't need to worry that it is lost. – stark May 18 '22 at 18:17
  • 1
    It's also generally unsafe to do anything after getting a segmentation fault. This often indicates that the heap is corrupted. – Barmar May 18 '22 at 18:18
  • All that said, you should still make a best effort to reclaim all allocated memory when you no longer need it.. – Robert Harvey May 18 '22 at 18:18
  • It's generally a good idea to explicitly free dynamically allocated memory (under some circumstances, the system might not free all the memory). – Promitheas Nikou May 18 '22 at 18:35
  • @PromitheasNikou The only exception I can think of would be shared memory. And it's not clear that the process should free that if there could be other processes still using it. – Barmar May 18 '22 at 18:49
  • I am pretty sure I read somewhere that on some OSes, if you have machine configured as a server, it doesn't free the memory when the program exits (but I can't remember where I read this, so I'm not 100% sure; I'll look it up). It can't hurt to free the memory anyway... – Promitheas Nikou May 18 '22 at 19:21
  • @PromitheasNikou I'm pretty sure that's not the case on any modern server OS. – Joseph Sible-Reinstate Monica May 18 '22 at 22:20

1 Answers1

0

Is there a catch-all like function that I can use to trap all the signals possible so when an unknown signal enters my program I can force memory to be free?

No, there isn't.

As others have commented, your entire premise for this question is based on the mistaken belief that you must free memory or it will be leaked.

Nothing of the sort will happen on any UNIX OS -- when your program dies, all malloc()ed memory will be automatically released by the OS.

In addition, you worry about SIGKILL and SIGSTOP being sent to your program "out of the blue", but signals don't get sent to your program willy nilly. If you (or other programs you run) don't send these signals, then your program will not receive them.

Employed Russian
  • 199,314
  • 34
  • 295
  • 362