1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
|
/*
* FCRON - periodic command scheduler
*
* Copyright 2000-2006 Thibault Godouet <fcron@free.fr>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* The GNU General Public License can also be found in the file
* `LICENSE' that comes with the fcron source distribution.
*/
/* $Id: temp_file.c,v 1.5 2006/01/11 00:54:06 thib Exp thib $ */
#include "global.h"
#include "temp_file.h"
extern char *tmp_path;
extern char debug_opt;
int
temp_file(char **name)
/* Open a temporary file and return its file descriptor */
/* Returns the filename to *name if name is not null. */
/* (die on error) */
{
int fd;
#ifdef HAVE_MKSTEMP
char name_local[PATH_LEN] = "";
strncpy(name_local, tmp_path, sizeof(name_local) - 1);
name_local[sizeof(name_local)-1] = '\0';
strcat(name_local, "fcr-XXXXXX");
if ( (fd = mkstemp(name_local)) == -1 )
die_e("Can't find a unique temporary filename");
/* we must set the file mode to 600 (some versions of mkstemp may set it
* incorrectly) */
if ( fchmod(fd, S_IWUSR | S_IRUSR) != 0 )
die_e("Can't fchmod temp file");
#else
const int max_retries = 50;
char *name_local = NULL;
int i;
i = 0;
do {
i++;
Set(name_local, tempnam(NULL, NULL));
if ( name_local == NULL )
die("Can't find a unique temporary filename");
fd = open(name_local, O_RDWR|O_CREAT|O_EXCL|O_APPEND, S_IRUSR|S_IWUSR);
/* I'm not sure we actually need to be so persistent here */
} while (fd == -1 && errno == EEXIST && i < max_retries);
if (fd == -1)
die_e("Can't open temporary file");
#endif
if ( name == NULL && unlink(name_local) != 0 )
die_e("Can't unlink temporary file %s", name_local);
fcntl(fd, F_SETFD, 1); /* set close-on-exec flag */
/* give the name of the temp file if necessary */
if (name != NULL)
*name = strdup2(name_local);
#ifndef HAVE_MKSTEMP
free(name_local);
#endif
return fd;
}
|