Before I start, this is meant for our own android based device and not for a phone nor for deployment elsewhere.
We have a service in init.rc that is a postgresql database server. This launches on startup and always runs in the background for the system. There's the possibility that it might close however and we would like to have a way to stop and start this service from the android side of the system.
Is there a way to send an init start command from an android Activity? From a root shell, this would be the equivalent of running "start servicename" and "stop servicename".
To start a service which is declared in the init.rc file, i think you must change the "ctl.start" system property with following commands :
In c file :
property_set("ctl.start", "<service_name>");
In java :
SystemProperties.set("ctl.start", "<service_name>");
This implies that your activity has system permissions (in the manifest) :
android:sharedUserId="android.uid.system"
and is signed by your system key (or put platform in the Android.mk)
As you can guess, to stop the service use following commands :
property_set("ctl.stop", "<service_name>");
or
SystemProperties.set("ctl.stop", "<service_name>");
Related
First of all, please note that this question is not same as all the "android foreground app" questions I found on SO, please read on :-)
I'm trying to write an android app for my own use, using golang, without using android-sdk or ndk (this is the KEY point). It is pretty simple, just use golang to write a http service, compile it for arm CPU and voila my app is running and can be access by simply visit http://localhost.
For the purpose of my app, I need to know the currently running foreground application, to define it precisely:
Foreground application is the application that occupies the screen, or has an "activity" what-so-ever (forgive me I'm not an android developer).
Anything that that is depended by the foreground application (e.g. services) is NOT what I am interested in.
If the phone is LOCKED/screen turned off, I want the solution to tell me there is NO foreground app.
And since I do not use anything android, just treat the phone as a LINUX machine, I want the solution to use native LINUX ways, e.g. by inspect /proc, or by calling any installed android command line tool (including sending messages via these command line tools), but NOT using any SDK/NDK way so that I have to use java or incorporate these thing into my app.
Starting from Android SDK 26 (if I remember well) Apps are executed on -one-User-per-App, so (i.e.) WhatsApp is running on UID=30 and Telegram on UID=76, so executing a ROOT command of "ps -A -o PID,USER,NAME" you can parse output and then Kill all Processes that you don't want to be executed.
36119 u30_a149 <WhatsApp_packagename>
36203 u76_a211 <Telegram_packagename>
37399 root [kworker/1:2H]
37423 u0_a329 su
38069 root sh
Without Root Permissions nothing of what you're trying to achieve is possible simply because is not possibile to denied an application to be executed or to kill it without Superuser privilege.
I am trying to execute an fopen() function on a file that is given permissions only to "shell" from a native (C++) application that is triggered from a service on my Android application. When I run the native code as a PIE from the shell, I am able to open the file for reading, but if I try from the Android application, it fails to open the file as the Android application is run in a different user space and so I am not able to open the file. My question is, is it possible to run the command as a "shell" user or a child of "shell" from the Android application. I want to be able to do this without rooting the device so su is out of question.
You can't change the user ID of your app without a rooted device. If you could, the security model wouldn't be very useful. If your app needs access to the file, you will need to grant appropriate permissions.
The other common workaround is to have a service, running as the "shell" user, whose job is to open the file and hand back a file descriptor. The tricky part is that you need a way to launch that service as the "shell" user, which brings us back to needing "su".
FWIW, the situation is the same whether you're coding in Java or C++.
As a part of a research project, I'm doing method profiling work on a bunch of android applications identified as having malicious code in them. To automate the process, I've made a batch file and a java executable to decode, add the android:debuggable flag, rebuild, sign, and install the application to a test device. The next step would be launching the application.
Is there a way to do this from adb without knowing the intent, package, or activity of the application in question? I'm aware of the adb shell's 'am start' command, but this requires the package and the activity to start at the least, if I recall correctly.
Is there a way to start the application without this information? Or failing that, another method to get the package and activity and then use that in my batch file?
It would be much easier to run those on a device where adb runs as root (or the emulator), then you can attach without having to modify. If you are parsing the APKs, you might as well parse the manifest and build a list of packages, intents, etc. And of course, there is not 'launching the application' in Android -- you may start an activity or service, not necessarily the main/root activity. Especially for (suspected) malware, which may well be trying to hide its main activity behind an benign entry one.
I did tried some sample code for service and installed the same on device and started the app. I tried to list this service with adb command, which was running now (could see my logs for the same in logcat)
adb shell service list
But, above command lists only system service and could not see my service listed there.
So, my question,
1. Does all services need to register for service manager or it is only the "system services"
2. If i have aidl implemented for this same service will it behave like system service (i mean, get displayed for command like "adb shell service list")
thanks for reading and appreciate any response for my question
-regards,
Manju
Android system services are different from the sort of services you create within an app. You don't need to worry that they don't appear within adb shell service list.
System services are provided in the ROM of the phone and are core parts of the Android OS, for example the "surface flinger" (graphics compositor), "package manager" etc. They are accessed within the Android frameworks so that a typical Android application developer never contacts them directly. They are looked up based on a simple string.
Normal services inherit from the Service class and you connect to them from within application code using an Intent and typically Context.bindService.
I am trying to run a shell script, which copies a file to a specific location, on phone power up and I added the following to my init.rc file:
service test_copy /system/bin/sh /system/bin/test_copy.sh
class pre-zygote_services
oneshot
user system
group system
When the service name (test_copy) is same as the script name, test_copy in this case, it doesn't execute the script but if I change the service name to a different one, like start_test_copy, it works. I am just eager to know the reason on why when service name is same as script name it doesn't work or am I wrong?
Try this one in your init.rc:
service test_copy /system/bin/test_copy.sh
user root
oneshot
Your test_copy.sh script must begin with:
#!/system/bin/sh
Always make sure use a different name for your service which Android init recommends.
Also, you can make your test_copy.sh into the executable by defining the Android Make file.