aboutsummaryrefslogtreecommitdiff
path: root/ports/winnt/libntp/setpriority.c
blob: 52ab785294228b0ac3c8598f8bb423c1a206aa2f (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
#include <config.h>
#include <stdio.h>
#include <sys/resource.h>	/* our private version */
#include "ntp_stdlib.h"
#include "ntp_syslog.h"
#include "ntp_debug.h"
#include "ntp_fp.h"
#include "ntp.h"
#include "clockstuff.h"


/*
 * setpriority
 *
 * to reduce the #ifdef forest in the portable code,
 * we emulate the BSD setpriority interface:
 *
 * 		if (-1 == setpriority(PRIO_PROCESS, 0, NTP_PRIO))
 * 			msyslog(LOG_ERR, "setpriority() error: %m");
 *
 * However, since the Windows port of ntpd has always raised its
 * priority (to realtime if allowed, or silently downgraded to 
 * high by the system if not) with or without -N.  Changing that
 * now would endanger users who upgrade the binary without adding
 * -N to its invocation.  Instsrv assumes ntpd.exe is installed
 * with no command-line arguments.
 *
 * This routine is used by utilities as well as ntpd itself, so
 * it checks if the priority is already high or realtime and 
 * logs no complaints in that case, to avoid duplicating.  ntpd
 * will have raised the priority to one of those in
 * init_winnt_time, while the utilities will rely on this
 * code.
 *
 */

int setpriority(
		int which,
		int who,
		int prio
		)
{
	BOOL success;
	DWORD prio_class;

	if (PRIO_PROCESS != which || who || NTP_PRIO != prio)
		TRACE(1, ("windows setpriority() clone needs work.\n"));

	prio_class = GetPriorityClass(GetCurrentProcess());
	
	if (HIGH_PRIORITY_CLASS == prio_class ||
	    REALTIME_PRIORITY_CLASS == prio_class)
		return 0;

	success = SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS);

	if (!success) {
		msyslog(LOG_ERR, "Unable to raise priority: %m"); 
		errno = EPERM;
		return -1;
	}

	prio_class = GetPriorityClass(GetCurrentProcess());

	if (REALTIME_PRIORITY_CLASS == prio_class)
		msyslog(LOG_INFO, "Raised to realtime priority class");
	else if (HIGH_PRIORITY_CLASS == prio_class)
		msyslog(LOG_ERR,  "Raised to high priority class, realtime "
				  "requires Increase Scheduling Priority "
				  "privilege (enabled with secpol.msc).");
	else
		msyslog(LOG_ERR,  "Unexpected process priority class %d",
				 prio_class);

	return 0; 
}