cpp: Allow to pass additional cpp options
[netsniff-ng.git] / ioops.c
blobe2b9136ae150e3dd2e3183af1814879a2a70e8da
1 #define _GNU_SOURCE
2 #include <unistd.h>
3 #include <sys/types.h>
4 #include <sys/stat.h>
5 #include <sys/ioctl.h>
6 #include <sys/socket.h>
7 #include <fcntl.h>
8 #include <linux/if.h>
9 #include <linux/if_tun.h>
11 #include "die.h"
12 #include "dev.h"
13 #include "ioops.h"
14 #include "str.h"
15 #include "built_in.h"
17 int open_or_die(const char *file, int flags)
19 int ret = open(file, flags);
20 if (unlikely(ret < 0))
21 panic("Cannot open file %s! %s.\n", file, strerror(errno));
22 return ret;
25 int open_or_die_m(const char *file, int flags, mode_t mode)
27 int ret = open(file, flags, mode);
28 if (unlikely(ret < 0))
29 panic("Cannot open or create file %s! %s.", file, strerror(errno));
30 return ret;
33 int dup_or_die(int oldfd)
35 int newfd = dup(oldfd);
36 if (unlikely(newfd < 0))
37 panic("Cannot dup old file descriptor!\n");
38 return newfd;
41 void dup2_or_die(int oldfd, int newfd)
43 int ret = dup2(oldfd, newfd);
44 if (unlikely(ret < 0))
45 panic("Cannot dup2 old/new file descriptor!\n");
48 void create_or_die(const char *file, mode_t mode)
50 int fd = open_or_die_m(file, O_WRONLY | O_CREAT, mode);
51 close(fd);
54 void pipe_or_die(int pipefd[2], int flags)
56 int ret = pipe2(pipefd, flags);
57 if (unlikely(ret < 0))
58 panic("Cannot create pipe2 event fd! %s.\n", strerror(errno));
61 int tun_open_or_die(const char *name, int type)
63 int fd, ret;
64 short flags;
65 struct ifreq ifr;
67 if (unlikely(!name))
68 panic("No name provided for tundev!\n");
70 fd = open_or_die("/dev/net/tun", O_RDWR);
72 memset(&ifr, 0, sizeof(ifr));
73 ifr.ifr_flags = type;
74 strlcpy(ifr.ifr_name, name, IFNAMSIZ);
76 ret = ioctl(fd, TUNSETIFF, &ifr);
77 if (unlikely(ret < 0))
78 panic("ioctl screwed up! %s.\n", strerror(errno));
80 ret = fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
81 if (unlikely(ret < 0))
82 panic("fctnl screwed up! %s.\n", strerror(errno));
84 flags = device_get_flags(name);
85 flags |= IFF_UP | IFF_RUNNING;
86 device_set_flags(name, flags);
88 return fd;
91 ssize_t read_or_die(int fd, void *buf, size_t len)
93 ssize_t ret = read(fd, buf, len);
94 if (unlikely(ret < 0)) {
95 if (errno == EPIPE)
96 die();
97 panic("Cannot read from descriptor! %s.\n", strerror(errno));
100 return ret;
103 ssize_t write_or_die(int fd, const void *buf, size_t len)
105 ssize_t ret = write(fd, buf, len);
106 if (unlikely(ret < 0)) {
107 if (errno == EPIPE)
108 die();
109 panic("Cannot write to descriptor! %s.", strerror(errno));
112 return ret;
115 int read_blob_or_die(const char *file, void *blob, size_t count)
117 int fd, ret;
119 fd = open_or_die(file, O_RDONLY);
120 ret = read_or_die(fd, blob, count);
121 close(fd);
123 return ret;
126 int write_blob_or_die(const char *file, const void *blob, size_t count)
128 int fd, ret;
130 fd = open_or_die_m(file, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
131 ret = write_or_die(fd, blob, count);
132 fdatasync(fd);
133 close(fd);
135 return ret;