| 201 | } |
| 202 | |
| 203 | int |
| 204 | conn_req_poll(struct conn *conn) |
| 205 | { |
| 206 | struct sockaddr_in client_address; |
| 207 | socklen_t client_address_length; |
| 208 | struct epoll_event event; |
| 209 | int fd_client, rc; |
| 210 | |
| 211 | /* Check input arguments */ |
| 212 | if (conn == NULL) |
| 213 | return -1; |
| 214 | |
| 215 | /* Server socket */ |
| 216 | client_address_length = sizeof(client_address); |
| 217 | fd_client = accept4(conn->fd_server, (struct sockaddr *)&client_address, |
| 218 | &client_address_length, SOCK_NONBLOCK); |
| 219 | if (fd_client == -1) { |
| 220 | if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) |
| 221 | return 0; |
| 222 | |
| 223 | return -1; |
| 224 | } |
| 225 | |
| 226 | /* Client group */ |
| 227 | event.events = EPOLLIN | EPOLLRDHUP | EPOLLHUP; |
| 228 | event.data.fd = fd_client; |
| 229 | |
| 230 | rc = epoll_ctl(conn->fd_client_group, EPOLL_CTL_ADD, fd_client, &event); |
| 231 | if (rc == -1) { |
| 232 | close(fd_client); |
| 233 | goto exit; |
| 234 | } |
| 235 | |
| 236 | /* Client */ |
| 237 | rc = write(fd_client, conn->welcome, strlen(conn->welcome)); |
| 238 | if (rc == -1) { |
| 239 | close(fd_client); |
| 240 | goto exit; |
| 241 | } |
| 242 | |
| 243 | rc = write(fd_client, conn->prompt, strlen(conn->prompt)); |
| 244 | if (rc == -1) { |
| 245 | close(fd_client); |
| 246 | goto exit; |
| 247 | } |
| 248 | |
| 249 | rc = 0; |
| 250 | |
| 251 | exit: |
| 252 | return rc; |
| 253 | } |
| 254 | |
| 255 | int |
| 256 | conn_msg_poll(struct conn *conn) |