Skip to content

Print Message of the Day (MOTD)

Mike Griese edited this page May 26, 2017 · 1 revision

This is an example of how to display the MOTD on Ubuntu when the user runs launcher.exe without any arguments.

We can do this by extending the LaunchDefault method of your launcher. First, go and mark the BaseLinuxDistroLauncher::LaunchDefault() method as virtual, and add a LaunchDefault() method to MyLinuxDistroLauncher.hpp

We'll define the MyLinuxDistroLauncher::LaunchDefault method to execute the necessary commands, like this:

HRESULT MyLinuxDistroLauncher::LaunchDefault()
{
    HRESULT hr = S_OK;
    DWORD returnValue;
    
    // Configure Ubuntu to generate a MOTD:
    WSL_DISTRIBUTION_FLAGS flags;
    ULONG defaultUid;
    ULONG version;
    PSTR * env;
    ULONG envCount;

    hr = wslApi.WslGetDistributionConfiguration(_myName, &version, &defaultUid, &flags, &env, &envCount);
    if (SUCCEEDED(hr))
    {
        // run as root
        hr = wslApi.WslConfigureDistribution(_myName, 0, flags);
        
        hr = wslApi.WslLaunchInteractive(_myName, 
                    L"run-parts --lsbsysinit /etc/update-motd.d > /var/run/motd.new; "
                    L"mv -f /var/run/motd.new /var/run/motd; "
                    L"cat /var/run/motd; "
                    , false, &returnValue);

        // restore default user
        hr = wslApi.WslConfigureDistribution(_myName, defaultUid, flags);
    }


    hr = wslApi.WslLaunchInteractive(_myName, L"", false, &returnValue);

    return hr;
}

There is a little bit of trickiness going on here - because the default user cannot write to /var/run/motd.new (this requires root priviledges) we'll have to run the command as root. To do that, we have to get the distro's configuration (to be able to restore it later), then change the distro's default UID to 0 - the UID of root.

We then run the process for updating the MOTD, and restore the original default user, before launching the user's default shell.

You can see the results here:

A warning

Changing the default UID to root like this can have unintended consequences. If the user interrupts the launcher before it's had a chance to restore the default user, then the default user will remain as root! The user can always run launcher config --default-user <username> to restore their user setting, but that's certainly not ideal.

A better solution would be to write the MOTD to a place that would be readable and writeable by the default user, and never touch the default UID.