changeset 28133:baf16e6f498b

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 4cfe24f56336
children 480490faf659
files libinterp/corefcn/sysdep.cc scripts/pkg/pkg.m
diffstat 2 files changed, 46 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/corefcn/sysdep.cc	Sat Feb 22 17:03:01 2020 +0100
+++ b/libinterp/corefcn/sysdep.cc	Sat Feb 22 17:26:34 2020 +0100
@@ -245,6 +245,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__)
--- a/scripts/pkg/pkg.m	Sat Feb 22 17:03:01 2020 +0100
+++ b/scripts/pkg/pkg.m	Sat Feb 22 17:26:34 2020 +0100
@@ -329,7 +329,7 @@
 
 function [local_packages, global_packages] = pkg (varargin)
 
-  ## Installation prefix (FIXME: what should these be on windows?)
+  ## Installation prefix
   persistent user_prefix = false;
   persistent prefix = false;
   persistent archprefix = -1;
@@ -337,9 +337,13 @@
   persistent global_list = fullfile (OCTAVE_HOME (), "share", "octave",
                                      "octave_packages");
 
-  ## If user is superuser set global_istall to true
-  ## FIXME: is it OK to set this always true on windows?
-  global_install = ((ispc () && ! isunix ()) || (geteuid () == 0));
+  ## If user is superuser (posix) or the process has elevated rights (Windows),
+  ## set global_install to true.
+  if (ispc () && ! isunix ())
+    global_install = __is_elevated_process__ ();
+  else
+    global_install = (geteuid () == 0);
+  endif
 
   if (isbool (prefix))
     [prefix, archprefix] = default_prefix (global_install);