Do You Coding?

[function] execve ํ•จ์ˆ˜ ์•Œ์•„๋ณด๊ธฐ ๋ณธ๋ฌธ

CS & Engineering/C

[function] execve ํ•จ์ˆ˜ ์•Œ์•„๋ณด๊ธฐ

๐Ÿ“Œ ๋งค๋‰ด์–ผ (Linux)

๋”๋ณด๊ธฐ

NAME
       execve - execute program
: ํ”„๋กœ๊ทธ๋žจ์„ ์‹คํ–‰


SYNOPSIS
       #include <unistd.h>
       int execve(const char *pathname, char *const argv[], char *const envp[]);

DESCRIPTION
       execve()  executes  the program referred to by pathname.  This causes the program that is curโ€
       rently being run by the calling process to be replaced with a new program, with newly initialโ€
       ized stack, heap, and (initialized and uninitialized) data segments.

       pathname must be either a binary executable, or a script starting with a line of the form:
       #!interpreter [optional-arg]


       argv  is  an  array of pointers to strings passed to the new program as its command-line arguโ€
       ments.  By convention, the first of these strings (i.e., argv[0]) should contain the  filename
       associated with the file being executed.  The argv array must be terminated by a NULL pointer.
       (Thus, in the new program, argv[argc] will be NULL.)

       envp is an array of pointers to strings, conventionally  of  the  form  key=value,  which  are
       passed  as  the  environment  of the new program.  The envp array must be terminated by a NULL
       pointer.


       execve() does not return on success, and the text, initialized data, uninitialized data (bss),
       and stack of the calling process are overwritten according to the contents of the newly loaded
       program.
: execve()๋Š” pathname์œผ๋กœ ์–ธ๊ธ‰๋œ ํ”„๋กœ๊ทธ๋žจ์„ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์ด๋กœ ์ธํ•ด ํ˜„์žฌ ํ˜ธ์ถœ ํ”„๋กœ์„ธ์Šค์—์„œ

์‹คํ–‰ ์ค‘์ธ ํ”„๋กœ๊ทธ๋žจ์ด ์ƒˆ๋กœ ์ดˆ๊ธฐํ™”๋œ ์Šคํƒ, ํž™ ๋ฐ (์ดˆ๊ธฐํ™” ๋ฐ ์ดˆ๊ธฐํ™”๋˜์ง€ ์•Š์€) ๋ฐ์ดํ„ฐ ์„ธ๊ทธ๋จผํŠธ๋กœ

์ƒˆ ํ”„๋กœ๊ทธ๋žจ์œผ๋กœ ๋Œ€์ฒด๋ฉ๋‹ˆ๋‹ค.

pathname์€ ์ด์ง„ ์‹คํ–‰ ํŒŒ์ผ์ด๊ฑฐ๋‚˜ #! interpreter [์˜ต์…˜-arg] ํ˜•์‹์˜ ํ•œ ํ–‰์œผ๋กœ ์‹œ์ž‘ํ•˜๋Š” ์Šคํฌ๋ฆฝํŠธ์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค.

argv๋Š” ์ƒˆ ํ”„๋กœ๊ทธ๋žจ์— ๋ช…๋ น์ค„ ์ธ์ˆ˜๋กœ ์ „๋‹ฌ๋˜๋Š” ๋ฌธ์ž์—ด์˜ ํฌ์ธํ„ฐ ๋ฐฐ์—ด์ž…๋‹ˆ๋‹ค.

๊ด€๋ก€์ ์œผ๋กœ ์ด๋Ÿฌํ•œ ๋ฌธ์ž์—ด ์ค‘ ์ฒซ ๋ฒˆ์งธ ๋ฌธ์ž์—ด(์ฆ‰, argv[0])์€ ์‹คํ–‰ ์ค‘์ธ ํŒŒ์ผ๊ณผ ๊ด€๋ จ๋œ ํŒŒ์ผ ์ด๋ฆ„์„ ํฌํ•จํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

argv ๋ฐฐ์—ด์€ NULL ํฌ์ธํ„ฐ์— ์˜ํ•ด ์ข…๋ฃŒ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. (๋”ฐ๋ผ์„œ ์ƒˆ ํ”„๋กœ๊ทธ๋žจ์—์„œ argv[argc]๋Š” NULL์ด ๋ฉ๋‹ˆ๋‹ค.)

