Skip to content

Commit

Permalink
Fixes for leaks
Browse files Browse the repository at this point in the history
  • Loading branch information
yury committed Oct 19, 2019
1 parent 11f6d91 commit 37442f2
Show file tree
Hide file tree
Showing 9 changed files with 216 additions and 40 deletions.
4 changes: 4 additions & 0 deletions Blink.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@
D24AFD58222410E700CFD3C1 /* MBProgressHUD.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 0732F10D1D062BF700AB5438 /* MBProgressHUD.framework */; };
D24AFD59222410E700CFD3C1 /* MBProgressHUD.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 0732F10D1D062BF700AB5438 /* MBProgressHUD.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
D2512AC520BD4C3000A80257 /* curl_ios_static.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D2E4F92E20B2BB4500B30F7B /* curl_ios_static.framework */; };
D25D58102358897B00D1BCAE /* Completions.swift in Sources */ = {isa = PBXBuildFile; fileRef = D25D580F2358897B00D1BCAE /* Completions.swift */; };
D263489820BEBFE000A8E89F /* libsshd.a in Frameworks */ = {isa = PBXBuildFile; fileRef = D263489720BEBFE000A8E89F /* libsshd.a */; };
D265FBBD2317DD3C0017EAC4 /* BlinkTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D265FBBC2317DD3C0017EAC4 /* BlinkTests.swift */; };
D265FBC52317E5090017EAC4 /* SessionParamsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D265FBC42317E5090017EAC4 /* SessionParamsTests.swift */; };
Expand Down Expand Up @@ -608,6 +609,7 @@
D248E67D22DE14100057FE67 /* SessionParams.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SessionParams.swift; sourceTree = "<group>"; };
D2496F3B20038B3300E75FE9 /* hterm_all.min.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = hterm_all.min.js; sourceTree = "<group>"; };
D2496F402003941B00E75FE9 /* term.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = term.js; sourceTree = "<group>"; };
D25D580F2358897B00D1BCAE /* Completions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Completions.swift; sourceTree = "<group>"; };
D263488A20BEBF5800A8E89F /* libsshpp.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = libsshpp.hpp; sourceTree = "<group>"; };
D263488B20BEBF5800A8E89F /* legacy.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = legacy.h; sourceTree = "<group>"; };
D263488C20BEBF5800A8E89F /* callbacks.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = callbacks.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -809,6 +811,7 @@
D2887A5D22DCA6D500701BD5 /* SceneDelegate.swift */,
D248E67522DDDF130057FE67 /* UIStateRestorable.swift */,
D2BC514C2355C3AE0034FDD4 /* History.swift */,
D25D580F2358897B00D1BCAE /* Completions.swift */,
);
path = Blink;
sourceTree = "<group>";
Expand Down Expand Up @@ -1797,6 +1800,7 @@
C9B2E0301D6B612400B89F69 /* BKPubKey.m in Sources */,
D27BBA1C20529FFF00AEA303 /* TermStream.m in Sources */,
D296198D214F96D50070935A /* digest-openssl.c in Sources */,
D25D58102358897B00D1BCAE /* Completions.swift in Sources */,
B7D4503C1DD4706000CE0DBE /* Reachability.m in Sources */,
07AA065D1E08B6EB008310B7 /* UserDefaultsPasscodeRepository.swift in Sources */,
B752EE2E1DFEF45300E305C8 /* BKSecurityConfigurationViewController.m in Sources */,
Expand Down
8 changes: 6 additions & 2 deletions Blink.xcodeproj/xcshareddata/xcschemes/Blink.xcscheme
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0920"
version = "1.3">
version = "2.0">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
Expand Down Expand Up @@ -72,9 +72,13 @@
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugXPCServices = "NO"
migratedStopOnEveryIssue = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
allowLocationSimulation = "YES"
viewDebuggingEnabled = "No"
queueDebuggingEnabled = "No"
memoryGraphOnResourceException = "Yes">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
Expand Down
11 changes: 9 additions & 2 deletions Blink/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,13 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
NSNotificationCenter *nc = NSNotificationCenter.defaultCenter;
[nc addObserver:self
selector:@selector(_onSceneDidEnterBackground:)
name:UISceneDidEnterBackgroundNotification object:self];
name:UISceneDidEnterBackgroundNotification object:nil];
[nc addObserver:self
selector:@selector(_onSceneWillEnterForeground:)
name:UISceneWillEnterForegroundNotification object:self];
name:UISceneWillEnterForegroundNotification object:nil];
[nc addObserver:self
selector:@selector(_onSceneDidActiveNotification:)
name:UISceneDidActivateNotification object:nil];
[nc addObserver:self
selector: @selector(_onScreenConnect)
name:UIScreenDidConnectNotification object:nil];
Expand Down Expand Up @@ -313,6 +316,10 @@ - (void)_onSceneWillEnterForeground:(NSNotification *)notification {
[self _cancelApplicationSuspend];
}

- (void)_onSceneDidActiveNotification:(NSNotification *)notification {
[self _cancelApplicationSuspend];
}

