blob: aedc2e6a3e3d55ee4d44187ab6ccc3ebc21bbccc [file] [log] [blame]
Vladimir Chtchetkinea8fc4912010-11-30 09:32:55 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/*
18 * Contains helper routines dealing with syncronous access to a non-blocking
19 * sokets.
20 */
21
22#include "qemu-common.h"
23#include "errno.h"
24#include "iolooper.h"
25#include "sockets.h"
26#include "android/utils/debug.h"
27#include "android/sync-utils.h"
28
29#define D(...) do { if (VERBOSE_CHECK(init)) dprint(__VA_ARGS__); } while (0)
30
31struct SyncSocket {
32 // Helper for performing synchronous I/O on the socket.
33 IoLooper* iolooper;
34
35 /* Opened socket handle. */
36 int fd;
37};
38
39SyncSocket*
40syncsocket_connect(int fd, SockAddress* sockaddr, int timeout)
41{
42 IoLooper* looper = NULL;
43 int connect_status;
44 SyncSocket* sync_socket;
45
46 socket_set_nonblock(fd);
47
48 for(;;) {
49 connect_status = socket_connect(fd, sockaddr);
50 if (connect_status >= 0) {
51 // Connected. Create IoLooper for the helper.
52 looper = iolooper_new();
53 break;
54 }
55
56 if (errno == EINPROGRESS || errno == EAGAIN || errno == EWOULDBLOCK) {
57 // Connection is in progress. Wait till it's finished.
58 looper = iolooper_new();
59 iolooper_add_write(looper, fd);
60 connect_status = iolooper_wait(looper, timeout);
61 if (connect_status > 0) {
62 iolooper_del_write(looper, fd);
63 } else {
64 iolooper_free(looper);
65 return NULL;
66 }
67 } else if (errno != EINTR) {
68 return NULL;
69 }
70 }
71
72 // We're now connected. Lets initialize SyncSocket instance
73 // for this connection.
74 sync_socket = malloc(sizeof(SyncSocket));
75 if (sync_socket == NULL) {
76 derror("PANIC: not enough memory\n");
77 exit(1);
78 }
79
80 sync_socket->iolooper = looper;
81 sync_socket->fd = fd;
82
83 return sync_socket;
84}
85
86void
87syncsocket_close(SyncSocket* ssocket)
88{
89 if (ssocket != NULL && ssocket->fd >= 0) {
90 if (ssocket->iolooper != NULL) {
91 iolooper_reset(ssocket->iolooper);
92 }
93 socket_close(ssocket->fd);
94 ssocket->fd = -1;
95 }
96}
97
98void
99syncsocket_free(SyncSocket* ssocket)
100{
101 if (ssocket != NULL) {
102 syncsocket_close(ssocket);
103 if (ssocket->iolooper != NULL) {
104 iolooper_free(ssocket->iolooper);
105 }
106 free(ssocket);
107 }
108}
109
110int
111syncsocket_start_read(SyncSocket* ssocket)
112{
113 if (ssocket == NULL || ssocket->fd < 0 || ssocket->iolooper == NULL) {
114 errno = EINVAL;
115 return -1;
116 }
117 iolooper_add_read(ssocket->iolooper, ssocket->fd);
118 return 0;
119}
120
121int
122syncsocket_stop_read(SyncSocket* ssocket)
123{
124 if (ssocket == NULL || ssocket->fd < 0 || ssocket->iolooper == NULL) {
125 errno = EINVAL;
126 return -1;
127 }
128 iolooper_del_read(ssocket->iolooper, ssocket->fd);
129 return 0;
130}
131
132int
133syncsocket_read_absolute(SyncSocket* ssocket,
134 void* buf,
135 int size,
136 int64_t deadline)
137{
138 int ret;
139
140 if (ssocket == NULL || ssocket->fd < 0 || ssocket->iolooper == NULL) {
141 errno = EINVAL;
142 return -1;
143 }
144
145 ret = iolooper_wait_absolute(ssocket->iolooper, deadline);
146 if (ret > 0) {
147 if (!iolooper_is_read(ssocket->iolooper, ssocket->fd)) {
148 D("%s: Internal error, iolooper_is_read() not set!", __FUNCTION__);
149 return -1;
150 }
151 do {
152 ret = read(ssocket->fd, buf, size);
153 } while( ret < 0 && errno == EINTR);
154 }
155 return ret;
156}
157
158int
159syncsocket_read(SyncSocket* ssocket, void* buf, int size, int timeout)
160{
161 return syncsocket_read_absolute(ssocket, buf, size, iolooper_now() + timeout);
162}