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 
     75 void setup();			/* Main setup function of test */
     76 void cleanup();			/* cleanup function for the test */
     77 
     78 char *TCID = "getpid02";
     79 int TST_TOTAL = 1;
     80 
     81 int main(int ac, char **av)
     82 {
     83 	int lc;
     84 	pid_t proc_id;		/* process id of the test process */
     85 	pid_t pid;		/* process id of the child process */
     86 	pid_t pproc_id;		/* parent process id */
     87 	int status;		/* exit status of child process */
     88 
     89 	tst_parse_opts(ac, av, NULL, NULL);
     90 
     91 	setup();
     92 
     93 	for (lc = 0; TEST_LOOPING(lc); lc++) {
     94 
     95 		tst_count = 0;
     96 
     97 		TEST(getpid());
     98 
     99 		proc_id = TEST_RETURN;
    100 
    101 		if ((pid = FORK_OR_VFORK()) == -1)
    102 			tst_resm(TFAIL | TERRNO, "fork failed");
    103 		else if (pid == 0) {
    104 			pproc_id = getppid();
    105 
    106 			if (pproc_id != proc_id)
    107 				exit(1);
    108 			exit(0);
    109 		} else {
    110 			if (wait(&status) == -1)
    111 				tst_brkm(TBROK | TERRNO, cleanup,
    112 					 "wait failed");
    113 			if (!WIFEXITED(status) ||
    114 			    WEXITSTATUS(status) != 0)
    115 				tst_resm(TFAIL, "getpid() returned "
    116 					 "invalid pid %d", proc_id);
    117 			else
    118 				tst_resm(TPASS,
    119 					 "getpid functionality is correct");
    120 		}
    121 	}
    122 
    123 	cleanup();
    124 	tst_exit();
    125 }
    126 
    127 void setup(void)
    128 {
    129 
    130 	tst_sig(FORK, DEF_HANDLER, cleanup);
    131 
    132 	TEST_PAUSE;
    133 }
    134 
    135 void cleanup(void)
    136 {
    137 }
    138