envp๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ ์ƒˆ ํ”„๋กœ๊ทธ๋žจ์˜ ํ™˜๊ฒฝ์œผ๋กœ ์ „๋‹ฌ๋˜๋Š” key=value ํ˜•ํƒœ์˜ ๋ฌธ์ž์—ด์— ๋Œ€ํ•œ ํฌ์ธํ„ฐ ๋ฐฐ์—ด์ž…๋‹ˆ๋‹ค.

envp ๋ฐฐ์—ด์€ NULL ํฌ์ธํ„ฐ์— ์˜ํ•ด ์ข…๋ฃŒ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

execve()๋Š” ์„ฑ๊ณต ์‹œ ๋ฐ˜ํ™˜๋˜์ง€ ์•Š์œผ๋ฉฐ, ์ƒˆ๋กœ ๋กœ๋“œ๋œ ํ”„๋กœ๊ทธ๋žจ์˜ ๋‚ด์šฉ์— ๋”ฐ๋ผ ํ…์ŠคํŠธ, ์ดˆ๊ธฐํ™”๋œ ๋ฐ์ดํ„ฐ,

์ดˆ๊ธฐํ™”๋˜์ง€ ์•Š์€ ๋ฐ์ดํ„ฐ(bss), ํ˜ธ์ถœ ํ”„๋กœ์„ธ์Šค์˜ ์Šคํƒ์„ ๋ฎ์–ด์”๋‹ˆ๋‹ค.

 

RETURN VALUE
       On success, execve() does not return, on error -1 is returned, and errno is set appropriately.

: ์„ฑ๊ณต ์‹œ execve()๊ฐ€ ๋ฐ˜ํ™˜๋˜์ง€ ์•Š๊ณ , ์˜ค๋ฅ˜์ด๋ฉด -1์ด ๋ฐ˜ํ™˜๋˜๋ฉฐ errno๊ฐ€ ์ ์ ˆํ•˜๊ฒŒ ์„ค์ •๋ฉ๋‹ˆ๋‹ค.

๐Ÿ“Œ ํ•จ์ˆ˜ ์„ค๋ช…

execve ํ•จ์ˆ˜๋Š” ๋‹ค๋ฅธ ํ”„๋กœ๊ทธ๋žจ(ex. ./a.out) ์„ ์‹คํ–‰ํ•˜๊ณ , ์ž์‹  ํ”„๋กœ๊ทธ๋žจ์€ ์ข…๋ฃŒ์‹œํ‚ค๋Š” ํ•จ์ˆ˜์ด๋‹ค.

์ธ์ž๋กœ ๋“ค์–ด์˜จ path ์ฆ‰, ํ”„๋กœ๊ทธ๋žจ์˜ ๊ฒฝ๋กœ๋ฅผ ๊ฐ€์ ธ์™€ ์‹คํ–‰ํ•˜๊ณ , argv์— ์ธ์ˆ˜ ๋ชฉ๋ก์„ ๋ฐ›์•„์˜ค๋ฉฐ,

envp์—์„œ ํ™˜๊ฒฝ ๋ณ€์ˆ˜ ์„ค์ • ๋ชฉ๋ก์„ ๊ฐ€์ ธ์˜จ๋‹ค.

 

exec ๊ณ„์—ด ์—ฌ๋Ÿฌ ํ•จ์ˆ˜๋“ค์ด ์กด์žฌ(execl, execlp, execle, execv, execvp ๋“ฑ) ํ•˜๋ฉฐ

execve๋Š” ๋‹ค๋ฅธ ํ•จ์ˆ˜๋“ค๊ณผ ๋‹ค๋ฅด๊ฒŒ ํ”„๋กœ๊ทธ๋žจ ๊ฒฝ๋กœ๋ฅผ ๊ฐ€์ ธ์˜ฌ ๋•Œ, ์ „์ฒด ๊ฒฝ๋กœ ๋ช…์„ ๊ฐ€์ ธ์™€์•ผํ•˜๋ฉฐ,

