os/ossrv/genericopenlibs/cstdlib/TSTLIB/t_waitpid.c
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/genericopenlibs/cstdlib/TSTLIB/t_waitpid.c	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,128 @@
     1.4 +/*
     1.5 +* Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.6 +* All rights reserved.
     1.7 +* This component and the accompanying materials are made available
     1.8 +* under the terms of "Eclipse Public License v1.0"
     1.9 +* which accompanies this distribution, and is available
    1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.11 +*
    1.12 +* Initial Contributors:
    1.13 +* Nokia Corporation - initial contribution.
    1.14 +*
    1.15 +* Contributors:
    1.16 +*
    1.17 +* Description:
    1.18 +* T_WAITPID.CPP
    1.19 +* 
    1.20 +*
    1.21 +*/
    1.22 +
    1.23 +
    1.24 +
    1.25 +#include <stdlib.h>
    1.26 +#include <stdio.h>
    1.27 +#include <string.h>
    1.28 +#include <unistd.h>
    1.29 +#include <sys/errno.h>
    1.30 +#include <sys/wait.h>
    1.31 +#include "CTEST.H"
    1.32 +
    1.33 +int fids[3];
    1.34 +test_Data;
    1.35 +
    1.36 +// simple function so the parent can wait for child and test waitpid	
    1.37 +void doafunc()
    1.38 +	{
    1.39 +	sleep(5);
    1.40 +	exit(0);
    1.41 +	}
    1.42 +/**
    1.43 +@SYMTestCaseID 			SYSLIB-STDLIB-UT-1576
    1.44 +@SYMTestCaseDesc	    Testing if POSIX waitpid function conforms to POSIX standard
    1.45 +@SYMTestPriority 	    High
    1.46 +@SYMTestActions  	    Starts a new processes and tests the POSIX waitpid function with 
    1.47 +						different parameters and checks if it behaves correctly
    1.48 +@SYMTestExpectedResults The test should not fail.
    1.49 +@SYMDEF 				INC073739
    1.50 +*/
    1.51 +
    1.52 +int test_waitpid()
    1.53 +	{
    1.54 +	test_Data;
    1.55 +	void* proc;
    1.56 +	void* proc2;
    1.57 +	int ret;
    1.58 +	int status=-1;
    1.59 +	test_Title("WAITPID");
    1.60 +	test_Next("waitpid");
    1.61 +	proc = create_process(doafunc, "A", "rw", fids);
    1.62 +	if (proc)
    1.63 +		start_process(proc);
    1.64 +	else
    1.65 +		perror("Failed to start process A: ");
    1.66 +	
    1.67 +	// test return values of calls to waitpid with different options and pid's
    1.68 +	
    1.69 +	if (proc)
    1.70 +		{
    1.71 +		// test with option WNOHANG and a correct pid on an active process
    1.72 +		ret = wait_for_process_id(proc,get_proc_id(proc),WNOHANG,&status);
    1.73 +		test(ret==0);
    1.74 +		// test with an invalid pid 
    1.75 +		ret = wait_for_process_id(proc,99,0,&status);
    1.76 +		test(errno==ECHILD&&ret==-1);
    1.77 +		// test with invalid options
    1.78 +		ret = wait_for_process_id(proc,99,17,&status);
    1.79 +		test(errno==EINVAL&&ret==-1);
    1.80 +		ret = wait_for_process_id(proc,99,18,&status);
    1.81 +		test(errno==EINVAL&&ret==-1);
    1.82 +		// process is still active keep waiting until it dies
    1.83 +		do 
    1.84 +			{
    1.85 +			ret = wait_for_process_id(proc,get_proc_id(proc),WNOHANG,&status);
    1.86 +			}
    1.87 +		while(!WIFEXITED(status)&&ret==0);
    1.88 +		test(ret==get_proc_id(proc));
    1.89 +   		printf("child exited, status=%d\n", WEXITSTATUS(status));
    1.90 +		}
    1.91 +
    1.92 +	// create another process and test waitpid with pid -1 and options 0
    1.93 +	// this is the same as calling wait
    1.94 +	proc2 = create_process(doafunc, "B", "rw", fids);
    1.95 +	if (proc2)
    1.96 +		start_process(proc2);
    1.97 +	else
    1.98 +		perror("Failed to start process B: ");
    1.99 +	if (proc2)
   1.100 +		{
   1.101 +		//wait for proc2 to finish
   1.102 +		do
   1.103 +			{
   1.104 +        	ret = wait_for_process_id(proc2,-1,0,&status);
   1.105 +    		}
   1.106 +    	while (!WIFEXITED(status));
   1.107 +		test(ret==get_proc_id(proc2));
   1.108 +    	printf("child exited, status=%d\n", WEXITSTATUS(status));
   1.109 +		}
   1.110 +		
   1.111 +	fflush(stdout);
   1.112 +	fclose(stdout);
   1.113 +	test_Close();
   1.114 +	return 0;
   1.115 +	}
   1.116 +
   1.117 +
   1.118 +
   1.119 +int main (int argc, char *argv[])
   1.120 +	{
   1.121 +
   1.122 +	start_redirection_server();
   1.123 +	if(argc>1)
   1.124 +		//do something with child process
   1.125 +		doafunc();
   1.126 +	else
   1.127 +		test_waitpid();
   1.128 +	// exit here for the moment crt0 libraries panic
   1.129 +	exit(0);
   1.130 +	return 0;
   1.131 +	}