Exemple #1
0
static int open_tmpfile(int flags) {
  const char *tmpdir;
  _cleanup_free_ char *p = NULL;
  int fd;

  tmpdir = getenv("TMPDIR");
  if (tmpdir == NULL) {
    tmpdir = "/tmp";
  }

#ifdef O_TMPFILE
  fd = open(tmpdir, flags | O_TMPFILE, S_IRUSR | S_IWUSR);
  if (fd >= 0) {
    return fd;
  }
#endif

  if (asprintf(&p, "%s/pkgfile-tmp-XXXXXX", tmpdir) < 0) {
    return -ENOMEM;
  }

  fd = mkostemp(p, flags);
  if (fd < 0) {
    return -errno;
  }

  /* ignore any (unlikely) error */
  unlink(p);

  return fd;
}
Exemple #2
0
int swTaskWorker_large_pack(swEventData *task, void *data, int data_len)
{
	swPackage_task pkg;
	memcpy(pkg.tmpfile, SwooleG.task_tmpdir, SwooleG.task_tmpdir_len);

#ifdef HAVE_MKOSTEMP
	int tpm_fd  = mkostemp(pkg.tmpfile, O_WRONLY);
#else
	int tpm_fd  = mkstemp(pkg.tmpfile);
#endif

	if (tpm_fd < 0)
	{
		swWarn("mkdtemp(%s) failed. Error: %s[%d]", pkg.tmpfile, strerror(errno), errno);
		return SW_ERR;
	}

	if (swoole_sync_writefile(tpm_fd, data, data_len) <=0)
	{
		swWarn("write to tmpfile failed.");
		return SW_ERR;
	}
	/**
	 * from_fd == 1, read from file
	 */
	task->info.from_fd = 1;
	task->info.len = sizeof(swPackage_task);
	pkg.length = data_len;
	memcpy(task->data, &pkg, sizeof(swPackage_task));
	return SW_OK;
}
Exemple #3
0
void
dbglock_init(void)
{
    if (dbglock_fd != -1)
        return;

    if (!dbg_enabled_p())
        return;

    const char envvar[] = "FB_ADB_DBGLOCK_NAME";
    /* No, we can't just inherit the file descriptor.  Without a
     * separate file open, taking the lock won't block.  */

    const char* fn = getenv(envvar);
    if (fn == NULL) {
        const char* pfx = DEFAULT_TEMP_DIR;
        char* tmpfname = xaprintf("%s/fb-adb-dbg-XXXXXX", pfx);
        struct cleanup* cl = cleanup_allocate();
        int tmpfd = mkostemp(tmpfname, O_CLOEXEC);
        if (tmpfd != -1) {
            setenv(envvar, tmpfname, 1);
            cleanup_commit(cl, cleanup_dbginit, tmpfname);
            dbglock_fd = tmpfd;
        }

        return;
    }

    dbglock_fd = open(fn, O_CLOEXEC | O_RDWR);
}
Exemple #4
0
static int
write_scores (const char *filename, mode_t mode,
	      const struct score_entry *scores, ptrdiff_t count)
{
  int fd;
  FILE *f;
  ptrdiff_t i;
  char *tempfile = malloc (strlen (filename) + strlen (".tempXXXXXX") + 1);
  if (!tempfile)
    return -1;
  strcpy (stpcpy (tempfile, filename), ".tempXXXXXX");
  fd = mkostemp (tempfile, 0);
  if (fd < 0)
    return -1;
#ifndef DOS_NT
  if (fchmod (fd, mode) != 0)
    return -1;
#endif
  f = fdopen (fd, "w");
  if (! f)
    return -1;
  for (i = 0; i < count; i++)
    if (fprintf (f, "%s %s\n", scores[i].score, scores[i].user_data) < 0)
      return -1;
  if (fclose (f) != 0)
    return -1;
  if (rename (tempfile, filename) != 0)
    return -1;
  return 0;
}
Exemple #5
0
static int open_tmpfile(int flags) {
  const char *tmpdir;
  char *p;
  int fd;

  tmpdir = getenv("TMPDIR");
  if (tmpdir == NULL) {
    tmpdir = "/tmp";
  }

#ifdef O_TMPFILE
  fd = open(tmpdir, flags | O_TMPFILE, S_IRUSR | S_IWUSR);
  if (fd >= 0) {
    return fd;
  }
#endif

  if (asprintf(&p, "%s/pkgfile-tmp-XXXXXX", tmpdir) < 0) {
    return -ENOMEM;
  }

  fd = mkostemp(p, flags);
  unlink(p);
  free(p);

  return fd;
}
Exemple #6
0
/*
 * Create and open a presmuably safe temp file for editing group data
 */
