From db528a5d90415ec209aec34b1fa8e76850e7a954 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Wed, 13 May 2026 22:18:16 +0200 Subject: First commit MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- .gitignore | 17 +++++++++++++++++ Makefile | 37 +++++++++++++++++++++++++++++++++++++ config.mk | 8 ++++++++ how-to-update-process.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 104 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 config.mk create mode 100644 how-to-update-process.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..dac55ff --- /dev/null +++ b/.gitignore @@ -0,0 +1,17 @@ +*\#* +*~ +*.o +*.a +*.t +*.lo +*.to +*.su +*.so +*.so.* +*.dll +*.dylib +*.gch +*.gcov +*.gcno +*.gcda +/how-to-update-process diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..97aef18 --- /dev/null +++ b/Makefile @@ -0,0 +1,37 @@ +.POSIX: + +CONFIGFILE = config.mk +include $(CONFIGFILE) + +OBJ =\ + how-to-update-process.o + +HDR = + +all: how-to-update-process +$(OBJ): $(HDR) + +.c.o: + $(CC) -c -o $@ $< $(CFLAGS) $(CPPFLAGS) + +how-to-update-process: $(OBJ) + $(CC) -o $@ $(OBJ) $(LDFLAGS) + +install: how-to-update-process + mkdir -p -- "$(DESTDIR)$(PREFIX)/bin" + mkdir -p -- "$(DESTDIR)$(MANPREFIX)/man1/" + cp -- how-to-update-process "$(DESTDIR)$(PREFIX)/bin/" + cp -- how-to-update-process.1 "$(DESTDIR)$(MANPREFIX)/man1/" + +uninstall: + -rm -f -- "$(DESTDIR)$(PREFIX)/bin/how-to-update-process" + -rm -f -- "$(DESTDIR)$(MANPREFIX)/man1/how-to-update-process.1" + +clean: + -rm -f -- *.o *.a *.lo *.su *.so *.so.* *.gch *.gcov *.gcno *.gcda + -rm -f -- how-to-update-process + +.SUFFIXES: +.SUFFIXES: .o .c + +.PHONY: all install uninstall clean diff --git a/config.mk b/config.mk new file mode 100644 index 0000000..f4adf12 --- /dev/null +++ b/config.mk @@ -0,0 +1,8 @@ +PREFIX = /usr +MANPREFIX = $(PREFIX)/share/man + +CC = c99 + +CPPFLAGS = -D_DEFAULT_SOURCE -D_BSD_SOURCE -D_XOPEN_SOURCE=700 -D_GNU_SOURCE +CFLAGS = +LDFLAGS = diff --git a/how-to-update-process.c b/how-to-update-process.c new file mode 100644 index 0000000..6328ecf --- /dev/null +++ b/how-to-update-process.c @@ -0,0 +1,42 @@ +#include +#include +#include +#include +#include +#include + + +static const char *text = "Change this text and recompile while the process is running"; + + +int +main(int argc, char *argv[]) +{ + uintptr_t exe_addr = (uintptr_t)getauxval(AT_EXECFN); + const char *exe; + + if (!exe_addr) { + fprintf(stderr, "%s\n", "Sorry, AT_EXECFN not found, would have to use /proc/self/exe, which is out of scope"); + /* by the way, if you just exec into /proc/self/exe + * you restart the process, to the same version as + * running even if it has been replaced (define + * RESTART to try it out) */ + return 1; + } + exe = (void *)exe_addr; /* beware, this might be a relative path */ + + if (argc != 2 || strcmp(argv[1], text)) + printf("%s\n", text); + + unlink(exe); + + for (;;) { + sleep(1); +#ifdef RESTART + execlp("/proc/self/exe", argv[0], text, NULL); + abort(); +#else + execlp(exe, argv[0], text, NULL); +#endif + } +} -- cgit v1.2.3-70-g09d2