Home  >  Q&A  >  body text

linux下execvp失败设置errno=7的原因是什么?

伊谢尔伦伊谢尔伦2741 days ago688

reply all(4)I'll reply

  • 迷茫

    迷茫2017-04-17 15:32:43

    Thank you.
    But none of the respondents gave the correct answer. It’s my fault that I didn’t provide all the information.
    In fact, there is nothing wrong with the code itself, but there is a section setrlimit in front of it to limit memory usage. Due to a configuration error, the upper limit of memory is too small, so that the string in argv cannot even be stored.
    The error is ridiculous.
    The error is ridiculous.
    The error is ridiculous.
    But at least we know that argv is not occupying the memory of the current process. :P

    reply
    0
  • 高洛峰

    高洛峰2017-04-17 15:32:43

    Look at the code below

    int main(int c,char** v)
    {
        v[c-1] = NULL;
        execvp("ls",v);
        perror("execvp");
    }

    Compile and run, the output is as follows

    > ./runner / / 
    bin  boot  dev    etc  home  lib    lib64  lost+found  mnt    opt  proc  root  run  sbin  srv  sys  tmp  usr    var

    Modify the code so that the parameters of execvp do not end with NULL

    int main(int c,char** v)
    {
        v[c] = "123";
        execvp("ls",v);
        perror("execvp");
    }

    Compile and run

    > ./runner 
    ./runner: 无法访问'123': 没有那个文件或目录
    ./runner: 无法访问'USER=o': 没有那个文件或目录
    ./runner: 无法访问'LOGNAME=o': 没有那个文件或目录
    ./runner: 无法访问'HOME=/home/o': 没有那个文件或目录
    ./runner: 无法访问'PATH=/usr/local/sbin:/usr/local/bin:/usr/bin:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl:/home/o/program/open64-5.0/bin': 没有那个文件或目录
    ./runner: 无法访问'MAIL=/var/spool/mail/o': 没有那个文件或目录
    ./runner: 无法访问'SHELL=/usr/bin/zsh': 没有那个文件或目录
    ./runner: 无法访问'SSH_CLIENT=192.168.0.25 22975 22': 没有那个文件或目录
    ./runner: 无法访问'SSH_CONNECTION=192.168.0.25 22975 192.168.0.241 22': 没有那个文件或目录
    ./runner: 无法访问'SSH_TTY=/dev/pts/0': 没有那个文件或目录
    ./runner: 无法访问'TERM=xterm': 没有那个文件或目录
    ./runner: 无法访问'XDG_SESSION_ID=c6': 没有那个文件或目录
    ./runner: 无法访问'XDG_RUNTIME_DIR=/run/user/1000': 没有那个文件或目录
    ./runner: 无法访问'DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus': 没有那个文件或目录
    ./runner: 无法访问'SHLVL=1': 没有那个文件或目录
    ./runner: 无法访问'PWD=/home/o/wTest/src/linux': 没有那个文件或目录
    ./runner: 无法访问'OLDPWD=/home/o/wTest/src': 没有那个文件或目录
    ./runner: 无法访问'LANG=en_US.UTF-8': 没有那个文件或目录
    ./runner: 无法访问'ZSH=/home/o/.oh-my-zsh': 没有那个文件或目录
    ./runner: 无法访问'LC_ALL=zh_CN.UTF-8': 没有那个文件或目录
    ./runner: 无法访问'LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=00:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.m4a=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.oga=00;36:*.opus=00;36:*.spx=00;36:*.xspf=00;36:': 文件名过长
    ./runner: 无法访问'PAGER=less': 没有那个文件或目录
    ./runner: 无法访问'LESS=-R': 没有那个文件或目录
    ./runner: 无法访问'LSCOLORS=Gxfxcxdxbxegedabagacad': 没有那个文件或目录
    ./runner: 无法访问'_=/home/o/wTest/src/linux/./runner': 没有那个文件或目录

    As you can see, the current environment variables are output here.
    If your config->args does not end with NULL and you have a lot of environment variables, will this happen?


    I didn’t read your code in detail, I just briefly looked at the execvp calling part.
    You directly called the execvp function without judging whether raise was successful.
    But raise this function is used to send a signal to the executing program. In other words, as long as you call execvp here, the SIGUSR1 signal will be sent without any accident.
    Because errno is a global variable, so here, it does not mean that it is set by execvp.

    reply
    0
  • PHPz

    PHPz2017-04-17 15:32:43

    Put ab in the background. If there are still errors, it must be an error in ab execution

    reply
    0
  • ringa_lee

    ringa_lee2017-04-17 15:32:43

    Looks like the last element of the config->args pointer array is not set to NULL.

    execvp determines the end of the list based on the null pointer.

    For example:

    config->path = "/path/to/your/bin";
    config->args = {
        "abc",
        "cde",
        NULL   // 如果没有加上这个,就会出现你说的错误
    };
    
    execvp(config->path, (char * const*)config->args);

    reply
    0
  • Cancelreply