Linaro / meta-qcom

OpenEmbedded/Yocto Project BSP layer for Qualcomm based platforms
MIT License
61 stars 70 forks source link

android-gadget-setup: fix conditional argument to assign serial variable #553

Closed chirag-jn closed 9 months ago

chirag-jn commented 9 months ago

Change the conditional argument to check if androidserial is non-empty.

Initially we observed the following logs on boot-up as well as on running systemctl restart android-tools-adbd

Boot-up logs:

Apr 28 17:42:28 qcm6490 systemd[1]: Starting Android Debug Bridge...
Apr 28 17:42:28 qcm6490 systemd[1]: android-tools-adbd.service: Control process exited, code=exited, status=1/FAILURE
Apr 28 17:42:28 qcm6490 android-gadget-cleanup[518]: /usr/bin/android-gadget-cleanup: cd: line 7: can't cd to adb: No such file or directory
Apr 28 17:42:28 qcm6490 android-gadget-cleanup[518]: /usr/bin/android-gadget-cleanup: line 9: can't create UDC: Permission denied
Apr 28 17:42:28 qcm6490 android-gadget-cleanup[520]: killall: adbd: no process killed
Apr 28 17:42:28 qcm6490 android-gadget-cleanup[521]: umount: /dev/usb-ffs/adb: no mount point specified.
Apr 28 17:42:28 qcm6490 android-gadget-cleanup[522]: rm: cannot remove 'configs/c.1/ffs.usb0': No such file or directory
Apr 28 17:42:28 qcm6490 android-gadget-cleanup[524]: rmdir: failed to remove 'configs/c.1/strings/0x409': No such file or directory
Apr 28 17:42:28 qcm6490 android-gadget-cleanup[525]: rmdir: failed to remove 'configs/c.1': No such file or directory
Apr 28 17:42:28 qcm6490 android-gadget-cleanup[526]: rmdir: failed to remove 'functions/ffs.usb0': No such file or directory

Logs of systemctl restart android-tools-adbd

...
Apr 28 19:04:04 qcm6490 systemd[1]: Starting Android Debug Bridge...
...
Apr 28 19:04:04 qcm6490 android-gadget-cleanup[933]: killall: adbd: no process killed
Apr 28 19:04:04 qcm6490 android-gadget-cleanup[934]: umount: /dev/usb-ffs/adb: not mounted.
Apr 28 19:04:04 qcm6490 android-gadget-cleanup[936]: rm: cannot remove 'configs/c.1/ffs.usb0': No such file or directory
Apr 28 19:04:04 qcm6490 android-gadget-cleanup[937]: rmdir: failed to remove 'configs/c.1/strings/0x409': No such file or directory
Apr 28 19:04:04 qcm6490 android-gadget-cleanup[938]: rmdir: failed to remove 'configs/c.1': No such file or directory
Apr 28 19:04:04 qcm6490 android-gadget-cleanup[939]: rmdir: failed to remove 'functions/ffs.usb0': No such file or dirory
Apr 28 19:04:04 qcm6490 android-gadget-cleanup[940]: rmdir: failed to remove 'strings/0x409': No such file or directory
Apr 28 19:04:04 qcm6490 android-gadget-cleanup[941]: rmdir: failed to remove 'adb': No such file or directory
Apr 28 19:04:04 qcm6490 systemd[1]: android-tools-adbd.service: Control process exited, code=exited, status=1/FAILURE
Apr 28 19:04:04 qcm6490 systemd[1]: android-tools-adbd.service: Failed with result 'exit-code'.
...
Apr 28 19:04:04 qcm6490 systemd[1]: Stopped Android Debug Bridge.
...
(repeated 5 times)

To analyze any issue in the script, we ran $ sh -x /usr/bin/android-gadget-setup, and got the following

+ set -e
+ manufacturer=RPB
+ model='Android device'
+ serial=0123456789ABCDEF
+ '[' -r /etc/android-gadget-setup.machine ]
+ . /etc/android-gadget-setup.machine
+ manufacturer=Qualcomm
+ hostname
+ model=qcm6490
+ sed -n -e '/androidboot.serialno/  s/.*androidboot.serialno=\([^ ]*\).*/\1/gp ' /proc/cmdline
+ androidserial=
+ '[' -n  ]

Script abruptly stopped at '[' -n ], which is a syntax error. Script is running under set -e so any non-zero exit code in a command will abort the script. To fix the issue, we made the patch as provided in the commit.

After the patch, we saw the following output of $ sh -x /usr/bin/android-gadget-setup

+ set -e
+ manufacturer=RPB
+ model='Android device'
+ serial=0123456789ABCDEF
+ '[' -r /etc/android-gadget-setup.machine ]
+ . /etc/android-gadget-setup.machine
+ manufacturer=Qualcomm
+ hostname
+ model=qcm6490
+ sed -n -e '/androidboot.serialno/  s/.*androidboot.serialno=\([^ ]*\).*/\1/gp ' /proc/cmdline
+ androidserial=
+ '[' -n  ]
+ '[' -d /sys/kernel/config/usb_gadget ]
...

We can see that the script progressed past the stage of [ -n ], confirming that the patch works. Further, android-tools-adbd started working and we could see the device visible in adb devices on host machine.

chirag-jn commented 9 months ago

