diff libinterp/corefcn/sysdep.cc @ 28260:ec77c790fce2 stable

pkg.m: Install packages globally if process has elevated rights (bug #44548). * pkg.m: On Windows, default to install packages globally if process is running with elevated rights. Otherwise, install packages locally. * sysdep.cc (F__is_elevated_process__): New function that checks if Octave is running as a Windows process with elevated rights.
author Markus Mützel <markus.muetzel@gmx.de>
date Sat, 22 Feb 2020 17:26:34 +0100
parents b895daca20e2
children 062f93e05658 0a5b15007766
line wrap: on
line diff
--- a/libinterp/corefcn/sysdep.cc	Fri May 01 00:51:01 2020 -0400
+++ b/libinterp/corefcn/sysdep.cc	Sat Feb 22 17:26:34 2020 +0100
@@ -244,6 +244,44 @@
 #endif
 }
 
+DEFUN (__is_elevated_process__, args, ,
+       doc: /* -*- texinfo -*-
+@deftypefn  {} {@var{retval} =} __is_elevated_process__ ()
+Check if current process has elevated rights.
+
+On Windows, return true if the current process has elevated right. Otherwise,
+return false.
+On non-Windows platforms, this function fails with an error.
+@end deftypefn */)
+{
+#if defined (OCTAVE_USE_WINDOWS_API)
+  if (args.length () != 0)
+    print_usage ();
+
+  bool retval = false;
+  HANDLE h_token = nullptr;
+
+  if (OpenProcessToken (GetCurrentProcess (), TOKEN_QUERY, &h_token))
+    {
+      TOKEN_ELEVATION elevation;
+      DWORD return_length = sizeof (TOKEN_ELEVATION);
+      if (GetTokenInformation (h_token, TokenElevation, &elevation,
+                               sizeof (elevation), &return_length))
+        retval = elevation.TokenIsElevated;
+    }
+
+  if (h_token)
+    CloseHandle (h_token);
+
+  return ovl (retval);
+
+#else
+  octave_unused_parameter (args);
+  error ("__is_elevated_process__: "
+         "Function is only supported on Windows platforms.");
+#endif
+}
+
 namespace octave
 {
 #if defined (__MINGW32__)