- /*
- * 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 <ctype.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;
- }
- #if 0
- static
- bool tagvalue_istagvalue(const char *str)
- {
- }
- #endif
- static
- bool tagvalue_getsize_tvalue(const char *str, SIZE_T *dest)
- {
- const char *s;
- if (s != NULL) {
- s++;
- *dest = atoll(s);
- return true;
- }
- }
- return false;
- }
- static
- bool tagvalue_istag(const char *str, const char *tag)
- {
- return strncasecmp(str, tag, taglen)?false:true;
- }
- static
- int get_sysfscachesize(void)
- {
- SIZE_T minimumFileCacheSize = 0;
- SIZE_T maximumFileCacheSize = 0;
- DWORD flags = 0;
- if (GetSystemFileCacheSize(&minimumFileCacheSize,
- &maximumFileCacheSize,
- &flags)) {
- "maximumFileCacheSize=%lld\n"
- "flags=0x%x\n",
- (long long)minimumFileCacheSize,
- (long long)maximumFileCacheSize,
- (int)flags);
- }
- else {
- "get_sysfscachesize: GetSystemFileCacheSize(), lasterr=%d\n",
- (int)GetLastError());
- }
- return 0;
- }
- static
- int set_sysfscachesize(int ac, char *av[])
- {
- HANDLE proc_token;
- SIZE_T minimumFileCacheSize = 0;
- SIZE_T maximumFileCacheSize = 0;
- DWORD flags = FILE_CACHE_MIN_HARD_ENABLE|FILE_CACHE_MAX_HARD_ENABLE;
- if (!OpenProcessToken(GetCurrentProcess(),
- TOKEN_QUERY|TOKEN_ADJUST_PRIVILEGES, &proc_token)) {
- "set_sysfscachesize: "
- "OpenProcessToken() filed, lasterr=%d\n",
- (int)GetLastError());
- return 1;
- }
- (void)set_token_privilege(proc_token,
- "SeIncreaseQuotaPrivilege", true);
- int i;
- for (i=2 ; (i < ac) && (av[i] != NULL) ; i++) {
- if (tagvalue_istag(av[i], "minimumfilecachesize=")) {
- (void)tagvalue_getsize_tvalue(av[i], &minimumFileCacheSize);
- }
- else if (tagvalue_istag(av[i], "maximumfilecachesize=")) {
- (void)tagvalue_getsize_tvalue(av[i], &maximumFileCacheSize);
- }
- else {
- av[i]);
- return 1;
- }
- }
- /*
- * maxsize must be at least 1MB larger than minsize, otherwise
- * we get an "invalid parameter" error in Win10
- */
- if (SetSystemFileCacheSize(
- minimumFileCacheSize,
- maximumFileCacheSize,
- flags)) {
- }
- else {
- "set_sysfscachesize: "
- "SetSystemFileCacheSize(), lasterr=%d\n",
- (int)GetLastError());
- }
- (void)CloseHandle(proc_token);
- return 0;
- }
- static
- int usage(void)
- {
- "winsystemfilecachesize: "
- "Get/set Win32 system filesystem cache size\n");
- return 2;
- }
- int main(int ac, char *av[])
- {
- const char *subcmd = av[1];
- if (!subcmd) {
- return usage();
- }
- return get_sysfscachesize();
- }
- return set_sysfscachesize(ac, av);
- }
- else {
- av[0], subcmd);
- return usage();
- }
- return 0;
- }
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)
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.