blob: ee68b4679980c58c840be5bd596510e7bf46abf4 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
// *****************************************************************************
// * This file is part of the FreeFileSync project. It is distributed under *
// * GNU General Public License: https://www.gnu.org/licenses/gpl-3.0 *
// * Copyright (C) Zenju (zenju AT freefilesync DOT org) - All Rights Reserved *
// *****************************************************************************
#include "shutdown.h"
#include "thread.h"
#include <zen/process_exec.h>
using namespace zen;
void zen::shutdownSystem() //throw FileError
{
assert(runningOnMainThread());
if (runningOnMainThread())
onSystemShutdownRunTasks();
try
{
//https://linux.die.net/man/2/reboot => needs admin rights!
//"systemctl" should work without admin rights:
auto [exitCode, output] = consoleExecute("systemctl poweroff", std::nullopt /*timeoutMs*/); //throw SysError, (SysErrorTimeOut)
trim(output);
if (!output.empty()) //see comment in suspendSystem()
throw SysError(utfTo<std::wstring>(output));
}
catch (const SysError& e) { throw FileError(_("Unable to shut down the system."), e.toString()); }
}
void zen::suspendSystem() //throw FileError
{
try
{
//"systemctl" should work without admin rights:
auto [exitCode, output] = consoleExecute("systemctl suspend", std::nullopt /*timeoutMs*/); //throw SysError, (SysErrorTimeOut)
trim(output);
//why does "systemctl suspend" return exit code 1 despite apparent success!??
if (!output.empty()) //at least we can assume "no output" on success
throw SysError(utfTo<std::wstring>(output));
}
catch (const SysError& e) { throw FileError(_("Unable to shut down the system."), e.toString()); }
}
void zen::terminateProcess(int exitCode)
{
std::quick_exit(exitCode); //[[noreturn]]; "Causes normal program termination to occur without completely cleaning the resources." => perfect
for (;;) //why still here?? => crash deliberately!
*reinterpret_cast<volatile int*>(0) = 0; //crude but at least we'll get crash dumps *if* it ever happens
}
//Command line alternatives:
//Shut down: systemctl poweroff //alternative requiring admin: sudo shutdown -h 1
//Sleep: systemctl suspend //alternative requiring admin: sudo pm-suspend
//Log off: gnome-session-quit --no-prompt
// alternative requiring admin: sudo killall Xorg
// alternative without admin: dbus-send --session --print-reply --dest=org.gnome.SessionManager /org/gnome/SessionManager org.gnome.SessionManager.Logout uint32:1
namespace
{
using ShutdownTaskList = std::vector<std::weak_ptr<const std::function<void()>>>;
constinit Global<ShutdownTaskList> globalShutdownTasks;
GLOBAL_RUN_ONCE(globalShutdownTasks.set(std::make_unique<ShutdownTaskList>()));
}
void zen::onSystemShutdownRegister(const SharedRef<std::function<void()>>& task)
{
assert(runningOnMainThread());
const auto& tasks = globalShutdownTasks.get();
assert(tasks);
if (tasks)
tasks->push_back(task.ptr());
}
void zen::onSystemShutdownRunTasks()
{
assert(runningOnMainThread()); //no multithreading! else: after taskWeak.lock() task() references may go out of scope! (e.g. "this")
const auto& tasks = globalShutdownTasks.get();
assert(tasks);
if (tasks)
for (const std::weak_ptr<const std::function<void()>>& taskWeak : *tasks)
if (const std::shared_ptr<const std::function<void()>>& task = taskWeak.lock();
task)
try
{ (*task)(); }
catch (...) { assert(false); }
globalShutdownTasks.set(nullptr); //trigger assert in onSystemShutdownRegister(), just in case...
}
|