execl() arguments in Ubuntu

巧了我就是萌 提交于 2019-12-30 08:30:50

问题


I am learning linux programming and came across exec function which is kind of very useful. But the problem is exec function arguments are very confusing and I am unable to grasp which argument is for what purpose.. In the following code execl() function is called from a child created through fork(), What is the purpose of the last argument (NULL) in execl()?

execl("/bin/ls","ls","-l",NULL);

If any one can explain what is the purpose of NULL argument and other arguments and the purpose of arguments of exec() family function, It would be a great help to me!


回答1:


To create undefined behavior. That is not a legal call to execl. A correct call might be:

execl( "/bin/ls", "ls", "-l", (char*)0 );

The last argument must be (char*)0, or you have undefined behavior. The first argument is the path of the executable. The following arguments appear in argv of the executed program. The list of these arguments is terminated by a (char*)0; that's how the called function knows that the last argument has been reached. In the above example, for example, the executable at "/bin/ls" will replace your code; in its main, it will have argc equal 2, with argv[0] equal "ls", and argv[1] equal "-l".

Immediately after this function, you should have the error handling code. (execl always returns -1, when it returns, so you don't need to test it. And it only returns if there was some sort of error.)




回答2:


The exec functions are variadic: they take a variable number of parameters so that you can pass a variable number of arguments to the command. The functions need to use NULL as a marker to mark the end of the argument list.

Within variadic functions is a loop that will iterate over the variable number of arguments. These loops need a terminating condition. In some cases, e.g. printf, the actual number of arguments can be inferred from another argument. In other functions, NULL is used to mark the end of the list.

Another option would be to add an additional function parameter for number of arguments, but that would make the code a little more brittle, requiring the programmer to manage an additional parameter, rather than simply always using a NULL as the final argument.

You'll also see (char *) 0 used as the marker:

execl("/bin/ls", "ls", "-l", (char *) 0);



回答3:


In /usr/include/libio.h, since gcc 2.8 (a long time ago) NULL is defined to be null ( is reserved for builtins), prior to that NULL was (void *)0 which is indistinguishable from (char *)0 in a varargs situation since the type is not passed, the exception being if __cplusplus is defined in which case NULL is defined as 0.

The safe thing to do especially if you have a 64-bit architecture is to explicitly use (void *)0 which is defined to be compatible with any pointer and not rely on any dodgy #defines that might happen to be in the standard library.




回答4:


The purpose of the ending argument (char *) 0 is to terminate the parameters. Undefined behavior may result if this is missing. The man page defines the execl signature as :

int execl(const char *path, const char *arg, ...);

path: The location of the program to invoke, the program to invoke.

arg, ...*: can be thought of as arg0, arg1, ..., argn.

In your case execl( "/bin/ls", "ls", "-l", (char*)0 ); is the correct function call.

"bin/ls" the program to invoke

"ls" the program name

"-l" is the parameter for that program called



来源:https://stackoverflow.com/questions/12677120/execl-arguments-in-ubuntu

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!