Loading and Running Programs

Một phần của tài liệu Computer systems a programmers perspective randal e bryant, david r ohallaron (Trang 785 - 788)

The execve function loads and runs a new program in the context of the current process.

#include <unistd.h>

int execve(const char *filename, canst char *argv[], const char *envp[]);

Does not return if OK; returns -1,on error

The execve function loads and runs the executable object file filename with the argument list argv and the environment variable list envp. Execve returns to the calling program only if there is an error, such as not being able to find filename.

So unlike fork, which is called once but returns twice, execve is called once and never returns.

The argument list is represented by the data structure shown in Figure 8.20.

The argv variable points to a null-terminated array of pointers, each of which points to an argument string. By convention, argv [OJ is the name of the executable object file. The list of environment variables is represented by a similar data structure, shown in Figure 8.21. The envp variable points to a null-terminated array of pointers to environment variable strings, each of which is a name-valueãpair of the form name=value.

Section 8.4 Process Control 751 Figure 8.20

Organization of an argument list.

argv "ls".

"-lt11

ãargv [argc - 1]

NULL I "/user/include" I

Figure 8.21

Organization of a,n environment variable list.

envp

envp[]

i"PWD=/usr/droh"I j 11PRINTER=iron")

I "USER=droh" I

After execve loads filename, 'it calls the start-up code described in Sec- tion 7.9. The start-up code sets up the stack and passes control to the main routine of the new program, which has a prototype of the form

iht main(fnt argc, char **argv, char **envp);

or equivalently,

int main(int argc, char *argv[], char *envp[]);

When main begins executing, the user stack has the organization shown in Fig- ure 8.22. Let's work our way from the bottom of the stack (the highest address) to the top (the lowest address). First are the argument-and enVironment strings.

These are followed further up the stack by a null-terminated array of pointers, each of which points to an environment variable string on-the stack. The global yariable enviroq points to the first qf these pointers, envp [OJ. The environment array is followed by th!' null-te~minated "Ff!!'[] (!Hay, with J<ach_t;l,eme,nt P.Oin,ting tp an argument string pn the. sta£k. At the.top of the stack, js the ~!ack fram~ fo1 the system start-up function, libc_start_main (Section 7.9).

There are three arguments to function main, each stored in a register accord- ing to the x86-64 stack discipline: (1) argc, which gives the number of non-null pointers in the argv [] array; (2) argv, which points to the first entry in the argv []

array; and (3) envp1 which points to the first entry in the. envp [] array.

Linux provides several functions for manipulating the environment array:

#include <stdlib.h>

char *getenv(const char *name);

Returns: pointer to name if it exists, NULL if no match

752 Chapter 8 'Exceptional Control Flow

Figure 8.22 Bottom of stack

Typical prganization of the user stack when a new program starts.

Null-terminated environment variable strings

Null-terminated command-line arg strings

envp (n] == NULL

envp[n-1] environ

[ / (global var) 1---•:::nc.vp...:.[0")'----4ãã .:f::ã.: .... .::~: ... ~==~

argv [argc] "" NULL envp , argv[a"rgc-i] (in %rdx) argv

(in %rsi) argc (in %rdi)

Stack frame for libc_start_main

1 - - - 1 Top of stack Future stack frame for

main

The getenv function searches the envin:mment array for a string name=value. If found, it returns a pointer to value; otherwise, it returns NULL.

#include <stdlib.h>

int setenv(const char *name, canst char •newvalue, int overwrite);

Returns:..O on success, -1 on error

void unsetenv(const char •name); '

Returns: nothing

. ,-,-

If the environment array contains. a string of the form 'name=oldvalue, then unsetenv deletes it and setenv replaces oldvalue' with newiralue, but only if overwrite is'nonzero. If name does not exist, tllen s'e'tenv adds name='n'.ewva'lue

to the array. "

' .

~~'",..,....,..bl . ""'ll'.'~ 'I".. J';:"i:'.'~>1"%'!0:: '' ''. >if~•',..,,_~,,,~~-~

!!;![i!J;llcgj,la:O, !l.rn;...,~ \ISOJ!lti111,_.;J.!a9Ê'il.:l~'ãa>dliiii..Gi!,~•ãi'.1ã$:tM~

Write a program calledãmyecho that prints its command-line arguments and envi- ronment variables. For example: ã ' "'

linux> ./myecho argl arg2 Command-ine arguments:

argv[ OJ: myecho argv [ 1] : argl argv[ 2]: arg2 '

Environment variables:

envp[ O]: PWD=/usrO/droh/ics/code/ecf en,,P [ 1] : TERM=emacs

envp[25]: USER=droh

envp[26]: SHELL=/usr/local/qin/tcsh envp[27]: HOME=/usrO/droh

Một phần của tài liệu Computer systems a programmers perspective randal e bryant, david r ohallaron (Trang 785 - 788)

Tải bản đầy đủ (PDF)

(1.120 trang)