Skip to content

Commit 3062fb2

Browse files
authored
Merge pull request #530 from drolevar/fix_master
Fix several multi-arch-related issues
2 parents 13d4721 + b687b16 commit 3062fb2

File tree

9 files changed

+473
-197
lines changed

9 files changed

+473
-197
lines changed

src/Makefile

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -168,17 +168,20 @@ all: ${LIBS} ${BINS}
168168

169169
libfaketimeMT.o: EXTRA_FLAGS := -DPTHREAD_SINGLETHREADED_TIME
170170

171-
${LIBS_OBJ}: libfaketime.c
171+
ft_sem.o: ft_sem.c ft_sem.h
172+
${CC} -o $@ -c ${CFLAGS} ${CPPFLAGS} $<
173+
174+
${LIBS_OBJ}: libfaketime.c ft_sem.h
172175
${CC} -o $@ -c ${CFLAGS} ${CPPFLAGS} ${EXTRA_FLAGS} $<
173176

174-
%.so.${SONAME}: %.o libfaketime.map
175-
${CC} -o $@ -Wl,-soname,$@ ${LDFLAGS} ${LIB_LDFLAGS} $< ${LDADD}
177+
%.so.${SONAME}: %.o ft_sem.o libfaketime.map
178+
${CC} -o $@ -Wl,-soname,$@ ${LDFLAGS} ${LIB_LDFLAGS} $< ft_sem.o ${LDADD}
176179

177-
${BINS}: faketime.c
178-
${CC} -o $@ ${CFLAGS} ${CPPFLAGS} ${EXTRA_FLAGS} $< ${LDFLAGS} ${BIN_LDFLAGS}
180+
${BINS}: faketime.c ft_sem.o ft_sem.h
181+
${CC} -o $@ ${CFLAGS} ${CPPFLAGS} ${EXTRA_FLAGS} $< ft_sem.o ${LDFLAGS} ${BIN_LDFLAGS}
179182

180183
clean:
181-
@rm -f ${LIBS_OBJ} ${LIBS} ${BINS}
184+
@rm -f ${LIBS_OBJ} ${LIBS} ${BINS} ft_sem.o
182185

183186
distclean: clean
184187
@echo

src/faketime.c

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
#include <sys/types.h>
4545
#include <sys/wait.h>
4646
#include <sys/mman.h>
47-
#include <semaphore.h>
47+
#include "ft_sem.h"
4848

4949
#include "faketime_common.h"
5050

@@ -60,6 +60,7 @@ static const char *date_cmd = "date";
6060

6161
/* semaphore and shared memory names */
6262
char sem_name[PATH_BUFSIZE] = {0}, shm_name[PATH_BUFSIZE] = {0};
63+
static ft_sem_t wrapper_sem;
6364

