Skip to content

Commit

Permalink
Added: Store pid in ExecutionCommand for sessions and tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
agnostic-apollo committed Jan 22, 2022
1 parent bf10c72 commit b45ff8a
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 12 deletions.
23 changes: 17 additions & 6 deletions app/src/main/java/com/termux/app/TermuxService.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,15 @@
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.net.Uri;
import android.net.wifi.WifiManager;
import android.os.Binder;
import android.os.Build;
import android.os.Handler;
import android.os.IBinder;
import android.os.PowerManager;
import android.provider.Settings;
import android.widget.ArrayAdapter;

import androidx.annotation.Nullable;
Expand Down Expand Up @@ -780,12 +777,12 @@ private synchronized void updateNotification() {



private void setCurrentStoredTerminalSession(TerminalSession session) {
if (session == null) return;
private void setCurrentStoredTerminalSession(TerminalSession terminalSession) {
if (terminalSession == null) return;
// Make the newly created session the current one to be displayed
TermuxAppSharedPreferences preferences = TermuxAppSharedPreferences.build(this);
if (preferences == null) return;
preferences.setCurrentSession(session.mHandle);
preferences.setCurrentSession(terminalSession.mHandle);
}

public synchronized boolean isTermuxSessionsEmpty() {
Expand All @@ -808,11 +805,25 @@ public synchronized TermuxSession getTermuxSession(int index) {
return null;
}

@Nullable
public synchronized TermuxSession getTermuxSessionForTerminalSession(TerminalSession terminalSession) {
if (terminalSession == null) return null;

for (int i = 0; i < mTermuxSessions.size(); i++) {
if (mTermuxSessions.get(i).getTerminalSession().equals(terminalSession))
return mTermuxSessions.get(i);
}

return null;
}

public synchronized TermuxSession getLastTermuxSession() {
return mTermuxSessions.isEmpty() ? null : mTermuxSessions.get(mTermuxSessions.size() - 1);
}

public synchronized int getIndexOfSession(TerminalSession terminalSession) {
if (terminalSession == null) return -1;

for (int i = 0; i < mTermuxSessions.size(); i++) {
if (mTermuxSessions.get(i).getTerminalSession().equals(terminalSession))
return i;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,17 @@ public void onTerminalCursorStateChange(boolean enabled) {
mActivity.getTerminalView().setTerminalCursorBlinkerState(enabled, false);
}

@Override
public void setTerminalShellPid(@NonNull TerminalSession terminalSession, int pid) {
TermuxService service = mActivity.getTermuxService();
if (service == null) return;

TermuxSession termuxSession = service.getTermuxSessionForTerminalSession(terminalSession);
if (termuxSession != null)
termuxSession.getExecutionCommand().mPid = pid;
}


/**
* Should be called when mActivity.onResetTerminalSession() is called
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ public void initializeEmulator(int columns, int rows) {
int[] processId = new int[1];
mTerminalFileDescriptor = JNI.createSubprocess(mShellPath, mCwd, mArgs, mEnv, processId, rows, columns);
mShellPid = processId[0];
mClient.setTerminalShellPid(this, mShellPid);

final FileDescriptor terminalFileDescriptorWrapped = wrapFileDescriptor(mTerminalFileDescriptor, mClient);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public interface TerminalSessionClient {

void onTerminalCursorStateChange(boolean state);

void setTerminalShellPid(@NonNull TerminalSession session, int pid);



Integer getTerminalCursorStyle();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ public int getValue() {
/** The optional unique id for the {@link ExecutionCommand}. */
public Integer id;

/** The process id of command. */
public int mPid = -1;

/** The current state of the {@link ExecutionCommand}. */
private ExecutionState currentState = ExecutionState.PRE_EXECUTION;
Expand Down Expand Up @@ -266,6 +268,9 @@ public static String getExecutionInputLogString(final ExecutionCommand execution

logString.append(executionCommand.getCommandIdAndLabelLogString()).append(":");

if (executionCommand.mPid != -1)
logString.append("\n").append(executionCommand.getPidLogString());

if (executionCommand.previousState != ExecutionState.PRE_EXECUTION)
logString.append("\n").append(executionCommand.getPreviousStateLogString());
logString.append("\n").append(executionCommand.getCurrentStateLogString());
Expand Down Expand Up @@ -358,6 +363,8 @@ public static String getExecutionCommandMarkdownString(final ExecutionCommand ex

markdownString.append("## ").append(executionCommand.commandLabel).append("\n");

if (executionCommand.mPid != -1)
markdownString.append("\n").append(MarkdownUtils.getSingleLineMarkdownStringEntry("Pid", executionCommand.mPid, "-"));

markdownString.append("\n").append(MarkdownUtils.getSingleLineMarkdownStringEntry("Previous State", executionCommand.previousState.getName(), "-"));
markdownString.append("\n").append(MarkdownUtils.getSingleLineMarkdownStringEntry("Current State", executionCommand.currentState.getName(), "-"));
Expand Down Expand Up @@ -408,6 +415,10 @@ public String getIdLogString() {
return "";
}

public String getPidLogString() {
return "Pid: `" + mPid + "`";
}

public String getCurrentStateLogString() {
return "Current State: `" + currentState.getName() + "`";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,16 +137,16 @@ public void run() {
* @param context The {@link Context} for operations.
*/
private void executeInner(@NonNull final Context context) throws IllegalThreadStateException, InterruptedException {
final int pid = ShellUtils.getPid(mProcess);
mExecutionCommand.mPid = ShellUtils.getPid(mProcess);

Logger.logDebug(LOG_TAG, "Running \"" + mExecutionCommand.getCommandIdAndLabelLogString() + "\" AppShell with pid " + pid);
Logger.logDebug(LOG_TAG, "Running \"" + mExecutionCommand.getCommandIdAndLabelLogString() + "\" AppShell with pid " + mExecutionCommand.mPid);

mExecutionCommand.resultData.exitCode = null;

// setup stdin, and stdout and stderr gobblers
DataOutputStream STDIN = new DataOutputStream(mProcess.getOutputStream());
StreamGobbler STDOUT = new StreamGobbler(pid + "-stdout", mProcess.getInputStream(), mExecutionCommand.resultData.stdout, mExecutionCommand.backgroundCustomLogLevel);
StreamGobbler STDERR = new StreamGobbler(pid + "-stderr", mProcess.getErrorStream(), mExecutionCommand.resultData.stderr, mExecutionCommand.backgroundCustomLogLevel);
StreamGobbler STDOUT = new StreamGobbler(mExecutionCommand.mPid + "-stdout", mProcess.getInputStream(), mExecutionCommand.resultData.stdout, mExecutionCommand.backgroundCustomLogLevel);
StreamGobbler STDERR = new StreamGobbler(mExecutionCommand.mPid + "-stderr", mProcess.getErrorStream(), mExecutionCommand.resultData.stderr, mExecutionCommand.backgroundCustomLogLevel);

// start gobbling
STDOUT.start();
Expand Down Expand Up @@ -196,9 +196,9 @@ private void executeInner(@NonNull final Context context) throws IllegalThreadSt

// Process result
if (exitCode == 0)
Logger.logDebug(LOG_TAG, "The \"" + mExecutionCommand.getCommandIdAndLabelLogString() + "\" AppShell with pid " + pid + " exited normally");
Logger.logDebug(LOG_TAG, "The \"" + mExecutionCommand.getCommandIdAndLabelLogString() + "\" AppShell with pid " + mExecutionCommand.mPid + " exited normally");
else
Logger.logDebug(LOG_TAG, "The \"" + mExecutionCommand.getCommandIdAndLabelLogString() + "\" AppShell with pid " + pid + " exited with code: " + exitCode);
Logger.logDebug(LOG_TAG, "The \"" + mExecutionCommand.getCommandIdAndLabelLogString() + "\" AppShell with pid " + mExecutionCommand.mPid + " exited with code: " + exitCode);

// If the execution command has already failed, like SIGKILL was sent, then don't continue
if (mExecutionCommand.isStateFailed()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ public void onColorsChanged(@NonNull TerminalSession changedSession) {
public void onTerminalCursorStateChange(boolean state) {
}

@Override
public void setTerminalShellPid(@NonNull TerminalSession session, int pid) {
}


@Override
Expand Down

0 comments on commit b45ff8a

Please sign in to comment.