int
gr_tmp(int mfd)
{
	char buf[8192];
	ssize_t nr;
	const char *p;
	int tfd;

	if (*group_file == '\0')
		return (-1);
	if ((p = strrchr(group_file, '/')))
		++p;
	else
		p = group_file;
	if (snprintf(tempname, sizeof(tempname), "%.*sgroup.XXXXXX",
		(int)(p - group_file), group_file) >= (int)sizeof(tempname)) {
		errno = ENAMETOOLONG;
		return (-1);
	}
	if ((tfd = mkostemp(tempname, O_SYNC)) == -1)
		return (-1);
	if (mfd != -1) {
		while ((nr = read(mfd, buf, sizeof(buf))) > 0)
			if (write(tfd, buf, (size_t)nr) != nr)
				break;
		if (nr != 0) {
			unlink(tempname);
			*tempname = '\0';
			close(tfd);
			return (-1);
		}
	}
	return (tfd);
}
Exemple #7
0
//you need file name and then the size, and many things
int create_buffer(int size)
{
	//using the process_id as the fname, is it a good idea?
	//this is safe as 2^32 -1 is 4294967295
	const char *path = getenv("XDG_RUNTIME_DIR");
	char fname[strlen(path) + strlen(template_fname) + 1];
	int fd;

	sprintf(fname, "%s%s", path, template_fname);
#ifdef  HAVE_MKOSTEMP		
	fd = mkostemp(fname, O_CLOEXEC);
	if (fd > 0)
		unlink(fname);
	else
		return fd;
#else
	fd = mkstemp(fname);
	if (fd > 0) {
		unlink(fname);
		long flags = fcntl(fd, F_GETFD);
		if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) == -1) {
			close(fd);
			fd = -1;
		}
	} else
		return fd;
#endif
	if (ftruncate(fd, size) < 0) {
		close(fd);
		return -1;
	}
	return fd;
}
Exemple #8
0
/* Create open temporary file in safe way.  Please notice that the
 * file permissions are -rw------- by default. */
int xmkstemp(char **tmpname, char *dir)
{
	char *localtmp;
	char *tmpenv;
	mode_t old_mode;
	int fd, rc;

	/* Some use cases must be capable of being moved atomically
	 * with rename(2), which is the reason why dir is here.  */
	if (dir != NULL)
		tmpenv = dir;
	else
		tmpenv = getenv("TMPDIR");

	if (tmpenv)
		rc = asprintf(&localtmp, "%s/%s.XXXXXX", tmpenv,
			  program_invocation_short_name);
	else
		rc = asprintf(&localtmp, "%s/%s.XXXXXX", _PATH_TMP,
			  program_invocation_short_name);

	if (rc < 0)
		return -1;

	old_mode = umask(077);
	fd = mkostemp(localtmp, O_RDWR|O_CREAT|O_EXCL|O_CLOEXEC);
	umask(old_mode);
	if (fd == -1) {
		free(localtmp);
		localtmp = NULL;
	}
	*tmpname = localtmp;
	return fd;
}
Exemple #9
0
/* returns file descriptor or -errno, @name returns a unique filename
 */
