Exemple #1
0
                #pragma warning restore CS0067

        public async Task <Connection> ActivateAsync(CancellationToken token)
        {
            await Task.Yield();

            var vscodeDirectory = Path.Combine(
                Environment.GetFolderPath(
                    Environment.SpecialFolder.UserProfile),
                ".vscode",
                "extensions");

            if (!Directory.Exists(vscodeDirectory))
            {
                throw new Exception("PowerShell VS Code extension required.");
            }

            var extensionDirectory = Directory.GetDirectories(vscodeDirectory)
                                     .FirstOrDefault(m => m.Contains("ms-vscode.powershell"));

            var script = Path.Combine(extensionDirectory, "modules", "PowerShellEditorServices", "Start-EditorServices.ps1");

            var info = new ProcessStartInfo {
                FileName               = @"/usr/local/bin/pwsh",
                Arguments              = $@"-NoProfile -NonInteractive -ExecutionPolicy Bypass -Command "" & '{script}' -HostName 'Visual Studio Code Host' -HostProfileId 'Microsoft.VSCode' -HostVersion '1.5.1' -AdditionalModules @('PowerShellEditorServices.VSCode') -BundledModulesPath '{extensionDirectory}/modules' -EnableConsoleRepl -LogLevel 'Diagnostic' -LogPath '{extensionDirectory}/logs/VSEditorServices.log' -SessionDetailsPath '{extensionDirectory}/logs/PSES-VS' -FeatureFlags @()""",
                RedirectStandardInput  = true,
                RedirectStandardOutput = true,
                RedirectStandardError  = true,
                UseShellExecute        = false,
                CreateNoWindow         = true
            };

            var process = new Process();

            process.StartInfo = info;

            if (process.Start())
            {
                //Wait for startup....
                string sessionFile = $@"{extensionDirectory}/logs/PSES-VS";
                var    sessionInfo = await WaitForSessionFileAsync(sessionFile);

                File.Delete(sessionFile);

                var sessionInfoJObject = JsonConvert.DeserializeObject <JObject> (sessionInfo);

                var status = (string)sessionInfoJObject ["status"];
                if (status != "started")
                {
                    LanguageClientLoggingService.Log(sessionInfoJObject.ToString());
                    var reason = (string)sessionInfoJObject ["reason"];
                    throw new ApplicationException($"Failed to start PowerShell console. {reason}");
                }

                var languageServicePipeName = (string)sessionInfoJObject ["languageServicePipeName"];
                var client = new UnixClient(languageServicePipeName);
                var stream = client.GetStream();
                return(new Connection(stream, stream));
            }

            return(null);
        }
Exemple #2
0
        protected override void SendRequest(RequestMessage request)
        {
            client = new UnixClient(this.socket_name);
            client.SendBufferSize    = 4096;
            client.ReceiveBufferSize = 4096;
            NetworkStream stream = client.GetStream();

            base.SendRequest(request, stream);
        }
Exemple #3
0
 public void TestPassUnixFd()
 {
     using (ListenUnix(FASTCGI_SOCKET_PATH, FastCgiAccept))
         using (ListenUnix(FPM_SOCKET_PATH, FpmUnixAccept)) {
             var    webserver = new UnixClient(FPM_SOCKET_PATH);
             string read;
             using (var stream = webserver.GetStream())
                 using (var reader = new StreamReader(stream))
                     read = reader.ReadLine();
             Assert.AreEqual(MESSAGE, read);
         }
 }
Exemple #4
0
        public void Response(Messages.Response response)
        {
            Messages.Communication comm = new Messages.Communication();

            comm.Type     = Messages.Communication.CommunicationType.Response;
            comm.Response = response;

            byte[] serialized = Messages.Messages.Create(comm);

            if (serialized != null)
            {
                d_client.GetStream().Write(serialized, 0, serialized.Length);
            }
        }
