Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion log/src/main/java/org/apache/karaf/log/command/LogTail.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.apache.karaf.shell.api.action.Command;
import org.apache.karaf.shell.api.action.lifecycle.Reference;
import org.apache.karaf.shell.api.action.lifecycle.Service;
import org.apache.karaf.shell.api.console.ChannelResourceCleaner;
import org.apache.karaf.shell.api.console.Session;
import org.ops4j.pax.logging.spi.PaxAppender;
import org.osgi.framework.BundleContext;
Expand All @@ -31,7 +32,8 @@

@Command(scope = "log", name = "tail", description = "Continuously display log entries. Use ctrl-c to quit this command")
@Service
public class LogTail extends DisplayLog {
public class LogTail extends DisplayLog implements ChannelResourceCleaner {

@Reference
Session session;

Expand All @@ -52,6 +54,9 @@ public Object execute() throws Exception {
PaxAppender appender = event -> printEvent(out, event, minLevel);
ServiceTracker<LogService, LogService> tracker = new LogServiceTracker(context, LogService.class, null, appender);
tracker.open();

context.registerService(ChannelResourceCleaner.class, this, null);

try {
synchronized (this) {
wait();
Expand All @@ -65,6 +70,12 @@ public Object execute() throws Exception {
out.println();
return null;
}

@Override
public void close() {
System.out.println("STOP TAIL");
stopTail();
}

private synchronized void stopTail() {
notifyAll();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.karaf.shell.api.console;

public interface ChannelResourceCleaner {

void close();

}
19 changes: 19 additions & 0 deletions shell/ssh/src/main/java/org/apache/karaf/shell/ssh/Activator.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.Duration;
import java.util.Collection;
import java.util.Collections;

import org.apache.karaf.shell.api.action.lifecycle.Manager;
import org.apache.karaf.shell.api.console.ChannelResourceCleaner;
import org.apache.karaf.shell.api.console.CommandLoggingFilter;
import org.apache.karaf.shell.api.console.Session;
import org.apache.karaf.shell.api.console.SessionFactory;
Expand All @@ -34,8 +36,11 @@
import org.apache.karaf.util.tracker.annotation.Managed;
import org.apache.karaf.util.tracker.annotation.RequireService;
import org.apache.karaf.util.tracker.annotation.Services;
import org.apache.sshd.common.channel.Channel;
import org.apache.sshd.common.channel.ChannelListener;
import org.apache.sshd.common.file.virtualfs.VirtualFileSystemFactory;
import org.apache.sshd.common.keyprovider.KeyPairProvider;
import org.apache.sshd.common.session.SessionListener;
import org.apache.sshd.core.CoreModuleProperties;
import org.apache.sshd.scp.server.ScpCommandFactory;
import org.apache.sshd.server.SshServer;
Expand Down Expand Up @@ -189,6 +194,20 @@ protected SshServer createSshServer(SessionFactory sessionFactory) {
server.setKeyExchangeFactories(SshUtils.buildKexAlgorithms(kexAlgorithms));
server.setSignatureFactories(SshUtils.buildSigAlgorithms(sigAlgorithms));
server.setShellFactory(new ShellFactoryImpl(sessionFactory));
server.addSessionListener(new SessionListener() {
@Override
public void sessionDisconnect(org.apache.sshd.common.session.Session session, int reason, String msg, String language, boolean initiator) {
try {
Collection<ServiceReference<ChannelResourceCleaner>> references = bundleContext.getServiceReferences(ChannelResourceCleaner.class, null);
for (ServiceReference<ChannelResourceCleaner> reference : references) {
ChannelResourceCleaner cleaner = bundleContext.getService(reference);
cleaner.close();
Copy link
Contributor

Choose a reason for hiding this comment

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

This appears to call .close() on every reference for every session that closes. Is that the correct association?

Wouldn't we want to close only the instance of the command associated with that session?

Copy link
Member Author

Choose a reason for hiding this comment

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

That's right it's a bit agressive: if we have several ssh clients on the Karaf ssh server, we will close all references whatever is the client.
It's pretty hard to "link" the session with the command (or maybe we can use a session property to identify the correct session).

Copy link
Contributor

Choose a reason for hiding this comment

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

Sounds good, a SSH sessionId stored in the command session state should do it.. then when the close loops, look for the matching one and close only that one.

Also, probably need a test with two sessions running.. first one aborts and second one stays active to confirm only the first one gets .close() called on it.

Copy link
Contributor

Choose a reason for hiding this comment

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

While we're at adding things to the Command/Action session.. this change is needed due to JDK removal of Security Manager: https://issues.apache.org/jira/browse/KARAF-7805

}
} catch (Exception e) {
// no-op
}
}
});

if (sftpEnabled) {
server.setCommandFactory(new ScpCommandFactory.Builder().withDelegate((channel, cmd) -> new ShellCommand(sessionFactory, cmd)).build());
Expand Down