/////////////////////////////////////////////////////////////////////////////// // // Copyright (c) Microsoft Corporation. All rights reserved. // using System; using System.Collections; using Microsoft.Singularity.Directory; using Microsoft.Singularity.Channels; using Microsoft.Singularity.FileSystem; using Microsoft.Singularity.V1.Services; using Microsoft.SingSharp.Reflection; using Microsoft.Singularity.Applications; using Microsoft.Singularity.Io; using Microsoft.Singularity.Configuration; [assembly: Transform(typeof(ApplicationResourceTransform))] namespace Microsoft.Singularity.Applications { [ConsoleCategory(HelpMessage="Mount CD device", DefaultAction=true)] internal class Parameters { [InputEndpoint("data")] public readonly TRef Stdin; [OutputEndpoint("data")] public readonly TRef Stdout; [Endpoint] public readonly TRef nsRef; [BoolParameter( "s", Default=false, HelpMessage="Silent.")] internal bool silent; reflective internal Parameters(); internal int AppMain() { return CdUnmount.AppMain(this); } } public class CdUnmount { public static void WriteLineWrapper(string format, params object[] args) { if (!silent) { Console.WriteLine(format, args); } } public static void WriteWrapper(string format, params object[] args) { if (!silent) { Console.Write(format, args); } } public static bool silent = false; public const string ControlLocation = "/CdCtrl"; internal static int AppMain(Parameters! config) { DirectoryServiceContract.Imp ds = (config.nsRef).Acquire(); if (ds == null) { throw new Exception("Unable to acquire handle to the Directory Service root"); } ds.RecvSuccess(); silent = config.silent; Iso9660ServiceControlContract.Imp! controlClient; Iso9660ServiceControlContract.Exp! controlServer; Iso9660ServiceControlContract.NewChannel( out controlClient, out controlServer); ErrorCode errorOut; bool ok = SdsUtils.Bind(ControlLocation, ds, controlServer, out errorOut); if (!ok) { delete controlClient; delete ds; WriteLineWrapper("Error binding to " + ControlLocation + " reason " + SdsUtils.ErrorCodeToString(errorOut) ); return 0; } else { WriteLineWrapper("Bind of "+ControlLocation+ " succeeded!"); } controlClient.RecvSuccess(); WriteLineWrapper("Unmounting..."); controlClient.SendUnmount(); switch receive { case controlClient.NackUnmount(): Console.WriteLine("Unmount failed. :("); delete ds; delete controlClient; return -1; break; case controlClient.AckUnmount(): Console.WriteLine("Unmount succeeded."); break; case controlClient.ChannelClosed(): throw new Exception("controlClient channel closed"); } delete ds; delete controlClient; WriteWrapper("Restarting Iso9660..."); Process p = new Process(new string[] {"iso9660"}); p.Start(); p.Dispose(true); WriteLineWrapper("done."); return 0; } } }