Problem with fork inside a thread and system() - Linux

This is a discussion on Problem with fork inside a thread and system() - Linux ; Hi everybody, I am experiencing an annoying problem with fork() and system() In my program I need to launch the "pppd" from a thread, so, I create a new process with fork and then in the child process I launch ...

+ Reply to Thread
Results 1 to 7 of 7

Thread: Problem with fork inside a thread and system()

  1. Problem with fork inside a thread and system()

    Hi everybody,
    I am experiencing an annoying problem with fork() and system()
    In my program I need to launch the "pppd" from a thread, so, I create
    a new process with fork and then in the child process I launch with
    system the pppd to connect an embedded system to internet.
    The problem is that pppd fails.
    Here is my example code:

    int my_thread(...)
    {
    pid=fork();
    if (pid==EAGAIN || pid==ENOMEM)
    {
    abort();
    }
    switch(pid)
    {
    case 0: // child
    {
    system ("/usr/sbin/pppd call gprs");
    printf("Error execv:%s",strerror(errno));
    exit(0);
    }
    break;
    case -1:
    abort();
    break;
    default: // father
    father stuff.....
    }
    }

    in var/log/messages I can read:
    daemon.notice pppd[2145]: pppd 2.4.3 started by root, uid 0 <----
    this is O.K.
    daemon.err pppd[2145]: Connect script failed <--- I can't believe
    it!!!

    On the other hand I made a simple program with a main and a simple
    fork and system call and this is right and works fine!!!! I can't
    believe it!!!!
    Is there any difference between call system inside a child created
    from a thread and a child created from a single main (wich is also a
    thread).

    Regards,
    Unai.

  2. Re: Problem with fork inside a thread and system()

    CMorgan writes:
    > I am experiencing an annoying problem with fork() and system()
    > In my program I need to launch the "pppd" from a thread, so, I create
    > a new process with fork and then in the child process I launch with
    > system the pppd to connect an embedded system to internet.
    > The problem is that pppd fails.
    > Here is my example code:
    >
    > int my_thread(...)
    > {
    > pid=fork();
    > if (pid==EAGAIN || pid==ENOMEM)
    > {
    > abort();
    > }
    > switch(pid)
    > {
    > case 0: // child
    > {
    > system ("/usr/sbin/pppd call gprs");
    > printf("Error execv:%s",strerror(errno));
    > exit(0);
    > }
    > break;
    > case -1:
    > abort();
    > break;
    > default: // father
    > father stuff.....
    > }
    > }


    Independently (but maybe related to) of your problem: system already
    forks & waits for you, meaning there is no reason to call system from
    a forked processes, except insofar this means the process calling
    system isn't halted until the command has exited. If you don't want
    the latter, don't use system,




  3. Re: Problem with fork inside a thread and system()

    On 28 dic, 16:36, Rainer Weikusat wrote:
    > CMorgan writes:
    > > I am experiencing an annoying problem withfork() andsystem()
    > > In my program I need to launch the "pppd" from a thread, so, I create
    > > a new process withforkand then in the child process I launch with
    > >systemthepppdto connect an embeddedsystemto internet.
    > > The problem is thatpppdfails.
    > > Here is my example code:

    >
    > > int my_thread(...)
    > > {
    > > pid=fork();
    > > if (pid==EAGAIN || pid==ENOMEM)
    > > {
    > > abort();
    > > }
    > > switch(pid)
    > > {
    > > case 0: // child
    > > {
    > > system("/usr/sbin/pppdcall gprs");
    > > printf("Error execv:%s",strerror(errno));
    > > exit(0);
    > > }
    > > break;
    > > case -1:
    > > abort();
    > > break;
    > > default: // father
    > > father stuff.....
    > > }
    > > }

    >
    > Independently (but maybe related to) of your problem:systemalready
    > forks & waits for you, meaning there is no reason to callsystemfrom
    > a forked processes, except insofar this means the process callingsystemisn't halted until the command has exited. If you don't want
    > the latter, don't usesystem,


    Ok I Have tryed with execv:
    I just change the exec line for this one:
    execv("/usr/sbin",macapi_gprs_ref->cmd_argv);

    explanation:
    "macapi_gprs_ref->cmd_argv" stores the parameters. I dumped it to my
    log to check if parameters are right, and they are right.
    Values of argv are:
    parameter 0 = /usr/sbin/pppd
    parameter 1 = call
    parameter 2 = gprs

    the result of execv is wrong:
    "Error execv:Permission denied"

    This is confusing, any hint?
    Thanks

  4. Re: Problem with fork inside a thread and system()

    On 28 dic, 16:36, Rainer Weikusat wrote:
    > CMorgan writes:
    > > I am experiencing an annoying problem withfork() andsystem()
    > > In my program I need to launch the "pppd" from a thread, so, I create
    > > a new process withforkand then in the child process I launch with
    > >systemthepppdto connect an embeddedsystemto internet.
    > > The problem is thatpppdfails.
    > > Here is my example code:

    >
    > > int my_thread(...)
    > > {
    > > pid=fork();
    > > if (pid==EAGAIN || pid==ENOMEM)
    > > {
    > > abort();
    > > }
    > > switch(pid)
    > > {
    > > case 0: // child
    > > {
    > > system("/usr/sbin/pppdcall gprs");
    > > printf("Error execv:%s",strerror(errno));
    > > exit(0);
    > > }
    > > break;
    > > case -1:
    > > abort();
    > > break;
    > > default: // father
    > > father stuff.....
    > > }
    > > }

    >
    > Independently (but maybe related to) of your problem:systemalready
    > forks & waits for you, meaning there is no reason to callsystemfrom
    > a forked processes, except insofar this means the process callingsystemisn't halted until the command has exited. If you don't want
    > the latter, don't usesystem,


    Ok I Have tryed with execv:
    I just change the exec line for this one:
    execv("/usr/sbin",macapi_gprs_ref->cmd_argv);

    explanation:
    "macapi_gprs_ref->cmd_argv" stores the parameters. I dumped it to my
    log to check if parameters are right, and they are right.
    Values of argv are:
    parameter 0 = /usr/sbin/pppd
    parameter 1 = call
    parameter 2 = gprs

    the result of execv is wrong:
    "Error execv:Permission denied"

    This is confusing, any hint?
    Thanks

  5. Re: Problem with fork inside a thread and system()

    CMorgan writes:
    > On 28 dic, 16:36, Rainer Weikusat wrote:
    >> CMorgan writes:
    >> > I am experiencing an annoying problem withfork() andsystem()
    >> > In my program I need to launch the "pppd" from a thread, so, I create
    >> > a new process withforkand then in the child process I launch with
    >> >systemthepppdto connect an embeddedsystemto internet.
    >> > The problem is thatpppdfails.
    >> > Here is my example code:

    >>
    >> > int my_thread(...)
    >> > {
    >> > pid=fork();
    >> > if (pid==EAGAIN || pid==ENOMEM)
    >> > {
    >> > abort();
    >> > }
    >> > switch(pid)
    >> > {
    >> > case 0: // child
    >> > {
    >> > system("/usr/sbin/pppdcall gprs");
    >> > printf("Error execv:%s",strerror(errno));
    >> > exit(0);
    >> > }
    >> > break;
    >> > case -1:
    >> > abort();
    >> > break;
    >> > default: // father
    >> > father stuff.....
    >> > }
    >> > }

    >>
    >> Independently (but maybe related to) of your problem:systemalready
    >> forks & waits for you, meaning there is no reason to callsystemfrom
    >> a forked processes, except insofar this means the process
    >> callingsystemisn't halted until the command has exited. If you
    >> don't want >> the latter, don't usesystem,

    >
    > Ok I Have tryed with execv:
    > I just change the exec line for this one:
    > execv("/usr/sbin",macapi_gprs_ref->cmd_argv);
    >
    > explanation:
    > "macapi_gprs_ref->cmd_argv" stores the parameters. I dumped it to my
    > log to check if parameters are right, and they are right.
    > Values of argv are:
    > parameter 0 = /usr/sbin/pppd
    > parameter 1 = call
    > parameter 2 = gprs
    >
    > the result of execv is wrong:
    > "Error execv:Permission denied"
    >
    > This is confusing, any hint?


    The first argument to execv needs to be the file to be executed,
    eg /usr/sbin/pppd.

  6. Re: Problem with fork inside a thread and system()

    On 28 dic, 16:55, CMorgan wrote:
    > On 28 dic, 16:36, Rainer Weikusat wrote:
    >
    >
    >
    > > CMorgan writes:
    > > > I am experiencing an annoying problem withfork() andsystem()
    > > > In my program I need to launch the "pppd" from a thread, so, I create
    > > > a new process withforkand then in the child process I launch with
    > > >systemthepppdto connect an embeddedsystemto internet.
    > > > The problem is thatpppdfails.
    > > > Here is my example code:

    >
    > > > int my_thread(...)
    > > > {
    > > > pid=fork();
    > > > if (pid==EAGAIN || pid==ENOMEM)
    > > > {
    > > > abort();
    > > > }
    > > > switch(pid)
    > > > {
    > > > case 0: // child
    > > > {
    > > > system("/usr/sbin/pppdcall gprs");
    > > > printf("Error execv:%s",strerror(errno));
    > > > exit(0);
    > > > }
    > > > break;
    > > > case -1:
    > > > abort();
    > > > break;
    > > > default: // father
    > > > father stuff.....
    > > > }
    > > > }

    >
    > > Independently (but maybe related to) of your problem:systemalready
    > > forks & waits for you, meaning there is no reason to callsystemfrom
    > > a forked processes, except insofar this means the process callingsystemisn't halted until the command has exited. If you don't want
    > > the latter, don't usesystem,

    >
    > Ok I Have tryed with execv:
    > I just change the exec line for this one:
    > execv("/usr/sbin",macapi_gprs_ref->cmd_argv);
    >
    > explanation:
    > "macapi_gprs_ref->cmd_argv" stores the parameters. I dumped it to my
    > log to check if parameters are right, and they are right.
    > Values of argv are:
    > parameter 0 = /usr/sbin/pppd
    > parameter 1 = call
    > parameter 2 = gprs
    >
    > the result of execv is wrong:
    > "Error execv:Permission denied"
    >
    > This is confusing, any hint?
    > Thanks


    OK, I have advanced a little bit,
    latest problem was due to an error in spanish translatin of execv man
    page!!!
    firs parameter of execv is not the path to the command but also the
    complete path and file.
    Anyway, now the script is launched but it fails
    In var log messages I can read:
    .... pppd 2.4.3 started by root, uid 0
    .... Connect script failed

    And the strangest thing is that from my easy program (only a main and
    a fork) it works!!!!
    any idea?
    Regards,
    Unai

  7. Re: Problem with fork inside a thread and system()

    On 28 dic, 17:17, CMorgan wrote:
    > On 28 dic, 16:55, CMorgan wrote:
    >
    >
    >
    > > On 28 dic, 16:36, Rainer Weikusat wrote:

    >
    > > > CMorgan writes:
    > > > > I am experiencing an annoying problem withfork() andsystem()
    > > > > In my program I need to launch the "pppd" from a thread, so, I create
    > > > > a new process withforkand then in the child process I launch with
    > > > >systemthepppdto connect an embeddedsystemto internet.
    > > > > The problem is thatpppdfails.
    > > > > Here is my example code:

    >
    > > > > int my_thread(...)
    > > > > {
    > > > > pid=fork();
    > > > > if (pid==EAGAIN || pid==ENOMEM)
    > > > > {
    > > > > abort();
    > > > > }
    > > > > switch(pid)
    > > > > {
    > > > > case 0: // child
    > > > > {
    > > > > system("/usr/sbin/pppdcall gprs");
    > > > > printf("Error execv:%s",strerror(errno));
    > > > > exit(0);
    > > > > }
    > > > > break;
    > > > > case -1:
    > > > > abort();
    > > > > break;
    > > > > default: // father
    > > > > father stuff.....
    > > > > }
    > > > > }

    >
    > > > Independently (but maybe related to) of your problem:systemalready
    > > > forks & waits for you, meaning there is no reason to callsystemfrom
    > > > a forked processes, except insofar this means the process callingsystemisn't halted until the command has exited. If you don't want
    > > > the latter, don't usesystem,

    >
    > > Ok I Have tryed with execv:
    > > I just change the exec line for this one:
    > > execv("/usr/sbin",macapi_gprs_ref->cmd_argv);

    >
    > > explanation:
    > > "macapi_gprs_ref->cmd_argv" stores the parameters. I dumped it to my
    > > log to check if parameters are right, and they are right.
    > > Values of argv are:
    > > parameter 0 = /usr/sbin/pppd
    > > parameter 1 = call
    > > parameter 2 = gprs

    >
    > > the result of execv is wrong:
    > > "Error execv:Permission denied"

    >
    > > This is confusing, any hint?
    > > Thanks

    >
    > OK, I have advanced a little bit,
    > latest problem was due to an error in spanish translatin of execv man
    > page!!!
    > firs parameter of execv is not the path to the command but also the
    > complete path and file.
    > Anyway, now the script is launched but it fails
    > In var log messages I can read:
    > ... pppd 2.4.3 started by root, uid 0
    > ... Connect script failed
    >
    > And the strangest thing is that from my easy program (only a main and
    > a fork) it works!!!!
    > any idea?
    > Regards,
    > Unai


    Ok, I have made another test program just to discard bugs (my
    applications is really big) and the result is the same, that is, when
    I call execv from a child process created from a thread the pppd
    script fails!!!!

    Here I paste my two test program, the test that fails, the test that
    success:

    ------- TEST PROGRAM THAT FAILS -------
    #include
    #include
    #include
    #include
    #include
    #include
    #include

    static bool ActivarGPRS (void);
    static void* macapi_gprs_connect_thread(void *param);

    int main(int argc,char** argv)
    {
    pthread_t thread;

    pthread_create(&thread,NULL,&macapi_gprs_connect_thread,NULL);

    while(1)
    {
    sleep(100);
    }
    return 0;
    }

    static void* macapi_gprs_connect_thread(void *param)
    {
    if( true == ActivarGPRS() )
    printf("GPRS activado\n");
    else
    printf("No se puede activar el GPRS\n");

    }

    static bool ActivarGPRS (void)
    {
    int pid;
    struct timeval T0, T1;
    FILE *fp = NULL;

    //************ GPRS *************
    printf ("ActivarGPRS\n");
    if ((pid = fork ()) < 0)
    {
    printf ("Error fork\n");
    return false;
    }
    // Tiempo de arranque
    gettimeofday (&T0, NULL);
    if (pid == 0)
    {
    char* argv[] = {"/usr/sbin/pppd","call","gprs",NULL};
    // Proceso hijo
    //system ("/usr/sbin/pppd call gprs > /home/sattoll/
    StatusGPRS");
    execv(argv[0],argv);
    printf("execv failure:%s\n",strerror(errno));
    system ("rm -f /home/sattoll/StatusGPRS");
    exit (0);
    }
    else
    {
    // Proceso padre
    while ((fp = fopen ("/home/sattoll/GPRSOK", "r")) == NULL)
    {
    // Si pasa mucho tiempo sin conexion (1 minutos), se
    sale y se reintentera mas tarde
    gettimeofday (&T1, NULL);
    if ((T1.tv_sec - T0.tv_sec) > 40)
    {
    printf ("Timeout\n");
    system ("killall -2 pppd");
    // hay que esperar porque si no se ha conectado al
    CE
    system ("sleep 3");
    system ("rm -f /home/sattoll/StatusGPRS");
    return false;
    }
    }
    return true;
    }
    }
    ------- END OF TEST PROGRAM THAT FAILS -------
    ------- TEST PROGRAM THAT WORKS -------------
    #include
    #include
    #include
    #include
    #include
    #include

    static bool ActivarGPRS (void);

    int main(int argc,char** argv)
    {
    if( true == ActivarGPRS() )
    printf("GPRS activado\n");
    else
    printf("No se puede activar el GPRS\n");
    }

    static bool ActivarGPRS (void)
    {
    int pid;
    struct timeval T0, T1;
    FILE *fp = NULL;

    //************ GPRS *************
    printf ("ActivarGPRS\n");
    if ((pid = fork ()) < 0)
    {
    printf ("Error fork\n");
    return false;
    }
    // Tiempo de arranque
    gettimeofday (&T0, NULL);
    if (pid == 0)
    {
    char* argv[] = {"/usr/sbin/pppd","call","gprs",NULL};
    // Proceso hijo
    //system ("/usr/sbin/pppd call gprs > /home/sattoll/
    StatusGPRS");
    execv(argv[0],argv);
    printf("execv failure:%s\n",strerror(errno));
    system ("rm -f /home/sattoll/StatusGPRS");
    exit (0);
    }
    else
    {
    // Proceso padre
    while ((fp = fopen ("/home/sattoll/GPRSOK", "r")) == NULL)
    {
    // Si pasa mucho tiempo sin conexion (1 minutos), se
    sale y se reintentera mas tarde
    gettimeofday (&T1, NULL);
    if ((T1.tv_sec - T0.tv_sec) > 40)
    {
    printf ("Timeout\n");
    system ("killall -2 pppd");
    // hay que esperar porque si no se ha conectado al
    CE
    system ("sleep 3");
    system ("rm -f /home/sattoll/StatusGPRS");
    return false;
    }
    }
    return true;
    }
    }
    ----------- END OF TEST PROGRAM THAT WORKS

+ Reply to Thread