Home | History | Annotate | Download | only in android
      1 /* Copyright (C) 2010 The Android Open Source Project
      2 **
      3 ** This software is licensed under the terms of the GNU General Public
      4 ** License version 2, as published by the Free Software Foundation, and
      5 ** may be copied, distributed, and modified under those terms.
      6 **
      7 ** This program is distributed in the hope that it will be useful,
      8 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
      9 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     10 ** GNU General Public License for more details.
     11 */
     12 
     13 /*
     14  * Contains implementation of routines and that are used in the course
     15  * of the emulator's core initialization.
     16  */
     17 
     18 #include <stdio.h>
     19 #include <stdlib.h>
     20 #include <errno.h>
     21 #include "qemu-common.h"
     22 #include "android/sockets.h"
     23 #include "android/android.h"
     24 #include "android/core-init-utils.h"
     25 #include "android/utils/bufprint.h"
     26 
     27 extern char* android_op_ui_port;
     28 
     29 /* Sends core initialization status message back to the UI that started this
     30  * core process.
     31  * Param:
     32  *  msg - Message to send. On success, message must begin with "ok:", followed
     33  *      by "port=<console port number>". On initialization failure, message must
     34  *      begin with "ko:", followed by a string describing the reason of failure.
     35  */
     36 static void
     37 android_core_send_init_response(const char* msg)
     38 {
     39     int fd;
     40     int ui_port;
     41 
     42     if (android_op_ui_port == NULL) {
     43         // No socket - no reply.
     44         return;
     45     }
     46 
     47     ui_port = atoi(android_op_ui_port);
     48     if (ui_port >= 0) {
     49         // At this point UI always starts the core on the same workstation.
     50         fd = socket_loopback_client(ui_port, SOCKET_STREAM);
     51         if (fd == -1) {
     52             fprintf(stderr, "Unable to create UI socket client for port %s: %s\n",
     53                     android_op_ui_port, errno_str);
     54             return;
     55         }
     56         socket_send(fd, msg, strlen(msg) + 1);
     57         socket_close(fd);
     58     } else {
     59         fprintf(stderr, "Invalid -ui-port parameter: %s\n", android_op_ui_port);
     60     }
     61 }
     62 
     63 void
     64 android_core_init_completed(void)
     65 {
     66     char msg[32];
     67     snprintf(msg, sizeof(msg), "ok:port=%d", android_base_port);
     68     android_core_send_init_response(msg);
     69 }
     70 
     71 void
     72 android_core_init_failure(const char* fmt, ...)
     73 {
     74     va_list  args;
     75     char msg[4096];
     76 
     77     // Format "ko" message to send back to the UI.
     78     snprintf(msg, sizeof(msg), "ko:");
     79 
     80     va_start(args, fmt);
     81     vbufprint(msg + strlen(msg), msg + sizeof(msg), fmt, args);
     82     va_end(args);
     83 
     84     // Send message back to the UI, print it to the error stdout, and exit the
     85     // process.
     86     android_core_send_init_response(msg);
     87     fprintf(stderr, "%s\n", msg);
     88     exit(1);
     89 }
     90 
     91 void
     92 android_core_init_exit(int exit_status)
     93 {
     94     char msg[32];
     95     // Build "ok" message with the exit status, and send it back to the UI.
     96     snprintf(msg, sizeof(msg), "ok:status=%d", exit_status);
     97     android_core_send_init_response(msg);
     98     exit(exit_status);
     99 }
    100