Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
remzi-arpacidusseau
GitHub Repository: remzi-arpacidusseau/ostep-projects
Path: blob/master/concurrency-webserver/src/io_helper.h
909 views
1
#ifndef __IO_HELPER__
2
#define __IO_HELPER__
3
4
#include <arpa/inet.h>
5
#include <assert.h>
6
#include <ctype.h>
7
#include <errno.h>
8
#include <fcntl.h>
9
#include <netdb.h>
10
#include <netinet/in.h>
11
#include <setjmp.h>
12
#include <signal.h>
13
#include <stdio.h>
14
#include <stdlib.h>
15
#include <string.h>
16
#include <strings.h>
17
#include <sys/mman.h>
18
#include <sys/time.h>
19
#include <sys/socket.h>
20
#include <sys/stat.h>
21
#include <sys/types.h>
22
#include <sys/wait.h>
23
#include <unistd.h>
24
25
typedef struct sockaddr sockaddr_t;
26
27
// useful here: gcc statement expressions
28
// http://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html
29
// macro ({ ...; x; }) returns value 'x' for caller
30
// e.g., macro 'fork_or_die()' below returns 'pid' value
31
#define fork_or_die() \
32
({ pid_t pid = fork(); assert(pid >= 0); pid; })
33
#define execve_or_die(filename, argv, envp) \
34
assert(execve(filename, argv, envp) == 0);
35
#define wait_or_die(status) \
36
({ pid_t pid = wait(status); assert(pid >= 0); pid; })
37
#define gethostname_or_die(name, len) \
38
({ int rc = gethostname(name, len); assert(rc == 0); rc; })
39
#define setenv_or_die(name, value, overwrite) \
40
({ int rc = setenv(name, value, overwrite); assert(rc == 0); rc; })
41
#define chdir_or_die(path) \
42
assert(chdir(path) == 0);
43
#define open_or_die(pathname, flags, mode) \
44
({ int rc = open(pathname, flags, mode); assert(rc >= 0); rc; })
45
#define read_or_die(fd, buf, count) \
46
({ ssize_t rc = read(fd, buf, count); assert(rc >= 0); rc; })
47
#define write_or_die(fd, buf, count) \
48
({ ssize_t rc = write(fd, buf, count); assert(rc >= 0); rc; })
49
#define lseek_or_die(fd, offset, whence) \
50
({ off_t rc = lseek(fd, offset, whence); assert(rc >= 0); rc; })
51
#define close_or_die(fd) \
52
assert(close(fd) == 0);
53
#define select_or_die(n, readfds, writefds, exceptfds, timeout) \
54
({ int rc = select(n, readfds, writefds, exceptfds, timeout); assert(rc >= 0); rc; })
55
#define dup2_or_die(fd1, fd2) \
56
({ int rc = dup2(fd1, fd2); assert(rc >= 0); rc; })
57
#define stat_or_die(filename, buf) \
58
assert(stat(filename, buf) >= 0);
59
#define fstat_or_die(fd, buf) \
60
{ assert(fstat(fd, buf) >= 0); }
61
#define mmap_or_die(addr, len, prot, flags, fd, offset) \
62
({ void *ptr = mmap(addr, len, prot, flags, fd, offset); assert(ptr != (void *) -1); ptr; })
63
#define munmap_or_die(start, length) \
64
assert(munmap(start, length) >= 0);
65
#define socket_or_die(domain, type, protocol) \
66
({ int rc = socket(domain, type, protocol); assert(rc >= 0); rc; })
67
#define setsockopt_or_die(s, level, optname, optval, optlen) \
68
{ assert(setsockopt(s, level, optname, optval, optlen) >= 0); }
69
#define bind_or_die(sockfd, my_addr, addrlen) \
70
{ assert(bind(sockfd, my_addr, addrlen) >= 0); }
71
#define listen_or_die(s, backlog) \
72
{ assert(listen(s, backlog) >= 0); }
73
#define accept_or_die(s, addr, addrlen) \
74
({ int rc = accept(s, addr, addrlen); assert(rc >= 0); rc; })
75
#define connect_or_die(sockfd, serv_addr, addrlen) \
76
{ assert(connect(sockfd, serv_addr, addrlen) >= 0); }
77
#define gethostbyname_or_die(name) \
78
({ struct hostent *p = gethostbyname(name); assert(p != NULL); p; })
79
#define gethostbyaddr_or_die(addr, len, type) \
80
({ struct hostent *p = gethostbyaddr(addr, len, type); assert(p != NULL); p; })
81
82
// client/server helper functions
83
ssize_t readline(int fd, void *buf, size_t maxlen);
84
int open_client_fd(char *hostname, int portno);
85
int open_listen_fd(int portno);
86
87
// wrappers for above
88
#define readline_or_die(fd, buf, maxlen) \
89
({ ssize_t rc = readline(fd, buf, maxlen); assert(rc >= 0); rc; })
90
#define open_client_fd_or_die(hostname, port) \
91
({ int rc = open_client_fd(hostname, port); assert(rc >= 0); rc; })
92
#define open_listen_fd_or_die(port) \
93
({ int rc = open_listen_fd(port); assert(rc >= 0); rc; })
94
95
#endif // __IO_HELPER__
96
97