Exemplo n.º 1
0
int main(int ac, char **av)
{
	struct stat stat_buf;	/* struct buffer to hold file info. */
	int lc;			/* loop counter */
	char *msg;		/* message returned from parse_opts */
	time_t modf_time, access_time;
	time_t pres_time;	/* file modification/access/present time */

	/* Parse standard options given to run the test. */
	msg = parse_opts(ac, av, NULL, NULL);
	if (msg != NULL) {
		tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);

	}

	setup();

	/*
	 * check if the current filesystem is nfs
	 */
	if (tst_is_cwd_nfs()) {
		tst_brkm(TCONF, cleanup,
			 "Cannot do utime on a file located on an NFS filesystem");
	}

        if (tst_is_cwd_v9fs()) {
                tst_brkm(TCONF, cleanup,
                         "Cannot do utime on a file located on an 9P filesystem");
        }

	/* set the expected errnos... */
	TEST_EXP_ENOS(exp_enos);

	for (lc = 0; TEST_LOOPING(lc); lc++) {

		Tst_count = 0;

		/*
		 * Invoke utime(2) to set TEMP_FILE access and
		 * modification times to the current time.
		 */
		TEST(utime(TEMP_FILE, NULL));

		if (TEST_RETURN == -1) {
			TEST_ERROR_LOG(TEST_ERRNO);
			tst_resm(TFAIL, "utime(%s) Failed, errno=%d : %s",
				 TEMP_FILE, TEST_ERRNO, strerror(TEST_ERRNO));
		} else {
			/*
			 * Perform functional verification if test
			 * executed without (-f) option.
			 */
			if (STD_FUNCTIONAL_TEST) {
				/*
				 * Sleep for a second so that mod time and
				 * access times will be different from the
				 * current time
				 */
				sleep(2);

				/*
				 * Get the current time now, after calling
				 * utime(2)
				 */
				if ((pres_time = time(&tloc)) < 0) {
					tst_brkm(TFAIL, cleanup, "time() "
						 "failed to get present time "
						 "after utime, error=%d",
						 errno);
				 }

				/*
				 * Get the modification and access times of
				 * temporary file using stat(2).
				 */
				if (stat(TEMP_FILE, &stat_buf) < 0) {
					tst_brkm(TFAIL, cleanup, "stat(2) of "
						 "%s failed, error:%d",
						 TEMP_FILE, TEST_ERRNO);
				 }
				modf_time = stat_buf.st_mtime;
				access_time = stat_buf.st_atime;

				/* Now do the actual verification */
				if (modf_time <= curr_time ||
				    modf_time >= pres_time ||
				    access_time <= curr_time ||
				    access_time >= pres_time) {
					tst_resm(TFAIL, "%s access and "
						 "modification times not set",
						 TEMP_FILE);
				} else {
					tst_resm(TPASS, "Functionality of "
						 "utime(%s, NULL) successful",
						 TEMP_FILE);
				}
			} else {
				tst_resm(TPASS, "%s call succeeded", TCID);
			}
		}
		Tst_count++;
	}

	cleanup();
	tst_exit();

}
Exemplo n.º 2
0
int main(int ac, char **av)
{
	struct stat stat_buf;	/* struct buffer to hold file info. */
	int lc;			/* loop counter */
	char *msg;		/* message returned from parse_opts */
	time_t modf_time, access_time;
	time_t pres_time;	/* file modification/access/present time */
	pid_t pid;

	/* Parse standard options given to run the test. */
	msg = parse_opts(ac, av, NULL, NULL);
	if (msg != NULL) {
		tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);

	}

	setup();

	/*
	 * check if the current filesystem is nfs
	 */
	if (tst_is_cwd_nfs()) {
		tst_brkm(TCONF, cleanup,
			 "Cannot do utime on a file located on an NFS filesystem");
	}

        if (tst_is_cwd_v9fs()) {
                tst_brkm(TCONF, cleanup,
                         "Cannot do utime on a file located on an 9P filesystem");
        }

	/* set the expected errnos... */
	TEST_EXP_ENOS(exp_enos);

	pid = FORK_OR_VFORK();

	if (pid == -1) {
		tst_brkm(TBROK, cleanup, "fork() failed");
	} else if (pid == 0) {
		if ((ltpuser = getpwnam(LTPUSER1)) == NULL) {
			tst_brkm(TBROK, cleanup, "%s not found in /etc/passwd",
				 LTPUSER1);
		}

		/* get uid/gid of user accordingly */
		user_uid = ltpuser->pw_uid;

		seteuid(user_uid);

		for (lc = 0; TEST_LOOPING(lc); lc++) {

			Tst_count = 0;

			/*
			 * Invoke utime(2) to set TEMP_FILE access and
			 * modification times to the current time.
			 */
			TEST(utime(TEMP_FILE, NULL));

			if (TEST_RETURN == -1) {
				TEST_ERROR_LOG(TEST_ERRNO);
				tst_resm(TFAIL,
					 "utime(%s) Failed, errno=%d : %s",
					 TEMP_FILE, TEST_ERRNO,
					 strerror(TEST_ERRNO));
			} else {
				/*
				 * Perform functional verification if test
				 * executed without (-f) option.
				 */
				if (STD_FUNCTIONAL_TEST) {
					/*
					 * Sleep for a second so that mod time
					 * and access times will be different
					 * from the current time.
					 */
					sleep(2);

					/*
					 * Get the current time now, after
					 * calling utime(2)
					 */
					if ((pres_time = time(&tloc)) < 0) {
						tst_brkm(TFAIL, cleanup,
							 "time() failed to get "
							 "present time after "
							 "utime, error=%d",
							 errno);
					 }

					/*
					 * Get the modification and access
					 * times of temporary file using
					 * stat(2).
					 */
					if (stat(TEMP_FILE, &stat_buf) < 0) {
						tst_brkm(TFAIL, cleanup,
							 "stat(2) of %s failed, "
							 "error:%d", TEMP_FILE,
							 TEST_ERRNO);
					 }
					modf_time = stat_buf.st_mtime;
					access_time = stat_buf.st_atime;

					/* Now do the actual verification */
					if (modf_time <= curr_time ||
					    modf_time >= pres_time ||
					    access_time <= curr_time ||
					    access_time >= pres_time) {
						tst_resm(TFAIL, "%s access and "
							 "modification times "
							 "not set", TEMP_FILE);
					} else {
						tst_resm(TPASS, "Functionality "
							 "of utime(%s, NULL) "
							 "successful",
							 TEMP_FILE);
					}
				} else {
					tst_resm(TPASS, "%s call succeeded",
						 TCID);
				}
			}
			Tst_count++;	/* incr. TEST_LOOP counter */
		}
	} else {
		waitpid(pid, &status, 0);
		_exit(0);	/*
				 * Exit here and let the child clean up.
				 * This allows the errno information set
				 * by the TEST_ERROR_LOG macro and the
				 * PASS/FAIL status to be preserved for
				 * use during cleanup.
				 */
	}

	cleanup();
	tst_exit();

}