Home | History | Annotate | Download | only in symlink
      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 : symlink04
     22  *
     23  * Test Description :
     24  *  Verify that, symlink will succeed to creat a symbolic link of an existing
     25  *  object name path.
     26  *
     27  * Expected Result:
     28  *  symlink() should return value 0 on success and symbolic link of an
     29  *  existing object should be created.
     30  *
     31  * Algorithm:
     32  *  Setup:
     33  *   Setup signal handling.
     34  *   Create temporary directory.
     35  *   Pause for SIGUSR1 if option specified.
     36  *
     37  *  Test:
     38  *   Loop if the proper options are given.
     39  *   Execute system call
     40  *   Check return code, if system call failed (return=-1)
     41  *   	Log the errno and Issue a FAIL message.
     42  *   Otherwise,
     43  *   	Verify the Functionality of system call
     44  *      if successful,
     45  *      	Issue Functionality-Pass message.
     46  *      Otherwise,
     47  *		Issue Functionality-Fail message.
     48  *  Cleanup:
     49  *   Print errno log and/or timing stats if options given
     50  *   Delete the temporary directory created.
     51  *
     52  * Usage:  <for command-line>
     53  *  symlink04 [-c n] [-e] [-f] [-i n] [-I x] [-p x] [-t]
     54  *	where,  -c n : Run n copies concurrently.
     55  *		-e   : Turn on errno logging.
     56  *		-f   : Turn off functionality Testing.
     57  *		-i n : Execute test n times.
     58  *		-I x : Execute test for x seconds.
     59  *		-P x : Pause for x seconds between iterations.
     60  *		-t   : Turn on syscall timing.
     61  *
     62  * History
     63  *	07/2001 John George
     64  *		-Ported
     65  *
     66  * Restrictions:
     67  *  None.
     68  *
     69  */
     70 
     71 #include <stdio.h>
     72 #include <sys/types.h>
     73 #include <fcntl.h>
     74 #include <errno.h>
     75 #include <string.h>
     76 #include <signal.h>
     77 #include <sys/stat.h>
     78 #include "test.h"
     79 #include "safe_macros.h"
     80 
     81 #define  TESTFILE	"testfile"
     82 #define  SYMFILE	"slink_file"
     83 #define FILE_MODE       S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
     84 
     85 char *TCID = "symlink04";
     86 int TST_TOTAL = 1;
     87 
     88 void setup();
     89 void cleanup();
     90 
     91 int main(int ac, char **av)
     92 {
     93 	struct stat stat_buf;	/* stat structure buffer */
     94 	int lc;
     95 
     96 	tst_parse_opts(ac, av, NULL, NULL);
     97 
     98 	setup();
     99 
    100 	for (lc = 0; TEST_LOOPING(lc); lc++) {
    101 
    102 		tst_count = 0;
    103 
    104 		/*
    105 		 * Call symlink(2) to create a symlink of
    106 		 * testfile.
    107 		 */
    108 		TEST(symlink(TESTFILE, SYMFILE));
    109 
    110 		if (TEST_RETURN == -1) {
    111 			tst_resm(TFAIL, "symlink(%s, %s) Failed, errno=%d : %s",
    112 				 TESTFILE, SYMFILE, TEST_ERRNO,
    113 				 strerror(TEST_ERRNO));
    114 		} else {
    115 			/*
    116 			 * Get the symlink file status information
    117 			 * using lstat(2).
    118 			 */
    119 			if (lstat(SYMFILE, &stat_buf) < 0) {
    120 				tst_brkm(TFAIL, cleanup, "lstat(2) of "
    121 					 "%s failed, error:%d", SYMFILE,
    122 					 errno);
    123 			}
    124 
    125 			/* Check if the st_mode contains a link  */
    126 			if (!S_ISLNK(stat_buf.st_mode)) {
    127 				tst_resm(TFAIL,
    128 					 "symlink of %s doesn't exist",
    129 					 TESTFILE);
    130 			} else {
    131 				tst_resm(TPASS, "symlink(%s, %s) "
    132 					 "functionality successful",
    133 					 TESTFILE, SYMFILE);
    134 			}
    135 		}
    136 
    137 		/* Unlink the symlink file for next loop */
    138 		SAFE_UNLINK(cleanup, SYMFILE);
    139 		tst_count++;	/* incr TEST_LOOP counter */
    140 	}
    141 
    142 	cleanup();
    143 	tst_exit();
    144 
    145 }
    146 
    147 /*
    148  * void
    149  * setup() - performs all ONE TIME setup for this test.
    150  *  Create a temporary directory and change directory to it.
    151  *  Create a test file under temporary directory and close it
    152  */
    153 void setup(void)
    154 {
    155 	int fd;			/* file handle for testfile */
    156 
    157 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
    158 
    159 	/* Pause if that option was specified
    160 	 * TEST_PAUSE contains the code to fork the test with the -i option.
    161 	 * You want to make sure you do this before you create your temporary
    162 	 * directory.
    163 	 */
    164 	TEST_PAUSE;
    165 
    166 	tst_tmpdir();
    167 
    168 	/* creat/open a testfile */
    169 	if ((fd = open(TESTFILE, O_RDWR | O_CREAT, FILE_MODE)) == -1) {
    170 		tst_brkm(TBROK, cleanup,
    171 			 "open(%s, O_RDWR|O_CREAT, %#o) Failed, errno=%d : %s",
    172 			 TESTFILE, FILE_MODE, errno, strerror(errno));
    173 	}
    174 
    175 	/* Close the temporary file created above */
    176 	if (close(fd) == -1) {
    177 		tst_resm(TBROK, "close(%s) Failed, errno=%d : %s",
    178 			 TESTFILE, errno, strerror(errno));
    179 	}
    180 }
    181 
    182 /*
    183  * void
    184  * cleanup() - performs all ONE TIME cleanup for this test at
    185  *             completion or premature exit.
    186  *  Remove the test directory and testfile created in the setup.
    187  */
    188 void cleanup(void)
    189 {
    190 
    191 	tst_rmdir();
    192 
    193 }
    194