Exemple #5
0
        public static Process SpawnOndemandChild(string socketFile)
        {
            CompatArraySegment <byte>?torelease = null;

            try {
                Logger.Write(LogLevel.Debug, "Spawning via the shim {0}", socketFile);
                var client = new UnixClient();
                client.Connect(socketFile);
                CompatArraySegment <byte> buffer = buffers.ClaimBuffer();
                torelease = buffer;
                int receivedCount;
                using (NetworkStream socket = client.GetStream()) {
                    socket.Write(spawnString, 0, spawnString.Length);
                    receivedCount = socket.Read(buffer.Array, buffer.Offset, buffer.Count);
                    if (receivedCount < 0)
                    {
                        throw new Exception("Didn't receive the child pid");
                    }
                }
                string received = Encoding.UTF8.GetString(buffer.Array, buffer.Offset, receivedCount);
                string clean    = received.Trim();
                int    pid;
                if (!Int32.TryParse(clean, out pid))
                {
                    throw new Exception("Couldn't parse the pid \"" + clean + "\"");
                }

                if (pid < 0)
                {
                    throw new Exception("Invalid pid: " + pid);
                }

                return(Process.GetProcessById(pid));
            } catch (Exception e) {
                Logger.Write(LogLevel.Error, "Error while talking to the shim for socket file {0}", socketFile);
                Logger.Write(e);
                return(null);
            } finally {
                if (torelease != null)
                {
                    buffers.ReturnBuffer(torelease.Value);
                }
            }
        }
Exemple #6
0
        /*
         * Loads the specific agent assembly into this vm.
         */
        public void Attach(string agent, string args)
        {
            string user = UnixUserInfo.GetRealUser().UserName;

            // Check whenever the attach socket exists
            string socket_file = "/tmp/mono-" + user + "/.mono-" + pid;

            if (!File.Exists(socket_file))
            {
                string     trigger_file = "/tmp/.mono_attach_pid" + pid;
                FileStream trigger      = null;

                try {
                    trigger = File.Create(trigger_file);
                    trigger.Close();

                    // Ask the vm to start the attach mechanism
                    Syscall.kill((int)pid, Signum.SIGQUIT);

                    // Wait for the socket file to materialize
                    int i;
                    for (i = 0; i < 10; ++i)
                    {
                        if (File.Exists(socket_file))
                        {
                            break;
                        }
                        Thread.Sleep(100);
                    }

                    if (i == 10)
                    {
                        throw new Exception(String.Format("Runtime failed to create attach socket '{0}'.", socket_file));
                    }
                } finally {
                    File.Delete(trigger_file);
                }
            }

            /*
             * We communicate with the agent inside the runtime using a simlified
             * version of the .net remoting protocol.
             */

            string path = "/tmp/mono-" + user + "/.mono-" + pid;

            UnixClient client = new UnixClient(path);

            NetworkStream stream = client.GetStream();

            // Compose payload
            MemoryStream ms     = new MemoryStream();
            BinaryWriter writer = new BinaryWriter(ms);

            write_string(writer, "attach");
            write_string(writer, agent);
            write_string(writer, args);

            // Write header
            byte[] magic = new byte [] { (byte)'M', (byte)'O', (byte)'N', (byte)'O', 1, 0 };
            stream.Write(magic, 0, magic.Length);

            // Write payload length
            new BinaryWriter(stream).Write((int)ms.Length);

            // Write payload
            stream.Write(ms.GetBuffer(), 0, (int)ms.Length);
        }
Exemple #7
0
        protected override Stream RequestStream()
        {
            string filename = Environment.GetEnvironmentVariable("OPTIMIZATION_UNIX_SOCKET");

            if (filename == null)
            {
                return(null);
            }

            if (d_client != null)
            {
                return(null);
            }

            // Open unix socket...
            d_client = new UnixClient();

            try
            {
                d_client.Connect(filename);
            }
            catch (Exception e)
            {
                Console.Error.WriteLine("Could not open unix socket: " + e.Message);
                return(null);
            }

            int ctx = 0;

            while (!d_client.GetStream().DataAvailable&& ctx < 10)
            {
                System.Threading.Thread.Sleep(100);
                ++ctx;
            }

            byte[] all = new byte[] {};

            while (d_client.GetStream().DataAvailable)
            {
                byte[] buffer = new byte[1024];
                int    ret;

                try
                {
                    ret = d_client.GetStream().Read(buffer, 0, buffer.Length);
                }
                catch (Exception e)
                {
                    Console.Error.WriteLine("Failed to read: " + e.Message);
                    return(null);
                }

                if (ret == 0)
                {
                    break;
                }

                int prev = all.Length;
                Array.Resize(ref all, all.Length + ret);
                Array.Copy(buffer, 0, all, prev, ret);
            }

            return(new MemoryStream(all));
        }