Skip to content

Commit 20d7b04

Browse files
committed
cmd_patch: pass arguments directly
Signed-off-by: Pavel Boldin <[email protected]>
1 parent 62ad7ba commit 20d7b04

File tree

1 file changed

+29
-55
lines changed

1 file changed

+29
-55
lines changed

src/kpatch_user.c

Lines changed: 29 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -63,19 +63,36 @@ static int usage_patch(const char *err)
6363
{
6464
if (err)
6565
fprintf(stderr, "err: %s\n", err);
66-
fprintf(stderr, "usage: libcare-ctl patch [options] <-p PID> <-r fd> <patch>\n");
66+
fprintf(stderr, "usage: libcare-ctl patch [options] <-p PID> <patch>\n");
6767
fprintf(stderr, "\nOptions:\n");
6868
fprintf(stderr, " -h - this message\n");
69-
fprintf(stderr, " -s - process was just executed\n");
7069
fprintf(stderr, " -p <PID> - target process\n");
71-
fprintf(stderr, " -r fd - fd used with LD_PRELOAD=execve.so.\n");
7270
return err ? 0 : -1;
7371
}
7472

75-
int cmd_patch_user(int argc, char *argv[])
73+
static int
74+
patch_user(const char *storage_path, int pid,
75+
int is_just_started, int send_fd)
7676
{
77+
int ret;
7778
kpatch_storage_t storage;
78-
int opt, pid = -1, is_pid_set = 0, ret, start = 0, send_fd = -1;
79+
80+
ret = storage_init(&storage, storage_path);
81+
if (ret < 0)
82+
return ret;
83+
84+
ret = processes_patch(&storage, pid, is_just_started, send_fd);
85+
86+
storage_free(&storage);
87+
88+
return ret;
89+
}
90+
91+
92+
int cmd_patch_user(int argc, char *argv[])
93+
{
94+
int opt, pid = -1, is_pid_set = 0, ret;
95+
const char *storage_path;
7996

8097
if (argc < 4)
8198
return usage_patch(NULL);
@@ -89,12 +106,6 @@ int cmd_patch_user(int argc, char *argv[])
89106
pid = atoi(optarg);
90107
is_pid_set = 1;
91108
break;
92-
case 'r':
93-
send_fd = atoi(optarg);
94-
break;
95-
case 's':
96-
start = 1;
97-
break;
98109
default:
99110
return usage_patch("unknown option");
100111
}
@@ -109,14 +120,9 @@ int cmd_patch_user(int argc, char *argv[])
109120
if (!kpatch_check_system())
110121
goto out_err;
111122

112-
ret = storage_init(&storage, argv[argc - 1]);
113-
if (ret < 0)
114-
goto out_err;
115-
116-
117-
ret = processes_patch(&storage, pid, start, send_fd);
118-
119-
storage_free(&storage);
123+
storage_path = argv[argc - 1];
124+
ret = patch_user(storage_path, pid,
125+
/* is_just_started */ 0, /* send_fd */ -1);
120126

121127
out_err:
122128
return ret;
@@ -474,41 +480,15 @@ static int
474480
cmd_execve_startup(int fd, int argc, char *argv[], int is_just_started)
475481
{
476482
int rv, pid;
477-
char pid_str[64], send_fd_str[64];
478-
char *patch_pid_argv_execve[] = {
479-
"patch",
480-
"-s",
481-
"-p",
482-
pid_str,
483-
"-r",
484-
send_fd_str,
485-
storage_dir
486-
};
487-
char *patch_pid_argv_startup[] = {
488-
"patch",
489-
"-p",
490-
pid_str,
491-
"-r",
492-
send_fd_str,
493-
storage_dir
494-
};
495483

496484
rv = sscanf(argv[1], "%d", &pid);
497485
if (rv != 1) {
498486
kperr("can't parse pid from %s", argv[1]);
499487
return -1;
500488
}
501489

502-
sprintf(pid_str, "%d", pid);
503-
sprintf(send_fd_str, "%d", fd);
504-
505490
optind = 1;
506-
if (is_just_started)
507-
rv = cmd_patch_user(ARRAY_SIZE(patch_pid_argv_execve),
508-
patch_pid_argv_execve);
509-
else
510-
rv = cmd_patch_user(ARRAY_SIZE(patch_pid_argv_startup),
511-
patch_pid_argv_startup);
491+
rv = patch_user(storage_dir, pid, is_just_started, fd);
512492

513493
if (rv < 0)
514494
kperr("can't patch pid %d\n", pid);
@@ -578,15 +558,9 @@ cmd_storage(int argc, char *argv[])
578558
static int
579559
cmd_update(int argc, char *argv[])
580560
{
581-
char *patch_all[] = {
582-
"patch",
583-
"-p",
584-
"all",
585-
storage_dir
586-
};
587-
588-
optind = 1;
589-
return cmd_patch_user(ARRAY_SIZE(patch_all), patch_all);
561+
return patch_user(storage_dir, /* pid */ -1,
562+
/* is_just_started */ 0,
563+
/* send_fd */ -1);
590564
}
591565

592566
static int

0 commit comments

Comments
 (0)