Blame src/internal/tpax_tmpfile_impl.c

5874a9
/**************************************************************/
5874a9
/*  tpax: a topological pax implementation                    */
5874a9
/*  Copyright (C) 2020--2021  SysDeer Technologies, LLC       */
5874a9
/*  Released under GPLv2 and GPLv3; see COPYING.TPAX.         */
5874a9
/**************************************************************/
4ebdc3
4ebdc3
#define _GNU_SOURCE
1b3749
#include <time.h>
4ebdc3
#include <fcntl.h>
559cff
#include <limits.h>
4ebdc3
#include <stdlib.h>
4ebdc3
#include <unistd.h>
4ebdc3
#include <string.h>
4ebdc3
#include <stdio.h>
1b3749
#include <inttypes.h>
1b3749
1b3749
#define PPRIX64 "%"PRIx64
4ebdc3
4ebdc3
/* mkostemp might be guarded by non-standard macros */
4ebdc3
/* unless HAVE_NO_MKOSTEMP, assume it is available */
4ebdc3
extern int mkstemp(char *);
4ebdc3
extern int mkostemp(char *, int);
4ebdc3
4ebdc3
/* __fs_tmpfile() atomically provides a private tmpfile */
4ebdc3
static int tpax_tmpfile_by_framework(void)
4ebdc3
{
4ebdc3
#ifdef _MIDIPIX_ABI
4ebdc3
	extern int __fs_tmpfile(int);
4ebdc3
	return __fs_tmpfile(O_CLOEXEC);
4ebdc3
#else
4ebdc3
	return (-1);
4ebdc3
#endif
4ebdc3
}
4ebdc3
4ebdc3
/* O_TMPFILE atomically provides a private tmpfile */
4ebdc3
static int tpax_tmpfile_by_kernel(void)
4ebdc3
{
4ebdc3
#ifdef O_TMPFILE
13eac9
	return openat(AT_FDCWD,"/tmp",O_RDWR|O_TMPFILE|O_CLOEXEC,0);
4ebdc3
#else
4ebdc3
	return (-1);
4ebdc3
#endif
4ebdc3
}
4ebdc3
4ebdc3
/* mk{o}stemp() provides a non-private tmpfile */
4ebdc3
static int tpax_mkostemp(char * tmplate)
4ebdc3
{
4ebdc3
	int fd;
4ebdc3
#ifdef HAVE_NO_MKOSTEMP
4ebdc3
	if ((fd = mkstemp(tmplate)) >= 0)
4ebdc3
		fcntl(fd,F_SETFD,FD_CLOEXEC);
4ebdc3
#else
4ebdc3
	fd = mkostemp(tmplate,O_CLOEXEC);
4ebdc3
#endif
4ebdc3
	return fd;
4ebdc3
}
4ebdc3
4ebdc3
int tpax_tmpfile(void)
4ebdc3
{
4ebdc3
	int             fd;
1b3749
	void *          addr;
1b3749
	char            tmplate[128];
4ebdc3
4ebdc3
	/* try with __fs_tmpfile() */
4ebdc3
	if ((fd = tpax_tmpfile_by_framework()) >= 0)
4ebdc3
		return fd;
4ebdc3
4ebdc3
	/* try with O_TMPFILE */
4ebdc3
	if ((fd = tpax_tmpfile_by_kernel()) >= 0)
4ebdc3
		return fd;
4ebdc3
4ebdc3
	/* fallback to mk{o}stemp */
1b3749
	addr = tmplate;
4ebdc3
	memset(tmplate,0,sizeof(tmplate));
1b3749
	snprintf(tmplate,sizeof(tmplate),
1b3749
		"/tmp/"
1b3749
		".tpax.tmpfile"
1b3749
		".time."PPRIX64
1b3749
		".salt.%p"
1b3749
		".pid.%d"
1b3749
		".XXXXXXXXXXXX",
1b3749
		time(0),
1b3749
		addr,
1b3749
		getpid());
4ebdc3
4ebdc3
	return tpax_mkostemp(tmplate);
4ebdc3
}