aboutsummaryrefslogtreecommitdiff
path: root/src/zen/cmds/service_cmd.cpp
blob: c43c4e6147393cf62a6378dc16b844e864d3d8c0 (plain) (blame)
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
// Copyright Epic Games, Inc. All Rights Reserved.

#include "service_cmd.h"

#include <zencore/filesystem.h>
#include <zencore/fmtutils.h>
#include <zencore/logging.h>
#include <zencore/zencore.h>
#include <zenutil/service.h>
#include <filesystem>

#if ZEN_PLATFORM_WINDOWS
#	include <zencore/windows.h>
#	include <shellapi.h>
#	include <shlwapi.h>
#	pragma comment(lib, "shlwapi.lib")
#endif

#if ZEN_PLATFORM_LINUX || ZEN_PLATFORM_MAC
#	include <unistd.h>
#endif

#if ZEN_PLATFORM_LINUX
#	include <pwd.h>
#	include <grp.h>
#endif

ZEN_THIRD_PARTY_INCLUDES_START
#include <gsl/gsl-lite.hpp>
ZEN_THIRD_PARTY_INCLUDES_END

#include <memory>

namespace zen {

//////////////////////////////////////////////////////////////////////////

namespace {

#if ZEN_PLATFORM_WINDOWS
	BOOL RequiresElevation()
	{
		BOOL   fRet	  = TRUE;
		HANDLE hToken = NULL;
		if (OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken))
		{
			TOKEN_ELEVATION Elevation;
			DWORD			cbSize = sizeof(TOKEN_ELEVATION);
			if (GetTokenInformation(hToken, TokenElevation, &Elevation, sizeof(Elevation), &cbSize))
			{
				fRet = !Elevation.TokenIsElevated;
			}
		}
		if (hToken)
		{
			CloseHandle(hToken);
		}
		return fRet;
	}

	void WinRelaunchElevated()
	{
		TCHAR CurrentDir[4096];
		GetCurrentDirectory(4096, CurrentDir);

		ExtendableWideStringBuilder<256> Parameters;
		std::filesystem::path			 ExecutablePath = GetRunningExecutablePath();
		std::wstring					 CommandLine(GetCommandLine());
		std::wstring					 CommandArguments(PathGetArgs(CommandLine.data()));

		ZEN_CONSOLE("Attempting to run '{} {}' elevated...", ExecutablePath, WideToUtf8(CommandArguments));

		SHELLEXECUTEINFO shExInfo = {0};
		shExInfo.cbSize			  = sizeof(shExInfo);
		shExInfo.fMask			  = SEE_MASK_NOCLOSEPROCESS | SEE_MASK_NO_CONSOLE;
		shExInfo.hwnd			  = 0;
		shExInfo.lpVerb			  = TEXT("runas");			   // Operation to perform
		shExInfo.lpFile			  = ExecutablePath.c_str();	   // Application to start
		shExInfo.lpParameters	  = CommandArguments.c_str();  // Additional parameters
		shExInfo.lpDirectory	  = CurrentDir;
		shExInfo.nShow			  = SW_SHOW;
		shExInfo.hInstApp		  = 0;

		DWORD ProcessReturnCode = 1;
		if (ShellExecuteEx(&shExInfo))
		{
			WaitForSingleObject(shExInfo.hProcess, INFINITE);
			GetExitCodeProcess(shExInfo.hProcess, &ProcessReturnCode);
			CloseHandle(shExInfo.hProcess);
			if (ProcessReturnCode == 0)
			{
				ZEN_CONSOLE("Elevated execution completed successfully.");
			}
			else
			{
				throw ErrorWithReturnCode(fmt::format("Elevated execution completed unsuccessfully, return code: '{}'.", ProcessReturnCode),
										  (int)ProcessReturnCode);
			}
		}
		else
		{
			ThrowLastError("Failed to run elevated, operation did not complete");
		}
	}

#elif ZEN_PLATFORM_MAC

	bool RequiresElevation() { return false; }	// Mac service mode commands can run without elevation

#else

	bool RequiresElevation() { return geteuid() != 0; }

#endif	// ZEN_PLATFORM_WINDOWS

	void RunElevated(bool AllowElevation)
	{
#if ZEN_PLATFORM_WINDOWS
		if (AllowElevation)
		{
			WinRelaunchElevated();
		}
		else
		{
			throw std::runtime_error(fmt::format(
				"This command requires elevated priviliges. Run command with elevated priviliges or add '--allow-elevation' command line "
				"option."));
		}
#endif	// ZEN_PLATFORM_WINDOWS
#if ZEN_PLATFORM_LINUX || ZEN_PLATFORM_MAC
		ZEN_UNUSED(AllowElevation);
		throw std::runtime_error(fmt::format("This command requires elevated priviliges. Run the command with `sudo`"));
#endif	// ZEN_PLATFORM_LINUX || ZEN_PLATFORM_MAC
	}

}  // namespace