6465
void usage(const char *name)
6566
{
@@ -98,9 +99,9 @@ void usage(const char *name)
9899
/** Clean up shared objects */
99100
static void cleanup_shobjs()
100101
{
101-
if (-1 == sem_unlink(sem_name))
102+
if (-1 == ft_sem_unlink(&wrapper_sem))
102103
{
103-
perror("faketime: sem_unlink");
104+
perror("faketime: ft_sem_unlink");
104105
}
105106
if (-1 == shm_unlink(shm_name))
106107
{
@@ -255,9 +256,8 @@ int main (int argc, char **argv)
255256
curr_opt++;
256257

257258
{
258-
/* create semaphores and shared memory */
259+
/* create lock and shared memory */
259260
int shm_fd;
260-
sem_t *sem;
261261
struct ft_shared_s *ft_shared;
262262
char shared_objs[PATH_BUFSIZE * 2 + 1];
263263

@@ -271,26 +271,23 @@ int main (int argc, char **argv)
271271
snprintf(sem_name, PATH_BUFSIZE -1 ,"/faketime_sem_%ld", (long)getpid());
272272
snprintf(shm_name, PATH_BUFSIZE -1 ,"/faketime_shm_%ld", (long)getpid());
273273

274-
if (SEM_FAILED == (sem = sem_open(sem_name, O_CREAT|O_EXCL, S_IWUSR|S_IRUSR, 1)))
274+
if (-1 == ft_sem_create(sem_name, &wrapper_sem))
275275
{
276-
perror("faketime: sem_open");
277-
fprintf(stderr, "The faketime wrapper only works on platforms that support the sem_open()\nsystem call. However, you may LD_PRELOAD libfaketime without using this wrapper.\n");
276+
perror("faketime: ft_sem_create");
277+
fprintf(stderr, "The faketime wrapper failed to create its lock.\nHowever, you may LD_PRELOAD libfaketime without using this wrapper.\n");
278278
exit(EXIT_FAILURE);
279279
}
280280

281281
/* create shm */
282282
if (-1 == (shm_fd = shm_open(shm_name, O_CREAT|O_EXCL|O_RDWR, S_IWUSR|S_IRUSR)))
283283
{
284284
perror("faketime: shm_open");
285-
if (-1 == sem_unlink(argv[2]))
286-
{
287-
perror("faketime: sem_unlink");
288-
}
285+
ft_sem_unlink(&wrapper_sem);
289286
exit(EXIT_FAILURE);
290287
}
291288

292289
/* set shm size */
293-
if (-1 == ftruncate(shm_fd, sizeof(uint64_t)))
290+
if (-1 == ftruncate(shm_fd, sizeof(struct ft_shared_s)))
294291
{
295292
perror("faketime: ftruncate");
296293
cleanup_shobjs();
@@ -306,22 +303,26 @@ int main (int argc, char **argv)
306303
exit(EXIT_FAILURE);
307304
}
308305

309-
if (sem_wait(sem) == -1)
306+
if (ft_sem_lock(&wrapper_sem) == -1)
310307
{
311-
perror("faketime: sem_wait");
308+
perror("faketime: ft_sem_lock");
312309
cleanup_shobjs();
313310
exit(EXIT_FAILURE);
314311
}
315312

316313
/* init elapsed time ticks to zero */
317314
ft_shared->ticks = 0;
318315
ft_shared->file_idx = 0;
319-
ft_shared->start_time.real.tv_sec = 0;
320-
ft_shared->start_time.real.tv_nsec = -1;
321-
ft_shared->start_time.mon.tv_sec = 0;
322-
ft_shared->start_time.mon.tv_nsec = -1;
323-
ft_shared->start_time.mon_raw.tv_sec = 0;
324-
ft_shared->start_time.mon_raw.tv_nsec = -1;
316+
ft_shared->start_time_real.sec = 0;
317+
ft_shared->start_time_real.nsec = -1;
318+
ft_shared->start_time_mon.sec = 0;
319+
ft_shared->start_time_mon.nsec = -1;
320+
ft_shared->start_time_mon_raw.sec = 0;
321+
ft_shared->start_time_mon_raw.nsec = -1;
322+
#ifdef CLOCK_BOOTTIME
323+
ft_shared->start_time_boot.sec = 0;
324+
ft_shared->start_time_boot.nsec = -1;
325+
#endif
325326

326327
if (-1 == munmap(ft_shared, (sizeof(struct ft_shared_s))))
327328
{
@@ -330,16 +331,16 @@ int main (int argc, char **argv)
330331
exit(EXIT_FAILURE);
331332
}
332333

333-
if (sem_post(sem) == -1)
334+
if (ft_sem_unlock(&wrapper_sem) == -1)
334335
{
335-
perror("faketime: semop");
336+
perror("faketime: ft_sem_unlock");
336337
cleanup_shobjs();
337338
exit(EXIT_FAILURE);
338339
}
339340

340341
snprintf(shared_objs, sizeof(shared_objs), "%s %s", sem_name, shm_name);
341342
setenv("FAKETIME_SHARED", shared_objs, true);
342-
sem_close(sem);
343+
ft_sem_close(&wrapper_sem);
343344
}
344345

345346
{

src/faketime_common.h

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ struct system_time_s
3838
#endif
3939
};
4040

41+
/* Fixed-width time representation for shared memory (arch-independent) */
42+
struct ft_shared_time_s
43+
{
44+
int64_t sec;
45+
int64_t nsec;
46+
};
47+
4148
/* Data shared among faketime-spawned processes */
4249
struct ft_shared_s
4350
{
@@ -47,8 +54,13 @@ struct ft_shared_s
4754
uint64_t ticks;
4855
/* Index of timestamp to be loaded from file */
4956
uint64_t file_idx;
50-
/* System time Faketime started at */
51-
struct system_time_s start_time;
57+
/* System time Faketime started at (fixed-width for cross-arch safety) */
58+
struct ft_shared_time_s start_time_real;
59+
struct ft_shared_time_s start_time_mon;
60+
struct ft_shared_time_s start_time_mon_raw;
61+
#ifdef CLOCK_BOOTTIME
62+
struct ft_shared_time_s start_time_boot;
63+
#endif
5264
};
5365

5466
/* These are all needed in order to properly build on OSX */

0 commit comments

Comments
 (0)