int mnt_open_uniq_filename(const char *filename, char **name)
{
	int rc, fd;
	char *n;
	mode_t oldmode;

	if (!filename)
		return -EINVAL;
	if (name)
		*name = NULL;

	rc = asprintf(&n, "%s.XXXXXX", filename);
	if (rc <= 0)
		return -errno;

	/* This is for very old glibc and for compatibility with Posix, which says
	 * nothing about mkstemp() mode. All sane glibc use secure mode (0600).
	 */
	oldmode = umask(S_IRGRP|S_IWGRP|S_IXGRP|
			S_IROTH|S_IWOTH|S_IXOTH);
	fd = mkostemp(n, O_RDWR|O_CREAT|O_EXCL|O_CLOEXEC);
	if (fd < 0)
		fd = -errno;
	umask(oldmode);

	if (fd >= 0 && name)
		*name = n;
	else
		free(n);

	return fd;
}
Exemple #10
0
int
pocl_mk_tempname (char *output, const char *prefix, const char *suffix,
                  int *ret_fd)
{
#if defined(_MSC_VER) || defined(__MINGW32__)
#error "making temporary files on Windows not implemented"
#elif defined(HAVE_MKOSTEMPS) || defined(HAVE_MKSTEMPS) || defined(__ANDROID__)
  /* using mkstemp() instead of tmpnam() has no real benefit
   * here, as we have to pass the filename to llvm,
   * but tmpnam() generates an annoying warning... */
  int fd;

  strncpy (output, prefix, POCL_FILENAME_LENGTH);
  size_t len = strlen (prefix);
  strncpy (output + len, "_XXXXXX", (POCL_FILENAME_LENGTH - len));

#ifdef __ANDROID__
  fd = pocl_mkstemp (output);
#else
  if (suffix)
    {
      len += 7;
      strncpy (output + len, suffix, (POCL_FILENAME_LENGTH - len));
#ifdef HAVE_MKOSTEMPS
      fd = mkostemps (output, strlen (suffix), O_CLOEXEC);
#else
      fd = mkstemps (output, strlen (suffix));
#endif
    }
  else
#ifdef HAVE_MKOSTEMPS
    fd = mkostemp (output, O_CLOEXEC);
#else
    fd = mkstemp (output);
#endif
#endif

  if (fd < 0)
    {
      POCL_MSG_ERR ("mkstemp() failed\n");
      return errno;
    }

  int err = 0;
  if (ret_fd)
    *ret_fd = fd;
  else
    {
      err = close (fd);
    }
  return err ? errno : 0;
#else
#error mkostemps() / mkstemps() both unavailable
#endif
}
Exemple #11
0
LinesBlock::LinesBlock(size_t size)
{
	begin = 0;
	end = 0;
	mmap_ptr = reinterpret_cast<void *>(-1);
	mmap_size = 0;
	mmap_file = 0;
	readonly = false;
	valid = false;

	if (!size)
	{
		printf("Can't create zero size block\n");
		return;
	}

	char fn[16] = "tmp_XXXXXX";
	int fd = mkostemp(fn, O_RDWR | O_NOATIME);
	if (fd < 0)
	{
		printf("Can't create temp file: %s\n", strerror(errno));
		return;
	}
	if (lseek(fd, size - 1, SEEK_SET) == -1)
	{
		remove(fn);
		printf("Can't expand temp file: %s\n", strerror(errno));
		return;
	}
	char z = '\0';
	if (write(fd, &z, sizeof(z)) == -1)
	{
		remove(fn);
		printf("Can't expand temp file: %s\n", strerror(errno));
		return;
	}

	mmap_ptr = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
	close(fd);

	if (mmap_ptr == (void *)(-1))
	{
		remove(fn);
		printf("Can't memory mapping temp file: %s\n", strerror(errno));
		return;
	}
	mmap_size = size;
	mmap_file = strdup(fn);

	begin = static_cast<unsigned char *>(mmap_ptr);
	end = begin + size;
	valid = true;

	return;
}
Exemple #12
0
static int
createTmpfileCloexec(char* tmpname)
{
    int fd;

    fd = mkostemp(tmpname, O_CLOEXEC);
    if (fd >= 0)
        unlink(tmpname);

    return fd;
}
Exemple #13
0
static int jrnl_create(struct jrnl_descriptor *jd, const char *jrnl_dir)
{
	snprintf(jd->path, sizeof(jd->path), "%sXXXXXX", jrnl_dir);
	jd->fd = mkostemp(jd->path, O_DSYNC);

	if (jd->fd < 0) {
		sd_eprintf("failed to create %s: %m", jd->path);
		return SD_RES_UNKNOWN;
	}

	return SD_RES_SUCCESS;
}
static void *
tf_msync (void *arg)
{
  if (arg == NULL)
    // XXX If somebody can provide a portable test case in which msync()
    // blocks we can enable this test to run in both rounds.
    abort ();

  char *fname= strdup("/tmp/eatmydataXXXXXX");

  tempfd = mkostemp(fname, O_RDONLY);
  if (tempfd == -1)
    {
      printf ("%s: cannot open %s\n", __func__, fname);
      exit (1);
    }

  void *p = mmap (NULL, 10, PROT_READ, MAP_SHARED, tempfd, 0);
  if (p == MAP_FAILED)
    {
      printf ("%s: mmap failed\n", __func__);
      exit (1);
    }

  int r = pthread_barrier_wait (&b2);
  if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
    {
      printf ("%s: barrier_wait failed\n", __func__);
      exit (1);
    }

  r = pthread_barrier_wait (&b2);
  if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
    {
      printf ("%s: 2nd barrier_wait failed\n", __func__);
      exit (1);
    }

  pthread_cleanup_push (cl, NULL);

  msync (p, 10, 0);

  pthread_cleanup_pop (0);

  printf ("%s: msync returned\n", __func__);

  unlink(fname);
  free(fname);

  exit (1);
}
 static int createAnonymousFile(off_t size) {
     char tmpname[] = "XXXXXX";
     int fd = mkostemp(tmpname, O_CLOEXEC);
     if (fd >= 0) {
         unlink(tmpname);
     } else {
         throw WLException("Cannot create anonymous file");
     }
     if (posix_fallocate(fd, 0, size) != 0) {
         close(fd);
         throw WLException("Cannot allocate anonymous file");
     }
     return fd;
 }