Commit message is pretty hard to follow. Also [ -n ] is not a syntax error, otherwise sh would have complained.

Updated the commit message to make it more readable. I added the logs in the PR to elaborate the issue faced. Further, In the case of [ -n ], as visible in the output of sh -x, the output isn't coming as expected. Also, using if-else makes the code more readable and understandable.

lumag commented 9 months ago

It still talks about the syntax error. [ condition ] && do_something is a perfectly fine from the syntax point of view. I think just adding true or exit 0 to the end of the setup script will fix your issue.

chirag-jn commented 9 months ago

It still talks about the syntax error. [ condition ] && do_something is a perfectly fine from the syntax point of view. I think just adding true or exit 0 to the end of the setup script will fix your issue.

I’ll try this and get back!

lumag commented 9 months ago

Also, I'm not sure how does that fix the issue for the serial number being not available in the /proc/cmdline

chirag-jn commented 9 months ago

Also, I'm not sure how does that fix the issue for the serial number being not available in the /proc/cmdline

if serial number is not available in /proc/cmdline, then default value will be picked up. Default value is defined in /usr/bin/android-gadget-setup

lumag commented 9 months ago

Ok, so we should be back to #547 to get the proper serial number even if we fix this script.

chirag-jn commented 9 months ago

Ok, so we should be back to #547 to get the proper serial number even if we fix this script.

Yeah we are working on #547 to fix reading serial number. Meanwhile, we can proceed with this PR.

chirag-jn commented 9 months ago

It still talks about the syntax error. [ condition ] && do_something is a perfectly fine from the syntax point of view. I think just adding true or exit 0 to the end of the setup script will fix your issue.

Followed this. Can you check?

lumag commented 9 months ago

It still talks about the syntax error. [ condition ] && do_something is a perfectly fine from the syntax point of view. I think just adding true or exit 0 to the end of the setup script will fix your issue.

Followed this. Can you check?

There is still a talk about the syntax error in the commit message. Also could you please check whether a single exit 0 helps? Because I think the issue is that the script returns 1 to systemd, nothing else.

chirag-jn commented 9 months ago

exit 0 did not help. It can be tested locally too.

set 1

$ cat file1
set -e

val="someval"

if [ -r ./file2 ] ; then
        . ./file2
fi

echo $val
$ cat file2
a=""
[ -n "$a" ] && val="$a"
exit 0
$ sh -x ./file1
+ set -e
+ val=someval
+ '[' -r ./file2 ']'
+ . ./file2
++ a=
++ '[' -n '' ']'
++ exit 0

set 2

$ cat file2
a=""
[ -n "$a" ] && val="$a" || true
$ sh -x ./file1
+ set -e
+ val=someval
+ '[' -r ./file2 ']'
+ . ./file2
++ a=
++ '[' -n '' ']'
++ true
+ echo someval
someval
chirag-jn commented 9 months ago

It still talks about the syntax error. [ condition ] && do_something is a perfectly fine from the syntax point of view. I think just adding true or exit 0 to the end of the setup script will fix your issue.

Followed this. Can you check?

There is still a talk about the syntax error in the commit message. Also could you please check whether a single exit 0 helps? Because I think the issue is that the script returns 1 to systemd, nothing else.

My mistake. Updated the commit message.

chirag-jn commented 9 months ago

set 3

$ cat file2
a="newval"
[ -n "$a" ] && val="$a" || true
$ sh -x ./file1
+ set -e
+ val=someval
+ '[' -r ./file2 ']'
+ . ./file2
++ a=newval
++ '[' -n newval ']'
++ val=newval
+ echo newval
newval
lumag commented 9 months ago

[ -n ] used is an incomplete conditional test. In bash and similar shells, [ -n ] without a string following -n is considered incomplete.

This is still not applicable to the source code in question. There is a string argument after -n, double quotes ensure that enven if the environment variable is empty, there will be an empty string argument:

[ -n "$androidserial" ] 
lumag commented 9 months ago

Ack, I checked, this works:

lumag@eriador:/tmp$ cat test.sh 
#!/bin/sh

set -e

if [ -r test2.sh ] ; then
    . ./test2.sh
fi

echo continued
lumag@eriador:/tmp$ cat test2.sh 
[ -n "$EMPTY" ] && echo not-empty
true
lumag@eriador:/tmp$ sh test.sh
continued
lumag@eriador:/tmp$ EMPTY=not sh test.sh
not-empty
continued
chirag-jn commented 9 months ago

Ack, I checked, this works:

lumag@eriador:/tmp$ cat test.sh 
#!/bin/sh

set -e

if [ -r test2.sh ] ; then
  . ./test2.sh
fi

echo continued
lumag@eriador:/tmp$ cat test2.sh 
[ -n "$EMPTY" ] && echo not-empty
true
lumag@eriador:/tmp$ sh test.sh
continued
lumag@eriador:/tmp$ EMPTY=not sh test.sh
not-empty
continued

yeah exit 0 as the last command doesn't work but true as the last command works. Do I still have to make a change or the current commit works fine?

lumag commented 9 months ago

Yes, please.

lumag commented 9 months ago

And fix the commit message too.

chirag-jn commented 9 months ago

And fix the commit message too.

fixed the code as well as the commit message. Thanks @lumag !

chirag-jn commented 9 months ago

@lumag so is it complete or anything else needed?