ServiceCommand::ServiceCommand()
{
	m_Options.add_option("",
						 "v",
						 "verb",
						 fmt::format("Verb for service - {}, {}, {}, {}, {}. Use '--' ",
									 m_StatusOptions.program(),
									 m_InstallOptions.program(),
									 m_UninstallOptions.program(),
									 m_StartOptions.program(),
									 m_StopOptions.program()),
						 cxxopts::value(m_Verb),
						 "<verb>");
	m_Options.parse_positional({"verb"});
	m_Options.positional_help("verb");

	m_StatusOptions.add_options()("h,help", "Print help");
	m_StatusOptions.add_option("",
							   "n",
							   "name",
							   fmt::format("Service name, defaults to \"{}\"", m_ServiceName),
							   cxxopts::value(m_ServiceName),
							   "<name>");
	m_StatusOptions.parse_positional({"name"});
	m_StatusOptions.positional_help("name");

	m_InstallOptions.add_options()("h,help", "Print help");
	m_InstallOptions.add_option("", "s", "executable", "Path to server executable", cxxopts::value(m_ServerExecutable), "<path>");
	m_InstallOptions.add_option("",
								"n",
								"name",
								fmt::format("Service name, defaults to \"{}\"", m_ServiceName),
								cxxopts::value(m_ServiceName),
								"<name>");

	m_InstallOptions.add_option("",
								"",
								"full",
								fmt::format("Uninstall a running service and update service binaries before installing"),
								cxxopts::value(m_FullInstall),
								"<full>");
	m_InstallOptions.add_option("",
								"",
								"install-path",
								fmt::format("Path in which to install service binaries"),
								cxxopts::value(m_InstallPath),
								"<install-path>");

	m_InstallOptions.add_option("", "u", "user", "User to run service as, defaults to current user", cxxopts::value(m_UserName), "<user>");
#if ZEN_PLATFORM_WINDOWS
	m_InstallOptions.add_option("",
								"d",
								"display-name",
								fmt::format("Service display, defaults to \"{}\"", m_ServiceDisplayName),
								cxxopts::value(m_ServiceDisplayName),
								"<display-name>");
	m_InstallOptions.add_option("",
								"",
								"description",
								fmt::format("Service description", m_ServiceDescription),
								cxxopts::value(m_ServiceDescription),
								"<description>");
	m_InstallOptions.add_option("",
								"",
								"allow-elevation",
								fmt::format("Allow attempt to relauch command using an elevated"),
								cxxopts::value(m_AllowElevation),
								"<allow-elevation>");
	m_InstallOptions.parse_positional({"executable", "name", "display-name"});
#else
	m_InstallOptions.parse_positional({"executable", "name"});
#endif	// ZEN_PLATFORM_WINDOWS
	m_InstallOptions.positional_help("executable name display-name");

	m_UninstallOptions.add_options()("h,help", "Print help");
	m_UninstallOptions.add_option("",
								  "n",
								  "name",
								  fmt::format("Service name, defaults to \"{}\"", m_ServiceName),
								  cxxopts::value(m_ServiceName),
								  "<name>");
	m_UninstallOptions.parse_positional({"name"});
	m_UninstallOptions.positional_help("name");
#if ZEN_PLATFORM_WINDOWS
	m_UninstallOptions.add_option("",
								  "",
								  "allow-elevation",
								  fmt::format("Allow attempt to relauch command using an elevated"),
								  cxxopts::value(m_AllowElevation),
								  "<allow-elevation>");
#endif	// ZEN_PLATFORM_WINDOWS

	m_StartOptions.add_options()("h,help", "Print help");
	m_StartOptions.add_option("",
							  "n",
							  "name",
							  fmt::format("Service name, defaults to \"{}\"", m_ServiceName),
							  cxxopts::value(m_ServiceName),
							  "<name>");
	m_StartOptions.parse_positional({"name"});
	m_StartOptions.positional_help("name");
#if ZEN_PLATFORM_WINDOWS
	m_StartOptions.add_option("",
							  "",
							  "allow-elevation",
							  fmt::format("Allow attempt to relauch command using an elevated"),
							  cxxopts::value(m_AllowElevation),
							  "<allow-elevation>");
#endif	// ZEN_PLATFORM_WINDOWS

	m_StopOptions.add_options()("h,help", "Print help");
	m_StopOptions.add_option("",
							 "n",
							 "name",
							 fmt::format("Service name, defaults to \"{}\"", m_ServiceName),
							 cxxopts::value(m_ServiceName),
							 "<name>");
	m_StopOptions.parse_positional({"name"});
	m_StopOptions.positional_help("name");
#if ZEN_PLATFORM_WINDOWS
	m_StopOptions.add_option("",
							 "",
							 "allow-elevation",
							 fmt::format("Allow attempt to relauch command using an elevated"),
							 cxxopts::value(m_AllowElevation),
							 "<allow-elevation>");
#endif	// ZEN_PLATFORM_WINDOWS
}

