Adaptation issues for vehicle-machine multi-user systems

Background of multi-user problem

Record the multi-user adaptation issues:

The background is that two new apks have been pushed under system/app, one is our business scenario apk and the other is the apk of the virtual car CarService service. Our apk needs to link to the CarService service and communicate through AIDL.

The following two pictures are without roo (the current car user is user14, and the userid of the virtual car is 0): The virtual car cannot be found, and the virtual car keeps crashing in the log: ServiceManager.addService verification failed

image.png

image.png

Next, the log after turning on root is:

image.png

It’s amazing. This article is about why communication can be normal after turning on Root:

The vehicle systems we used in the past were all single-user, so there was no problem of different users not sharing data; however, a new project recently used a vehicle system that is a multi-user system. I have summarized some information about multi-users. You can take a look. The following statement:

Multi-user system knowledge

1. In the multi-user state, the user creation process is to share the system/app directory, but the parameters are modified (its userid defaults to 0), and the data/app directory is reinstalled (the userid is determined during installation, and the same is true for installation under multi-users) )

2. To start the four major components, you need to specify the corresponding accepted user (default system current user)

3. The Binder communication of systemServer will carry UserId. If the service does not exist in the current UserId, the corresponding Binder cannot be found (because the current user is 10, but the user under systemapp is 0 and needs to be rooted before communication can be performed)

4. Duopen uses this multi-user method, as long as the uid is different (userid*10000+appid). The appid is the same, but the userid is different, so the uid is different, so you can open multiple apps. But how do different users communicate?

5. When the app reads the storage SD card path and the app private directory, it will actually be soft-connected to the partition of the corresponding user directory.

Therefore, multi-user switching is essentially changing the storage and reading information of data/app to a directory; for the system app, it only modifies some parameters and changes some attributes of the system; for the service manager, it switches to another directory. The binder thread pool used by the user (this is why virtual cars can be added to servicemanager after root)

6. Therefore, one thing we need to consider when using multi-user is how to let our app communicate with the system app, which are two different users. The default startup of the car and machine is userid10, but the app under the system is user0. The premise of communication is that the binder thread pool of the app under the current car and machine user must have the binder of the system service. This leads to a conflict. The car and machine user is indeed a 10 system user. 0 I’ll explain how to solve it later.

7. The system can be restricted and can restrict the userid to which the corresponding APP must belong. (This is also the focus of this article because this knowledge point is our solution)

What are the problems caused by multiple users and why

The first problem is that our process keeps reconnecting when binding Service.

The default user for the car startup system is user10 , but the service we need to link to is under user0. Even if our APK and virtual car are both user0, we cannot connect. According to the third point listed above:

The Binder we need to link to is the bound current system user, which is user10, so when using the service, an exception that the Service cannot be found was reported . Because this service is not under user10 at all, it is under user0, so the natural link fails.

The second problem is that the virtual car keeps reporting ServiceManager.addService verification failure exception.

This is also easy to understand. When adding a system service, the ServiceManager will verify whether the current system user and the corresponding user of the Service to be added are consistent. If they are inconsistent (and cannot be found), the addition will not be allowed.

How to solve the problem?

Here are the answers to the questions raised in the sixth knowledge point above:

The simplest way is to switch the ROOT user. Use adb root to switch to the system user user0 and you can connect to the service normally, and the virtual car can also be added to the ServiceManager normally.

The reason is that the servicemanager under user0 can retrieve this virtual car service.

Remember to kill the process after switching users. Although AMS kills the process during user switching, the data will still be retained, and the data of user10 will still be used.

But the problem can never be solved so easily. Test students must manually root when they need to test. Fortunately, what if they are users? Users cannot root, so they have to find other solutions. Fortunately, this afternoon I discovered a particularly weird phenomenon:

A particularly strange breaking point

Another colleague in our department, his APP is also under system/app, but the userid is always user10. I have a question: Shouldn’t everything under system/app be user0? How is it possible that his is 10? Later, after chatting with their leader, I found that their APP has communicated with ROM in advance and can limit their userid, so their userid has always been 10.

Problem solved~

Therefore, we have a solution for communicating with the virtual car. That’s right: tell the ROM manufacturer to limit the userid of the virtual car to 10, so that our app can normally connect to the service under the user10 that is started by default in the system . The virtual car service It can also be added to SystemServer under user10 .

Solve the sequelae caused by

The problem is solved, but there is a serious sequelae that bothers me:

Let’s make the conclusion first: APP1 after userid10 is restricted by ROM, it can only launch the process of userid10.

The same is that system/app has no limit on userid, why is it userid10?

A question arises: The other process APP2 is also under the system. Although its userid is not restricted by the ROM, its userid is also 10. Why is this? ? ? The only difference is that APP2 will be pulled up again by APP1

After knowing this only difference, excluding all constants, the only variable is the userid of APP1. Or more broadly speaking, which userid is your app, then the process you launch is also the userid.

Because different users are isolated, if he is user10's APP, then he can only launch user10's APP. This sequelae is something I have been wondering about since then, and it suddenly became clear to me at this point. Let me end this article with a summary of knowledge points:

I forgot to mention an important point in the above knowledge points:

When APP1 pulls up another APP2, what is the userid of APP2?

The answer is that it has nothing to do with the current user of the system. Different users are isolated. The userid of APP2 and the userid of APP1 are the same.

That is to say, if the same APP1 under different users wants to communicate with another APP2, the basis for whether he can pull up another APP2 is to judge whether there is a process of APP2 under the userid of APP1. If there is, there is no need to pull it up. If not, then it will not be started. The process needs to be re-created.

To give a specific example:

The system default user is still user10, and our process userid is 0 (under system/app). An APP with a userid of 10 needs to pull up our process. It will determine that the usreid of this APP is 10, and then search under userid 10 and find that there is no our process , so it will pull up a process of ours with a userid of 10 again .

At this time, we have two processes, one is user0, which is started by the system by default, and the other is pulled up by the userid10 process.

Reference link

This article talks about multi-user adaptation

This article is about application clones

Basic knowledge:

(Mainly introduces the difference between userid, uid and appid)

  1. blog.csdn.net/qq_34888036…
  2. www.yht7.com/news/156702

Preservation and reconstruction of processes under multi-user:

(It is better to focus on the multi-user switching, creation and deletion process in these two articles. The basic points are not as good as the first link)

1.  In-depth understanding of multi-user Android system_environment.get_ulangch's blog under multi-user - CSDN  blog

2.   Android--Multi-user mode_disallow_cross_profile_copy_paste_I am an ordinary person's blog-CSDN blog

This article is reprinted in: Adaptation issues for vehicle-machine multi-user systems - Nuggets (juejin.cn)

Home page link: Beiyang's personal homepage - Articles - Nuggets (juejin.cn)

Guess you like

Origin blog.csdn.net/m0_65909361/article/details/132837632