PageRenderTime 39ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/base/Applications/StartProcess/StartProcess.cs

#
C# | 105 lines | 73 code | 18 blank | 14 comment | 12 complexity | 67a2e16d91317d68e5df1b524fad448c MD5 | raw file
  1. ////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Microsoft Research Singularity
  4. //
  5. // Copyright (c) Microsoft Corporation. All rights reserved.
  6. //
  7. // Note: Run test code, as identified by the distro's test.xml manifest
  8. //
  9. using FileSystem.Utils;
  10. using Microsoft.Singularity.Applications;
  11. using Microsoft.Singularity.Channels;
  12. using Microsoft.Singularity.Io;
  13. using Microsoft.Singularity.Xml;
  14. using System;
  15. using System.Collections;
  16. using Microsoft.Singularity.Channels;
  17. using Microsoft.Singularity.V1.Services;
  18. using Microsoft.SingSharp.Reflection;
  19. using Microsoft.Singularity.Directory;
  20. using Microsoft.Singularity.Applications;
  21. using Microsoft.Contracts;
  22. using Microsoft.Singularity.Configuration;
  23. [assembly: Transform(typeof(ApplicationResourceTransform))]
  24. namespace Microsoft.Singularity.Applications
  25. {
  26. [ConsoleCategory(HelpMessage="Test harness", DefaultAction=true)]
  27. internal sealed class Parameters {
  28. [InputEndpoint("data")]
  29. public readonly TRef<UnicodePipeContract.Exp:READY> Stdin;
  30. [OutputEndpoint("data")]
  31. public readonly TRef<UnicodePipeContract.Imp:READY> Stdout;
  32. [Endpoint]
  33. public readonly TRef<DirectoryServiceContract.Imp:Start> nsRef;
  34. [StringArrayParameter( "EveryThingElse", Mandatory=false)]
  35. internal string[] Args;
  36. reflective internal Parameters();
  37. internal int AppMain() {
  38. return RunTests.AppMain(this);
  39. }
  40. }
  41. public class RunTests
  42. {
  43. //[ShellCommand("StartProcess", "tests lib routine to start process with Manifest")]
  44. internal static int AppMain(Parameters! config)
  45. {
  46. DirectoryServiceContract.Imp ds = null;
  47. if (config.nsRef != null) ds = config.nsRef.Acquire();
  48. if (ds == null) {
  49. throw new Exception("Unable to acquire handle to the Directory Service root");
  50. }
  51. ds.RecvSuccess();
  52. // Make ourselves a new output pipe
  53. UnicodePipeContract.Exp! newOutputExp;
  54. UnicodePipeContract.Imp! newOutputImp;
  55. UnicodePipeContract.NewChannel(out newOutputImp, out newOutputExp);
  56. // Retrieve our real stdOut and start using the one we just created
  57. // instead
  58. UnicodePipeContract.Imp stdOut = ConsoleOutput.Swap(newOutputImp);
  59. if (stdOut == null) {
  60. Console.WriteLine("runtests expects a STDOUT pipe");
  61. delete newOutputExp;
  62. delete ds;
  63. return 1;
  64. }
  65. // Use a mux to splice our own output together with the child
  66. // processes we will run.
  67. PipeMultiplexer! outputMux = PipeMultiplexer.Start(stdOut, newOutputExp);
  68. string[] args = config.Args;
  69. if (args != null && args[0] != null) RunProcess(ds, args ,outputMux);
  70. outputMux.Dispose();
  71. delete ds;
  72. return 0;
  73. }
  74. private static int RunProcess(DirectoryServiceContract.Imp! ds,
  75. string[] args,
  76. PipeMultiplexer! outputMux)
  77. {
  78. Process child = Binder.CreateProcess(ds,args,outputMux);
  79. if (child != null) {
  80. child.Start();
  81. child.Join();
  82. return 0;
  83. }
  84. return -1;
  85. }
  86. }
  87. }