Exemple #16
0
void process()
{
    char temp[] = "/tmp/tmp.XXXXXX";
    int fd;

    if ((fd = mkostemp(temp, O_WRONLY|O_CREAT|O_EXCL|O_TRUNC)) < 0)
    {
        //handle error
    }
    else
    {
        //process data using temp file
    }
}
Exemple #17
0
static int create_tmpfile_cloexec(char *tmpname) {
    int fd;

#ifdef HAVE_MKOSTEMP
    fd = mkostemp(tmpname, O_CLOEXEC);
    if (fd >= 0) unlink(tmpname);
#else
    fd = mkstemp(tmpname);
    if (fd >= 0) {
        fd = set_cloexec_or_close(fd);
        unlink(tmpname);
    }
#endif

    return fd;
}
Exemple #18
0
/* Open a temporary file name, and immediately unlink it.  */
static int
open_temp_exec_file_name (char *name, int flags)
{
  int fd;

#ifdef HAVE_MKOSTEMP
  fd = mkostemp (name, flags);
#else
  fd = mkstemp (name);
#endif

  if (fd != -1)
    unlink (name);

  return fd;
}
	int XdevLWindowWayland::createTmpFileCloExec(char *tmpname) {
		int fd;

#ifdef HAVE_MKOSTEMP
		fd = mkostemp(tmpname, O_CLOEXEC);
		if(fd >= 0)
			unlink(tmpname);
#else
		fd = mkstemp(tmpname);
		if(fd >= 0) {
			fd = setCloexecOrClose(fd);
			unlink(tmpname);
		}
#endif

		return fd;
	}
Exemple #20
0
int w_mkstemp(char *templ)
{
  int fd;

#ifdef HAVE_MKOSTEMP
  fd = mkostemp(templ, O_CLOEXEC);
#else
  fd = mkstemp(templ);
#endif

  if (fd == -1) {
    return -1;
  }

  w_set_cloexec(fd);

  return fd;
}
Exemple #21
0
int swoole_tmpfile(char *filename)
{
#if defined(HAVE_MKOSTEMP) && defined(HAVE_EPOLL)
    int tmp_fd = mkostemp(filename, O_WRONLY | O_CREAT);
#else
    int tmp_fd = mkstemp(filename);
#endif

    if (tmp_fd < 0)
    {
        swSysError("mkstemp(%s) failed.", filename);
        return SW_ERR;
    }
    else
    {
        return tmp_fd;
    }
}
Exemple #22
0
int swoole_tmpfile(char *filename)
{
#ifdef HAVE_MKOSTEMP
    int tmp_fd = mkostemp(filename, O_WRONLY);
#else
    int tmp_fd = mkstemp(filename);
#endif

    if (tmp_fd < 0)
    {
        swSysError("mkdtemp(%s) failed.", filename);
        return SW_ERR;
    }
    else
    {
        return tmp_fd;
    }
}
Exemple #23
0
static int
createTmpFileCloexec(char *tmpname)
{
    int fd;
#ifdef HAVE_MKOSTEMP
    fd = mkostemp(tmpname, O_CLOEXEC);
    if (fd >= 0){
        unlink(tmpname);
    }
#else
    fd = mkstemp(tmpname);
    if (fd >= 0){
        fd = setCloexecOrClose(fd);
        unlink(tmpname);
    }
#endif
    return fd;
}
bool env_universal_t::open_temporary_file(const wcstring &directory, wcstring *out_path, int *out_fd)
{
    /* Create and open a temporary file for writing within the given directory */
    /* Try to create a temporary file, up to 10 times. We don't use mkstemps because we want to open it CLO_EXEC. This should almost always succeed on the first try. */
    assert(! string_suffixes_string(L"/", directory));
    
    bool success = false;
    const wcstring tmp_name_template = directory + L"/fishd.tmp.XXXXXX";
    wcstring tmp_name;
    for (size_t attempt = 0; attempt < 10 && ! success; attempt++)
    {
        int result_fd = -1;
        char *narrow_str = wcs2str(tmp_name_template.c_str());
#if HAVE_MKOSTEMP
        result_fd = mkostemp(narrow_str, O_CLOEXEC);
        if (result_fd >= 0)
        {
            tmp_name = str2wcstring(narrow_str);
        }
#else
        if (mktemp(narrow_str))
        {
            /* It was successfully templated; try opening it atomically */
            tmp_name = str2wcstring(narrow_str);
            result_fd = wopen_cloexec(tmp_name, O_WRONLY | O_CREAT | O_EXCL | O_TRUNC, 0644);
        }
#endif
        
        if (result_fd >= 0)
        {
            /* Success */
            *out_fd = result_fd;
            *out_path = str2wcstring(narrow_str);
            success = true;
        }
        free(narrow_str);
    }
    if (! success)
    {
        int err = errno;
        report_error(err, L"Unable to open file '%ls'", tmp_name.c_str());
    }
    return success;
}
  inline int mkstemp(std::string& filename)
  {
#if defined(_WIN32)

    std::string path, name;
    detail::splitpath(filename, path, name);
    char temppath[MAX_PATH];
    // note: GetTempFileName only uses the first 3 chars of the prefix (name)
    // specified.
    if (GetTempFileName(path.c_str(), name.c_str(), 0, temppath) == 0)
    {
      // failed! return invalid handle.
      return -1;
    }
    int handle = -1;
    // _sopen_s sets handle to -1 on error.
    _sopen_s(&handle, temppath, _O_WRONLY | _O_CREAT | _O_BINARY, _SH_DENYNO, _S_IWRITE);
    if (handle != -1)
      filename = temppath;
    return handle;

#else // defined(_WIN32)

    std::unique_ptr<char[]> s_template(new char[filename.size() + 1]);
    std::copy(filename.begin(), filename.end(), s_template.get());
    s_template[filename.size()] = 0;

    int handle = -1;
#if defined(__MACH__)
    // TODO: figure out how to open with O_SYNC
    handle = ::mkstemp(s_template.get());
#else
    handle = mkostemp(s_template.get(), O_WRONLY | O_SYNC);
#endif
    if (handle != -1)
      filename = s_template.get();
    return handle;

#endif // defined(_WIN32)
  }
