Skip to content

Commit f299b47

Browse files
committed
Oct: is_admin()
1 parent 8453ede commit f299b47

File tree

9 files changed

+63
-32
lines changed

9 files changed

+63
-32
lines changed

+stdlib/is_admin.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
11
%% IS_ADMIN is the process run as root / admin (requires MEX)
2+
3+
function is_admin()
4+
error("need to 'buildtool mex' or 'legacy_mex_build()' first")
5+
end

+stdlib/set_permissions.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,7 @@
99
% * ok (1,1) logical
1010
%
1111
% This function is written in C++ using STL <filesystem> and is only available in Matlab.
12+
13+
function set_permissions(~, ~, ~, ~)
14+
error("need to 'buildtool mex' or 'legacy_mex_build()' first")
15+
end

octave_build.m

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
%% specific source
1111
srcs = {
1212
fullfile(d, "is_rosetta.cpp"), ...
13+
fullfile(d, "is_admin.cpp"), ...
1314
};
1415

1516

@@ -18,5 +19,6 @@
1819
src = s{1};
1920
[~, n] = fileparts(src);
2021

21-
mkoctfile(src,"-v", ["-I", inc], "--output", fullfile(t, n))
22+
disp(["mkoctfile: ", src, " => ", n, ".oct"])
23+
mkoctfile(src, ["-I", inc], "--output", fullfile(t, n))
2224
end

private/get_mex_sources.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717

1818
srcs = {
19-
fullfile(top, "src/is_admin.cpp"), ...
19+
[fullfile(top, "src/is_admin.cpp"), fullfile(top, "src/admin_fs.cpp")] ...
2020
fullfile(top, "src/is_char_device.cpp"), ...
2121
fullfile(top, "src/set_permissions.cpp"), ...
2222
[fullfile(top, "src/is_rosetta.cpp"), mac], ...

src/admin_fs.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#if defined(_WIN32)
2+
#define WIN32_LEAN_AND_MEAN
3+
#include <windows.h> // GetTokenInformation
4+
#else
5+
#include <unistd.h> // geteuid
6+
#include <sys/types.h> // geteuid, pid_t
7+
#endif
8+
9+
#include "admin_fs.h"
10+
11+
12+
bool fs_is_admin(){
13+
// running as admin / root / superuser
14+
#if defined(_WIN32)
15+
HANDLE hToken = nullptr;
16+
TOKEN_ELEVATION elevation;
17+
DWORD dwSize;
18+
19+
const bool ok = (OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken) &&
20+
GetTokenInformation(hToken, TokenElevation, &elevation, sizeof(elevation), &dwSize));
21+
22+
if(hToken)
23+
CloseHandle(hToken);
24+
if(ok)
25+
return elevation.TokenIsElevated;
26+
27+
return false;
28+
#else
29+
return geteuid() == 0;
30+
#endif
31+
}

src/admin_fs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
bool fs_is_admin();

src/is_admin.cpp

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,7 @@
44
#include <vector>
55
#include <memory>
66

7-
#if defined(_WIN32)
8-
#define WIN32_LEAN_AND_MEAN
9-
#include <windows.h> // GetTokenInformation
10-
#else
11-
#include <unistd.h> // geteuid
12-
#include <sys/types.h> // geteuid, pid_t
13-
#endif
14-
15-
16-
static bool fs_is_admin(){
17-
// running as admin / root / superuser
18-
#if defined(_WIN32)
19-
HANDLE hToken = nullptr;
20-
TOKEN_ELEVATION elevation;
21-
DWORD dwSize;
22-
23-
const bool ok = (OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken) &&
24-
GetTokenInformation(hToken, TokenElevation, &elevation, sizeof(elevation), &dwSize));
25-
26-
if(hToken)
27-
CloseHandle(hToken);
28-
if(ok)
29-
return elevation.TokenIsElevated;
30-
31-
return false;
32-
#else
33-
return geteuid() == 0;
34-
#endif
35-
}
7+
#include "admin_fs.h"
368

379

3810
class MexFunction : public matlab::mex::Function {

src/octave/is_admin.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include <octave/oct.h>
2+
3+
#include "admin_fs.cpp"
4+
5+
6+
DEFUN_DLD (is_admin, args, nargout,
7+
"is the process running as admin / root / superuser")
8+
{
9+
if (args.length() != 0){
10+
octave_stdout << "Oct: No input required\n";
11+
return octave_value(false);
12+
}
13+
14+
bool y = fs_is_admin();
15+
16+
return octave_value(y);
17+
}

src/octave/is_rosetta.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55

66
DEFUN_DLD (is_rosetta, args, nargout,
7-
"Hello World Help String")
7+
"is the process running under macOS Rosetta")
88
{
99
if (args.length() != 0){
1010
octave_stdout << "is_rosetta: No input required\n";

0 commit comments

Comments
 (0)