screen: Add helpers to easy use color by name
[netsniff-ng-new.git] / ioops.c
blobc1eeddcc069785c6c9e108896ea190120cb5d6d5
1 #ifndef _GNU_SOURCE
2 # define _GNU_SOURCE
3 #endif
4 #include <unistd.h>
5 #include <sys/types.h>
6 #include <sys/stat.h>
7 #include <sys/ioctl.h>
8 #include <sys/socket.h>
9 #include <fcntl.h>
10 #include <linux/if.h>
11 #include <linux/if_tun.h>
13 #include "die.h"
14 #include "dev.h"
15 #include "ioops.h"
16 #include "str.h"
17 #include "built_in.h"
19 int open_or_die(const char *file, int flags)
21 int ret = open(file, flags);
22 if (unlikely(ret < 0))
23 panic("Cannot open file %s! %s.\n", file, strerror(errno));
24 return ret;
27 int open_or_die_m(const char *file, int flags, mode_t mode)
29 int ret = open(file, flags, mode);
30 if (unlikely(ret < 0))
31 panic("Cannot open or create file %s! %s.", file, strerror(errno));
32 return ret;
35 int dup_or_die(int oldfd)
37 int newfd = dup(oldfd);
38 if (unlikely(newfd < 0))
39 panic("Cannot dup old file descriptor!\n");
40 return newfd;
43 void dup2_or_die(int oldfd, int newfd)
45 int ret = dup2(oldfd, newfd);
46 if (unlikely(ret < 0))
47 panic("Cannot dup2 old/new file descriptor!\n");
50 void create_or_die(const char *file, mode_t mode)
52 int fd = open_or_die_m(file, O_WRONLY | O_CREAT, mode);
53 close(fd);
56 void pipe_or_die(int pipefd[2], int flags)
58 int ret = pipe2(pipefd, flags);
59 if (unlikely(ret < 0))
60 panic("Cannot create pipe2 event fd! %s.\n", strerror(errno));
63 int tun_open_or_die(const char *name, int type)
65 int fd, ret;
66 short flags;
67 struct ifreq ifr;
69 if (unlikely(!name))
70 panic("No name provided for tundev!\n");
72 fd = open_or_die("/dev/net/tun", O_RDWR);
74 memset(&ifr, 0, sizeof(ifr));
75 ifr.ifr_flags = type;
76 strlcpy(ifr.ifr_name, name, IFNAMSIZ);
78 ret = ioctl(fd, TUNSETIFF, &ifr);
79 if (unlikely(ret < 0))
80 panic("ioctl screwed up! %s.\n", strerror(errno));
82 ret = fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
83 if (unlikely(ret < 0))
84 panic("fctnl screwed up! %s.\n", strerror(errno));
86 flags = device_get_flags(name);
87 flags |= IFF_UP | IFF_RUNNING;
88 device_set_flags(name, flags);
90 return fd;
93 ssize_t read_or_die(int fd, void *buf, size_t len)
95 ssize_t ret = read(fd, buf, len);
96 if (unlikely(ret < 0)) {
97 if (errno == EPIPE)
98 die();
99 panic("Cannot read from descriptor! %s.\n", strerror(errno));
102 return ret;
105 ssize_t write_or_die(int fd, const void *buf, size_t len)
107 ssize_t ret = write(fd, buf, len);
108 if (unlikely(ret < 0)) {
109 if (errno == EPIPE)
110 die();
111 panic("Cannot write to descriptor! %s.", strerror(errno));
114 return ret;
117 int read_blob_or_die(const char *file, void *blob, size_t count)
119 int fd, ret;
121 fd = open_or_die(file, O_RDONLY);
122 ret = read_or_die(fd, blob, count);
123 close(fd);
125 return ret;
128 int write_blob_or_die(const char *file, const void *blob, size_t count)
130 int fd, ret;
132 fd = open_or_die_m(file, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
133 ret = write_or_die(fd, blob, count);
134 fdatasync(fd);
135 close(fd);
137 return ret;