Skip to content

Commit

Permalink
ioops: Add mkostemp_or_die() wrapper for mkostemp(3)
Browse files Browse the repository at this point in the history
Add a panic()ing wrapper for mkostemp(3). This will be used to safely
create unique temporary files.

Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
  • Loading branch information
tklauser committed Apr 26, 2016
1 parent 77d5cae commit f3057cd
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
10 changes: 10 additions & 0 deletions ioops.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef _GNU_SOURCE
# define _GNU_SOURCE
#endif
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
Expand Down Expand Up @@ -53,6 +54,15 @@ void create_or_die(const char *file, mode_t mode)
close(fd);
}

int mkostemp_or_die(char *templ, int flags)
{
/* mode is 0600 (S_IRUSR | S_IWUSR) by default */
int fd = mkostemp(templ, flags);
if (unlikely(fd < 0))
panic("Cannot create unique temporary file! %s\n", strerror(errno));
return fd;
}

void pipe_or_die(int pipefd[2], int flags)
{
int ret = pipe2(pipefd, flags);
Expand Down
1 change: 1 addition & 0 deletions ioops.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ extern int open_or_die_m(const char *file, int flags, mode_t mode);
extern int dup_or_die(int oldfd);
extern void dup2_or_die(int oldfd, int newfd);
extern void create_or_die(const char *file, mode_t mode);
extern int mkostemp_or_die(char *templ, int flags);
extern int tun_open_or_die(const char *name, int type);
extern void pipe_or_die(int pipefd[2], int flags);
extern ssize_t read_or_die(int fd, void *buf, size_t count);
Expand Down

0 comments on commit f3057cd

Please sign in to comment.