Genymobile / gnirehtet

Gnirehtet provides reverse tethering for Android
Apache License 2.0
6.46k stars 594 forks source link

`autorun` doesn't work when it is brought up by launchd #168

Open ohenak opened 5 years ago

ohenak commented 5 years ago

On Mac OS Mojave 10.14.2, using gnirehtet v2.3

I tried to bring up gnirehtet autorun using launchd, when I plug in a new phone it is not able to detect it, it only shows the following line in the log,

2019-01-25 14:11:20.695 I Gnirehtet: Checking gnirehtet client...

but not the log lines,

2019-01-25 14:11:20.873 I Gnirehtet: Starting client...
Broadcasting: Intent { act=com.genymobile.gnirehtet.START flg=0x400000 cmp=com.genymobile.gnirehtet/.GnirehtetControlReceiver }
Broadcast completed: result=0

But if I run gnirehtet start manually it is working fine. Any idea how to troubleshoot this issue?

Below is my plist config:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
  <dict>
    <key>Label</key>
    <string>com.genymobile.gnirehtet.relay</string>
    <key>ProgramArguments</key>
    <array>
      <string>java</string>
      <string>-jar</string>
      <string>/Users/<>/dltools/gnirehtet-java/gnirehtet.jar</string>
      <string>autorun</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
    <key>KeepAlive</key>
    <true/>
    <key>StandardErrorPath</key>
    <string>/tmp/com.genymobile.gnirehtet.relay.err</string>
    <key>StandardOutPath</key>
    <string>/tmp/com.genymobile.gnirehtet.relay.out</string>
  </dict>
</plist>
ohenak commented 5 years ago

Forgot to mention, I am using Java version.

rom1v commented 5 years ago

But if I run gnirehtet start manually it is working fine. Any idea how to troubleshoot this issue?

What about gnirehtet autorun from the command line?

ohenak commented 5 years ago

Yes, gnirehtet autorun works fine from command line, just not when using launchd

ohenak commented 5 years ago

Is there a way I can turn on debug log? Or I have to recompile the java code?

ohenak commented 5 years ago

Ok, I found the problem, adb shell dumpsys package com.genymobile.gnirehtet was not returning anything. This issue is fixed after I added PATH into EnvironmentVariables in plist file, i.e.:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
  <dict>
    <key>Label</key>
    <string>com.genymobile.gnirehtet.relay</string>
    <key>ProgramArguments</key>
    <array>
      <string>java</string>
      <string>-jar</string>
      <string>/Users/kaneho/dltools/gnirehtet-java/gnirehtet.jar</string>
      <string>autorun</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
    <key>KeepAlive</key>
    <true/>
    <key>StandardErrorPath</key>
    <string>/tmp/com.genymobile.gnirehtet.relay.err</string>
    <key>StandardOutPath</key>
    <string>/tmp/com.genymobile.gnirehtet.relay.out</string>
    <key>EnvironmentVariables</key>
    <dict>
      <key>PATH</key>
      <string>/bin:/usr/bin:/usr/local/bin:/Users/kaneho/Library/Android/sdk/platform-tools</string>
    </dict>
  </dict>
</plist>

But now I am wondering if adb is not found in launchd session, why doesn't it throw any error but remain silence instead? Is this by design?

rom1v commented 5 years ago

But now I am wondering if adb is not found in launchd session, why doesn't it throw any error but remain silence instead?

The error should be printed on stderr.

I just tested the Rust version when adb is not in PATH:

$ ./gnirehtet autorun
2019-01-26 09:23:16.774 INFO Main: Starting relay server...
2019-01-26 09:23:16.774 INFO Relay: Relay server started
2019-01-26 09:23:44.041 INFO Main: Checking gnirehtet client...
2019-01-26 09:23:44.041 ERROR Main: Cannot start client: Command adb ["-s", "05f5e60a0ae518e5", "shell", "dumpsys", "package", "com.genymobile.gnirehtet"] failed: No such file or directory (os error 2)

and the Java version:

$ ./gnirehtet autorun
2019-01-26 09:24:01.800 I Gnirehtet: Starting relay server...
2019-01-26 09:24:01.800 I Gnirehtet: Checking gnirehtet client...
2019-01-26 09:24:01.806 I Relay: Relay server started
2019-01-26 09:24:01.807 E Gnirehtet: Cannot start client
java.io.IOException: Cannot run program "adb": error=2, Aucun fichier ou dossier de ce type
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)
    at com.genymobile.gnirehtet.Main.mustInstallClient(Main.java:344)
    at com.genymobile.gnirehtet.Main.cmdStart(Main.java:250)
    at com.genymobile.gnirehtet.Main.lambda$asyncStart$3(Main.java:302)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.io.IOException: error=2, Aucun fichier ou dossier de ce type
    at java.lang.UNIXProcess.forkAndExec(Native Method)
    at java.lang.UNIXProcess.<init>(UNIXProcess.java:247)
    at java.lang.ProcessImpl.start(ProcessImpl.java:134)
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029)
    ... 4 more

Is your /tmp/com.genymobile.gnirehtet.relay.err empty?