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