• Re: The exeve system call and the exec family of functions (was: Try to

    From hongyi.zhao@gmail.com@21:1/5 to hongy...@gmail.com on Tue Dec 14 17:12:40 2021
    On Wednesday, December 15, 2021 at 9:08:28 AM UTC+8, hongy...@gmail.com wrote:
    On Wednesday, December 15, 2021 at 5:49:14 AM UTC+8, Helmut Waitzmann wrote:
    "hongy...@gmail.com" <hongy...@gmail.com>:
    And strace can't trace the system calls and signals of this script:

    $ strace ./test.sh
    execve("./test.sh", ["./test.sh"], 0x7ffc7d8136c0 /* 102 vars */) = -1 ENOEXEC (Exec format error)
    strace: exec: Exec format error
    +++ exited with 1 +++
    An interpreter (here: shell) script is not a runable program
    image. That's why the system call execve, that is told to execute
    that script, returns an error.

    See the execve(2) and errno(3) manual pages and the POSIX error
    numbers (<https://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html#tag_15_03>)
    for what the ENOEXEC error is.

    Give that script a Shebang in the first line of it, then make it executable:

    chmod -- u+x ./test.sh

    Try again tracing it. Does ist work now?
    I've granted the execute permission to the script:

    $ ls -la test.sh
    -rwxrw-r-- 1 werner werner 29 Dec 15 09:02 test.sh

    $ strace ./test.sh
    execve("./test.sh", ["./test.sh"], 0x7ffc06199f90 /* 103 vars */) = -1 ENOEXEC (Exec format error)
    strace: exec: Exec format error
    +++ exited with 1 +++

    Additional information:

    $ cat -A test.sh
    $
    #!/bin/bash$
    echo 'Hello.'$

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From hongyi.zhao@gmail.com@21:1/5 to Helmut Waitzmann on Tue Dec 14 17:08:25 2021
    On Wednesday, December 15, 2021 at 5:49:14 AM UTC+8, Helmut Waitzmann wrote:
    "hongy...@gmail.com" <hongy...@gmail.com>:
    And strace can't trace the system calls and signals of this script:

    $ strace ./test.sh
    execve("./test.sh", ["./test.sh"], 0x7ffc7d8136c0 /* 102 vars */) = -1 ENOEXEC (Exec format error)
    strace: exec: Exec format error
    +++ exited with 1 +++
    An interpreter (here: shell) script is not a runable program
    image. That's why the system call execve, that is told to execute
    that script, returns an error.

    See the execve(2) and errno(3) manual pages and the POSIX error
    numbers (<https://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html#tag_15_03>)
    for what the ENOEXEC error is.

    Give that script a Shebang in the first line of it, then make it
    executable:

    chmod -- u+x ./test.sh

    Try again tracing it. Does ist work now?

    I've granted the execute permission to the script:

    $ ls -la test.sh
    -rwxrw-r-- 1 werner werner 29 Dec 15 09:02 test.sh

    $ strace ./test.sh
    execve("./test.sh", ["./test.sh"], 0x7ffc06199f90 /* 103 vars */) = -1 ENOEXEC (Exec format error)
    strace: exec: Exec format error
    +++ exited with 1 +++




    Let me append my ceterum censeo:

    You really should get a book about the unix kernel, not just about
    the shell. Learn the purpose and behavior of the system calls – in
    this case: the execve system call and the various wrappers for that
    system call in the C language runtime library: the exec family of
    functions.

    Understand, what the wrapper function execlp does, when given a file
    with read and execute permissions, starting with a Shebang line:
    It invokes the named interpreter, giving it the pathname of the
    given (script) file as a parameter.

    Then, get a book about the shell (or just read the shell manual page
    and the POSIX specification <https://pubs.opengroup.org/onlinepubs/9699919799/utilities/toc.html>),
    to know, what the shell does when reading and executing a shell
    script, especially: Understand, how the shell transforms a shell
    command line invoking a simple command into the corresponding execve argument vector.

    Finally, understand, why the seen "strace" behavior above follows
    from the lessions learned.

    As long as you don't understand the unix system calls provided by
    the kernel, you'll neither understand the strace error message above
    nor make progress in shell programming.

    Thank you for your comment.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From David W. Hodgins@21:1/5 to hongy...@gmail.com on Tue Dec 14 21:40:31 2021
    On Tue, 14 Dec 2021 20:12:40 -0500, hongy...@gmail.com <hongyi.zhao@gmail.com> wrote:
    $ strace ./test.sh
    execve("./test.sh", ["./test.sh"], 0x7ffc06199f90 /* 103 vars */) = -1 ENOEXEC (Exec format error)
    strace: exec: Exec format error
    +++ exited with 1 +++

    Additional information:

    $ cat -A test.sh
    $
    #!/bin/bash$
    echo 'Hello.'$

    Strange.

    [tester@x3 ~]$ cat -A bin/test
    #!/bin/bash$
    echo 'Hello.'$
    [tester@x3 ~]$ bin/test
    Hello.
    [tester@x3 ~]$ strace bin/test
    execve("bin/test", ["bin/test"], 0x7fff84f3fc70 /* 53 vars */) = 0
    brk(NULL) = 0xaf8000
    access("/etc/ld.so.preload", R_OK) = 0
    openat(AT_FDCWD, "/etc/ld.so.preload", O_RDONLY|O_CLOEXEC) = 3
    <snip rest>

    [tester@x3 ~]$ bash --version
    GNU bash, version 5.1.4(1)-release (x86_64-mageia-linux-gnu)
    Copyright (C) 2020 Free Software Foundation, Inc.
    License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

    This is free software; you are free to change and redistribute it.
    There is NO WARRANTY, to the extent permitted by law.

    Regards, Dave Hodgins

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From hongyi.zhao@gmail.com@21:1/5 to David W. Hodgins on Tue Dec 14 20:39:37 2021
    On Wednesday, December 15, 2021 at 10:40:51 AM UTC+8, David W. Hodgins wrote:
    On Tue, 14 Dec 2021 20:12:40 -0500, hongy...@gmail.com <hongy...@gmail.com> wrote:
    $ strace ./test.sh
    execve("./test.sh", ["./test.sh"], 0x7ffc06199f90 /* 103 vars */) = -1 ENOEXEC (Exec format error)
    strace: exec: Exec format error
    +++ exited with 1 +++

    Additional information:

    $ cat -A test.sh
    $
    #!/bin/bash$
    echo 'Hello.'$
    Strange.

    The culprit is the first empty line in the script. If I remove it, strace will work smoothly:

    $ cat -A test.sh
    #!/bin/bash$
    echo 'Hello.'$

    $ strace ./test.sh |& head
    execve("./test.sh", ["./test.sh"], 0x7ffdd1b43170 /* 102 vars */) = 0
    brk(NULL) = 0x55aa39c9c000
    arch_prctl(0x3001 /* ARCH_??? */, 0x7ffd34810430) = -1 EINVAL (Invalid argument)
    access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory) openat(AT_FDCWD, "/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
    fstat(3, {st_mode=S_IFREG|0644, st_size=180831, ...}) = 0
    mmap(NULL, 180831, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7fee6b896000
    close(3) = 0
    openat(AT_FDCWD, "/lib/x86_64-linux-gnu/libtinfo.so.6", O_RDONLY|O_CLOEXEC) = 3 read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\240\346\0\0\0\0\0\0"..., 832) = 832

    $ bash --version
    GNU bash, version 5.0.17(1)-release (x86_64-pc-linux-gnu)
    Copyright (C) 2019 Free Software Foundation, Inc.
    License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

    This is free software; you are free to change and redistribute it.
    There is NO WARRANTY, to the extent permitted by law.

    Regards,
    HZ


    [tester@x3 ~]$ cat -A bin/test
    #!/bin/bash$
    echo 'Hello.'$
    [tester@x3 ~]$ bin/test
    Hello.
    [tester@x3 ~]$ strace bin/test
    execve("bin/test", ["bin/test"], 0x7fff84f3fc70 /* 53 vars */) = 0
    brk(NULL) = 0xaf8000
    access("/etc/ld.so.preload", R_OK) = 0
    openat(AT_FDCWD, "/etc/ld.so.preload", O_RDONLY|O_CLOEXEC) = 3
    <snip rest>

    [tester@x3 ~]$ bash --version
    GNU bash, version 5.1.4(1)-release (x86_64-mageia-linux-gnu)
    Copyright (C) 2020 Free Software Foundation, Inc.
    License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

    This is free software; you are free to change and redistribute it.
    There is NO WARRANTY, to the extent permitted by law.

    Regards, Dave Hodgins

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Kenny McCormack@21:1/5 to David W. Hodgins on Wed Dec 15 05:42:28 2021
    In article <op.1eff7zuoa3w0dxdave@hodgins.homeip.net>,
    David W. Hodgins <dwhodgins@nomail.afraid.org> wrote:
    On Tue, 14 Dec 2021 23:39:37 -0500, hongy...@gmail.com <hongyi.zhao@gmail.com> wrote:

    On Wednesday, December 15, 2021 at 10:40:51 AM UTC+8, David W. Hodgins wrote:
    On Tue, 14 Dec 2021 20:12:40 -0500, hongy...@gmail.com <hongy...@gmail.com> wrote:
    $ strace ./test.sh
    execve("./test.sh", ["./test.sh"], 0x7ffc06199f90 /* 103 vars */) = -1 >ENOEXEC (Exec format error)
    strace: exec: Exec format error
    +++ exited with 1 +++

    Additional information:

    $ cat -A test.sh
    $
    #!/bin/bash$
    echo 'Hello.'$
    Strange.

    The culprit is the first empty line in the script. If I remove it, strace will
    work smoothly:

    Ah. Yes. I didn't notice the leading blank line. The shebang must
    be the first characters in the file.
    #! = interpreted script
    .ELF = Executable and Linking Format files
    MZ = dos executable
    etc

    Regards, Dave Hodgins

    At some point in the not too distant future, he's going to post some unexplainable result - where weird error messages are coming out at the simplest commands - and it turn out, eventually, that the problems all
    started when he smashed his hard drive with a hammer.

    ... And we'll all just say, well, see, you should have mentioned that earlier...

    -- http://www.rollingstone.com/politics/news/the-10-dumbest-things-ever-said-about-global-warming-20130619

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From David W. Hodgins@21:1/5 to hongy...@gmail.com on Wed Dec 15 00:29:49 2021
    On Tue, 14 Dec 2021 23:39:37 -0500, hongy...@gmail.com <hongyi.zhao@gmail.com> wrote:

    On Wednesday, December 15, 2021 at 10:40:51 AM UTC+8, David W. Hodgins wrote:
    On Tue, 14 Dec 2021 20:12:40 -0500, hongy...@gmail.com <hongy...@gmail.com> wrote:
    $ strace ./test.sh
    execve("./test.sh", ["./test.sh"], 0x7ffc06199f90 /* 103 vars */) = -1 ENOEXEC (Exec format error)
    strace: exec: Exec format error
    +++ exited with 1 +++

    Additional information:

    $ cat -A test.sh
    $
    #!/bin/bash$
    echo 'Hello.'$
    Strange.

    The culprit is the first empty line in the script. If I remove it, strace will work smoothly:

    Ah. Yes. I didn't notice the leading blank line. The shebang must
    be the first characters in the file.
    #! = interpreted script
    .ELF = Executable and Linking Format files
    MZ = dos executable
    etc

    Regards, Dave Hodgins

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)