Exemple #26
0
int main(void)
{
  pid_t myPid;
  char tempFile[128];
  int fdtmp, count;

  strcpy(tempFile, "/tmp/fdinfo-XXXXXX");
  myPid = getpid();
  if((fdtmp = mkostemp(tempFile, O_CREAT | O_NONBLOCK | O_WRONLY | O_TRUNC)) == -1)
    {
      strerror(errno);
      exit(-1);
    }

  printf("Creating a temporary file: %s", tempFile);

  printf(" (fdtmp=%ld)\n", (long)fdtmp);

  printf("Before:\n");
  outputFileInformation(fdtmp, myPid);

  printf("Performing three write operations:\n");

  for(count = 0; count < 3; count++)
    {
      printf("\nPass %d:\n", count + 1);
      (void)write(fdtmp, "This is some data.\n", 19);
      outputFileInformation(fdtmp, myPid);
    }

  if(close(fdtmp) == -1)
    {
      strerror(errno);
      exit(-1);
    }

  return 0;
}
Exemple #27
0
static int
tmp(void)
{
	sigset_t set, oset;
	int fd, len;
	char *envtmp = NULL;
	char path[MAXPATHLEN];

	if (issetugid() == 0)
		envtmp = getenv("TMPDIR");
	len = snprintf(path,
	    sizeof(path), "%s/bt.XXXXXXXXXX", envtmp ? envtmp : "/tmp");
	if (len < 0 || len >= (int)sizeof(path)) {
		errno = ENAMETOOLONG;
		return(-1);
	}

	(void)sigfillset(&set);
	(void)_sigprocmask(SIG_BLOCK, &set, &oset);
	if ((fd = mkostemp(path, O_CLOEXEC)) != -1)
		(void)unlink(path);
	(void)_sigprocmask(SIG_SETMASK, &oset, NULL);
	return(fd);
}
/* Like mkostemp, but do not return STDIN_FILENO, STDOUT_FILENO, or
   STDERR_FILENO.  */
int
mkostemp_safer (char *templ, int flags)
{
  return fd_safer_flag (mkostemp (templ, flags), flags);
}
Exemple #29
0
static void test_parse_env_file(void) {
        char t[] = "/tmp/test-parse-env-file-XXXXXX";
        int fd, r;
        FILE *f;
        _cleanup_free_ char *one = NULL, *two = NULL, *three = NULL, *four = NULL, *five = NULL,
                        *six = NULL, *seven = NULL, *eight = NULL, *nine = NULL, *ten = NULL;
        _cleanup_strv_free_ char **a = NULL, **b = NULL;
        char **i;
        unsigned k;

        fd = mkostemp(t, O_CLOEXEC);
        assert_se(fd >= 0);

        f = fdopen(fd, "w");
        assert_se(f);

        fputs("one=BAR   \n"
              "# comment\n"
              " # comment \n"
              " ; comment \n"
              "  two   =   bar    \n"
              "invalid line\n"
              "invalid line #comment\n"
              "three = \"333\n"
              "xxxx\"\n"
              "four = \'44\\\"44\'\n"
              "five = \'55\\\'55\' \"FIVE\" cinco   \n"
              "six = seis sechs\\\n"
              " sis\n"
              "seven=\"sevenval\" #nocomment\n"
              "eight=eightval #nocomment\n"
              "export nine=nineval\n"
              "ten=", f);

        fflush(f);
        fclose(f);

        r = load_env_file(t, NULL, &a);
        assert_se(r >= 0);

        STRV_FOREACH(i, a)
                log_info("Got: <%s>", *i);

        assert_se(streq(a[0], "one=BAR"));
        assert_se(streq(a[1], "two=bar"));
        assert_se(streq(a[2], "three=333\nxxxx"));
        assert_se(streq(a[3], "four=44\"44"));
        assert_se(streq(a[4], "five=55\'55FIVEcinco"));
        assert_se(streq(a[5], "six=seis sechs sis"));
        assert_se(streq(a[6], "seven=sevenval#nocomment"));
        assert_se(streq(a[7], "eight=eightval #nocomment"));
        assert_se(streq(a[8], "export nine=nineval"));
        assert_se(streq(a[9], "ten="));
        assert_se(a[10] == NULL);

        strv_env_clean_log(a, "/tmp/test-fileio");

        k = 0;
        STRV_FOREACH(i, b) {
                log_info("Got2: <%s>", *i);
                assert_se(streq(*i, a[k++]));
        }
Exemple #30
0
int
octave_mkostemp_wrapper (char *tmpl)
{
  return mkostemp (tmpl, O_BINARY);
}