qemu.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. /*
  2. * Copyright (C) 2008 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. /* this file contains various functions used by all libhardware modules
  17. * that support QEMU emulation
  18. */
  19. #include "qemu.h"
  20. #define LOG_TAG "hardware-qemu"
  21. #include <cutils/log.h>
  22. #include <cutils/properties.h>
  23. #include <cutils/sockets.h>
  24. #include <errno.h>
  25. #include <fcntl.h>
  26. #include <termios.h>
  27. #include <stdio.h>
  28. #include <stdarg.h>
  29. #define QEMU_DEBUG 0
  30. #if QEMU_DEBUG
  31. # define D(...) ALOGD(__VA_ARGS__)
  32. #else
  33. # define D(...) ((void)0)
  34. #endif
  35. #include "qemu_pipe.h"
  36. int
  37. qemu_check(void)
  38. {
  39. static int in_qemu = -1;
  40. if (__builtin_expect(in_qemu < 0,0)) {
  41. char propBuf[PROPERTY_VALUE_MAX];
  42. property_get("ro.kernel.qemu", propBuf, "");
  43. in_qemu = (propBuf[0] == '1');
  44. }
  45. return in_qemu;
  46. }
  47. static int
  48. qemu_fd_write( int fd, const char* cmd, int len )
  49. {
  50. int len2;
  51. do {
  52. len2 = write(fd, cmd, len);
  53. } while (len2 < 0 && errno == EINTR);
  54. return len2;
  55. }
  56. static int
  57. qemu_fd_read( int fd, char* buff, int len )
  58. {
  59. int len2;
  60. do {
  61. len2 = read(fd, buff, len);
  62. } while (len2 < 0 && errno == EINTR);
  63. return len2;
  64. }
  65. static int
  66. qemu_channel_open_qemud_pipe( QemuChannel* channel,
  67. const char* name )
  68. {
  69. int fd;
  70. char pipe_name[512];
  71. snprintf(pipe_name, sizeof(pipe_name), "qemud:%s", name);
  72. fd = qemu_pipe_open(pipe_name);
  73. if (fd < 0) {
  74. D("no qemud pipe: %s", strerror(errno));
  75. return -1;
  76. }
  77. channel->is_qemud = 1;
  78. channel->fd = fd;
  79. return 0;
  80. }
  81. static int
  82. qemu_channel_open_qemud( QemuChannel* channel,
  83. const char* name )
  84. {
  85. int fd, ret, namelen = strlen(name);
  86. char answer[2];
  87. fd = socket_local_client( "qemud",
  88. ANDROID_SOCKET_NAMESPACE_RESERVED,
  89. SOCK_STREAM );
  90. if (fd < 0) {
  91. D("no qemud control socket: %s", strerror(errno));
  92. return -1;
  93. }
  94. /* send service name to connect */
  95. if (qemu_fd_write(fd, name, namelen) != namelen) {
  96. D("can't send service name to qemud: %s",
  97. strerror(errno));
  98. close(fd);
  99. return -1;
  100. }
  101. /* read answer from daemon */
  102. if (qemu_fd_read(fd, answer, 2) != 2 ||
  103. answer[0] != 'O' || answer[1] != 'K') {
  104. D("cant' connect to %s service through qemud", name);
  105. close(fd);
  106. return -1;
  107. }
  108. channel->is_qemud = 1;
  109. channel->fd = fd;
  110. return 0;
  111. }
  112. static int
  113. qemu_channel_open_qemud_old( QemuChannel* channel,
  114. const char* name )
  115. {
  116. int fd;
  117. snprintf(channel->device, sizeof channel->device,
  118. "qemud_%s", name);
  119. fd = socket_local_client( channel->device,
  120. ANDROID_SOCKET_NAMESPACE_RESERVED,
  121. SOCK_STREAM );
  122. if (fd < 0) {
  123. D("no '%s' control socket available: %s",
  124. channel->device, strerror(errno));
  125. return -1;
  126. }
  127. close(fd);
  128. channel->is_qemud_old = 1;
  129. return 0;
  130. }
  131. static int
  132. qemu_channel_open_tty( QemuChannel* channel,
  133. const char* name,
  134. int mode )
  135. {
  136. char key[PROPERTY_KEY_MAX];
  137. char prop[PROPERTY_VALUE_MAX];
  138. int ret;
  139. ret = snprintf(key, sizeof key, "ro.kernel.android.%s", name);
  140. if (ret >= (int)sizeof key)
  141. return -1;
  142. if (property_get(key, prop, "") == 0) {
  143. D("no kernel-provided %s device name", name);
  144. return -1;
  145. }
  146. ret = snprintf(channel->device, sizeof channel->device,
  147. "/dev/%s", prop);
  148. if (ret >= (int)sizeof channel->device) {
  149. D("%s device name too long: '%s'", name, prop);
  150. return -1;
  151. }
  152. channel->is_tty = !memcmp("/dev/tty", channel->device, 8);
  153. return 0;
  154. }
  155. int
  156. qemu_channel_open( QemuChannel* channel,
  157. const char* name,
  158. int mode )
  159. {
  160. int fd = -1;
  161. /* initialize the channel is needed */
  162. if (!channel->is_inited)
  163. {
  164. channel->is_inited = 1;
  165. do {
  166. if (qemu_channel_open_qemud_pipe(channel, name) == 0)
  167. break;
  168. if (qemu_channel_open_qemud(channel, name) == 0)
  169. break;
  170. if (qemu_channel_open_qemud_old(channel, name) == 0)
  171. break;
  172. if (qemu_channel_open_tty(channel, name, mode) == 0)
  173. break;
  174. channel->is_available = 0;
  175. return -1;
  176. } while (0);
  177. channel->is_available = 1;
  178. }
  179. /* try to open the file */
  180. if (!channel->is_available) {
  181. errno = ENOENT;
  182. return -1;
  183. }
  184. if (channel->is_qemud) {
  185. return dup(channel->fd);
  186. }
  187. if (channel->is_qemud_old) {
  188. do {
  189. fd = socket_local_client( channel->device,
  190. ANDROID_SOCKET_NAMESPACE_RESERVED,
  191. SOCK_STREAM );
  192. } while (fd < 0 && errno == EINTR);
  193. }
  194. else /* /dev/ttySn ? */
  195. {
  196. do {
  197. fd = open(channel->device, mode);
  198. } while (fd < 0 && errno == EINTR);
  199. /* disable ECHO on serial lines */
  200. if (fd >= 0 && channel->is_tty) {
  201. struct termios ios;
  202. tcgetattr( fd, &ios );
  203. ios.c_lflag = 0; /* disable ECHO, ICANON, etc... */
  204. tcsetattr( fd, TCSANOW, &ios );
  205. }
  206. }
  207. return fd;
  208. }
  209. static int
  210. qemu_command_vformat( char* buffer,
  211. int buffer_size,
  212. const char* format,
  213. va_list args )
  214. {
  215. char header[5];
  216. int len;
  217. if (buffer_size < 6)
  218. return -1;
  219. len = vsnprintf(buffer+4, buffer_size-4, format, args);
  220. if (len >= buffer_size-4)
  221. return -1;
  222. snprintf(header, sizeof header, "%04x", len);
  223. memcpy(buffer, header, 4);
  224. return len + 4;
  225. }
  226. extern int
  227. qemu_command_format( char* buffer,
  228. int buffer_size,
  229. const char* format,
  230. ... )
  231. {
  232. va_list args;
  233. int ret;
  234. va_start(args, format);
  235. ret = qemu_command_format(buffer, buffer_size, format, args);
  236. va_end(args);
  237. return ret;
  238. }
  239. static int
  240. qemu_control_fd(void)
  241. {
  242. static QemuChannel channel[1];
  243. int fd;
  244. fd = qemu_channel_open( channel, "hw-control", O_RDWR );
  245. if (fd < 0) {
  246. D("%s: could not open control channel: %s", __FUNCTION__,
  247. strerror(errno));
  248. }
  249. return fd;
  250. }
  251. static int
  252. qemu_control_send(const char* cmd, int len)
  253. {
  254. int fd, len2;
  255. if (len < 0) {
  256. errno = EINVAL;
  257. return -1;
  258. }
  259. fd = qemu_control_fd();
  260. if (fd < 0)
  261. return -1;
  262. len2 = qemu_fd_write(fd, cmd, len);
  263. close(fd);
  264. if (len2 != len) {
  265. D("%s: could not send everything %d < %d",
  266. __FUNCTION__, len2, len);
  267. return -1;
  268. }
  269. return 0;
  270. }
  271. int
  272. qemu_control_command( const char* fmt, ... )
  273. {
  274. va_list args;
  275. char command[256];
  276. int len, fd;
  277. va_start(args, fmt);
  278. len = qemu_command_vformat( command, sizeof command, fmt, args );
  279. va_end(args);
  280. if (len < 0 || len >= (int)sizeof command) {
  281. if (len < 0) {
  282. D("%s: could not send: %s", __FUNCTION__, strerror(errno));
  283. } else {
  284. D("%s: too large %d > %d", __FUNCTION__, len, (int)(sizeof command));
  285. }
  286. errno = EINVAL;
  287. return -1;
  288. }
  289. return qemu_control_send( command, len );
  290. }
  291. extern int qemu_control_query( const char* question, int questionlen,
  292. char* answer, int answersize )
  293. {
  294. int ret, fd, len, result = -1;
  295. char header[5], *end;
  296. if (questionlen <= 0) {
  297. errno = EINVAL;
  298. return -1;
  299. }
  300. fd = qemu_control_fd();
  301. if (fd < 0)
  302. return -1;
  303. ret = qemu_fd_write( fd, question, questionlen );
  304. if (ret != questionlen) {
  305. D("%s: could not write all: %d < %d", __FUNCTION__,
  306. ret, questionlen);
  307. goto Exit;
  308. }
  309. /* read a 4-byte header giving the length of the following content */
  310. ret = qemu_fd_read( fd, header, 4 );
  311. if (ret != 4) {
  312. D("%s: could not read header (%d != 4)",
  313. __FUNCTION__, ret);
  314. goto Exit;
  315. }
  316. header[4] = 0;
  317. len = strtol( header, &end, 16 );
  318. if ( len < 0 || end == NULL || end != header+4 || len > answersize ) {
  319. D("%s: could not parse header: '%s'",
  320. __FUNCTION__, header);
  321. goto Exit;
  322. }
  323. /* read the answer */
  324. ret = qemu_fd_read( fd, answer, len );
  325. if (ret != len) {
  326. D("%s: could not read all of answer %d < %d",
  327. __FUNCTION__, ret, len);
  328. goto Exit;
  329. }
  330. result = len;
  331. Exit:
  332. close(fd);
  333. return result;
  334. }