Skip to content

Commit

Permalink
improving code to work better in UAC environment
Browse files Browse the repository at this point in the history
  • Loading branch information
kohsuke committed Nov 18, 2010
1 parent 2fa75e7 commit f86f622
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 5 deletions.
38 changes: 33 additions & 5 deletions Main.cs
Expand Up @@ -435,8 +435,12 @@ private static void CopyStream(Stream i, Stream o)

public class WrapperService : ServiceBase
{
[DllImport("ADVAPI32.DLL", EntryPoint = "SetServiceStatus")]
[DllImport("ADVAPI32.DLL")]
private static extern bool SetServiceStatus(IntPtr hServiceStatus, ref SERVICE_STATUS lpServiceStatus);

[DllImport("Kernel32.dll", SetLastError = true)]
public static extern int SetStdHandle(int device, IntPtr handle);

private SERVICE_STATUS wrapperServiceStatus;

private Process process = new Process();
Expand Down Expand Up @@ -672,7 +676,7 @@ private void WriteEvent(String message)
log.Close();
}

protected override void OnStart(string[] args)
protected override void OnStart(string[] _)
{
envs = descriptor.EnvironmentVariables;
foreach (string key in envs.Keys)
Expand Down Expand Up @@ -915,14 +919,38 @@ private static void ThrowNoSuchService()
throw new WmiException(ReturnValue.NoSuchService);
}

public static void Run(string[] args)
public static void Run(string[] _args)
{
if (args.Length > 0)
if (_args.Length > 0)
{
var d = new ServiceDescriptor();
Win32Services svc = new WmiRoot().GetCollection<Win32Services>();
Win32Service s = svc.Select(d.Id);

var args = new List<string>(Array.AsReadOnly(_args));
if (args[0] == "/redirect")
{
// Redirect output
// One might ask why we support this when the caller
// can redirect the output easily. The answer is for supporting UAC.
// On UAC-enabled Windows such as Vista, SCM operation requires
// elevated privileges, thus winsw.exe needs to be launched
// accordingly. This in turn limits what the caller can do,
// and among other things it makes it difficult for the caller
// to read stdout/stderr. Thus redirection becomes handy.
var f = new FileStream(args[1], FileMode.Create);
var w = new StreamWriter(f);
w.AutoFlush = true;
Console.SetOut(w);
Console.SetError(w);

var handle = f.Handle;
SetStdHandle(-11, handle); // set stdout
SetStdHandle(-12, handle); // set stder

args = args.GetRange(2, args.Count - 2);
}

args[0] = args[0].ToLower();
if (args[0] == "install")
{
Expand Down Expand Up @@ -999,7 +1027,7 @@ public static void Run(string[] args)
if (args[0] == "test")
{
WrapperService wsvc = new WrapperService();
wsvc.OnStart(args);
wsvc.OnStart(args.ToArray());
Thread.Sleep(1000);
wsvc.OnStop();
}
Expand Down
10 changes: 10 additions & 0 deletions manifest.xml
@@ -0,0 +1,10 @@
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<assemblyIdentity version="1.0.0.0" name="winsw" type="win32"/>
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
<security>
<requestedPrivileges>
<requestedExecutionLevel level="requireAdministrator"/>
</requestedPrivileges>
</security>
</trustInfo>
</assembly>
10 changes: 10 additions & 0 deletions winsw.csproj
Expand Up @@ -14,6 +14,10 @@
<FileAlignment>512</FileAlignment>
<StartupObject>
</StartupObject>
<SignManifests>false</SignManifests>
<SignAssembly>false</SignAssembly>
<AssemblyOriginatorKeyFile>
</AssemblyOriginatorKeyFile>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand Down Expand Up @@ -51,6 +55,7 @@
<Compile Include="WmiSchema.cs" />
</ItemGroup>
<ItemGroup>
<Content Include="manifest.xml" />
<Content Include="winsw.xml" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
Expand All @@ -61,4 +66,9 @@
<Target Name="AfterBuild">
</Target>
-->
<PropertyGroup>
<PostBuildEvent>"C:\Program Files\Microsoft SDKs\Windows\v7.1\Bin\mt.exe" -manifest "$(ProjectDir)manifest.xml" –outputresource:"$(TargetDir)$(TargetFileName)";#1

"C:\Program Files\Microsoft SDKs\Windows\v7.1\Bin\signtool.exe" sign /f c:\winsw-do-not-add-to-scm.pfx /p winsw /t http://timestamp.comodoca.com/authenticode $(TargetFileName)</PostBuildEvent>
</PropertyGroup>
</Project>

0 comments on commit f86f622

Please sign in to comment.