genericopenlibs/cstdlib/TSTLIB/t_waitpid.c
changeset 0 e4d67989cc36
equal deleted inserted replaced
-1:000000000000 0:e4d67989cc36
       
     1 /*
       
     2 * Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 * T_WAITPID.CPP
       
    16 * 
       
    17 *
       
    18 */
       
    19 
       
    20 
       
    21 
       
    22 #include <stdlib.h>
       
    23 #include <stdio.h>
       
    24 #include <string.h>
       
    25 #include <unistd.h>
       
    26 #include <sys/errno.h>
       
    27 #include <sys/wait.h>
       
    28 #include "CTEST.H"
       
    29 
       
    30 int fids[3];
       
    31 test_Data;
       
    32 
       
    33 // simple function so the parent can wait for child and test waitpid	
       
    34 void doafunc()
       
    35 	{
       
    36 	sleep(5);
       
    37 	exit(0);
       
    38 	}
       
    39 /**
       
    40 @SYMTestCaseID 			SYSLIB-STDLIB-UT-1576
       
    41 @SYMTestCaseDesc	    Testing if POSIX waitpid function conforms to POSIX standard
       
    42 @SYMTestPriority 	    High
       
    43 @SYMTestActions  	    Starts a new processes and tests the POSIX waitpid function with 
       
    44 						different parameters and checks if it behaves correctly
       
    45 @SYMTestExpectedResults The test should not fail.
       
    46 @SYMDEF 				INC073739
       
    47 */
       
    48 
       
    49 int test_waitpid()
       
    50 	{
       
    51 	test_Data;
       
    52 	void* proc;
       
    53 	void* proc2;
       
    54 	int ret;
       
    55 	int status=-1;
       
    56 	test_Title("WAITPID");
       
    57 	test_Next("waitpid");
       
    58 	proc = create_process(doafunc, "A", "rw", fids);
       
    59 	if (proc)
       
    60 		start_process(proc);
       
    61 	else
       
    62 		perror("Failed to start process A: ");
       
    63 	
       
    64 	// test return values of calls to waitpid with different options and pid's
       
    65 	
       
    66 	if (proc)
       
    67 		{
       
    68 		// test with option WNOHANG and a correct pid on an active process
       
    69 		ret = wait_for_process_id(proc,get_proc_id(proc),WNOHANG,&status);
       
    70 		test(ret==0);
       
    71 		// test with an invalid pid 
       
    72 		ret = wait_for_process_id(proc,99,0,&status);
       
    73 		test(errno==ECHILD&&ret==-1);
       
    74 		// test with invalid options
       
    75 		ret = wait_for_process_id(proc,99,17,&status);
       
    76 		test(errno==EINVAL&&ret==-1);
       
    77 		ret = wait_for_process_id(proc,99,18,&status);
       
    78 		test(errno==EINVAL&&ret==-1);
       
    79 		// process is still active keep waiting until it dies
       
    80 		do 
       
    81 			{
       
    82 			ret = wait_for_process_id(proc,get_proc_id(proc),WNOHANG,&status);
       
    83 			}
       
    84 		while(!WIFEXITED(status)&&ret==0);
       
    85 		test(ret==get_proc_id(proc));
       
    86    		printf("child exited, status=%d\n", WEXITSTATUS(status));
       
    87 		}
       
    88 
       
    89 	// create another process and test waitpid with pid -1 and options 0
       
    90 	// this is the same as calling wait
       
    91 	proc2 = create_process(doafunc, "B", "rw", fids);
       
    92 	if (proc2)
       
    93 		start_process(proc2);
       
    94 	else
       
    95 		perror("Failed to start process B: ");
       
    96 	if (proc2)
       
    97 		{
       
    98 		//wait for proc2 to finish
       
    99 		do
       
   100 			{
       
   101         	ret = wait_for_process_id(proc2,-1,0,&status);
       
   102     		}
       
   103     	while (!WIFEXITED(status));
       
   104 		test(ret==get_proc_id(proc2));
       
   105     	printf("child exited, status=%d\n", WEXITSTATUS(status));
       
   106 		}
       
   107 		
       
   108 	fflush(stdout);
       
   109 	fclose(stdout);
       
   110 	test_Close();
       
   111 	return 0;
       
   112 	}
       
   113 
       
   114 
       
   115 
       
   116 int main (int argc, char *argv[])
       
   117 	{
       
   118 
       
   119 	start_redirection_server();
       
   120 	if(argc>1)
       
   121 		//do something with child process
       
   122 		doafunc();
       
   123 	else
       
   124 		test_waitpid();
       
   125 	// exit here for the moment crt0 libraries panic
       
   126 	exit(0);
       
   127 	return 0;
       
   128 	}