From 6dcc8153aaad9b19ab52f036ce3b54037544a465 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mariusz=20Koz=C5=82owski?= Date: Mon, 15 Jul 2024 16:00:41 +0200 Subject: [PATCH] Add memcr version info git describe output should be good enough. --- Makefile | 6 ++++-- memcr-client.c | 16 +++++++++++++--- memcr.c | 18 +++++++++++++++--- memcr.h | 4 ++++ 4 files changed, 36 insertions(+), 8 deletions(-) diff --git a/Makefile b/Makefile index 2dcf394..b00793a 100644 --- a/Makefile +++ b/Makefile @@ -104,6 +104,8 @@ endif PCFLAGS += -Wstrict-prototypes -fno-stack-protector -fpie -nostdlib -ffreestanding -fomit-frame-pointer -Wa,--noexecstack +GIT_VERSION = $(shell git describe --dirty 2>/dev/null | sed 's/^v//' || echo "unknown") + GOFF = ./gen-offsets.sh B ?= . @@ -141,7 +143,7 @@ $(B)/cpu.o: arch/$(ARCH)/cpu.c $(CC) $(MCFLAGS) -c $^ -o $@ $(B)/memcr.o: memcr.c $(B)/parasite-blob.h - $(CC) $(MCFLAGS) -I$(B) -c $< -o $@ + $(CC) $(MCFLAGS) -DGIT_VERSION='"$(GIT_VERSION)"' -I$(B) -c $< -o $@ $(B)/memcr: $(B)/memcr.o $(B)/cpu.o $(B)/enter.o $(CC) $(MCFLAGS) $^ $(LDFLAGS) -o $@ @@ -150,7 +152,7 @@ $(B)/memcr: $(B)/memcr.o $(B)/cpu.o $(B)/enter.o $(B)/memcr-client.o: memcr-client.c - $(CC) $(CFLAGS) -I$(B) -c $< -o $@ + $(CC) $(CFLAGS) -DGIT_VERSION='"$(GIT_VERSION)"' -I$(B) -c $< -o $@ $(B)/memcr-client: $(B)/memcr-client.o $(CC) $(CFLAGS) $^ -o $@ diff --git a/memcr-client.c b/memcr-client.c index c1f73d5..3fccbc1 100644 --- a/memcr-client.c +++ b/memcr-client.c @@ -91,17 +91,23 @@ static int send_cmd(int cd, struct service_command cmd) return resp.resp_code; } +static void print_version(void) +{ + fprintf(stdout, "[i] memcr-client version %s\n", GIT_VERSION); +} + static void usage(const char *name, int status) { fprintf(status ? stderr : stdout, - "%s -l PORT|PATH -p PID [-c -r]\n" \ + "%s -l PORT|PATH -p PID [-c -r] [-V]\n" \ "options: \n" \ " -h --help\t\thelp\n" \ " -l --location\t\tTCP port number of localhost memcr service\n" \ "\t\t\t or filesystem path to memcr service UNIX socket\n" \ " -p --pid\t\tprocess ID to be checkpointed / restored\n" \ " -c --checkpoint\tsend checkpoint command to memcr service\n" \ - " -r --restore\t\tsend restore command to memcr service\n", + " -r --restore\t\tsend restore command to memcr service\n" \ + " -V --version\t\tprint version and exit\n", name); exit(status); } @@ -123,10 +129,11 @@ int main(int argc, char *argv[]) { "pid", 1, 0, 'p'}, { "checkpoint", 0, 0, 'c'}, { "restore", 0, 0, 'r'}, + { "version", 0, 0, 'V'}, { NULL, 0, 0, 0 } }; - while ((opt = getopt_long(argc, argv, "hl:p:cr", long_options, &option_index)) != -1) { + while ((opt = getopt_long(argc, argv, "hl:p:crV", long_options, &option_index)) != -1) { switch (opt) { case 'h': usage(argv[0], 0); @@ -143,6 +150,9 @@ int main(int argc, char *argv[]) case 'r': restore = 1; break; + case 'V': + print_version(); + exit(0); default: /* '?' */ usage(argv[0], 1); break; diff --git a/memcr.c b/memcr.c index b66ffbb..264912f 100644 --- a/memcr.c +++ b/memcr.c @@ -2833,10 +2833,15 @@ static int user_interactive_mode(pid_t pid) return ret; } +static void print_version(void) +{ + fprintf(stdout, "[i] memcr version %s\n", GIT_VERSION); +} + static void usage(const char *name, int status) { fprintf(status ? stderr : stdout, - "%s [-h] [-p PID] [-d DIR] [-S DIR] [-l PORT|PATH] [-n] [-m] [-f] [-z] [-c] [-e]\n" \ + "%s [-h] [-p PID] [-d DIR] [-S DIR] [-l PORT|PATH] [-n] [-m] [-f] [-z] [-c] [-e] [-V]\n" \ "options:\n" \ " -h --help help\n" \ " -p --pid target processs pid\n" \ @@ -2854,7 +2859,8 @@ static void usage(const char *name, int status) " -z --compress compress memory dump\n" \ " -c --checksum enable md5 checksum for memory dump\n" \ " -e --encrypt enable encryption of memory dump\n" \ - " -t --timeout timeout in seconds for checkpoint/restore execution in service mode\n", + " -t --timeout timeout in seconds for checkpoint/restore execution in service mode\n" \ + " -v --version print version and exit\n", name); exit(status); @@ -2895,6 +2901,7 @@ int main(int argc, char *argv[]) { "checksum", 0, NULL, 'c'}, { "encrypt", 2, 0, 'e'}, { "timeout", 1, 0, 't'}, + { "version", 0, 0, 'V'}, { NULL, 0, NULL, 0 } }; @@ -2902,7 +2909,7 @@ int main(int argc, char *argv[]) parasite_socket_dir = NULL; parasite_socket_use_netns = 0; - while ((opt = getopt_long(argc, argv, "hp:d:S:Nl:nmfzce::t:", long_options, &option_index)) != -1) { + while ((opt = getopt_long(argc, argv, "hp:d:S:Nl:nmfzce::t:V", long_options, &option_index)) != -1) { switch (opt) { case 'h': usage(argv[0], 0); @@ -2954,6 +2961,9 @@ int main(int argc, char *argv[]) case 't': timeout = atoi(optarg); break; + case 'V': + print_version(); + exit(0); default: /* '?' */ usage(argv[0], 1); } @@ -2965,6 +2975,8 @@ int main(int argc, char *argv[]) if (pid <= 0 && !listen_location) die("pid must be > 0\n"); + print_version(); + ret = access("/proc/self/pagemap", F_OK); if (ret) die("/proc/self/pagemap not present (depends on CONFIG_PROC_PAGE_MONITOR)\n"); diff --git a/memcr.h b/memcr.h index 05dce41..6ce8001 100644 --- a/memcr.h +++ b/memcr.h @@ -22,6 +22,10 @@ #include #include +#ifndef GIT_VERSION +#define GIT_VERSION "unknown" +#endif + #ifndef PAGE_SIZE #define PAGE_SIZE 4096 #endif