1 | /* $Id: RTShutdown.cpp 106061 2024-09-16 14:03:52Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT Testcase - System Shutdown.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2012-2024 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * The contents of this file may alternatively be used under the terms
|
---|
26 | * of the Common Development and Distribution License Version 1.0
|
---|
27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
29 | * CDDL are applicable instead of those of the GPL.
|
---|
30 | *
|
---|
31 | * You may elect to license modified versions of this file under the
|
---|
32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
33 | *
|
---|
34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
35 | */
|
---|
36 |
|
---|
37 |
|
---|
38 | /*********************************************************************************************************************************
|
---|
39 | * Header Files *
|
---|
40 | *********************************************************************************************************************************/
|
---|
41 | #include <iprt/system.h>
|
---|
42 |
|
---|
43 | #include <iprt/buildconfig.h>
|
---|
44 | #include <iprt/errcore.h>
|
---|
45 | #include <iprt/getopt.h>
|
---|
46 | #include <iprt/initterm.h>
|
---|
47 | #include <iprt/message.h>
|
---|
48 | #include <iprt/stream.h>
|
---|
49 | #include <iprt/string.h>
|
---|
50 |
|
---|
51 |
|
---|
52 | int main(int argc, char **argv)
|
---|
53 | {
|
---|
54 | int rc = RTR3InitExe(argc, &argv, 0);
|
---|
55 | if (RT_FAILURE(rc))
|
---|
56 | return RTMsgInitFailure(rc);
|
---|
57 |
|
---|
58 | /*
|
---|
59 | * Parse the command line.
|
---|
60 | */
|
---|
61 | static const RTGETOPTDEF s_aOptions[] =
|
---|
62 | {
|
---|
63 | { "--halt", 'H', RTGETOPT_REQ_NOTHING },
|
---|
64 | { "--poweroff", 'p', RTGETOPT_REQ_NOTHING },
|
---|
65 | { "--reboot", 'r', RTGETOPT_REQ_NOTHING },
|
---|
66 | { "--force", 'f', RTGETOPT_REQ_NOTHING },
|
---|
67 | { "--delay", 'd', RTGETOPT_REQ_UINT32 },
|
---|
68 | { "--message", 'm', RTGETOPT_REQ_STRING }
|
---|
69 | };
|
---|
70 |
|
---|
71 | const char *pszMsg = "RTShutdown";
|
---|
72 | RTMSINTERVAL cMsDelay = 0;
|
---|
73 | uint32_t fFlags = RTSYSTEM_SHUTDOWN_POWER_OFF | RTSYSTEM_SHUTDOWN_PLANNED;
|
---|
74 |
|
---|
75 | RTGETOPTSTATE GetState;
|
---|
76 | rc = RTGetOptInit(&GetState, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions), 1,
|
---|
77 | RTGETOPTINIT_FLAGS_OPTS_FIRST);
|
---|
78 | for (;;)
|
---|
79 | {
|
---|
80 | RTGETOPTUNION ValueUnion;
|
---|
81 | rc = RTGetOpt(&GetState, &ValueUnion);
|
---|
82 | if (rc == 0)
|
---|
83 | break;
|
---|
84 | switch (rc)
|
---|
85 | {
|
---|
86 | case 'H': fFlags = (fFlags & ~RTSYSTEM_SHUTDOWN_ACTION_MASK) | RTSYSTEM_SHUTDOWN_HALT; break;
|
---|
87 | case 'p': fFlags = (fFlags & ~RTSYSTEM_SHUTDOWN_ACTION_MASK) | RTSYSTEM_SHUTDOWN_POWER_OFF_HALT; break;
|
---|
88 | case 'r': fFlags = (fFlags & ~RTSYSTEM_SHUTDOWN_ACTION_MASK) | RTSYSTEM_SHUTDOWN_REBOOT; break;
|
---|
89 | case 'f': fFlags |= RTSYSTEM_SHUTDOWN_FORCE; break;
|
---|
90 | case 'd': cMsDelay = ValueUnion.u32; break;
|
---|
91 | case 'm': pszMsg = ValueUnion.psz; break;
|
---|
92 |
|
---|
93 | case 'h':
|
---|
94 | RTPrintf("Usage: RTShutdown [-H|-p|-r] [-f] [-d <milliseconds>] [-m <msg>]\n");
|
---|
95 | return RTEXITCODE_SUCCESS;
|
---|
96 |
|
---|
97 | case 'V':
|
---|
98 | RTPrintf("%sr%d\n", RTBldCfgVersion(), RTBldCfgRevision());
|
---|
99 | return RTEXITCODE_SUCCESS;
|
---|
100 |
|
---|
101 | default:
|
---|
102 | return RTGetOptPrintError(rc, &ValueUnion);
|
---|
103 | }
|
---|
104 | }
|
---|
105 |
|
---|
106 | /*
|
---|
107 | * Do the deed.
|
---|
108 | */
|
---|
109 | rc = RTSystemShutdown(cMsDelay, fFlags, pszMsg);
|
---|
110 | if (RT_FAILURE(rc))
|
---|
111 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "RTSystemShutdown(%u, %#x, \"%s\") returned %Rrc\n", cMsDelay, fFlags, pszMsg, rc);
|
---|
112 | return RTEXITCODE_SUCCESS;
|
---|
113 | }
|
---|
114 |
|
---|