Skip to content

Commit

Permalink
Minor source code style fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Wolfgang Hommel committed Nov 7, 2013
1 parent e653c38 commit 1068186
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 101 deletions.
36 changes: 26 additions & 10 deletions src/faketime.c
Expand Up @@ -61,7 +61,6 @@ static const char *date_cmd = "date";
/* semaphore and shared memory names */
char sem_name[PATH_BUFSIZE] = {0}, shm_name[PATH_BUFSIZE] = {0};


void usage(const char *name)
{
printf("\n");
Expand Down Expand Up @@ -109,7 +108,8 @@ int main (int argc, char **argv)
int pfds[2];
long offset;

while(curr_opt < argc) {
while(curr_opt < argc)
{
if (0 == strcmp(argv[curr_opt], "-m"))
{
use_mt = true;
Expand Down Expand Up @@ -164,7 +164,7 @@ int main (int argc, char **argv)
close(pfds[0]); /* we don't need this */
if (EXIT_SUCCESS != execlp(date_cmd, date_cmd, "-d", argv[curr_opt], "+%s",(char *) NULL))
{
perror("Running (g)date failed");
perror("Running (g)date failed");
exit(EXIT_FAILURE);
}
}
Expand All @@ -176,7 +176,7 @@ int main (int argc, char **argv)
waitpid(child_pid, &ret, 0);
if (ret != EXIT_SUCCESS)
{
printf("Error: Timestamp to fake not recognized, please re-try with a "
printf("Error: Timestamp to fake not recognized, please re-try with a "
"different timestamp.\n");
exit(EXIT_FAILURE);
}
Expand Down Expand Up @@ -216,7 +216,7 @@ int main (int argc, char **argv)
perror("shm_open");
if (-1 == sem_unlink(argv[2]))
{
perror("sem_unlink");
perror("sem_unlink");
}
exit(EXIT_FAILURE);
}
Expand Down Expand Up @@ -271,7 +271,6 @@ int main (int argc, char **argv)

snprintf(shared_objs, PATH_BUFSIZE, "%s %s", sem_name, shm_name);
setenv("FAKETIME_SHARED", shared_objs, true);

}

{
Expand All @@ -280,10 +279,12 @@ int main (int argc, char **argv)
ftpl_path = PREFIX "/libfaketime.1.dylib";
FILE *check;
check = fopen(ftpl_path, "ro");
if (check == NULL) {
if (check == NULL)
{
ftpl_path = PREFIX "/lib/faketime/libfaketime.1.dylib";
}
else {
else
{
fclose(check);
}
setenv("DYLD_INSERT_LIBRARIES", ftpl_path, true);
Expand All @@ -298,15 +299,15 @@ int main (int argc, char **argv)
* on MultiArch platforms, such as Debian, we put a literal $LIB into LD_PRELOAD.
*/
#ifndef MULTI_ARCH
ftpl_path = PREFIX LIBDIRNAME "/libfaketimeMT.so.1";
ftpl_path = PREFIX LIBDIRNAME "/libfaketimeMT.so.1";
#else
ftpl_path = PREFIX "/$LIB/faketime/libfaketimeMT.so.1";
#endif
}
else
{
#ifndef MULTI_ARCH
ftpl_path = PREFIX LIBDIRNAME "/libfaketime.so.1";
ftpl_path = PREFIX LIBDIRNAME "/libfaketime.so.1";
#else
ftpl_path = PREFIX "/$LIB/faketime/libfaketime.so.1";
#endif
Expand Down Expand Up @@ -338,3 +339,18 @@ int main (int argc, char **argv)
exit(ret);
}
}

/*
* Editor modelines
*
* Local variables:
* c-basic-offset: 2
* tab-width: 2
* indent-tabs-mode: nil
* End:
*
* vi: set shiftwidth=2 tabstop=2 expandtab:
* :indentSize=2:tabSize=2:noTabs=true:
*/

/* eof */
99 changes: 57 additions & 42 deletions src/libfaketime.c
Expand Up @@ -89,23 +89,22 @@ typedef int clockid_t;
#define CLOCK_MONOTONIC_RAW (CLOCK_MONOTONIC + 1)
#endif


/*
* Per thread variable, which we turn on inside real_* calls to avoid modifying
* time multiple times of for the whole process to prevent faking time
*/
__thread bool dont_fake = false;

/* Wrapper for function calls, which we want to return system time */
#define DONT_FAKE_TIME(call) \
{ \
bool dont_fake_orig = dont_fake; \
if (!dont_fake) \
{ \
dont_fake = true; \
} \
call; \
dont_fake = dont_fake_orig; \
#define DONT_FAKE_TIME(call) \
{ \
bool dont_fake_orig = dont_fake; \
if (!dont_fake) \
{ \
dont_fake = true; \
} \
call; \
dont_fake = dont_fake_orig; \
} while (0)

/* pointers to real (not faked) functions */
Expand Down Expand Up @@ -257,6 +256,7 @@ static void ft_shm_init (void)
perror("shm_open");
exit(1);
}

if (MAP_FAILED == (ft_shared = mmap(NULL, sizeof(struct ft_shared_s), PROT_READ|PROT_WRITE,
MAP_SHARED, ticks_shm_fd, 0)))
{
Expand Down Expand Up @@ -365,10 +365,12 @@ static void save_time(struct timespec *tp)
}

lseek(outfile, 0, SEEK_END);
do {
do
{
written = write(outfile, &(((char*)&time_write)[n]), sizeof(time_write) - n);
} while (((written == -1) && (errno == EINTR)) ||
(sizeof(time_write) < (n += written)));
}
while (((written == -1) && (errno == EINTR)) ||
(sizeof(time_write) < (n += written)));

if ((written == -1) || (n < sizeof(time_write)))
{
Expand Down Expand Up @@ -494,7 +496,7 @@ static inline void fake_stat64buf (struct stat64 *buf) {
int __xstat (int ver, const char *path, struct stat *buf)
{
if (NULL == real_stat)
{ /* dlsym() failed */
{ /* dlsym() failed */
#ifdef DEBUG
(void) fprintf(stderr, "faketime problem: original stat() not found.\n");
#endif
Expand Down Expand Up @@ -552,7 +554,7 @@ int __fxstat (int ver, int fildes, struct stat *buf)
int __fxstatat(int ver, int fildes, const char *filename, struct stat *buf, int flag)
{
if (NULL == real_fstatat)
{ /* dlsym() failed */
{ /* dlsym() failed */
#ifdef DEBUG
(void) fprintf(stderr, "faketime problem: original fstatat() not found.\n");
#endif
Expand All @@ -568,7 +570,8 @@ int __fxstatat(int ver, int fildes, const char *filename, struct stat *buf, int

if (buf != NULL)
{
if (!fake_stat_disabled) {
if (!fake_stat_disabled)
{
fake_statbuf(buf);
}
}
Expand Down Expand Up @@ -608,7 +611,7 @@ int __lxstat (int ver, const char *path, struct stat *buf)
int __xstat64 (int ver, const char *path, struct stat64 *buf)
{
if (NULL == real_stat64)
{ /* dlsym() failed */
{ /* dlsym() failed */
#ifdef DEBUG
(void) fprintf(stderr, "faketime problem: original stat() not found.\n");
#endif
Expand Down Expand Up @@ -896,7 +899,8 @@ int ppoll(struct pollfd *fds, nfds_t nfds,
/* cast away constness */
real_timeout_pt = (struct timespec *)timeout_ts;
}
} else
}
else
{
real_timeout_pt = NULL;
}
Expand Down Expand Up @@ -1013,7 +1017,7 @@ timer_settime_common(timer_t_or_int timerid, int flags,
DONT_FAKE_TIME(result = (*real_timer_settime_22)(timerid.int_member, flags,
new_real_pt, old_value));
break;
case FT_COMPAT_GLIBC_2_3_3:
case FT_COMPAT_GLIBC_2_3_3:
DONT_FAKE_TIME(result = (*real_timer_settime_233)(timerid.timer_t_member,
flags, new_real_pt, old_value));
break;
Expand Down Expand Up @@ -1315,7 +1319,9 @@ static void parse_ft_string(const char *user_faked_time)
user_faked_time_timespec.tv_sec = mktime(&user_faked_time_tm);
user_faked_time_timespec.tv_nsec = 0;
user_faked_time_set = true;
} else {
}
else
{
perror("Failed to parse FAKETIME timestamp");
exit(EXIT_FAILURE);
}
Expand Down Expand Up @@ -1423,12 +1429,14 @@ void __attribute__ ((constructor)) ftpl_init(void)
#ifdef FAKE_TIMERS
real_timer_settime_22 = dlvsym(RTLD_NEXT, "timer_settime","GLIBC_2.2");
real_timer_settime_233 = dlvsym(RTLD_NEXT, "timer_settime","GLIBC_2.3.3");
if (NULL == real_timer_settime_233) {
if (NULL == real_timer_settime_233)
{
real_timer_settime_233 = dlsym(RTLD_NEXT, "timer_settime");
}
real_timer_gettime_22 = dlvsym(RTLD_NEXT, "timer_gettime","GLIBC_2.2");
real_timer_gettime_233 = dlvsym(RTLD_NEXT, "timer_gettime","GLIBC_2.3.3");
if (NULL == real_timer_gettime_233) {
if (NULL == real_timer_gettime_233)
{
real_timer_gettime_233 = dlsym(RTLD_NEXT, "timer_gettime");
}
#endif
Expand All @@ -1445,11 +1453,14 @@ void __attribute__ ((constructor)) ftpl_init(void)
/* Check whether we actually should be faking the returned timestamp. */

/* We can prevent faking time for specified commands */
if ((tmp_env = getenv("FAKETIME_SKIP_CMDS")) != NULL) {
if ((tmp_env = getenv("FAKETIME_SKIP_CMDS")) != NULL)
{
char *skip_cmd, *saveptr;
skip_cmd = strtok_r(tmp_env, ",", &saveptr);
while (skip_cmd != NULL) {
if (0 == strcmp(progname, skip_cmd)) {
while (skip_cmd != NULL)
{
if (0 == strcmp(progname, skip_cmd))
{
ft_mode = FT_NOOP;
dont_fake = true;
break;
Expand All @@ -1459,25 +1470,30 @@ void __attribute__ ((constructor)) ftpl_init(void)
}

/* We can limit faking time to specified commands */
if ((tmp_env = getenv("FAKETIME_ONLY_CMDS")) != NULL) {
if ((tmp_env = getenv("FAKETIME_ONLY_CMDS")) != NULL)
{
char *only_cmd, *saveptr;
bool cmd_matched = false;

if (getenv("FAKETIME_SKIP_CMDS") != NULL) {
if (getenv("FAKETIME_SKIP_CMDS") != NULL)
{
fprintf(stderr, "Error: Both FAKETIME_SKIP_CMDS and FAKETIME_ONLY_CMDS can't be set.\n");
exit(EXIT_FAILURE);
}

only_cmd = strtok_r(tmp_env, ",", &saveptr);
while (only_cmd != NULL) {
if (0 == strcmp(progname, only_cmd)) {
while (only_cmd != NULL)
{
if (0 == strcmp(progname, only_cmd))
{
cmd_matched = true;
break;
}
only_cmd = strtok_r(NULL, ",", &saveptr);
}

if (!cmd_matched) {
if (!cmd_matched)
{
ft_mode = FT_NOOP;
dont_fake = true;
}
Expand Down Expand Up @@ -1680,12 +1696,12 @@ int fake_clock_gettime(clockid_t clk_id, struct timespec *tp)
{
case CLOCK_REALTIME:
timespecsub(tp, &ftpl_starttime.real, &tmp_ts);
break;
break;
case CLOCK_MONOTONIC:
timespecsub(tp, &ftpl_starttime.mon, &tmp_ts);
break;
case CLOCK_MONOTONIC_RAW:
timespecsub(tp, &ftpl_starttime.mon_raw, &tmp_ts);
timespecsub(tp, &ftpl_starttime.mon_raw, &tmp_ts);
break;
default:
printf("Invalid clock_id for clock_gettime: %d", clk_id);
Expand All @@ -1695,12 +1711,12 @@ int fake_clock_gettime(clockid_t clk_id, struct timespec *tp)
if (limited_faking)
{
/* Check whether we actually should be faking the returned timestamp. */
/* fprintf(stderr, "(libfaketime limits -> runtime: %lu, callcounter: %lu\n", (*time_tptr - ftpl_starttime), callcounter); */
/* fprintf(stderr, "(libfaketime limits -> runtime: %lu, callcounter: %lu\n", (*time_tptr - ftpl_starttime), callcounter); */
if ((ft_start_after_secs != -1) && (tmp_ts.tv_sec < ft_start_after_secs)) return 0;
if ((ft_stop_after_secs != -1) && (tmp_ts.tv_sec >= ft_stop_after_secs)) return 0;
if ((ft_stop_after_secs != -1) && (tmp_ts.tv_sec >= ft_stop_after_secs)) return 0;
if ((ft_start_after_ncalls != -1) && (callcounter < ft_start_after_ncalls)) return 0;
if ((ft_stop_after_ncalls != -1) && (callcounter >= ft_stop_after_ncalls)) return 0;
/* fprintf(stderr, "(libfaketime limits -> runtime: %lu, callcounter: %lu continues\n", (*time_tptr - ftpl_starttime), callcounter); */
if ((ft_stop_after_ncalls != -1) && (callcounter >= ft_stop_after_ncalls)) return 0;
/* fprintf(stderr, "(libfaketime limits -> runtime: %lu, callcounter: %lu continues\n", (*time_tptr - ftpl_starttime), callcounter); */
}

if (spawnsupport)
Expand Down Expand Up @@ -1735,7 +1751,6 @@ int fake_clock_gettime(clockid_t clk_id, struct timespec *tp)

if (cache_expired == 1)
{

last_data_fetch = tp->tv_sec;
/* Can be enabled for testing ...
fprintf(stderr, "***************++ Cache expired ++**************\n");
Expand Down Expand Up @@ -1788,7 +1803,7 @@ int fake_clock_gettime(clockid_t clk_id, struct timespec *tp)
{
if (load_time(tp))
{
return 0;
return 0;
}
}

Expand All @@ -1805,13 +1820,13 @@ int fake_clock_gettime(clockid_t clk_id, struct timespec *tp)
case FT_START_AT: /* User-specified offset */
if (user_per_tick_inc_set)
{
/* increment time with every time() call*/
/* increment time with every time() call*/
next_time(tp, &user_per_tick_inc);
}
else
{
/* Speed-up / slow-down contributed by Karl Chen in v0.8 */
struct timespec tdiff, timeadj;
struct timespec tdiff, timeadj;
switch (clk_id)
{
case CLOCK_REALTIME:
Expand Down Expand Up @@ -1887,7 +1902,8 @@ int fake_gettimeofday(struct timeval *tv)
* clock_gettime implementation for __APPLE__
* @note It always behave like being called with CLOCK_REALTIME.
*/
static int apple_clock_gettime(clockid_t clk_id, struct timespec *tp) {
static int apple_clock_gettime(clockid_t clk_id, struct timespec *tp)
{
int result;
mach_timespec_t cur_timeclockid_t;
(void) clk_id; /* unused */
Expand Down Expand Up @@ -2052,7 +2068,6 @@ int __ftime(struct timeb *tb)
return result; /* will always be 0 (see manpage) */
}


#endif

/*
Expand Down

0 comments on commit 1068186

Please sign in to comment.