]>
diplodocus.org Git - nmh/blob - sbr/m_mktemp.c
1 /* m_mktemp.c -- Construct a temporary file.
3 * This code is Copyright (c) 2010, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
10 #include <h/signals.h>
11 #include "m_maildir.h"
14 static void register_for_removal(const char *);
17 /* Create a temporary file. If pfx_in is null, the temporary file
18 * will be created in the temporary directory (more on that later).
19 * If pfx_in is not null, then the temporary file location will be
20 * defined by the value pfx_in.
22 * The file created will be at the pathname specified appended with
23 * 6 random (we hope :) characters.
25 * The return value will be the pathname to the file created.
27 * CAUTION: The return pointer references static data. If
28 * you need to modify, or save, the return string, make a copy of it
31 * When pfx_in is null, the temporary directory is determined as
36 * User's mail directory.
38 * NOTE: One will probably use m_mktemp2() instead of this function.
39 * For example, if you want to create a temp file in the defined
40 * temporary directory, but with a custom basename prefix, do
41 * something like the following:
43 * char *tmp_pathname = m_mktemp2(NULL, "mypre", ...);
47 const char *pfx_in
, /* Pathname prefix for temporary file. */
48 int *fd_ret
, /* (return,optional) File descriptor to temp file. */
49 FILE **fp_ret
/* (return,optional) FILE pointer to temp file. */
52 static char tmpfil
[BUFSIZ
];
55 mode_t oldmode
= umask(077);
58 snprintf(tmpfil
, sizeof(tmpfil
), "%s/nmhXXXXXX", get_temp_dir());
60 snprintf(tmpfil
, sizeof(tmpfil
), "%sXXXXXX", pfx_in
);
70 register_for_removal(tmpfil
);
77 FILE *fp
= fdopen(fd
, "w+");
80 (void) m_unlink(tmpfil
);
96 /* This version allows one to specify the directory the temp file should
97 * by created based on a given pathname. Although m_mktemp() technically
98 * supports this, this version is when the directory is defined by
99 * a separate variable from the prefix, eliminating the caller from having
100 * to do string manipulation to generate the desired pathname prefix.
102 * The pfx_in parameter specifies a basename prefix for the file. If dir_in
103 * is NULL, then the defined temporary directory (see comments to m_mktemp()
104 * above) is used to create the temp file.
108 const char *dir_in
, /* Directory to place temp file. */
109 const char *pfx_in
, /* Basename prefix for temp file. */
110 int *fd_ret
, /* (return,optional) File descriptor to temp file. */
111 FILE **fp_ret
/* (return,optional) FILE pointer to temp file. */
114 static char buffer
[BUFSIZ
];
118 if (dir_in
== NULL
) {
119 if (pfx_in
== NULL
) {
120 return m_mktemp(NULL
, fd_ret
, fp_ret
);
122 snprintf(buffer
, sizeof(buffer
), "%s/%s", get_temp_dir(), pfx_in
);
123 return m_mktemp(buffer
, fd_ret
, fp_ret
);
126 if ((cp
= r1bindex ((char *)dir_in
, '/')) == dir_in
) {
127 /* No directory component */
128 return m_mktemp(pfx_in
, fd_ret
, fp_ret
);
130 n
= (int)(cp
-dir_in
); /* Length of dir component */
131 snprintf(buffer
, sizeof(buffer
), "%.*s%s", n
, dir_in
, pfx_in
);
132 return m_mktemp(buffer
, fd_ret
, fp_ret
);
137 * This version supports a suffix for the temp filename.
138 * It has two other differences from m_mktemp() and m_mktemp2():
139 * 1) It does not support specification of a directory for the temp
140 * file. Instead it always uses get_temp_dir().
141 * 2) It returns a dynamically allocated string. The caller is
142 * responsible for freeing the allocated memory.
146 const char *pfx_in
, /* Basename prefix for temp file. */
147 const char *suffix
, /* Suffix, including any '.', for temp file. */
148 int *fd_ret
, /* (return,optional) File descriptor to temp file. */
149 FILE **fp_ret
/* (return,optional) FILE pointer to temp file. */
155 mode_t oldmode
= umask(077);
157 if (suffix
== NULL
) {
158 if ((tmpfil
= m_mktemp2(NULL
, pfx_in
, fd_ret
, fp_ret
))) {
159 return mh_xstrdup(tmpfil
);
165 if (pfx_in
== NULL
) {
166 tmpfil
= concat(get_temp_dir(), "/nmhXXXXXX", suffix
, NULL
);
168 tmpfil
= concat(get_temp_dir(), "/", pfx_in
, "XXXXXX", suffix
, NULL
);
171 fd
= mkstemps(tmpfil
, (int) strlen(suffix
));
172 #else /* ! HAVE_MKSTEMPS */
173 /* NetBSD, Solaris 10 */
175 if (pfx_in
== NULL
) {
176 tmpfil
= concat(get_temp_dir(), "/nmhXXXXXX", NULL
);
178 tmpfil
= concat(get_temp_dir(), "/", pfx_in
, "XXXXXX", NULL
);
181 fd
= mkstemp(tmpfil
);
183 char *oldfilename
= tmpfil
;
184 tmpfil
= concat(oldfilename
, suffix
, NULL
);
186 if (rename(oldfilename
, tmpfil
) != 0) {
187 (void) unlink(oldfilename
);
195 #endif /* ! HAVE_MKSTEMPS */
203 register_for_removal(tmpfil
);
205 if (fd_ret
!= NULL
) {
209 if (fp_ret
!= NULL
) {
210 FILE *fp
= fdopen(fd
, "w+");
213 (void) m_unlink(tmpfil
);
235 /* Ignore envvars if we are setuid */
236 if ((getuid()==geteuid()) && (getgid()==getegid())) {
238 tmpdir
= getenv("MHTMPDIR");
239 if (tmpdir
!= NULL
&& *tmpdir
!= '\0') return tmpdir
;
241 tmpdir
= getenv("TMPDIR");
242 if (tmpdir
!= NULL
&& *tmpdir
!= '\0') return tmpdir
;
244 return m_maildir("");
249 * Array of files (full pathnames) to remove on process exit.
250 * Instead of removing slots from the array, just set to NULL
251 * to indicate that the file should no longer be removed.
253 static svector_t exit_filelist
= NULL
;
256 * Register a file for removal at program termination.
259 register_for_removal(const char *pathname
) {
260 if (exit_filelist
== NULL
) exit_filelist
= svector_create(20);
261 (void) svector_push_back(exit_filelist
, add(pathname
, NULL
));
265 * Unregister all files that were registered to be removed at program
266 * termination. When called with remove_files of 0, this function is
267 * intended for use by one of the programs after a fork(3) without a
268 * subsequent call to one of the exec(3) functions or _exit(3). When
269 * called with non-0 remove_files argument, it is intended for use by
270 * an atexit() function.
272 * Right after a fork() and before calling exec() or _exit(), if the
273 * child catches one of the appropriate signals, it will remove
274 * all the registered temporary files. Some of those may be needed by
275 * the parent. Some nmh programs ignore or block some of the signals
276 * in the child right after fork(). For the other programs, rather
277 * than complicate things by preventing that, just take the chance
278 * that it might happen. It is harmless to attempt to unlink a
279 * temporary file twice, assuming another one wasn't created too
280 * quickly created with the same name.
283 unregister_for_removal(int remove_files
) {
287 for (i
= 0; i
< svector_size(exit_filelist
); ++i
) {
288 char *filename
= svector_at(exit_filelist
, i
);
291 if (remove_files
) (void) unlink(filename
);
296 svector_free(exit_filelist
);
297 exit_filelist
= NULL
;
302 * If the file was registered for removal, deregister it. In
303 * any case, unlink it.
306 m_unlink(const char *pathname
) {
308 char **slot
= svector_find(exit_filelist
, pathname
);
316 return unlink(pathname
);
317 /* errno should be set to ENOENT if file was not found */
321 * Remove all registered temporary files.
324 remove_registered_files_atexit(void) {
325 unregister_for_removal(1);
329 * Remove all registered temporary files. Suitable for use as a
330 * signal handler. If the signal is one of the usual process
331 * termination signals, calls exit(). Otherwise, disables itself
332 * by restoring the default action and then re-raises the signal,
333 * in case the use was expecting a core dump.
336 remove_registered_files(int sig
) {
337 struct sigaction act
;
340 * Ignore this signal for the duration. And we either exit() or
341 * disable this signal handler below, so don't restore this handler.
343 act
.sa_handler
= SIG_IGN
;
344 (void) sigemptyset(&act
.sa_mask
);
346 (void) sigaction(sig
, &act
, 0);
348 if (sig
== SIGHUP
|| sig
== SIGINT
|| sig
== SIGQUIT
|| sig
== SIGTERM
) {
350 * We don't need to call remove_registered_files_atexit() if
351 * it was registered with atexit(), but let's not assume that.
352 * It's harmless to call it twice.
354 remove_registered_files_atexit();
358 remove_registered_files_atexit();
360 act
.sa_handler
= SIG_DFL
;
361 (void) sigemptyset(&act
.sa_mask
);
363 (void) sigaction(sig
, &act
, 0);