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