__system_property_get()

2,892 views
Skip to first unread message

John Dallman

unread,
Nov 1, 2018, 2:17:10 PM11/1/18
to andro...@googlegroups.com
I need to get some kind of unique identifier for each of the Android devices in my testing setup from native code. I need to be able to do this on API 21 or later, on 64-bit ARM. The [ro.serialno] property will do nicely, since that's what "adb devices" displays. 

From a bit of searching, the recommended way to get system properties from native code seems to be to use the functions in sys/system_properties.h.
__system_property_find() will find a property, and get a prop_info* for it, but I'm floundering on how to get the string from that. The __system_property_read_callback function only appears at API 26. The obvious alternative is to use the deprecated __system_property_get(), but is there a better way? 
Thanks,
John 

Dan Albert

unread,
Nov 1, 2018, 7:33:08 PM11/1/18
to andro...@googlegroups.com
__system_property_get should be fine for serial numbers. The new APIs exist to work around the fact that __system_property_get could only read values up to a fixed size (and it wasn't particularly large), but that's very unlikely to be a problem for a serial number. On the off chance it is, you can safely assume that they won't be until those APIs are available, so you can always dlsym __system_property_read_callback, use it if it's available, fallback if it isn't.

That's only part of the question though. Newer versions of Android (O? N? I don't remember) restrict access to various system properties with selinux, and it seems the serial number is one of them (it is PII, after all). I think you need an app permission to access it from the Java side too.

The way my test runner handles getting serial numbers for attached devices is just with `adb devices`. That works for me because the device side of the tests doesn't need the information, it's just for orchestration on the host side. Not sure if that's any help for your use case.

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-ndk...@googlegroups.com.
To post to this group, send email to andro...@googlegroups.com.
Visit this group at https://groups.google.com/group/android-ndk.
To view this discussion on the web visit https://groups.google.com/d/msgid/android-ndk/CAH1xqgkbiPmjhhNUshfpmnEO%2Bq4w9AYHcoGa340EGzzfZ_OMYg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

John Dallman

unread,
Nov 2, 2018, 6:07:45 AM11/2/18
to andro...@googlegroups.com
> __system_property_get should be fine for serial numbers. The new APIs 
> exist to work around the fact that __system_property_get could only read 
> values up to a fixed size (and it wasn't particularly large), but that's very 
> unlikely to be a problem for a serial number.  

The longest serial-number device I have is <20 characters, so the 32-character limit looks OK. 

> Newer versions of Android (O? N? I don't remember) restrict access to various 
> system properties with selinux, and it seems the serial number is one of them  

I could cope with that if there's a way to get access without rooting the device (which I'm not forbidden, but I'd much rather avoid). But I know very little about selinux: can you point me to a source? 

> ... device side of the tests doesn't need the information, it's just for orchestration on the 
> host side. Not sure if that's any help for your use case.  

The output of my tests is a big plain-text file, entirely generated by my on-device test harness, and having the device serial number in that, obtained by the test harness, reduces the possibility for confusion. 

Thanks very much, 

John 

John Dallman

unread,
Nov 2, 2018, 11:36:24 AM11/2/18
to andro...@googlegroups.com

John Dallman

unread,
Nov 2, 2018, 11:52:20 AM11/2/18
to andro...@googlegroups.com
> The longest serial-number device I have is <20 characters, so the 32-character limit looks OK.   

No, that's a 92-character limit, isn't it? 

>> Newer versions of Android (O? N? I don't remember) restrict access to various 
>> system properties with selinux, and it seems the serial number is one of them  

Android O, it seems. 

> The output of my tests is a big plain-text file, entirely generated by my on-device test 
> harness, and having the device serial number in that, obtained by the test harness, 
> reduces the possibility for confusion.   

Well, it looks as if it will stop working when I move up to O. 

Oh, well. 

John

Alex Cohn

unread,
Nov 5, 2018, 7:02:45 AM11/5/18
to android-ndk
> The output of my tests is a big plain-text file, entirely generated by my on-device test 
> harness, and having the device serial number in that, obtained by the test harness, 
> reduces the possibility for confusion.   

Well, it looks as if it will stop working when I move up to O. 

An easy batch script will run adb, fetch the number, and push it as a text file to /data/local/tmp. Now your test will easily know the ID.

BR,
Alex

John Dallman

unread,
Nov 6, 2018, 4:43:57 AM11/6/18
to andro...@googlegroups.com
On Mon, Nov 5, 2018 at 12:02 PM Alex Cohn <sasha...@gmail.com> wrote
An easy batch script will run adb, fetch the number, and push it as a text file to /data/local/tmp.
Now your test will easily know the ID.

I've done a bit of a variant on that, but it works fine. 

Thanks,

John
Reply all
Reply to author
Forward
0 new messages