| 150 | } |
| 151 | |
| 152 | int |
| 153 | conn_poll_for_conn(struct conn *conn) |
| 154 | { |
| 155 | struct sockaddr_in client_address; |
| 156 | struct epoll_event event; |
| 157 | socklen_t client_address_length; |
| 158 | int fd_client, status; |
| 159 | |
| 160 | /* Check input arguments */ |
| 161 | if (conn == NULL) |
| 162 | return -1; |
| 163 | |
| 164 | /* Server socket */ |
| 165 | client_address_length = sizeof(client_address); |
| 166 | fd_client = accept4(conn->fd_server, |
| 167 | (struct sockaddr *) &client_address, |
| 168 | &client_address_length, |
| 169 | SOCK_NONBLOCK); |
| 170 | if (fd_client == -1) { |
| 171 | if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) |
| 172 | return 0; |
| 173 | |
| 174 | return -1; |
| 175 | } |
| 176 | |
| 177 | /* Client group */ |
| 178 | event.events = EPOLLIN | EPOLLRDHUP | EPOLLHUP; |
| 179 | event.data.fd = fd_client; |
| 180 | |
| 181 | status = epoll_ctl(conn->fd_client_group, |
| 182 | EPOLL_CTL_ADD, |
| 183 | fd_client, |
| 184 | &event); |
| 185 | if (status == -1) { |
| 186 | close(fd_client); |
| 187 | return -1; |
| 188 | } |
| 189 | |
| 190 | /* Client */ |
| 191 | status = write(fd_client, |
| 192 | conn->welcome, |
| 193 | strlen(conn->welcome)); |
| 194 | if (status == -1) { |
| 195 | close(fd_client); |
| 196 | return -1; |
| 197 | } |
| 198 | |
| 199 | status = write(fd_client, |
| 200 | conn->prompt, |
| 201 | strlen(conn->prompt)); |
| 202 | if (status == -1) { |
| 203 | close(fd_client); |
| 204 | return -1; |
| 205 | } |
| 206 | |
| 207 | return 0; |
| 208 | } |
| 209 | |