sl@0: /* sl@0: * Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: * All rights reserved. sl@0: * This component and the accompanying materials are made available sl@0: * under the terms of "Eclipse Public License v1.0" sl@0: * which accompanies this distribution, and is available sl@0: * at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: * sl@0: * Initial Contributors: sl@0: * Nokia Corporation - initial contribution. sl@0: * sl@0: * Contributors: sl@0: * sl@0: * Description: sl@0: * T_WAITPID.CPP sl@0: * sl@0: * sl@0: */ sl@0: sl@0: sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include "CTEST.H" sl@0: sl@0: int fids[3]; sl@0: test_Data; sl@0: sl@0: // simple function so the parent can wait for child and test waitpid sl@0: void doafunc() sl@0: { sl@0: sleep(5); sl@0: exit(0); sl@0: } sl@0: /** sl@0: @SYMTestCaseID SYSLIB-STDLIB-UT-1576 sl@0: @SYMTestCaseDesc Testing if POSIX waitpid function conforms to POSIX standard sl@0: @SYMTestPriority High sl@0: @SYMTestActions Starts a new processes and tests the POSIX waitpid function with sl@0: different parameters and checks if it behaves correctly sl@0: @SYMTestExpectedResults The test should not fail. sl@0: @SYMDEF INC073739 sl@0: */ sl@0: sl@0: int test_waitpid() sl@0: { sl@0: test_Data; sl@0: void* proc; sl@0: void* proc2; sl@0: int ret; sl@0: int status=-1; sl@0: test_Title("WAITPID"); sl@0: test_Next("waitpid"); sl@0: proc = create_process(doafunc, "A", "rw", fids); sl@0: if (proc) sl@0: start_process(proc); sl@0: else sl@0: perror("Failed to start process A: "); sl@0: sl@0: // test return values of calls to waitpid with different options and pid's sl@0: sl@0: if (proc) sl@0: { sl@0: // test with option WNOHANG and a correct pid on an active process sl@0: ret = wait_for_process_id(proc,get_proc_id(proc),WNOHANG,&status); sl@0: test(ret==0); sl@0: // test with an invalid pid sl@0: ret = wait_for_process_id(proc,99,0,&status); sl@0: test(errno==ECHILD&&ret==-1); sl@0: // test with invalid options sl@0: ret = wait_for_process_id(proc,99,17,&status); sl@0: test(errno==EINVAL&&ret==-1); sl@0: ret = wait_for_process_id(proc,99,18,&status); sl@0: test(errno==EINVAL&&ret==-1); sl@0: // process is still active keep waiting until it dies sl@0: do sl@0: { sl@0: ret = wait_for_process_id(proc,get_proc_id(proc),WNOHANG,&status); sl@0: } sl@0: while(!WIFEXITED(status)&&ret==0); sl@0: test(ret==get_proc_id(proc)); sl@0: printf("child exited, status=%d\n", WEXITSTATUS(status)); sl@0: } sl@0: sl@0: // create another process and test waitpid with pid -1 and options 0 sl@0: // this is the same as calling wait sl@0: proc2 = create_process(doafunc, "B", "rw", fids); sl@0: if (proc2) sl@0: start_process(proc2); sl@0: else sl@0: perror("Failed to start process B: "); sl@0: if (proc2) sl@0: { sl@0: //wait for proc2 to finish sl@0: do sl@0: { sl@0: ret = wait_for_process_id(proc2,-1,0,&status); sl@0: } sl@0: while (!WIFEXITED(status)); sl@0: test(ret==get_proc_id(proc2)); sl@0: printf("child exited, status=%d\n", WEXITSTATUS(status)); sl@0: } sl@0: sl@0: fflush(stdout); sl@0: fclose(stdout); sl@0: test_Close(); sl@0: return 0; sl@0: } sl@0: sl@0: sl@0: sl@0: int main (int argc, char *argv[]) sl@0: { sl@0: sl@0: start_redirection_server(); sl@0: if(argc>1) sl@0: //do something with child process sl@0: doafunc(); sl@0: else sl@0: test_waitpid(); sl@0: // exit here for the moment crt0 libraries panic sl@0: exit(0); sl@0: return 0; sl@0: }