wsltty.appx icon indicating copy to clipboard operation
wsltty.appx copied to clipboard

TODO: Remove Launcher and execute mintty directly in Appx package

Open Biswa96 opened this issue 7 years ago • 19 comments

Here are my thoughts:

  1. Get execution path with GetModuleFileName() which has only read permission.
  2. Get Local\AppData\Package\mintty.wsltty-xxx\LocalCache folder (UWP environment specific) with SHGetKnownFolderPath() which has read/write/execute permission.
  3. Copy wslbridge.exe, wslbridge-backend and cygwin1.dll from execution path to LocalCache folder.
  4. Execute mintty.exe with parameters having wslbridge.exe in LocalCache folder (like in the C# project).

All functions/variable should be with UNICODE/wide character.

Biswa96 avatar Mar 28 '18 20:03 Biswa96

I'm not sure what to do with this:

  • What's the benefit of using C instead of C++? (Not that I would mined, but the existing launcher works, so why spend effort?)
  • I've never understood why this copying is needed.
  • If the behaviour of the Windows API functions used is obscure, I'm not more familiar with them than you.
  • About your enquiry to port this to cygwin (received per email, comments seems deleted): if this worked, I would see no reason anymore to use a launcher at all, mintty (maybe in a special compiled brand) could be invoked directly.

mintty avatar Mar 30 '18 09:03 mintty

Previously, you said to make the launcher in C/C++. So I do some search to make a C project. This is not so important. Just making it so that user can also get alternative to make that launcher with cygwin or mingw.

Biswa96 avatar Mar 30 '18 09:03 Biswa96

OK, so the current launcher is C#, right? Actually, I rather said two things:

  • build the launcher without the VS IDE (which works now)
  • check whether we can do without the launcher at all (which is still unclear)

In cygwin, your code compiles (but does not link) with the following additions:

#include <wchar.h>
#include <w32api/windows.h>
#include <w32api/pathcch.h>
#undef NTDDI_VERSION
#define NTDDI_VERSION 0x06000000  // needed to test compilation on Windows 7
#include <w32api/shlobj.h>
#define KF_FLAG_FORCE_APP_DATA_REDIRECTION 0x00080000
...
                SHGetKnownFolderPath(&FOLDERID_LocalAppData, ...  // add & here

mintty avatar Mar 30 '18 09:03 mintty

This is not complete. I just provide an outline. I've not fully tested it. And there are some mistakes. If I complete that I shall let you know. BTW, this code need Pathcch.lib and Shlwapi.lib. If this can be done without launcher, then you may have to add the code in mintty, which I think is not a great idea. Because mintty project is most important and it is shared in many project e.g. git, cygwin, comemu etc.

Biswa96 avatar Mar 30 '18 10:03 Biswa96

It's not a problem to add code to mintty that gets compiled in conditionally, e.g. only when building for wsltty.appx. If that saves us the launcher, I'll gladly do it.

mintty avatar Mar 30 '18 10:03 mintty

Actually the current repository version supports that already, you just need to rename mintty.exe to wsltty.exe. If you don't want to install a cygwin environment, I can provide a binary.

mintty avatar Mar 31 '18 20:03 mintty

I've never understood why this copying is needed.

  • When you install AppX package the files installed in ProgramFiles\WindowsApps. We know that ProgramFiles folder has not any normal user permission (i.e. you can't add/edit files). With UWP environment that acts like a container. So you can't write or execute other files from that folder. Here is what happen if you execute wslbridge from WindowsApps folder:

uwp_permission_denied

  • Hence by copying wslbridge in Local\Packages folder you get write and execute permission. So that mintty can easily execute wslbridge from that folder (with --exec option).

  • But with normal unpacked installation with command Add-AppxPackage -Path <Path_To_AppxManifest.xml> -Register, user can install it anywhere. So mintty has full control in that folder and copying not required. Hence I announce that Launcher is no longer required with unpacked files.

Biswa96 avatar Apr 01 '18 04:04 Biswa96

Great to see this progress. Actually, testing the execute permissions, if I copy e.g. /bin/ls (from WSL) into the app /bin directory, I can run it from WSL.

mintty avatar Apr 01 '18 13:04 mintty

Just a note: it's not necessary to pull in C++ just for this function (but maybe there will be other APIs to use): https://msdn.microsoft.com/en-us/library/windows/desktop/bb762188%28v=vs.85%29.aspx

mintty avatar May 07 '18 12:05 mintty

How about using a function like this in a separate DLL? Also include the icon extracting function from so-called Appx-wraper-exe file.

void getAppdata(PWSTR backend, PWSTR config, PWSTR icon) {
    PWSTR appData = NULL;
    HMODULE dll = LoadLibraryExW(L"shell32.dll", NULL, LOAD_LIBRARY_SEARCH_SYSTEM32);
    pSHGetKnownFolderPath SHGetKnownFolderPath = (pSHGetKnownFolderPath)GetProcAddress(dll, "SHGetKnownFolderPath");
    HRESULT hres = SHGetKnownFolderPath(FOLDERID_LocalAppData, KF_FLAG_FORCE_APP_DATA_REDIRECTION, 0, &appData);

    swprintf(backend, MAX_PATH, L"%ls\\%ls", appData, L"wslbridge-backend");
    swprintf(config, MAX_PATH, L"%ls\\%ls", appData, L".minttyrc");
    swprintf(icon, MAX_PATH, L"%ls\\%ls", appData, L"wsl.ico");
}

Biswa96 avatar Jun 13 '18 05:06 Biswa96

Sorry I hadn't responded to this yet. For all three we have other solutions already, or meanwhile (for the backend), also the .minttyrc is not the preferred one, which should be in $APPDATA, for which the standard mintty solution will find it.

mintty avatar Jun 24 '18 10:06 mintty

Released 1.9.0. It is now possible to run a stand-alone wsltty.exe (or mintty.exe if compiled with -DWSLTTY_APPX) which handles the wslbridge backend copying. I'll appreciate a contribution to generate a modified appx package. (Or I may check whether I can generate an updated appx package, following the old scheme.)

mintty avatar Jul 01 '18 23:07 mintty

Latest mintty commit doesn't work. It shows "wslbridge: unknown option --backend". May be there is some problem with the command line string.

Biswa96 avatar Jul 02 '18 06:07 Biswa96

I somehow lost the update to use the latest wslbridge commit. (Would be easier if that were a release eventually...) Add this before makefile section "# default target":

# after 0.2.4, with --backend option:
wslbridge-commit=47b41bec6c32da58ab01de9345087b1a4fd836e3

mintty avatar Jul 02 '18 12:07 mintty

How can I add the wsltty_appx option in mintty makefile?

(comment deleted?) Easiest would be to rename mintty.exe to wsltty.exe, so the static option WSLTTY_APPX is not needed. Otherwise I'll have to fiddle it into the makefile when I find the time.

mintty avatar Jul 02 '18 12:07 mintty

I've compiled the mintty with make CCOPT=-DWSLTTY_APPX command. Also compiled wslbridge from it's repository manually. wsltty.exe may not take the wsl.ico from /usr/share/mintty/icon/wsl.ico path. Any solution?

Biswa96 avatar Jul 02 '18 13:07 Biswa96

If mintty does not find a distro-specific icon, it would look for one in the APPDATA places, not in the config directories (like /usr/share...). (The latter, which you may have seen in the code, is a specific configurable fallback for the mintty session launcher and does not apply to the icon of the current mintty session.)

mintty avatar Jul 02 '18 14:07 mintty

I made a fixed wsltty release and also updated the makefile for wsltty.appx, which now defines WSLTTY_APPX for the backend copying. I'd appreciate your contribution of further updated files (manifest probably, and the appx package of course) as I won't find the time to try it myself soon.

mintty avatar Jul 04 '18 19:07 mintty

  • Things that works:
  1. wslbridge-backend copies correctly.
  2. .minttyrc created correctly.
  • Things that doesn't work well:
  1. mintty desn't find wsl.ico. Where to place?
  2. mintty opens in System32 folder at first run (as expected), should be configured to set default path.

See my forked repo. I've change the building procedure with batch file and will change if necessary.

Biswa96 avatar Jul 04 '18 21:07 Biswa96