I need a technical explanation on the following shell behavior - Unix

This is a discussion on I need a technical explanation on the following shell behavior - Unix ; Given the following.... #include #include #include #include #include #include #include int sys(char *cmd) { pid_t pid; int stat; if ((pid = fork()) fprintf(stderr, "fork error\n"); exit(1); } else if (pid == 0) { /*execl("/home/cdalten/oakland/am", "am", "-c", cmd, (char *)0);*/ execl(cmd, ...

+ Reply to Thread
Results 1 to 13 of 13

Thread: I need a technical explanation on the following shell behavior

  1. I need a technical explanation on the following shell behavior

    Given the following....

    #include
    #include
    #include
    #include
    #include
    #include
    #include

    int sys(char *cmd)
    {
    pid_t pid;
    int stat;

    if ((pid = fork()) < 0) {
    fprintf(stderr, "fork error\n");
    exit(1);
    } else if (pid == 0) {
    /*execl("/home/cdalten/oakland/am", "am", "-c", cmd, (char *)0);*/
    execl(cmd, (char *)0);
    _exit(127);
    } else {
    while (waitpid(pid, &stat, 0) < 0) {
    if (errno != EINTR) {
    stat = -1;
    break;
    }
    }
    }

    return(stat);
    }

    int main(void)
    {
    pid_t pid;
    int stat;
    /*signal(SIGCHLD, sig_child);
    wait(&stat);*/

    stat = sys("/home/cdalten/oakland/am");
    /*stat = sys("/usr/bin/who");*/
    printf("%d\n", WEXITSTATUS(stat));
    /*stat = sys("/usr/bin/who");*/
    /*printf("%d\n", stat);*/

    exit(0);
    }
    [cdalten@localhost oakland]$ gcc -g child.c -o child

    The program produces a value of 127
    [cdalten@localhost oakland]$ ./child
    127

    Yet the shell shows a vaue of 0
    [cdalten@localhost oakland]$ echo $?
    0
    [cdalten@localhost oakland]$

    However, both of these are wrong because when I run the program 'am',
    I get...

    [cdalten@localhost oakland]$ ./am
    bash: ./am: Permission denied
    [cdalten@localhost oakland]$ echo $?
    126
    [cdalten@localhost oakland]$


    Maybe this poorly worded, but why does the program produce a value of
    127, but the shell shows a vaue of zero?

  2. Re: I need a technical explanation on the following shell behavior

    In article
    <144424f8-c509-4c43-b4e7-de64857dd0fa@m73g2000hsh.googlegroups.com>,
    K-mart Cashier wrote:

    > Given the following....
    >
    > #include
    > #include
    > #include
    > #include
    > #include
    > #include
    > #include
    >
    > int sys(char *cmd)
    > {
    > pid_t pid;
    > int stat;
    >
    > if ((pid = fork()) < 0) {
    > fprintf(stderr, "fork error\n");
    > exit(1);
    > } else if (pid == 0) {
    > /*execl("/home/cdalten/oakland/am", "am", "-c", cmd, (char *)0);*/
    > execl(cmd, (char *)0);
    > _exit(127);
    > } else {
    > while (waitpid(pid, &stat, 0) < 0) {
    > if (errno != EINTR) {
    > stat = -1;
    > break;
    > }
    > }
    > }
    >
    > return(stat);
    > }
    >
    > int main(void)
    > {
    > pid_t pid;
    > int stat;
    > /*signal(SIGCHLD, sig_child);
    > wait(&stat);*/
    >
    > stat = sys("/home/cdalten/oakland/am");
    > /*stat = sys("/usr/bin/who");*/
    > printf("%d\n", WEXITSTATUS(stat));
    > /*stat = sys("/usr/bin/who");*/
    > /*printf("%d\n", stat);*/
    >
    > exit(0);
    > }
    > [cdalten@localhost oakland]$ gcc -g child.c -o child
    >
    > The program produces a value of 127
    > [cdalten@localhost oakland]$ ./child
    > 127
    >
    > Yet the shell shows a vaue of 0
    > [cdalten@localhost oakland]$ echo $?
    > 0
    > [cdalten@localhost oakland]$
    >
    > However, both of these are wrong because when I run the program 'am',
    > I get...
    >
    > [cdalten@localhost oakland]$ ./am
    > bash: ./am: Permission denied
    > [cdalten@localhost oakland]$ echo $?
    > 126
    > [cdalten@localhost oakland]$
    >
    >
    > Maybe this poorly worded, but why does the program produce a value of
    > 127, but the shell shows a vaue of zero?


    Because your program ends with exit(0), not exit(stat).

    --
    Barry Margolin, barmar@alum.mit.edu
    Arlington, MA
    *** PLEASE post questions in newsgroups, not directly to me ***
    *** PLEASE don't copy me on replies, I'll read them in the group ***

  3. Re: I need a technical explanation on the following shell behavior

    On Sep 19, 8:11 pm, Barry Margolin wrote:
    > In article
    > <144424f8-c509-4c43-b4e7-de64857dd...@m73g2000hsh.googlegroups.com>,
    > K-mart Cashier wrote:
    >
    >
    >
    > > Given the following....

    >
    > > #include
    > > #include
    > > #include
    > > #include
    > > #include
    > > #include
    > > #include

    >
    > > int sys(char *cmd)
    > > {
    > > pid_t pid;
    > > int stat;

    >
    > > if ((pid = fork()) < 0) {
    > > fprintf(stderr, "fork error\n");
    > > exit(1);
    > > } else if (pid == 0) {
    > > /*execl("/home/cdalten/oakland/am", "am", "-c", cmd, (char *)0);*/
    > > execl(cmd, (char *)0);
    > > _exit(127);
    > > } else {
    > > while (waitpid(pid, &stat, 0) < 0) {
    > > if (errno != EINTR) {
    > > stat = -1;
    > > break;
    > > }
    > > }
    > > }

    >
    > > return(stat);
    > > }

    >
    > > int main(void)
    > > {
    > > pid_t pid;
    > > int stat;
    > > /*signal(SIGCHLD, sig_child);
    > > wait(&stat);*/

    >
    > > stat = sys("/home/cdalten/oakland/am");
    > > /*stat = sys("/usr/bin/who");*/
    > > printf("%d\n", WEXITSTATUS(stat));
    > > /*stat = sys("/usr/bin/who");*/
    > > /*printf("%d\n", stat);*/

    >
    > > exit(0);
    > > }
    > > [cdalten@localhost oakland]$ gcc -g child.c -o child

    >
    > > The program produces a value of 127
    > > [cdalten@localhost oakland]$ ./child
    > > 127

    >
    > > Yet the shell shows a vaue of 0
    > > [cdalten@localhost oakland]$ echo $?
    > > 0
    > > [cdalten@localhost oakland]$

    >
    > > However, both of these are wrong because when I run the program 'am',
    > > I get...

    >
    > > [cdalten@localhost oakland]$ ./am
    > > bash: ./am: Permission denied
    > > [cdalten@localhost oakland]$ echo $?
    > > 126
    > > [cdalten@localhost oakland]$

    >
    > > Maybe this poorly worded, but why does the program produce a value of
    > > 127, but the shell shows a vaue of zero?

    >
    > Because your program ends with exit(0), not exit(stat).
    >
    > --


    Okay, one last question. How come the program doesn't show the correct
    exit status for the program 'am'?


  4. Re: I need a technical explanation on the following shell behavior

    On Sep 19, 8:25 pm, K-mart Cashier wrote:
    > On Sep 19, 8:11 pm, Barry Margolin wrote:
    >
    >
    >
    > > In article
    > > <144424f8-c509-4c43-b4e7-de64857dd...@m73g2000hsh.googlegroups.com>,
    > > K-mart Cashier wrote:

    >
    > > > Given the following....

    >
    > > > #include
    > > > #include
    > > > #include
    > > > #include
    > > > #include
    > > > #include
    > > > #include

    >
    > > > int sys(char *cmd)
    > > > {
    > > > pid_t pid;
    > > > int stat;

    >
    > > > if ((pid = fork()) < 0) {
    > > > fprintf(stderr, "fork error\n");
    > > > exit(1);
    > > > } else if (pid == 0) {
    > > > /*execl("/home/cdalten/oakland/am", "am", "-c", cmd, (char *)0);*/
    > > > execl(cmd, (char *)0);
    > > > _exit(127);
    > > > } else {
    > > > while (waitpid(pid, &stat, 0) < 0) {
    > > > if (errno != EINTR) {
    > > > stat = -1;
    > > > break;
    > > > }
    > > > }
    > > > }

    >
    > > > return(stat);
    > > > }

    >
    > > > int main(void)
    > > > {
    > > > pid_t pid;
    > > > int stat;
    > > > /*signal(SIGCHLD, sig_child);
    > > > wait(&stat);*/

    >
    > > > stat = sys("/home/cdalten/oakland/am");
    > > > /*stat = sys("/usr/bin/who");*/
    > > > printf("%d\n", WEXITSTATUS(stat));
    > > > /*stat = sys("/usr/bin/who");*/
    > > > /*printf("%d\n", stat);*/

    >
    > > > exit(0);
    > > > }
    > > > [cdalten@localhost oakland]$ gcc -g child.c -o child

    >
    > > > The program produces a value of 127
    > > > [cdalten@localhost oakland]$ ./child
    > > > 127

    >
    > > > Yet the shell shows a vaue of 0
    > > > [cdalten@localhost oakland]$ echo $?
    > > > 0
    > > > [cdalten@localhost oakland]$

    >
    > > > However, both of these are wrong because when I run the program 'am',
    > > > I get...

    >
    > > > [cdalten@localhost oakland]$ ./am
    > > > bash: ./am: Permission denied
    > > > [cdalten@localhost oakland]$ echo $?
    > > > 126
    > > > [cdalten@localhost oakland]$

    >
    > > > Maybe this poorly worded, but why does the program produce a value of
    > > > 127, but the shell shows a vaue of zero?

    >
    > > Because your program ends with exit(0), not exit(stat).

    >
    > > --

    >
    > Okay, one last question. How come the program doesn't show the correct
    > exit status for the program 'am'?


    Ie, how come it doesnt show 127 + code>?

  5. Re: I need a technical explanation on the following shell behavior

    On 20 Sep, 04:32, K-mart Cashier wrote:
    > On Sep 19, 8:25 pm, K-mart Cashier wrote:
    > > On Sep 19, 8:11 pm, Barry Margolin wrote:
    > > > *K-mart Cashier wrote:

    >
    > > > > #include
    > > > > #include
    > > > > #include
    > > > > #include
    > > > > #include
    > > > > #include
    > > > > #include

    >
    > > > > int sys(char *cmd)
    > > > > {
    > > > > * pid_t pid;
    > > > > * int stat;

    >
    > > > > * if ((pid = fork()) < 0) {
    > > > > * * fprintf(stderr, "fork error\n");
    > > > > * * exit(1);
    > > > > * } else if (pid == 0) {
    > > > > * * /*execl("/home/cdalten/oakland/am", "am", "-c", cmd, (char *)0);*/
    > > > > * * execl(cmd, (char *)0);
    > > > > * * _exit(127);
    > > > > * } else {
    > > > > * * while (waitpid(pid, &stat, 0) < 0) {
    > > > > * * * if (errno != EINTR) {
    > > > > * * * * stat = -1;
    > > > > * * * * break;
    > > > > * * * }
    > > > > * * }
    > > > > * }

    >
    > > > > * return(stat);
    > > > > }

    >
    > > > > int main(void)
    > > > > {
    > > > > * pid_t pid;
    > > > > * int stat;
    > > > > * /*signal(SIGCHLD, sig_child);
    > > > > * wait(&stat);*/

    >
    > > > > * stat = sys("/home/cdalten/oakland/am");
    > > > > * /*stat = sys("/usr/bin/who");*/
    > > > > * printf("%d\n", WEXITSTATUS(stat));
    > > > > * /*stat = sys("/usr/bin/who");*/
    > > > > * /*printf("%d\n", stat);*/

    >
    > > > > * exit(0);
    > > > > }
    > > > > [cdalten@localhost oakland]$ gcc -g child.c -o child

    >
    > > > > The program produces a value of 127
    > > > > [cdalten@localhost oakland]$ ./child
    > > > > 127

    >
    > > > > Yet the shell shows a vaue of 0
    > > > > [cdalten@localhost oakland]$ echo $?
    > > > > 0
    > > > > [cdalten@localhost oakland]$

    >
    > > > > However, both of these are wrong because when I run the program 'am',
    > > > > I get...

    >
    > > > > [cdalten@localhost oakland]$ ./am
    > > > > bash: ./am: Permission denied
    > > > > [cdalten@localhost oakland]$ echo $?
    > > > > 126
    > > > > [cdalten@localhost oakland]$

    >
    > > > > Maybe this poorly worded, but why does the program produce a value of
    > > > > 127, but the shell shows a vaue of zero?

    >
    > > > Because your program ends with exit(0), not exit(stat).

    >
    > > > --

    >
    > > Okay, one last question. How come the program doesn't show the correct
    > > exit status for the program 'am'?

    >
    > Ie, how come it doesnt show 127 + > code>?


    Does execl succeed? If not, your _exit(127). Since
    you're printing 127 and bash claims you don't
    have permission, I would suspect that execl is not
    successful.

    Add a perror() after the execl and see what happens.


  6. Re: I need a technical explanation on the following shell behavior

    On Sep 20, 5:12 am, William Pursell wrote:
    > On 20 Sep, 04:32, K-mart Cashier wrote:
    >
    >
    >
    > > On Sep 19, 8:25 pm, K-mart Cashier wrote:
    > > > On Sep 19, 8:11 pm, Barry Margolin wrote:
    > > > > K-mart Cashier wrote:

    >
    > > > > > #include
    > > > > > #include
    > > > > > #include
    > > > > > #include
    > > > > > #include
    > > > > > #include
    > > > > > #include

    >
    > > > > > int sys(char *cmd)
    > > > > > {
    > > > > > pid_t pid;
    > > > > > int stat;

    >
    > > > > > if ((pid = fork()) < 0) {
    > > > > > fprintf(stderr, "fork error\n");
    > > > > > exit(1);
    > > > > > } else if (pid == 0) {
    > > > > > /*execl("/home/cdalten/oakland/am", "am", "-c", cmd, (char *)0);*/
    > > > > > execl(cmd, (char *)0);
    > > > > > _exit(127);
    > > > > > } else {
    > > > > > while (waitpid(pid, &stat, 0) < 0) {
    > > > > > if (errno != EINTR) {
    > > > > > stat = -1;
    > > > > > break;
    > > > > > }
    > > > > > }
    > > > > > }

    >
    > > > > > return(stat);
    > > > > > }

    >
    > > > > > int main(void)
    > > > > > {
    > > > > > pid_t pid;
    > > > > > int stat;
    > > > > > /*signal(SIGCHLD, sig_child);
    > > > > > wait(&stat);*/

    >
    > > > > > stat = sys("/home/cdalten/oakland/am");
    > > > > > /*stat = sys("/usr/bin/who");*/
    > > > > > printf("%d\n", WEXITSTATUS(stat));
    > > > > > /*stat = sys("/usr/bin/who");*/
    > > > > > /*printf("%d\n", stat);*/

    >
    > > > > > exit(0);
    > > > > > }
    > > > > > [cdalten@localhost oakland]$ gcc -g child.c -o child

    >
    > > > > > The program produces a value of 127
    > > > > > [cdalten@localhost oakland]$ ./child
    > > > > > 127

    >
    > > > > > Yet the shell shows a vaue of 0
    > > > > > [cdalten@localhost oakland]$ echo $?
    > > > > > 0
    > > > > > [cdalten@localhost oakland]$

    >
    > > > > > However, both of these are wrong because when I run the program 'am',
    > > > > > I get...

    >
    > > > > > [cdalten@localhost oakland]$ ./am
    > > > > > bash: ./am: Permission denied
    > > > > > [cdalten@localhost oakland]$ echo $?
    > > > > > 126
    > > > > > [cdalten@localhost oakland]$

    >
    > > > > > Maybe this poorly worded, but why does the program produce a value of
    > > > > > 127, but the shell shows a vaue of zero?

    >
    > > > > Because your program ends with exit(0), not exit(stat).

    >
    > > > > --

    >
    > > > Okay, one last question. How come the program doesn't show the correct
    > > > exit status for the program 'am'?

    >
    > > Ie, how come it doesnt show 127 + > > code>?

    >
    > Does execl succeed? If not, your _exit(127). Since
    > you're printing 127 and bash claims you don't
    > have permission, I would suspect that execl is not
    > successful.
    >
    > Add a perror() after the execl and see what happens.



    Here is what happens when I add perror()

    [cdalten@localhost oakland]$ more child.c
    #include
    #include
    #include
    #include
    #include
    #include
    #include

    int sys(char *cmd)
    {
    pid_t pid;
    int stat;

    if ((pid = fork()) < 0) {
    fprintf(stderr, "fork error\n");
    exit(1);
    } else if (pid == 0) {
    /*execl("/bin/sh", "sh", "-c", cmd, (char *)0);*/
    execl(cmd, (char *)0);
    perror("error:");
    _exit(127);
    } else {
    while (waitpid(pid, &stat, 0) < 0) {
    if (errno != EINTR) {
    stat = -1;
    break;
    }
    }
    }

    return(stat);
    }

    int main(void)
    {
    pid_t pid;
    int stat;
    /*signal(SIGCHLD, sig_child);
    wait(&stat);*/

    stat = sys("/home/cdalten/oakland/am");
    /*stat = sys("/usr/bin/who");*/
    printf("%d\n", WEXITSTATUS(stat));
    /*stat = sys("/usr/bin/who");*/
    /*printf("%d\n", stat);*/

    exit(0);
    }
    [cdalten@localhost oakland]$ ./child
    error:: Permission denied
    127
    [cdalten@localhost oakland]$

    When excel() fails, it returns -1. This means the program 'child'
    should have printed back 126 and not 127. Why does the program 'child'
    show 127 and not 126?

  7. Re: I need a technical explanation on the following shell behavior

    On 20 Sep, 15:55, K-mart Cashier wrote:

    > [cdalten@localhost oakland]$ more child.c
    > #include
    > #include
    > #include
    > #include
    > #include
    > #include
    > #include
    >
    > int sys(char *cmd)
    > {
    > * pid_t pid;
    > * int stat;
    >
    > * if ((pid = fork()) < 0) {
    > * * fprintf(stderr, "fork error\n");
    > * * exit(1);
    > * } else if (pid == 0) {
    > * * /*execl("/bin/sh", "sh", "-c", cmd, (char *)0);*/
    > * * execl(cmd, (char *)0);
    > * * perror("error:");
    > * * _exit(127);
    > * } else {
    > * * while (waitpid(pid, &stat, 0) < 0) {
    > * * * if (errno != EINTR) {
    > * * * * stat = -1;
    > * * * * break;
    > * * * }
    > * * }
    > * }
    >
    > * return(stat);
    >
    > }
    >
    > int main(void)
    > {
    > * pid_t pid;
    > * int stat;
    > * /*signal(SIGCHLD, sig_child);
    > * wait(&stat);*/
    >
    > * stat = sys("/home/cdalten/oakland/am");
    > * /*stat = sys("/usr/bin/who");*/
    > * printf("%d\n", WEXITSTATUS(stat));
    > * /*stat = sys("/usr/bin/who");*/
    > * /*printf("%d\n", stat);*/
    >
    > * exit(0);}
    >
    > [cdalten@localhost oakland]$ ./child
    > error:: Permission denied
    > 127
    > [cdalten@localhost oakland]$
    >
    > When excel() fails, it returns -1. This means the program 'child'
    > should have printed back 126 and not 127. Why does the program 'child'
    > show 127 and not 126?


    Why do you think 'child' should print 126? In the child, execl fails
    and returns -1. Then you see the 'Permission denied' message.
    Then the child calls _exit with an argument of 127, which
    the parent call to sys() returns and WEXITSTATUS of
    127 is sent to printf...which is 127. So 127 is
    printed. Perhaps if you instead did:

    v = execl( ... )
    _exit( 127 + v );

    but that would be odd.

  8. Re: I need a technical explanation on the following shell behavior

    On Sep 20, 8:28 am, William Pursell wrote:
    > On 20 Sep, 15:55, K-mart Cashier wrote:
    >
    >
    >
    > > [cdalten@localhost oakland]$ more child.c
    > > #include
    > > #include
    > > #include
    > > #include
    > > #include
    > > #include
    > > #include

    >
    > > int sys(char *cmd)
    > > {
    > > pid_t pid;
    > > int stat;

    >
    > > if ((pid = fork()) < 0) {
    > > fprintf(stderr, "fork error\n");
    > > exit(1);
    > > } else if (pid == 0) {
    > > /*execl("/bin/sh", "sh", "-c", cmd, (char *)0);*/
    > > execl(cmd, (char *)0);
    > > perror("error:");
    > > _exit(127);
    > > } else {
    > > while (waitpid(pid, &stat, 0) < 0) {
    > > if (errno != EINTR) {
    > > stat = -1;
    > > break;
    > > }
    > > }
    > > }

    >
    > > return(stat);

    >
    > > }

    >
    > > int main(void)
    > > {
    > > pid_t pid;
    > > int stat;
    > > /*signal(SIGCHLD, sig_child);
    > > wait(&stat);*/

    >
    > > stat = sys("/home/cdalten/oakland/am");
    > > /*stat = sys("/usr/bin/who");*/
    > > printf("%d\n", WEXITSTATUS(stat));
    > > /*stat = sys("/usr/bin/who");*/
    > > /*printf("%d\n", stat);*/

    >
    > > exit(0);}

    >
    > > [cdalten@localhost oakland]$ ./child
    > > error:: Permission denied
    > > 127
    > > [cdalten@localhost oakland]$

    >
    > > When excel() fails, it returns -1. This means the program 'child'
    > > should have printed back 126 and not 127. Why does the program 'child'
    > > show 127 and not 126?

    >
    > Why do you think 'child' should print 126? In the child, execl fails
    > and returns -1. Then you see the 'Permission denied' message.
    > Then the child calls _exit with an argument of 127, which
    > the parent call to sys() returns and WEXITSTATUS of
    > 127 is sent to printf...which is 127. So 127 is
    > printed. Perhaps if you instead did:
    >
    > v = execl( ... )
    > _exit( 127 + v );
    >
    > but that would be odd.



    Well, when I modify excel like the following

    execl("/bin/sh", "sh", "-c", cmd, (char *)0);


    And then run it...

    #include
    #include
    #include
    #include
    #include
    #include
    #include

    int sys(char *cmd)
    {
    pid_t pid;
    int stat;

    if ((pid = fork()) < 0) {
    fprintf(stderr, "fork error\n");
    exit(1);
    } else if (pid == 0) {
    execl("/bin/sh", "sh", "-c", cmd, (char *)0);
    /*execl(cmd, (char *)0);*/
    perror("error:");
    _exit(127);
    } else {
    while (waitpid(pid, &stat, 0) < 0) {
    if (errno != EINTR) {
    stat = -1;
    break;
    }
    }
    }

    return(stat);
    }

    int main(void)
    {
    pid_t pid;
    int stat;
    /*signal(SIGCHLD, sig_child);
    wait(&stat);*/

    stat = sys("/home/cdalten/oakland/am");
    /*stat = sys("/usr/bin/who");*/
    printf("%d\n", WEXITSTATUS(stat));
    /*stat = sys("/usr/bin/who");*/
    /*printf("%d\n", stat);*/

    exit(0);
    }
    [cdalten@localhost oakland]$ gcc -g child.c -o child
    [cdalten@localhost oakland]$ ./child
    sh: /home/cdalten/oakland/am: Permission denied
    126
    [cdalten@localhost oakland]$

    The program 'child' does indeed produce 126. How come the call to /bin/
    bsh in excel() actually produces 127 + failure status?

  9. Re: I need a technical explanation on the following shell behavior

    On Sep 20, 9:03 am, K-mart Cashier wrote:
    > On Sep 20, 8:28 am, William Pursell wrote:
    >
    >
    >
    > > On 20 Sep, 15:55, K-mart Cashier wrote:

    >
    > > > [cdalten@localhost oakland]$ more child.c
    > > > #include
    > > > #include
    > > > #include
    > > > #include
    > > > #include
    > > > #include
    > > > #include

    >
    > > > int sys(char *cmd)
    > > > {
    > > > pid_t pid;
    > > > int stat;

    >
    > > > if ((pid = fork()) < 0) {
    > > > fprintf(stderr, "fork error\n");
    > > > exit(1);
    > > > } else if (pid == 0) {
    > > > /*execl("/bin/sh", "sh", "-c", cmd, (char *)0);*/
    > > > execl(cmd, (char *)0);
    > > > perror("error:");
    > > > _exit(127);
    > > > } else {
    > > > while (waitpid(pid, &stat, 0) < 0) {
    > > > if (errno != EINTR) {
    > > > stat = -1;
    > > > break;
    > > > }
    > > > }
    > > > }

    >
    > > > return(stat);

    >
    > > > }

    >
    > > > int main(void)
    > > > {
    > > > pid_t pid;
    > > > int stat;
    > > > /*signal(SIGCHLD, sig_child);
    > > > wait(&stat);*/

    >
    > > > stat = sys("/home/cdalten/oakland/am");
    > > > /*stat = sys("/usr/bin/who");*/
    > > > printf("%d\n", WEXITSTATUS(stat));
    > > > /*stat = sys("/usr/bin/who");*/
    > > > /*printf("%d\n", stat);*/

    >
    > > > exit(0);}

    >
    > > > [cdalten@localhost oakland]$ ./child
    > > > error:: Permission denied
    > > > 127
    > > > [cdalten@localhost oakland]$

    >
    > > > When excel() fails, it returns -1. This means the program 'child'
    > > > should have printed back 126 and not 127. Why does the program 'child'
    > > > show 127 and not 126?

    >
    > > Why do you think 'child' should print 126? In the child, execl fails
    > > and returns -1. Then you see the 'Permission denied' message.
    > > Then the child calls _exit with an argument of 127, which
    > > the parent call to sys() returns and WEXITSTATUS of
    > > 127 is sent to printf...which is 127. So 127 is
    > > printed. Perhaps if you instead did:

    >
    > > v = execl( ... )
    > > _exit( 127 + v );

    >
    > > but that would be odd.

    >
    > Well, when I modify excel like the following
    >
    > execl("/bin/sh", "sh", "-c", cmd, (char *)0);
    >
    > And then run it...
    >
    > #include
    > #include
    > #include
    > #include
    > #include
    > #include
    > #include
    >
    > int sys(char *cmd)
    > {
    > pid_t pid;
    > int stat;
    >
    > if ((pid = fork()) < 0) {
    > fprintf(stderr, "fork error\n");
    > exit(1);
    > } else if (pid == 0) {
    > execl("/bin/sh", "sh", "-c", cmd, (char *)0);
    > /*execl(cmd, (char *)0);*/
    > perror("error:");
    > _exit(127);
    > } else {
    > while (waitpid(pid, &stat, 0) < 0) {
    > if (errno != EINTR) {
    > stat = -1;
    > break;
    > }
    > }
    > }
    >
    > return(stat);
    >
    > }
    >
    > int main(void)
    > {
    > pid_t pid;
    > int stat;
    > /*signal(SIGCHLD, sig_child);
    > wait(&stat);*/
    >
    > stat = sys("/home/cdalten/oakland/am");
    > /*stat = sys("/usr/bin/who");*/
    > printf("%d\n", WEXITSTATUS(stat));
    > /*stat = sys("/usr/bin/who");*/
    > /*printf("%d\n", stat);*/
    >
    > exit(0);}
    >
    > [cdalten@localhost oakland]$ gcc -g child.c -o child
    > [cdalten@localhost oakland]$ ./child
    > sh: /home/cdalten/oakland/am: Permission denied
    > 126
    > [cdalten@localhost oakland]$
    >
    > The program 'child' does indeed produce 126. How come the call to /bin/
    > bsh in excel() actually produces 127 + failure status?



    I meant /bin/sh and not /bin/bsh

  10. Re: I need a technical explanation on the following shell behavior

    On Sat, 20 Sep 2008 09:07:02 -0700, K-mart Cashier wrote:

    > On Sep 20, 9:03 am, K-mart Cashier wrote:
    >> On Sep 20, 8:28 am, William Pursell wrote:
    >>
    >>


    >> > to sys() returns and WEXITSTATUS of 127 is sent to printf...which is
    >> > 127. So 127 is printed. Perhaps if you instead did:

    >>
    >> > v = execl( ... )
    >> > _exit( 127 + v );

    >>
    >> > but that would be odd.

    >>
    >> Well, when I modify excel like the following
    >>
    >> execl("/bin/sh", "sh", "-c", cmd, (char *)0);
    >>
    >> And then run it...
    >>
    >> #include
    >> #include
    >> #include
    >> #include
    >> #include
    >> #include
    >> #include
    >>
    >> int sys(char *cmd)
    >> {
    >> pid_t pid;
    >> int stat;
    >>
    >> if ((pid = fork()) < 0) {
    >> fprintf(stderr, "fork error\n");
    >> exit(1);
    >> } else if (pid == 0) {
    >> execl("/bin/sh", "sh", "-c", cmd, (char *)0); /*execl(cmd, (char
    >> *)0);*/
    >> perror("error:");
    >> _exit(127);
    >> } else {
    >> while (waitpid(pid, &stat, 0) < 0) {
    >> if (errno != EINTR) {
    >> stat = -1;
    >> break;
    >> }
    >> }
    >> }
    >>
    >> return(stat);
    >>
    >> }
    >>
    >> int main(void)
    >> {
    >> pid_t pid;
    >> int stat;
    >> /*signal(SIGCHLD, sig_child);
    >> wait(&stat);*/
    >>
    >> stat = sys("/home/cdalten/oakland/am"); /*stat =
    >> sys("/usr/bin/who");*/
    >> printf("%d\n", WEXITSTATUS(stat));
    >> /*stat = sys("/usr/bin/who");*/
    >> /*printf("%d\n", stat);*/
    >>
    >> exit(0);}
    >>
    >> [cdalten@localhost oakland]$ gcc -g child.c -o child [cdalten@localhost
    >> oakland]$ ./child
    >> sh: /home/cdalten/oakland/am: Permission denied 126
    >> [cdalten@localhost oakland]$
    >>
    >> The program 'child' does indeed produce 126. How come the call to /bin/
    >> bsh in excel() actually produces 127 + failure status?

    >
    >
    > I meant /bin/sh and not /bin/bsh

    From TFM:

    EXIT STATUS
    For the shell’s purposes, a command which exits with a zero exit
    status has succeeded. An exit status of zero indicates success. A
    non-zero exit status indicates failure. When a command terminates
    on a fatal signal N, bash uses the value of 128+N as the exit sta-
    tus.

    If a command is not found, the child process created to execute
    it returns a status of 127. If a command is found but is not exe-
    cutable, the return status is 126.

    HTH,
    AvK

  11. Re: I need a technical explanation on the following shell behavior

    On Sep 20, 9:45 am, Moi wrote:
    > On Sat, 20 Sep 2008 09:07:02 -0700, K-mart Cashier wrote:
    > > On Sep 20, 9:03 am, K-mart Cashier wrote:
    > >> On Sep 20, 8:28 am, William Pursell wrote:

    >
    > >> > to sys() returns and WEXITSTATUS of 127 is sent to printf...which is
    > >> > 127. So 127 is printed. Perhaps if you instead did:

    >
    > >> > v = execl( ... )
    > >> > _exit( 127 + v );

    >
    > >> > but that would be odd.

    >
    > >> Well, when I modify excel like the following

    >
    > >> execl("/bin/sh", "sh", "-c", cmd, (char *)0);

    >
    > >> And then run it...

    >
    > >> #include
    > >> #include
    > >> #include
    > >> #include
    > >> #include
    > >> #include
    > >> #include

    >
    > >> int sys(char *cmd)
    > >> {
    > >> pid_t pid;
    > >> int stat;

    >
    > >> if ((pid = fork()) < 0) {
    > >> fprintf(stderr, "fork error\n");
    > >> exit(1);
    > >> } else if (pid == 0) {
    > >> execl("/bin/sh", "sh", "-c", cmd, (char *)0); /*execl(cmd, (char
    > >> *)0);*/
    > >> perror("error:");
    > >> _exit(127);
    > >> } else {
    > >> while (waitpid(pid, &stat, 0) < 0) {
    > >> if (errno != EINTR) {
    > >> stat = -1;
    > >> break;
    > >> }
    > >> }
    > >> }

    >
    > >> return(stat);

    >
    > >> }

    >
    > >> int main(void)
    > >> {
    > >> pid_t pid;
    > >> int stat;
    > >> /*signal(SIGCHLD, sig_child);
    > >> wait(&stat);*/

    >
    > >> stat = sys("/home/cdalten/oakland/am"); /*stat =
    > >> sys("/usr/bin/who");*/
    > >> printf("%d\n", WEXITSTATUS(stat));
    > >> /*stat = sys("/usr/bin/who");*/
    > >> /*printf("%d\n", stat);*/

    >
    > >> exit(0);}

    >
    > >> [cdalten@localhost oakland]$ gcc -g child.c -o child [cdalten@localhost
    > >> oakland]$ ./child
    > >> sh: /home/cdalten/oakland/am: Permission denied 126
    > >> [cdalten@localhost oakland]$

    >
    > >> The program 'child' does indeed produce 126. How come the call to /bin/
    > >> bsh in excel() actually produces 127 + failure status?

    >
    > > I meant /bin/sh and not /bin/bsh

    >
    > From TFM:
    >
    > EXIT STATUS
    > For the shell’s purposes, a command which exits with a zero exit
    > status has succeeded. An exit status of zero indicates success. A
    > non-zero exit status indicates failure. When a command terminates
    > on a fatal signal N, bash uses the value of 128+N as the exit sta-
    > tus.
    >
    > If a command is not found, the child process created to execute
    > it returns a status of 127. If a command is found but is not exe-
    > cutable, the return status is 126.
    >



    But when I added perror to the original code, the program 'child'
    produced


    [cdalten@localhost oakland]$ ./child
    error:: Permission denied
    127
    [cdalten@localhost oakland]$

    If the command wasn't found, the how come my computer shows error::
    Permission denied? I clearly had to find something in order to
    produced this error message.

  12. Re: I need a technical explanation on the following shell behavior

    On 2008-09-20, K-mart Cashier wrote:
    > On Sep 20, 9:45 am, Moi wrote:
    >> On Sat, 20 Sep 2008 09:07:02 -0700, K-mart Cashier wrote:
    >> > On Sep 20, 9:03 am, K-mart Cashier wrote:
    >> >> On Sep 20, 8:28 am, William Pursell wrote:

    >>
    >> >> > to sys() returns and WEXITSTATUS of 127 is sent to printf...which is
    >> >> > 127. So 127 is printed. Perhaps if you instead did:

    >>
    >> >> > v = execl( ... )
    >> >> > _exit( 127 + v );

    >>
    >> >> > but that would be odd.

    >>
    >> >> Well, when I modify excel like the following

    >>
    >> >> execl("/bin/sh", "sh", "-c", cmd, (char *)0);

    >>
    >> >> And then run it...

    >>
    >> >> #include
    >> >> #include
    >> >> #include
    >> >> #include
    >> >> #include
    >> >> #include
    >> >> #include

    >>
    >> >> int sys(char *cmd)
    >> >> {
    >> >> pid_t pid;
    >> >> int stat;

    >>
    >> >> if ((pid = fork()) < 0) {
    >> >> fprintf(stderr, "fork error\n");
    >> >> exit(1);
    >> >> } else if (pid == 0) {
    >> >> execl("/bin/sh", "sh", "-c", cmd, (char *)0); /*execl(cmd, (char
    >> >> *)0);*/
    >> >> perror("error:");
    >> >> _exit(127);
    >> >> } else {
    >> >> while (waitpid(pid, &stat, 0) < 0) {
    >> >> if (errno != EINTR) {
    >> >> stat = -1;
    >> >> break;
    >> >> }
    >> >> }
    >> >> }

    >>
    >> >> return(stat);

    >>
    >> >> }

    >>
    >> >> int main(void)
    >> >> {
    >> >> pid_t pid;
    >> >> int stat;
    >> >> /*signal(SIGCHLD, sig_child);
    >> >> wait(&stat);*/

    >>
    >> >> stat = sys("/home/cdalten/oakland/am"); /*stat =
    >> >> sys("/usr/bin/who");*/
    >> >> printf("%d\n", WEXITSTATUS(stat));
    >> >> /*stat = sys("/usr/bin/who");*/
    >> >> /*printf("%d\n", stat);*/

    >>
    >> >> exit(0);}

    >>
    >> >> [cdalten@localhost oakland]$ gcc -g child.c -o child [cdalten@localhost
    >> >> oakland]$ ./child
    >> >> sh: /home/cdalten/oakland/am: Permission denied 126
    >> >> [cdalten@localhost oakland]$

    >>
    >> >> The program 'child' does indeed produce 126. How come the call to /bin/
    >> >> bsh in excel() actually produces 127 + failure status?

    >>
    >> > I meant /bin/sh and not /bin/bsh

    >>
    >> From TFM:
    >>
    >> EXIT STATUS
    >> For the shell?s purposes, a command which exits with a zero exit
    >> status has succeeded. An exit status of zero indicates success. A
    >> non-zero exit status indicates failure. When a command terminates
    >> on a fatal signal N, bash uses the value of 128+N as the exit sta-
    >> tus.
    >>
    >> If a command is not found, the child process created to execute
    >> it returns a status of 127. If a command is found but is not exe-
    >> cutable, the return status is 126.
    >>

    >
    >
    > But when I added perror to the original code, the program 'child'
    > produced
    >
    >
    > [cdalten@localhost oakland]$ ./child
    > error:: Permission denied
    > 127
    > [cdalten@localhost oakland]$
    >
    > If the command wasn't found, the how come my computer shows error::
    > Permission denied? I clearly had to find something in order to
    > produced this error message.


    Because in the original code, you never /used/ the return value
    of execl. You just exited with the hardcoded value 127.

    --
    Andrew Poelstra apoelstra@wpsoftware.net
    That was a joke. Jokes in mathematics, are sometimes not funny.
    -Veselin Jungic

  13. Re: I need a technical explanation on the following shell behavior

    On 20 Sep, 18:39, K-mart Cashier wrote:
    > On Sep 20, 9:45 am, Moi wrote:
    >
    >
    >
    > > On Sat, 20 Sep 2008 09:07:02 -0700, K-mart Cashier wrote:
    > > > On Sep 20, 9:03 am, K-mart Cashier wrote:
    > > >> On Sep 20, 8:28 am, William Pursell wrote:

    >
    > > >> > to sys() returns and WEXITSTATUS of 127 is sent to printf...which is
    > > >> > 127. *So 127 is printed. *Perhaps if you instead did:

    >
    > > >> > v = execl( ... )
    > > >> > _exit( 127 + v );

    >
    > > >> > but that would be odd.

    >
    > > >> Well, when I modify excel like the following

    >
    > > >> execl("/bin/sh", "sh", "-c", cmd, (char *)0);

    >
    > > >> And then run it...

    >
    > > >> #include
    > > >> #include
    > > >> #include
    > > >> #include
    > > >> #include
    > > >> #include
    > > >> #include

    >
    > > >> int sys(char *cmd)
    > > >> {
    > > >> * pid_t pid;
    > > >> * int stat;

    >
    > > >> * if ((pid = fork()) < 0) {
    > > >> * * fprintf(stderr, "fork error\n");
    > > >> * * exit(1);
    > > >> * } else if (pid == 0) {
    > > >> * * execl("/bin/sh", "sh", "-c", cmd, (char *)0); /*execl(cmd, (char
    > > >> * * *)0);*/
    > > >> * * perror("error:");
    > > >> * * _exit(127);
    > > >> * } else {
    > > >> * * while (waitpid(pid, &stat, 0) < 0) {
    > > >> * * * if (errno != EINTR) {
    > > >> * * * * stat = -1;
    > > >> * * * * break;
    > > >> * * * }
    > > >> * * }
    > > >> * }

    >
    > > >> * return(stat);

    >
    > > >> }

    >
    > > >> int main(void)
    > > >> {
    > > >> * pid_t pid;
    > > >> * int stat;
    > > >> * /*signal(SIGCHLD, sig_child);
    > > >> * wait(&stat);*/

    >
    > > >> * stat = sys("/home/cdalten/oakland/am"); /*stat =
    > > >> * sys("/usr/bin/who");*/
    > > >> * printf("%d\n", WEXITSTATUS(stat));
    > > >> * /*stat = sys("/usr/bin/who");*/
    > > >> * /*printf("%d\n", stat);*/

    >
    > > >> * exit(0);}

    >
    > > >> [cdalten@localhost oakland]$ gcc -g child.c -o child [cdalten@localhost
    > > >> oakland]$ ./child
    > > >> sh: /home/cdalten/oakland/am: Permission denied 126
    > > >> [cdalten@localhost oakland]$

    >
    > > >> The program 'child' does indeed produce 126. How come the call to /bin/
    > > >> bsh in excel() actually produces 127 + failure status?

    >
    > > > I meant /bin/sh and not /bin/bsh

    >
    > > From TFM:

    >
    > > EXIT STATUS
    > > * * * *For *the *shell’s purposes, a command which exits with a zero exit
    > > status has succeeded. *An exit status of zero indicates success. *A
    > > * * * *non-zero exit status indicates failure. *When a command terminates
    > > on a fatal signal N, bash uses the value of 128+N as the exit *sta-
    > > * * * *tus.

    >
    > > * * * *If *a *command *is *not found, the child processcreated to execute
    > > it returns a status of 127. *If a command is found but is not exe-
    > > * * * *cutable, the return status is 126.

    >
    > But when I added perror to the original code, the program 'child'
    > produced
    >
    > [cdalten@localhost oakland]$ ./child
    > error:: Permission denied
    > 127
    > [cdalten@localhost oakland]$
    >
    > If the command wasn't found, the how come my computer shows error::
    > Permission denied? I clearly had to find something in order to
    > produced this error message.



    The execl failed and set errno. Your code called
    perror which printed the message.

    All that's happening is that your code is exiting
    with a parameter of 127, but the shell is setting
    $? to 126. If you look in the bash source, you'll
    see the following:

    execute_cmd.c: return (EX_NOEXEC);
    execute_cmd.c: return ((i == ENOENT) ? EX_NOTFOUND : EX_NOEXEC); /
    * XXX Posix.2 says that exit status is 126 */
    shell.c: exit (EX_NOEXEC);
    shell.h:#define EX_NOEXEC 126

+ Reply to Thread