์ธ์ˆ˜ ๋ฐฐ์—ด์„ ์ด์ค‘๋ฐฐ์—ด๋กœ ๊ฐ€์ ธ์˜ค๊ณ , ํ™˜๊ฒฝ ์„ค์ •๋„ ์ด์ค‘๋ฐฐ์—ด๋กœ ๊ฐ€์ ธ์˜จ๋‹ค๋Š” ํŠน์ง•์ด ์žˆ๋‹ค.

 

[ code_a.c ] 

#include <stdlib.h>
#include <stdio.h>

int	main(int argc, char *argv[])
{
	if (1 < argc)
		printf("argv[1] = %s, getenv argv[1] = %s", argv[1], getenv(argv[1])); 
	return (0);
}

 

์šฐ์„  code_a.c ์ด๋‹ค.

์ด ์ฝ”๋“œ๋Š” ์ถ”ํ›„์— execve๋กœ ๋ถˆ๋Ÿฌ์˜ฌ ํ”„๋กœ๊ทธ๋žจ์˜ ์ฝ”๋“œ์ด๊ณ , argv๋กœ ์ธ์ž๋ฅผ ๋ฐ›์•„์™€ ์ฒ˜๋ฆฌํ•ด์ฃผ๋Š” ๋‹จ์ˆœํ•œ ์ฝ”๋“œ์ด๋‹ค.

์ธ์ž๋กœ ํ™˜๊ฒฝ๋ณ€์ˆ˜๋ฅผ ๋ฐ›์•„์™€ getenv ํ•จ์ˆ˜์— ๋„ฃ์„ ๊ฑด๋ฐ, ์ด getenv ํ•จ์ˆ˜๋Š” ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋ฅผ ๊ฒ€์ƒ‰ํ•˜์—ฌ

ํ•ด๋‹น ๋ฌธ์ž์—ด ํฌ์ธํ„ฐ๋ฅผ ๋ฆฌํ„ดํ•ด์ค€๋‹ค.

 

 

> cc code_a.c -o show
> ./show HOME
argv[1] = HOME, getenv argv[1] = /home/doyoucode

 

์ถ”ํ›„์— ํ”„๋กœ๊ทธ๋žจ์„ show๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ ์œ„ํ•ด ์ปดํŒŒ์ผ ์˜ต์…˜์„ ํ†ตํ•ด ์ง€์ •ํ•ด์ฃผ์—ˆ๊ณ ,

./show ๋กœ ์ฒซ๋ฒˆ์งธ ์ธ์ž๋กœ HOME์„ ๋„ฃ๊ณ  ์‹คํ–‰ํ•˜๊ฒŒ ๋˜๋ฉด, ํ™˜๊ฒฝ๋ณ€์ˆ˜ HOME๊ณผ ๊ทธ ํ™˜๊ฒฝ๋ณ€์ˆ˜์˜ ๊ฐ’ ๋ฌธ์ž์—ด์„ ์ถœ๋ ฅํ•ด์ค€๋‹ค.

 

[ code_b.c ]

#include <stdio.h>
#include <unistd.h>

int main()
{
	char *argv[] = { "./show", "DOYOU", NULL};
	char *envp[] = { "DOYOU=dyomyo.tistory.com", NULL};

	execve( "./show", argv, envp);

	printf( "error\n");
	return (0);
}

 

์ด์ œ, code_b.c๋กœ ๋„˜์–ด๊ฐ€๋ฉด,

execve์— ๋„ฃ์–ด์ค„ argv ๋ชฉ๋ก๊ณผ envp ๋ชฉ๋ก์„ ์ƒ์„ฑํ•ด์ค„ ๊ฒƒ์ด๋‹ค.

๋‘ ์ด์ค‘ ๋ฐฐ์—ด์€ ํ•ญ์ƒ NULL๋กœ ๋๋งˆ์ณ์•ผํ•˜๋ฉฐ, ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

์ƒ์„ฑ ์‹œ, argv์—๋Š” ์ธ์ž๋ฅผ ๋ฐ›์•„์˜ฌ ๊ฒƒ์ธ๋ฐ, ์ฒซ ์ธ์ž๋Š” ๋ถˆ๋Ÿฌ์˜ฌ ํ”„๋กœ๊ทธ๋žจ๋ช…์œผ๋กœ ํ•œ๋‹ค.

 

