/* * * Copyright (c) International Business Machines Corp., 2001 * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See * the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ /* * NAME * shmctl01.c * * DESCRIPTION * shmctl01 - test the IPC_STAT, IPC_SET and IPC_RMID commands as * they are used with shmctl() * * ALGORITHM * loop if that option was specified * create a shared memory segment with read and write permission * set up any test case specific conditions * call shmctl() using the TEST macro * check the return code * if failure, issue a FAIL message. * otherwise, * if doing functionality testing * call the correct test function * if the conditions are correct, * issue a PASS message * otherwise * issue a FAIL message * otherwise * issue a PASS message * call cleanup * * USAGE: * shmctl01 [-c n] [-f] [-i n] [-I x] [-P x] [-t] * where, -c n : Run n copies concurrently. * -f : Turn off functionality Testing. * -i n : Execute test n times. * -I x : Execute test for x seconds. * -P x : Pause for x seconds between iterations. * -t : Turn on syscall timing. * * HISTORY * 03/2001 - Written by Wayne Boyer * * RESTRICTIONS * none */ #include "ipcshm.h" char *TCID = "shmctl01"; extern int Tst_count; int shm_id_1 = -1; struct shmid_ds buf; long save_time; #define FIRST 0 #define SECOND 1 int stat_time; /* set to either FIRST or SECOND for IPC_STAT tests */ void *set_shared; #define N_ATTACH 4 pid_t pid_arr[N_ATTACH]; void sighandler(int); /* * These are the various setup and check functions for the commands * that we are checking. */ /* Setup, cleanup and check routines for IPC_STAT */ void stat_setup(void), func_stat(void); void stat_cleanup(void); /* Setup and check routines for IPC_SET */ void set_setup(void), func_set(void); /* Check routine for IPC_RMID */ void func_rmid(void); /* Child function */ void do_child(void); struct test_case_t { int cmd; /* the command to test */ void (*func_test)(void); /* the test function */ void (*func_setup)(void); /* the setup function if necessary */ } TC[] = { {IPC_STAT, func_stat, stat_setup}, #ifndef UCLINUX /* The second test is not applicable to uClinux; shared memory segments are detached on exec(), so cannot be passed to uClinux children. */ {IPC_STAT, func_stat, stat_setup}, #endif {IPC_SET, func_set, set_setup}, {IPC_RMID, func_rmid, NULL} }; int TST_TOTAL = (sizeof(TC) / sizeof(*TC)); #define NEWMODE 0066 #ifdef UCLINUX static char *argv0; #endif static int stat_i; /* Shared between do_child and stat_setup */ int main(int ac, char **av) { int lc; /* loop counter */ char *msg; /* message returned from parse_opts */ int i; void check_functionality(void); /* parse standard options */ if ((msg = parse_opts(ac, av, (option_t *)NULL, NULL)) != (char *)NULL){ tst_brkm(TBROK, cleanup, "OPTION PARSING ERROR - %s", msg); } #ifdef UCLINUX argv0 = av[0]; maybe_run_child(do_child, "ddd", &stat_i, &stat_time, &shm_id_1); #endif setup(); /* global setup */ /* The following loop checks looping state if -i option given */ for (lc = 0; TEST_LOOPING(lc); lc++) { /* reset Tst_count in case we are looping */ Tst_count = 0; /* initialize stat_time */ stat_time = FIRST; /* * Create a shared memory segment with read and write * permissions. Do this here instead of in setup() * so that looping (-i) will work correctly. */ if ((shm_id_1 = shmget(shmkey, SHM_SIZE, IPC_CREAT | IPC_EXCL | SHM_RW)) == -1) { tst_brkm(TBROK, cleanup, "couldn't create the shared" " memory segment"); } /* loop through the test cases */ for (i=0; i= buf.shm_ctime) { tst_resm(TFAIL, "change time is incorrect"); fail = 1; } if (fail) { return; } tst_resm(TPASS, "new mode and change time are correct"); } /* * func_rmid() - check the functionality of the IPC_RMID command with shmctl() */ void func_rmid() { /* Do another shmctl() - we should get EINVAL */ if (shmctl(shm_id_1, IPC_STAT, &buf) != -1) { tst_brkm(TBROK, cleanup, "shmctl succeeded on expected fail"); } if (errno != EINVAL) { tst_resm(TFAIL, "returned unexpected errno %d", errno); } else { tst_resm(TPASS, "shared memory appears to be removed"); } shm_id_1 = -1; } /* * sighandler() - handle signals, in this case SIGUSR1 is the only one expected */ void sighandler(sig) { if (sig != SIGUSR1) { tst_resm(TINFO, "received unexpected signal %d", sig); } } /* * setup() - performs all the ONE TIME setup for this test. */ void setup(void) { /* capture signals */ tst_sig(FORK, sighandler, cleanup); /* Pause if that option was specified */ TEST_PAUSE; /* * Create a temporary directory and cd into it. * This helps to ensure that a unique msgkey is created. * See ../lib/libipc.c for more information. */ tst_tmpdir(); /* get an IPC resource key */ shmkey = getipckey(); } /* * cleanup() - performs all the ONE TIME cleanup for this test at completion * or premature exit. */ void cleanup(void) { /* if it exists, remove the shared memory segment */ rm_shm(shm_id_1); /* Remove the temporary directory */ tst_rmdir(); /* * print timing stats if that option was specified. * print errno log if that option was specified. */ TEST_CLEANUP; /* exit with return code appropriate for results */ tst_exit(); }