# HG changeset patch # User Markus Mützel # Date 1582388794 -3600 # Node ID ec77c790fce2e911362c8620e08aba8e0c019fcc # Parent ae94e3fad6d45780cca585a23cd3cd601112291f 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. diff -r ae94e3fad6d4 -r ec77c790fce2 libinterp/corefcn/sysdep.cc --- 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__) diff -r ae94e3fad6d4 -r ec77c790fce2 scripts/pkg/pkg.m --- a/scripts/pkg/pkg.m Fri May 01 00:51:01 2020 -0400 +++ 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);