kennedy
kennedy

Reputation: 151

Stack overflow in thread 1: can't grow stack to 0xffe601ff8 Valgrind Error

I am new to C programming, any help is appreciated. The code will be used to check if a pid is still active. The argument that provides the path to the pid file is passed via command line. Please see my Valgrind and GDB errors below, along with the code.

**Valgrind Error**
==6553== Memcheck, a memory error detector
==6553== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==6553== Using Valgrind-3.10.0 and LibVEX; rerun with -h for copyright info
==6553== Command: ./pid1108_2 /var/run/httpd/httpd.pid
==6553== 
==6553== Stack overflow in thread 1: can't grow stack to 0xffe601ff8
==6553== 
==6553== Process terminating with default action of signal 11 (SIGSEGV)
==6553==  Access not within mapped region at address 0xFFE601FF8
==6553==    at 0x40069F: kill (in /home/ehubbard/C_Checks/pid1108_2)
==6553==  If you believe this happened as a result of a stack
==6553==  overflow in your program's main thread (unlikely but
==6553==  possible), you can try to increase the size of the
==6553==  main thread stack using the --main-stacksize= flag.
==6553==  The main thread stack size used in this run was 10485760.
==6553== Stack overflow in thread 1: can't grow stack to 0xffe601ff0
==6553== 
==6553== Process terminating with default action of signal 11 (SIGSEGV)
==6553==  Access not within mapped region at address 0xFFE601FF0
==6553==    at 0x4801661: _vgnU_freeres (vg_preloaded.c:58)
==6553==  If you believe this happened as a result of a stack
==6553==  overflow in your program's main thread (unlikely but
==6553==  possible), you can try to increase the size of the
==6553==  main thread stack using the --main-stacksize= flag.
==6553==  The main thread stack size used in this run was 10485760.
==6553== 
==6553== HEAP SUMMARY:
==6553==     in use at exit: 0 bytes in 0 blocks
==6553==   total heap usage: 1 allocs, 1 frees, 568 bytes allocated
==6553== 
==6553== All heap blocks were freed -- no leaks are possible
==6553== 
==6553== For counts of detected and suppressed errors, rerun with: -v
==6553== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 4 from 4)

**GDB Error**
Program received signal SIGSEGV, Segmentation fault.
0x000000000040068f in kill ()`enter code here`
#include <stdio.h>      //Needed for standard I/O
#include <stdlib.h>     //Needed for exit
#include <sys/types.h>  //Needed for kill function
#include <signal.h>     //Needed for kill function
#include <inttypes.h>
#include <iso646.h>

int kill(pid_t pid, int sig);

int main(int argc, char *argv[])
{
    FILE *fp;
    int pid;

    fp = fopen(argv[1], "r");

    if (fp == NULL){
        printf("Pid file doesn't exist:");
        return 2;}
    else {
          fscanf(fp, "%d", &pid);
          printf("Pid number is %d", pid);
          fclose(fp);
         }
    kill(pid, 0);

}

int kill(pid_t pid, int sig)
{
    if ((kill (pid, sig)) == -1){
         printf("Pid %d is no longer valid", pid);
         return 2;
    }
    else if ((kill (pid, sig)) == 0){
         printf("Pid %d is active.", pid);
         return 0;
    }
    else{
         printf("Could not determine value!");
         return 2;
    }
}

Upvotes: 1

Views: 10534

Answers (2)

Ilya
Ilya

Reputation: 4689

The reason of the problem is infinite loop (recursion) in function kill(). It is hard to say, what you are trying to do, but now this implementation of kill() calls itself in the first line (if ((kill (pid, sig)) == -1){) and there are no condition to stop this infinite recursion. So it works while the system has enough memory to continue. To fix it you need to correct logic of this function.

If you are trying to call external function kill() from your own one, it is easier to rename your function:

int my_kill(pid_t pid, int sig)
{
  // your current code
}

Upvotes: 1

hcs
hcs

Reputation: 1534

Calling kill from within your custom kill causes an infinite recursion. You should call your custom kill something else, like custom_kill, and call that from main, then the calls to kill will go to the proper Unix kill(2) (or they will fail if the linking is not set up correctly).

Upvotes: 3

Related Questions