fork(2)ã¨execve(2)ã使ã£ã¦ã¿ã
OSã®ææ¥ã§æ±ã£ã¦ããfork(2)ã¨execve(2)ã§ãã,å®éã«ä½¿ã£ã¦ã¿ããã¨ããªãã£ãã®ã§å®éã«è©¦ãã«ä½¿ã£ã¦ã¿ã¾ãã.
å¥ã«ç¹å¥ãªãã¨ããããã£ãããã§ã¯ãªãã®ã§,ã¨ããããã·ã§ã«ã¨åãããã«åãã¦ãããã°ããããªã¼ã¨ããè¨è¨ã«ãªã£ã¦ãã¾ã.
æãªæéã«å¤§å¦ã§ä½ã£ãã®ã§Mac OS Xã§ããåä½ç¢ºèªããã¦ãã¾ãã.å¤å大ä¸å¤«ã ã¨æãã¾ãã,å ·ä½çã«ã¯errnoã¾ããã§ä½ãããããããã¾ãã.
ã§ã¯ã½ã¼ã¹ã³ã¼ãããã¤ãã®ããã«è¿½è¨ã«ç½®ãã¦ããã¾ã.
- switchã§breakãå¿ãã¦ãã®ã§ä¿®æ£(erãã¯ã)
- id:showyou ã®ææã«ããå®è¡çµæã追è¨ã«æ¸ãã¾ãã
- åãããããã®ã¾ã¾ã«ãã¦ãããã©argv[argc]ã¯å¿
ãNULLã£ã¦æ±ºã¾ã£ã¦ãã®ããªï¼
- ISO Cã®è¦æ ¼æ¸ã«æ¸ãã¦ããã¨ã http://cobito.tea-nifty.com/bleu/2004/02/argvargc.html
ãµã³ãã«ããã°ã©ã
#include <stdio.h> #include <unistd.h> #include <errno.h> #define er(mes) fprintf(stderr, mes);break int main(int argc, char** argv) { if(argc < 2) { fprintf(stderr, "Please Specify command\n"); return 1; } pid_t pid = fork(); if(pid == 0) { int i; for (i = 0; i <= argc; i++) { fprintf(stderr, "argv[%d] = \"%s\"\n", i, argv[i]); } int e = execve(argv[1], &argv[1], 0); fprintf(stderr, "Error Occurred\n"); switch(errno) { case E2BIG: er("Too much arguments\n"); case EACCES: er("Exec Access Error\n"); case EFAULT: er("Illegal Path or Arguments\n"); case EIO: er("I/O Error\n"); case ELOOP: er("Too Many Symbolic Links\n"); case ENAMETOOLONG: er("Specified Path name is too long\n"); case ENOENT: er("File does not exist\n"); case ENOEXEC: er("Specified program has appropreate permission but has invalid header to execute\n"); case ENOMEM: er("Memory size exceeded\n"); case ENOTDIR: er("A component of the path prefix is not a directory\n"); case ETXTBSY: er("File is currently used by another process\n"); } } else { int a; wait(&a); //fprintf(stderr, "Parent\n"); } return 0; }
å®è¡çµæ
limit@mkl ~/prog/c/sample $ gcc -o execve execve.c
- /bin/lsãå¼ã³åºãã¦ã¿ã
limit@mkl ~/prog/c/sample $ ./execve /bin/ls argv[0] = "./execve" argv[1] = "/bin/ls" argv[2] = "(null)" execve execve.c
- èªåèªèº«ãå¼ã³åºãã¦ã¿ã
limit@mkl ~/prog/c/sample $ ./execve ./execve /bin/ls argv[0] = "./execve" argv[1] = "./execve" argv[2] = "/bin/ls" argv[3] = "(null)" argv[0] = "./execve" argv[1] = "/bin/ls" argv[2] = "(null)" execve execve.c
- åå¨ããªã/usr/bin/lsãå¼ã³åºãã¦ã¿ã
limit@mkl ~/prog/c/sample $ ./execve /usr/bin/ls argv[0] = "./execve" argv[1] = "/usr/bin/ls" argv[2] = "(null)" Error Occurred File does not exist
- å®è¡ã§ããªããã¡ã¤ã«ãå®è¡ãããã¨ãã¦ã¿ã
limit@mkl ~/prog/c/sample $ ./execve /etc/fstab argv[0] = "./execve" argv[1] = "/etc/fstab" argv[2] = "(null)" Error Occurred Exec Access Error
- ãªãã·ã§ã³ã渡ãã¦ã¿ã
limit@mkl ~/prog/c/sample $ ./execve /bin/ls -l argv[0] = "./execve" argv[1] = "/bin/ls" argv[2] = "-l" argv[3] = "(null)" total 16 -rwxr-xr-x 1 limit limit 8184 Feb 7 15:19 execve -rw-r--r-- 1 limit limit 1264 Feb 7 15:19 execve.c -rw-r--r-- 1 limit limit 1258 Feb 7 15:15 execve.c.~1~
- å¼æ°ã¨ãªãã·ã§ã³ãåæã«æ¸¡ãã¦ã¿ã
limit@mkl ~/prog/c/sample $ ./execve /bin/uname -a argv[0] = "./execve" argv[1] = "/bin/uname" argv[2] = "-a" argv[3] = "(null)" Linux mkl 2.6.24-23-generic #1 SMP Mon Jan 26 00:13:11 UTC 2009 i686 GNU/Linux