File: m_mktemp.c

package info (click to toggle)
nmh 1.6-2
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 6,204 kB
  • ctags: 3,851
  • sloc: ansic: 48,922; sh: 16,422; makefile: 559; perl: 509; lex: 402; awk: 74
file content (367 lines) | stat: -rw-r--r-- 10,521 bytes parent folder | download | duplicates (2)
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/*
 * m_mktemp.c -- Construct a temporary file.
 *
 * This code is Copyright (c) 2010, by the authors of nmh.  See the
 * COPYRIGHT file in the root directory of the nmh distribution for
 * complete copyright information.
 */

#include <h/mh.h>
#include <h/utils.h>
#include <h/signals.h>

static void register_for_removal(const char *);


/*  Create a temporary file.  If pfx_in is null, the temporary file
 *  will be created in the temporary directory (more on that later).
 *  If pfx_in is not null, then the temporary file location will be
 *  defined by the value pfx_in.
 *
 *  The file created will be at the pathname specified appended with
 *  6 random (we hope :) characters.
 *
 *  The return value will be the pathname to the file created.
 *
 *  CAUTION: The return pointer references static data.  If
 *  you need to modify, or save, the return string, make a copy of it
 *  first.
 *
 *  When pfx_in is null, the temporary directory is determined as
 *  follows, in order:
 *
 *    MHTMPDIR envvar
 *    TMPDIR envvar
 *    User's mail directory.
 *
 *  NOTE: One will probably use m_mktemp2() instead of this function.
 *  For example, if you want to create a temp file in the defined
 *  temporary directory, but with a custom basename prefix, do
 *  something like the following:
 *
 *    char *tmp_pathname = m_mktemp2(NULL, "mypre", ...);
 */
char *
m_mktemp (
    const char *pfx_in,   /* Pathname prefix for temporary file. */
    int *fd_ret,          /* (return,optional) File descriptor to temp file. */
    FILE **fp_ret         /* (return,optional) FILE pointer to temp file. */
)
{
    static char tmpfil[BUFSIZ];
    int fd = -1;
    int keep_open = 0;
    mode_t oldmode = umask(077);

    if (pfx_in == NULL) {
        snprintf(tmpfil, sizeof(tmpfil), "%s/nmhXXXXXX", get_temp_dir());
    } else {
        snprintf(tmpfil, sizeof(tmpfil), "%sXXXXXX", pfx_in);
    }

    fd = mkstemp(tmpfil);

    if (fd < 0) {
        umask(oldmode);
        return NULL;
    }

    register_for_removal(tmpfil);

    if (fd_ret != NULL) {
        *fd_ret = fd;
        keep_open = 1;
    }
    if (fp_ret != NULL) {
        FILE *fp = fdopen(fd, "w+");
        if (fp == NULL) {
            int olderr = errno;
            (void) m_unlink(tmpfil);
            close(fd);
            errno = olderr;
            umask(oldmode);
            return NULL;
        }
        *fp_ret = fp;
        keep_open = 1;
    }
    if (!keep_open) {
        close(fd);
    }
    umask(oldmode);
    return tmpfil;
}

/* This version allows one to specify the directory the temp file should
 * by created based on a given pathname.  Although m_mktemp() technically
 * supports this, this version is when the directory is defined by
 * a separate variable from the prefix, eliminating the caller from having
 * to do string manipulation to generate the desired pathname prefix.
 *
 * The pfx_in parameter specifies a basename prefix for the file.  If dir_in
 * is NULL, then the defined temporary directory (see comments to m_mktemp()
 * above) is used to create the temp file.
 */
char *
m_mktemp2 (
    const char *dir_in,   /* Directory to place temp file. */
    const char *pfx_in,   /* Basename prefix for temp file. */
    int *fd_ret,          /* (return,optional) File descriptor to temp file. */
    FILE **fp_ret         /* (return,optional) FILE pointer to temp file. */
)
{
    static char buffer[BUFSIZ];
    char *cp;
    int n;

    if (dir_in == NULL) {
        if (pfx_in == NULL) {
            return m_mktemp(NULL, fd_ret, fp_ret);
        }
        snprintf(buffer, sizeof(buffer), "%s/%s", get_temp_dir(), pfx_in);
        return m_mktemp(buffer, fd_ret, fp_ret);
    }

    if ((cp = r1bindex ((char *)dir_in, '/')) == dir_in) {
        /* No directory component */
        return m_mktemp(pfx_in, fd_ret, fp_ret);
    }
    n = (int)(cp-dir_in); /* Length of dir component */
    snprintf(buffer, sizeof(buffer), "%.*s%s", n, dir_in, pfx_in);
    return m_mktemp(buffer, fd_ret, fp_ret);
}


/*
 * This version supports a suffix for the temp filename.
 * It has two other differences from m_mktemp() and m_mktemp2():
 * 1) It does not support specification of a directory for the temp
 *    file.  Instead it always uses get_temp_dir().
 * 2) It returns a dynamically allocated string.  The caller is
 *    responsible for freeing the allocated memory.
 */
