| 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 |
#include "server_utils.h" |
|---|
| 29 |
#include "wlog.h" |
|---|
| 30 |
|
|---|
| 31 |
void w_listen(int s,int b) |
|---|
| 32 |
{ |
|---|
| 33 |
if (listen(s,b)) |
|---|
| 34 |
fatal("Fatal listen(): %s", strerror(errno)); |
|---|
| 35 |
return; |
|---|
| 36 |
} |
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
int w_poll(struct pollfd *p, int n, int timeout) |
|---|
| 40 |
{ |
|---|
| 41 |
int res; |
|---|
| 42 |
|
|---|
| 43 |
if ((res=poll(p, n, timeout))==-1) |
|---|
| 44 |
fatal("Fatal poll: %s.", strerror(errno)); |
|---|
| 45 |
return res; |
|---|
| 46 |
} |
|---|
| 47 |
|
|---|
| 48 |
void w_pthread_cond_signal(pthread_cond_t *cond) |
|---|
| 49 |
{ |
|---|
| 50 |
if (pthread_cond_signal(cond)) |
|---|
| 51 |
fatal("Fatal pthread_cond_signal()"); |
|---|
| 52 |
return; |
|---|
| 53 |
} |
|---|
| 54 |
|
|---|
| 55 |
void w_pthread_mutex_lock(pthread_mutex_t *m) |
|---|
| 56 |
{ |
|---|
| 57 |
int res; |
|---|
| 58 |
char buf[10]; |
|---|
| 59 |
|
|---|
| 60 |
res=pthread_mutex_lock(m); |
|---|
| 61 |
if (res) { |
|---|
| 62 |
switch(res) { |
|---|
| 63 |
|
|---|
| 64 |
|
|---|
| 65 |
|
|---|
| 66 |
|
|---|
| 67 |
|
|---|
| 68 |
|
|---|
| 69 |
|
|---|
| 70 |
case EBUSY: |
|---|
| 71 |
sprintf(buf,"EBUSY"); |
|---|
| 72 |
break; |
|---|
| 73 |
case EINVAL: |
|---|
| 74 |
sprintf(buf,"EINVAL"); |
|---|
| 75 |
break; |
|---|
| 76 |
case EAGAIN: |
|---|
| 77 |
sprintf(buf,"EAGAIN"); |
|---|
| 78 |
break; |
|---|
| 79 |
case EDEADLK: |
|---|
| 80 |
sprintf(buf,"EDEADLK"); |
|---|
| 81 |
break; |
|---|
| 82 |
case EPERM: |
|---|
| 83 |
sprintf(buf,"EPERM"); |
|---|
| 84 |
break; |
|---|
| 85 |
default: |
|---|
| 86 |
sprintf(buf,"EUNKNOW"); |
|---|
| 87 |
break; |
|---|
| 88 |
} |
|---|
| 89 |
fatal("Fatal pthread_mutex_lock(): %s.", buf); |
|---|
| 90 |
} |
|---|
| 91 |
return; |
|---|
| 92 |
} |
|---|
| 93 |
|
|---|
| 94 |
void w_pthread_mutex_unlock(pthread_mutex_t *m) |
|---|
| 95 |
{ |
|---|
| 96 |
int res; |
|---|
| 97 |
char buf[10]; |
|---|
| 98 |
|
|---|
| 99 |
if ((res=pthread_mutex_unlock(m))) { |
|---|
| 100 |
switch(res) { |
|---|
| 101 |
case EINVAL: |
|---|
| 102 |
sprintf(buf,"EINVAL"); |
|---|
| 103 |
break; |
|---|
| 104 |
case EBUSY: |
|---|
| 105 |
sprintf(buf,"EBUSY"); |
|---|
| 106 |
break; |
|---|
| 107 |
case EAGAIN: |
|---|
| 108 |
sprintf(buf,"EAGAIN"); |
|---|
| 109 |
break; |
|---|
| 110 |
case EDEADLK: |
|---|
| 111 |
sprintf(buf,"EDEADLK"); |
|---|
| 112 |
break; |
|---|
| 113 |
case EPERM: |
|---|
| 114 |
sprintf(buf,"EPERM"); |
|---|
| 115 |
break; |
|---|
| 116 |
default: |
|---|
| 117 |
sprintf(buf,"EUNKNOW"); |
|---|
| 118 |
break; |
|---|
| 119 |
} |
|---|
| 120 |
fatal("Fatal pthread_mutex_lock(): %s.", buf); |
|---|
| 121 |
} |
|---|
| 122 |
return; |
|---|
| 123 |
} |
|---|
| 124 |
|
|---|
| 125 |
void w_pthread_mutex_lock_unlock(pthread_mutex_t *m) |
|---|
| 126 |
{ |
|---|
| 127 |
w_pthread_mutex_lock(m); |
|---|
| 128 |
w_pthread_mutex_unlock(m); |
|---|
| 129 |
return; |
|---|
| 130 |
} |
|---|
| 131 |
|
|---|
| 132 |
void w_pthread_create(pthread_t *t,pthread_attr_t *a, |
|---|
| 133 |
void *(*f)(void*), void *arg) |
|---|
| 134 |
{ |
|---|
| 135 |
int res; |
|---|
| 136 |
char buf[10]; |
|---|
| 137 |
|
|---|
| 138 |
if ((res=pthread_create(t,a,f,arg))) { |
|---|
| 139 |
switch(res) { |
|---|
| 140 |
case EINVAL: |
|---|
| 141 |
sprintf(buf,"EINVAL"); |
|---|
| 142 |
break; |
|---|
| 143 |
case EBUSY: |
|---|
| 144 |
sprintf(buf,"EBUSY"); |
|---|
| 145 |
break; |
|---|
| 146 |
case EAGAIN: |
|---|
| 147 |
sprintf(buf,"EAGAIN"); |
|---|
| 148 |
break; |
|---|
| 149 |
case EDEADLK: |
|---|
| 150 |
sprintf(buf,"EDEADLK"); |
|---|
| 151 |
break; |
|---|
| 152 |
case EPERM: |
|---|
| 153 |
sprintf(buf,"EPERM"); |
|---|
| 154 |
break; |
|---|
| 155 |
default: |
|---|
| 156 |
sprintf(buf,"EUNKNOW"); |
|---|
| 157 |
break; |
|---|
| 158 |
} |
|---|
| 159 |
fatal("Fatal pthread_mutex_lock(): %s.", buf); |
|---|
| 160 |
} |
|---|
| 161 |
return; |
|---|
| 162 |
} |
|---|
| 163 |
|
|---|
| 164 |
void w_pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *m) |
|---|
| 165 |
{ |
|---|
| 166 |
int res; |
|---|
| 167 |
char buf[10]; |
|---|
| 168 |
|
|---|
| 169 |
if ((res=pthread_cond_wait(cond,m))) { |
|---|
| 170 |
switch(res) { |
|---|
| 171 |
case EINVAL: |
|---|
| 172 |
sprintf(buf,"EINVAL"); |
|---|
| 173 |
break; |
|---|
| 174 |
case EBUSY: |
|---|
| 175 |
sprintf(buf,"EBUSY"); |
|---|
| 176 |
break; |
|---|
| 177 |
case EAGAIN: |
|---|
| 178 |
sprintf(buf,"EAGAIN"); |
|---|
| 179 |
break; |
|---|
| 180 |
case ETIMEDOUT: |
|---|
| 181 |
sprintf(buf,"ETIMEDOUT"); |
|---|
| 182 |
break; |
|---|
| 183 |
case EPERM: |
|---|
| 184 |
sprintf(buf,"EPERM"); |
|---|
| 185 |
break; |
|---|
| 186 |
default: |
|---|
| 187 |
sprintf(buf,"EUNKNOW"); |
|---|
| 188 |
break; |
|---|
| 189 |
} |
|---|
| 190 |
fatal("Fatal pthread_mutex_lock(): %s.", buf); |
|---|
| 191 |
} |
|---|
| 192 |
return; |
|---|
| 193 |
} |
|---|
| 194 |
|
|---|
| 195 |
void w_pthread_cond_wait_unlock(pthread_cond_t *cond, pthread_mutex_t *m) |
|---|
| 196 |
{ |
|---|
| 197 |
w_pthread_cond_wait(cond,m); |
|---|
| 198 |
w_pthread_mutex_unlock(m); |
|---|
| 199 |
return; |
|---|
| 200 |
} |
|---|
| 201 |
|
|---|
| 202 |
void w_pthread_attr_init(pthread_attr_t *a) |
|---|
| 203 |
{ |
|---|
| 204 |
if (pthread_attr_init(a)) |
|---|
| 205 |
fatal("Fatal pthread_attr_init()."); |
|---|
| 206 |
return; |
|---|
| 207 |
} |
|---|
| 208 |
|
|---|
| 209 |
void w_pthread_attr_setdetachstate(pthread_attr_t *a,int s) |
|---|
| 210 |
{ |
|---|
| 211 |
if (pthread_attr_setdetachstate(a,s)) |
|---|
| 212 |
fatal("Fatal pthread_attr_setdetachstate()."); |
|---|
| 213 |
return; |
|---|
| 214 |
} |
|---|
| 215 |
|
|---|
| 216 |
void w_pthread_attr_destroy(pthread_attr_t *a) |
|---|
| 217 |
{ |
|---|
| 218 |
if (pthread_attr_destroy(a)) |
|---|
| 219 |
fatal("Fatal pthread_attr_destroy()."); |
|---|
| 220 |
return; |
|---|
| 221 |
} |
|---|
| 222 |
|
|---|
| 223 |
void w_pthread_mutex_init(pthread_mutex_t *t,pthread_mutexattr_t *a) |
|---|
| 224 |
{ |
|---|
| 225 |
if (pthread_mutex_init(t,a)) |
|---|
| 226 |
fatal("Fatal pthread_mutex_init()."); |
|---|
| 227 |
return; |
|---|
| 228 |
} |
|---|
| 229 |
|
|---|
| 230 |
void w_pthread_mutex_destroy(pthread_mutex_t *t) |
|---|
| 231 |
{ |
|---|
| 232 |
if (pthread_mutex_destroy(t)) |
|---|
| 233 |
fatal("Fatal pthread_mutex_destroy()."); |
|---|
| 234 |
return; |
|---|
| 235 |
} |
|---|
| 236 |
|
|---|
| 237 |
void w_pthread_cond_init(pthread_cond_t *c,pthread_condattr_t *a) |
|---|
| 238 |
{ |
|---|
| 239 |
if (pthread_cond_init(c,a)) |
|---|
| 240 |
fatal("Fatal pthread_cond_init()."); |
|---|
| 241 |
return; |
|---|
| 242 |
} |
|---|
| 243 |
|
|---|
| 244 |
int any_sockaddr(struct sockaddr *sa,int family) |
|---|
| 245 |
{ |
|---|
| 246 |
struct sockaddr_in *si; |
|---|
| 247 |
struct sockaddr_in6 *si6; |
|---|
| 248 |
|
|---|
| 249 |
if (family==AF_INET) { |
|---|
| 250 |
si=(struct sockaddr_in*)sa; |
|---|
| 251 |
si->sin_addr.s_addr=htonl(INADDR_ANY); |
|---|
| 252 |
return sizeof(struct sockaddr_in); |
|---|
| 253 |
} else if (family==AF_INET6) { |
|---|
| 254 |
si6=(struct sockaddr_in6*)sa; |
|---|
| 255 |
si6->sin6_addr = in6addr_any; |
|---|
| 256 |
return sizeof(struct sockaddr_in6); |
|---|
| 257 |
} else |
|---|
| 258 |
fatal("Fatal any_sockaddr(): family not supported."); |
|---|
| 259 |
return -1; |
|---|
| 260 |
} |
|---|
| 261 |
|
|---|
| 262 |
void w_setsockopt(int s,int lvl,int opt,void *optval,socklen_t optlen) |
|---|
| 263 |
{ |
|---|
| 264 |
if (setsockopt(s,lvl,opt,optval,optlen)) |
|---|
| 265 |
fatal("Fatal setsockopt(): %s.", strerror(errno)); |
|---|
| 266 |
return; |
|---|
| 267 |
} |
|---|
| 268 |
|
|---|
| 269 |
void w_setsockopt_reuseaddr(int s) |
|---|
| 270 |
{ |
|---|
| 271 |
int status=1; |
|---|
| 272 |
w_setsockopt(s,SOL_SOCKET,SO_REUSEADDR,&status,sizeof(int)); |
|---|
| 273 |
return; |
|---|
| 274 |
} |
|---|
| 275 |
|
|---|
| 276 |
int w_accept(int skt,struct sockaddr *sa, socklen_t *salen) |
|---|
| 277 |
{ |
|---|
| 278 |
|
|---|
| 279 |
return accept(skt,sa,salen); |
|---|
| 280 |
} |
|---|
| 281 |
|
|---|
| 282 |
const char* w_inet_ntop(struct sockaddr *s, socklen_t slen, char *buf, int buflen) |
|---|
| 283 |
{ |
|---|
| 284 |
struct sockaddr_in *si; |
|---|
| 285 |
struct sockaddr_in6 *si6; |
|---|
| 286 |
const char *res; |
|---|
| 287 |
|
|---|
| 288 |
switch (slen) { |
|---|
| 289 |
case sizeof(struct sockaddr_in): |
|---|
| 290 |
si=(struct sockaddr_in*)s; |
|---|
| 291 |
res=inet_ntop(AF_INET,(void*)(&(si->sin_addr)),buf,buflen); |
|---|
| 292 |
break; |
|---|
| 293 |
case sizeof(struct sockaddr_in6): |
|---|
| 294 |
si6=(struct sockaddr_in6*)s; |
|---|
| 295 |
res=inet_ntop(AF_INET6,(void*)(&(si6->sin6_addr)),buf,buflen); |
|---|
| 296 |
break; |
|---|
| 297 |
default: |
|---|
| 298 |
strncpy(buf,"Unknow family.",buflen); |
|---|
| 299 |
res=0; |
|---|
| 300 |
} |
|---|
| 301 |
return res; |
|---|
| 302 |
} |
|---|
| 303 |
|
|---|
| 304 |
int w_socket(int family,int type, int proto,int die) |
|---|
| 305 |
{ |
|---|
| 306 |
int sk; |
|---|
| 307 |
sk=socket(family,type,proto); |
|---|
| 308 |
if (sk==-1) { |
|---|
| 309 |
if (die) |
|---|
| 310 |
fatal("Fatal socket(): %s.", strerror(errno)); |
|---|
| 311 |
return -1; |
|---|
| 312 |
} |
|---|
| 313 |
return sk; |
|---|
| 314 |
} |
|---|
| 315 |
|
|---|