FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
rstudio/src/cpp/core/system/PosixParentProcessMonitor.cpp at master · pearsonca/rstudio · GitHub
pearsonca
/
rstudio
Public
forked from
rstudio/rstudio
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
rstudio
/
src
/
cpp
/
core
/
system
/
PosixParentProcessMonitor.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
110 lines (87 loc) · 2.66 KB
Breadcrumbs
rstudio
/
src
/
cpp
/
core
/
system
/
PosixParentProcessMonitor.cpp
Copy path
File metadata and controls
110 lines (87 loc) · 2.66 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
* PosixParentProcessMonitor.cpp
*
* Copyright (C) 2009-12 by RStudio, PBC
*
* Unless you have received this program directly from RStudio pursuant
* to the terms of a commercial license agreement with RStudio, then
* this program is licensed to you under the terms of version 3 of the
* GNU Affero General Public License. This program is distributed WITHOUT
* ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
* MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
* AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
*
*/
#
include
<
core/system/ParentProcessMonitor.hpp
>
#
include
<
errno.h
>
#
include
<
unistd.h
>
#
include
<
boost/assert.hpp
>
#
include
<
shared_core/SafeConvert.hpp
>
#
include
<
core/Log.hpp
>
namespace
rstudio
{
namespace
core
{
namespace
parent_process_monitor
{
namespace
{
std::vector<
int
> s_writeOnExit;
int
setFdEnv
(std::string name,
int
val)
{
std::string strVal =
safe_convert::numberToString
(val);
return
::
setenv
(name.
c_str
(), strVal.
c_str
(), strVal.
size
());
}
int
getFdEnv
(std::string name,
int
defaultVal)
{
char
* result = ::
getenv
(name.
c_str
());
if
(!result)
return
defaultVal;
return
core::safe_convert::stringTo
(result, defaultVal);
}
void
exitHandler
()
{
//
Signal normal termination to all child processes
//
that may be waiting
for
(
size_t
i =
0
; i < s_writeOnExit.
size
(); i++)
{
//
write to child (don't bother with checking error as there may
//
be one in the case that the child is already gone)
(
void
)::
write
(s_writeOnExit.
at
(i),
"
done
"
,
4
);
}
}
}
//
anonymous namespace
Error
wrapFork
(boost::function<
void
()> func)
{
int
fds[
2
];
int
result = ::
pipe
(fds);
if
(result !=
0
)
return
systemError
(errno,
ERROR_LOCATION
);
result =
setFdEnv
(
"
RS_PPM_FD_READ
"
, fds[
0
]);
if
(result !=
0
)
return
systemError
(errno,
ERROR_LOCATION
);
result =
setFdEnv
(
"
RS_PPM_FD_WRITE
"
, fds[
1
]);
if
(result !=
0
)
return
systemError
(errno,
ERROR_LOCATION
);
func
();
::close
(fds[
0
]);
::atexit
(exitHandler);
s_writeOnExit.
push_back
(fds[
1
]);
return
Success
();
}
ParentTermination
waitForParentTermination
()
{
int
fds[
2
];
fds[
0
] =
getFdEnv
(
"
RS_PPM_FD_READ
"
, -
1
);
fds[
1
] =
getFdEnv
(
"
RS_PPM_FD_WRITE
"
, -
1
);
if
(fds[
0
] <
0
|| fds[
1
] <
0
)
return
ParentTerminationNoParent;
::close
(fds[
1
]);
char
buf[
256
];
int
result = ::
read
(fds[
0
], buf,
256
);
if
(result ==
0
)
return
ParentTerminationAbnormal;
else
if
(result >
0
)
return
ParentTerminationNormal;
else
return
ParentTerminationWaitFailure;
}
}
//
namespace parent_process_monitor
}
//
namespace core
}
//
namespace rstudio
Back
|
FazBrowse Home
|
New Git URL