From 44966693666a70a875d80a9b379ff6421c716c6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Axel=20D=C3=B6rfler?= Date: Thu, 14 Oct 2004 17:52:41 +0000 Subject: [PATCH] Added minimal fork() test app. Enhanced exec_test.c to use some process functions as well. Switched to load_image() instead of using the old _kern_create_team() syscall. git-svn-id: file:///srv/svn/repos/haiku/trunk/current@9359 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/kernel/apps/Jamfile | 1 + src/kernel/apps/consoled/main.c | 16 ++++++++------- src/kernel/apps/envtest/main.c | 18 ++++++++-------- src/kernel/apps/exec_test.c | 19 +++++++++++++++++ src/kernel/apps/fibo_main.c | 22 ++++++++++---------- src/kernel/apps/fork_test.c | 31 ++++++++++++++++++++++++++++ src/kernel/apps/init.c | 33 +++++++++++++++++------------- src/kernel/apps/shell/commands.c | 12 ++++++----- src/kernel/apps/shell/file_utils.c | 7 ++++--- src/kernel/apps/testapp/main.c | 9 ++++---- 10 files changed, 116 insertions(+), 52 deletions(-) create mode 100644 src/kernel/apps/fork_test.c diff --git a/src/kernel/apps/Jamfile b/src/kernel/apps/Jamfile index fa1f54152b..b62945a45a 100644 --- a/src/kernel/apps/Jamfile +++ b/src/kernel/apps/Jamfile @@ -4,6 +4,7 @@ KernelObjects exec_test.c false_main.c fibo_main.c + fork_test.c init.c monitor_test.c pipe_test.c diff --git a/src/kernel/apps/consoled/main.c b/src/kernel/apps/consoled/main.c index ccc03cf9fe..60a478befb 100644 --- a/src/kernel/apps/consoled/main.c +++ b/src/kernel/apps/consoled/main.c @@ -5,6 +5,7 @@ #include +#include #include #include @@ -143,7 +144,7 @@ start_console(struct console *con) static pid_t -start_process(const char *path, const char *name, char **argv, int argc, struct console *con) +start_process(int argc, const char **argv, struct console *con) { int saved_stdin, saved_stdout, saved_stderr; pid_t pid; @@ -156,8 +157,9 @@ start_process(const char *path, const char *name, char **argv, int argc, struct dup2(con->tty_slave_fd, 1); dup2(con->tty_slave_fd, 2); - // XXX launch - pid = _kern_create_team(path, name, argv, argc, NULL, 0, 5/*, PROC_FLAG_NEW_PGROUP*/); + pid = load_image(argc, argv, (const char **)environ); + resume_thread(pid); + setpgid(pid, 0); dup2(saved_stdin, 0); dup2(saved_stdout, 1); @@ -192,15 +194,15 @@ main(void) for (;;) { pid_t shell_process; status_t retcode; - char *argv[3]; + const char *argv[3]; argv[0] = "/bin/shell"; // argv[1] = "-s"; // argv[2] = "/boot/loginscript"; - shell_process = start_process("/bin/shell", "shell", argv, 1, &theconsole); + shell_process = start_process(1, argv, &theconsole); + + wait_for_thread(shell_process, &retcode); - _kern_wait_for_team(shell_process, &retcode); - puts("Restart shell"); } diff --git a/src/kernel/apps/envtest/main.c b/src/kernel/apps/envtest/main.c index 71d727d014..9b15cc5556 100644 --- a/src/kernel/apps/envtest/main.c +++ b/src/kernel/apps/envtest/main.c @@ -1,20 +1,22 @@ /* -** Copyright 2003-2004, The OpenBeOS Team. All rights reserved. -** Distributed under the terms of the OpenBeOS License. +** Copyright 2003-2004, The Haiku Team. All rights reserved. +** Distributed under the terms of the Haiku License. */ +#include + #include #include #include -#include int main(int argc, char **argv) { - int i, rc; - team_id pid; + int i; + thread_id pid; + status_t rc; char temp[16]; char *var; @@ -42,7 +44,7 @@ main(int argc, char **argv) if (argc > 1) { char buffer[16]; - char *_argv[] = { argv[0], buffer, NULL }; + const char *_argv[] = { argv[0], buffer, NULL }; int val = atoi(argv[1]) - 1; sprintf(temp, "VAR_%d", val); @@ -54,8 +56,8 @@ main(int argc, char **argv) if (val > 0) { printf("Spawning test (%d left)\n", val); sprintf(buffer, "%d", val); - pid = _kern_create_team(_argv[0], _argv[0], _argv, 2, NULL, 0, 5); - _kern_wait_for_team(pid, &rc); + pid = load_image(2, _argv, (const char **)environ); + wait_for_thread(pid, &rc); } } return 0; diff --git a/src/kernel/apps/exec_test.c b/src/kernel/apps/exec_test.c index 4f6c9bae17..f8d52e2198 100644 --- a/src/kernel/apps/exec_test.c +++ b/src/kernel/apps/exec_test.c @@ -12,20 +12,39 @@ #include +void +print_process_info(const char *text) +{ + puts(text); + + printf("\tsession_id = %ld\n", getsid(0)); + printf("\tgroup_id = %ld\n", getpgid(0)); + printf("\tprocess_id = %ld\n", getpid()); + printf("\tparent_id = %ld\n", getppid()); +} + + int main(int argc, char **argv) { printf("exec_test: this is thread %ld\n", find_thread(NULL)); if (argc < 2) { + print_process_info("before exec():"); puts("going to execl()..."); + execl("/bin/exec_test", "/bin/exec_test", "argument 1", NULL); printf("execl() returned: %s\n", strerror(errno)); } else { int i; + + print_process_info("after exec():"); puts("got arguments:"); for (i = 0; i < argc; i++) printf("%d: \"%s\"\n", i, argv[i]); + + setsid(); + print_process_info("after setsid():"); } return 0; diff --git a/src/kernel/apps/fibo_main.c b/src/kernel/apps/fibo_main.c index 13481106cf..2fd4a70113 100644 --- a/src/kernel/apps/fibo_main.c +++ b/src/kernel/apps/fibo_main.c @@ -4,18 +4,18 @@ */ +#include + #include #include #include -#include -static -void +static void usage(char const *app) { printf("usage: %s [-s] ###\n", app); - _kern_exit(-1); + exit(-1); } @@ -46,20 +46,20 @@ main(int argc, char *argv[]) if (num < 2) { result = 1; } else { - team_id pid; - int retcode; + thread_id pid; + status_t retcode; char buffer[64]; - char *aaargv[] = { "/boot/bin/fibo", "-s", buffer, NULL }; + const char *aaargv[] = { "/boot/bin/fibo", "-s", buffer, NULL }; int aaargc = 3; sprintf(buffer, "%d", num-1); - pid = _kern_create_team(aaargv[0], aaargv[0], aaargv, aaargc, NULL, 0, 5); - _kern_wait_for_team(pid, &retcode); + pid = load_image(aaargc, aaargv, (const char **)environ); + wait_for_thread(pid, &retcode); result = retcode; sprintf(buffer, "%d", num-2); - pid = _kern_create_team(aaargv[0], aaargv[0], aaargv, aaargc, NULL, 0, 5); - _kern_wait_for_team(pid, &retcode); + pid = load_image(aaargc, aaargv, (const char **)environ); + wait_for_thread(pid, &retcode); result += retcode; } diff --git a/src/kernel/apps/fork_test.c b/src/kernel/apps/fork_test.c new file mode 100644 index 0000000000..bad1ed8bab --- /dev/null +++ b/src/kernel/apps/fork_test.c @@ -0,0 +1,31 @@ +/* +** Copyright 2004, Axel Dörfler, axeld@pinc-software.de. All rights reserved. +** Distributed under the terms of the Haiku License. +*/ + + +#include + +#include +#include +#include +#include + + +int +main(int argc, char **argv) +{ + pid_t child = fork(); + if (child == 0) { + write(1, "CHILD: is awake!\n", 17); + exit_thread(0); + //printf("CHILD: we're the child!\n"); + } else if (child > 0) { + printf("PARENT: we're the parent, our child has pid %ld\n", child); + wait_for_thread(child, NULL); + } else + fprintf(stderr, "fork() failed: %s\n", strerror(errno)); + + return 0; +} + diff --git a/src/kernel/apps/init.c b/src/kernel/apps/init.c index 9ee7539fea..857c4e0e90 100644 --- a/src/kernel/apps/init.c +++ b/src/kernel/apps/init.c @@ -4,9 +4,11 @@ */ +#include + #include #include -#include +#include #include @@ -26,32 +28,35 @@ setup_io(void) int -main() +main(int argc, char **argv) { setup_io(); printf("Welcome to Haiku!\n"); { - team_id pid; + const char *args[] = {"fortune", NULL}; + thread_id thread; - pid = _kern_create_team("/bin/fortune", "/bin/fortune", NULL, 0, NULL, 0, 5); - if (pid >= 0) { - int retcode; - _kern_wait_for_team(pid, &retcode); + thread = load_image(1, args, (const char **)environ); + if (thread >= B_OK) { + status_t returnCode; + wait_for_thread(thread, &returnCode); } else printf("Failed to create a team for fortune.\n"); } while (1) { - team_id pid; + const char *args[] = {"shell", NULL}; + thread_id thread; - pid = _kern_create_team("/bin/shell", "/bin/shell", NULL, 0, NULL, 0, 5); - if (pid >= 0) { - int retcode; - printf("init: spawned shell, pid 0x%lx\n", pid); - _kern_wait_for_team(pid, &retcode); - printf("init: shell exited with return code %d\n", retcode); + thread = load_image(1, args, (const char **)environ); + if (thread >= B_OK) { + status_t returnCode; + + printf("init: spawned shell, pid 0x%lx\n", thread); + wait_for_thread(thread, &returnCode); + printf("init: shell exited with return code %ld\n", returnCode); } else printf("Failed to start a shell :(\n"); } diff --git a/src/kernel/apps/shell/commands.c b/src/kernel/apps/shell/commands.c index 4c51ad8459..ee5abc20ac 100644 --- a/src/kernel/apps/shell/commands.c +++ b/src/kernel/apps/shell/commands.c @@ -4,6 +4,8 @@ */ #include +#include + #include #include #include @@ -36,7 +38,7 @@ cmd_exec(int argc, char *argv[]) int -cmd_create_proc(int argc,char *argv[]) +cmd_create_proc(int argc, char *argv[]) { bool must_wait=true; team_id pid; @@ -50,9 +52,9 @@ cmd_create_proc(int argc,char *argv[]) return 0; } - tmp = argv[argc - 1]; + tmp = argv[argc - 1]; - if( !find_file_in_path(argv[0],filename,SCAN_SIZE)){ + if (!find_file_in_path(argv[0], filename, SCAN_SIZE)) { printf("can't find '%s' \n",argv[0]); return 0; } @@ -74,10 +76,10 @@ cmd_create_proc(int argc,char *argv[]) } } - pid = _kern_create_team(filename,filename, argv, argc, NULL, 0, 5); + pid = load_image(argc, (const char **)argv, (const char **)environ); if (pid >= 0) { if (must_wait) - _kern_wait_for_team(pid, NULL); + wait_for_thread(pid, NULL); } else { printf("Error: cannot execute '%s'\n", filename); return 0; // should be -1, but the shell would exit diff --git a/src/kernel/apps/shell/file_utils.c b/src/kernel/apps/shell/file_utils.c index 993b56598c..42e7350fd5 100644 --- a/src/kernel/apps/shell/file_utils.c +++ b/src/kernel/apps/shell/file_utils.c @@ -1,6 +1,7 @@ +#include + #include #include -#include #include #include #include @@ -85,11 +86,11 @@ exec_file(int argc, char *argv[], status_t *retcode) if (!find_file_in_path(argv[0], filename, SCAN_SIZE)) return SHE_FILE_NOT_FOUND; - pid = _kern_create_team(filename, filename, argv, argc, NULL, 0, 5); + pid = load_image(argc, (const char **)argv, (const char **)environ); if (pid < 0) return SHE_CANT_EXECUTE; - _kern_wait_for_team(pid, retcode); + wait_for_thread(pid, retcode); return SHE_NO_ERROR; } diff --git a/src/kernel/apps/testapp/main.c b/src/kernel/apps/testapp/main.c index e6fe8e04ef..8d83fdcfcf 100644 --- a/src/kernel/apps/testapp/main.c +++ b/src/kernel/apps/testapp/main.c @@ -345,19 +345,20 @@ int main(int argc, char **argv) printf("spawning %d copies of true\n", 100); for(i=0; i<100; i++) { - team_id id; + const char *args[] = {"true", NULL}; + thread_id id; bigtime_t t; printf("%d...", i); t = system_time(); - id = _kern_create_team("/bin/true", "true", NULL, 0, NULL, 0, 20); - if(id <= 0x2) { + id = load_image(1, args, (const char **)environ); + if (id <= 0x2) { printf("new team returned 0x%lx!\n", id); return -1; } - _kern_wait_for_team(id, NULL); + wait_for_thread(id, NULL); printf("done (%Ld usecs)\n", system_time() - t); }