Annotation of embedaddon/ntp/ports/winnt/libntp/setpriority.c, revision 1.1

1.1     ! misho       1: #include <config.h>
        !             2: #include <windows.h>
        !             3: #include <stdio.h>
        !             4: #include <sys/resource.h>      /* our private version */
        !             5: #include "ntp_machine.h"
        !             6: #include "ntp_stdlib.h"
        !             7: #include "ntp_syslog.h"
        !             8: #include "ntp_debug.h"
        !             9: #include "ntp_fp.h"
        !            10: #include "ntp.h"
        !            11: #include "clockstuff.h"
        !            12: 
        !            13: 
        !            14: /*
        !            15:  * setpriority
        !            16:  *
        !            17:  * to reduce the #ifdef forest in the portable code,
        !            18:  * we emulate the BSD setpriority interface:
        !            19:  *
        !            20:  *             if (-1 == setpriority(PRIO_PROCESS, 0, NTP_PRIO))
        !            21:  *                     msyslog(LOG_ERR, "setpriority() error: %m");
        !            22:  *
        !            23:  * However, since the Windows port of ntpd has always raised its
        !            24:  * priority (to realtime if allowed, or silently downgraded to 
        !            25:  * high by the system if not) with or without -N.  Changing that
        !            26:  * now would endanger users who upgrade the binary without adding
        !            27:  * -N to its invocation.  Instsrv assumes ntpd.exe is installed
        !            28:  * with no command-line arguments.
        !            29:  *
        !            30:  * This routine is used by utilities as well as ntpd itself, so
        !            31:  * it checks if the priority is already high or realtime and 
        !            32:  * logs no complaints in that case, to avoid duplicating.  ntpd
        !            33:  * will have raised the priority to one of those in
        !            34:  * init_winnt_time, while the utilities will rely on this
        !            35:  * code.
        !            36:  *
        !            37:  */
        !            38: 
        !            39: int setpriority(
        !            40:                int which,
        !            41:                int who,
        !            42:                int prio
        !            43:                )
        !            44: {
        !            45:        BOOL success;
        !            46:        DWORD prio_class;
        !            47: 
        !            48:        if (PRIO_PROCESS != which || who || NTP_PRIO != prio) {
        !            49:                DPRINTF(1,("windows setpriority() clone needs work.\n"));
        !            50:        }
        !            51: 
        !            52:        prio_class = GetPriorityClass(GetCurrentProcess());
        !            53:        
        !            54:        if (HIGH_PRIORITY_CLASS == prio_class ||
        !            55:            REALTIME_PRIORITY_CLASS == prio_class)
        !            56:                return 0;
        !            57: 
        !            58:        success = SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS);
        !            59: 
        !            60:        if (!success) {
        !            61:                msyslog(LOG_ERR, "Unable to raise priority: %m"); 
        !            62:                errno = EPERM;
        !            63:                return -1;
        !            64:        }
        !            65: 
        !            66:        prio_class = GetPriorityClass(GetCurrentProcess());
        !            67: 
        !            68:        if (REALTIME_PRIORITY_CLASS == prio_class)
        !            69:                msyslog(LOG_INFO, "Raised to realtime priority class");
        !            70:        else if (HIGH_PRIORITY_CLASS == prio_class)
        !            71:                msyslog(LOG_ERR,  "Raised to high priority class, realtime "
        !            72:                                  "requires Increase Scheduling Priority "
        !            73:                                  "privilege (enabled with secpol.msc).");
        !            74:        else
        !            75:                msyslog(LOG_ERR,  "Unexpected process priority class %d",
        !            76:                                 prio_class);
        !            77: 
        !            78:        return 0; 
        !            79: }
        !            80: 
        !            81: /*
        !            82:  * InitSockets -- once known as Win32InitSockets()
        !            83:  *
        !            84:  * This doesn't have much to do with setpriority but we
        !            85:  * want the routine in libntp and this is a convenient
        !            86:  * existing Windows-only libntp source file.
        !            87:  */
        !            88: void
        !            89: InitSockets(
        !            90:        void
        !            91:        )
        !            92: {
        !            93:        WORD wVersionRequested;
        !            94:        WSADATA wsaData;
        !            95:        int err;
        !            96: 
        !            97:        /* Need Winsock 2.0 or better */
        !            98:        wVersionRequested = MAKEWORD(2, 0);
        !            99:  
        !           100:        err = WSAStartup(wVersionRequested, &wsaData);
        !           101:        if ( err != 0 ) {
        !           102:                fprintf(stderr, "No useable winsock.dll: %s\n", strerror(err));
        !           103:                SetLastError(err);
        !           104:                msyslog(LOG_ERR, "No usable winsock.dll: %m");
        !           105:                exit(1);
        !           106:        }
        !           107: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>