Skip to content
Browse files

move all native vars to a String envp[] to set the exec environment v…

…ariables
  • Loading branch information...
1 parent 62a43ae commit 1dee2b89ccab02a0ff0893a57eb4a8b10af66392 @eighthave eighthave committed Mar 16, 2012
Showing with 89 additions and 106 deletions.
  1. +1 −1 assets/olsrd.conf
  2. +31 −3 assets/run
  3. +0 −39 assets/setup
  4. +0 −2 res/values/strings.xml
  5. +0 −43 src/net/szym/barnacle/BarnacleApp.java
  6. +57 −18 src/net/szym/barnacle/BarnacleService.java
View
2 assets/olsrd.conf
@@ -14,7 +14,7 @@
# Debug level(0-9)
# If set to 0 the daemon runs in the background
-DebugLevel 0
+DebugLevel 2
# Interfaces and their rules
View
34 assets/run
@@ -1,22 +1,50 @@
#!/system/bin/sh
# main runner (process manager) for barnacle, root required
-cd ${brncl_path:-.}
+echo "running $0"
+
+# set defaults
+: ${brncl_lan_gw:="192.168.5.1"}
+: ${brncl_lan_netmask:="255.255.255.0"}
+: ${brncl_if_lan:="`getprop wifi.interface`"}
+: ${brncl_path:=.}
+
+# for init.rc parsing
+: ${brncl_hardware:="`getprop ro.hardware`"}
+
+export brncl_lan_gw
+export brncl_lan_netmask
+export brncl_if_lan
+export brncl_path
+export brncl_hardware
+
+# log environment for debugging
+set > $brncl_path/../app_log/setup_environment.log 2>&1
+
+
+cd $brncl_path
# load the driver
# NOTE: Android's shell is ash and there is no "test" or "["
: ${brncl_lan_script:=wifi}
+echo "./$brncl_lan_script load"
./$brncl_lan_script load
# necessary hack for HTC phones
-type ip >/dev/null 2>&1 && ip route del table gprs default >/dev/null 2>&1
+#type ip >/dev/null 2>&1 && ip route del table gprs default >/dev/null 2>&1
# ifconfig $brncl_if_lan $brncl_lan_gw netmask $brncl_lan_netmask up
+echo "./wifi config"
./wifi config
+# run olsrd
+echo "starting olsrd"
+(./olsrd -f olsrd.conf -i $brncl_if_lan > $brncl_path/../app_log/olsrd.log 2>&1) &
+
# the association loop
+echo "./wifi assoc"
./wifi assoc
# cleanup
+echo "./wifi unload"
./wifi unload
-
View
39 assets/setup
@@ -1,42 +1,3 @@
#!/system/bin/sh
-# an intermediate non-root shell script to configure the environment for ./run
-# TODO: this can probably be better done using ProcessBuilder.environment()
-
-set -e
-
-# configuration parameters: brncl_...
-# if_wan REQUIRED
-# if_lan
-# lan_gw
-# lan_netmask
-# lan_essid
-# lan_bssid
-# lan_channel
-# lan_script
-
-. ./brncl.ini
-
-# set defaults
-: ${brncl_lan_gw:="192.168.5.1"}
-: ${brncl_lan_netmask:="255.255.255.0"}
-: ${brncl_dhcp_dns1:="`getprop net.dns1`"}
-: ${brncl_dhcp_dns2:="`getprop net.dns2`"}
-: ${brncl_if_lan:="`getprop wifi.interface`"}
-: ${brncl_path:="."}
-
-export brncl_if_wan brncl_if_lan
-export brncl_lan_gw brncl_lan_netmask
-export brncl_lan_essid brncl_lan_bssid brncl_lan_wep brncl_lan_channel brncl_lan_script brncl_lan_wext
-export brncl_dhcp_dns1 brncl_dhcp_dns2 brncl_dhcp_leasetime brncl_dhcp_firsthost brncl_dhcp_numhosts
-export brncl_nat_queue brncl_nat_timeout brncl_nat_timeout_tcp brncl_nat_firstport brncl_nat_numports
-export brncl_nat_log brncl_nat_ctrl brncl_nat_preserve
-
-# some su out there always take us to /data/local
-export brncl_path
-
-# for init.rc parsing
-export brncl_hardware="`getprop ro.hardware`"
-
exec su -c ${1:-${brncl_path}/run}
-
View
2 res/values/strings.xml
@@ -43,8 +43,6 @@
<string name="wanerr">Could not find WAN interface</string>
<string name="wanok">Mobile data interface found: </string>
<string name="lanok">Wireless interface found: </string>
- <string name="inierr">Could not prepare .ini</string>
- <string name="iniok">.ini file prepared</string>
<string name="starterr">Could not start the process!</string>
<string name="unpackerr">Could not unpack binaries</string>
<string name="unpackerr1">Could not unpack %s"</string>
View
43 src/net/szym/barnacle/BarnacleApp.java
@@ -280,49 +280,6 @@ void failed(int err) {
}
}
- /** Prepare .ini file from preferences */
- protected boolean prepareIni() {
- StringBuilder sb = new StringBuilder();
- final int[] ids = SettingsActivity.prefids;
- for (int i = 0; i < ids.length; ++i) {
- String k = getString(ids[i]);
- String v = prefs.getString(k, null);
- if (v != null && v.length() != 0) {
- if (ids[i] == R.string.lan_essid) {
- v = '"'+v+'"';
- }
- sb.append("brncl_").append(k).append('=').append(v).append('\n');
- }
- }
- // not included in prefids are checkboxes
- final int[] checks = SettingsActivity.checks;
- for (int i = 0; i < checks.length; ++i) {
- String k = getString(checks[i]);
- if (prefs.getBoolean(k, false))
- sb.append("brncl_").append(k).append("=1\n");
- }
-
- sb.append("brncl_path=").append(getFilesDir()).append('\n');
-
- /*
- if (sb.length() == 0) {
- // wow, no preferences?
- updateToast(getString(R.string.noprefs), false);
- PreferenceManager.setDefaultValues(this, R.xml.preferences, true);
- statusActivity.startActivity(new Intent(this, SettingsActivity.class));
- return false;
- }
- */
- try {
- java.io.OutputStream os = openFileOutput(FILE_INI, MODE_PRIVATE);
- os.write(sb.toString().getBytes());
- os.close();
- return true;
- } catch (java.io.IOException e) {
- return false;
- }
- }
-
/** find default route interface */
protected boolean findIfWan() {
String if_wan = prefs.getString(getString(R.string.if_wan), "");
View
75 src/net/szym/barnacle/BarnacleService.java
@@ -21,19 +21,22 @@
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
+import java.util.Map;
import android.app.Notification;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
+import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.PowerManager;
+import android.preference.PreferenceManager;
import android.util.Log;
/**
@@ -264,7 +267,8 @@ private void handle(Message msg) {
log.clear();
log(false, getString(R.string.starting));
- if (!app.prepareBinaries()) {
+ // TODO make this only overwrite on upgrade to new version
+ if (!NativeHelper.unzipAssets(this)) {
log(true, getString(R.string.unpackerr));
state = STATE_STOPPED;
break;
@@ -276,23 +280,16 @@ private void handle(Message msg) {
Log.e(TAG, String.format("NETSCHANGE: %d %d %s", wifiState, state, process == null ? "null" : "proc"));
if (wifiState == WifiManager.WIFI_STATE_DISABLED) {
// wifi is good (or lost), we can start now...
- // TODO for mesh, we don't check the uplink first, but is it useful somehow else?
- //if ((state == STATE_STARTING) && (process == null) && checkUplink()) {
if ((state == STATE_STARTING) && (process == null)) {
- log(false, getString(R.string.dataready));
- if (!app.findIfWan()) {
- // TODO if WAN found, then setup Hna4 routing
+ if (app.findIfWan()) {
+ // TODO if WAN found with checkUplink(), then setup Hna4 routing
+ log(false, "Found active WAN interface");
+ } else {
///log(true, getString(R.string.wanerr));
//state = STATE_STOPPED;
//break;
log(false, "no active WAN interface found");
}
- if (!app.prepareIni()) {
- log(true, getString(R.string.inierr));
- state = STATE_STOPPED;
- break;
- }
- log(false, getString(R.string.iniok));
if (!startProcess()) {
log(true, getString(R.string.starterr));
state = STATE_STOPPED;
@@ -407,19 +404,61 @@ private boolean checkUplink() {
return (mobileInfo.isConnected() || ((wimaxInfo != null) && wimaxInfo.isConnected()));
}
+ /** Prepare env vars for ./wifi from preferences */
+ protected String[] buildEnvFromPrefs() {
+ ArrayList<String> envlist = new ArrayList<String>();
+
+ // get the existing environment first, since many programs like 'su' require
+ // env vars like LD_LIBRARY_PATH to be set
+ Map<String, String> env = System.getenv();
+ for (String envName : env.keySet()) {
+ envlist.add(envName + "=" + env.get(envName));
+ }
+
+ // initialize default values if not done this in the past
+ PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
+ SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
+
+ final int[] ids = SettingsActivity.prefids;
+ for (int i = 0; i < ids.length; ++i) {
+ String k = getString(ids[i]);
+ String v = prefs.getString(k, null);
+ if (v != null && v.length() != 0) {
+ if (ids[i] == R.string.lan_essid) {
+ v = '"'+v+'"';
+ }
+ envlist.add("brncl_" + k + "=" + v);
+ }
+ }
+ // not included in prefids are checkboxes
+ final int[] checks = SettingsActivity.checks;
+ for (int i = 0; i < checks.length; ++i) {
+ String k = getString(checks[i]);
+ if (prefs.getBoolean(k, false))
+ envlist.add("brncl_" + k + "=1");
+ }
+ envlist.add("brncl_path=" + NativeHelper.app_bin.getAbsolutePath());
+
+ String[] ret = (String[]) envlist.toArray(new String[0]);
+ for (String s : ret) {
+ Log.i(TAG, "env var: " + s);
+ }
+ return ret;
+ }
+
private boolean startProcess() {
- // start the process
+ String cmd = NativeHelper.SETUP;
try {
- ProcessBuilder pb = new ProcessBuilder();
- pb.command("./" + BarnacleApp.FILE_SCRIPT).directory(getFilesDir());
- // TODO: consider putting brncl.ini in pb.environment() instead of using ./setup
- process = pb.start(); //Runtime.getRuntime().exec(cmd);
+ process = Runtime.getRuntime().exec(cmd,
+ buildEnvFromPrefs(), NativeHelper.app_bin);
threads[0] = new Thread(new OutputMonitor(MSG_OUTPUT, process.getInputStream()));
threads[1] = new Thread(new OutputMonitor(MSG_ERROR, process.getErrorStream()));
threads[0].start();
threads[1].start();
+ log(false, "started " + cmd);
+ Log.i(TAG, "started " + cmd);
} catch (Exception e) {
- log(true, String.format(getString(R.string.execerr), BarnacleApp.FILE_SCRIPT));
+ log(true, String.format(getString(R.string.execerr), cmd));
Log.e(TAG, "start failed " + e.toString());
return false;
}

0 comments on commit 1dee2b8

Please sign in to comment.
Something went wrong with that request. Please try again.