๊ทธ๋ฆฌ๊ณ , ๋‘๋ฒˆ์งธ ์ธ์ž๋Š” ํ™˜๊ฒฝ๋ณ€์ˆ˜๋ช…์„ ๊ฐ€์ ธ์˜ค๋Š”๋ฐ, ์ง€๊ธˆ์€ envp์—์„œ ํ™˜๊ฒฝ์„ค์ •์„ ํ†ตํ•ด ๋งŒ๋“ค 'DOYOU'๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค.

envp์—์„œ๋Š” ์ฒซ ์ธ์ž๋กœ ์ด 'DOYOU'์˜ ๊ฐ’์„ ์ง€์ •ํ•ด์„œ ๋„ฃ์–ด์ฃผ๋ฉฐ, ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ NULL๋กœ ๋งˆ๋ฌด๋ฆฌํ•œ๋‹ค.

 

์ด๋Ÿฌ๋ฉด ์ด์ œ execve์—์„œ ํ”„๋กœ๊ทธ๋žจ ๊ฒฝ๋กœ๋ช…๊ณผ ๋งŒ๋“ค์–ด๋‘” argv, envp๋ฅผ ๊ฐ€์ ธ์™€ ์‹คํ–‰์„ ์‹œ์ผœ์ฃผ๋Š”๋ฐ,

์ •์ƒ์ ์œผ๋กœ ์‹คํ–‰์ด ๋˜๋ฉด ์ด ํ”„๋กœ๊ทธ๋žจ์€ ์ข…๋ฃŒ๊ฐ€ ๋˜๊ณ  ./show๊ฐ€ ์‹คํ–‰์ด ๋˜๋ฉฐ, ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด

ํ˜„์žฌ ํ”„๋กœ๊ทธ๋žจ์ด ์ง„ํ–‰๋˜์–ด printf("error\n"); ๊ฐ€ ์‹คํ–‰๋  ๊ฒƒ์ด๋‹ค.

 

> cc code_b.c
> ./a.out
argv[1] = DOYOU, getenv argv[1] = dyomyo.tistory.com

 

๋ฐ”๋กœ cc๋กœ ์ปดํŒŒ์ผ ํ•ด์„œ ./a.out ์‹คํ–‰ํŒŒ์ผ์„ ๋งŒ๋“ค์–ด์ค€ ๋’ค ์‹คํ–‰ํ•˜๋ฉด,

์ •์ƒ์ ์œผ๋กœ ./show๊ฐ€ ์‹คํ–‰๋˜์–ด ์šฐ๋ฆฌ๊ฐ€ ์ „๋‹ฌํ•œ argv์˜ ์ธ์ž ๊ฐ’์ด ์ „๋‹ฌ๋˜์–ด ํ•ด๋‹น ํ™˜๊ฒฝ๋ณ€์ˆ˜์™€ ๊ฐ’์ด ์ถœ๋ ฅ๋œ๋‹ค.

 

์ง€๊ธˆ์ฒ˜๋Ÿผ, ๋‹ค๋ฅธ ํ”„๋กœ๊ทธ๋žจ์— ์ ์ ˆํ•œ ์ธ์ž์™€ ํ•จ๊ป˜ ํ•ด๋‹น ์ธ์ž์˜ ํ™˜๊ฒฝ ์„ค์ •๊นŒ์ง€ ์ „๋‹ฌํ•˜์—ฌ ์‹คํ–‰์‹œ์ผœ์ฃผ๋Š” ํ•จ์ˆ˜๊ฐ€

execve์ด๋ฉฐ, ๋‹ค๋ฅธ exec ๊ณ„์—ด ํ•จ์ˆ˜๋“ค์€ ๋’ค์˜ ์ธ์ž๋“ค์ด ์กฐ๊ธˆ ๋‹ค๋ฅด๊ฒŒ ์„ค์ •๋˜์–ด ์žˆ๊ณ  ์ผ๋ฐ˜์ ์ธ ๋™์ž‘์€ ๋น„์Šทํ•˜๋‹ค.