Home | History | Annotate | Download | only in getpid
      1 /*
      2  *
      3  *   Copyright (c) International Business Machines  Corp., 2001
      4  *
      5  *   This program is free software;  you can redistribute it and/or modify
      6  *   it under the terms of the GNU General Public License as published by
      7  *   the Free Software Foundation; either version 2 of the License, or
      8  *   (at your option) any later version.
      9  *
     10  *   This program is distributed in the hope that it will be useful,
     11  *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
     12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
     13  *   the GNU General Public License for more details.
     14  *
     15  *   You should have received a copy of the GNU General Public License
     16  *   along with this program;  if not, write to the Free Software
     17  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
     18  */
     19 
     20 /*
     21  * Test Name: getpid02
     22  *
     23  * Test Description:
     24  *  Verify that getpid() system call gets the process ID of the of the
     25  *  calling process.
     26  *
     27  * Expected Result:
     28  *  getpid() should return pid of the process on success.
     29  *
     30  * Algorithm:
     31  *  Setup:
     32  *   Setup signal handling.
     33  *   Pause for SIGUSR1 if option specified.
     34  *
     35  *  Test:
     36  *   Loop if the proper options are given.
     37  *   Execute system call
     38  *   Check return code, if system call failed (return=-1)
     39  *   	Log the errno and Issue a FAIL message.
     40  *   Otherwise,
     41  *   	Verify the Functionality of system call
     42  *      if successful,
     43  *      	Issue Functionality-Pass message.
     44  *      Otherwise,
     45  *		Issue Functionality-Fail message.
     46  *  Cleanup:
     47  *   Print errno log and/or timing stats if options given
     48  *
     49  * Usage:  <for command-line>
     50  *  getpid02 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
     51  *     where,  -c n : Run n copies concurrently.
     52  *             -f   : Turn off functionality Testing.
     53  *	       -i n : Execute test n times.
     54  *	       -I x : Execute test for x seconds.
     55  *	       -P x : Pause for x seconds between iterations.
     56  *	       -t   : Turn on syscall timing.
     57  *
     58  * HISTORY
     59  *	07/2001 Ported by Wayne Boyer
     60  *
     61  * RESTRICTIONS:
     62  *  None.
     63  */
     64 
     65 #include <unistd.h>
     66 #include <sys/types.h>
     67 #include <errno.h>
     68 #include <unistd.h>
     69 #include <string.h>
     70 #include <signal.h>
     71 #include <sys/wait.h>
     72 
     73 #include "test.h"
     74 #include "safe_macros.h"
     75 
     76 void setup();			/* Main setup function of test */
     77 void cleanup();			/* cleanup function for the test */
     78 
     79 char *TCID = "getpid02";
     80 int TST_TOTAL = 1;
     81 
     82 int main(int ac, char **av)
     83 {
     84 	int lc;
     85 	pid_t proc_id;		/* process id of the test process */
     86 	pid_t pid;		/* process id of the child process */
     87 	pid_t pproc_id;		/* parent process id */
     88 	int status;		/* exit status of child process */
     89 
     90 	tst_parse_opts(ac, av, NULL, NULL);
     91 
     92 	setup();
     93 
     94 	for (lc = 0; TEST_LOOPING(lc); lc++) {
     95 
     96 		tst_count = 0;
     97 
     98 		TEST(getpid());
     99 
    100 		proc_id = TEST_RETURN;
    101 
    102 		if ((pid = FORK_OR_VFORK()) == -1)
    103 			tst_resm(TFAIL | TERRNO, "fork failed");
    104 		else if (pid == 0) {
    105 			pproc_id = getppid();
    106 
    107 			if (pproc_id != proc_id)
    108 				exit(1);
    109 			exit(0);
    110 		} else {
    111 			SAFE_WAIT(cleanup, &status);
    112 			if (!WIFEXITED(status) ||
    113 			    WEXITSTATUS(status) != 0)
    114 				tst_resm(TFAIL, "getpid() returned "
    115 					 "invalid pid %d", proc_id);
    116 			else
    117 				tst_resm(TPASS,
    118 					 "getpid functionality is correct");
    119 		}
    120 	}
    121 
    122 	cleanup();
    123 	tst_exit();
    124 }
    125 
    126 void setup(void)
    127 {
    128 
    129 	tst_sig(FORK, DEF_HANDLER, cleanup);
    130 
    131 	TEST_PAUSE;
    132 }
    133 
    134 void cleanup(void)
    135 {
    136 }
    137