加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
0001-Split-kpatch_storage.c-from-kpatch_user.c.patch 25.36 KB
一键复制 编辑 原始数据 按行查看 历史
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149
From 252f1bdb9a5ab1c1793e213f97389d79e55838b3 Mon Sep 17 00:00:00 2001
From: Roman Rashchupkin <rrashchupkin@cloudlinux.com>
Date: Wed, 17 Jan 2018 13:34:50 +0300
Subject: [PATCH 01/89] Split kpatch_storage.c from kpatch_user.c
---
src/Makefile | 4 +-
src/kpatch_storage.c | 488 +++++++++++++++++++++++++++++++++++++++++++
src/kpatch_storage.h | 66 ++++++
src/kpatch_user.c | 482 +-----------------------------------------
src/kpatch_user.h | 39 ----
5 files changed, 557 insertions(+), 522 deletions(-)
create mode 100644 src/kpatch_storage.c
create mode 100644 src/kpatch_storage.h
diff --git a/src/Makefile b/src/Makefile
index 72ec073..c008535 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -35,8 +35,8 @@ kpatch_make: kpatch_make.o
LIBUNWIND_LIBS := $(shell pkg-config --libs libunwind libunwind-ptrace)
-libcare-ctl: kpatch_user.o kpatch_elf.o kpatch_ptrace.o kpatch_coro.o rbtree.o kpatch_log.o
-libcare-ctl: kpatch_process.o kpatch_common.o
+libcare-ctl: kpatch_user.o kpatch_storage.o kpatch_elf.o kpatch_ptrace.o kpatch_coro.o
+libcare-ctl: kpatch_process.o kpatch_common.o rbtree.o kpatch_log.o
libcare-ctl: LDLIBS += -lelf -lrt $(LIBUNWIND_LIBS)
libcare-client: libcare-client.o
diff --git a/src/kpatch_storage.c b/src/kpatch_storage.c
new file mode 100644
index 0000000..a466460
--- /dev/null
+++ b/src/kpatch_storage.c
@@ -0,0 +1,488 @@
+#include <stdlib.h>
+#include <ctype.h>
+#include <unistd.h>
+#include <errno.h>
+#include <string.h>
+#include <dirent.h>
+#include <regex.h>
+#include <sys/fcntl.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+#include <gelf.h>
+
+#include "kpatch_storage.h"
+#include "kpatch_file.h"
+#include "kpatch_common.h"
+#include "kpatch_elf.h"
+#include "kpatch_ptrace.h"
+#include "list.h"
+#include "kpatch_log.h"
+
+
+/*****************************************************************************
+ * Patch storage subroutines.
+ ****************************************************************************/
+
+static int
+patch_file_verify(struct kp_file *kpfile)
+{
+ GElf_Ehdr *hdr;
+ struct kpatch_file *k = kpfile->patch;
+ ssize_t size = kpfile->size;
+
+ kpdebug("Verifying patch for '%s'...", k->modulename);
+
+ if (memcmp(k->magic, KPATCH_FILE_MAGIC1, sizeof(k->magic))) {
+ kperr("'%s' patch is invalid: Invalid magic.\n",
+ k->modulename);
+ return -1;
+ }
+ if (k->total_size > size) {
+ kperr("'%s' patch is invalid: Invalid size: %u/%ld.\n",
+ k->modulename, k->total_size, size);
+ return -1;
+ }
+ hdr = (void *)k + k->kpatch_offset;
+ if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) ||
+ hdr->e_type != ET_REL ||
+ hdr->e_shentsize != sizeof(GElf_Shdr)) {
+ kperr("'%s' patch is invalid: Wrong ELF header or not ET_REL\n",
+ k->modulename);
+ return -1;
+ }
+ kpdebug("OK\n");
+ return 1;
+}
+
+int storage_init(kpatch_storage_t *storage,
+ const char *fname)
+{
+ int patch_fd = -1;
+ struct stat stat = { .st_mode = 0 };
+
+ if (fname != NULL) {
+ patch_fd = open(fname, O_RDONLY | O_CLOEXEC);
+ if (patch_fd < 0)
+ goto out_err;
+
+ if (fstat(patch_fd, &stat) < 0)
+ goto out_close;
+ }
+
+ storage->patch_fd = patch_fd;
+ storage->is_patch_dir = S_ISDIR(stat.st_mode);
+ storage->path = NULL;
+
+ if (storage->is_patch_dir) {
+ rb_init(&storage->tree);
+ } else {
+ int ret;
+
+ ret = kpatch_open_fd(storage->patch_fd, &storage->patch.kpfile);
+ if (ret < 0)
+ goto out_close;
+
+ ret = patch_file_verify(&storage->patch.kpfile);
+ if (ret < 0) {
+ kpatch_close_file(&storage->patch.kpfile);
+ goto out_close;
+ }
+ strcpy(storage->patch.buildid, storage->patch.kpfile.patch->uname);
+ }
+
+ storage->path = strdup(fname);
+
+ return 0;
+
+out_close:
+ close(patch_fd);
+out_err:
+ kplogerror("cannot open storage '%s'\n", fname);
+ return -1;
+}
+
+static void
+free_storage_patch_cb(struct rb_node *node)
+{
+ struct kpatch_storage_patch *patch;
+
+ patch = rb_entry(node, struct kpatch_storage_patch, node);
+ kpatch_close_file(&patch->kpfile);
+
+ free(patch->desc);
+ free(patch);
+}
+
+void storage_free(kpatch_storage_t *storage)
+{
+ close(storage->patch_fd);
+ if (storage->is_patch_dir)
+ rb_destroy(&storage->tree, free_storage_patch_cb);
+ free(storage->path);
+}
+
+static int
+cmp_buildid(struct rb_node *node, unsigned long key)
+{
+ const char *bid = (const char *)key;
+ struct kpatch_storage_patch *patch;
+
+ patch = rb_entry(node, struct kpatch_storage_patch, node);
+
+ return strcmp(patch->buildid, bid);
+}
+
+#define PATCHLEVEL_TEMPLATE_NUM 0
+
+static char *pathtemplates[] = {
+ "%s/latest/kpatch.bin",
+ "%s.kpatch"
+};
+
+static int
+readlink_patchlevel(int dirfd, const char *fname)
+{
+ ssize_t r;
+ char buf[32];
+
+ *strrchr(fname, '/') = '\0';
+ r = readlinkat(dirfd, fname, buf, sizeof(buf));
+ if (r > 0 && r < 32) {
+ buf[r] = '\0';
+ return atoi(buf);
+ } else if (r >= 32) {
+ r = -1;
+ errno = ERANGE;
+ }
+
+ kplogerror("can't readlink '%s' to find patchlevel\n",
+ fname);
+ return -1;
+}
+
+static inline int
+storage_open_patch(kpatch_storage_t *storage,
+ const char *buildid,
+ struct kpatch_storage_patch* patch)
+{
+ char fname[96];
+ int i, rv;
+
+ for (i = 0; i < ARRAY_SIZE(pathtemplates); i++) {
+ sprintf(fname, pathtemplates[i], buildid);
+
+ rv = kpatch_openat_file(storage->patch_fd, fname, &patch->kpfile);
+ if (rv == 0) {
+ rv = patch_file_verify(&patch->kpfile);
+
+ if (rv < 0)
+ kpatch_close_file(&patch->kpfile);
+ else
+ rv = PATCH_FOUND;
+ break;
+ }
+ }
+
+ if (rv == PATCH_FOUND && i == PATCHLEVEL_TEMPLATE_NUM) {
+ rv = readlink_patchlevel(storage->patch_fd, fname);
+ if (rv < 0) {
+ rv = PATCH_OPEN_ERROR;
+ kpatch_close_file(&patch->kpfile);
+ } else {
+ patch->patchlevel = rv;
+ patch->kpfile.patch->user_level = patch->patchlevel;
+ rv = PATCH_FOUND;
+ }
+
+ }
+
+ return rv;
+}
+
+static inline int
+storage_stat_patch(kpatch_storage_t *storage,
+ const char *buildid,
+ struct kpatch_storage_patch* patch)
+{
+ char fname[96];
+ struct stat buf;
+ int i, rv;
+
+ for (i = 0; i < ARRAY_SIZE(pathtemplates); i++) {
+ sprintf(fname, pathtemplates[i], buildid);
+
+ rv = fstatat(storage->patch_fd, fname, &buf, /* flags */ 0);
+
+ if (rv == 0) {
+ rv = PATCH_FOUND;
+ patch->kpfile.size = buf.st_size;
+ break;
+ } else if (rv < 0 && errno == ENOENT) {
+ rv = PATCH_NOT_FOUND;
+ }
+ }
+
+ if (rv == PATCH_FOUND && i == PATCHLEVEL_TEMPLATE_NUM) {
+ rv = readlink_patchlevel(storage->patch_fd, fname);
+ if (rv < 0) {
+ rv = PATCH_OPEN_ERROR;
+ } else {
+ patch->patchlevel = rv;
+ rv = PATCH_FOUND;
+ }
+ }
+
+ return rv;
+}
+
+/*
+ * TODO(pboldin) I duplicate a lot of code kernel has for filesystems already.
+ * Should we avoid this caching at all?
+ */
+#define ERR_PATCH ((struct kpatch_storage_patch *)1)
+static struct kpatch_storage_patch *
+storage_get_patch(kpatch_storage_t *storage, const char *buildid,
+ int load)
+{
+ struct kpatch_storage_patch *patch = NULL;
+ struct rb_node *node;
+ int rv;
+
+ if (!storage->is_patch_dir) {
+ if (!strcmp(storage->patch.buildid, buildid)) {
+ return &storage->patch;
+ }
+ return NULL;
+ }
+
+ /* Look here, could be loaded already */
+ node = rb_search_node(&storage->tree, cmp_buildid,
+ (unsigned long)buildid);
+ if (node != NULL)
+ return rb_entry(node, struct kpatch_storage_patch, node);
+
+ /* OK, look at the filesystem */
+ patch = malloc(sizeof(*patch));
+ if (patch == NULL)
+ return ERR_PATCH;
+
+ memset(patch, 0, sizeof(*patch));
+ patch->patchlevel = -1;
+ init_kp_file(&patch->kpfile);
+
+ if (load)
+ rv = storage_open_patch(storage, buildid, patch);
+ else
+ rv = storage_stat_patch(storage, buildid, patch);
+
+ if (rv == PATCH_OPEN_ERROR) {
+ free(patch);
+ return ERR_PATCH;
+ }
+
+ strcpy(patch->buildid, buildid);
+
+ rb_insert_node(&storage->tree,
+ &patch->node,
+ cmp_buildid,
+ (unsigned long)patch->buildid);
+
+ return patch;
+}
+
+int storage_patch_found(struct kpatch_storage_patch *patch)
+{
+ return patch && patch->kpfile.size >= 0;
+}
+
+static int
+storage_load_patch(kpatch_storage_t *storage, const char *buildid,
+ struct kp_file **pkpfile)
+{
+ struct kpatch_storage_patch *patch = NULL;
+
+ if (pkpfile == NULL) {
+ kperr("pkpfile == NULL\n");
+ return PATCH_OPEN_ERROR;
+ }
+
+ patch = storage_get_patch(storage, buildid, /* load */ 1);
+ if (patch == ERR_PATCH)
+ return PATCH_OPEN_ERROR;
+ if (patch == NULL)
+ return PATCH_NOT_FOUND;
+
+ *pkpfile = &patch->kpfile;
+
+ return storage_patch_found(patch) ? PATCH_FOUND : PATCH_NOT_FOUND;
+}
+
+int storage_have_patch(kpatch_storage_t *storage, const char *buildid,
+ struct kpatch_storage_patch **ppatch)
+{
+ struct kpatch_storage_patch *patch = NULL;
+
+ if (ppatch)
+ *ppatch = NULL;
+
+ patch = storage_get_patch(storage, buildid, /* load */ 0);
+ if (patch == ERR_PATCH)
+ return PATCH_OPEN_ERROR;
+
+ if (!storage_patch_found(patch))
+ return PATCH_NOT_FOUND;
+
+ if (ppatch)
+ *ppatch = patch;
+ return PATCH_FOUND;
+}
+
+char *storage_get_description(kpatch_storage_t *storage,
+ struct kpatch_storage_patch *patch)
+{
+ char *desc = NULL;
+ char path[PATH_MAX];
+ int fd, rv, alloc = 0, sz = 0;
+
+ if (!storage->is_patch_dir)
+ return NULL;
+
+ if (patch->desc)
+ return patch->desc;
+
+ sprintf(path, "%s/%d/description", patch->buildid, patch->patchlevel);
+ fd = openat(storage->patch_fd, path, O_RDONLY);
+ if (fd == -1)
+ return NULL;
+
+ while (1) {
+ if (sz + 1024 >= alloc) {
+ char *olddesc = desc;
+ alloc += PAGE_SIZE;
+
+ desc = malloc(alloc);
+
+ if (olddesc != NULL) {
+ memcpy(desc, olddesc, sz);
+ free(olddesc);
+ }
+
+ olddesc = desc;
+ }
+
+ rv = read(fd, desc + sz, alloc - sz);
+ if (rv == -1 && errno == EINTR)
+ continue;
+
+ if (rv == -1)
+ goto err_free;
+
+ if (rv == 0)
+ break;
+
+ sz += rv;
+ }
+
+ patch->desc = desc;
+
+ return desc;
+
+err_free:
+ free(desc);
+ close(fd);
+ return NULL;
+}
+
+int storage_lookup_patches(kpatch_storage_t *storage, kpatch_process_t *proc)
+{
+ struct kp_file *pkpfile;
+ struct object_file *o;
+ const char *bid;
+ int found = 0, ret;
+
+ list_for_each_entry(o, &proc->objs, list) {
+ if (!o->is_elf || is_kernel_object_name(o->name))
+ continue;
+
+ bid = kpatch_get_buildid(o);
+ if (bid == NULL) {
+ kpinfo("can't get buildid for %s\n",
+ o->name);
+ continue;
+ }
+
+ ret = storage_load_patch(storage, bid, &pkpfile);
+ if (ret == PATCH_OPEN_ERROR) {
+ if (errno != ENOENT)
+ kplogerror("error finding patch for %s (%s)\n",
+ o->name, bid);
+ continue;
+ }
+
+ if (ret == PATCH_FOUND) {
+ o->skpfile = pkpfile;
+ found++;
+ }
+ }
+
+ kpinfo("%d object(s) have valid patch(es)\n", found);
+
+ kpdebug("Object files dump:\n");
+ list_for_each_entry(o, &proc->objs, list)
+ kpatch_object_dump(o);
+
+ return found;
+}
+
+static int
+storage_execute_script(kpatch_storage_t *storage,
+ kpatch_process_t *proc,
+ const char *name)
+{
+ int childpid, rv = 0, status;
+ char pidbuf[16], pathbuf[PATH_MAX];
+
+ if (!storage->is_patch_dir)
+ return 0;
+
+ sprintf(pathbuf, "%s/%s", storage->path, name);
+
+ rv = access(pathbuf, X_OK);
+ /* No file -- no problems */
+ if (rv < 0)
+ return errno == ENOENT ? 0 : -1;
+
+ sprintf(pidbuf, "%d", proc->pid);
+
+ childpid = fork();
+ if (childpid == 0) {
+ rv = execl(pathbuf, name, pidbuf, NULL);
+ if (rv < 0)
+ kplogerror("execl failed\n");
+ exit(EXIT_FAILURE);
+ } else {
+ rv = waitpid(childpid, &status, 0);
+ if (rv < 0)
+ kplogerror("waitpid failed for %d\n", childpid);
+
+ if (WIFEXITED(status))
+ rv = WEXITSTATUS(status);
+ else if (WIFSIGNALED(status))
+ rv = WTERMSIG(status);
+ if (rv)
+ kperr("child script failed %d\n", rv);
+ }
+
+ return -rv;
+}
+
+int storage_execute_before_script(kpatch_storage_t *storage, kpatch_process_t *proc)
+{
+ return storage_execute_script(storage, proc, "before");
+}
+
+int storage_execute_after_script(kpatch_storage_t *storage, kpatch_process_t *proc)
+{
+ return storage_execute_script(storage, proc, "after");
+}
+
diff --git a/src/kpatch_storage.h b/src/kpatch_storage.h
new file mode 100644
index 0000000..eb8064b
--- /dev/null
+++ b/src/kpatch_storage.h
@@ -0,0 +1,66 @@
+#ifndef __KPATCH_STORAGE__
+#define __KPATCH_STORAGE__
+
+#include "kpatch_common.h"
+#include "kpatch_file.h"
+#include "kpatch_process.h"
+#include "rbtree.h"
+
+struct kpatch_storage_patch {
+ /* Pointer to the object's patch (if any) */
+ struct kp_file kpfile;
+
+ /* Build id kept here for negative caching */
+ char buildid[41];
+
+ /* Patch level */
+ int patchlevel;
+
+ /* Description cache */
+ char *desc;
+
+ /* Node for rb_root */
+ struct rb_node node;
+};
+
+struct kpatch_storage {
+ /* Patch storage path */
+ char *path;
+
+ /* Patch file (or directory) descriptor */
+ int patch_fd;
+
+ /* Is patch_fd a directory or a file? */
+ char is_patch_dir;
+
+ union {
+ /* Tree with BuildID keyed `kp_file's,
+ * is_patch_dir = 1 */
+ struct rb_root tree;
+
+ /* A single file, is_patch_dir = 0 */
+ struct kpatch_storage_patch patch;
+ };
+};
+
+typedef struct kpatch_storage kpatch_storage_t;
+
+int storage_init(kpatch_storage_t *storage,
+ const char *fname);
+void storage_free(kpatch_storage_t *storage);
+
+enum {
+ PATCH_OPEN_ERROR = -1,
+ PATCH_NOT_FOUND = 0,
+ PATCH_FOUND = 1,
+};
+int storage_lookup_patches(kpatch_storage_t *storage, kpatch_process_t *proc);
+int storage_have_patch(kpatch_storage_t *storage, const char *buildid,
+ struct kpatch_storage_patch **ppatch);
+int storage_patch_found(struct kpatch_storage_patch *patch);
+int storage_execute_before_script(kpatch_storage_t *storage, kpatch_process_t *proc);
+int storage_execute_after_script(kpatch_storage_t *storage, kpatch_process_t *proc);
+char *storage_get_description(kpatch_storage_t *storage,
+ struct kpatch_storage_patch *patch);
+
+#endif
diff --git a/src/kpatch_user.c b/src/kpatch_user.c
index 21a90ab..672b4d4 100644
--- a/src/kpatch_user.c
+++ b/src/kpatch_user.c
@@ -19,6 +19,7 @@
#include <libunwind-ptrace.h>
#include "kpatch_user.h"
+#include "kpatch_storage.h"
#include "kpatch_process.h"
#include "kpatch_file.h"
#include "kpatch_common.h"
@@ -41,487 +42,6 @@ typedef int (callback_t)(int pid, void *data);
static int
processes_do(int pid, callback_t callback, void *data);
-/*****************************************************************************
- * Patch storage subroutines.
- ****************************************************************************/
-
-static int
-patch_file_verify(struct kp_file *kpfile)
-{
- GElf_Ehdr *hdr;
- struct kpatch_file *k = kpfile->patch;
- ssize_t size = kpfile->size;
-
- kpdebug("Verifying patch for '%s'...", k->modulename);
-
- if (memcmp(k->magic, KPATCH_FILE_MAGIC1, sizeof(k->magic))) {
- kperr("'%s' patch is invalid: Invalid magic.\n",
- k->modulename);
- return -1;
- }
- if (k->total_size > size) {
- kperr("'%s' patch is invalid: Invalid size: %u/%ld.\n",
- k->modulename, k->total_size, size);
- return -1;
- }
- hdr = (void *)k + k->kpatch_offset;
- if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) ||
- hdr->e_type != ET_REL ||
- hdr->e_shentsize != sizeof(GElf_Shdr)) {
- kperr("'%s' patch is invalid: Wrong ELF header or not ET_REL\n",
- k->modulename);
- return -1;
- }
- kpdebug("OK\n");
- return 1;
-}
-
-static int
-storage_init(kpatch_storage_t *storage,
- const char *fname)
-{
- int patch_fd = -1;
- struct stat stat = { .st_mode = 0 };
-
- if (fname != NULL) {
- patch_fd = open(fname, O_RDONLY | O_CLOEXEC);
- if (patch_fd < 0)
- goto out_err;
-
- if (fstat(patch_fd, &stat) < 0)
- goto out_close;
- }
-
- storage->patch_fd = patch_fd;
- storage->is_patch_dir = S_ISDIR(stat.st_mode);
- storage->path = NULL;
-
- if (storage->is_patch_dir) {
- rb_init(&storage->tree);
- } else {
- int ret;
-
- ret = kpatch_open_fd(storage->patch_fd, &storage->patch.kpfile);
- if (ret < 0)
- goto out_close;
-
- ret = patch_file_verify(&storage->patch.kpfile);
- if (ret < 0) {
- kpatch_close_file(&storage->patch.kpfile);
- goto out_close;
- }
- strcpy(storage->patch.buildid, storage->patch.kpfile.patch->uname);
- }
-
- storage->path = strdup(fname);
-
- return 0;
-
-out_close:
- close(patch_fd);
-out_err:
- kplogerror("cannot open storage '%s'\n", fname);
- return -1;
-}
-
-static void
-free_storage_patch_cb(struct rb_node *node)
-{
- struct kpatch_storage_patch *patch;
-
- patch = rb_entry(node, struct kpatch_storage_patch, node);
- kpatch_close_file(&patch->kpfile);
-
- free(patch->desc);
- free(patch);
-}
-
-static void
-storage_free(kpatch_storage_t *storage)
-{
- close(storage->patch_fd);
- if (storage->is_patch_dir)
- rb_destroy(&storage->tree, free_storage_patch_cb);
- free(storage->path);
-}
-
-static int
-cmp_buildid(struct rb_node *node, unsigned long key)
-{
- const char *bid = (const char *)key;
- struct kpatch_storage_patch *patch;
-
- patch = rb_entry(node, struct kpatch_storage_patch, node);
-
- return strcmp(patch->buildid, bid);
-}
-
-#define PATCHLEVEL_TEMPLATE_NUM 0
-
-static char *pathtemplates[] = {
- "%s/latest/kpatch.bin",
- "%s.kpatch"
-};
-
-static int
-readlink_patchlevel(int dirfd, const char *fname)
-{
- ssize_t r;
- char buf[32];
-
- *strrchr(fname, '/') = '\0';
- r = readlinkat(dirfd, fname, buf, sizeof(buf));
- if (r > 0 && r < 32) {
- buf[r] = '\0';
- return atoi(buf);
- } else if (r >= 32) {
- r = -1;
- errno = ERANGE;
- }
-
- kplogerror("can't readlink '%s' to find patchlevel\n",
- fname);
- return -1;
-}
-
-enum {
- PATCH_OPEN_ERROR = -1,
- PATCH_NOT_FOUND = 0,
- PATCH_FOUND = 1,
-};
-
-static inline int
-storage_open_patch(kpatch_storage_t *storage,
- const char *buildid,
- struct kpatch_storage_patch* patch)
-{
- char fname[96];
- int i, rv;
-
- for (i = 0; i < ARRAY_SIZE(pathtemplates); i++) {
- sprintf(fname, pathtemplates[i], buildid);
-
- rv = kpatch_openat_file(storage->patch_fd, fname, &patch->kpfile);
- if (rv == 0) {
- rv = patch_file_verify(&patch->kpfile);
-
- if (rv < 0)
- kpatch_close_file(&patch->kpfile);
- else
- rv = PATCH_FOUND;
- break;
- }
- }
-
- if (rv == PATCH_FOUND && i == PATCHLEVEL_TEMPLATE_NUM) {
- rv = readlink_patchlevel(storage->patch_fd, fname);
- if (rv < 0) {
- rv = PATCH_OPEN_ERROR;
- kpatch_close_file(&patch->kpfile);
- } else {
- patch->patchlevel = rv;
- patch->kpfile.patch->user_level = patch->patchlevel;
- rv = PATCH_FOUND;
- }
-
- }
-
- return rv;
-}
-
-static inline int
-storage_stat_patch(kpatch_storage_t *storage,
- const char *buildid,
- struct kpatch_storage_patch* patch)
-{
- char fname[96];
- struct stat buf;
- int i, rv;
-
- for (i = 0; i < ARRAY_SIZE(pathtemplates); i++) {
- sprintf(fname, pathtemplates[i], buildid);
-
- rv = fstatat(storage->patch_fd, fname, &buf, /* flags */ 0);
-
- if (rv == 0) {
- rv = PATCH_FOUND;
- patch->kpfile.size = buf.st_size;
- break;
- } else if (rv < 0 && errno == ENOENT) {
- rv = PATCH_NOT_FOUND;
- }
- }
-
- if (rv == PATCH_FOUND && i == PATCHLEVEL_TEMPLATE_NUM) {
- rv = readlink_patchlevel(storage->patch_fd, fname);
- if (rv < 0) {
- rv = PATCH_OPEN_ERROR;
- } else {
- patch->patchlevel = rv;
- rv = PATCH_FOUND;
- }
- }
-
- return rv;
-}
-
-/*
- * TODO(pboldin) I duplicate a lot of code kernel has for filesystems already.
- * Should we avoid this caching at all?
- */
-#define ERR_PATCH ((struct kpatch_storage_patch *)1)
-static struct kpatch_storage_patch *
-storage_get_patch(kpatch_storage_t *storage, const char *buildid,
- int load)
-{
- struct kpatch_storage_patch *patch = NULL;
- struct rb_node *node;
- int rv;
-
- if (!storage->is_patch_dir) {
- if (!strcmp(storage->patch.buildid, buildid)) {
- return &storage->patch;
- }
- return NULL;
- }
-
- /* Look here, could be loaded already */
- node = rb_search_node(&storage->tree, cmp_buildid,
- (unsigned long)buildid);
- if (node != NULL)
- return rb_entry(node, struct kpatch_storage_patch, node);
-
- /* OK, look at the filesystem */
- patch = malloc(sizeof(*patch));
- if (patch == NULL)
- return ERR_PATCH;
-
- memset(patch, 0, sizeof(*patch));
- patch->patchlevel = -1;
- init_kp_file(&patch->kpfile);
-
- if (load)
- rv = storage_open_patch(storage, buildid, patch);
- else
- rv = storage_stat_patch(storage, buildid, patch);
-
- if (rv == PATCH_OPEN_ERROR) {
- free(patch);
- return ERR_PATCH;
- }
-
- strcpy(patch->buildid, buildid);
-
- rb_insert_node(&storage->tree,
- &patch->node,
- cmp_buildid,
- (unsigned long)patch->buildid);
-
- return patch;
-}
-
-static int
-storage_patch_found(struct kpatch_storage_patch *patch)
-{
- return patch && patch->kpfile.size >= 0;
-}
-
-static int
-storage_load_patch(kpatch_storage_t *storage, const char *buildid,
- struct kp_file **pkpfile)
-{
- struct kpatch_storage_patch *patch = NULL;
-
- if (pkpfile == NULL) {
- kperr("pkpfile == NULL\n");
- return PATCH_OPEN_ERROR;
- }
-
- patch = storage_get_patch(storage, buildid, /* load */ 1);
- if (patch == ERR_PATCH)
- return PATCH_OPEN_ERROR;
- if (patch == NULL)
- return PATCH_NOT_FOUND;
-
- *pkpfile = &patch->kpfile;
-
- return storage_patch_found(patch) ? PATCH_FOUND : PATCH_NOT_FOUND;
-}
-
-static int
-storage_have_patch(kpatch_storage_t *storage, const char *buildid,
- struct kpatch_storage_patch **ppatch)
-{
- struct kpatch_storage_patch *patch = NULL;
-
- if (ppatch)
- *ppatch = NULL;
-
- patch = storage_get_patch(storage, buildid, /* load */ 0);
- if (patch == ERR_PATCH)
- return PATCH_OPEN_ERROR;
-
- if (!storage_patch_found(patch))
- return PATCH_NOT_FOUND;
-
- if (ppatch)
- *ppatch = patch;
- return PATCH_FOUND;
-}
-
-static char *
-storage_get_description(kpatch_storage_t *storage,
- struct kpatch_storage_patch *patch)
-{
- char *desc = NULL;
- char path[PATH_MAX];
- int fd, rv, alloc = 0, sz = 0;
-
- if (!storage->is_patch_dir)
- return NULL;
-
- if (patch->desc)
- return patch->desc;
-
- sprintf(path, "%s/%d/description", patch->buildid, patch->patchlevel);
- fd = openat(storage->patch_fd, path, O_RDONLY);
- if (fd == -1)
- return NULL;
-
- while (1) {
- if (sz + 1024 >= alloc) {
- char *olddesc = desc;
- alloc += PAGE_SIZE;
-
- desc = malloc(alloc);
-
- if (olddesc != NULL) {
- memcpy(desc, olddesc, sz);
- free(olddesc);
- }
-
- olddesc = desc;
- }
-
- rv = read(fd, desc + sz, alloc - sz);
- if (rv == -1 && errno == EINTR)
- continue;
-
- if (rv == -1)
- goto err_free;
-
- if (rv == 0)
- break;
-
- sz += rv;
- }
-
- patch->desc = desc;
-
- return desc;
-
-err_free:
- free(desc);
- close(fd);
- return NULL;
-}
-
-static int
-storage_lookup_patches(kpatch_storage_t *storage, kpatch_process_t *proc)
-{
- struct kp_file *pkpfile;
- struct object_file *o;
- const char *bid;
- int found = 0, ret;
-
- list_for_each_entry(o, &proc->objs, list) {
- if (!o->is_elf || is_kernel_object_name(o->name))
- continue;
-
- bid = kpatch_get_buildid(o);
- if (bid == NULL) {
- kpinfo("can't get buildid for %s\n",
- o->name);
- continue;
- }
-
- ret = storage_load_patch(storage, bid, &pkpfile);
- if (ret == PATCH_OPEN_ERROR) {
- if (errno != ENOENT)
- kplogerror("error finding patch for %s (%s)\n",
- o->name, bid);
- continue;
- }
-
- if (ret == PATCH_FOUND) {
- o->skpfile = pkpfile;
- found++;
- }
- }
-
- kpinfo("%d object(s) have valid patch(es)\n", found);
-
- kpdebug("Object files dump:\n");
- list_for_each_entry(o, &proc->objs, list)
- kpatch_object_dump(o);
-
- return found;
-}
-
-static int
-storage_execute_script(kpatch_storage_t *storage,
- kpatch_process_t *proc,
- const char *name)
-{
- int childpid, rv = 0, status;
- char pidbuf[16], pathbuf[PATH_MAX];
-
- if (!storage->is_patch_dir)
- return 0;
-
- sprintf(pathbuf, "%s/%s", storage->path, name);
-
- rv = access(pathbuf, X_OK);
- /* No file -- no problems */
- if (rv < 0)
- return errno == ENOENT ? 0 : -1;
-
- sprintf(pidbuf, "%d", proc->pid);
-
- childpid = fork();
- if (childpid == 0) {
- rv = execl(pathbuf, name, pidbuf, NULL);
- if (rv < 0)
- kplogerror("execl failed\n");
- exit(EXIT_FAILURE);
- } else {
- rv = waitpid(childpid, &status, 0);
- if (rv < 0)
- kplogerror("waitpid failed for %d\n", childpid);
-
- if (WIFEXITED(status))
- rv = WEXITSTATUS(status);
- else if (WIFSIGNALED(status))
- rv = WTERMSIG(status);
- if (rv)
- kperr("child script failed %d\n", rv);
- }
-
- return -rv;
-}
-
-static int
-storage_execute_before_script(kpatch_storage_t *storage, kpatch_process_t *proc)
-{
- return storage_execute_script(storage, proc, "before");
-}
-
-static int
-storage_execute_after_script(kpatch_storage_t *storage, kpatch_process_t *proc)
-{
- return storage_execute_script(storage, proc, "after");
-}
-
enum {
ACTION_APPLY_PATCH,
ACTION_UNAPPLY_PATCH
diff --git a/src/kpatch_user.h b/src/kpatch_user.h
index 091c463..c0b52ff 100644
--- a/src/kpatch_user.h
+++ b/src/kpatch_user.h
@@ -5,45 +5,6 @@
#include "kpatch_file.h"
#include "rbtree.h"
-struct kpatch_storage_patch {
- /* Pointer to the object's patch (if any) */
- struct kp_file kpfile;
-
- /* Build id kept here for negative caching */
- char buildid[41];
-
- /* Patch level */
- int patchlevel;
-
- /* Description cache */
- char *desc;
-
- /* Node for rb_root */
- struct rb_node node;
-};
-
-struct kpatch_storage {
- /* Patch storage path */
- char *path;
-
- /* Patch file (or directory) descriptor */
- int patch_fd;
-
- /* Is patch_fd a directory or a file? */
- char is_patch_dir;
-
- union {
- /* Tree with BuildID keyed `kp_file's,
- * is_patch_dir = 1 */
- struct rb_root tree;
-
- /* A single file, is_patch_dir = 0 */
- struct kpatch_storage_patch patch;
- };
-};
-
-typedef struct kpatch_storage kpatch_storage_t;
-
int cmd_patch_user(int argc, char *argv[]);
int cmd_unpatch_user(int argc, char *argv[]);
--
2.23.0
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化