• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2009 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 /* this program is used to read a set of system properties and their values
18  * from the emulator program and set them in the currently-running emulated
19  * system. It does so by connecting to the 'boot-properties' qemud service.
20  *
21  * This program should be run as root and called from
22  * /system/etc/init.goldfish.rc exclusively.
23  */
24 
25 #define LOG_TAG  "qemu-props"
26 
27 #define DEBUG  1
28 
29 #if DEBUG
30 #  include <log/log.h>
31 #  define  DD(...)    ALOGI(__VA_ARGS__)
32 #else
33 #  define  DD(...)    ((void)0)
34 #endif
35 
36 #include <cutils/properties.h>
37 #include <unistd.h>
38 #include <qemu_pipe_bp.h>
39 #include <qemud.h>
40 #include <string.h>
41 #include <stdio.h>
42 
43 /* Name of the qemud service we want to connect to.
44  */
45 #define  QEMUD_SERVICE  "boot-properties"
46 
47 #define  MAX_TRIES      5
48 
49 #define QEMU_MISC_PIPE "QemuMiscPipe"
50 
51 int s_QemuMiscPipe = -1;
52 void static notifyHostBootComplete();
53 void static sendHeartBeat();
54 void static sendMessage(const char* mesg);
55 
main(void)56 int  main(void)
57 {
58     int  qemud_fd, count = 0;
59 
60     /* try to connect to the qemud service */
61     {
62         int  tries = MAX_TRIES;
63 
64         while (1) {
65             qemud_fd = qemud_channel_open( "boot-properties" );
66             if (qemud_fd >= 0)
67                 break;
68 
69             if (--tries <= 0) {
70                 DD("Could not connect after too many tries. Aborting");
71                 return 1;
72             }
73 
74             DD("waiting 1s to wait for qemud.");
75             sleep(1);
76         }
77     }
78 
79     DD("connected to '%s' qemud service.", QEMUD_SERVICE);
80 
81     /* send the 'list' command to the service */
82     if (qemud_channel_send(qemud_fd, "list", -1) < 0) {
83         DD("could not send command to '%s' service", QEMUD_SERVICE);
84         return 1;
85     }
86 
87     /* read each system property as a single line from the service,
88      * until exhaustion.
89      */
90     for (;;)
91     {
92 #define  BUFF_SIZE   (PROPERTY_KEY_MAX + PROPERTY_VALUE_MAX + 2)
93         DD("receiving..");
94         char* q;
95         char  temp[BUFF_SIZE];
96         char  vendortemp[BUFF_SIZE];
97         int   len = qemud_channel_recv(qemud_fd, temp, sizeof(temp) - 1);
98 
99         /* lone NUL-byte signals end of properties */
100         if (len < 0 || len > BUFF_SIZE-1 || temp[0] == '\0')
101             break;
102 
103         temp[len] = '\0';  /* zero-terminate string */
104 
105         DD("received: %.*s", len, temp);
106 
107         /* separate propery name from value */
108         q = strchr(temp, '=');
109         if (q == NULL) {
110             DD("invalid format, ignored.");
111             continue;
112         }
113         *q++ = '\0';
114 
115         char* final_prop_name = NULL;
116         if (strcmp(temp, "qemu.sf.lcd.density") == 0 ) {
117             final_prop_name = temp;
118         } else if (strcmp(temp, "qemu.hw.mainkeys") == 0 ) {
119             final_prop_name = temp;
120         } else if (strcmp(temp, "qemu.cmdline") == 0 ) {
121             final_prop_name = temp;
122         } else if (strcmp(temp, "dalvik.vm.heapsize") == 0 ) {
123             continue; /* cannot set it here */
124         } else if (strcmp(temp, "ro.opengles.version") == 0 ) {
125             continue; /* cannot set it here */
126         } else {
127             snprintf(vendortemp, sizeof(vendortemp), "vendor.%s", temp);
128             final_prop_name = vendortemp;
129         }
130         if (property_set(temp, q) < 0) {
131             ALOGW("could not set property '%s' to '%s'", final_prop_name, q);
132         } else {
133             ALOGI("successfully set property '%s' to '%s'", final_prop_name, q);
134             count += 1;
135         }
136     }
137 
138     close(qemud_fd);
139 
140     char temp[BUFF_SIZE];
141     sendHeartBeat();
142     while (s_QemuMiscPipe >= 0) {
143         usleep(5000000); /* 5 seconds */
144         sendHeartBeat();
145         property_get("vendor.qemu.dev.bootcomplete", temp, "");
146         int is_boot_completed = (strncmp(temp, "1", 1) == 0) ? 1 : 0;
147         if (is_boot_completed) {
148             ALOGI("tell the host boot completed");
149             notifyHostBootComplete();
150             break;
151         }
152     }
153 
154     while (s_QemuMiscPipe >= 0) {
155         usleep(30*1000000); /* 30 seconds */
156         sendHeartBeat();
157     }
158 
159     /* finally, close the channel and exit */
160     if (s_QemuMiscPipe >= 0) {
161         close(s_QemuMiscPipe);
162         s_QemuMiscPipe = -1;
163     }
164     DD("exiting (%d properties set).", count);
165     return 0;
166 }
167 
sendHeartBeat()168 void sendHeartBeat() {
169     sendMessage("heartbeat");
170 }
171 
notifyHostBootComplete()172 void notifyHostBootComplete() {
173     sendMessage("bootcomplete");
174 }
175 
sendMessage(const char * mesg)176 void sendMessage(const char* mesg) {
177    if (s_QemuMiscPipe < 0) {
178         s_QemuMiscPipe = qemu_pipe_open_ns(NULL, QEMU_MISC_PIPE, O_RDWR);
179         if (s_QemuMiscPipe < 0) {
180             ALOGE("failed to open %s", QEMU_MISC_PIPE);
181             return;
182         }
183     }
184     char set[64];
185     snprintf(set, sizeof(set), "%s", mesg);
186     int pipe_command_length = strlen(set)+1; //including trailing '\0'
187     qemu_pipe_write_fully(s_QemuMiscPipe, &pipe_command_length, sizeof(pipe_command_length));
188     qemu_pipe_write_fully(s_QemuMiscPipe, set, pipe_command_length);
189     qemu_pipe_read_fully(s_QemuMiscPipe, &pipe_command_length, sizeof(pipe_command_length));
190     if (pipe_command_length > (int)(sizeof(set)) || pipe_command_length <= 0)
191         return;
192     qemu_pipe_read_fully(s_QemuMiscPipe, set, pipe_command_length);
193 }
194