File: halt.c

package info (click to toggle)
nut 2.8.1-5
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 18,284 kB
  • sloc: ansic: 108,785; sh: 9,370; cpp: 3,370; makefile: 2,842; python: 1,029; perl: 763; xml: 47
file content (92 lines) | stat: -rw-r--r-- 1,810 bytes parent folder | download | duplicates (2)
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
/* gcc -mwindows -mno-cygwin -o halt.exe halt.c
NAME
	halt - stopping the system

SYNOPSIS
	halt [-pq]

DESCRIPTION
	The halt utility logs off the current user, flushes the file system
	buffers to disk, stops all processes (non-responsive processes are
	only forced to stop in Windows 2000), and shuts the system down.

	The options are as follows

	-p	Attempt to powerdown the system.  If the powerdown fails, or
		the system does not support software powerdown, the system
		will halt.

	-q	Do not give processes a chance to shut down before halting or
		restarting.  This option should not normally be used.

AUTHOR
	Ben Collver <collver@softhome.net>
	Jim Klimov <jimklimov+nut@gmail.com> - slight adjustments for NUT builds
 */

#include "config.h"	/* should be first */
#include "common.h"

#include <windows.h>

#ifndef EWX_FORCEIFHUNG
#define EWX_FORCEIFHUNG 0x00000010
#endif

int WINAPI WinMain(
	HINSTANCE hInstance,
	HINSTANCE hPrevInstance,
	LPSTR lpCmdLine,
	int nCmdShow)
{
	TOKEN_PRIVILEGES privileges = {1, {{{0, 0}, SE_PRIVILEGE_ENABLED}}};
	HANDLE my_token;
	UINT my_flags;

	NUT_UNUSED_VARIABLE(hInstance);
	NUT_UNUSED_VARIABLE(hPrevInstance);
	NUT_UNUSED_VARIABLE(nCmdShow);

	my_flags = EWX_SHUTDOWN | EWX_FORCEIFHUNG;

	if (strstr(lpCmdLine, "q") != NULL) {
		my_flags |= EWX_FORCE;
	}

	if (strstr(lpCmdLine, "p") != NULL) {
		my_flags |= EWX_POWEROFF;
	}

	if (!LookupPrivilegeValue(
		NULL,
		SE_SHUTDOWN_NAME,
		&privileges.Privileges[0].Luid))
	{
		exit(1);
	}

	if (!OpenProcessToken(
		GetCurrentProcess(),
		TOKEN_ADJUST_PRIVILEGES,
		&my_token))
	{
		exit(2);
	}

	if (!AdjustTokenPrivileges(
		my_token,
		FALSE,
		&privileges,
		sizeof(TOKEN_PRIVILEGES),
		NULL,
		NULL))
	{
		exit(3);
	}

	CloseHandle(my_token);
	if (!ExitWindowsEx(my_flags, 0)) {
		exit(4);
	}
	exit(0);
}