CellsPrivateService.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. #define LOG_TAG "CELLSSERVICE"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <sys/types.h>
  6. #include <sys/socket.h>
  7. #include <netinet/in.h>
  8. #include <arpa/inet.h>
  9. #include <fcntl.h>
  10. #include <errno.h>
  11. #include <cutils/log.h>
  12. #include <cutils/properties.h>
  13. #include "CellsPrivateService.h"
  14. #include <binder/IServiceManager.h>
  15. #include <gui/ISurfaceComposer.h>
  16. #include <utils/String16.h>
  17. #include <powermanager/IPowerManager.h>
  18. #include <powermanager/PowerManager.h>
  19. namespace android {
  20. #define SYSTEMPRIVATE_LOGV(x, ...) ALOGV("[CellsPrivate] " x, ##__VA_ARGS__)
  21. #define SYSTEMPRIVATE_LOGD(x, ...) ALOGD("[CellsPrivate] " x, ##__VA_ARGS__)
  22. #define SYSTEMPRIVATE_LOGI(x, ...) ALOGI("[CellsPrivate] " x, ##__VA_ARGS__)
  23. #define SYSTEMPRIVATE_LOGW(x, ...) ALOGW("[CellsPrivate] " x, ##__VA_ARGS__)
  24. #define SYSTEMPRIVATE_LOGE(x, ...) ALOGE("[CellsPrivate] " x, ##__VA_ARGS__)
  25. CellsPrivateService::CellsPrivateService()
  26. {
  27. setCellstared();
  28. }
  29. CellsPrivateService::~CellsPrivateService()
  30. {
  31. }
  32. int CellsPrivateService::isInCellstar()
  33. {
  34. return mtar_pthread_t;
  35. }
  36. void CellsPrivateService::setCellstaring()
  37. {
  38. mtar_pthread_t = 1;
  39. }
  40. void CellsPrivateService::setCellstared()
  41. {
  42. mtar_pthread_t = 0;
  43. }
  44. static void* tar_pthead(void* o)
  45. {
  46. CellsPrivateService* pService = (CellsPrivateService*)o;
  47. if(!pService)
  48. return NULL;
  49. errno = 0;
  50. if ( nice((int)-15) == -1 || errno) {
  51. ALOGE("Cannot change priority %s (%d)\n",strerror(errno),errno);
  52. }
  53. pService->setCellstaring();
  54. // Delete tar
  55. system("rm -rf /data/cells/hwcell.img");
  56. ALOGE("rm -rf /data/cells/hwcell.img \n");
  57. // Make tar
  58. //system("cd /data/cells && tar zcvfp hwcell.img --exclude=cell1-rw/data/dalvik-cache --exclude=cell1-rw/data/logs cell1-rw");
  59. system("cd /data/cells && tar zcvfp hwcell.img --exclude=cell1-rw/data/dalvik-cache --exclude=cell1-rw/data/cells.logs --exclude=cell1-rw/data/misc/logd cell1-rw");
  60. ALOGE("cd /data/cells && tar zcvfp hwcell.img --exclude=cell1-rw/data/dalvik-cache --exclude=cell1-rw/data/cells.logs --exclude=cell1-rw/data/misc/logd cell1-rw \n");
  61. pService->setCellstared();
  62. return (void*)0;
  63. }
  64. void CellsPrivateService::startCellstar()
  65. {
  66. int ret;
  67. pthread_t daemon_thread;
  68. if(isInCellstar())
  69. return ;
  70. /* Start listening for connections in a new thread */
  71. ret = pthread_create(&daemon_thread, NULL,tar_pthead, this);
  72. if (ret) {
  73. ALOGE("create_tar_pthread err: %s", strerror(errno));
  74. }
  75. }
  76. status_t CellsPrivateService::setProperty(const String16& name,const String16& value)
  77. {
  78. SYSTEMPRIVATE_LOGD("SETPROPERTY arg %s %s", String8(name).string(), String8(value).string());
  79. status_t result = property_set(String8(name).string(), String8(value).string());
  80. SYSTEMPRIVATE_LOGD("SETPROPERTY result = %d", result);
  81. return result;
  82. }
  83. status_t CellsPrivateService::startCellsVM(const String16& name)
  84. {
  85. name;
  86. char cmd[200];
  87. snprintf(cmd, sizeof(cmd), "cellc start %s",String8(name).string());
  88. SYSTEMPRIVATE_LOGD("STARTCELLSVM cmd = %s", cmd);
  89. system(cmd);
  90. return NO_ERROR;
  91. }
  92. status_t CellsPrivateService::stopCellsVM(const String16& name)
  93. {
  94. name;
  95. char cmd[200];
  96. snprintf(cmd, sizeof(cmd), "cellc stop %s",String8(name).string());
  97. SYSTEMPRIVATE_LOGD("STOPCELLSVM cmd = %s", cmd);
  98. system(cmd);
  99. return NO_ERROR;
  100. }
  101. status_t CellsPrivateService::cellsSwitchVM(const String16& name)
  102. {
  103. name;
  104. char cmd[200];
  105. snprintf(cmd, sizeof(cmd), "cellc switch %s",String8(name).string());
  106. SYSTEMPRIVATE_LOGD("CELLSSWITCHVM cmd = %s", cmd);
  107. system(cmd);
  108. property_set("persist.sys.active", String8(name).string());
  109. return NO_ERROR;
  110. }
  111. status_t CellsPrivateService::cellsSwitchHOST(const String16& name)
  112. {
  113. name;
  114. char cmd[200];
  115. snprintf(cmd, sizeof(cmd), "cellc switch host");
  116. SYSTEMPRIVATE_LOGD("CELLSSWITCHHOST cmd = %s", cmd);
  117. system(cmd);
  118. property_set("persist.sys.active", "");
  119. return NO_ERROR;
  120. }
  121. static void* gotosleep(void* o)
  122. {
  123. o;
  124. {
  125. android::sp<android::IServiceManager> sm = android::defaultServiceManager();
  126. android::sp<android::IPowerManager> mPowerManager =
  127. android::interface_cast<android::IPowerManager>(sm->checkService(android::String16("power")));
  128. if(mPowerManager != NULL){
  129. mPowerManager->goToSleep(long(ns2ms(systemTime())),GO_TO_SLEEP_REASON_POWER_BUTTON,0);
  130. }
  131. }
  132. ALOGD("BACK SYSTEM go to sleep...");
  133. return (void*)0;
  134. };
  135. static void create_gotosleep_pthread(void)
  136. {
  137. int ret;
  138. pthread_t daemon_thread;
  139. /* Start listening for connections in a new thread */
  140. ret = pthread_create(&daemon_thread, NULL,gotosleep, NULL);
  141. if (ret) {
  142. ALOGE("create_gotosleep_pthread err: %s", strerror(errno));
  143. }
  144. };
  145. status_t CellsPrivateService::switchCellsVM(const String16& name)
  146. {
  147. int i = 0;
  148. char value[PROPERTY_VALUE_MAX];
  149. char pname[PATH_MAX];
  150. ALOGD("switchCellsVM: %s",String8(name).string());
  151. property_get("ro.boot.vm", value, "1");
  152. if((strcmp(value, "0") == 0)){
  153. {
  154. sscanf(String8(name).string(), "cell%d",&i);
  155. if(i <= 0)
  156. return 0;
  157. memset(value,0,PROPERTY_VALUE_MAX);
  158. memset(pname,0,PATH_MAX);
  159. sprintf(pname, "persist.sys.%s.init", String8(name).string());
  160. property_get(pname, value, "0");
  161. if((strcmp(value, "0") == 0))
  162. return 0;
  163. }
  164. {
  165. exitHost(android::String16("host"));
  166. }
  167. {
  168. cellsSwitchVM(name);
  169. }
  170. {
  171. android::sp<android::IServiceManager> sm = android::OtherServiceManager(i);
  172. android::sp<android::ICellsPrivateService> mCellsPrivateService =
  173. android::interface_cast<android::ICellsPrivateService>(sm->checkService(android::String16("CellsPrivateService")));
  174. if(mCellsPrivateService != NULL){
  175. mCellsPrivateService->enterCell(name);
  176. }else{
  177. SYSTEMPRIVATE_LOGD("OtherServiceManager = 0");
  178. }
  179. }
  180. }else{
  181. {
  182. if(strcmp(String8(name).string(), "host") != 0)
  183. return 0;
  184. }
  185. {
  186. exitCell(name);
  187. }
  188. {
  189. android::sp<android::IServiceManager> sm = android::initdefaultServiceManager();
  190. android::sp<android::ICellsPrivateService> mCellsPrivateService =
  191. android::interface_cast<android::ICellsPrivateService>(sm->checkService(android::String16("CellsPrivateService")));
  192. if(mCellsPrivateService != NULL){
  193. mCellsPrivateService->cellsSwitchHOST(android::String16("host"));
  194. }
  195. }
  196. {
  197. android::sp<android::IServiceManager> sm = android::initdefaultServiceManager();
  198. android::sp<android::ICellsPrivateService> mCellsPrivateService =
  199. android::interface_cast<android::ICellsPrivateService>(sm->checkService(android::String16("CellsPrivateService")));
  200. if(mCellsPrivateService != NULL){
  201. mCellsPrivateService->enterHost(android::String16("host"));
  202. }
  203. }
  204. }
  205. {
  206. create_gotosleep_pthread();
  207. }
  208. SYSTEMPRIVATE_LOGD("SWITCHCELLSVM result = %d", 0);
  209. return NO_ERROR;
  210. }
  211. status_t CellsPrivateService::enterHost(const String16& name)
  212. {
  213. name;
  214. {
  215. property_set("persist.sys.exit", "0");
  216. }
  217. {
  218. android::sp<android::IServiceManager> sm = android::defaultServiceManager();
  219. android::sp<android::IPowerManager> mPowerManager =
  220. android::interface_cast<android::IPowerManager>(sm->checkService(android::String16("power")));
  221. if(mPowerManager != NULL){
  222. mPowerManager->wakeUp(long(ns2ms(systemTime())),WAKE_REASON_POWER_BUTTON,
  223. android::String16("enter_self"),android::String16("CellsPrivateService"));
  224. }
  225. }
  226. {
  227. property_set("ctl.restart", "adbd");
  228. }
  229. {
  230. android::sp<android::IServiceManager> sm = android::defaultServiceManager();
  231. android::sp<android::ISurfaceComposer> mComposer =
  232. android::interface_cast<android::ISurfaceComposer>(sm->checkService(android::String16("SurfaceFlinger")));
  233. if(mComposer != NULL){
  234. mComposer->enterSelf();
  235. }
  236. }
  237. {
  238. //startCellstar();
  239. }
  240. SYSTEMPRIVATE_LOGD("ENTERHOST result = %d", 0);
  241. return NO_ERROR;
  242. }
  243. status_t CellsPrivateService::exitHost(const String16& name)
  244. {
  245. name;
  246. {
  247. property_set("persist.sys.exit", "1");
  248. }
  249. {
  250. android::sp<android::IServiceManager> sm = android::defaultServiceManager();
  251. android::sp<android::ISurfaceComposer> mComposer =
  252. android::interface_cast<android::ISurfaceComposer>(sm->checkService(android::String16("SurfaceFlinger")));
  253. if(mComposer != NULL){
  254. mComposer->exitSelf();
  255. }
  256. }
  257. {
  258. property_set("ctl.stop", "adbd");
  259. }
  260. SYSTEMPRIVATE_LOGD("EXITHOST result = %d", 0);
  261. return NO_ERROR;
  262. }
  263. static void write_vm_exit(bool bexit){
  264. int vmfd = open("/.cell",O_WRONLY);
  265. if(vmfd>=0){
  266. if(bexit)
  267. write(vmfd,"1",strlen("1"));
  268. else
  269. write(vmfd,"0",strlen("0"));
  270. close(vmfd);
  271. }
  272. }
  273. status_t CellsPrivateService::enterCell(const String16& name)
  274. {
  275. name;
  276. {
  277. write_vm_exit(false);
  278. property_set("persist.sys.exit", "0");
  279. }
  280. {
  281. android::sp<android::IServiceManager> sm = android::defaultServiceManager();
  282. android::sp<android::IPowerManager> mPowerManager =
  283. android::interface_cast<android::IPowerManager>(sm->checkService(android::String16("power")));
  284. if(mPowerManager != NULL){
  285. mPowerManager->wakeUp(long(ns2ms(systemTime())),WAKE_REASON_POWER_BUTTON,
  286. android::String16("enter_self"),android::String16("CellsPrivateService"));
  287. }
  288. }
  289. {
  290. property_set("ctl.restart", "adbd");
  291. }
  292. {
  293. android::sp<android::IServiceManager> sm = android::defaultServiceManager();
  294. android::sp<android::ISurfaceComposer> mComposer =
  295. android::interface_cast<android::ISurfaceComposer>(sm->checkService(android::String16("SurfaceFlinger")));
  296. if(mComposer != NULL){
  297. mComposer->enterSelf();
  298. }
  299. }
  300. SYSTEMPRIVATE_LOGD("ENTERCELL result = %d", 0);
  301. return NO_ERROR;
  302. }
  303. status_t CellsPrivateService::exitCell(const String16& name)
  304. {
  305. name;
  306. {
  307. write_vm_exit(true);
  308. property_set("persist.sys.exit", "1");
  309. }
  310. {
  311. android::sp<android::IServiceManager> sm = android::defaultServiceManager();
  312. android::sp<android::ISurfaceComposer> mComposer =
  313. android::interface_cast<android::ISurfaceComposer>(sm->checkService(android::String16("SurfaceFlinger")));
  314. if(mComposer != NULL){
  315. mComposer->exitSelf();
  316. }
  317. }
  318. {
  319. property_set("ctl.stop", "adbd");
  320. }
  321. SYSTEMPRIVATE_LOGD("EXITCELL result = %d", 0);
  322. return NO_ERROR;
  323. }
  324. status_t CellsPrivateService::uploadCellsVM(const String16& name)
  325. {
  326. name;
  327. // Delete tar
  328. system("rm -rf /data/cells/hwcell.img");
  329. ALOGE("rm -rf /data/cells/hwcell.img \n");
  330. // Make tar
  331. system("cd /data/cells && busybox tar zcvfp hwcell.img cell1-rw");
  332. ALOGE("busybox tar zcvfp /data/cells/hwcell.img /data/cells/cell1-rw \n");
  333. // Upload tar
  334. system("cd /data/cells && busybox ftpput -u ftp-hw-user -p 1 129.204.70.73 hwcell.img");
  335. ALOGE("busybox ftpput -u ftp-hw-user -p 1 129.204.70.73 /data/cells/hwcell.img err=%s \n", strerror(errno));
  336. return NO_ERROR;
  337. }
  338. status_t CellsPrivateService::downloadCellsVM(const String16& name)
  339. {
  340. name;
  341. char cmd[200];
  342. if(access("/data/cells/hwcell.img",F_OK)!= 0){
  343. system("touch /data/cells/hwcell.img ");
  344. }
  345. // Breakpoint renewal
  346. system("cd /data/cells && busybox ftpget -c -u ftp-hw-user -p 1 129.204.70.73 hwcell.img");
  347. ALOGE("busybox ftpget -u ftp-hw-user -p 1 129.204.70.73 /data/cells/hwcell.img err=%s \n", strerror(errno));
  348. // Stop the vm
  349. snprintf(cmd, sizeof(cmd), "cellc stop cell1");
  350. system(cmd);
  351. ALOGE("cellc stop cell1 \n");
  352. // sleep 2s
  353. sleep(2);
  354. // un tar
  355. system("rm -rf /data/cells/cell1-rw && mkdir /data/cells/cell1-rw");
  356. ALOGE("rm -rf /data/cells/cell1-rw \n");
  357. system("cd /data/cells && busybox tar zxvfp hwcell.img");
  358. ALOGE("busybox tar zxvfp /data/cells/hwcell.img /data/cells/cell1-rw \n");
  359. return NO_ERROR;
  360. }
  361. status_t CellsPrivateService::untarCellsVM(const String16& name)
  362. {
  363. name;
  364. char cmd[200];
  365. // Get the sdcard gz
  366. if(access("/data/data/com.cells.cellswitch.secure/hwcell.img",F_OK) != 0){
  367. return NO_ERROR;
  368. }
  369. // un tar
  370. //system("rm -rf /data/cells/cell1-rw && mkdir /data/cells/cell1-rw");
  371. // ALOGE("rm -rf /data/cells/cell1-rw \n");
  372. system("cd /data/cells && tar zxvfp /data/data/com.cells.cellswitch.secure/hwcell.img");
  373. ALOGE("cd /data/cells && tar zxvfp /data/data/com.cells.cellswitch.secure/hwcell.img \n");
  374. system("rm -rf /data/data/com.cells.cellswitch.secure/hwcell.img");
  375. ALOGE("rm -rf /data/data/com.cells.cellswitch.secure/hwcell.img \n");
  376. return NO_ERROR;
  377. }
  378. status_t CellsPrivateService::tarCellsVM(const String16& name)
  379. {
  380. name;
  381. if(!isInCellstar())
  382. {
  383. if(access("/data/cells/hwcell.img",F_OK) != 0){
  384. startCellstar();
  385. sleep(2);
  386. }
  387. }
  388. while(isInCellstar())
  389. sleep(1);
  390. return NO_ERROR;
  391. }
  392. status_t CellsPrivateService::sendCellsVM(const String16& path, const String16& address)
  393. {
  394. path;
  395. int sock = 0;
  396. struct sockaddr_in addr;
  397. FILE* file = fopen("/data/cells/hwcell.img", "r");
  398. if(file == NULL){
  399. ALOGE("sendCellsVM fopen err. ");
  400. return -1;
  401. }
  402. if((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0){
  403. fclose(file);
  404. return INVALID_OPERATION;
  405. }
  406. memset(&addr, 0, sizeof(addr));
  407. addr.sin_family = AF_INET;
  408. addr.sin_port = htons(10000);
  409. inet_pton(AF_INET, String8(address).string(), &addr.sin_addr);
  410. if(connect(sock, (struct sockaddr*)&addr, sizeof(addr)) < 0){
  411. ALOGE("sendCellsVM connect err. ");
  412. fclose(file);
  413. return INVALID_OPERATION;
  414. }
  415. fseek(file, 0, SEEK_END);
  416. int filelen = ftell(file);
  417. if(send(sock, &filelen, sizeof(filelen), 0) < 0){
  418. close(sock);
  419. fclose(file);
  420. ALOGE("send file len err. ");
  421. return INVALID_OPERATION;
  422. }
  423. rewind(file);
  424. char buffer[4096] = {0};
  425. int len = 0, count = 0;
  426. while((len = fread(buffer, 1, 4096, file)) > 0){
  427. if(send(sock, buffer, len, 0) < 0){
  428. close(sock);
  429. fclose(file);
  430. ALOGE("sendCellsVM send err. ");
  431. return INVALID_OPERATION;
  432. }
  433. count += len;
  434. }
  435. close(sock);
  436. fclose(file);
  437. system("rm -rf /data/cells/hwcell.img");
  438. ALOGD("rm -rf /data/cells/hwcell.img");
  439. return (count == filelen)?NO_ERROR:UNKNOWN_ERROR;
  440. }
  441. status_t CellsPrivateService::vmSystemReady(const String16& name)
  442. {
  443. char pname[PATH_MAX] = {0};
  444. sprintf(pname, "persist.sys.%s.init", String8(name).string());
  445. property_set(pname, "1");
  446. property_set("ctl.restart", "adbd");
  447. SYSTEMPRIVATE_LOGD("SYSTEMREADY name = %s", String8(name).string());
  448. return NO_ERROR;
  449. }
  450. };