exec 函数的第 0 个参数

exec 函数的第 0 个参数

以下是标准exec*功能:

int execl(const char *path, const char *arg, ...);
int execlp(const char *file, const char *arg, ...);
int execle(const char *path, const char *arg,  ..., char * const envp[]);
int execv(const char *path, char *const argv[]);
int execvp(const char *file, char *const argv[]);
int execvpe(const char *file, char *const argv[], char *const envp[]);

Unix 约定将要执行的程序名称作为参数数组的第一个成员传递。

在什么现实背景下(如果有的话),偏离惯例而不是做出有意义的事情才有意义?路径/文件参数/参数v[0]

答案1

由于种种原因,这种差异被用来改变过程在 中的显示方式ps。其中一些已通过操作系统的更改而消除。

考虑到这一点,以下是一些找到的示例的链接:

  • 为什么我们在 execlp() 的前两个参数中传递相同的值
    尤其,@萨米库莫宁关于使符号链接“看起来”像真实文件的评论。
  • 隐藏.c /*---------------------------------------------------------------------------+ | Copyright (c) 1992 Oracle Corporation Belmont, California, USA | | All rights reserved | +---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------+ | FILENAME | | hide.c | | DESCRIPTION | | Hides arguments for programs on UNIX systems. | | Can be used as a program prefix: hide program arguments | | or as a symbolic link. If this program is not invoked as hide, it | | will hide its arguments and invoke the program name.hide | | The best way to use this is to rename your critical programs to | | program.hide, and create a symbolic link program to hide. | | mv sqlplus sqlplus.hide; ln -s hide sqlplus | | Thus when sqlplus is invoked, its arguments will be hidden | | NOTES | | This program works by padding 3000 '/' chars in argv[0]. This fools | | all known ps's. This will reduce the argument capacity of your | | program by 3000 chars. A good enhancement would be to reduce the | | padding if needed so that no arguments are lost - would require a | | method of determining the max argument size on the system. Some | | system's provide the E2BIG error on exec. | | There is some performace penalty for using this program, but it is | | minimal because this program is so small - the biggest cost is the | | extra exec required to get this program started. | | HISTORY | | 09/15/92 R Brodersen Created, based on D Beusee's hideargs() | | 09/17/92 D Beusee Fixed to compile on any system | +---------------------------------------------------------------------------*/

答案2

程序的行为可能会有所不同,具体取决于它被调用的名称。

一个例子是bash,它将输入POSIX模式如果调用为sh

$ bash -c 'set() { echo 1; }; set'
1

尽管:

$ ARGV0=sh bash -c 'set() { echo 1; }; set'
sh: `set': is a special builtin

zsh使用ARGV0要传递的变量argv[0]

相关内容