Skip to content

Commit e3eec08

Browse files
committed
only attach when necessary
Don't attach before we are sure there are patches. Signed-off-by: Pavel Boldin <[email protected]>
1 parent 37daaa6 commit e3eec08

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

src/kpatch_process.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,10 @@ kpatch_process_mem_open(kpatch_process_t *proc, int mode)
622622
{
623623
char path[sizeof("/proc/0123456789/mem")];
624624

625+
if (proc->memfd >= 0) {
626+
close(proc->memfd);
627+
}
628+
625629
snprintf(path, sizeof(path), "/proc/%d/mem", proc->pid);
626630
proc->memfd = open(path, mode == MEM_WRITE ? O_RDWR : O_RDONLY);
627631
if (proc->memfd < 0) {
@@ -883,6 +887,12 @@ kpatch_process_load_libraries(kpatch_process_t *proc)
883887
if (!proc->is_just_started)
884888
return 0;
885889

890+
ret = kpatch_process_attach(proc);
891+
if (ret < 0) {
892+
kperr("unable to attach to just started process\n");
893+
return -1;
894+
}
895+
886896
if (proc->send_fd != -1) {
887897
ret = kpatch_process_kickstart_execve_wrapper(proc);
888898
if (ret < 0) {

src/kpatch_user.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,7 +1010,7 @@ static int process_patch(int pid, void *_data)
10101010

10111011
kpatch_process_print_short(proc);
10121012

1013-
ret = kpatch_process_attach(proc);
1013+
ret = kpatch_process_mem_open(proc, MEM_READ);
10141014
if (ret < 0)
10151015
goto out_free;
10161016

@@ -1030,11 +1030,10 @@ static int process_patch(int pid, void *_data)
10301030
goto out_free;
10311031

10321032
/*
1033-
* For each object file that we want to patch (either binary
1034-
* itself or shared library) we need its ELF structure
1035-
* to perform relocations. Because we know uniq BuildID of
1036-
* the object the section addresses stored in the patch
1037-
* are valid for the original object.
1033+
* For each object file that we want to patch (either binary itself or
1034+
* shared library) we need its ELF structure to perform relocations.
1035+
* Because we know uniq BuildID of the object the section addresses
1036+
* stored in the patch are valid for the original object.
10381037
*/
10391038
ret = kpatch_process_map_object_files(proc);
10401039
if (ret < 0)
@@ -1047,6 +1046,11 @@ static int process_patch(int pid, void *_data)
10471046
if (ret <= 0)
10481047
goto out_free;
10491048

1049+
/* Finally, attach to process */
1050+
ret = kpatch_process_attach(proc);
1051+
if (ret < 0)
1052+
goto out_free;
1053+
10501054
ret = kpatch_coroutines_find(proc);
10511055
if (ret < 0)
10521056
goto out_free;

0 commit comments

Comments
 (0)