| |
| |
| |
| |
| |
| |
| #define _GNU_SOURCE |
| #include <time.h> |
| #include <fcntl.h> |
| #include <limits.h> |
| #include <stdlib.h> |
| #include <unistd.h> |
| #include <string.h> |
| #include <stdio.h> |
| #include <inttypes.h> |
| |
| #define PPRIX64 "%"PRIx64 |
| |
| |
| |
| extern int mkstemp(char *); |
| extern int mkostemp(char *, int); |
| |
| |
| static int tpax_tmpfile_by_framework(void) |
| { |
| #ifdef _MIDIPIX_ABI |
| extern int __fs_tmpfile(int); |
| return __fs_tmpfile(O_CLOEXEC); |
| #else |
| return (-1); |
| #endif |
| } |
| |
| |
| static int tpax_tmpfile_by_kernel(void) |
| { |
| #ifdef O_TMPFILE |
| return openat(AT_FDCWD,"/tmp",O_RDWR|O_TMPFILE|O_CLOEXEC,0); |
| #else |
| return (-1); |
| #endif |
| } |
| |
| |
| static int tpax_mkostemp(char * tmplate) |
| { |
| int fd; |
| #ifdef HAVE_NO_MKOSTEMP |
| if ((fd = mkstemp(tmplate)) >= 0) |
| fcntl(fd,F_SETFD,FD_CLOEXEC); |
| #else |
| fd = mkostemp(tmplate,O_CLOEXEC); |
| #endif |
| return fd; |
| } |
| |
| int tpax_tmpfile(void) |
| { |
| int fd; |
| void * addr; |
| char tmplate[128]; |
| |
| |
| if ((fd = tpax_tmpfile_by_framework()) >= 0) |
| return fd; |
| |
| |
| if ((fd = tpax_tmpfile_by_kernel()) >= 0) |
| return fd; |
| |
| |
| addr = tmplate; |
| memset(tmplate,0,sizeof(tmplate)); |
| snprintf(tmplate,sizeof(tmplate), |
| "/tmp/" |
| ".tpax.tmpfile" |
| ".time."PPRIX64 |
| ".salt.%p" |
| ".pid.%d" |
| ".XXXXXXXXXXXX", |
| time(0), |
| addr, |
| getpid()); |
| |
| return tpax_mkostemp(tmplate); |
| } |