Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ACTION_SERVICE_STOP intent to only stop a single AppShell #3821

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

lvogt
Copy link

@lvogt lvogt commented Feb 4, 2024

This adds the possibility to basically stop a running AppShell that was created by sending a ACTION_SERVICE_EXECUTE intent.

This is used by my ongoing implementation of a cron-like service into TermuxAPI (PR: termux/termux-api#657)


Uri executableUri = intent.getData();
String executable = UriUtils.getUriFilePathWithFragment(executableUri);
String shellName = ShellUtils.getExecutableBasename(executable);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There would be conflicts if same executable was run from someplace else, and that function only returns first one. You should generate at least a six character alphanumeric [a-zA-Z0-9] random string and store that in cron tab in addition to the id and when sending execute or stop intent, pass that as TERMUX_SERVICE.EXTRA_SHELL_NAME, so that the executable for the job is killed, and not any other.

d287734

#3709

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point.
Is there a character limit on the shell name?
Or the other way around: Could I just use a UUID?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no limit currently, but there should be, ideally 255, equal to NAME_NAME.

UUID would be too long and harder to see in app shells UI list and logs, just use a six character alphanumeric [a-zA-Z0-9] string, like mktemp template uses for unique files. Following should work, make sure <script_name>_<public_id>_<private_id> doesn't already exist in cron tab, otherwise regenerate private_id. The public_id here is the incrementing number you are currently using.

char[] allowedCharsArray = ("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789").toCharArray();
char[] private_id = new char[6];
Random random = new SecureRandom();
for (int i = 0; i < private_id.length; i++) {
    private_id[i] = allowedCharsArray[random.nextInt(allowedCharsArray.length)];
}

https://man7.org/linux/man-pages/man3/mktemp.3.html

https://bugs.python.org/issue12015

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. Thanks for the example.

I changed the method to only work if the shell name is explicitly set.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Welcome. Makes sense. I think it would be better to call getTermuxTaskForShellName() in a while loop until it starts returning null, so that all shells with same name get killed, since currently only first one would get killed, which may not be the one caller intended to kill. To prevent duplicates, callers should start shells with a unique shell name, like the cron API would do.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point - I had not considered multiple shells with the same name.

@@ -1046,6 +1049,9 @@ public static final class TERMUX_SERVICE {
* be created in {@link #EXTRA_RESULT_DIRECTORY} if {@link #EXTRA_RESULT_SINGLE_FILE} is
* {@code false} for the TERMUX_SERVICE.ACTION_SERVICE_EXECUTE intent */
public static final String EXTRA_RESULT_FILES_SUFFIX = TERMUX_PACKAGE_NAME + ".execute.result_files_suffix"; // Default: "com.termux.execute.result_files_suffix"
/** Intent {@code long} extra for graceperiod between SIGTERM and SIGKILL
* for the TERMUX_SERVICE.ACTION_SERVICE_STOP intent */
public static final String EXTRA_TERMINATE_GRACE_PERIOD = TERMUX_PACKAGE_NAME + ".execute.stop.delay"; // Default: "com.termux.execute.stop.delay"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be stop_delay

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Applied

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The extra name was also different. Both should be same with respective case, naming them sigkill_delay_on_stop makes it more clear on function of extra.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will have to rename gracePeriod and gracePeriodMsec as well

* fix stop_delay constant
* terminate all appshells with matching name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants