pastebin - collaborative debugging tool
nrubsig.kpaste.net RSS


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

  1. /*
  2.  * MIT License
  3.  *
  4.  * Copyright (c) 2024 Roland Mainz <roland.mainz@nrubsig.org>
  5.  *
  6.  * Permission is hereby granted, free of charge, to any person obtaining a copy
  7.  * of this software and associated documentation files (the "Software"), to deal
  8.  * in the Software without restriction, including without limitation the rights
  9.  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10.  * copies of the Software, and to permit persons to whom the Software is
  11.  * furnished to do so, subject to the following conditions:
  12.  *
  13.  * The above copyright notice and this permission notice shall be included in all
  14.  * copies or substantial portions of the Software.
  15.  *
  16.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17.  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19.  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20.  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21.  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22.  * SOFTWARE.
  23.  */
  24.  
  25. /*
  26.  * winsystemfilecachesize.c - get/set Win32 system file cache
  27.  *
  28.  * Written by Roland Mainz <roland.mainz@nrubsig.org>
  29.  */
  30.  
  31. #include <windows.h>
  32. #include <memoryapi.h>
  33. #include <securitybaseapi.h>
  34. #include <stdlib.h>
  35. #include <stdbool.h>
  36. #include <stdio.h>
  37.  
  38. static int _dprint_level = 2;
  39.  
  40. #define DPRINTF(level, fmt) \
  41.     if ((level) <= _dprint_level) { \
  42.         (void)printf fmt ; \
  43.     }
  44.  
  45. static
  46. bool set_token_privilege(HANDLE tok, const char *seprivname, bool enable_priv)
  47. {
  48.     TOKEN_PRIVILEGES tp;
  49.     LUID luid;
  50.     bool res;
  51.  
  52.     if(!LookupPrivilegeValueA(NULL, seprivname, &luid)) {
  53.         DPRINTF(1, ("set_token_privilege: "
  54.             "LookupPrivilegeValue(seprivname='%s') failed, "
  55.             "status=%d\n",
  56.             seprivname,
  57.             (int)GetLastError()));
  58.         res = false;
  59.         goto out;
  60.     }
  61.  
  62.     tp.PrivilegeCount = 1;
  63.     tp.Privileges[0].Luid = luid;
  64.     tp.Privileges[0].Attributes = enable_priv?(SE_PRIVILEGE_ENABLED):0;
  65.  
  66.     if(!AdjustTokenPrivileges(tok,
  67.         FALSE, &tp, sizeof(TOKEN_PRIVILEGES),
  68.         NULL, NULL)) {
  69.         DPRINTF(1, ("set_token_privilege: "
  70.             "AdjustTokenPrivileges() for '%s' failed, status=%d\n",
  71.             seprivname,
  72.             (int)GetLastError()));
  73.         res = false;
  74.         goto out;
  75.     }
  76.  
  77.     res = true;
  78. out:
  79.     DPRINTF(0,
  80.         ("set_token_privilege(seprivname='%s',enable_priv=%d), res=%d\n",
  81.         seprivname, (int)enable_priv, (int)res));
  82.  
  83.     return res;
  84. }
  85.  
  86. static
  87. int get_sysfscachesize(void)
  88. {
  89.     SIZE_T minimumFileCacheSize;
  90.     SIZE_T maximumFileCacheSize;
  91.     DWORD flags;
  92.  
  93.     if (GetSystemFileCacheSize(&minimumFileCacheSize,
  94.         &maximumFileCacheSize,
  95.         &flags)) {
  96.         (void)printf("minimumFileCacheSize=%lld\n"
  97.             "maximumFileCacheSize=%lld\n"
  98.             "flags=0x%x\n",
  99.             (long long)minimumFileCacheSize,
  100.             (long long)maximumFileCacheSize,
  101.             (int)flags);
  102.     }
  103.     else {
  104.         (void)fprintf(stderr,
  105.             "get_sysfscachesize: GetSystemFileCacheSize(), lasterr=%d\n",
  106.             (int)GetLastError());
  107.     }
  108.     return 0;
  109. }
  110.  
  111.  
  112. int main(int ac, char *av[])
  113. {
  114.     if (!strcmp(av[1], "get")) {
  115.         get_sysfscachesize();
  116.     }
  117.     else if (!strcmp(av[1], "set")) {
  118.         HANDLE proc_token;
  119.  
  120.         if (!OpenProcessToken(GetCurrentProcess(),
  121.             TOKEN_QUERY|TOKEN_ADJUST_PRIVILEGES, &proc_token)) {
  122.             (void)fprintf(stderr,
  123.                 "set_sysfscachesize: "
  124.                 "OpenProcessToken() filed, lasterr=%d\n",
  125.                 (int)GetLastError());
  126.             return 1;
  127.         }
  128.  
  129.         (void)set_token_privilege(proc_token,
  130.             "SeIncreaseQuotaPrivilege", true);
  131.  
  132.         if (SetSystemFileCacheSize(
  133.             128*1024*1024,
  134.             129*1024*1024,
  135.             FILE_CACHE_MIN_HARD_ENABLE|FILE_CACHE_MAX_HARD_ENABLE)) {
  136.             (void)puts("OK");
  137.         }
  138.         else {
  139.             (void)fprintf(stderr,
  140.                 "set_sysfscachesize: "
  141.                 "SetSystemFileCacheSize(), lasterr=%d\n",
  142.                 (int)GetLastError());
  143.         }
  144.     }
  145.    
  146.     return 0;
  147. }

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.

Syntax highlighting:

To highlight particular lines, prefix each line with {%HIGHLIGHT}




All content is user-submitted.
The administrators of this site (kpaste.net) are not responsible for their content.
Abuse reports should be emailed to us at