/* Check if negative offsets are handled correctly by mmap. */ static int do_test (void) { const int prot = PROT_READ | PROT_WRITE; const int flags = MAP_SHARED; const unsigned long length = 0x10000; const unsigned long offset = 0xace00000; const unsigned long size = offset + length; void *addr; int fd; char fname[] = "tst-mmap-offset-XXXXXX"; fd = mkstemp64 (fname); if (fd < 0) return printmsg (1, "mkstemp"); if (unlink (fname)) return printmsg (1, "unlink"); if (ftruncate64 (fd, size)) return printmsg (0, "ftruncate64"); addr = mmap (NULL, length, prot, flags, fd, offset); if (MAP_FAILED == addr) return printmsg (1, "mmap"); /* This memcpy is likely to SIGBUS if mmap has messed up with offset. */ memcpy (addr, fname, sizeof (fname)); return 0; }
int lfs_mkstemp(char *templ) { #ifdef __linux__ return mkstemp64(templ); #else return mkstemp(templ); #endif }
int file_cache_begin(struct file_cache *f, const char *path, char *txn) { int result; txn_name(f, path, txn); result = mkstemp64(txn); if(result >= 0) { debug(D_CACHE, "begin %s %s", path, txn); fchmod(result, 0700); } return result; }
void do_prepare (int argc, char *argv[]) { size_t name_len; struct rlimit64 rlim; name_len = strlen (test_dir); name = malloc (name_len + sizeof ("/lfsXXXXXX")); mempcpy (mempcpy (name, test_dir, name_len), "/lfsXXXXXX", sizeof ("/lfsXXXXXX")); add_temp_file (name); /* Open our test file. */ fd = mkstemp64 (name); if (fd == -1) { if (errno == ENOSYS) { /* Fail silently. */ error (0, 0, "open64 is not supported"); exit (EXIT_SUCCESS); } else error (EXIT_FAILURE, errno, "cannot create temporary file"); } if (getrlimit64 (RLIMIT_FSIZE, &rlim) != 0) { error (0, errno, "cannot get resource limit"); exit (0); } if (rlim.rlim_cur < TWO_GB + 200) { rlim.rlim_cur = TWO_GB + 200; if (setrlimit64 (RLIMIT_FSIZE, &rlim) != 0) { error (0, errno, "cannot reset file size limits"); exit (0); } } }
/*ARGSUSED*/ static FILE * _common(boolean_t large_file) { char tfname[L_tmpnam]; FILE *p; char *q; int mkret; mode_t current_umask; (void) strcpy(tfname, P_tmpdir); lmutex_lock(&seed_lk); (void) strcat(tfname, seed); (void) strcat(tfname, XS); q = seed; while (*q == 'z') *q++ = 'a'; if (*q != '\0') ++*q; lmutex_unlock(&seed_lk); #if !defined(_LP64) if (large_file == B_TRUE) { if ((mkret = mkstemp64(tfname)) == -1) return (NULL); } else #endif if ((mkret = mkstemp(tfname)) == -1) return (NULL); (void) unlink(tfname); current_umask = umask(0777); (void) umask(current_umask); (void) fchmod(mkret, 0666 & ~current_umask); if ((p = fdopen(mkret, "w+")) == NULL) { (void) close(mkret); return (NULL); } return (p); }
HB_FHANDLE hb_fsCreateTempEx( char * pszName, const char * pszDir, const char * pszPrefix, const char * pszExt, HB_FATTR ulAttr ) { /* less attemps */ int iAttemptLeft = 99, iLen; HB_FHANDLE fd; do { pszName[ 0 ] = '\0'; if( pszDir && pszDir[ 0 ] != '\0' ) { hb_strncpy( pszName, pszDir, HB_PATH_MAX - 1 ); iLen = ( int ) strlen( pszName ); if( pszName[ iLen - 1 ] != HB_OS_PATH_DELIM_CHR && iLen < HB_PATH_MAX - 1 ) { pszName[ iLen ] = HB_OS_PATH_DELIM_CHR; pszName[ iLen + 1 ] = '\0'; } } else hb_fsTempDir( pszName ); if( pszPrefix ) hb_strncat( pszName, pszPrefix, HB_PATH_MAX - 1 ); iLen = ( int ) strlen( pszName ); if( iLen > ( HB_PATH_MAX - 1 ) - 6 - ( pszExt ? ( int ) strlen( pszExt ) : 0 ) ) { fd = FS_ERROR; break; } #if defined( HB_HAS_MKSTEMP ) if( hb_setGetFileCase() != HB_SET_CASE_LOWER && hb_setGetFileCase() != HB_SET_CASE_UPPER && hb_setGetDirCase() != HB_SET_CASE_LOWER && hb_setGetDirCase() != HB_SET_CASE_UPPER #if ! defined( HB_HAS_MKSTEMPS ) && ( pszExt == NULL || *pszExt == 0 ) #endif ) { hb_vmUnlock(); hb_strncat( pszName, "XXXXXX", HB_PATH_MAX - 1 ); #if defined( HB_HAS_MKSTEMPS ) if( pszExt && *pszExt ) { hb_strncat( pszName, pszExt, HB_PATH_MAX - 1 ); #if defined( HB_USE_LARGEFILE64 ) fd = ( HB_FHANDLE ) mkstemps64( pszName, ( int ) strlen( pszExt ) ); #else fd = ( HB_FHANDLE ) mkstemps( pszName, ( int ) strlen( pszExt ) ); #endif } else #endif #if defined( HB_USE_LARGEFILE64 ) fd = ( HB_FHANDLE ) mkstemp64( pszName ); #else fd = ( HB_FHANDLE ) mkstemp( pszName ); #endif hb_fsSetIOError( fd != ( HB_FHANDLE ) -1, 0 ); hb_vmLock(); } else #endif /* HB_HAS_MKSTEMP */ { int i, n; double d = hb_random_num(), x; for( i = 0; i < 6; i++ ) { d = d * 36; n = ( int ) d; d = modf( d, &x ); pszName[ iLen++ ] = ( char ) ( n + ( n > 9 ? 'a' - 10 : '0' ) ); } pszName[ iLen ] = '\0'; if( pszExt ) hb_strncat( pszName, pszExt, HB_PATH_MAX - 1 ); fd = hb_fsCreateEx( pszName, ulAttr, FO_EXCLUSIVE | FO_EXCL ); } if( fd != ( HB_FHANDLE ) FS_ERROR ) break; } while( --iAttemptLeft ); return fd; }
/* Write the actual file name at fname. */ static FILE * gp_open_scratch_file_generic(const gs_memory_t *mem, const char *prefix, char fname[gp_file_name_sizeof], const char *mode, bool b64) { /* The -8 is for XXXXXX plus a possible final / and -. */ int prefix_length = strlen(prefix); int len = gp_file_name_sizeof - prefix_length - 8; FILE *fp; if (gp_file_name_is_absolute(prefix, prefix_length)) *fname = 0; else if (gp_gettmpdir(fname, &len) != 0) strcpy(fname, "/tmp/"); else { if (strlen(fname) != 0 && fname[strlen(fname) - 1] != '/') strcat(fname, "/"); } if (strlen(fname) + prefix_length + 8 >= gp_file_name_sizeof) return 0; /* file name too long */ strcat(fname, prefix); /* Prevent trailing X's in path from being converted by mktemp. */ if (*fname != 0 && fname[strlen(fname) - 1] == 'X') strcat(fname, "-"); strcat(fname, "XXXXXX"); #ifdef HAVE_MKSTEMP { int file; char ofname[gp_file_name_sizeof]; /* save the old filename template in case mkstemp fails */ memcpy(ofname, fname, gp_file_name_sizeof); #ifdef HAVE_MKSTEMP64 if (b64) file = mkstemp64(fname); else file = mkstemp(fname); #else file = mkstemp(fname); #endif if (file < -1) { emprintf1(mem, "**** Could not open temporary file %s\n", ofname); return NULL; } #if defined(O_LARGEFILE) && defined(__hpux) if (b64) fcntl(file, F_SETFD, fcntl(file, F_GETFD) | O_LARGEFILE); #else /* Fixme : what to do with b64 and 32-bit mkstemp? Unimplemented. */ #endif fp = fdopen(file, mode); if (fp == NULL) close(file); } #else mktemp(fname); fp = (b64 ? gp_fopentemp : gp_fopentemp_64)(fname, mode); #endif if (fp == NULL) emprintf1(mem, "**** Could not open temporary file %s\n", fname); return fp; }