- /*
 - * MIT License
 - *
 - * Copyright (c) 2024 Roland Mainz <roland.mainz@nrubsig.org>
 - *
 - * Permission is hereby granted, free of charge, to any person obtaining a copy
 - * of this software and associated documentation files (the "Software"), to deal
 - * in the Software without restriction, including without limitation the rights
 - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 - * copies of the Software, and to permit persons to whom the Software is
 - * furnished to do so, subject to the following conditions:
 - *
 - * The above copyright notice and this permission notice shall be included in all
 - * copies or substantial portions of the Software.
 - *
 - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 - * SOFTWARE.
 - */
 - /*
 - * winsystemfilecachesize.c - get/set Win32 system file cache
 - *
 - * Written by Roland Mainz <roland.mainz@nrubsig.org>
 - */
 - #include <windows.h>
 - #include <memoryapi.h>
 - #include <securitybaseapi.h>
 - #include <stdlib.h>
 - #include <stdbool.h>
 - #include <stdio.h>
 - static int _dprint_level = 2;
 - #define DPRINTF(level, fmt) \
 - if ((level) <= _dprint_level) { \
 - (void)printf fmt ; \
 - }
 - static
 - bool set_token_privilege(HANDLE tok, const char *seprivname, bool enable_priv)
 - {
 - TOKEN_PRIVILEGES tp;
 - LUID luid;
 - bool res;
 - if(!LookupPrivilegeValueA(NULL, seprivname, &luid)) {
 - DPRINTF(1, ("set_token_privilege: "
 - "LookupPrivilegeValue(seprivname='%s') failed, "
 - "status=%d\n",
 - seprivname,
 - (int)GetLastError()));
 - res = false;
 - goto out;
 - }
 - tp.PrivilegeCount = 1;
 - tp.Privileges[0].Luid = luid;
 - tp.Privileges[0].Attributes = enable_priv?(SE_PRIVILEGE_ENABLED):0;
 - if(!AdjustTokenPrivileges(tok,
 - FALSE, &tp, sizeof(TOKEN_PRIVILEGES),
 - NULL, NULL)) {
 - DPRINTF(1, ("set_token_privilege: "
 - "AdjustTokenPrivileges() for '%s' failed, status=%d\n",
 - seprivname,
 - (int)GetLastError()));
 - res = false;
 - goto out;
 - }
 - res = true;
 - out:
 - DPRINTF(0,
 - ("set_token_privilege(seprivname='%s',enable_priv=%d), res=%d\n",
 - seprivname, (int)enable_priv, (int)res));
 - return res;
 - }
 - static
 - int get_sysfscachesize(void)
 - {
 - SIZE_T minimumFileCacheSize;
 - SIZE_T maximumFileCacheSize;
 - DWORD flags;
 - if (GetSystemFileCacheSize(&minimumFileCacheSize,
 - &maximumFileCacheSize,
 - &flags)) {
 - (void)printf("minimumFileCacheSize=%lld\n"
 - "maximumFileCacheSize=%lld\n"
 - "flags=0x%x\n",
 - (long long)minimumFileCacheSize,
 - (long long)maximumFileCacheSize,
 - (int)flags);
 - }
 - else {
 - (void)fprintf(stderr,
 - "get_sysfscachesize: GetSystemFileCacheSize(), lasterr=%d\n",
 - (int)GetLastError());
 - }
 - return 0;
 - }
 - int main(int ac, char *av[])
 - {
 - if (!strcmp(av[1], "get")) {
 - get_sysfscachesize();
 - }
 - else if (!strcmp(av[1], "set")) {
 - HANDLE proc_token;
 - if (!OpenProcessToken(GetCurrentProcess(),
 - TOKEN_QUERY|TOKEN_ADJUST_PRIVILEGES, &proc_token)) {
 - (void)fprintf(stderr,
 - "set_sysfscachesize: "
 - "OpenProcessToken() filed, lasterr=%d\n",
 - (int)GetLastError());
 - return 1;
 - }
 - (void)set_token_privilege(proc_token,
 - "SeIncreaseQuotaPrivilege", true);
 - if (SetSystemFileCacheSize(
 - 128*1024*1024,
 - 129*1024*1024,
 - FILE_CACHE_MIN_HARD_ENABLE|FILE_CACHE_MAX_HARD_ENABLE)) {
 - (void)puts("OK");
 - }
 - else {
 - (void)fprintf(stderr,
 - "set_sysfscachesize: "
 - "SetSystemFileCacheSize(), lasterr=%d\n",
 - (int)GetLastError());
 - }
 - }
 - return 0;
 - }
 
winsystemfilecachesize.c - get/set Win32 system file cache
Posted by Anonymous on Fri 10th May 2024 14:04
raw | new post
view followups (newest first): winsystemfilecachesize.c - get/set Win32 system file cache by Anonymous
Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.
 nrubsig.kpaste.net RSS