ServiceCommand::~ServiceCommand() = default;

std::string
FmtServiceInfo(const ServiceInfo& Info, std::string_view Prefix)
{
	std::string Result = fmt::format(
		"{}Status:             {}\n"
		"{}Executable:         {}\n"
		"{}CommandLineOptions: {}",
		Prefix,
		ToString(Info.Status),
		Prefix,
		Info.Spec.ExecutablePath,
		Prefix,
		Info.Spec.CommandLineOptions);

#if ZEN_PLATFORM_WINDOWS
	Result += fmt::format(
		"\n"
		"{}Display Name:       {}\n"
		"{}Description:        {}",
		Prefix,
		Info.Spec.DisplayName,
		Prefix,
		Info.Spec.Description);
#endif	// ZEN_PLATFORM_WINDOWS

	return Result;
}

enum class ServiceStatusReturnCode
{
	Running = 0,  // successful return indicates proper installation and everything running smoothly
	NotInstalled,
	NotRunning,
	UnknownError
};

void
ServiceCommand::Run(const ZenCliOptions& GlobalOptions, int argc, char** argv)
{
	using namespace std::literals;

	std::vector<char*> SubCommandArguments;
	cxxopts::Options*  SubOption			 = nullptr;
	int				   ParentCommandArgCount = GetSubCommand(m_Options, argc, argv, m_SubCommands, SubOption, SubCommandArguments);

	if (SubOption == nullptr)
	{
		if (!ParseOptions(ParentCommandArgCount, argv))
		{
			return;
		}
		throw OptionParseException("'verb' option is required", m_Options.help());
	}

	// Parse subcommand permissively - forward unrecognised options to the parent parser.
	std::vector<std::string> SubUnmatched;
	if (!ParseOptionsPermissive(*SubOption, gsl::narrow<int>(SubCommandArguments.size()), SubCommandArguments.data(), SubUnmatched))
	{
		return;
	}

	// Build parent arg list: original parent args (without subcommand name) + forwarded unmatched.
	std::vector<char*> ParentArgs;
	ParentArgs.reserve(static_cast<size_t>(ParentCommandArgCount - 1) + SubUnmatched.size());
	ParentArgs.push_back(argv[0]);
	std::copy(argv + 1, argv + ParentCommandArgCount - 1, std::back_inserter(ParentArgs));
	for (std::string& Arg : SubUnmatched)
	{
		ParentArgs.push_back(Arg.data());
	}

	if (!ParseOptions(static_cast<int>(ParentArgs.size()), ParentArgs.data()))
	{
		return;
	}

	if (SubOption == &m_StatusOptions)
	{
		ServiceInfo		Info;
		std::error_code Ec = QueryInstalledService(m_ServiceName, Info);
		if (Ec)
		{
			throw std::runtime_error(fmt::format("Can't get information about service '{}'. Reason: '{}'", m_ServiceName, Ec.message()));
		}
		if (Info.Status == ServiceStatus::NotInstalled)
		{
			throw std::runtime_error(fmt::format("Service '{}' is not installed", m_ServiceName));
		}
		else if (Info.Status != ServiceStatus::Running)
		{
			throw std::runtime_error(fmt::format("Service '{}' is not running", m_ServiceName));
		}
		else
		{
			ZEN_CONSOLE("Service '{}':\n{}", m_ServiceName, FmtServiceInfo(Info, "  "));
		}
	}

	if (SubOption == &m_InstallOptions)
	{
		if (RequiresElevation())
		{
			RunElevated(m_AllowElevation);
			return;
		}

		ServiceInfo		Info;
		std::error_code Ec = QueryInstalledService(m_ServiceName, Info);
		if (!Ec && Info.Status != ServiceStatus::NotInstalled)
		{
			if (m_FullInstall)
			{
				if (Info.Status == ServiceStatus::Running)
				{
					Ec = StopService(m_ServiceName);
					if (Ec)
					{
						throw std::runtime_error(fmt::format("Failed to stop service '{}' using '{}'. Reason: '{}'",
															 m_ServiceName,
															 m_ServerExecutable,
															 Ec.message()));
					}

					int Timeout = 30000;  // Wait up to 30 seconds for the service to fully stop before uninstalling
					while (Timeout > 0)
					{
						Ec = QueryInstalledService(m_ServiceName, Info);
						if (Ec)
						{
							throw std::runtime_error(fmt::format("Failed to wait for service to stop: '{}'", Ec.message()));
						}

						if (Info.Status == ServiceStatus::Stopped)
						{
							break;
						}

						Sleep(100);
						Timeout -= 100;
					}

					if (Info.Status != ServiceStatus::Stopped)
					{
						throw std::runtime_error("Timed out waiting for service to stop");
					}
				}

				Ec = UninstallService(m_ServiceName);
				if (Ec)
				{
					throw std::runtime_error(fmt::format("Failed to uninstall running service '{}' using '{}'. Reason: '{}'",
														 m_ServiceName,
														 m_ServerExecutable,
														 Ec.message()));
				}
			}
			else
			{
				ZEN_CONSOLE("Service '{}' already installed:\n{}", m_ServiceName, FmtServiceInfo(Info, "  "));
				return;
			}
		}
		if (m_ServerExecutable.empty())
		{
			std::filesystem::path ExePath = zen::GetRunningExecutablePath();
			ExePath.replace_filename("zenserver" + ExePath.extension().string());
			m_ServerExecutable = ExePath;
		}
		m_ServerExecutable = std::filesystem::absolute(m_ServerExecutable);

		if (m_FullInstall)
		{
			if (m_InstallPath.empty())
			{
				throw OptionParseException("'--full' requires '--install-path'", SubOption->help());
			}

			std::filesystem::path ExePath = zen::GetRunningExecutablePath();

			std::filesystem::path FilesToCopy[] = {
				ExePath,
				m_ServerExecutable,
#if ZEN_PLATFORM_WINDOWS
				ExePath.parent_path() / ExePath.stem().replace_extension("pdb"),
				m_ServerExecutable.parent_path() / m_ServerExecutable.stem().replace_extension("pdb"),
				ExePath.parent_path() /
					std::filesystem::path("crashpad_handler.exe")
#endif
#if ZEN_PLATFORM_MAC
						ExePath.parent_path() /
					std::filesystem::path("crashpad_handler")
#endif
			};

			std::filesystem::path DestinationExePath	= m_InstallPath / ExePath.filename();
			std::filesystem::path DestinationServerPath = m_InstallPath / m_ServerExecutable.filename();

			if (!std::filesystem::is_directory(m_InstallPath) && !CreateDirectories(m_InstallPath))
			{
				throw std::runtime_error(fmt::format("Unable to create install directory '{}'", m_InstallPath));
			}

#if ZEN_PLATFORM_LINUX
			uid_t UserId  = 0;
			gid_t GroupId = 0;
			if (!m_UserName.empty())
			{
				struct passwd* Passwd = getpwnam(m_UserName.c_str());
				if (Passwd == NULL)
				{
					throw std::runtime_error(fmt::format("Unable to determine user ID for user '{}'", m_UserName));
				}

				UserId = Passwd->pw_uid;

				struct group* Grp = getgrnam(m_UserName.c_str());
				if (Grp == NULL)
				{
					throw std::runtime_error(fmt::format("Unable to determine group ID for user '{}'", m_UserName));
				}

				GroupId = Grp->gr_gid;
				if (chown(m_InstallPath.c_str(), UserId, GroupId) != 0)
				{
					throw std::runtime_error(
						fmt::format("Unable to set ownership of directory '{}' for user '{}'", m_InstallPath, m_UserName));
				}
			}
#endif

			for (const std::filesystem::path& File : FilesToCopy)
			{
				std::filesystem::path Destination = m_InstallPath / File.filename();

				if (std::error_code CopyEc = CopyFile(File, Destination, {.EnableClone = false}); CopyEc)
				{
					throw std::system_error(CopyEc, fmt::format("Failed to copy '{}' to '{}'", File, Destination));
				}

				ZEN_INFO("Copied '{}' to '{}'", File, Destination);

				if (File.extension() != "pdb")
				{
					std::filesystem::permissions(Destination, std::filesystem::perms::owner_exec, std::filesystem::perm_options::add);
				}

				std::filesystem::permissions(Destination,
											 std::filesystem::perms::owner_write | std::filesystem::perms::group_write,
											 std::filesystem::perm_options::add);

#if ZEN_PLATFORM_LINUX
				if (UserId != 0)
				{
					if (chown(Destination.c_str(), UserId, GroupId) != 0)
					{
						throw std::runtime_error(
							fmt::format("Unable to set ownership of file '{}' for user '{}'", Destination, m_UserName));
					}
				}
#endif
			}

			m_ServerExecutable = m_InstallPath / m_ServerExecutable.filename();
		}

		Ec = InstallService(
			m_ServiceName,
			ServiceSpec {
				.ExecutablePath = m_ServerExecutable, .CommandLineOptions = GlobalOptions.PassthroughCommandLine, .UserName = m_UserName
#if ZEN_PLATFORM_WINDOWS
					,
				.DisplayName = m_ServiceDisplayName, .Description = m_ServiceDescription
#endif	// ZEN_PLATFORM_WINDOWS
			});
		if (Ec)
		{
			throw std::runtime_error(
				fmt::format("Failed to install service '{}' using '{}' . Reason: '{}'", m_ServiceName, m_ServerExecutable, Ec.message()));
		}
		ZEN_CONSOLE("Installed service '{}' using '{}' successfully", m_ServiceName, m_ServerExecutable);

		if (m_FullInstall)
		{
			Ec = StartService(m_ServiceName);
			if (Ec)
			{
				throw std::runtime_error(fmt::format("Failed to start service '{}'. Reason: '{}'", m_ServiceName, Ec.message()));
			}
		}
	}

	if (SubOption == &m_UninstallOptions)
	{
		ServiceInfo		Info;
		std::error_code Ec = QueryInstalledService(m_ServiceName, Info);
		if (Ec)
		{
			throw std::runtime_error(fmt::format("Failed to inspect installed service '{}'. Reason: '{}'", m_ServiceName, Ec.message()));
		}
		if (Info.Status == ServiceStatus::NotInstalled)
		{
			ZEN_CONSOLE("Service '{}' is not installed", m_ServiceName);
			return;
		}
		if (Info.Status != ServiceStatus::Stopped)
		{
			throw std::runtime_error(fmt::format("Service '{}' is running, stop before uninstalling", m_ServiceName));
		}

		if (RequiresElevation())
		{
			RunElevated(m_AllowElevation);
			return;
		}

		Ec = UninstallService(m_ServiceName);
		if (Ec)
		{
			throw std::runtime_error(fmt::format("Failed to uninstall service '{}'. Reason: '{}'", m_ServiceName, Ec.message()));
		}
		ZEN_CONSOLE("Uninstalled service {} successfully", m_ServiceName);
	}

	if (SubOption == &m_StartOptions)
	{
		ServiceInfo		Info;
		std::error_code Ec = QueryInstalledService(m_ServiceName, Info);
		if (Ec)
		{
			throw std::runtime_error(fmt::format("Failed to inspect installed service '{}'. Reason: '{}'", m_ServiceName, Ec.message()));
		}
		if (Info.Status == ServiceStatus::NotInstalled)
		{
			throw std::runtime_error(fmt::format("Service '{}' is not installed", m_ServiceName));
		}
		if (Info.Status != ServiceStatus::Stopped)
		{
			ZEN_CONSOLE("Service '{}' is already running", m_ServiceName);
			return;
		}

		if (RequiresElevation())
		{
			RunElevated(m_AllowElevation);
			return;
		}

		Ec = StartService(m_ServiceName);
		if (Ec)
		{
			throw std::runtime_error(fmt::format("Failed to start service '{}'. Reason: '{}'", m_ServiceName, Ec.message()));
		}
		ZEN_CONSOLE("Started service '{}' successfully", m_ServiceName);
	}

	if (SubOption == &m_StopOptions)
	{
		ServiceInfo		Info;
		std::error_code Ec = QueryInstalledService(m_ServiceName, Info);
		if (Ec)
		{
			throw std::runtime_error(fmt::format("Failed to inspect installed service '{}'. Reason: '{}'", m_ServiceName, Ec.message()));
		}
		if (Info.Status == ServiceStatus::NotInstalled)
		{
			throw std::runtime_error(fmt::format("Service '{}' is not installed", m_ServiceName));
		}
		if (Info.Status != ServiceStatus::Running)
		{
			ZEN_CONSOLE("Service '{}' is not running", m_ServiceName);
			return;
		}

		if (RequiresElevation())
		{
			RunElevated(m_AllowElevation);
			return;
		}

		Ec = StopService(m_ServiceName);
		if (Ec)
		{
			throw std::runtime_error(fmt::format("Failed to stop service '{}'. Reason: '{}'", m_ServiceName, Ec.message()));
		}
		ZEN_CONSOLE("Stopped service '{}' successfully", m_ServiceName);
	}
}

}  // namespace zen