11using System . Diagnostics ;
22using System . Text ;
33using CliWrap ;
4+ using CliWrap . Buffered ;
45
56namespace ServiceLib . Handler ;
67
@@ -11,6 +12,7 @@ public class CoreAdminHandler
1112 private Config _config ;
1213 private Action < bool , string > ? _updateFunc ;
1314 private int _linuxSudoPid = - 1 ;
15+ private const string _tag = "CoreAdminHandler" ;
1416
1517 public async Task Init ( Config config , Action < bool , string > updateFunc )
1618 {
@@ -31,8 +33,11 @@ private void UpdateFunc(bool notify, string msg)
3133
3234 public async Task < Process ? > RunProcessAsLinuxSudo ( string fileName , CoreInfo coreInfo , string configPath )
3335 {
36+ StringBuilder sb = new ( ) ;
37+ sb . AppendLine ( "#!/bin/bash" ) ;
3438 var cmdLine = $ "{ fileName . AppendQuotes ( ) } { string . Format ( coreInfo . Arguments , Utils . GetBinConfigPath ( configPath ) . AppendQuotes ( ) ) } ";
35- var shFilePath = await CreateLinuxShellFile ( cmdLine , "run_as_sudo.sh" ) ;
39+ sb . AppendLine ( $ "sudo -S { cmdLine } ") ;
40+ var shFilePath = await FileManager . CreateLinuxShellFile ( "run_as_sudo.sh" , sb . ToString ( ) , true ) ;
3641
3742 Process proc = new ( )
3843 {
@@ -87,35 +92,24 @@ public async Task KillProcessAsLinuxSudo()
8792 return ;
8893 }
8994
90- var cmdLine = $ "pkill -P { _linuxSudoPid } ; kill { _linuxSudoPid } ";
91- var shFilePath = await CreateLinuxShellFile ( cmdLine , "kill_as_sudo.sh" ) ;
95+ try
96+ {
97+ var shellFileName = Utils . IsOSX ( ) ? Global . KillAsSudoOSXShellFileName : Global . KillAsSudoLinuxShellFileName ;
98+ var shFilePath = await FileManager . CreateLinuxShellFile ( "kill_as_sudo.sh" , EmbedUtils . GetEmbedText ( shellFileName ) , true ) ;
9299
93- await Cli . Wrap ( shFilePath )
94- . WithStandardInputPipe ( PipeSource . FromString ( AppHandler . Instance . LinuxSudoPwd ) )
95- . ExecuteAsync ( ) ;
100+ var arg = new List < string > ( ) { "-c" , $ "sudo -S { shFilePath } { _linuxSudoPid } " } ;
101+ var result = await Cli . Wrap ( Global . LinuxBash )
102+ . WithArguments ( arg )
103+ . WithStandardInputPipe ( PipeSource . FromString ( AppHandler . Instance . LinuxSudoPwd ) )
104+ . ExecuteBufferedAsync ( ) ;
96105
97- _linuxSudoPid = - 1 ;
98- }
99-
100- private async Task < string > CreateLinuxShellFile ( string cmdLine , string fileName )
101- {
102- var shFilePath = Utils . GetBinConfigPath ( fileName ) ;
103- File . Delete ( shFilePath ) ;
104-
105- var sb = new StringBuilder ( ) ;
106- sb . AppendLine ( "#!/bin/sh" ) ;
107- if ( Utils . IsAdministrator ( ) )
108- {
109- sb . AppendLine ( $ "{ cmdLine } ") ;
106+ UpdateFunc ( false , result . StandardOutput . ToString ( ) ) ;
110107 }
111- else
108+ catch ( Exception ex )
112109 {
113- sb . AppendLine ( $ "sudo -S { cmdLine } " ) ;
110+ Logging . SaveLog ( _tag , ex ) ;
114111 }
115112
116- await File . WriteAllTextAsync ( shFilePath , sb . ToString ( ) ) ;
117- await Utils . SetLinuxChmod ( shFilePath ) ;
118-
119- return shFilePath ;
113+ _linuxSudoPid = - 1 ;
120114 }
121115}
0 commit comments