abort_socket.h revision 92a7541bce0f8ed7649dcbadd83b27d71e4f391f
192a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pelly/* 292a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pelly * Copyright 2009, The Android Open Source Project 392a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pelly * 492a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pelly * Licensed under the Apache License, Version 2.0 (the "License"); 592a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pelly * you may not use this file except in compliance with the License. 692a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pelly * You may obtain a copy of the License at 792a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pelly * 892a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pelly * http://www.apache.org/licenses/LICENSE-2.0 992a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pelly * 1092a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pelly * Unless required by applicable law or agreed to in writing, software 1192a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pelly * distributed under the License is distributed on an "AS IS" BASIS, 1292a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pelly * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1392a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pelly * See the License for the specific language governing permissions and 1492a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pelly * limitations under the License. 1592a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pelly */ 1692a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pelly 1792a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pelly/* Helper to perform abortable blocking operations on a socket: 1892a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pelly * asocket_connect() 1992a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pelly * asocket_accept() 2092a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pelly * asocket_read() 2192a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pelly * asocket_write() 2292a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pelly * These calls are similar to the regular syscalls, but can be aborted with: 2392a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pelly * asocket_abort() 2492a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pelly * 2592a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pelly * Calling close() on a regular POSIX socket does not abort blocked syscalls on 2692a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pelly * that socket in other threads. 2792a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pelly * 2892a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pelly * After calling asocket_abort() the socket cannot be reused. 2992a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pelly * 3092a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pelly * Call asocket_destory() *after* all threads have finished with the socket to 3192a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pelly * finish closing the socket and free the asocket structure. 3292a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pelly * 3392a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pelly * The helper is implemented by setting the socket non-blocking to initiate 3492a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pelly * syscalls connect(), accept(), read(), write(), then using a blocking poll() 3592a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pelly * on both the primary socket and a local pipe. This makes the poll() abortable 3692a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pelly * by writing a byte to the local pipe in asocket_abort(). 3792a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pelly * 3892a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pelly * asocket_create() sets the fd to non-blocking mode. It must not be changed to 3992a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pelly * blocking mode. 4092a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pelly * 4192a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pelly * Using asocket will triple the number of file descriptors required per 4292a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pelly * socket, due to the local pipe. It may be possible to use a global pipe per 4392a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pelly * process rather than per socket, but we have not been able to come up with a 4492a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pelly * race-free implementation yet. 4592a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pelly * 4692a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pelly * All functions except asocket_init() and asocket_destroy() are thread safe. 4792a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pelly */ 4892a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pelly 4992a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pelly#include <stdlib.h> 5092a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pelly#include <sys/socket.h> 5192a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pelly 5292a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pelly#ifndef __CUTILS_ABORT_SOCKET_H__ 5392a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pelly#define __CUTILS_ABORT_SOCKET_H__ 5492a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pelly#ifdef __cplusplus 5592a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pellyextern "C" { 5692a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pelly#endif 5792a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pelly 5892a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pellystruct asocket { 5992a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pelly int fd; /* primary socket fd */ 6092a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pelly int abort_fd[2]; /* pipe used to abort */ 6192a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pelly}; 6292a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pelly 6392a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pelly/* Create an asocket from fd. 6492a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pelly * Sets the socket to non-blocking mode. 6592a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pelly * Returns NULL on error with errno set. 6692a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pelly */ 6792a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pellystruct asocket *asocket_init(int fd); 6892a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pelly 6992a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pelly/* Blocking socket I/O with timeout. 7092a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pelly * Calling asocket_abort() from another thread will cause each of these 7192a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pelly * functions to immediately return with value -1 and errno ECANCELED. 7292a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pelly * timeout is in ms, use -1 to indicate no timeout. On timeout -1 is returned 7392a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pelly * with errno ETIMEDOUT. 7492a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pelly * EINTR is handled in-call. 7592a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pelly * Other semantics are identical to the regular syscalls. 7692a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pelly */ 7792a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pellyint asocket_connect(struct asocket *s, const struct sockaddr *addr, 7892a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pelly socklen_t addrlen, int timeout); 7992a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pelly 8092a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pellyint asocket_accept(struct asocket *s, struct sockaddr *addr, 8192a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pelly socklen_t *addrlen, int timeout); 8292a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pelly 8392a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pellyint asocket_read(struct asocket *s, void *buf, size_t count, int timeout); 8492a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pelly 8592a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pellyint asocket_write(struct asocket *s, const void *buf, size_t count, 8692a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pelly int timeout); 8792a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pelly 8892a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pelly/* Abort above calls and shutdown socket. 8992a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pelly * Further I/O operations on this socket will immediately fail after this call. 9092a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pelly * asocket_destroy() should be used to release resources once all threads 9192a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pelly * have returned from blocking calls on the socket. 9292a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pelly */ 9392a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pellyvoid asocket_abort(struct asocket *s); 9492a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pelly 9592a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pelly/* Close socket and free asocket structure. 9692a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pelly * Must not be called until all calls on this structure have completed. 9792a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pelly */ 9892a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pellyvoid asocket_destroy(struct asocket *s); 9992a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pelly 10092a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pelly#ifdef __cplusplus 10192a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pelly} 10292a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pelly#endif 10392a7541bce0f8ed7649dcbadd83b27d71e4f391fNick Pelly#endif //__CUTILS_ABORT_SOCKET__H__ 104