FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
unified-runtime/source/common/ur_util.cpp at main · bashbaug/unified-runtime · GitHub
bashbaug
/
unified-runtime
Public
forked from
oneapi-src/unified-runtime
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
unified-runtime
/
source
/
common
/
ur_util.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
92 lines (80 loc) · 2.72 KB
Breadcrumbs
unified-runtime
/
source
/
common
/
ur_util.cpp
Copy path
File metadata and controls
92 lines (80 loc) · 2.72 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
*
* Copyright (C) 2022-2024 Intel Corporation
*
* Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM Exceptions.
* See LICENSE.TXT
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*
*/
#
include
"
ur_util.hpp
"
#
include
"
logger/ur_logger.hpp
"
#
ifdef
_WIN32
#
include
<
windows.h
>
int
ur_getpid
(
void
) {
return
static_cast
<
int
>(
GetCurrentProcessId
()); }
int
ur_close_fd
(
int
fd) {
return
-
1
; }
int
ur_duplicate_fd
(
int
pid,
int
fd_in) {
//
TODO: find another way to obtain a duplicate of another process's file descriptor
(
void
)pid;
//
unused
(
void
)fd_in;
//
unused
return
-
1
;
}
#
else
#
include
<
sys/syscall.h
>
#
include
<
unistd.h
>
int
ur_getpid
(
void
) {
return
static_cast
<
int
>(
getpid
()); }
int
ur_close_fd
(
int
fd) {
return
close
(fd); }
int
ur_duplicate_fd
(
int
pid,
int
fd_in) {
//
pidfd_getfd(2) is used to obtain a duplicate of another process's file descriptor.
//
Permission to duplicate another process's file descriptor
//
is governed by a ptrace access mode PTRACE_MODE_ATTACH_REALCREDS check (see ptrace(2))
//
that can be changed using the /proc/sys/kernel/yama/ptrace_scope interface.
//
pidfd_getfd(2) is supported since Linux 5.6
//
pidfd_open(2) is supported since Linux 5.3
#
if
defined(__NR_pidfd_open) && defined(__NR_pidfd_getfd)
errno =
0
;
int
pid_fd =
syscall
(__NR_pidfd_open, pid,
0
);
if
(pid_fd == -
1
) {
logger::error
(
"
__NR_pidfd_open
"
);
return
-
1
;
}
int
fd_dup =
syscall
(__NR_pidfd_getfd, pid_fd, fd_in,
0
);
close
(pid_fd);
if
(fd_dup == -
1
) {
logger::error
(
"
__NR_pidfd_getfd
"
);
return
-
1
;
}
return
fd_dup;
#
else
//
TODO: find another way to obtain a duplicate of another process's file descriptor
(
void
)pid;
//
unused
(
void
)fd_in;
//
unused
errno =
ENOTSUP
;
//
unsupported
logger::error
(
"
__NR_pidfd_open or __NR_pidfd_getfd not available
"
);
return
-
1
;
#
endif
/*
defined(__NR_pidfd_open) && defined(__NR_pidfd_getfd)
*/
}
#
endif
/*
_WIN32
*/
std::optional<std::string>
ur_getenv
(
const
char
*name) {
#
if
defined(_WIN32)
constexpr
int
buffer_size =
1024
;
char
buffer[buffer_size];
auto
rc =
GetEnvironmentVariableA
(name, buffer, buffer_size);
if
(
0
!= rc && rc < buffer_size) {
return
std::string
(buffer);
}
else
if
(rc >= buffer_size) {
std::stringstream ex_ss;
ex_ss <<
"
Environment variable
"
<< name <<
"
value too long!
"
<<
"
Maximum length is
"
<< buffer_size -
1
<<
"
characters.
"
;
throw
std::invalid_argument
(ex_ss.
str
());
}
return
std::
nullopt
;
#
else
const
char
*tmp_env =
getenv
(name);
if
(tmp_env !=
nullptr
) {
return
std::string
(tmp_env);
}
else
{
return
std::
nullopt
;
}
#
endif
}
Back
|
FazBrowse Home
|
New Git URL