Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
adam.huang
htop
Commits
6c2b3b54
Commit
6c2b3b54
authored
Mar 16, 2018
by
wangqr
Committed by
Hisham Muhammad
Apr 06, 2018
Browse files
Use fork/exec instead of popen to run lsof (#757)
Fixes #675
parent
f48f5a10
Changes
1
Show whitespace changes
Inline
Side-by-side
OpenFilesScreen.c
View file @
6c2b3b54
...
@@ -76,16 +76,35 @@ void OpenFilesScreen_draw(InfoScreen* this) {
...
@@ -76,16 +76,35 @@ void OpenFilesScreen_draw(InfoScreen* this) {
}
}
static
OpenFiles_ProcessData
*
OpenFilesScreen_getProcessData
(
pid_t
pid
)
{
static
OpenFiles_ProcessData
*
OpenFilesScreen_getProcessData
(
pid_t
pid
)
{
char
command
[
1025
];
char
buffer
[
1025
];
xSnprintf
(
command
,
1024
,
"lsof -P -p %d -F 2> /dev/null"
,
pid
);
xSnprintf
(
buffer
,
1024
,
"%d"
,
pid
);
FILE
*
fd
=
popen
(
command
,
"r"
);
OpenFiles_ProcessData
*
pdata
=
xCalloc
(
1
,
sizeof
(
OpenFiles_ProcessData
));
OpenFiles_ProcessData
*
pdata
=
xCalloc
(
1
,
sizeof
(
OpenFiles_ProcessData
));
OpenFiles_FileData
*
fdata
=
NULL
;
OpenFiles_FileData
*
fdata
=
NULL
;
OpenFiles_Data
*
item
=
&
(
pdata
->
data
);
OpenFiles_Data
*
item
=
&
(
pdata
->
data
);
if
(
!
fd
)
{
int
fdpair
[
2
];
pdata
->
error
=
127
;
if
(
pipe
(
fdpair
)
==
-
1
)
{
pdata
->
error
=
1
;
return
pdata
;
return
pdata
;
}
}
pid_t
child
=
fork
();
if
(
child
==
-
1
)
{
pdata
->
error
=
1
;
return
pdata
;
}
if
(
child
==
0
)
{
close
(
fdpair
[
0
]);
dup2
(
fdpair
[
1
],
STDOUT_FILENO
);
close
(
fdpair
[
1
]);
int
fdnull
=
open
(
"/dev/null"
,
O_WRONLY
);
if
(
fdnull
<
0
)
exit
(
1
);
dup2
(
fdnull
,
STDERR_FILENO
);
close
(
fdnull
);
execlp
(
"lsof"
,
"lsof"
,
"-P"
,
"-p"
,
buffer
,
"-F"
,
NULL
);
exit
(
127
);
}
close
(
fdpair
[
1
]);
FILE
*
fd
=
fdopen
(
fdpair
[
0
],
"r"
);
for
(;;)
{
for
(;;)
{
char
*
line
=
String_readLine
(
fd
);
char
*
line
=
String_readLine
(
fd
);
if
(
!
line
)
{
if
(
!
line
)
{
...
@@ -105,7 +124,15 @@ static OpenFiles_ProcessData* OpenFilesScreen_getProcessData(pid_t pid) {
...
@@ -105,7 +124,15 @@ static OpenFiles_ProcessData* OpenFilesScreen_getProcessData(pid_t pid) {
item
->
data
[
cmd
]
=
xStrdup
(
line
+
1
);
item
->
data
[
cmd
]
=
xStrdup
(
line
+
1
);
free
(
line
);
free
(
line
);
}
}
pdata
->
error
=
pclose
(
fd
);
int
wstatus
;
if
(
waitpid
(
child
,
&
wstatus
,
0
)
==
-
1
)
{
pdata
->
error
=
1
;
return
pdata
;
}
if
(
!
WIFEXITED
(
wstatus
))
pdata
->
error
=
1
;
else
pdata
->
error
=
WEXITSTATUS
(
wstatus
);
return
pdata
;
return
pdata
;
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment