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:41
raw | new post
modification of post by Anonymous (view diff)

  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 <ctype.h>
  37. #include <stdio.h>
  38.  
  39. static int _dprint_level = 2;
  40.  
  41. #define DPRINTF(level, fmt) \
  42.     if ((level) <= _dprint_level) { \
  43.         (void)printf fmt ; \
  44.     }
  45.  
  46. static
  47. bool set_token_privilege(HANDLE tok, const char *seprivname, bool enable_priv)
  48. {
  49.     TOKEN_PRIVILEGES tp;
  50.     LUID luid;
  51.     bool res;
  52.  
  53.     if(!LookupPrivilegeValueA(NULL, seprivname, &luid)) {
  54.         DPRINTF(1, ("set_token_privilege: "
  55.             "LookupPrivilegeValue(seprivname='%s') failed, "
  56.             "status=%d\n",
  57.             seprivname,
  58.             (int)GetLastError()));
  59.         res = false;
  60.         goto out;
  61.     }
  62.  
  63.     tp.PrivilegeCount = 1;
  64.     tp.Privileges[0].Luid = luid;
  65.     tp.Privileges[0].Attributes = enable_priv?(SE_PRIVILEGE_ENABLED):0;
  66.  
  67.     if(!AdjustTokenPrivileges(tok,
  68.         FALSE, &tp, sizeof(TOKEN_PRIVILEGES),
  69.         NULL, NULL)) {
  70.         DPRINTF(1, ("set_token_privilege: "
  71.             "AdjustTokenPrivileges() for '%s' failed, status=%d\n",
  72.             seprivname,
  73.             (int)GetLastError()));
  74.         res = false;
  75.         goto out;
  76.     }
  77.  
  78.     res = true;
  79. out:
  80.     DPRINTF(0,
  81.         ("set_token_privilege(seprivname='%s',enable_priv=%d), res=%d\n",
  82.         seprivname, (int)enable_priv, (int)res));
  83.  
  84.     return res;
  85. }
  86.  
  87. #if 0
  88. static
  89. bool tagvalue_istagvalue(const char *str)
  90. {
  91.     return strstr(str, "=")?true:false;
  92. }
  93. #endif
  94.  
  95. static
  96. bool tagvalue_getsize_tvalue(const char *str, SIZE_T *dest)
  97. {
  98.     const char *s;
  99.    
  100.     s=strstr(str, "=");
  101.     if (s != NULL) {
  102.         s++;
  103.         if (isdigit((int)(*s))) {
  104.             *dest = atoll(s);
  105.             return true;
  106.         }
  107.     }
  108.    
  109.     return false;
  110. }
  111.  
  112. static
  113. bool tagvalue_istag(const char *str, const char *tag)
  114. {
  115.     size_t taglen = strlen(tag);
  116.    
  117.     return strncasecmp(str, tag, taglen)?false:true;
  118. }
  119.  
  120. static
  121. int get_sysfscachesize(void)
  122. {
  123.     SIZE_T minimumFileCacheSize = 0;
  124.     SIZE_T maximumFileCacheSize = 0;
  125.     DWORD flags = 0;
  126.  
  127.     if (GetSystemFileCacheSize(&minimumFileCacheSize,
  128.         &maximumFileCacheSize,
  129.         &flags)) {
  130.         (void)printf("minimumFileCacheSize=%lld\n"
  131.             "maximumFileCacheSize=%lld\n"
  132.             "flags=0x%x\n",
  133.             (long long)minimumFileCacheSize,
  134.             (long long)maximumFileCacheSize,
  135.             (int)flags);
  136.     }
  137.     else {
  138.         (void)fprintf(stderr,
  139.             "get_sysfscachesize: GetSystemFileCacheSize(), lasterr=%d\n",
  140.             (int)GetLastError());
  141.     }
  142.     return 0;
  143. }
  144.  
  145. static
  146. int set_sysfscachesize(int ac, char *av[])
  147. {
  148.     HANDLE proc_token;
  149.     SIZE_T minimumFileCacheSize = 0;
  150.     SIZE_T maximumFileCacheSize = 0;
  151.     DWORD flags = FILE_CACHE_MIN_HARD_ENABLE|FILE_CACHE_MAX_HARD_ENABLE;
  152.  
  153.  
  154.     if (!OpenProcessToken(GetCurrentProcess(),
  155.         TOKEN_QUERY|TOKEN_ADJUST_PRIVILEGES, &proc_token)) {
  156.         (void)fprintf(stderr,
  157.             "set_sysfscachesize: "
  158.             "OpenProcessToken() filed, lasterr=%d\n",
  159.             (int)GetLastError());
  160.         return 1;
  161.     }
  162.  
  163.     (void)set_token_privilege(proc_token,
  164.         "SeIncreaseQuotaPrivilege", true);
  165.  
  166.     int i;
  167.     for (i=2 ; (i < ac) && (av[i] != NULL) ; i++) {
  168.         if (tagvalue_istag(av[i], "minimumfilecachesize=")) {
  169.             (void)tagvalue_getsize_tvalue(av[i], &minimumFileCacheSize);
  170.         }
  171.         else if (tagvalue_istag(av[i], "maximumfilecachesize=")) {
  172.             (void)tagvalue_getsize_tvalue(av[i], &maximumFileCacheSize);
  173.         }
  174.         else {
  175.             (void)fprintf(stderr, "Unknown argument '%s'\n",
  176.                 av[i]);
  177.             return 1;
  178.         }
  179.     }
  180.  
  181.     /*
  182.      * maxsize must be at least 1MB larger than minsize, otherwise
  183.      * we get an "invalid parameter" error in Win10
  184.      */
  185.     if (SetSystemFileCacheSize(
  186.         minimumFileCacheSize,
  187.         maximumFileCacheSize,
  188.         flags)) {
  189.         (void)puts("OK");
  190.     }
  191.     else {
  192.         (void)fprintf(stderr,
  193.             "set_sysfscachesize: "
  194.             "SetSystemFileCacheSize(), lasterr=%d\n",
  195.             (int)GetLastError());
  196.     }
  197.  
  198.     (void)CloseHandle(proc_token);
  199.  
  200.     return 0;
  201. }
  202.  
  203. static
  204. int usage(void)
  205. {
  206.     (void)fprintf(stderr,
  207.         "winsystemfilecachesize: "
  208.         "Get/set Win32 system filesystem cache size\n");
  209.     return 2;
  210. }
  211.  
  212. int main(int ac, char *av[])
  213. {
  214.     const char *subcmd = av[1];
  215.  
  216.     if (!subcmd) {
  217.         return usage();
  218.     }
  219.  
  220.     if (!strcmp(subcmd, "get")) {
  221.         return get_sysfscachesize();
  222.     }
  223.     else if (!strcmp(subcmd, "set")) {
  224.         return set_sysfscachesize(ac, av);
  225.     }
  226.     else {
  227.         (void)fprintf(stderr, "%s: Unknown cmd '%s'\n",
  228.             av[0], subcmd);
  229.         return usage();
  230.     }
  231.  
  232.     return 0;
  233. }

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