public void StopService(int stopTimeout)
        {
            HiveData.SyncWithStorage(HiveSyncFlags.LoadFromFile, true);

            long pid = HiveData.ManagedData.Pid;

            if (pid != 0)
            {
                if (Env.IsWindows == false)
                {
                    if (UnixApi.Kill((int)pid, UnixApi.Signals.SIGTERM) == 0)
                    {
                        Con.WriteLine($"Shutting down the daemon \"{Name}\" (pid = {pid}) ...");

                        if (UnixApi.WaitProcessExit((int)pid, stopTimeout) == false)
                        {
                            Con.WriteLine($"Shutting down the daemon \"{Name}\" (pid = {pid}) timed out.");

                            throw new ApplicationException($"Shutting down the daemon \"{Name}\" (pid = {pid}) timed out.");
                        }
                    }
                    else
                    {
                        Con.WriteLine($"The daemon \"{Name}\" is not running.");
                    }
                }
                else
                {
                    if (ManualResetEvent.TryOpenExisting(HiveData.ManagedData.EventName !, out EventWaitHandle? eventHandle))
                    {
                        try
                        {
                            Con.WriteLine($"Stopping the daemon \"{Name}\" (pid = {pid}) ...");

                            eventHandle.Set();

                            if (Win32ApiUtil.WaitProcessExit((int)pid, stopTimeout) == false)
                            {
                                Con.WriteLine($"Stopping the daemon \"{Name}\" (pid = {pid}) timed out.");

                                throw new ApplicationException($"Stopping the daemon \"{Name}\" (pid = {pid}) timed out.");
                            }
                        }
                        finally
                        {
                            eventHandle._DisposeSafe();
                        }
                    }
                    else
                    {
                        Con.WriteLine($"The daemon \"{Name}\" is not running.");
                    }
                }
示例#2
0
 // 現在のユーザーが管理者権限を有するかどうか確認をする
 static bool CheckIsAdmin()
 {
     if (Env.IsUnix == false)
     {
         // Windows
         return(Win32ApiUtil.IsUserAnAdmin());
     }
     else
     {
         // Unix: 現在のユーザー名が「root」であるかどうかで判別をする
         return(Env.UserName._IsSamei(Consts.Strings.RootUsername));
     }
 }
        public static string GetFinalPath(SafeFileHandle handle,
                                          Win32Api.Kernel32.FinalPathFlags flags = Win32Api.Kernel32.FinalPathFlags.FILE_NAME_NORMALIZED | Win32Api.Kernel32.FinalPathFlags.VOLUME_NAME_DOS)
        {
            StringBuilder str = new StringBuilder(260 + 10);
            int           r;

            r = Win32Api.Kernel32.GetFinalPathNameByHandle(handle, str, 260, flags);

            if (r >= 260)
            {
                str = new StringBuilder(65536 + 10);
                r   = Win32Api.Kernel32.GetFinalPathNameByHandle(handle, str, 65536, flags);
            }

            if (r == 0)
            {
                Win32ApiUtil.ThrowLastWin32Error();
            }

            return(str.ToString());
        }