char *
m_mktemps(
    const char *pfx_in,   /* Basename prefix for temp file. */
    const char *suffix,   /* Suffix, including any '.', for temp file. */
    int *fd_ret,          /* (return,optional) File descriptor to temp file. */
    FILE **fp_ret         /* (return,optional) FILE pointer to temp file. */
)
{
    char *tmpfil;
    int fd = -1;
    int keep_open = 0;
    mode_t oldmode = umask(077);

    if (suffix == NULL) {
        if ((tmpfil = m_mktemp2(NULL, pfx_in, fd_ret, fp_ret))) {
            return add(tmpfil, NULL);
        } else {
            return NULL;
        }
    }

#if HAVE_MKSTEMPS
    if (pfx_in == NULL) {
        tmpfil = concat(get_temp_dir(), "/nmhXXXXXX", suffix, NULL);
    } else {
        tmpfil = concat(get_temp_dir(), "/", pfx_in, "XXXXXX", suffix, NULL);
    }

    fd = mkstemps(tmpfil, (int) strlen(suffix));
#else  /* ! HAVE_MKSTEMPS */
    /* NetBSD, Solaris 10 */

    if (pfx_in == NULL) {
        tmpfil = concat(get_temp_dir(), "/nmhXXXXXX", NULL);
    } else {
        tmpfil = concat(get_temp_dir(), "/", pfx_in, "XXXXXX", NULL);
    }

    fd = mkstemp(tmpfil);
    {
        char *oldfilename = tmpfil;
        tmpfil = concat(oldfilename, suffix, NULL);

        if (rename(oldfilename, tmpfil) != 0) {
            (void) unlink(oldfilename);
            free(oldfilename);
            free(tmpfil);
            return NULL;
        }

        free(oldfilename);
    }
#endif /* ! HAVE_MKSTEMPS */

    if (fd < 0) {
        umask(oldmode);
        free(tmpfil);
        return NULL;
    }

    register_for_removal(tmpfil);

    if (fd_ret != NULL) {
        *fd_ret = fd;
        keep_open = 1;
    }
    if (fp_ret != NULL) {
        FILE *fp = fdopen(fd, "w+");
        if (fp == NULL) {
            int olderr = errno;
            (void) m_unlink(tmpfil);
            close(fd);
            errno = olderr;
            umask(oldmode);
            free(tmpfil);
            return NULL;
        }
        *fp_ret = fp;
        keep_open = 1;
    }
    if (!keep_open) {
        close(fd);
    }
    umask(oldmode);

    return tmpfil;
}


char *
get_temp_dir()
{
    /* Ignore envvars if we are setuid */
    if ((getuid()==geteuid()) && (getgid()==getegid())) {
        char *tmpdir = NULL;
        tmpdir = getenv("MHTMPDIR");
        if (tmpdir != NULL && *tmpdir != '\0') return tmpdir;

        tmpdir = getenv("TMPDIR");
        if (tmpdir != NULL && *tmpdir != '\0') return tmpdir;
    }
    return m_maildir("");
}


/*
 * Array of files (full pathnames) to remove on process exit.
 * Instead of removing slots from the array, just set to NULL
 * to indicate that the file should no longer be removed.
 */
static svector_t exit_filelist = NULL;

/*
 * Register a file for removal at program termination.
 */
static void
register_for_removal(const char *pathname) {
    if (exit_filelist == NULL) exit_filelist = svector_create(20);
    (void) svector_push_back(exit_filelist, add(pathname, NULL));
}

/*
 * Unregister all files that were registered to be removed at program
 * termination.  When called with remove_files of 0, this function is
 * intended for use by one of the programs after a fork(3) without a
 * subsequent call to one of the exec(3) functions or _exit(3).  When
 * called with non-0 remove_files argument, it is intended for use by
 * an atexit() function.
 *
 * Right after a fork() and before calling exec() or _exit(), if the
 * child catches one of the appropriate signals, it will remove
 * all the registered temporary files.  Some of those may be needed by
 * the parent.  Some nmh programs ignore or block some of the signals
 * in the child right after fork().  For the other programs, rather
 * than complicate things by preventing that, just take the chance
 * that it might happen.  It is harmless to attempt to unlink a
 * temporary file twice, assuming another one wasn't created too
 * quickly created with the same name.
 */
void
unregister_for_removal(int remove_files) {
    if (exit_filelist) {
        size_t i;

        for (i = 0; i < svector_size(exit_filelist); ++i) {
            char *filename = svector_at(exit_filelist, i);

            if (filename) {
                if (remove_files) (void) unlink(filename);
                free(filename);
            }
        }

        svector_free(exit_filelist);
        exit_filelist = NULL;
    }
}

/*
 * If the file was registered for removal, deregister it.  In
 * any case, unlink it.
 */
int
m_unlink(const char *pathname) {
    if (exit_filelist) {
        char **slot = svector_find(exit_filelist, pathname);

        if (slot  &&  *slot) {
            free(*slot);
            *slot = NULL;
        }
    }

    return unlink(pathname);
    /* errno should be set to ENOENT if file was not found */
}

/*
 * Remove all registered temporary files.
 */
void
remove_registered_files_atexit() {
    unregister_for_removal(1);
}

/*
 * Remove all registered temporary files.  Suitable for use as a
 * signal handler.  If the signal is one of the usual process
 * termination signals, calls exit().  Otherwise, disables itself
 * by restoring the default action and then re-raises the signal,
 * in case the use was expecting a core dump.
 */
void
remove_registered_files(int sig) {
    struct sigaction act;

    /*
     * Ignore this signal for the duration.  And we either exit() or
     * disable this signal handler below, so don't restore this handler.
     */
    act.sa_handler = SIG_IGN;
    (void) sigemptyset(&act.sa_mask);
    act.sa_flags = 0;
    (void) sigaction(sig, &act, 0);

    if (sig == SIGHUP || sig == SIGINT || sig == SIGQUIT || sig == SIGTERM) {
        /*
         * We don't need to call remove_registered_files_atexit() if
         * it was registered with atexit(), but let's not assume that.
         * It's harmless to call it twice.
         */
        remove_registered_files_atexit();

        exit(1);
    } else {
        remove_registered_files_atexit();

        act.sa_handler = SIG_DFL;
        (void) sigemptyset(&act.sa_mask);
        act.sa_flags = 0;
        (void) sigaction(sig, &act, 0);

        (void) raise(sig);
    }
}