-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsyscall_err.h
240 lines (218 loc) · 4.57 KB
/
syscall_err.h
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
#ifndef SYSCALL_ERR_H
#define SYSCALL_ERR_H
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <signal.h>
#include <stdio.h>
#include <pthread.h>
int fcntl_err(int fd, int cmd, int arg)
{
int rtr_val = fcntl(fd, cmd, arg);
if (rtr_val == -1)
{
perror("fcntl");
exit(EXIT_FAILURE);
}
return rtr_val;
}
int select_err(int nfds, fd_set* readfds, fd_set* writefds, fd_set* exceptfds,
struct timeval* timeout)
{
int rtr_val = select(nfds, readfds, writefds, exceptfds, timeout);
if (rtr_val == -1)
{
perror("select");
exit(EXIT_FAILURE);
}
return rtr_val;
}
int close_err(int fd)
{
int rtr_val = close(fd);
if (rtr_val == -1)
{
perror("close");
exit(EXIT_FAILURE);
}
return rtr_val;
}
int open_err(const char* pathname, int flags)
{
int rtr_val = open(pathname, flags);
if (rtr_val == -1)
{
perror("open");
exit(EXIT_FAILURE);
}
return rtr_val;
}
pid_t fork_err(void)
{
pid_t rtr_val = fork();
if (rtr_val == -1)
{
perror("fork");
exit(EXIT_FAILURE);
}
return rtr_val;
}
int pipe_err(int pipefd[2])
{
int rtr_val = pipe(pipefd);
if (rtr_val == -1)
{
perror("pipe");
exit(EXIT_FAILURE);
}
return rtr_val;
}
ssize_t read_err (int fd, void* buf, size_t count)
{
ssize_t rtr_val = read(fd, buf, count);
if(rtr_val == -1)
{
perror("read");
exit(EXIT_FAILURE);
}
return rtr_val;
}
ssize_t write_err (int fd, void* buf, size_t count)
{
ssize_t rtr_val = write(fd, buf, count);
if(rtr_val == -1)
{
perror("write");
exit(EXIT_FAILURE);
}
return rtr_val;
}
pid_t setsid_err(void)
{
pid_t rtr_val = setsid();
if(rtr_val == -1)
{
perror("setsid");
exit(EXIT_FAILURE);
}
return rtr_val;
}
int ioctl_err(int d, int request, void* arg)
{
int rtr_val = ioctl(d, request, arg);
if (rtr_val == -1)
{
perror("ioctl");
exit(EXIT_FAILURE);
}
return rtr_val;
}
int sigaction_err(int signum, const struct sigaction* act,
struct sigaction* oldact)
{
int rtr_val = sigaction(signum, act, oldact);
if (rtr_val == -1)
{
perror("sigaction");
exit(EXIT_FAILURE);
}
return rtr_val;
}
unsigned long int strtoul_err(const char* nptr, char** endptr, int base)
{
char* endptr_tmp = NULL;
unsigned long int rtr_val = strtoul(nptr, &endptr_tmp, base);
if (*nptr != '\0' && *endptr_tmp == '\0')
{
return rtr_val;
}
else
{
perror("strtoul");
exit(EXIT_FAILURE);
}
}
int pthread_create_err(pthread_t* thread, const pthread_attr_t* attr,
void*(*start_routine)(void*), void* arg)
{
int rtr_val = pthread_create(thread, attr, start_routine, arg);
if (rtr_val != 0)
{
perror("pthread_create");
exit(EXIT_FAILURE);
}
return rtr_val;
}
int pthread_join_err(pthread_t thread, void** retval)
{
int rtr_val = pthread_join(thread, retval);
if (rtr_val != 0)
{
perror("pthread_join");
exit(EXIT_FAILURE);
}
return rtr_val;
}
int pthread_mutex_lock_err(pthread_mutex_t* mutex)
{
int rtr_val = pthread_mutex_lock(mutex);
if (rtr_val != 0)
{
perror("pthread_mutex_lock");
exit(EXIT_FAILURE);
}
return rtr_val;
}
int pthread_mutex_unlock_err(pthread_mutex_t* mutex)
{
int rtr_val = pthread_mutex_unlock(mutex);
if (rtr_val != 0)
{
perror("pthread_mutex_unlock");
exit(EXIT_FAILURE);
}
return rtr_val;
}
int pthread_cond_broadcast_err(pthread_cond_t* cond)
{
int rtr_val = pthread_cond_broadcast(cond);
if (rtr_val != 0)
{
perror("pthread_cond_broadcast");
exit(EXIT_FAILURE);
}
return rtr_val;
}
int pthread_mutex_init_err(pthread_mutex_t* mutex, const pthread_mutexattr_t*
attr)
{
int rtr_val = pthread_mutex_init(mutex, attr);
if (rtr_val != 0)
{
perror("pthread_mutex_init");
exit(EXIT_FAILURE);
}
return rtr_val;
}
int pthread_cond_init_err(pthread_cond_t* cond, const pthread_condattr_t* attr)
{
int rtr_val = pthread_cond_init(cond, attr);
if (rtr_val != 0)
{
perror("pthread_cond_init");
exit(EXIT_FAILURE);
}
return rtr_val;
}
void* calloc_err (size_t nmemb, size_t size)
{
void* rtr_val = calloc(nmemb, size);
if (rtr_val == NULL)
{
perror("calloc");
exit(EXIT_FAILURE);
}
return rtr_val;
}
#endif //SYSCALL_ERR_H