- (void)_onScreenConnect {
[BKDefaults applyExternalScreenCompensation:BKDefaults.overscanCompensation];
}
Expand Down
120 changes: 120 additions & 0 deletions Blink/Completions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
//////////////////////////////////////////////////////////////////////////////////
//
// B L I N K
//
// Copyright (C) 2016-2019 Blink Mobile Shell Project
//
// This file is part of Blink.
//
// Blink is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Blink is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Blink. If not, see <http://www.gnu.org/licenses/>.
//
// In addition, Blink is also subject to certain additional terms under
// GNU GPL version 3 section 7.
//
// You should have received a copy of these additional terms immediately
// following the terms and conditions of the GNU General Public License
// which accompanied the Blink Source Code. If not, see
// <http://www.github.com/blinksh/blink>.
//
////////////////////////////////////////////////////////////////////////////////


import Foundation

struct Completions {

func _commands() -> [String: String] {
[
"awk": "Select particular records in a file and perform operations upon them.",
"cat": "Concatenate and print files.",
"cd": "Change directory.",
// // "chflags": "chflags", // TODO
// // "chksum": "chksum", // TODO
"clear": "Clear the terminal screen. 🙈",
"compress": "Compress data.",
"config": "Add keys, hosts, themes, etc... 🔧 ",
"cp": "Copy files and directories",
"curl": "Transfer data from or to a server.",
"date": "Display or set date and time.",
"diff": "Compare files line by line.",
"dig": "DNS lookup utility.",
"du": "Disk usage",
"echo": "Write arguments to the standard output.",
"egrep": "Search for a pattern using extended regex.", // https://www.computerhope.com/unix/uegrep.htm
"env": "Set environment and execute command, or print environment.", // fish
"exit": "Exit current session. 👋",
"fgrep": "File pattern searcher.", // fish
"find": "Walk a file hierarchy.", // fish
"grep": "File pattern searcher.", // fish
"gunzip": "Compress or expand files", // https://linux.die.net/man/1/gunzip
"gzip": "Compression/decompression tool using Lempel-Ziv coding (LZ77)", // fish
"head": "Display first lines of a file", // fish
"help": "Prints all commands. 🧐 ",
"history": "Use -c option to clear history. 🙈 ",
"host": "DNS lookup utility.", // fish
"link": "Make links.", // fish
"ln": "", // TODO
"ls": "List files and directories",
"md5": "Calculate a message-digest fingerprint (checksum) for a file.", // fish
"mkdir": "Make directories.", // fish
"mosh": "Runs mosh client. 🦄",
"mv": "Move files and directories.",
"nc": "", // TODO
"nslookup": "Query Internet name servers interactively", // fish
"pbcopy": "Copy to the pasteboard.",
"pbpaste": "Paste from the pasteboard.",
"ping": "Send ICMP ECHO_REQUEST packets to network hosts.", // fish
"printenv": "Print out the environment.", // fish
"pwd": "Return working directory name.", // fish
"readlink": "Display file status.", // fish
// // @"rlogin": @"", // TODO: REMOVE
"rm": "Remove files and directories.",
"rmdir": "Remove directories.", // fish
"scp": "Secure copy (remote file copy program).", // fish
"sed": "Stream editor.", // fish
// // @"setenv": @"", // TODO
"sftp": "Secure file transfer program.", // fish
"showkey": "Display typed chars.",
"sort": "Sort or merge records (lines) of text and binary files.", // fish
"ssh": "Runs ssh client. 🐌",
"ssh-copy-id": "Copy an identity to the server. 💌",
// // @"ssh-keygen": @"", // TODO
"stat": "Display file status.", // fish
"sum": "Display file checksums and block counts.", // fish
"tail": "Display the last part of a file.", // fish
"tar": "Manipulate tape archives.", // fish
"tee": "Pipe fitting.", // fish
"telnet": "User interface to the TELNET protocol.", // fish
"theme": "Choose a theme 💅",
"touch": "Change file access and modification times.", // fish
"tr": "", // TODO
"uname": "Print operating system name.", // fish
"uncompress": "Expand data.",
"uniq": "Report or filter out repeated lines in a file.", // fish
"unlink": "Remove directory entries.", // fish
// // @"unsetenv": @"", // TODO
"uptime": "Show how long system has been running.", // fish
"wc": "Words and lines counter.",
"whoami": "Display effective user id.", // fish
"whois": "Internet domain name and network number directory service.", // fish

"open": "open url of file (Experimental). 📤",
"link-files": "link folders from Files.app (Experimental)."
]
}

func complete(str: String) {

}
}
29 changes: 12 additions & 17 deletions Blink/TermController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@ class TermController: UIViewController {
view.setNeedsLayout()
}

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated);
resumeIfNeeded()
}

public override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()

Expand Down Expand Up @@ -296,24 +301,14 @@ extension TermController: SuspendableSession {
}

_sessionParams = params

guard
_sessionParams.hasEncodedState(),
_session == nil // will be created on startSession
else {
return
_session?.sessionParams = params

if _sessionParams.hasEncodedState() {
_session?.execute(withArgs: "")
}

let input = _termDevice.input
_termDevice = TermDevice()
_termDevice.cols = params.cols
_termDevice.rows = params.rows
_termDevice.delegate = self
_termDevice.attachView(_termView)
_termDevice.attachInput(input)

if _termView.isReady {
startSession()

if view.bounds.size != _sessionParams.viewSize {
_session?.sigwinch()
}
}

Expand Down
40 changes: 27 additions & 13 deletions Sessions/MCPSession.m
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ @implementation MCPSession {
NSMutableArray<WeakSSHClient *> *_sshClients;
dispatch_queue_t _cmdQueue;
TermStream *_cmdStream;
NSString *_currentCmdLine;
}

@dynamic sessionParams;
Expand All @@ -79,7 +80,29 @@ - (id)initWithDevice:(TermDevice *)device andParams:(MCPParams *)params {
}

- (void)executeWithArgs:(NSString *)args {
[_device prompt:@"blink> " secure:NO shell:YES];
dispatch_async(_cmdQueue, ^{
[self setActiveSession];
ios_setMiniRoot([BlinkPaths documents]);

ios_setContext((__bridge void*)self);

thread_stdout = nil;
thread_stdin = nil;
thread_stderr = nil;

ios_setStreams(_stream.in, _stream.out, _stream.err);

// We are restoring mosh session if possible first.
if ([@"mosh" isEqualToString:self.sessionParams.childSessionType] && self.sessionParams.hasEncodedState) {
_childSession = [[MoshSession alloc] initWithDevice:_device andParams:self.sessionParams.childSessionParams];
[_childSession executeAttachedWithArgs:@""];
_childSession = nil;
if (self.sessionParams.hasEncodedState) {
return;
}
}
[_device prompt:@"blink> " secure:NO shell:YES];
});
}

- (void)enqueueCommand:(NSString *)cmd {
Expand All @@ -88,7 +111,9 @@ - (void)enqueueCommand:(NSString *)cmd {
return;
}
dispatch_async(_cmdQueue, ^{
self->_currentCmdLine = cmd;
[self _runCommand:cmd];
self->_currentCmdLine = nil;
});
}

Expand Down Expand Up @@ -154,17 +179,6 @@ - (BOOL)_runCommand:(NSString *)cmdline {

- (int)main:(int)argc argv:(char **)argv
{

// We are restoring mosh session if possible first.
if ([@"mosh" isEqualToString:self.sessionParams.childSessionType] && self.sessionParams.hasEncodedState) {
_childSession = [[MoshSession alloc] initWithDevice:_device andParams:self.sessionParams.childSessionParams];
[_childSession executeAttachedWithArgs:@""];
_childSession = nil;
if (self.sessionParams.hasEncodedState) {
return 0;
}
}

return 0;
}

Expand All @@ -187,7 +201,7 @@ - (void)unregisterSSHClient:(SSHClient *)sshClient {
}

- (bool)isRunningCmd {
return _childSession != nil || _currentCmd != nil;
return _childSession != nil || _currentCmd != nil || _currentCmdLine != nil;
}

- (NSArray<NSString *> *)_symlinksInHomeDirectory
Expand Down
19 changes: 18 additions & 1 deletion Sessions/MoshSession.m
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ @implementation MoshSession {
int _debug;
// NSLock * _lock;
dispatch_semaphore_t _sema;
CFTypeRef _selfRef;
}

@dynamic sessionParams;
Expand Down Expand Up @@ -254,9 +255,10 @@ - (int)main:(int)argc argv:(char **)argv

[self.sessionParams cleanEncodedState];

_selfRef = CFBridgingRetain(self);
mosh_main(
_stream.in, _stream.out, &_device->win,
&__state_callback, (__bridge void *) self,
&__state_callback, (void *)_selfRef,
[self.sessionParams.ip UTF8String],
[self.sessionParams.port UTF8String],
[self.sessionParams.key UTF8String],
Expand All @@ -272,6 +274,14 @@ - (int)main:(int)argc argv:(char **)argv
return 0;
}

- (void)main_cleanup {
if (_selfRef) {
CFBridgingRelease(_selfRef);
_selfRef = NULL;
}
[super main_cleanup];
}

- (void)processMoshSettings:(BKHosts *)host
{
NSString *server = host.moshServer.length ?
Expand Down Expand Up @@ -485,10 +495,17 @@ - (void)suspend

- (void)onStateEncoded: (NSData *) encodedState
{
NSLog(@"encodedState: %@", encodedState);
self.sessionParams.encodedState = encodedState;
NSLog(@"signalling");
dispatch_semaphore_signal(_sema);

NSLog(@"signalled");
}

- (void)dealloc
{
NSLog(@"deallocating mosh");
}

@end
1 change: 1 addition & 0 deletions Sessions/Session.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
- (void)executeWithArgs:(NSString *)args;
- (void)executeAttachedWithArgs:(NSString *)args;
- (int)main:(int)argc argv:(char **)argv;
- (void)main_cleanup;
- (void)sigwinch;
- (void)kill;
- (void)suspend;
Expand Down
Loading

0 comments on commit 37442f2

Please sign in to comment.